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