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