]> Git Repo - binutils.git/blob - gdb/xcoffexec.c
* xcoffexec.c (exec_close): Don't close exec_bfd twice.
[binutils.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 /* true, if symbol table and minimal symbol table are relocated. */
305
306 int symtab_relocated = 0;
307
308
309 /*  vmap_symtab -       handle symbol translation on vmapping */
310
311 vmap_symtab(vp, old_start, vip)
312 register struct vmap *vp;
313 CORE_ADDR old_start;
314 struct stat *vip; 
315 {
316   register struct symtab *s;
317   register struct objfile *objfile;
318   register struct minimal_symbol *msymbol;
319   
320   objfile = vp->objfile;
321   if (objfile == NULL)
322     {
323       /* OK, it's not an objfile we opened ourselves.
324          Currently, that can only happen with the exec file, so
325          relocate the symbols for the symfile.  */
326       if (symfile_objfile == NULL)
327         return;
328       objfile = symfile_objfile;
329     }
330
331   s = objfile->symtabs;
332
333         if (vp->tstart != old_start) {
334
335           /* Once we find a relocation base address for one of the symtabs
336              in this objfile, it will be the same for all symtabs in this
337              objfile. Clean this algorithm. FIXME. */
338
339           for (; s; s = s->next)
340             if (!s->nonreloc || LINETABLE(s))
341                 vmap_symtab_1(s, vp, old_start);
342
343 #if 1
344           /* I believe trampoline entries now have a name like
345              <trampoline>.  In any event, if something needs to be changed,
346              it should be changed in ALL_MSYMBOLS, so it works everywhere.  */
347           /*
348           Himm.., recently we nullified trampoline entry names in order not
349           to confuse them with real symbols.  Appearently this turned into a
350           problem, and msymbol vector did not get relocated properly.  If
351           msymbols have to have non-null names, then we should name
352           trampoline entries with empty strings. */
353
354           ALL_MSYMBOLS (objfile, msymbol)
355 #else
356           for (msymbol = objfile->msymbols;
357                SYMBOL_NAME (msymbol) || SYMBOL_VALUE_ADDRESS (msymbol);
358                (msymbol)++)
359 #endif
360               if (SYMBOL_VALUE_ADDRESS (msymbol) < TEXT_SEGMENT_BASE)
361                 SYMBOL_VALUE_ADDRESS (msymbol) += vp->tstart - old_start;
362
363         }
364
365   if (vp->tstart != old_start) {
366     /* breakpoints need to be relocated as well. */
367     fixup_breakpoints (0, TEXT_SEGMENT_BASE, vp->tstart - old_start);
368   }
369   
370   symtab_relocated = 1;
371 }
372
373
374 vmap_symtab_1(s, vp, old_start)
375 register struct symtab *s;
376 register struct vmap *vp;
377 CORE_ADDR old_start; 
378 {
379     register int i, j;
380     int len, blen;
381     register struct linetable *l;
382     struct blockvector *bv;
383     register struct block *b;
384     int depth;
385     register ulong reloc, dreloc;
386     
387     if ((reloc = vp->tstart - old_start) == 0)
388         return;
389
390     dreloc = vp->dstart;                        /* data relocation */
391
392     /*
393      * The line table must be relocated.  This is only present for
394      * .text sections, so only vp->text type maps need be considered.
395      */
396     l = LINETABLE (s);
397     if (l) {
398       len = l->nitems;
399       for (i = 0; i < len; i++)
400         l->item[i].pc += reloc;
401     }
402
403     /* if this symbol table is not relocatable, only line table should
404        be relocated and the rest ignored. */
405     if (s->nonreloc)
406       return;
407     
408     bv  = BLOCKVECTOR(s);
409     len = BLOCKVECTOR_NBLOCKS(bv);
410     
411     for (i = 0; i < len; i++) {
412         b = BLOCKVECTOR_BLOCK(bv, i);
413         
414         BLOCK_START(b) += reloc;
415         BLOCK_END(b)   += reloc;
416         
417         blen = BLOCK_NSYMS(b);
418         for (j = 0; j < blen; j++) {
419             register struct symbol *sym;
420             
421             sym = BLOCK_SYM(b, j);
422             switch (SYMBOL_NAMESPACE(sym)) {
423               case STRUCT_NAMESPACE:
424               case UNDEF_NAMESPACE:
425                 continue;
426                 
427               case LABEL_NAMESPACE:
428               case VAR_NAMESPACE:
429                 break;
430             }
431             
432             switch (SYMBOL_CLASS(sym)) {
433               case LOC_CONST:
434               case LOC_CONST_BYTES:
435               case LOC_LOCAL:
436               case LOC_REGISTER:
437               case LOC_ARG:
438               case LOC_LOCAL_ARG:
439               case LOC_REF_ARG:
440               case LOC_REGPARM:
441               case LOC_TYPEDEF:
442                 continue;
443                 
444 #ifdef FIXME
445               case LOC_EXTERNAL:
446 #endif
447               case LOC_LABEL:
448                 SYMBOL_VALUE_ADDRESS(sym) += reloc;
449                 break;
450
451               case LOC_STATIC:
452                 SYMBOL_VALUE_ADDRESS(sym) += dreloc;
453                 break;
454
455               case LOC_BLOCK:
456                 break;
457                 
458               default:
459                 fatal("botched symbol class %x"
460                       , SYMBOL_CLASS(sym));
461                 break;
462             }
463         }
464     }
465 }
466
467 /* Add symbols for an objfile.  */
468 static int
469 objfile_symbol_add (arg)
470      char *arg;
471 {
472   struct objfile *obj = (struct objfile *) arg;
473   syms_from_objfile (obj, 0, 0, 0);
474   new_symfile_objfile (obj, 0, 0);
475   return 1;
476 }
477
478 static struct vmap *add_vmap PARAMS ((struct ld_info *));
479
480 /* Add a new vmap entry based on ldinfo() information.
481
482    If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
483    core file), the caller should set it to -1, and we will open the file.
484
485    Return the vmap new entry.  */
486 static struct vmap *
487 add_vmap(ldi)
488      register struct ld_info *ldi; 
489 {
490         bfd *bfd, *last;
491         register char *mem, *objname;
492         struct objfile *obj;
493         struct vmap *vp;
494
495         /* This ldi structure was allocated using alloca() in 
496            xcoff_relocate_symtab(). Now we need to have persistent object 
497            and member names, so we should save them. */
498
499         mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1;
500         mem = savestring (mem, strlen (mem));
501         objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
502
503         if (ldi->ldinfo_fd < 0)
504           /* Note that this opens it once for every member; a possible
505              enhancement would be to only open it once for every object.  */
506           bfd = bfd_openr (objname, NULL);
507         else
508           bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd);
509         if (!bfd)
510           error("Could not open `%s' as an executable file: %s",
511                                         objname, bfd_errmsg(bfd_error));
512
513
514         /* make sure we have an object file */
515
516         if (bfd_check_format(bfd, bfd_object))
517           vp = map_vmap (bfd, 0);
518
519         else if (bfd_check_format(bfd, bfd_archive)) {
520                 last = 0;
521                 /*
522                  * FIXME??? am I tossing BFDs?  bfd?
523                  */
524                 while (last = bfd_openr_next_archived_file(bfd, last))
525                         if (STREQ(mem, last->filename))
526                                 break;
527
528                 if (!last) {
529                   bfd_close(bfd);
530                   /* FIXME -- should be error */
531                   warning("\"%s\": member \"%s\" missing.", bfd->filename, mem);
532                   return;
533                 }
534
535                 if (!bfd_check_format(last, bfd_object)) {
536                         bfd_close(last);        /* XXX???       */
537                         goto obj_err;
538                 }
539
540                 vp = map_vmap (last, bfd);
541         }
542         else {
543             obj_err:
544                 bfd_close(bfd);
545                 error ("\"%s\": not in executable format: %s.",
546                        objname, bfd_errmsg(bfd_error));
547                 /*NOTREACHED*/
548         }
549         obj = allocate_objfile (vp->bfd, 0);
550         vp->objfile = obj;
551
552 #ifndef SOLIB_SYMBOLS_MANUAL
553         if (catch_errors (objfile_symbol_add, (char *)obj,
554                           "Error while reading shared library symbols:\n"))
555           {
556             /* Note this is only done if symbol reading was successful.  */
557             vmap_symtab (vp, 0, 0);
558             vp->loaded = 1;
559           }
560 #endif
561         return vp;
562 }
563
564
565 /* As well as symbol tables, exec_sections need relocation. After
566    the inferior process' termination, there will be a relocated symbol
567    table exist with no corresponding inferior process. At that time, we
568    need to use `exec' bfd, rather than the inferior process's memory space
569    to look up symbols.
570
571    `exec_sections' need to be relocated only once, as long as the exec
572    file remains unchanged.
573 */
574 vmap_exec ()
575 {
576   static bfd *execbfd;
577   int i;
578
579   if (execbfd == exec_bfd)
580     return;
581
582   execbfd = exec_bfd;
583
584   if (!vmap || !exec_ops.to_sections)
585     error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
586
587   for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
588     {
589       if (STREQ(".text", exec_ops.to_sections[i].sec_ptr->name))
590         {
591           exec_ops.to_sections[i].addr += vmap->tstart;
592           exec_ops.to_sections[i].endaddr += vmap->tstart;
593         }
594       else if (STREQ(".data", exec_ops.to_sections[i].sec_ptr->name))
595         {
596           exec_ops.to_sections[i].addr += vmap->dstart;
597           exec_ops.to_sections[i].endaddr += vmap->dstart;
598         }
599     }
600 }
601
602 #if 0
603 /* This was for the old, half-assed, core file support.  */
604 int
605 text_adjustment (abfd)
606 bfd *abfd;
607 {
608   static bfd *execbfd;
609   static int adjustment;
610   sec_ptr  sect;
611
612   if (exec_bfd == execbfd)
613     return adjustment;
614
615   sect = bfd_get_section_by_name (abfd, ".text");
616   if (sect)
617     adjustment = sect->filepos - sect->vma;
618   else
619     adjustment = 0x200;                         /* just a wild assumption */
620
621   return adjustment;
622 }
623 #endif
624
625 /*
626  * vmap_ldinfo -        update VMAP info with ldinfo() information
627  *
628  * Input:
629  *      ldi     -       ^ to ldinfo() results.
630  */
631 vmap_ldinfo(ldi)
632 register struct ld_info *ldi;
633 {
634   struct stat ii, vi;
635   register struct vmap *vp;
636   register got_one, retried;
637   CORE_ADDR ostart;
638
639   /*
640    * for each *ldi, see if we have a corresponding *vp
641    *    if so, update the mapping, and symbol table.
642    *    if not, add an entry and symbol table.
643    */
644   do {
645         char *name = ldi->ldinfo_filename;
646         char *memb = name + strlen(name) + 1;
647
648         retried = 0;
649
650         if (fstat(ldi->ldinfo_fd, &ii) < 0)
651                 fatal("cannot fstat(%d) on %s"
652                       , ldi->ldinfo_fd
653                       , name);
654 retry:
655         for (got_one = 0, vp = vmap; vp; vp = vp->nxt) {
656           FILE *io;
657
658           /* First try to find a `vp', which is the same as in ldinfo.
659              If not the same, just continue and grep the next `vp'. If same,
660              relocate its tstart, tend, dstart, dend values. If no such `vp'
661              found, get out of this for loop, add this ldi entry as a new vmap
662              (add_vmap) and come back, fins its `vp' and so on... */
663
664           /* The filenames are not always sufficient to match on. */
665
666           if ((name[0] == '/' && !STREQ(name, vp->name))
667                 || (memb[0] && !STREQ(memb, vp->member)))
668             continue;
669
670           io = bfd_cache_lookup(vp->bfd);               /* totally opaque! */
671           if (!io)
672             fatal("cannot find BFD's iostream for %s", vp->name);
673
674           /* see if we are referring to the same file */
675
676           if (fstat(fileno(io), &vi) < 0)
677             fatal("cannot fstat BFD for %s", vp->name);
678
679           if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
680             continue;
681
682           if (!retried)
683             close(ldi->ldinfo_fd);
684
685           ++got_one;
686
687           /* found a corresponding VMAP. remap! */
688           ostart = vp->tstart;
689
690           vp->tstart = ldi->ldinfo_textorg;
691           vp->tend   = vp->tstart + ldi->ldinfo_textsize;
692           vp->dstart = ldi->ldinfo_dataorg;
693           vp->dend   = vp->dstart + ldi->ldinfo_datasize;
694
695           if (vp->tadj) {
696             vp->tstart += vp->tadj;
697             vp->tend   += vp->tadj;
698           }
699
700           /* relocate symbol table(s). */
701           vmap_symtab(vp, ostart, &vi);
702
703           /* there may be more, so we don't break out of the loop. */
704         }
705
706         /* if there was no matching *vp, we must perforce create the sucker(s) */
707         if (!got_one && !retried) {
708           add_vmap(ldi);
709           ++retried;
710           goto retry;
711         }
712   } while (ldi->ldinfo_next
713          && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
714
715 }
716
717 /*
718  * vmap_inferior -      print VMAP info for inferior
719  */
720 vmap_inferior() {
721
722         if (inferior_pid == 0)
723           return 0;                             /* normal processing    */
724
725         exec_files_info();
726         return 1;
727 }
728
729 /* Read or write the exec file.
730
731    Args are address within exec file, address within gdb address-space,
732    length, and a flag indicating whether to read or write.
733
734    Result is a length:
735
736         0:    We cannot handle this address and length.
737         > 0:  We have handled N bytes starting at this address.
738               (If N == length, we did it all.)  We might be able
739               to handle more bytes beyond this length, but no
740               promises.
741         < 0:  We cannot handle this address, but if somebody
742               else handles (-N) bytes, we can start from there.
743
744     The same routine is used to handle both core and exec files;
745     we just tail-call it with more arguments to select between them.  */
746
747 int
748 xfer_memory (memaddr, myaddr, len, write, target)
749      CORE_ADDR memaddr;
750      char *myaddr;
751      int len;
752      int write;
753      struct target_ops *target;
754 {
755   boolean res;
756   struct section_table *p;
757   CORE_ADDR nextsectaddr, memend;
758   boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
759
760   if (len <= 0)
761     abort();
762
763   memend = memaddr + len;
764   xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
765   nextsectaddr = memend;
766
767   for (p = target->to_sections; p < target->to_sections_end; p++)
768     {
769       if (p->addr <= memaddr)
770         if (p->endaddr >= memend)
771           {
772             /* Entire transfer is within this section.  */
773             res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
774             return (res != false)? len: 0;
775           }
776         else if (p->endaddr <= memaddr)
777           {
778             /* This section ends before the transfer starts.  */
779             continue;
780           }
781         else 
782           {
783             /* This section overlaps the transfer.  Just do half.  */
784             len = p->endaddr - memaddr;
785             res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
786             return (res != false)? len: 0;
787           }
788       else if (p->addr < nextsectaddr)
789         nextsectaddr = p->addr;
790     }
791
792   if (nextsectaddr >= memend)
793     return 0;                           /* We can't help */
794   else
795     return - (nextsectaddr - memaddr);  /* Next boundary where we can help */
796 }
797
798 void
799 print_section_info (t, abfd)
800   struct target_ops *t;
801   bfd *abfd;
802 {
803   struct section_table *p;
804
805   printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
806   wrap_here ("        ");
807   printf_filtered ("file type %s.\n", bfd_get_target(abfd));
808
809   for (p = t->to_sections; p < t->to_sections_end; p++) {
810     printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08"));
811     printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08"));
812     if (info_verbose)
813       printf_filtered (" @ %s",
814                        local_hex_string_custom (p->sec_ptr->filepos, "08"));
815     printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr));
816     if (p->bfd != abfd) {
817       printf_filtered (" in %s", bfd_get_filename (p->bfd));
818     }
819     printf_filtered ("\n");
820   }
821 }
822
823
824 static void
825 exec_files_info (t)
826   struct target_ops *t;
827 {
828   register struct vmap *vp = vmap;
829
830   print_section_info (t, exec_bfd);
831
832   if (!vp)
833     return;
834
835   printf("\tMapping info for file `%s'.\n", vp->name);
836
837   printf("\t  %8.8s   %8.8s   %8.8s   %8.8s %8.8s %s\n",
838     "tstart", "tend", "dstart", "dend", "section", "file(member)");
839
840   for (; vp; vp = vp->nxt)
841      printf("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
842         vp->tstart,
843         vp->tend,
844         vp->dstart,
845         vp->dend,
846         vp->name,
847         *vp->member ? "(" : "",
848         vp->member,
849         *vp->member ? ")" : "");
850 }
851
852 #ifdef DAMON
853 /*  Damon's implementation of set_section_command! It is based on the sex member
854   (which is a section pointer from vmap) of vmap.
855   We will not have multiple vmap entries (one for each section), rather transmit
856   text and data base offsets and fix them at the same time. Elimination of sex
857   entry in vmap make this function obsolute, use the one from exec.c. 
858   Need further testing!!        FIXMEmgo.  */
859
860 static void
861 set_section_command(args, from_tty)
862 char *args; 
863 {
864         register struct vmap *vp = vmap;
865         char *secname;
866         unsigned seclen;
867         unsigned long secaddr;
868         char secprint[100];
869         long offset;
870
871         if (args == 0)
872                 error("Must specify section name and its virtual address");
873
874         /* Parse out section name */
875         for (secname = args; !isspace(*args); args++)
876                 ;
877         seclen = args - secname;
878
879         /* Parse out new virtual address */
880         secaddr = parse_and_eval_address(args);
881
882         for (vp = vmap; vp; vp = vp->nxt) {
883                 if (!strncmp(secname
884                              , bfd_section_name(vp->bfd, vp->sex), seclen)
885                     && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
886                         offset = secaddr - vp->tstart;
887                         vp->tstart += offset;
888                         vp->tend   += offset;
889                         exec_files_info();
890                         return;
891                 }
892         } 
893
894         if (seclen >= sizeof(secprint))
895                 seclen = sizeof(secprint) - 1;
896         strncpy(secprint, secname, seclen);
897         secprint[seclen] = '\0';
898         error("Section %s not found", secprint);
899 }
900 #else
901 static void
902 set_section_command (args, from_tty)
903      char *args;
904      int from_tty;
905 {
906   struct section_table *p;
907   char *secname;
908   unsigned seclen;
909   unsigned long secaddr;
910   char secprint[100];
911   long offset;
912
913   if (args == 0)
914     error ("Must specify section name and its virtual address");
915
916   /* Parse out section name */
917   for (secname = args; !isspace(*args); args++) ;
918   seclen = args - secname;
919
920   /* Parse out new virtual address */
921   secaddr = parse_and_eval_address (args);
922
923   for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
924     if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
925         && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
926       offset = secaddr - p->addr;
927       p->addr += offset;
928       p->endaddr += offset;
929       if (from_tty)
930         exec_files_info(&exec_ops);
931       return;
932     }
933   } 
934   if (seclen >= sizeof (secprint))
935     seclen = sizeof (secprint) - 1;
936   strncpy (secprint, secname, seclen);
937   secprint[seclen] = '\0';
938   error ("Section %s not found", secprint);
939 }
940
941 #endif /* !DAMON */
942
943 struct target_ops exec_ops = {
944         "exec", "Local exec file",
945         "Use an executable file as a target.\n\
946 Specify the filename of the executable file.",
947         exec_file_command, exec_close, /* open, close */
948         find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
949         0, 0, /* fetch_registers, store_registers, */
950         0, /* prepare_to_store */
951         xfer_memory, exec_files_info,
952         0, 0, /* insert_breakpoint, remove_breakpoint, */
953         0, 0, 0, 0, 0, /* terminal stuff */
954         0, 0, /* kill, load */
955         0, /* lookup sym */
956         find_default_create_inferior,
957         0, /* mourn_inferior */
958         0, /* can_run */
959         0, /* notice_signals */
960         file_stratum, 0, /* next */
961         0, 1, 0, 0, 0,  /* all mem, mem, stack, regs, exec */
962         0, 0,                   /* section pointers */
963         OPS_MAGIC,              /* Always the last thing */
964 };
965 \f
966 /* Core file stuff.  */
967
968 /* Relocate symtabs and read in shared library info, based on symbols
969    from the core file.  */
970 void
971 xcoff_relocate_core ()
972 {
973 /* Offset of member MEMBER in a struct of type TYPE.  */
974 #ifndef offsetof
975 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
976 #endif
977
978 /* Size of a struct ld_info except for the variable-length filename.  */
979 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
980
981   sec_ptr ldinfo_sec;
982   int offset = 0;
983   struct ld_info *ldip;
984   struct vmap *vp;
985   
986   /* Allocated size of buffer.  */
987   int buffer_size = LDINFO_SIZE;
988   char *buffer = xmalloc (buffer_size);
989   struct cleanup *old = make_cleanup (free_current_contents, &buffer);
990     
991   ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
992   if (ldinfo_sec == NULL)
993     {
994 bfd_err:
995       fprintf_filtered (stderr, "Couldn't get ldinfo from core file: %s\n",
996                         bfd_errmsg (bfd_error));
997       do_cleanups (old);
998       return;
999     }
1000   do
1001     {
1002       int i;
1003       int names_found = 0;
1004
1005       /* Read in everything but the name.  */
1006       if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
1007                                     offset, LDINFO_SIZE) == 0)
1008         goto bfd_err;
1009
1010       /* Now the name.  */
1011       i = LDINFO_SIZE;
1012       do
1013         {
1014           if (i == buffer_size)
1015             {
1016               buffer_size *= 2;
1017               buffer = xrealloc (buffer, buffer_size);
1018             }
1019           if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
1020                                         offset + i, 1) == 0)
1021             goto bfd_err;
1022           if (buffer[i++] == '\0')
1023             ++names_found;
1024         } while (names_found < 2);
1025
1026       ldip = (struct ld_info *)buffer;
1027
1028       /* Can't use a file descriptor from the core file; need to open it.  */
1029       ldip->ldinfo_fd = -1;
1030       
1031       /* The first ldinfo is for the exec file, allocated elsewhere.  */
1032       if (offset == 0)
1033         vp = vmap;
1034       else
1035         vp = add_vmap (ldip);
1036
1037       offset += ldip->ldinfo_next;
1038
1039       vp->tstart = ldip->ldinfo_textorg;
1040       vp->tend = vp->tstart + ldip->ldinfo_textsize;
1041       vp->dstart = ldip->ldinfo_dataorg;
1042       vp->dend = vp->dstart + ldip->ldinfo_datasize;
1043
1044       if (vp->tadj != 0) {
1045         vp->tstart += vp->tadj;
1046         vp->tend += vp->tadj;
1047       }
1048
1049       /* Unless this is the exec file,
1050          add our sections to the section table for the core target.  */
1051       if (vp != vmap)
1052         {
1053           int count;
1054           struct section_table *stp;
1055           
1056           count = core_ops.to_sections_end - core_ops.to_sections;
1057           count += 2;
1058           core_ops.to_sections = (struct section_table *)
1059             xrealloc (core_ops.to_sections,
1060                       sizeof (struct section_table) * count);
1061           core_ops.to_sections_end = core_ops.to_sections + count;
1062           stp = core_ops.to_sections_end - 2;
1063
1064           /* "Why do we add bfd_section_vma?", I hear you cry.
1065              Well, the start of the section in the file is actually
1066              that far into the section as the struct vmap understands it.
1067              So for text sections, bfd_section_vma tends to be 0x200,
1068              and if vp->tstart is 0xd0002000, then the first byte of
1069              the text section on disk corresponds to address 0xd0002200.  */
1070           stp->bfd = vp->bfd;
1071           stp->sec_ptr = bfd_get_section_by_name (stp->bfd, ".text");
1072           stp->addr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->tstart;
1073           stp->endaddr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->tend;
1074           stp++;
1075           
1076           stp->bfd = vp->bfd;
1077           stp->sec_ptr = bfd_get_section_by_name (stp->bfd, ".data");
1078           stp->addr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->dstart;
1079           stp->endaddr = bfd_section_vma (stp->bfd, stp->sec_ptr) + vp->dend;
1080         }
1081
1082       vmap_symtab (vp, 0, 0);
1083
1084       add_text_to_loadinfo (ldip->ldinfo_textorg, ldip->ldinfo_dataorg);
1085     } while (ldip->ldinfo_next != 0);
1086   vmap_exec ();
1087   do_cleanups (old);
1088 }
1089
1090 void
1091 _initialize_exec()
1092 {
1093
1094   add_com("file", class_files, file_command,
1095            "Use FILE as program to be debugged.\n\
1096 It is read for its symbols, for getting the contents of pure memory,\n\
1097 and it is the program executed when you use the `run' command.\n\
1098 If FILE cannot be found as specified, your execution directory path\n\
1099 ($PATH) is searched for a command of that name.\n\
1100 No arg means to have no executable file and no symbols.");
1101
1102   add_com("exec-file", class_files, exec_file_command,
1103            "Use FILE as program for getting contents of pure memory.\n\
1104 If FILE cannot be found as specified, your execution directory path\n\
1105 is searched for a command of that name.\n\
1106 No arg means have no executable file.");
1107
1108   add_com("section", class_files, set_section_command,
1109    "Change the base address of section SECTION of the exec file to ADDR.\n\
1110 This can be used if the exec file does not contain section addresses,\n\
1111 (such as in the a.out format), or when the addresses specified in the\n\
1112 file itself are wrong.  Each section must be changed separately.  The\n\
1113 ``info files'' command lists all the sections and their addresses.");
1114
1115   add_target(&exec_ops);
1116 }
This page took 0.132209 seconds and 4 git commands to generate.