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