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