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