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