]>
Commit | Line | Data |
---|---|---|
41abdfbd | 1 | /* Execute AIXcoff files, for GDB. |
e17960fb | 2 | Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc. |
41abdfbd JG |
3 | Derived from exec.c. Modified by IBM Corporation. |
4 | Donated by IBM Corporation and Cygnus Support. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | /* xcoff-exec - deal with executing XCOFF files. */ | |
23 | ||
d747e0af MT |
24 | #include "defs.h" |
25 | ||
41abdfbd JG |
26 | #include <sys/types.h> |
27 | #include <sys/param.h> | |
28 | #include <fcntl.h> | |
29 | #include <string.h> | |
30 | #include <ctype.h> | |
31 | #include <sys/stat.h> | |
32 | #include <sys/ldr.h> | |
33 | ||
41abdfbd JG |
34 | #include "frame.h" |
35 | #include "inferior.h" | |
36 | #include "target.h" | |
37 | #include "gdbcmd.h" | |
38 | #include "gdbcore.h" | |
39 | #include "symfile.h" | |
5e2e79f8 | 40 | #include "objfiles.h" |
41abdfbd JG |
41 | |
42 | #include "libbfd.h" /* BFD internals (sigh!) FIXME */ | |
2aefe6e4 | 43 | #include "bfd.h" |
1eeba686 | 44 | #include "xcoffsolib.h" |
41abdfbd | 45 | |
1ab3bf1b JG |
46 | /* Prototypes for local functions */ |
47 | ||
1ab3bf1b JG |
48 | static void |
49 | file_command PARAMS ((char *, int)); | |
50 | ||
51 | static void | |
52 | exec_close PARAMS ((int)); | |
53 | ||
2aefe6e4 JK |
54 | static struct vmap * |
55 | map_vmap PARAMS ((bfd *, bfd *)); | |
56 | ||
41abdfbd JG |
57 | struct section_table *exec_sections, *exec_sections_end; |
58 | ||
41abdfbd JG |
59 | /* Whether to open exec and core files read-only or read-write. */ |
60 | ||
61 | int write_files = 0; | |
62 | ||
818de002 PB |
63 | extern int info_verbose; |
64 | ||
41abdfbd JG |
65 | bfd *exec_bfd; /* needed by core.c */ |
66 | ||
67 | extern char *getenv(); | |
41abdfbd JG |
68 | extern void add_syms_addr_command (); |
69 | extern void symbol_file_command (); | |
70 | static void exec_files_info(); | |
1eeba686 PB |
71 | |
72 | struct vmap *vmap; /* current vmap */ | |
41abdfbd JG |
73 | |
74 | extern struct target_ops exec_ops; | |
75 | ||
76 | ||
77 | /* exec_close - done with exec file, clean up all resources. */ | |
78 | ||
1ab3bf1b | 79 | static void |
818de002 PB |
80 | exec_close(quitting) |
81 | { | |
82 | register struct vmap *vp, *nxt; | |
83 | struct objfile *obj; | |
84 | ||
85 | for (nxt = vmap; vp = nxt; ) | |
86 | { | |
87 | nxt = vp->nxt; | |
88 | ||
89 | /* if there is an objfile associated with this bfd, | |
90 | free_objfile() will do proper cleanup of objfile *and* bfd. */ | |
91 | ||
2aefe6e4 JK |
92 | if (vp->objfile) |
93 | free_objfile (vp->objfile); | |
818de002 PB |
94 | else |
95 | bfd_close(vp->bfd); | |
2aefe6e4 JK |
96 | |
97 | /* FIXME: This routine is #if 0'd in symfile.c. What should we | |
98 | be doing here? Should we just free everything in | |
99 | vp->objfile->symtabs? Should free_objfile do that? */ | |
818de002 PB |
100 | free_named_symtabs(vp->name); |
101 | free(vp); | |
102 | } | |
103 | ||
104 | vmap = 0; | |
507e4004 | 105 | |
c161de74 JK |
106 | /* exec_bfd was already closed (the exec file has a vmap entry). */ |
107 | exec_bfd = NULL; | |
108 | ||
507e4004 PB |
109 | if (exec_ops.to_sections) { |
110 | free (exec_ops.to_sections); | |
111 | exec_ops.to_sections = NULL; | |
112 | exec_ops.to_sections_end = NULL; | |
113 | } | |
41abdfbd JG |
114 | } |
115 | ||
116 | /* | |
117 | * exec_file_command - handle the "exec" command, &c. | |
118 | */ | |
119 | void | |
120 | exec_file_command(filename, from_tty) | |
121 | char *filename; | |
122 | { | |
41abdfbd | 123 | target_preopen(from_tty); |
507e4004 PB |
124 | |
125 | /* Remove any previous exec file. */ | |
41abdfbd JG |
126 | unpush_target(&exec_ops); |
127 | ||
128 | /* Now open and digest the file the user requested, if any. */ | |
129 | ||
130 | if (filename) { | |
131 | char *scratch_pathname; | |
132 | int scratch_chan; | |
133 | ||
134 | filename = tilde_expand(filename); | |
1ab3bf1b | 135 | make_cleanup (free, filename); |
41abdfbd | 136 | |
507e4004 PB |
137 | scratch_chan = openp(getenv("PATH"), 1, filename, |
138 | write_files? O_RDWR: O_RDONLY, 0, | |
139 | &scratch_pathname); | |
41abdfbd JG |
140 | if (scratch_chan < 0) |
141 | perror_with_name(filename); | |
142 | ||
507e4004 PB |
143 | exec_bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan); |
144 | if (!exec_bfd) | |
41abdfbd JG |
145 | error("Could not open `%s' as an executable file: %s" |
146 | , scratch_pathname, bfd_errmsg(bfd_error)); | |
147 | ||
148 | /* make sure we have an object file */ | |
149 | ||
507e4004 PB |
150 | if (!bfd_check_format(exec_bfd, bfd_object)) |
151 | error("\"%s\": not in executable format: %s.", | |
152 | scratch_pathname, bfd_errmsg(bfd_error)); | |
41abdfbd JG |
153 | |
154 | ||
155 | /* setup initial vmap */ | |
156 | ||
507e4004 | 157 | map_vmap (exec_bfd, 0); |
41abdfbd | 158 | if (!vmap) |
507e4004 PB |
159 | error("Can't find the file sections in `%s': %s", |
160 | exec_bfd->filename, bfd_errmsg(bfd_error)); | |
41abdfbd | 161 | |
507e4004 PB |
162 | if (build_section_table (exec_bfd, &exec_ops.to_sections, |
163 | &exec_ops.to_sections_end)) | |
41abdfbd JG |
164 | error ("Can't find the file sections in `%s': %s", |
165 | exec_bfd->filename, bfd_errmsg (bfd_error)); | |
166 | ||
167 | /* make sure core, if present, matches */ | |
168 | validate_files(); | |
169 | ||
170 | push_target(&exec_ops); | |
171 | ||
172 | /* Tell display code(if any) about the changed file name. */ | |
173 | ||
174 | if (exec_file_display_hook) | |
175 | (*exec_file_display_hook)(filename); | |
176 | } | |
177 | else { | |
178 | exec_close(0); /* just in case */ | |
179 | if (from_tty) | |
180 | printf("No exec file now.\n"); | |
181 | } | |
182 | } | |
183 | ||
184 | /* Set both the exec file and the symbol file, in one command. What a | |
185 | * novelty. Why did GDB go through four major releases before this | |
186 | * command was added? | |
187 | */ | |
1ab3bf1b | 188 | static void |
41abdfbd JG |
189 | file_command(arg, from_tty) |
190 | char *arg; { | |
191 | ||
192 | exec_file_command(arg, from_tty); | |
193 | symbol_file_command(arg, from_tty); | |
194 | } | |
195 | ||
196 | /* Locate all mappable sections of a BFD file. | |
197 | table_pp_char is a char * to get it through bfd_map_over_sections; | |
198 | we cast it back to its proper type. */ | |
199 | ||
1ab3bf1b | 200 | static void |
41abdfbd JG |
201 | add_to_section_table (abfd, asect, table_pp_char) |
202 | bfd *abfd; | |
203 | sec_ptr asect; | |
204 | char *table_pp_char; | |
205 | { | |
206 | struct section_table **table_pp = (struct section_table **)table_pp_char; | |
207 | flagword aflag; | |
208 | ||
209 | aflag = bfd_get_section_flags (abfd, asect); | |
210 | /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */ | |
211 | if (!(aflag & SEC_LOAD)) | |
212 | return; | |
507e4004 PB |
213 | if (0 == bfd_section_size (abfd, asect)) |
214 | return; | |
215 | (*table_pp)->bfd = abfd; | |
41abdfbd JG |
216 | (*table_pp)->sec_ptr = asect; |
217 | (*table_pp)->addr = bfd_section_vma (abfd, asect); | |
218 | (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); | |
219 | (*table_pp)++; | |
220 | } | |
221 | ||
222 | int | |
223 | build_section_table (some_bfd, start, end) | |
224 | bfd *some_bfd; | |
225 | struct section_table **start, **end; | |
226 | { | |
227 | unsigned count; | |
228 | ||
229 | count = bfd_count_sections (some_bfd); | |
230 | if (count == 0) | |
0848ad1c | 231 | fatal ("aborting"); /* return 1? */ |
41abdfbd JG |
232 | if (*start) |
233 | free (*start); | |
234 | *start = (struct section_table *) xmalloc (count * sizeof (**start)); | |
235 | *end = *start; | |
236 | bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); | |
237 | if (*end > *start + count) | |
0848ad1c | 238 | fatal ("aborting"); |
41abdfbd JG |
239 | /* We could realloc the table, but it probably loses for most files. */ |
240 | return 0; | |
241 | } | |
2aefe6e4 | 242 | \f |
41abdfbd | 243 | void |
359a097f | 244 | sex_to_vmap(bfd *bf, sec_ptr sex, PTR arg3) |
41abdfbd | 245 | { |
359a097f | 246 | struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *)arg3; |
41abdfbd JG |
247 | register struct vmap *vp, **vpp; |
248 | register struct symtab *syms; | |
249 | bfd *arch = vmap_bfd->pbfd; | |
250 | vp = vmap_bfd->pvmap; | |
251 | ||
252 | if ((bfd_get_section_flags(bf, sex) & SEC_LOAD) == 0) | |
253 | return; | |
254 | ||
2e4964ad | 255 | if (STREQ(bfd_section_name(bf, sex), ".text")) { |
41abdfbd JG |
256 | vp->tstart = 0; |
257 | vp->tend = vp->tstart + bfd_section_size(bf, sex); | |
258 | ||
507e4004 PB |
259 | /* When it comes to this adjustment value, in contrast to our previous |
260 | belief shared objects should behave the same as the main load segment. | |
261 | This is the offset from the beginning of text section to the first | |
262 | real instruction. */ | |
263 | ||
264 | vp->tadj = sex->filepos - bfd_section_vma(bf, sex); | |
41abdfbd JG |
265 | } |
266 | ||
2e4964ad | 267 | else if (STREQ(bfd_section_name(bf, sex), ".data")) { |
41abdfbd JG |
268 | vp->dstart = 0; |
269 | vp->dend = vp->dstart + bfd_section_size(bf, sex); | |
270 | } | |
271 | ||
2e4964ad | 272 | else if (STREQ(bfd_section_name(bf, sex), ".bss")) /* FIXMEmgo */ |
41abdfbd JG |
273 | printf ("bss section in exec! Don't know what the heck to do!\n"); |
274 | } | |
275 | ||
276 | /* Make a vmap for the BFD "bf", which might be a member of the archive | |
2aefe6e4 JK |
277 | BFD "arch". Return the new vmap. */ |
278 | struct vmap * | |
41abdfbd JG |
279 | map_vmap (bfd *bf, bfd *arch) |
280 | { | |
281 | struct vmap_and_bfd vmap_bfd; | |
282 | struct vmap *vp, **vpp; | |
283 | struct objfile *obj; | |
41abdfbd JG |
284 | |
285 | vp = (void*) xmalloc (sizeof (*vp)); | |
1eeba686 | 286 | bzero (vp, sizeof (*vp)); |
41abdfbd JG |
287 | vp->nxt = 0; |
288 | vp->bfd = bf; | |
289 | vp->name = bfd_get_filename(arch ? arch : bf); | |
290 | vp->member = arch ? bfd_get_filename(bf) : ""; | |
291 | ||
292 | vmap_bfd.pbfd = arch; | |
293 | vmap_bfd.pvmap = vp; | |
294 | bfd_map_over_sections (bf, sex_to_vmap, &vmap_bfd); | |
295 | ||
41abdfbd JG |
296 | /* find the end of the list, and append. */ |
297 | for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) | |
298 | ; | |
299 | *vpp = vp; | |
2aefe6e4 JK |
300 | |
301 | return vp; | |
41abdfbd JG |
302 | } |
303 | ||
507e4004 | 304 | |
41abdfbd JG |
305 | /* vmap_symtab - handle symbol translation on vmapping */ |
306 | ||
3c02636b JK |
307 | static void |
308 | vmap_symtab (vp) | |
309 | register struct vmap *vp; | |
41abdfbd | 310 | { |
1ab3bf1b | 311 | register struct objfile *objfile; |
3c02636b JK |
312 | asection *textsec; |
313 | asection *datasec; | |
314 | asection *bsssec; | |
315 | CORE_ADDR old_text_offset; | |
316 | struct section_offsets *new_offsets; | |
317 | int i; | |
1ab3bf1b | 318 | |
2aefe6e4 JK |
319 | objfile = vp->objfile; |
320 | if (objfile == NULL) | |
1ab3bf1b | 321 | { |
2aefe6e4 JK |
322 | /* OK, it's not an objfile we opened ourselves. |
323 | Currently, that can only happen with the exec file, so | |
324 | relocate the symbols for the symfile. */ | |
325 | if (symfile_objfile == NULL) | |
326 | return; | |
327 | objfile = symfile_objfile; | |
328 | } | |
cbf15d9a | 329 | |
3c02636b JK |
330 | new_offsets = alloca |
331 | (sizeof (struct section_offsets) | |
332 | + sizeof (new_offsets->offsets) * objfile->num_sections); | |
556f3d90 | 333 | |
3c02636b JK |
334 | for (i = 0; i < objfile->num_sections; ++i) |
335 | ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i); | |
1ab3bf1b | 336 | |
3c02636b JK |
337 | textsec = bfd_get_section_by_name (vp->bfd, ".text"); |
338 | old_text_offset = ANOFFSET (objfile->section_offsets, textsec->target_index); | |
339 | ANOFFSET (new_offsets, textsec->target_index) = vp->tstart; | |
340 | datasec = bfd_get_section_by_name (vp->bfd, ".data"); | |
341 | ANOFFSET (new_offsets, datasec->target_index) = vp->dstart; | |
342 | bsssec = bfd_get_section_by_name (vp->bfd, ".bss"); | |
343 | ANOFFSET (new_offsets, bsssec->target_index) = vp->dstart; | |
344 | ||
345 | objfile_relocate (objfile, new_offsets); | |
346 | ||
347 | if (old_text_offset != ANOFFSET (new_offsets, textsec->target_index)) | |
348 | /* breakpoints need to be relocated as well. */ | |
349 | fixup_breakpoints (0, TEXT_SEGMENT_BASE, vp->tstart - old_text_offset); | |
41abdfbd JG |
350 | } |
351 | ||
8112a711 JK |
352 | /* Add symbols for an objfile. */ |
353 | static int | |
354 | objfile_symbol_add (arg) | |
355 | char *arg; | |
356 | { | |
357 | struct objfile *obj = (struct objfile *) arg; | |
358 | syms_from_objfile (obj, 0, 0, 0); | |
359 | new_symfile_objfile (obj, 0, 0); | |
360 | return 1; | |
361 | } | |
362 | ||
2aefe6e4 JK |
363 | static struct vmap *add_vmap PARAMS ((struct ld_info *)); |
364 | ||
365 | /* Add a new vmap entry based on ldinfo() information. | |
366 | ||
367 | If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a | |
368 | core file), the caller should set it to -1, and we will open the file. | |
369 | ||
370 | Return the vmap new entry. */ | |
371 | static struct vmap * | |
41abdfbd | 372 | add_vmap(ldi) |
2aefe6e4 JK |
373 | register struct ld_info *ldi; |
374 | { | |
41abdfbd | 375 | bfd *bfd, *last; |
507e4004 | 376 | register char *mem, *objname; |
2aefe6e4 JK |
377 | struct objfile *obj; |
378 | struct vmap *vp; | |
507e4004 PB |
379 | |
380 | /* This ldi structure was allocated using alloca() in | |
9b280a7f | 381 | xcoff_relocate_symtab(). Now we need to have persistent object |
507e4004 | 382 | and member names, so we should save them. */ |
41abdfbd JG |
383 | |
384 | mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1; | |
507e4004 PB |
385 | mem = savestring (mem, strlen (mem)); |
386 | objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename)); | |
387 | ||
2aefe6e4 JK |
388 | if (ldi->ldinfo_fd < 0) |
389 | /* Note that this opens it once for every member; a possible | |
390 | enhancement would be to only open it once for every object. */ | |
391 | bfd = bfd_openr (objname, NULL); | |
392 | else | |
393 | bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd); | |
41abdfbd | 394 | if (!bfd) |
507e4004 PB |
395 | error("Could not open `%s' as an executable file: %s", |
396 | objname, bfd_errmsg(bfd_error)); | |
41abdfbd JG |
397 | |
398 | ||
399 | /* make sure we have an object file */ | |
400 | ||
401 | if (bfd_check_format(bfd, bfd_object)) | |
2aefe6e4 | 402 | vp = map_vmap (bfd, 0); |
41abdfbd JG |
403 | |
404 | else if (bfd_check_format(bfd, bfd_archive)) { | |
405 | last = 0; | |
406 | /* | |
407 | * FIXME??? am I tossing BFDs? bfd? | |
408 | */ | |
409 | while (last = bfd_openr_next_archived_file(bfd, last)) | |
2e4964ad | 410 | if (STREQ(mem, last->filename)) |
41abdfbd JG |
411 | break; |
412 | ||
413 | if (!last) { | |
507e4004 PB |
414 | bfd_close(bfd); |
415 | /* FIXME -- should be error */ | |
416 | warning("\"%s\": member \"%s\" missing.", bfd->filename, mem); | |
417 | return; | |
41abdfbd JG |
418 | } |
419 | ||
420 | if (!bfd_check_format(last, bfd_object)) { | |
421 | bfd_close(last); /* XXX??? */ | |
422 | goto obj_err; | |
423 | } | |
424 | ||
2aefe6e4 | 425 | vp = map_vmap (last, bfd); |
41abdfbd JG |
426 | } |
427 | else { | |
428 | obj_err: | |
429 | bfd_close(bfd); | |
2aefe6e4 JK |
430 | error ("\"%s\": not in executable format: %s.", |
431 | objname, bfd_errmsg(bfd_error)); | |
432 | /*NOTREACHED*/ | |
41abdfbd | 433 | } |
2aefe6e4 JK |
434 | obj = allocate_objfile (vp->bfd, 0); |
435 | vp->objfile = obj; | |
436 | ||
437 | #ifndef SOLIB_SYMBOLS_MANUAL | |
8112a711 JK |
438 | if (catch_errors (objfile_symbol_add, (char *)obj, |
439 | "Error while reading shared library symbols:\n")) | |
440 | { | |
441 | /* Note this is only done if symbol reading was successful. */ | |
3c02636b | 442 | vmap_symtab (vp); |
8112a711 JK |
443 | vp->loaded = 1; |
444 | } | |
2aefe6e4 JK |
445 | #endif |
446 | return vp; | |
41abdfbd JG |
447 | } |
448 | ||
449 | ||
507e4004 PB |
450 | /* As well as symbol tables, exec_sections need relocation. After |
451 | the inferior process' termination, there will be a relocated symbol | |
452 | table exist with no corresponding inferior process. At that time, we | |
453 | need to use `exec' bfd, rather than the inferior process's memory space | |
454 | to look up symbols. | |
455 | ||
456 | `exec_sections' need to be relocated only once, as long as the exec | |
457 | file remains unchanged. | |
41abdfbd JG |
458 | */ |
459 | vmap_exec () | |
460 | { | |
461 | static bfd *execbfd; | |
1e939db1 SG |
462 | int i; |
463 | ||
41abdfbd JG |
464 | if (execbfd == exec_bfd) |
465 | return; | |
466 | ||
467 | execbfd = exec_bfd; | |
468 | ||
1e939db1 SG |
469 | if (!vmap || !exec_ops.to_sections) |
470 | error ("vmap_exec: vmap or exec_ops.to_sections == 0\n"); | |
507e4004 | 471 | |
1e939db1 SG |
472 | for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++) |
473 | { | |
2e4964ad | 474 | if (STREQ(".text", exec_ops.to_sections[i].sec_ptr->name)) |
1e939db1 SG |
475 | { |
476 | exec_ops.to_sections[i].addr += vmap->tstart; | |
477 | exec_ops.to_sections[i].endaddr += vmap->tstart; | |
478 | } | |
2e4964ad | 479 | else if (STREQ(".data", exec_ops.to_sections[i].sec_ptr->name)) |
1e939db1 SG |
480 | { |
481 | exec_ops.to_sections[i].addr += vmap->dstart; | |
482 | exec_ops.to_sections[i].endaddr += vmap->dstart; | |
483 | } | |
484 | } | |
41abdfbd JG |
485 | } |
486 | ||
2aefe6e4 JK |
487 | #if 0 |
488 | /* This was for the old, half-assed, core file support. */ | |
41abdfbd JG |
489 | int |
490 | text_adjustment (abfd) | |
491 | bfd *abfd; | |
492 | { | |
493 | static bfd *execbfd; | |
494 | static int adjustment; | |
495 | sec_ptr sect; | |
496 | ||
497 | if (exec_bfd == execbfd) | |
498 | return adjustment; | |
499 | ||
500 | sect = bfd_get_section_by_name (abfd, ".text"); | |
501 | if (sect) | |
502 | adjustment = sect->filepos - sect->vma; | |
503 | else | |
504 | adjustment = 0x200; /* just a wild assumption */ | |
505 | ||
506 | return adjustment; | |
507 | } | |
2aefe6e4 | 508 | #endif |
41abdfbd JG |
509 | |
510 | /* | |
511 | * vmap_ldinfo - update VMAP info with ldinfo() information | |
512 | * | |
513 | * Input: | |
514 | * ldi - ^ to ldinfo() results. | |
515 | */ | |
516 | vmap_ldinfo(ldi) | |
517 | register struct ld_info *ldi; | |
518 | { | |
519 | struct stat ii, vi; | |
520 | register struct vmap *vp; | |
521 | register got_one, retried; | |
522 | CORE_ADDR ostart; | |
523 | ||
524 | /* | |
525 | * for each *ldi, see if we have a corresponding *vp | |
526 | * if so, update the mapping, and symbol table. | |
527 | * if not, add an entry and symbol table. | |
528 | */ | |
529 | do { | |
530 | char *name = ldi->ldinfo_filename; | |
531 | char *memb = name + strlen(name) + 1; | |
532 | ||
533 | retried = 0; | |
534 | ||
535 | if (fstat(ldi->ldinfo_fd, &ii) < 0) | |
536 | fatal("cannot fstat(%d) on %s" | |
537 | , ldi->ldinfo_fd | |
538 | , name); | |
539 | retry: | |
540 | for (got_one = 0, vp = vmap; vp; vp = vp->nxt) { | |
1eeba686 PB |
541 | FILE *io; |
542 | ||
543 | /* First try to find a `vp', which is the same as in ldinfo. | |
544 | If not the same, just continue and grep the next `vp'. If same, | |
545 | relocate its tstart, tend, dstart, dend values. If no such `vp' | |
546 | found, get out of this for loop, add this ldi entry as a new vmap | |
547 | (add_vmap) and come back, fins its `vp' and so on... */ | |
548 | ||
549 | /* The filenames are not always sufficient to match on. */ | |
41abdfbd | 550 | |
9116f7c7 | 551 | if ((name[0] == '/' && !STREQ(name, vp->name)) |
2e4964ad | 552 | || (memb[0] && !STREQ(memb, vp->member))) |
1eeba686 | 553 | continue; |
41abdfbd | 554 | |
1eeba686 PB |
555 | io = bfd_cache_lookup(vp->bfd); /* totally opaque! */ |
556 | if (!io) | |
557 | fatal("cannot find BFD's iostream for %s", vp->name); | |
41abdfbd | 558 | |
1eeba686 | 559 | /* see if we are referring to the same file */ |
41abdfbd | 560 | |
1eeba686 PB |
561 | if (fstat(fileno(io), &vi) < 0) |
562 | fatal("cannot fstat BFD for %s", vp->name); | |
41abdfbd | 563 | |
1eeba686 PB |
564 | if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino) |
565 | continue; | |
41abdfbd | 566 | |
1eeba686 PB |
567 | if (!retried) |
568 | close(ldi->ldinfo_fd); | |
41abdfbd | 569 | |
1eeba686 | 570 | ++got_one; |
41abdfbd | 571 | |
1eeba686 PB |
572 | /* found a corresponding VMAP. remap! */ |
573 | ostart = vp->tstart; | |
41abdfbd | 574 | |
359a097f JK |
575 | /* We can assume pointer == CORE_ADDR, this code is native only. */ |
576 | vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg; | |
1eeba686 | 577 | vp->tend = vp->tstart + ldi->ldinfo_textsize; |
359a097f | 578 | vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg; |
1eeba686 | 579 | vp->dend = vp->dstart + ldi->ldinfo_datasize; |
41abdfbd | 580 | |
1eeba686 PB |
581 | if (vp->tadj) { |
582 | vp->tstart += vp->tadj; | |
583 | vp->tend += vp->tadj; | |
584 | } | |
585 | ||
586 | /* relocate symbol table(s). */ | |
3c02636b | 587 | vmap_symtab (vp); |
41abdfbd | 588 | |
1eeba686 | 589 | /* there may be more, so we don't break out of the loop. */ |
41abdfbd JG |
590 | } |
591 | ||
1eeba686 PB |
592 | /* if there was no matching *vp, we must perforce create the sucker(s) */ |
593 | if (!got_one && !retried) { | |
594 | add_vmap(ldi); | |
595 | ++retried; | |
596 | goto retry; | |
41abdfbd JG |
597 | } |
598 | } while (ldi->ldinfo_next | |
599 | && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi))); | |
600 | ||
41abdfbd JG |
601 | } |
602 | ||
603 | /* | |
604 | * vmap_inferior - print VMAP info for inferior | |
605 | */ | |
606 | vmap_inferior() { | |
607 | ||
608 | if (inferior_pid == 0) | |
507e4004 | 609 | return 0; /* normal processing */ |
41abdfbd JG |
610 | |
611 | exec_files_info(); | |
41abdfbd JG |
612 | return 1; |
613 | } | |
614 | ||
615 | /* Read or write the exec file. | |
616 | ||
617 | Args are address within exec file, address within gdb address-space, | |
618 | length, and a flag indicating whether to read or write. | |
619 | ||
620 | Result is a length: | |
621 | ||
622 | 0: We cannot handle this address and length. | |
623 | > 0: We have handled N bytes starting at this address. | |
624 | (If N == length, we did it all.) We might be able | |
625 | to handle more bytes beyond this length, but no | |
626 | promises. | |
627 | < 0: We cannot handle this address, but if somebody | |
628 | else handles (-N) bytes, we can start from there. | |
629 | ||
630 | The same routine is used to handle both core and exec files; | |
631 | we just tail-call it with more arguments to select between them. */ | |
632 | ||
633 | int | |
818de002 | 634 | xfer_memory (memaddr, myaddr, len, write, target) |
41abdfbd JG |
635 | CORE_ADDR memaddr; |
636 | char *myaddr; | |
637 | int len; | |
638 | int write; | |
818de002 | 639 | struct target_ops *target; |
41abdfbd JG |
640 | { |
641 | boolean res; | |
642 | struct section_table *p; | |
643 | CORE_ADDR nextsectaddr, memend; | |
818de002 | 644 | boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); |
41abdfbd JG |
645 | |
646 | if (len <= 0) | |
0848ad1c | 647 | fatal ("aborting"); |
41abdfbd JG |
648 | |
649 | memend = memaddr + len; | |
650 | xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents; | |
651 | nextsectaddr = memend; | |
652 | ||
818de002 | 653 | for (p = target->to_sections; p < target->to_sections_end; p++) |
41abdfbd JG |
654 | { |
655 | if (p->addr <= memaddr) | |
656 | if (p->endaddr >= memend) | |
657 | { | |
658 | /* Entire transfer is within this section. */ | |
818de002 | 659 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
41abdfbd JG |
660 | return (res != false)? len: 0; |
661 | } | |
662 | else if (p->endaddr <= memaddr) | |
663 | { | |
664 | /* This section ends before the transfer starts. */ | |
665 | continue; | |
666 | } | |
667 | else | |
668 | { | |
669 | /* This section overlaps the transfer. Just do half. */ | |
670 | len = p->endaddr - memaddr; | |
818de002 | 671 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
41abdfbd JG |
672 | return (res != false)? len: 0; |
673 | } | |
674 | else if (p->addr < nextsectaddr) | |
675 | nextsectaddr = p->addr; | |
676 | } | |
677 | ||
678 | if (nextsectaddr >= memend) | |
679 | return 0; /* We can't help */ | |
680 | else | |
681 | return - (nextsectaddr - memaddr); /* Next boundary where we can help */ | |
682 | } | |
683 | ||
818de002 PB |
684 | void |
685 | print_section_info (t, abfd) | |
686 | struct target_ops *t; | |
687 | bfd *abfd; | |
41abdfbd | 688 | { |
818de002 | 689 | struct section_table *p; |
41abdfbd | 690 | |
818de002 PB |
691 | printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); |
692 | wrap_here (" "); | |
693 | printf_filtered ("file type %s.\n", bfd_get_target(abfd)); | |
694 | ||
695 | for (p = t->to_sections; p < t->to_sections_end; p++) { | |
696 | printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08")); | |
697 | printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08")); | |
698 | if (info_verbose) | |
699 | printf_filtered (" @ %s", | |
700 | local_hex_string_custom (p->sec_ptr->filepos, "08")); | |
701 | printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr)); | |
702 | if (p->bfd != abfd) { | |
703 | printf_filtered (" in %s", bfd_get_filename (p->bfd)); | |
704 | } | |
705 | printf_filtered ("\n"); | |
706 | } | |
818de002 PB |
707 | } |
708 | ||
507e4004 | 709 | |
818de002 PB |
710 | static void |
711 | exec_files_info (t) | |
712 | struct target_ops *t; | |
713 | { | |
507e4004 PB |
714 | register struct vmap *vp = vmap; |
715 | ||
818de002 | 716 | print_section_info (t, exec_bfd); |
507e4004 PB |
717 | |
718 | if (!vp) | |
719 | return; | |
720 | ||
1eeba686 PB |
721 | printf("\tMapping info for file `%s'.\n", vp->name); |
722 | ||
723 | printf("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n", | |
724 | "tstart", "tend", "dstart", "dend", "section", "file(member)"); | |
507e4004 PB |
725 | |
726 | for (; vp; vp = vp->nxt) | |
1eeba686 PB |
727 | printf("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n", |
728 | vp->tstart, | |
729 | vp->tend, | |
730 | vp->dstart, | |
731 | vp->dend, | |
732 | vp->name, | |
733 | *vp->member ? "(" : "", | |
734 | vp->member, | |
735 | *vp->member ? ")" : ""); | |
41abdfbd JG |
736 | } |
737 | ||
738 | #ifdef DAMON | |
76b28d05 | 739 | /* Damon's implementation of set_section_command! It is based on the sex member |
41abdfbd JG |
740 | (which is a section pointer from vmap) of vmap. |
741 | We will not have multiple vmap entries (one for each section), rather transmit | |
742 | text and data base offsets and fix them at the same time. Elimination of sex | |
743 | entry in vmap make this function obsolute, use the one from exec.c. | |
76b28d05 | 744 | Need further testing!! FIXMEmgo. */ |
41abdfbd JG |
745 | |
746 | static void | |
747 | set_section_command(args, from_tty) | |
748 | char *args; | |
749 | { | |
750 | register struct vmap *vp = vmap; | |
751 | char *secname; | |
752 | unsigned seclen; | |
753 | unsigned long secaddr; | |
754 | char secprint[100]; | |
755 | long offset; | |
756 | ||
757 | if (args == 0) | |
758 | error("Must specify section name and its virtual address"); | |
759 | ||
760 | /* Parse out section name */ | |
761 | for (secname = args; !isspace(*args); args++) | |
762 | ; | |
763 | seclen = args - secname; | |
764 | ||
765 | /* Parse out new virtual address */ | |
766 | secaddr = parse_and_eval_address(args); | |
767 | ||
768 | for (vp = vmap; vp; vp = vp->nxt) { | |
769 | if (!strncmp(secname | |
770 | , bfd_section_name(vp->bfd, vp->sex), seclen) | |
771 | && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') { | |
772 | offset = secaddr - vp->tstart; | |
773 | vp->tstart += offset; | |
774 | vp->tend += offset; | |
775 | exec_files_info(); | |
776 | return; | |
777 | } | |
778 | } | |
779 | ||
780 | if (seclen >= sizeof(secprint)) | |
781 | seclen = sizeof(secprint) - 1; | |
782 | strncpy(secprint, secname, seclen); | |
783 | secprint[seclen] = '\0'; | |
784 | error("Section %s not found", secprint); | |
785 | } | |
786 | #else | |
787 | static void | |
788 | set_section_command (args, from_tty) | |
789 | char *args; | |
790 | int from_tty; | |
791 | { | |
792 | struct section_table *p; | |
793 | char *secname; | |
794 | unsigned seclen; | |
795 | unsigned long secaddr; | |
796 | char secprint[100]; | |
797 | long offset; | |
798 | ||
799 | if (args == 0) | |
800 | error ("Must specify section name and its virtual address"); | |
801 | ||
802 | /* Parse out section name */ | |
803 | for (secname = args; !isspace(*args); args++) ; | |
804 | seclen = args - secname; | |
805 | ||
806 | /* Parse out new virtual address */ | |
807 | secaddr = parse_and_eval_address (args); | |
808 | ||
507e4004 | 809 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { |
41abdfbd JG |
810 | if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) |
811 | && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { | |
812 | offset = secaddr - p->addr; | |
813 | p->addr += offset; | |
814 | p->endaddr += offset; | |
507e4004 PB |
815 | if (from_tty) |
816 | exec_files_info(&exec_ops); | |
41abdfbd JG |
817 | return; |
818 | } | |
819 | } | |
820 | if (seclen >= sizeof (secprint)) | |
821 | seclen = sizeof (secprint) - 1; | |
822 | strncpy (secprint, secname, seclen); | |
823 | secprint[seclen] = '\0'; | |
824 | error ("Section %s not found", secprint); | |
825 | } | |
826 | ||
827 | #endif /* !DAMON */ | |
828 | ||
829 | struct target_ops exec_ops = { | |
830 | "exec", "Local exec file", | |
831 | "Use an executable file as a target.\n\ | |
832 | Specify the filename of the executable file.", | |
833 | exec_file_command, exec_close, /* open, close */ | |
050d45c5 | 834 | find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */ |
41abdfbd | 835 | 0, 0, /* fetch_registers, store_registers, */ |
a03d4f8e | 836 | 0, /* prepare_to_store */ |
818de002 | 837 | xfer_memory, exec_files_info, |
41abdfbd JG |
838 | 0, 0, /* insert_breakpoint, remove_breakpoint, */ |
839 | 0, 0, 0, 0, 0, /* terminal stuff */ | |
840 | 0, 0, /* kill, load */ | |
e17960fb | 841 | 0, /* lookup sym */ |
050d45c5 | 842 | find_default_create_inferior, |
41abdfbd | 843 | 0, /* mourn_inferior */ |
5ee4e16c | 844 | 0, /* can_run */ |
3950a34e | 845 | 0, /* notice_signals */ |
41abdfbd JG |
846 | file_stratum, 0, /* next */ |
847 | 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ | |
848 | 0, 0, /* section pointers */ | |
849 | OPS_MAGIC, /* Always the last thing */ | |
850 | }; | |
2aefe6e4 JK |
851 | \f |
852 | /* Core file stuff. */ | |
853 | ||
854 | /* Relocate symtabs and read in shared library info, based on symbols | |
855 | from the core file. */ | |
856 | void | |
857 | xcoff_relocate_core () | |
858 | { | |
859 | /* Offset of member MEMBER in a struct of type TYPE. */ | |
860 | #ifndef offsetof | |
861 | #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) | |
862 | #endif | |
863 | ||
864 | /* Size of a struct ld_info except for the variable-length filename. */ | |
865 | #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename)) | |
866 | ||
867 | sec_ptr ldinfo_sec; | |
868 | int offset = 0; | |
869 | struct ld_info *ldip; | |
870 | struct vmap *vp; | |
871 | ||
872 | /* Allocated size of buffer. */ | |
873 | int buffer_size = LDINFO_SIZE; | |
874 | char *buffer = xmalloc (buffer_size); | |
875 | struct cleanup *old = make_cleanup (free_current_contents, &buffer); | |
876 | ||
877 | ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo"); | |
878 | if (ldinfo_sec == NULL) | |
879 | { | |
880 | bfd_err: | |
881 | fprintf_filtered (stderr, "Couldn't get ldinfo from core file: %s\n", | |
882 | bfd_errmsg (bfd_error)); | |
883 | do_cleanups (old); | |
884 | return; | |
885 | } | |
886 | do | |
887 | { | |
888 | int i; | |
889 | int names_found = 0; | |
890 | ||
891 | /* Read in everything but the name. */ | |
892 | if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer, | |
893 | offset, LDINFO_SIZE) == 0) | |
894 | goto bfd_err; | |
895 | ||
896 | /* Now the name. */ | |
897 | i = LDINFO_SIZE; | |
898 | do | |
899 | { | |
900 | if (i == buffer_size) | |
901 | { | |
902 | buffer_size *= 2; | |
903 | buffer = xrealloc (buffer, buffer_size); | |
904 | } | |
905 | if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i], | |
906 | offset + i, 1) == 0) | |
907 | goto bfd_err; | |
908 | if (buffer[i++] == '\0') | |
909 | ++names_found; | |
910 | } while (names_found < 2); | |
911 | ||
912 | ldip = (struct ld_info *)buffer; | |
913 | ||
914 | /* Can't use a file descriptor from the core file; need to open it. */ | |
915 | ldip->ldinfo_fd = -1; | |
916 | ||
917 | /* The first ldinfo is for the exec file, allocated elsewhere. */ | |
918 | if (offset == 0) | |
919 | vp = vmap; | |
920 | else | |
921 | vp = add_vmap (ldip); | |
922 | ||
923 | offset += ldip->ldinfo_next; | |
41abdfbd | 924 | |
359a097f JK |
925 | /* We can assume pointer == CORE_ADDR, this code is native only. */ |
926 | vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg; | |
2aefe6e4 | 927 | vp->tend = vp->tstart + ldip->ldinfo_textsize; |
359a097f | 928 | vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg; |
2aefe6e4 JK |
929 | vp->dend = vp->dstart + ldip->ldinfo_datasize; |
930 | ||
931 | if (vp->tadj != 0) { | |
932 | vp->tstart += vp->tadj; | |
933 | vp->tend += vp->tadj; | |
934 | } | |
935 | ||
936 | /* Unless this is the exec file, | |
937 | add our sections to the section table for the core target. */ | |
938 | if (vp != vmap) | |
939 | { | |
940 | int count; | |
941 | struct section_table *stp; | |
942 | ||
943 | count = core_ops.to_sections_end - core_ops.to_sections; | |
944 | count += 2; | |
945 | core_ops.to_sections = (struct section_table *) | |
946 | xrealloc (core_ops.to_sections, | |
947 | sizeof (struct section_table) * count); | |
948 | core_ops.to_sections_end = core_ops.to_sections + count; | |
949 | stp = core_ops.to_sections_end - 2; | |
950 | ||
951 | /* "Why do we add bfd_section_vma?", I hear you cry. | |
952 | Well, the start of the section in the file is actually | |
953 | that far into the section as the struct vmap understands it. | |
954 | So for text sections, bfd_section_vma tends to be 0x200, | |
955 | and if vp->tstart is 0xd0002000, then the first byte of | |
956 | the text section on disk corresponds to address 0xd0002200. */ | |
957 | stp->bfd = vp->bfd; | |
958 | stp->sec_ptr = bfd_get_section_by_name (stp->bfd, ".text"); | |
959 | stp->addr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->tstart; | |
960 | stp->endaddr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->tend; | |
961 | stp++; | |
962 | ||
963 | stp->bfd = vp->bfd; | |
964 | stp->sec_ptr = bfd_get_section_by_name (stp->bfd, ".data"); | |
965 | stp->addr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->dstart; | |
966 | stp->endaddr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->dend; | |
967 | } | |
968 | ||
3c02636b | 969 | vmap_symtab (vp); |
2aefe6e4 | 970 | |
359a097f JK |
971 | add_text_to_loadinfo ((CORE_ADDR)ldip->ldinfo_textorg, |
972 | (CORE_ADDR)ldip->ldinfo_dataorg); | |
2aefe6e4 JK |
973 | } while (ldip->ldinfo_next != 0); |
974 | vmap_exec (); | |
975 | do_cleanups (old); | |
976 | } | |
41abdfbd JG |
977 | |
978 | void | |
979 | _initialize_exec() | |
980 | { | |
981 | ||
982 | add_com("file", class_files, file_command, | |
983 | "Use FILE as program to be debugged.\n\ | |
984 | It is read for its symbols, for getting the contents of pure memory,\n\ | |
985 | and it is the program executed when you use the `run' command.\n\ | |
986 | If FILE cannot be found as specified, your execution directory path\n\ | |
987 | ($PATH) is searched for a command of that name.\n\ | |
988 | No arg means to have no executable file and no symbols."); | |
989 | ||
990 | add_com("exec-file", class_files, exec_file_command, | |
991 | "Use FILE as program for getting contents of pure memory.\n\ | |
992 | If FILE cannot be found as specified, your execution directory path\n\ | |
993 | is searched for a command of that name.\n\ | |
994 | No arg means have no executable file."); | |
995 | ||
996 | add_com("section", class_files, set_section_command, | |
997 | "Change the base address of section SECTION of the exec file to ADDR.\n\ | |
998 | This can be used if the exec file does not contain section addresses,\n\ | |
999 | (such as in the a.out format), or when the addresses specified in the\n\ | |
1000 | file itself are wrong. Each section must be changed separately. The\n\ | |
1001 | ``info files'' command lists all the sections and their addresses."); | |
1002 | ||
1003 | add_target(&exec_ops); | |
1004 | } |