]>
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 */ | |
43 | ||
1ab3bf1b JG |
44 | /* Prototypes for local functions */ |
45 | ||
1ab3bf1b JG |
46 | static void |
47 | file_command PARAMS ((char *, int)); | |
48 | ||
49 | static void | |
50 | exec_close PARAMS ((int)); | |
51 | ||
41abdfbd JG |
52 | struct section_table *exec_sections, *exec_sections_end; |
53 | ||
54 | #define eq(s0, s1) !strcmp(s0, s1) | |
55 | ||
56 | /* Whether to open exec and core files read-only or read-write. */ | |
57 | ||
58 | int write_files = 0; | |
59 | ||
818de002 PB |
60 | extern int info_verbose; |
61 | ||
41abdfbd JG |
62 | bfd *exec_bfd; /* needed by core.c */ |
63 | ||
64 | extern char *getenv(); | |
65 | extern void child_create_inferior (), child_attach (); | |
66 | extern void add_syms_addr_command (); | |
67 | extern void symbol_file_command (); | |
68 | static void exec_files_info(); | |
818de002 | 69 | extern struct objfile *lookup_objfile_bfd (); |
41abdfbd JG |
70 | |
71 | /* | |
72 | * the vmap struct is used to describe the virtual address space of | |
73 | * the target we are manipulating. The first entry is always the "exec" | |
74 | * file. Subsequent entries correspond to other objects that are | |
75 | * mapped into the address space of a process created from the "exec" file. | |
76 | * These are either in response to exec()ing the file, in which case all | |
77 | * shared libraries are loaded, or a "load" system call, followed by the | |
78 | * user's issuance of a "load" command. | |
79 | */ | |
80 | struct vmap { | |
81 | struct vmap *nxt; /* ^ to next in chain */ | |
82 | bfd *bfd; /* BFD for mappable object library */ | |
83 | char *name; /* ^ to object file name */ | |
84 | char *member; /* ^ to member name */ | |
85 | CORE_ADDR tstart; /* virtual addr where member is mapped */ | |
86 | CORE_ADDR tend; /* virtual upper bound of member */ | |
87 | CORE_ADDR tadj; /* heuristically derived adjustment */ | |
88 | CORE_ADDR dstart; /* virtual address of data start */ | |
89 | CORE_ADDR dend; /* vitrual address of data end */ | |
90 | }; | |
91 | ||
92 | ||
93 | struct vmap_and_bfd { | |
94 | bfd *pbfd; | |
95 | struct vmap *pvmap; | |
96 | }; | |
97 | ||
98 | static struct vmap *vmap; /* current vmap */ | |
99 | ||
100 | extern struct target_ops exec_ops; | |
101 | ||
102 | ||
103 | /* exec_close - done with exec file, clean up all resources. */ | |
104 | ||
1ab3bf1b | 105 | static void |
818de002 PB |
106 | exec_close(quitting) |
107 | { | |
108 | register struct vmap *vp, *nxt; | |
109 | struct objfile *obj; | |
110 | ||
111 | for (nxt = vmap; vp = nxt; ) | |
112 | { | |
113 | nxt = vp->nxt; | |
114 | ||
115 | /* if there is an objfile associated with this bfd, | |
116 | free_objfile() will do proper cleanup of objfile *and* bfd. */ | |
117 | ||
118 | if (obj = lookup_objfile_bfd (vp->bfd)) | |
119 | free_objfile (obj); | |
120 | else | |
121 | bfd_close(vp->bfd); | |
122 | ||
123 | free_named_symtabs(vp->name); | |
124 | free(vp); | |
125 | } | |
126 | ||
127 | vmap = 0; | |
507e4004 PB |
128 | |
129 | if (exec_bfd) { | |
130 | bfd_close (exec_bfd); | |
131 | exec_bfd = NULL; | |
132 | } | |
133 | if (exec_ops.to_sections) { | |
134 | free (exec_ops.to_sections); | |
135 | exec_ops.to_sections = NULL; | |
136 | exec_ops.to_sections_end = NULL; | |
137 | } | |
41abdfbd JG |
138 | } |
139 | ||
140 | /* | |
141 | * exec_file_command - handle the "exec" command, &c. | |
142 | */ | |
143 | void | |
144 | exec_file_command(filename, from_tty) | |
145 | char *filename; | |
146 | { | |
41abdfbd | 147 | target_preopen(from_tty); |
507e4004 PB |
148 | |
149 | /* Remove any previous exec file. */ | |
41abdfbd JG |
150 | unpush_target(&exec_ops); |
151 | ||
152 | /* Now open and digest the file the user requested, if any. */ | |
153 | ||
154 | if (filename) { | |
155 | char *scratch_pathname; | |
156 | int scratch_chan; | |
157 | ||
158 | filename = tilde_expand(filename); | |
1ab3bf1b | 159 | make_cleanup (free, filename); |
41abdfbd | 160 | |
507e4004 PB |
161 | scratch_chan = openp(getenv("PATH"), 1, filename, |
162 | write_files? O_RDWR: O_RDONLY, 0, | |
163 | &scratch_pathname); | |
41abdfbd JG |
164 | if (scratch_chan < 0) |
165 | perror_with_name(filename); | |
166 | ||
507e4004 PB |
167 | exec_bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan); |
168 | if (!exec_bfd) | |
41abdfbd JG |
169 | error("Could not open `%s' as an executable file: %s" |
170 | , scratch_pathname, bfd_errmsg(bfd_error)); | |
171 | ||
172 | /* make sure we have an object file */ | |
173 | ||
507e4004 PB |
174 | if (!bfd_check_format(exec_bfd, bfd_object)) |
175 | error("\"%s\": not in executable format: %s.", | |
176 | scratch_pathname, bfd_errmsg(bfd_error)); | |
41abdfbd JG |
177 | |
178 | ||
179 | /* setup initial vmap */ | |
180 | ||
507e4004 | 181 | map_vmap (exec_bfd, 0); |
41abdfbd | 182 | if (!vmap) |
507e4004 PB |
183 | error("Can't find the file sections in `%s': %s", |
184 | exec_bfd->filename, bfd_errmsg(bfd_error)); | |
41abdfbd | 185 | |
507e4004 PB |
186 | if (build_section_table (exec_bfd, &exec_ops.to_sections, |
187 | &exec_ops.to_sections_end)) | |
41abdfbd JG |
188 | error ("Can't find the file sections in `%s': %s", |
189 | exec_bfd->filename, bfd_errmsg (bfd_error)); | |
190 | ||
191 | /* make sure core, if present, matches */ | |
192 | validate_files(); | |
193 | ||
194 | push_target(&exec_ops); | |
195 | ||
196 | /* Tell display code(if any) about the changed file name. */ | |
197 | ||
198 | if (exec_file_display_hook) | |
199 | (*exec_file_display_hook)(filename); | |
200 | } | |
201 | else { | |
202 | exec_close(0); /* just in case */ | |
203 | if (from_tty) | |
204 | printf("No exec file now.\n"); | |
205 | } | |
206 | } | |
207 | ||
208 | /* Set both the exec file and the symbol file, in one command. What a | |
209 | * novelty. Why did GDB go through four major releases before this | |
210 | * command was added? | |
211 | */ | |
1ab3bf1b | 212 | static void |
41abdfbd JG |
213 | file_command(arg, from_tty) |
214 | char *arg; { | |
215 | ||
216 | exec_file_command(arg, from_tty); | |
217 | symbol_file_command(arg, from_tty); | |
218 | } | |
219 | ||
220 | /* Locate all mappable sections of a BFD file. | |
221 | table_pp_char is a char * to get it through bfd_map_over_sections; | |
222 | we cast it back to its proper type. */ | |
223 | ||
1ab3bf1b | 224 | static void |
41abdfbd JG |
225 | add_to_section_table (abfd, asect, table_pp_char) |
226 | bfd *abfd; | |
227 | sec_ptr asect; | |
228 | char *table_pp_char; | |
229 | { | |
230 | struct section_table **table_pp = (struct section_table **)table_pp_char; | |
231 | flagword aflag; | |
232 | ||
233 | aflag = bfd_get_section_flags (abfd, asect); | |
234 | /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */ | |
235 | if (!(aflag & SEC_LOAD)) | |
236 | return; | |
507e4004 PB |
237 | if (0 == bfd_section_size (abfd, asect)) |
238 | return; | |
239 | (*table_pp)->bfd = abfd; | |
41abdfbd JG |
240 | (*table_pp)->sec_ptr = asect; |
241 | (*table_pp)->addr = bfd_section_vma (abfd, asect); | |
242 | (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); | |
243 | (*table_pp)++; | |
244 | } | |
245 | ||
246 | int | |
247 | build_section_table (some_bfd, start, end) | |
248 | bfd *some_bfd; | |
249 | struct section_table **start, **end; | |
250 | { | |
251 | unsigned count; | |
252 | ||
253 | count = bfd_count_sections (some_bfd); | |
254 | if (count == 0) | |
255 | abort(); /* return 1? */ | |
256 | if (*start) | |
257 | free (*start); | |
258 | *start = (struct section_table *) xmalloc (count * sizeof (**start)); | |
259 | *end = *start; | |
260 | bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); | |
261 | if (*end > *start + count) | |
262 | abort(); | |
263 | /* We could realloc the table, but it probably loses for most files. */ | |
264 | return 0; | |
265 | } | |
266 | ||
267 | /* | |
268 | * lookup_symtab_bfd - find if we currently have any symbol tables from bfd | |
269 | */ | |
270 | struct objfile * | |
271 | lookup_objfile_bfd(bfd *bfd) { | |
272 | register struct objfile *s; | |
273 | ||
274 | for (s = object_files; s; s = s->next) | |
275 | if (s->obfd == bfd) | |
276 | return s; | |
277 | return 0; | |
278 | } | |
279 | ||
280 | ||
281 | void | |
282 | sex_to_vmap(bfd *bf, sec_ptr sex, struct vmap_and_bfd *vmap_bfd) | |
283 | { | |
284 | register struct vmap *vp, **vpp; | |
285 | register struct symtab *syms; | |
286 | bfd *arch = vmap_bfd->pbfd; | |
287 | vp = vmap_bfd->pvmap; | |
288 | ||
289 | if ((bfd_get_section_flags(bf, sex) & SEC_LOAD) == 0) | |
290 | return; | |
291 | ||
292 | if (!strcmp(bfd_section_name(bf, sex), ".text")) { | |
293 | vp->tstart = 0; | |
294 | vp->tend = vp->tstart + bfd_section_size(bf, sex); | |
295 | ||
507e4004 PB |
296 | /* When it comes to this adjustment value, in contrast to our previous |
297 | belief shared objects should behave the same as the main load segment. | |
298 | This is the offset from the beginning of text section to the first | |
299 | real instruction. */ | |
300 | ||
301 | vp->tadj = sex->filepos - bfd_section_vma(bf, sex); | |
41abdfbd JG |
302 | } |
303 | ||
304 | else if (!strcmp(bfd_section_name(bf, sex), ".data")) { | |
305 | vp->dstart = 0; | |
306 | vp->dend = vp->dstart + bfd_section_size(bf, sex); | |
307 | } | |
308 | ||
309 | else if (!strcmp(bfd_section_name(bf, sex), ".bss")) /* FIXMEmgo */ | |
310 | printf ("bss section in exec! Don't know what the heck to do!\n"); | |
311 | } | |
312 | ||
313 | /* Make a vmap for the BFD "bf", which might be a member of the archive | |
314 | BFD "arch". If we have not yet read in symbols for this file, do so. */ | |
315 | ||
316 | map_vmap (bfd *bf, bfd *arch) | |
317 | { | |
318 | struct vmap_and_bfd vmap_bfd; | |
319 | struct vmap *vp, **vpp; | |
320 | struct objfile *obj; | |
41abdfbd JG |
321 | |
322 | vp = (void*) xmalloc (sizeof (*vp)); | |
323 | vp->nxt = 0; | |
324 | vp->bfd = bf; | |
325 | vp->name = bfd_get_filename(arch ? arch : bf); | |
326 | vp->member = arch ? bfd_get_filename(bf) : ""; | |
327 | ||
328 | vmap_bfd.pbfd = arch; | |
329 | vmap_bfd.pvmap = vp; | |
330 | bfd_map_over_sections (bf, sex_to_vmap, &vmap_bfd); | |
331 | ||
332 | obj = lookup_objfile_bfd (bf); | |
333 | if (exec_bfd && !obj) { | |
b0246b3b | 334 | obj = allocate_objfile (bf, 0); |
4369a140 | 335 | syms_from_objfile (obj, 0, 0, 0); |
41abdfbd JG |
336 | } |
337 | ||
338 | /* find the end of the list, and append. */ | |
339 | for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) | |
340 | ; | |
341 | *vpp = vp; | |
342 | } | |
343 | ||
507e4004 PB |
344 | |
345 | #define FASTER_MSYMBOL_RELOCATION 1 | |
346 | ||
347 | #ifdef FASTER_MSYMBOL_RELOCATION | |
348 | ||
349 | /* Used to relocate an object file's minimal symbols. */ | |
350 | ||
351 | static void | |
352 | reloc_objfile_msymbols (objf, addr) | |
353 | struct objfile *objf; | |
354 | CORE_ADDR addr; | |
355 | { | |
356 | register struct minimal_symbol *msymbol; | |
357 | int ii; | |
358 | ||
359 | for (msymbol = objf->msymbols, ii=0; | |
360 | msymbol && ii < objf->minimal_symbol_count; ++msymbol, ++ii) | |
361 | ||
362 | if (msymbol->address < TEXT_SEGMENT_BASE) | |
363 | msymbol->address += addr; | |
364 | } | |
365 | ||
366 | #else /* !FASTER_MSYMBOL_RELOCATION */ | |
367 | ||
1ab3bf1b | 368 | /* Called via iterate_over_msymbols to relocate minimal symbols */ |
41abdfbd | 369 | |
507e4004 | 370 | static int |
1ab3bf1b JG |
371 | relocate_minimal_symbol (objfile, msymbol, arg1, arg2, arg3) |
372 | struct objfile *objfile; | |
373 | struct minimal_symbol *msymbol; | |
374 | PTR arg1; | |
375 | PTR arg2; | |
376 | PTR arg3; | |
377 | { | |
818de002 PB |
378 | if (msymbol->address < TEXT_SEGMENT_BASE) |
379 | msymbol -> address += (int) arg1; | |
507e4004 PB |
380 | |
381 | /* return 0, otherwise `iterate_over_msymbols()' will stop at the | |
382 | first iteration. */ | |
383 | return 0; | |
1ab3bf1b | 384 | } |
507e4004 | 385 | #endif /* FASTER_MSYMBOL_RELOCATION */ |
1ab3bf1b JG |
386 | |
387 | /* true, if symbol table and minimal symbol table are relocated. */ | |
41abdfbd JG |
388 | |
389 | int symtab_relocated = 0; | |
390 | ||
391 | ||
392 | /* vmap_symtab - handle symbol translation on vmapping */ | |
393 | ||
394 | vmap_symtab(vp, old_start, vip) | |
395 | register struct vmap *vp; | |
396 | CORE_ADDR old_start; | |
397 | struct stat *vip; | |
398 | { | |
1ab3bf1b JG |
399 | register struct symtab *s; |
400 | register struct objfile *objfile; | |
401 | ||
402 | /* | |
403 | * for each symbol table generated from the vp->bfd | |
404 | */ | |
405 | for (objfile = object_files; objfile != NULL; objfile = objfile -> next) | |
406 | { | |
407 | for (s = objfile -> symtabs; s != NULL; s = s -> next) { | |
408 | ||
409 | /* skip over if this is not relocatable and doesn't have a line table */ | |
410 | if (s->nonreloc && !LINETABLE (s)) | |
411 | continue; | |
412 | ||
413 | /* matching the symbol table's BFD and the *vp's BFD is hairy. | |
414 | exec_file creates a seperate BFD for possibly the | |
415 | same file as symbol_file.FIXME ALL THIS MUST BE RECTIFIED. */ | |
416 | ||
417 | if (objfile->obfd == vp->bfd) { | |
418 | /* if they match, we luck out. */ | |
419 | ; | |
420 | } else if (vp->member[0]) { | |
421 | /* no match, and member present, not this one. */ | |
422 | continue; | |
423 | } else { | |
424 | struct stat si; | |
425 | FILE *io; | |
426 | ||
427 | /* | |
428 | * no match, and no member. need to be sure. | |
429 | */ | |
430 | io = bfd_cache_lookup(objfile->obfd); | |
431 | if (!io) | |
432 | fatal("cannot find BFD's iostream for sym"); | |
433 | /* | |
434 | * see if we are referring to the same file | |
435 | */ | |
436 | if (fstat(fileno(io), &si) < 0) | |
437 | fatal("cannot fstat BFD for sym"); | |
438 | ||
439 | if (si.st_dev != vip->st_dev | |
440 | || si.st_ino != vip->st_ino) | |
41abdfbd | 441 | continue; |
41abdfbd | 442 | } |
1ab3bf1b | 443 | |
507e4004 PB |
444 | if (vp->tstart != old_start) { |
445 | ||
446 | /* Once we find a relocation base address for one of the symtabs | |
447 | in this objfile, it will be the same for all symtabs in this | |
448 | objfile. Clean this algorithm. FIXME. */ | |
449 | ||
450 | for (; s; s = s->next) | |
451 | if (!s->nonreloc || LINETABLE(s)) | |
452 | vmap_symtab_1(s, vp, old_start); | |
453 | ||
454 | #ifdef FASTER_MSYMBOL_RELOCATION | |
455 | /* we can rely on the fact that at least one symtab in this objfile | |
456 | will get relocated. Thus, we can be sure that minimal symbol | |
457 | vector is guaranteed for relocation. */ | |
458 | ||
459 | reloc_objfile_msymbols (objfile, vp->tstart - old_start); | |
460 | #endif | |
461 | break; | |
462 | } | |
1ab3bf1b JG |
463 | } |
464 | } | |
507e4004 | 465 | |
818de002 | 466 | if (vp->tstart != old_start) { |
507e4004 | 467 | #ifndef FASTER_MSYMBOL_RELOCATION |
a846ac55 | 468 | (void) iterate_over_msymbols (relocate_minimal_symbol, |
1ab3bf1b JG |
469 | (PTR) (vp->tstart - old_start), |
470 | (PTR) NULL, (PTR) NULL); | |
507e4004 | 471 | #endif |
818de002 PB |
472 | |
473 | /* breakpoints need to be relocated as well. */ | |
474 | fixup_breakpoints (0, TEXT_SEGMENT_BASE, vp->tstart - old_start); | |
475 | } | |
1ab3bf1b JG |
476 | |
477 | symtab_relocated = 1; | |
41abdfbd JG |
478 | } |
479 | ||
507e4004 | 480 | |
41abdfbd JG |
481 | vmap_symtab_1(s, vp, old_start) |
482 | register struct symtab *s; | |
483 | register struct vmap *vp; | |
484 | CORE_ADDR old_start; | |
485 | { | |
486 | register int i, j; | |
487 | int len, blen; | |
488 | register struct linetable *l; | |
489 | struct blockvector *bv; | |
490 | register struct block *b; | |
491 | int depth; | |
492 | register ulong reloc, dreloc; | |
493 | ||
494 | if ((reloc = vp->tstart - old_start) == 0) | |
495 | return; | |
496 | ||
497 | dreloc = vp->dstart; /* data relocation */ | |
498 | ||
499 | /* | |
500 | * The line table must be relocated. This is only present for | |
818de002 | 501 | * .text sections, so only vp->text type maps need be considered. |
41abdfbd JG |
502 | */ |
503 | l = LINETABLE (s); | |
818de002 PB |
504 | if (l) { |
505 | len = l->nitems; | |
506 | for (i = 0; i < len; i++) | |
41abdfbd | 507 | l->item[i].pc += reloc; |
818de002 | 508 | } |
41abdfbd JG |
509 | |
510 | /* if this symbol table is not relocatable, only line table should | |
511 | be relocated and the rest ignored. */ | |
512 | if (s->nonreloc) | |
513 | return; | |
514 | ||
515 | bv = BLOCKVECTOR(s); | |
516 | len = BLOCKVECTOR_NBLOCKS(bv); | |
517 | ||
518 | for (i = 0; i < len; i++) { | |
519 | b = BLOCKVECTOR_BLOCK(bv, i); | |
520 | ||
521 | BLOCK_START(b) += reloc; | |
522 | BLOCK_END(b) += reloc; | |
523 | ||
524 | blen = BLOCK_NSYMS(b); | |
525 | for (j = 0; j < blen; j++) { | |
526 | register struct symbol *sym; | |
527 | ||
528 | sym = BLOCK_SYM(b, j); | |
529 | switch (SYMBOL_NAMESPACE(sym)) { | |
530 | case STRUCT_NAMESPACE: | |
531 | case UNDEF_NAMESPACE: | |
532 | continue; | |
533 | ||
534 | case LABEL_NAMESPACE: | |
535 | case VAR_NAMESPACE: | |
536 | break; | |
537 | } | |
538 | ||
539 | switch (SYMBOL_CLASS(sym)) { | |
540 | case LOC_CONST: | |
541 | case LOC_CONST_BYTES: | |
542 | case LOC_LOCAL: | |
543 | case LOC_REGISTER: | |
544 | case LOC_ARG: | |
545 | case LOC_LOCAL_ARG: | |
546 | case LOC_REF_ARG: | |
547 | case LOC_REGPARM: | |
548 | case LOC_TYPEDEF: | |
549 | continue; | |
550 | ||
551 | #ifdef FIXME | |
552 | case LOC_EXTERNAL: | |
553 | #endif | |
554 | case LOC_LABEL: | |
555 | SYMBOL_VALUE_ADDRESS(sym) += reloc; | |
556 | break; | |
557 | ||
558 | case LOC_STATIC: | |
559 | SYMBOL_VALUE_ADDRESS(sym) += dreloc; | |
560 | break; | |
561 | ||
562 | case LOC_BLOCK: | |
563 | break; | |
564 | ||
565 | default: | |
566 | fatal("botched symbol class %x" | |
567 | , SYMBOL_CLASS(sym)); | |
568 | break; | |
569 | } | |
570 | } | |
571 | } | |
572 | } | |
573 | ||
574 | /* | |
575 | * add_vmap - add a new vmap entry based on ldinfo() information | |
576 | */ | |
577 | add_vmap(ldi) | |
578 | register struct ld_info *ldi; { | |
579 | bfd *bfd, *last; | |
507e4004 PB |
580 | register char *mem, *objname; |
581 | ||
582 | /* This ldi structure was allocated using alloca() in | |
583 | aixcoff_relocate_symtab(). Now we need to have persistent object | |
584 | and member names, so we should save them. */ | |
41abdfbd JG |
585 | |
586 | mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1; | |
507e4004 PB |
587 | mem = savestring (mem, strlen (mem)); |
588 | objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename)); | |
589 | ||
590 | bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd); | |
41abdfbd | 591 | if (!bfd) |
507e4004 PB |
592 | error("Could not open `%s' as an executable file: %s", |
593 | objname, bfd_errmsg(bfd_error)); | |
41abdfbd JG |
594 | |
595 | ||
596 | /* make sure we have an object file */ | |
597 | ||
598 | if (bfd_check_format(bfd, bfd_object)) | |
507e4004 | 599 | map_vmap (bfd, 0); |
41abdfbd JG |
600 | |
601 | else if (bfd_check_format(bfd, bfd_archive)) { | |
602 | last = 0; | |
603 | /* | |
604 | * FIXME??? am I tossing BFDs? bfd? | |
605 | */ | |
606 | while (last = bfd_openr_next_archived_file(bfd, last)) | |
607 | if (eq(mem, last->filename)) | |
608 | break; | |
609 | ||
610 | if (!last) { | |
507e4004 PB |
611 | bfd_close(bfd); |
612 | /* FIXME -- should be error */ | |
613 | warning("\"%s\": member \"%s\" missing.", bfd->filename, mem); | |
614 | return; | |
41abdfbd JG |
615 | } |
616 | ||
617 | if (!bfd_check_format(last, bfd_object)) { | |
618 | bfd_close(last); /* XXX??? */ | |
619 | goto obj_err; | |
620 | } | |
621 | ||
622 | map_vmap (last, bfd); | |
623 | } | |
624 | else { | |
625 | obj_err: | |
626 | bfd_close(bfd); | |
627 | /* FIXME -- should be error */ | |
628 | warning("\"%s\": not in executable format: %s." | |
507e4004 | 629 | , objname, bfd_errmsg(bfd_error)); |
41abdfbd JG |
630 | return; |
631 | } | |
632 | } | |
633 | ||
634 | ||
507e4004 PB |
635 | /* As well as symbol tables, exec_sections need relocation. After |
636 | the inferior process' termination, there will be a relocated symbol | |
637 | table exist with no corresponding inferior process. At that time, we | |
638 | need to use `exec' bfd, rather than the inferior process's memory space | |
639 | to look up symbols. | |
640 | ||
641 | `exec_sections' need to be relocated only once, as long as the exec | |
642 | file remains unchanged. | |
41abdfbd JG |
643 | */ |
644 | vmap_exec () | |
645 | { | |
646 | static bfd *execbfd; | |
647 | if (execbfd == exec_bfd) | |
648 | return; | |
649 | ||
650 | execbfd = exec_bfd; | |
651 | ||
41abdfbd | 652 | /* First exec section is `.text', second is `.data'. If this is changed, |
507e4004 PB |
653 | then this routine will choke. */ |
654 | ||
655 | if (!vmap || !exec_ops.to_sections || | |
656 | strcmp (exec_ops.to_sections[0].sec_ptr->name, ".text") || | |
657 | strcmp (exec_ops.to_sections[1].sec_ptr->name, ".data")) | |
658 | ||
659 | fatal ("aix: Improper exec_ops sections."); | |
660 | ||
661 | exec_ops.to_sections [0].addr += vmap->tstart; | |
662 | exec_ops.to_sections [0].endaddr += vmap->tstart; | |
663 | exec_ops.to_sections [1].addr += vmap->dstart; | |
664 | exec_ops.to_sections [1].endaddr += vmap->dstart; | |
41abdfbd JG |
665 | } |
666 | ||
667 | ||
668 | int | |
669 | text_adjustment (abfd) | |
670 | bfd *abfd; | |
671 | { | |
672 | static bfd *execbfd; | |
673 | static int adjustment; | |
674 | sec_ptr sect; | |
675 | ||
676 | if (exec_bfd == execbfd) | |
677 | return adjustment; | |
678 | ||
679 | sect = bfd_get_section_by_name (abfd, ".text"); | |
680 | if (sect) | |
681 | adjustment = sect->filepos - sect->vma; | |
682 | else | |
683 | adjustment = 0x200; /* just a wild assumption */ | |
684 | ||
685 | return adjustment; | |
686 | } | |
687 | ||
688 | ||
689 | /* | |
690 | * vmap_ldinfo - update VMAP info with ldinfo() information | |
691 | * | |
692 | * Input: | |
693 | * ldi - ^ to ldinfo() results. | |
694 | */ | |
695 | vmap_ldinfo(ldi) | |
696 | register struct ld_info *ldi; | |
697 | { | |
698 | struct stat ii, vi; | |
699 | register struct vmap *vp; | |
700 | register got_one, retried; | |
701 | CORE_ADDR ostart; | |
702 | ||
703 | /* | |
704 | * for each *ldi, see if we have a corresponding *vp | |
705 | * if so, update the mapping, and symbol table. | |
706 | * if not, add an entry and symbol table. | |
707 | */ | |
708 | do { | |
709 | char *name = ldi->ldinfo_filename; | |
710 | char *memb = name + strlen(name) + 1; | |
711 | ||
712 | retried = 0; | |
713 | ||
714 | if (fstat(ldi->ldinfo_fd, &ii) < 0) | |
715 | fatal("cannot fstat(%d) on %s" | |
716 | , ldi->ldinfo_fd | |
717 | , name); | |
718 | retry: | |
719 | for (got_one = 0, vp = vmap; vp; vp = vp->nxt) { | |
720 | FILE *io; | |
721 | ||
722 | /* The filenames are not always sufficient to match on. */ | |
723 | if ((name[0] == "/" | |
724 | && !eq(name, vp->name)) | |
725 | || (memb[0] && !eq(memb, vp->member))) | |
726 | continue; | |
727 | ||
728 | /* totally opaque! */ | |
729 | io = bfd_cache_lookup(vp->bfd); | |
730 | if (!io) | |
731 | fatal("cannot find BFD's iostream for %s" | |
732 | , vp->name); | |
733 | ||
734 | /* see if we are referring to the same file */ | |
735 | if (fstat(fileno(io), &vi) < 0) | |
736 | fatal("cannot fstat BFD for %s", vp->name); | |
737 | ||
738 | if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino) | |
739 | continue; | |
740 | ||
741 | if (!retried) | |
742 | close(ldi->ldinfo_fd); | |
743 | ||
744 | ++got_one; | |
745 | ||
746 | /* found a corresponding VMAP. remap! */ | |
747 | ostart = vp->tstart; | |
748 | ||
749 | vp->tstart = ldi->ldinfo_textorg; | |
750 | vp->tend = vp->tstart + ldi->ldinfo_textsize; | |
751 | vp->dstart = ldi->ldinfo_dataorg; | |
752 | vp->dend = vp->dstart + ldi->ldinfo_datasize; | |
753 | ||
754 | if (vp->tadj) { | |
755 | vp->tstart += vp->tadj; | |
756 | vp->tend += vp->tadj; | |
757 | } | |
758 | ||
759 | /* relocate symbol table(s). */ | |
760 | vmap_symtab(vp, ostart, &vi); | |
761 | ||
762 | /* there may be more, so we don't break out of the loop. */ | |
763 | } | |
764 | ||
765 | /* | |
766 | * if there was no matching *vp, we must perforce create | |
767 | * the sucker(s) | |
768 | */ | |
769 | if (!got_one && !retried) { | |
770 | add_vmap(ldi); | |
771 | ++retried; | |
772 | goto retry; | |
773 | } | |
774 | } while (ldi->ldinfo_next | |
775 | && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi))); | |
776 | ||
41abdfbd JG |
777 | } |
778 | ||
779 | /* | |
780 | * vmap_inferior - print VMAP info for inferior | |
781 | */ | |
782 | vmap_inferior() { | |
783 | ||
784 | if (inferior_pid == 0) | |
507e4004 | 785 | return 0; /* normal processing */ |
41abdfbd JG |
786 | |
787 | exec_files_info(); | |
41abdfbd JG |
788 | return 1; |
789 | } | |
790 | ||
791 | /* Read or write the exec file. | |
792 | ||
793 | Args are address within exec file, address within gdb address-space, | |
794 | length, and a flag indicating whether to read or write. | |
795 | ||
796 | Result is a length: | |
797 | ||
798 | 0: We cannot handle this address and length. | |
799 | > 0: We have handled N bytes starting at this address. | |
800 | (If N == length, we did it all.) We might be able | |
801 | to handle more bytes beyond this length, but no | |
802 | promises. | |
803 | < 0: We cannot handle this address, but if somebody | |
804 | else handles (-N) bytes, we can start from there. | |
805 | ||
806 | The same routine is used to handle both core and exec files; | |
807 | we just tail-call it with more arguments to select between them. */ | |
808 | ||
809 | int | |
818de002 | 810 | xfer_memory (memaddr, myaddr, len, write, target) |
41abdfbd JG |
811 | CORE_ADDR memaddr; |
812 | char *myaddr; | |
813 | int len; | |
814 | int write; | |
818de002 | 815 | struct target_ops *target; |
41abdfbd JG |
816 | { |
817 | boolean res; | |
818 | struct section_table *p; | |
819 | CORE_ADDR nextsectaddr, memend; | |
818de002 | 820 | boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); |
41abdfbd JG |
821 | |
822 | if (len <= 0) | |
823 | abort(); | |
824 | ||
825 | memend = memaddr + len; | |
826 | xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents; | |
827 | nextsectaddr = memend; | |
828 | ||
818de002 | 829 | for (p = target->to_sections; p < target->to_sections_end; p++) |
41abdfbd JG |
830 | { |
831 | if (p->addr <= memaddr) | |
832 | if (p->endaddr >= memend) | |
833 | { | |
834 | /* Entire transfer is within this section. */ | |
818de002 | 835 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
41abdfbd JG |
836 | return (res != false)? len: 0; |
837 | } | |
838 | else if (p->endaddr <= memaddr) | |
839 | { | |
840 | /* This section ends before the transfer starts. */ | |
841 | continue; | |
842 | } | |
843 | else | |
844 | { | |
845 | /* This section overlaps the transfer. Just do half. */ | |
846 | len = p->endaddr - memaddr; | |
818de002 | 847 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
41abdfbd JG |
848 | return (res != false)? len: 0; |
849 | } | |
850 | else if (p->addr < nextsectaddr) | |
851 | nextsectaddr = p->addr; | |
852 | } | |
853 | ||
854 | if (nextsectaddr >= memend) | |
855 | return 0; /* We can't help */ | |
856 | else | |
857 | return - (nextsectaddr - memaddr); /* Next boundary where we can help */ | |
858 | } | |
859 | ||
818de002 PB |
860 | void |
861 | print_section_info (t, abfd) | |
862 | struct target_ops *t; | |
863 | bfd *abfd; | |
41abdfbd | 864 | { |
818de002 | 865 | struct section_table *p; |
41abdfbd | 866 | |
818de002 PB |
867 | printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); |
868 | wrap_here (" "); | |
869 | printf_filtered ("file type %s.\n", bfd_get_target(abfd)); | |
870 | ||
871 | for (p = t->to_sections; p < t->to_sections_end; p++) { | |
872 | printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08")); | |
873 | printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08")); | |
874 | if (info_verbose) | |
875 | printf_filtered (" @ %s", | |
876 | local_hex_string_custom (p->sec_ptr->filepos, "08")); | |
877 | printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr)); | |
878 | if (p->bfd != abfd) { | |
879 | printf_filtered (" in %s", bfd_get_filename (p->bfd)); | |
880 | } | |
881 | printf_filtered ("\n"); | |
882 | } | |
818de002 PB |
883 | } |
884 | ||
507e4004 | 885 | |
818de002 PB |
886 | static void |
887 | exec_files_info (t) | |
888 | struct target_ops *t; | |
889 | { | |
507e4004 PB |
890 | register struct vmap *vp = vmap; |
891 | ||
818de002 | 892 | print_section_info (t, exec_bfd); |
507e4004 PB |
893 | |
894 | if (!vp) | |
895 | return; | |
896 | ||
897 | printf("\n\tMapping info for file `%s'.\n", vp->name); | |
898 | printf("\t %8.8s %8.8s %8.8s %s\n", | |
899 | "start", "end", "section", "file(member)"); | |
900 | ||
901 | for (; vp; vp = vp->nxt) | |
902 | printf("\t0x%8.8x 0x%8.8x %s%s%s%s\n", | |
903 | vp->tstart, | |
904 | vp->tend, | |
905 | vp->name, | |
906 | vp->member ? "(" : "", | |
907 | vp->member, | |
908 | vp->member ? ")" : ""); | |
41abdfbd JG |
909 | } |
910 | ||
911 | #ifdef DAMON | |
76b28d05 | 912 | /* Damon's implementation of set_section_command! It is based on the sex member |
41abdfbd JG |
913 | (which is a section pointer from vmap) of vmap. |
914 | We will not have multiple vmap entries (one for each section), rather transmit | |
915 | text and data base offsets and fix them at the same time. Elimination of sex | |
916 | entry in vmap make this function obsolute, use the one from exec.c. | |
76b28d05 | 917 | Need further testing!! FIXMEmgo. */ |
41abdfbd JG |
918 | |
919 | static void | |
920 | set_section_command(args, from_tty) | |
921 | char *args; | |
922 | { | |
923 | register struct vmap *vp = vmap; | |
924 | char *secname; | |
925 | unsigned seclen; | |
926 | unsigned long secaddr; | |
927 | char secprint[100]; | |
928 | long offset; | |
929 | ||
930 | if (args == 0) | |
931 | error("Must specify section name and its virtual address"); | |
932 | ||
933 | /* Parse out section name */ | |
934 | for (secname = args; !isspace(*args); args++) | |
935 | ; | |
936 | seclen = args - secname; | |
937 | ||
938 | /* Parse out new virtual address */ | |
939 | secaddr = parse_and_eval_address(args); | |
940 | ||
941 | for (vp = vmap; vp; vp = vp->nxt) { | |
942 | if (!strncmp(secname | |
943 | , bfd_section_name(vp->bfd, vp->sex), seclen) | |
944 | && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') { | |
945 | offset = secaddr - vp->tstart; | |
946 | vp->tstart += offset; | |
947 | vp->tend += offset; | |
948 | exec_files_info(); | |
949 | return; | |
950 | } | |
951 | } | |
952 | ||
953 | if (seclen >= sizeof(secprint)) | |
954 | seclen = sizeof(secprint) - 1; | |
955 | strncpy(secprint, secname, seclen); | |
956 | secprint[seclen] = '\0'; | |
957 | error("Section %s not found", secprint); | |
958 | } | |
959 | #else | |
960 | static void | |
961 | set_section_command (args, from_tty) | |
962 | char *args; | |
963 | int from_tty; | |
964 | { | |
965 | struct section_table *p; | |
966 | char *secname; | |
967 | unsigned seclen; | |
968 | unsigned long secaddr; | |
969 | char secprint[100]; | |
970 | long offset; | |
971 | ||
972 | if (args == 0) | |
973 | error ("Must specify section name and its virtual address"); | |
974 | ||
975 | /* Parse out section name */ | |
976 | for (secname = args; !isspace(*args); args++) ; | |
977 | seclen = args - secname; | |
978 | ||
979 | /* Parse out new virtual address */ | |
980 | secaddr = parse_and_eval_address (args); | |
981 | ||
507e4004 | 982 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { |
41abdfbd JG |
983 | if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) |
984 | && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { | |
985 | offset = secaddr - p->addr; | |
986 | p->addr += offset; | |
987 | p->endaddr += offset; | |
507e4004 PB |
988 | if (from_tty) |
989 | exec_files_info(&exec_ops); | |
41abdfbd JG |
990 | return; |
991 | } | |
992 | } | |
993 | if (seclen >= sizeof (secprint)) | |
994 | seclen = sizeof (secprint) - 1; | |
995 | strncpy (secprint, secname, seclen); | |
996 | secprint[seclen] = '\0'; | |
997 | error ("Section %s not found", secprint); | |
998 | } | |
999 | ||
1000 | #endif /* !DAMON */ | |
1001 | ||
1002 | struct target_ops exec_ops = { | |
1003 | "exec", "Local exec file", | |
1004 | "Use an executable file as a target.\n\ | |
1005 | Specify the filename of the executable file.", | |
1006 | exec_file_command, exec_close, /* open, close */ | |
1007 | child_attach, 0, 0, 0, /* attach, detach, resume, wait, */ | |
1008 | 0, 0, /* fetch_registers, store_registers, */ | |
1009 | 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */ | |
818de002 | 1010 | xfer_memory, exec_files_info, |
41abdfbd JG |
1011 | 0, 0, /* insert_breakpoint, remove_breakpoint, */ |
1012 | 0, 0, 0, 0, 0, /* terminal stuff */ | |
1013 | 0, 0, /* kill, load */ | |
e17960fb | 1014 | 0, /* lookup sym */ |
41abdfbd JG |
1015 | child_create_inferior, |
1016 | 0, /* mourn_inferior */ | |
1017 | file_stratum, 0, /* next */ | |
1018 | 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ | |
1019 | 0, 0, /* section pointers */ | |
1020 | OPS_MAGIC, /* Always the last thing */ | |
1021 | }; | |
1022 | ||
1023 | ||
1024 | void | |
1025 | _initialize_exec() | |
1026 | { | |
1027 | ||
1028 | add_com("file", class_files, file_command, | |
1029 | "Use FILE as program to be debugged.\n\ | |
1030 | It is read for its symbols, for getting the contents of pure memory,\n\ | |
1031 | and it is the program executed when you use the `run' command.\n\ | |
1032 | If FILE cannot be found as specified, your execution directory path\n\ | |
1033 | ($PATH) is searched for a command of that name.\n\ | |
1034 | No arg means to have no executable file and no symbols."); | |
1035 | ||
1036 | add_com("exec-file", class_files, exec_file_command, | |
1037 | "Use FILE as program for getting contents of pure memory.\n\ | |
1038 | If FILE cannot be found as specified, your execution directory path\n\ | |
1039 | is searched for a command of that name.\n\ | |
1040 | No arg means have no executable file."); | |
1041 | ||
1042 | add_com("section", class_files, set_section_command, | |
1043 | "Change the base address of section SECTION of the exec file to ADDR.\n\ | |
1044 | This can be used if the exec file does not contain section addresses,\n\ | |
1045 | (such as in the a.out format), or when the addresses specified in the\n\ | |
1046 | file itself are wrong. Each section must be changed separately. The\n\ | |
1047 | ``info files'' command lists all the sections and their addresses."); | |
1048 | ||
1049 | add_target(&exec_ops); | |
1050 | } |