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