Speed up GDB startup time by not demangling partial symbols.
[platform/upstream/binutils.git] / gdb / xcoffexec.c
1 /* Execute AIXcoff files, for GDB.
2    Copyright 1988, 1989, 1991, 1992, 1994 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
33 #include "frame.h"
34 #include "inferior.h"
35 #include "target.h"
36 #include "gdbcmd.h"
37 #include "gdbcore.h"
38 #include "language.h"
39 #include "symfile.h"
40 #include "objfiles.h"
41
42 #include "bfd.h"
43 #include "xcoffsolib.h"
44
45 /* Prototypes for local functions */
46
47 static void
48 file_command PARAMS ((char *, int));
49
50 static void
51 exec_close PARAMS ((int));
52
53 struct vmap *
54 map_vmap PARAMS ((bfd *, bfd *));
55
56 struct section_table *exec_sections, *exec_sections_end;
57
58 /* Whether to open exec and core files read-only or read-write.  */
59
60 int write_files = 0;
61
62 extern int info_verbose;
63
64 bfd *exec_bfd;                  /* needed by core.c     */
65
66 extern char *getenv();
67 extern void add_syms_addr_command ();
68 extern void symbol_file_command ();
69 static void exec_files_info();
70
71 struct vmap *vmap;              /* current vmap */
72
73 extern struct target_ops exec_ops;
74
75 /* exec_close - done with exec file, clean up all resources. */
76
77 static void
78 exec_close (quitting)
79      int quitting;
80 {
81   register struct vmap *vp, *nxt;
82   int need_symtab_cleanup = 0;
83   
84   for (nxt = vmap; nxt; )
85     {
86       vp = nxt;
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         {
94           free_objfile (vp->objfile);
95           need_symtab_cleanup = 1;
96         }
97       else
98         bfd_close (vp->bfd);
99
100       /* FIXME: This routine is #if 0'd in symfile.c.  What should we
101          be doing here?  Should we just free everything in
102          vp->objfile->symtabs?  Should free_objfile do that?  */
103       free_named_symtabs (vp->name);
104       free (vp);
105     }
106   
107   vmap = 0;
108
109   /* exec_bfd was already closed (the exec file has a vmap entry).  */
110   exec_bfd = NULL;
111
112   if (exec_ops.to_sections)
113     {
114       /* Reflect to_sections freeing in current_target if it is
115          the exec target. Otherwise target_xfer_memory (eventually
116          called from clear_symtab_users) might access the freed
117          exec_ops.to_sections via the copy in current_target.  */
118       if (current_target.to_sections == exec_ops.to_sections)
119         {
120           current_target.to_sections = NULL;
121           current_target.to_sections_end = NULL;
122         }
123
124       free (exec_ops.to_sections);
125       exec_ops.to_sections = NULL;
126       exec_ops.to_sections_end = NULL;
127     }
128
129   /* If we are quitting, we don't want to call breakpoint_re_set which may
130      output messages which would just be confusing in this context.  */
131   if (!quitting && need_symtab_cleanup)
132     clear_symtab_users ();
133 }
134
135 /*  Process the first arg in ARGS as the new exec file.
136
137     Note that we have to explicitly ignore additional args, since we can
138     be called from file_command(), which also calls symbol_file_command()
139     which can take multiple args. */
140
141 void
142 exec_file_command (filename, from_tty)
143      char *filename;
144      int from_tty;
145 {
146   target_preopen (from_tty);
147
148   /* Remove any previous exec file.  */
149   unpush_target (&exec_ops);
150
151   /* Now open and digest the file the user requested, if any. */
152
153   if (filename)
154     {
155       char *scratch_pathname;
156       int scratch_chan;
157       
158       filename = tilde_expand (filename);
159       make_cleanup (free, filename);
160       
161       scratch_chan = openp (getenv ("PATH"), 1, filename,
162                             write_files? O_RDWR: O_RDONLY, 0,
163                             &scratch_pathname);
164       if (scratch_chan < 0)
165         perror_with_name (filename);
166
167       exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
168       if (!exec_bfd)
169         error ("Could not open `%s' as an executable file: %s",
170                scratch_pathname, bfd_errmsg(bfd_get_error ()));
171
172       /* make sure we have an object file */
173
174       if (!bfd_check_format (exec_bfd, bfd_object))
175         error ("\"%s\": not in executable format: %s.", scratch_pathname,
176                bfd_errmsg (bfd_get_error ()));
177
178       /* setup initial vmap */
179
180       map_vmap (exec_bfd, 0);
181       if (!vmap)
182         error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
183                bfd_errmsg(bfd_get_error ()));
184
185       if (build_section_table (exec_bfd, &exec_ops.to_sections,
186                                &exec_ops.to_sections_end))
187         error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
188                bfd_errmsg (bfd_get_error ()));
189
190       /* make sure core, if present, matches */
191       validate_files ();
192
193       push_target(&exec_ops);
194
195       /* Tell display code (if any) about the changed file name. */
196       if (exec_file_display_hook)
197         (*exec_file_display_hook) (filename);
198     } 
199   else
200     {
201       exec_close (0);           /* just in case */
202       if (from_tty)
203         printf_unfiltered ("No exec file now.\n");
204     }
205 }
206
207 /* Set both the exec file and the symbol file, in one command.  What a
208    novelty.  Why did GDB go through four major releases before this
209    command was added?  */
210
211 static void
212 file_command (arg, from_tty)
213      char *arg;
214      int from_tty;
215 {
216   /* FIXME, if we lose on reading the symbol file, we should revert
217      the exec file, but that's rough.  */
218   exec_file_command (arg, from_tty);
219   symbol_file_command (arg, from_tty);
220 }
221
222 /* Locate all mappable sections of a BFD file. 
223    table_pp_char is a char * to get it through bfd_map_over_sections;
224    we cast it back to its proper type.  */
225
226 static void
227 add_to_section_table (abfd, asect, table_pp_char)
228      bfd *abfd;
229      sec_ptr asect;
230      char *table_pp_char;
231 {
232   struct section_table **table_pp = (struct section_table **)table_pp_char;
233   flagword aflag;
234
235   aflag = bfd_get_section_flags (abfd, asect);
236   /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
237   if (!(aflag & SEC_LOAD))
238     return;
239   if (0 == bfd_section_size (abfd, asect))
240     return;
241   (*table_pp)->bfd = abfd;
242   (*table_pp)->the_bfd_section = asect;
243   (*table_pp)->addr = bfd_section_vma (abfd, asect);
244   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
245   (*table_pp)++;
246 }
247
248 int
249 build_section_table (some_bfd, start, end)
250      bfd *some_bfd;
251      struct section_table **start, **end;
252 {
253   unsigned count;
254
255   count = bfd_count_sections (some_bfd);
256   if (count == 0)
257     fatal ("aborting"); /* return 1? */
258   if (*start)
259     free (*start);
260   *start = (struct section_table *) xmalloc (count * sizeof (**start));
261   *end = *start;
262   bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
263   if (*end > *start + count)
264     fatal ("aborting");
265   /* We could realloc the table, but it probably loses for most files.  */
266   return 0;
267 }
268 \f
269 static void
270 bfdsec_to_vmap(bf, sect, arg3) 
271      bfd *bf;
272      sec_ptr sect;
273      PTR arg3;
274 {
275   struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
276   register struct vmap *vp;
277   vp = vmap_bfd->pvmap;
278
279   if ((bfd_get_section_flags (bf, sect) & SEC_LOAD) == 0)
280     return;
281
282   if (STREQ(bfd_section_name (bf, sect), ".text"))
283     {
284       vp->tstart = 0;
285       vp->tend = vp->tstart + bfd_section_size (bf, sect);
286
287       /* When it comes to this adjustment value, in contrast to our previous
288          belief shared objects should behave the same as the main load segment.
289          This is the offset from the beginning of text section to the first
290          real instruction. */
291
292       vp->tadj = sect->filepos - bfd_section_vma (bf, sect);
293     }
294   else if (STREQ(bfd_section_name (bf, sect), ".data"))
295     {
296       vp->dstart = 0;
297       vp->dend   = vp->dstart + bfd_section_size (bf, sect);
298     }
299   else if (STREQ(bfd_section_name(bf, sect), ".bss"))   /* FIXMEmgo */
300     printf_unfiltered ("bss section in exec! Don't know what the heck to do!\n");
301 }
302
303 /* Make a vmap for the BFD "bf", which might be a member of the archive
304    BFD "arch".  Return the new vmap.  */
305
306 struct vmap *
307 map_vmap (bf, arch)
308      bfd *bf;
309      bfd *arch;
310 {
311   struct vmap_and_bfd vmap_bfd;
312   struct vmap *vp, **vpp;
313
314   vp = (PTR) xmalloc (sizeof (*vp));
315   memset (vp, '\0', sizeof (*vp));
316   vp->nxt = 0;
317   vp->bfd = bf;
318   vp->name = bfd_get_filename (arch ? arch : bf);
319   vp->member = arch ? bfd_get_filename (bf) : "";
320   
321   vmap_bfd.pbfd = arch;
322   vmap_bfd.pvmap = vp;
323   bfd_map_over_sections (bf, bfdsec_to_vmap, &vmap_bfd);
324
325   /* find the end of the list, and append. */
326   for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
327   ;
328   *vpp = vp;
329
330   return vp;
331 }
332
333 /* Read or write the exec file.
334
335    Args are address within exec file, address within gdb address-space,
336    length, and a flag indicating whether to read or write.
337
338    Result is a length:
339
340         0:    We cannot handle this address and length.
341         > 0:  We have handled N bytes starting at this address.
342               (If N == length, we did it all.)  We might be able
343               to handle more bytes beyond this length, but no
344               promises.
345         < 0:  We cannot handle this address, but if somebody
346               else handles (-N) bytes, we can start from there.
347
348     The same routine is used to handle both core and exec files;
349     we just tail-call it with more arguments to select between them.  */
350
351 int
352 xfer_memory (memaddr, myaddr, len, write, target)
353      CORE_ADDR memaddr;
354      char *myaddr;
355      int len;
356      int write;
357      struct target_ops *target;
358 {
359   boolean res;
360   struct section_table *p;
361   CORE_ADDR nextsectaddr, memend;
362   boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
363
364   if (len <= 0)
365     fatal ("aborting");
366
367   memend = memaddr + len;
368   xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
369   nextsectaddr = memend;
370
371   for (p = target->to_sections; p < target->to_sections_end; p++)
372     {
373       if (p->addr <= memaddr)
374         if (p->endaddr >= memend)
375           {
376             /* Entire transfer is within this section.  */
377             res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
378             return (res != 0) ? len : 0;
379           }
380         else if (p->endaddr <= memaddr)
381           {
382             /* This section ends before the transfer starts.  */
383             continue;
384           }
385         else 
386           {
387             /* This section overlaps the transfer.  Just do half.  */
388             len = p->endaddr - memaddr;
389             res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
390             return (res != 0) ? len : 0;
391           }
392       else if (p->addr < nextsectaddr)
393         nextsectaddr = p->addr;
394     }
395
396   if (nextsectaddr >= memend)
397     return 0;                           /* We can't help */
398   else
399     return - (nextsectaddr - memaddr);  /* Next boundary where we can help */
400 }
401
402 void
403 print_section_info (t, abfd)
404   struct target_ops *t;
405   bfd *abfd;
406 {
407   struct section_table *p;
408
409   /* FIXME-32x64: Need a version of print_address_numeric with field width.  */
410   printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
411   wrap_here ("        ");
412   printf_filtered ("file type %s.\n", bfd_get_target(abfd));
413
414   for (p = t->to_sections; p < t->to_sections_end; p++) {
415     printf_filtered ("\t%s",
416                      local_hex_string_custom ((unsigned long) p->addr, "08l"));
417     printf_filtered (" - %s",
418                      local_hex_string_custom ((unsigned long) p->endaddr, "08l"));
419     if (info_verbose)
420       printf_filtered (" @ %s",
421                        local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l"));
422     printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
423     if (p->bfd != abfd) {
424       printf_filtered (" in %s", bfd_get_filename (p->bfd));
425     }
426     printf_filtered ("\n");
427   }
428 }
429
430
431 static void
432 exec_files_info (t)
433      struct target_ops *t;
434 {
435   register struct vmap *vp = vmap;
436
437   print_section_info (t, exec_bfd);
438
439   if (!vp)
440     return;
441
442   printf_unfiltered ("\tMapping info for file `%s'.\n", vp->name);
443
444   printf_unfiltered ("\t  %8.8s   %8.8s   %8.8s   %8.8s %8.8s %s\n",
445     "tstart", "tend", "dstart", "dend", "section", "file(member)");
446
447   for (; vp; vp = vp->nxt)
448     printf_unfiltered ("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
449                        vp->tstart,
450                        vp->tend,
451                        vp->dstart,
452                        vp->dend,
453                        vp->name,
454                        *vp->member ? "(" : "",
455                        vp->member,
456                        *vp->member ? ")" : "");
457 }
458
459 #ifdef DAMON
460 /*  Damon's implementation of set_section_command! It is based on the sex member
461   (which is a section pointer from vmap) of vmap.
462   We will not have multiple vmap entries (one for each section), rather transmit
463   text and data base offsets and fix them at the same time. Elimination of sex
464   entry in vmap make this function obsolute, use the one from exec.c. 
465   Need further testing!!        FIXMEmgo.  */
466
467 static void
468 set_section_command(args, from_tty)
469 char *args; 
470 {
471         register struct vmap *vp = vmap;
472         char *secname;
473         unsigned seclen;
474         unsigned long secaddr;
475         char secprint[100];
476         long offset;
477
478         if (args == 0)
479                 error("Must specify section name and its virtual address");
480
481         /* Parse out section name */
482         for (secname = args; !isspace(*args); args++)
483                 ;
484         seclen = args - secname;
485
486         /* Parse out new virtual address */
487         secaddr = parse_and_eval_address(args);
488
489         for (vp = vmap; vp; vp = vp->nxt) {
490                 if (!strncmp(secname
491                              , bfd_section_name(vp->bfd, vp->sex), seclen)
492                     && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
493                         offset = secaddr - vp->tstart;
494                         vp->tstart += offset;
495                         vp->tend   += offset;
496                         exec_files_info();
497                         return;
498                 }
499         } 
500
501         if (seclen >= sizeof(secprint))
502                 seclen = sizeof(secprint) - 1;
503         strncpy(secprint, secname, seclen);
504         secprint[seclen] = '\0';
505         error("Section %s not found", secprint);
506 }
507 #else
508 static void
509 set_section_command (args, from_tty)
510      char *args;
511      int from_tty;
512 {
513   struct section_table *p;
514   char *secname;
515   unsigned seclen;
516   unsigned long secaddr;
517   char secprint[100];
518   long offset;
519
520   if (args == 0)
521     error ("Must specify section name and its virtual address");
522
523   /* Parse out section name */
524   for (secname = args; !isspace(*args); args++) ;
525   seclen = args - secname;
526
527   /* Parse out new virtual address */
528   secaddr = parse_and_eval_address (args);
529
530   for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
531     if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
532         && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0')
533       {
534         offset = secaddr - p->addr;
535         p->addr += offset;
536         p->endaddr += offset;
537         if (from_tty)
538           exec_files_info (&exec_ops);
539         return;
540       }
541
542   if (seclen >= sizeof (secprint))
543     seclen = sizeof (secprint) - 1;
544   strncpy (secprint, secname, seclen);
545   secprint[seclen] = '\0';
546   error ("Section %s not found", secprint);
547 }
548
549 #endif /* !DAMON */
550
551 struct target_ops exec_ops = {
552         "exec", "Local exec file",
553         "Use an executable file as a target.\n\
554 Specify the filename of the executable file.",
555         exec_file_command, exec_close, /* open, close */
556         find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
557         0, 0, /* fetch_registers, store_registers, */
558         0, /* prepare_to_store */
559         xfer_memory, exec_files_info,
560         0, 0, /* insert_breakpoint, remove_breakpoint, */
561         0, 0, 0, 0, 0, /* terminal stuff */
562         0, 0, /* kill, load */
563         0, /* lookup sym */
564         find_default_create_inferior,
565         0, /* mourn_inferior */
566         0, /* can_run */
567         0, /* notice_signals */
568         file_stratum, 0, /* next */
569         0, 1, 0, 0, 0,  /* all mem, mem, stack, regs, exec */
570         0, 0,                   /* section pointers */
571         OPS_MAGIC,              /* Always the last thing */
572 };
573
574 void
575 _initialize_exec()
576 {
577
578   add_com("file", class_files, file_command,
579            "Use FILE as program to be debugged.\n\
580 It is read for its symbols, for getting the contents of pure memory,\n\
581 and it is the program executed when you use the `run' command.\n\
582 If FILE cannot be found as specified, your execution directory path\n\
583 ($PATH) is searched for a command of that name.\n\
584 No arg means to have no executable file and no symbols.");
585
586   add_com("exec-file", class_files, exec_file_command,
587            "Use FILE as program for getting contents of pure memory.\n\
588 If FILE cannot be found as specified, your execution directory path\n\
589 is searched for a command of that name.\n\
590 No arg means have no executable file.");
591
592   add_com("section", class_files, set_section_command,
593    "Change the base address of section SECTION of the exec file to ADDR.\n\
594 This can be used if the exec file does not contain section addresses,\n\
595 (such as in the a.out format), or when the addresses specified in the\n\
596 file itself are wrong.  Each section must be changed separately.  The\n\
597 ``info files'' command lists all the sections and their addresses.");
598
599   add_target(&exec_ops);
600 }