doc/gdbinv-s.m4.in: remove text on special procedures to continue after
[deliverable/binutils-gdb.git] / gdb / xcoffexec.c
index 7259b2199e2398950f5effb0b8311e23326dcfdb..d4586ee8288cb328b39ddc691c17cb219cf124d7 100644 (file)
@@ -21,7 +21,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /* xcoff-exec -        deal with executing XCOFF files.  */
   
-#include <stdio.h>
+#include "defs.h"
+
 #include <sys/types.h>
 #include <sys/param.h>
 #include <fcntl.h>
@@ -30,15 +31,16 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <sys/stat.h>
 #include <sys/ldr.h>
 
-#include "defs.h"
 #include "frame.h"
 #include "inferior.h"
 #include "target.h"
 #include "gdbcmd.h"
 #include "gdbcore.h"
 #include "symfile.h"
+#include "objfiles.h"
 
 #include "libbfd.h"            /* BFD internals (sigh!)  FIXME */
+#include "xcoffsolib.h"
 
 /* Prototypes for local functions */
 
@@ -67,6 +69,7 @@ extern void symbol_file_command ();
 static void exec_files_info();
 extern struct objfile *lookup_objfile_bfd ();
 
+#if 0
 /*
  * the vmap struct is used to describe the virtual address space of
  * the target we are manipulating.  The first entry is always the "exec"
@@ -95,6 +98,9 @@ struct vmap_and_bfd {
 };
 
 static struct vmap *vmap;      /* current vmap                         */
+#endif /* 0 */
+
+struct vmap *vmap;             /* current vmap */
 
 extern struct target_ops exec_ops;
 
@@ -124,7 +130,16 @@ exec_close(quitting)
     }
   
   vmap = 0;
-  exec_bfd = 0;
+
+  if (exec_bfd) {
+    bfd_close (exec_bfd);
+    exec_bfd = NULL;
+  }
+  if (exec_ops.to_sections) {
+    free (exec_ops.to_sections);
+    exec_ops.to_sections = NULL;
+    exec_ops.to_sections_end = NULL;
+  }
 }
 
 /*
@@ -134,9 +149,9 @@ void
 exec_file_command(filename, from_tty)
 char *filename;
 {
-  bfd *bfd;
-
   target_preopen(from_tty);
+
+  /* Remove any previous exec file.  */
   unpush_target(&exec_ops);
 
   /* Now open and digest the file the user requested, if any. */
@@ -148,33 +163,33 @@ char *filename;
        filename = tilde_expand(filename);
        make_cleanup (free, filename);
       
-       scratch_chan = openp(getenv("PATH"), 1, filename, O_RDONLY, 0
-                            , &scratch_pathname);
+       scratch_chan = openp(getenv("PATH"), 1, filename,
+                            write_files? O_RDWR: O_RDONLY, 0,
+                            &scratch_pathname);
        if (scratch_chan < 0)
          perror_with_name(filename);
 
-       bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan);
-       if (!bfd)
+       exec_bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan);
+       if (!exec_bfd)
          error("Could not open `%s' as an executable file: %s"
                      , scratch_pathname, bfd_errmsg(bfd_error));
 
        /* make sure we have an object file */
 
-       if (!bfd_check_format(bfd, bfd_object))
-               error("\"%s\": not in executable format: %s."
-                     scratch_pathname, bfd_errmsg(bfd_error));
+       if (!bfd_check_format(exec_bfd, bfd_object))
+               error("\"%s\": not in executable format: %s.",
+                     scratch_pathname, bfd_errmsg(bfd_error));
 
 
        /* setup initial vmap */
 
-       map_vmap (bfd, 0);
+       map_vmap (exec_bfd, 0);
        if (!vmap)
-               error("Can't find the file sections in `%s': %s"
-                     , bfd->filename, bfd_errmsg(bfd_error));
-
-       exec_bfd = bfd;
+               error("Can't find the file sections in `%s': %s",
+                     exec_bfd->filename, bfd_errmsg(bfd_error));
 
-       if (build_section_table (exec_bfd, &exec_sections, &exec_sections_end))
+       if (build_section_table (exec_bfd, &exec_ops.to_sections,
+                               &exec_ops.to_sections_end))
          error ("Can't find the file sections in `%s': %s", 
                exec_bfd->filename, bfd_errmsg (bfd_error));
 
@@ -224,6 +239,9 @@ add_to_section_table (abfd, asect, table_pp_char)
   /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
   if (!(aflag & SEC_LOAD))
     return;
+  if (0 == bfd_section_size (abfd, asect))
+    return;
+  (*table_pp)->bfd = abfd;
   (*table_pp)->sec_ptr = asect;
   (*table_pp)->addr = bfd_section_vma (abfd, asect);
   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
@@ -280,12 +298,12 @@ sex_to_vmap(bfd *bf, sec_ptr sex, struct vmap_and_bfd *vmap_bfd)
     vp->tstart = 0;
     vp->tend   = vp->tstart + bfd_section_size(bf, sex);
 
-    /* This is quite a tacky way to recognize the `exec' load segment (rather
-       than shared libraries. You should use `arch' instead. FIXMEmgo */
-    if (!vmap)
-      vp->tadj = sex->filepos - bfd_section_vma(bf, sex);
-    else
-      vp->tadj = 0;
+    /* When it comes to this adjustment value, in contrast to our previous
+       belief shared objects should behave the same as the main load segment.
+       This is the offset from the beginning of text section to the first
+       real instruction. */
+
+    vp->tadj = sex->filepos - bfd_section_vma(bf, sex);
   }
 
   else if (!strcmp(bfd_section_name(bf, sex), ".data")) {
@@ -307,6 +325,7 @@ map_vmap (bfd *bf, bfd *arch)
   struct objfile *obj;
 
   vp = (void*) xmalloc (sizeof (*vp));
+  bzero (vp, sizeof (*vp));
   vp->nxt = 0;
   vp->bfd = bf;
   vp->name = bfd_get_filename(arch ? arch : bf);
@@ -318,8 +337,16 @@ map_vmap (bfd *bf, bfd *arch)
 
   obj = lookup_objfile_bfd (bf);
   if (exec_bfd && !obj) {
-    obj = allocate_objfile (bf, bfd_get_filename (bf), 0);
+    obj = allocate_objfile (bf, 0);
+
+#if 0
+    /* This is only needed if we want to load shared libraries no matter what.
+       Since we provide the choice of incremental loading of shared objects
+       now, we do not have to load them as default anymore. */
+    
     syms_from_objfile (obj, 0, 0, 0);
+    new_symfile_objfile (obj, 0, 0);
+#endif
   }
 
   /* find the end of the list, and append. */
@@ -328,19 +355,6 @@ map_vmap (bfd *bf, bfd *arch)
   *vpp = vp;
 }
 
-/* Called via iterate_over_msymbols to relocate minimal symbols */
-
-static void
-relocate_minimal_symbol (objfile, msymbol, arg1, arg2, arg3)
-     struct objfile *objfile;
-     struct minimal_symbol *msymbol;
-     PTR arg1;
-     PTR arg2;
-     PTR arg3;
-{
-  if (msymbol->address < TEXT_SEGMENT_BASE)
-    msymbol -> address += (int) arg1;
-}
 
 /* true, if symbol table and minimal symbol table are relocated. */
 
@@ -356,11 +370,12 @@ struct stat *vip;
 {
   register struct symtab *s;
   register struct objfile *objfile;
+  register struct minimal_symbol *msymbol;
   
   /*
    * for each symbol table generated from the vp->bfd
    */
-  for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
+  ALL_OBJFILES (objfile)
     {
       for (s = objfile -> symtabs; s != NULL; s = s -> next) {
        
@@ -394,20 +409,42 @@ struct stat *vip;
          if (fstat(fileno(io), &si) < 0)
            fatal("cannot fstat BFD for sym");
          
-         if (si.st_dev != vip->st_dev
-             || si.st_ino != vip->st_ino)
+         if (vip && (si.st_dev != vip->st_dev
+             || si.st_ino != vip->st_ino))
            continue;
        }
        
-       if (vp->tstart != old_start)
-         vmap_symtab_1(s, vp, old_start);
+       if (vp->tstart != old_start) {
+
+         /* Once we find a relocation base address for one of the symtabs
+            in this objfile, it will be the same for all symtabs in this
+            objfile. Clean this algorithm. FIXME. */
+
+         for (; s; s = s->next)
+           if (!s->nonreloc || LINETABLE(s))
+               vmap_symtab_1(s, vp, old_start);
+
+#if 0 
+         Himm.., recently we nullified trampoline entry names in order not
+         to confuse them with real symbols.  Appearently this turned into a
+         problem, and msymbol vector did not get relocated properly.  If
+         msymbols have to have non-null names, then we should name
+         trampoline entries with empty strings. 
+
+         ALL_MSYMBOLS (objfile, msymbol)
+#else
+         for (msymbol = objfile->msymbols;
+               msymbol->name || msymbol->address; (msymbol)++)
+#endif
+             if (msymbol->address < TEXT_SEGMENT_BASE)
+               msymbol -> address += vp->tstart - old_start;
+
+          break;
+       }
       }
     }
-  if (vp->tstart != old_start) {
-    iterate_over_msymbols (relocate_minimal_symbol,
-                          (PTR) (vp->tstart - old_start),
-                          (PTR) NULL, (PTR) NULL);
 
+  if (vp->tstart != old_start) {
     /* breakpoints need to be relocated as well. */
     fixup_breakpoints (0, TEXT_SEGMENT_BASE, vp->tstart - old_start);
   }
@@ -415,6 +452,7 @@ struct stat *vip;
   symtab_relocated = 1;
 }
 
+
 vmap_symtab_1(s, vp, old_start)
 register struct symtab *s;
 register struct vmap *vp;
@@ -514,19 +552,26 @@ CORE_ADDR old_start;
 add_vmap(ldi)
 register struct ld_info *ldi; {
        bfd *bfd, *last;
-       register char *mem;
+       register char *mem, *objname;
+
+       /* This ldi structure was allocated using alloca() in 
+          xcoff_relocate_symtab(). Now we need to have persistent object 
+          and member names, so we should save them. */
 
        mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1;
-       bfd = bfd_fdopenr(ldi->ldinfo_filename, NULL, ldi->ldinfo_fd);
+       mem = savestring (mem, strlen (mem));
+       objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
+
+       bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd);
        if (!bfd)
-               error("Could not open `%s' as an executable file: %s"
-                     , ldi->ldinfo_filename, bfd_errmsg(bfd_error));
+         error("Could not open `%s' as an executable file: %s",
+                                       objname, bfd_errmsg(bfd_error));
 
 
        /* make sure we have an object file */
 
        if (bfd_check_format(bfd, bfd_object))
-               map_vmap (bfd, 0);
+         map_vmap (bfd, 0);
 
        else if (bfd_check_format(bfd, bfd_archive)) {
                last = 0;
@@ -538,11 +583,10 @@ register struct ld_info *ldi; {
                                break;
 
                if (!last) {
-                       bfd_close(bfd);
-/* FIXME -- should be error */
-                       warning("\"%s\": member \"%s\" missing.",
-                             bfd->filename, mem);
-                       return;
+                 bfd_close(bfd);
+                 /* FIXME -- should be error */
+                 warning("\"%s\": member \"%s\" missing.", bfd->filename, mem);
+                 return;
                }
 
                if (!bfd_check_format(last, bfd_object)) {
@@ -557,38 +601,47 @@ register struct ld_info *ldi; {
                bfd_close(bfd);
 /* FIXME -- should be error */
                warning("\"%s\": not in executable format: %s."
-                     , ldi->ldinfo_filename, bfd_errmsg(bfd_error));
+                     , objname, bfd_errmsg(bfd_error));
                return;
        }
 }
 
 
-/* As well as symbol tables, exec_sections need relocation. Otherwise after
-   the inferior process terminates, symbol table is relocated but there is
-   no inferior process. Thus, we have to use `exec' bfd, rather than the inferior
-   process's memory space, when lookipng at symbols.
-   `exec_sections' need to be relocated only once though, as long as the exec
-   file was not changed.
+/* As well as symbol tables, exec_sections need relocation. After
+   the inferior process' termination, there will be a relocated symbol
+   table exist with no corresponding inferior process. At that time, we
+   need to use `exec' bfd, rather than the inferior process's memory space
+   to look up symbols.
+
+   `exec_sections' need to be relocated only once, as long as the exec
+   file remains unchanged.
 */
 vmap_exec ()
 {
   static bfd *execbfd;
+  int i;
+
   if (execbfd == exec_bfd)
     return;
 
   execbfd = exec_bfd;
 
-  if (!vmap || !exec_sections) {
-    printf ("WARNING: vmap not found in vmap_exec()!\n");
-    return;
-  }
-  /* First exec section is `.text', second is `.data'. If this is changed,
-     then this routine will choke. Better you should check section names,
-     FIXMEmgo. */
-  exec_sections [0].addr += vmap->tstart;
-  exec_sections [0].endaddr += vmap->tstart;
-  exec_sections [1].addr += vmap->dstart;
-  exec_sections [1].endaddr += vmap->dstart;
+  if (!vmap || !exec_ops.to_sections)
+    error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
+
+  for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
+    {
+      if (strcmp(".text", exec_ops.to_sections[i].sec_ptr->name) == 0)
+       {
+         exec_ops.to_sections[i].addr += vmap->tstart;
+         exec_ops.to_sections[i].endaddr += vmap->tstart;
+       }
+      else if (strcmp(".data", exec_ops.to_sections[i].sec_ptr->name) == 0)
+       {
+         exec_ops.to_sections[i].addr += vmap->dstart;
+         exec_ops.to_sections[i].endaddr += vmap->dstart;
+       }
+    }
 }
 
 
@@ -644,59 +697,61 @@ register struct ld_info *ldi;
                      , name);
 retry:
        for (got_one = 0, vp = vmap; vp; vp = vp->nxt) {
-               FILE *io;
+         FILE *io;
 
-               /* The filenames are not always sufficient to match on. */
-               if ((name[0] == "/"
-                   && !eq(name, vp->name))
-                   || (memb[0] && !eq(memb, vp->member)))
-                       continue;
+         /* First try to find a `vp', which is the same as in ldinfo.
+            If not the same, just continue and grep the next `vp'. If same,
+            relocate its tstart, tend, dstart, dend values. If no such `vp'
+            found, get out of this for loop, add this ldi entry as a new vmap
+            (add_vmap) and come back, fins its `vp' and so on... */
 
-               /* totally opaque! */
-               io = bfd_cache_lookup(vp->bfd);
-               if (!io)
-                       fatal("cannot find BFD's iostream for %s"
-                             , vp->name);
+         /* The filenames are not always sufficient to match on. */
 
-               /* see if we are referring to the same file */
-               if (fstat(fileno(io), &vi) < 0)
-                       fatal("cannot fstat BFD for %s", vp->name);
+         if ((name[0] == "/" && !eq(name, vp->name))
+               || (memb[0] && !eq(memb, vp->member)))
+           continue;
 
-               if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
-                       continue;
+         io = bfd_cache_lookup(vp->bfd);               /* totally opaque! */
+         if (!io)
+           fatal("cannot find BFD's iostream for %s", vp->name);
 
-               if (!retried)
-                   close(ldi->ldinfo_fd);
+         /* see if we are referring to the same file */
 
-               ++got_one;
+         if (fstat(fileno(io), &vi) < 0)
+           fatal("cannot fstat BFD for %s", vp->name);
 
-               /* found a corresponding VMAP. remap! */
-               ostart = vp->tstart;
+         if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
+           continue;
 
-               vp->tstart = ldi->ldinfo_textorg;
-               vp->tend   = vp->tstart + ldi->ldinfo_textsize;
-               vp->dstart = ldi->ldinfo_dataorg;
-               vp->dend   = vp->dstart + ldi->ldinfo_datasize;
+         if (!retried)
+           close(ldi->ldinfo_fd);
 
-               if (vp->tadj) {
-                 vp->tstart += vp->tadj;
-                 vp->tend   += vp->tadj;
-               }
+         ++got_one;
 
-               /* relocate symbol table(s). */
-               vmap_symtab(vp, ostart, &vi);
+         /* found a corresponding VMAP. remap! */
+         ostart = vp->tstart;
 
-               /* there may be more, so we don't break out of the loop. */
+         vp->tstart = ldi->ldinfo_textorg;
+         vp->tend   = vp->tstart + ldi->ldinfo_textsize;
+         vp->dstart = ldi->ldinfo_dataorg;
+         vp->dend   = vp->dstart + ldi->ldinfo_datasize;
+
+         if (vp->tadj) {
+           vp->tstart += vp->tadj;
+           vp->tend   += vp->tadj;
+         }
+
+         /* relocate symbol table(s). */
+         vmap_symtab(vp, ostart, &vi);
+
+         /* there may be more, so we don't break out of the loop. */
        }
 
-       /*
-        * if there was no matching *vp, we must perforce create
-        * the sucker(s)
-        */
-       if (!got_one && !retried) {
-               add_vmap(ldi);
-               ++retried;
-               goto retry;
+       /* if there was no matching *vp, we must perforce create the sucker(s) */
+       if (!got_one && !retried) {
+         add_vmap(ldi);
+         ++retried;
+         goto retry;
        }
   } while (ldi->ldinfo_next
         && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
@@ -709,10 +764,9 @@ retry:
 vmap_inferior() {
 
        if (inferior_pid == 0)
-               return 0;               /* normal processing    */
+         return 0;                             /* normal processing    */
 
        exec_files_info();
-
        return 1;
 }
 
@@ -790,7 +844,6 @@ print_section_info (t, abfd)
   struct target_ops *t;
   bfd *abfd;
 {
-#if 1
   struct section_table *p;
 
   printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
@@ -809,32 +862,35 @@ print_section_info (t, abfd)
     }
     printf_filtered ("\n");
   }
-#else
-       register struct vmap *vp = vmap;
-
-       if (!vp)
-               return;
-
-       printf("\tMapping info for file `%s'.\n", vp->name);
-       printf("\t  %8.8s   %8.8s %8.8s %s\n"
-              , "start", "end", "section", "file(member)");
-
-       for (; vp; vp = vp->nxt)
-               printf("\t0x%8.8x 0x%8.8x %s%s%s%s\n"
-                      , vp->tstart
-                      , vp->tend
-                      , vp->name
-                      , *vp->member ? "(" : ""
-                      ,  vp->member
-                      , *vp->member ? ")" : "");
-#endif
 }
 
+
 static void
 exec_files_info (t)
   struct target_ops *t;
 {
+  register struct vmap *vp = vmap;
+
   print_section_info (t, exec_bfd);
+
+  if (!vp)
+    return;
+
+  printf("\tMapping info for file `%s'.\n", vp->name);
+
+  printf("\t  %8.8s   %8.8s   %8.8s   %8.8s %8.8s %s\n",
+    "tstart", "tend", "dstart", "dend", "section", "file(member)");
+
+  for (; vp; vp = vp->nxt)
+     printf("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
+       vp->tstart,
+       vp->tend,
+       vp->dstart,
+       vp->dend,
+       vp->name,
+       *vp->member ? "(" : "",
+       vp->member,
+       *vp->member ? ")" : "");
 }
 
 #ifdef DAMON
@@ -908,13 +964,14 @@ set_section_command (args, from_tty)
   /* Parse out new virtual address */
   secaddr = parse_and_eval_address (args);
 
-  for (p = exec_sections; p < exec_sections_end; p++) {
+  for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
     if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
        && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
       offset = secaddr - p->addr;
       p->addr += offset;
       p->endaddr += offset;
-      exec_files_info();
+      if (from_tty)
+        exec_files_info(&exec_ops);
       return;
     }
   } 
This page took 0.029676 seconds and 4 git commands to generate.