GDB (API)
/home/stan/gdb/src/gdb/solib-osf.c
Go to the documentation of this file.
00001 /* Handle OSF/1, Digital UNIX, and Tru64 shared libraries
00002    for GDB, the GNU Debugger.
00003    Copyright (C) 1993-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 /* When handling shared libraries, GDB has to find out the pathnames
00021    of all shared libraries that are currently loaded (to read in their
00022    symbols) and where the shared libraries are loaded in memory
00023    (to relocate them properly from their prelinked addresses to the
00024    current load address).
00025 
00026    Under OSF/1 there are two possibilities to get at this information:
00027 
00028    1) Peek around in the runtime loader structures.
00029    These are not documented, and they are not defined in the system
00030    header files.  The definitions below were obtained by experimentation,
00031    but they seem stable enough.
00032 
00033    2) Use the libxproc.a library, which contains the equivalent ldr_*
00034    routines.  The library is documented in Tru64 5.x, but as of 5.1, it
00035    only allows a process to examine itself.  On earlier versions, it
00036    may require that the GDB executable be dynamically linked and that
00037    NAT_CLIBS include -lxproc -Wl,-expect_unresolved,ldr_process_context
00038    for GDB and all applications that are using libgdb.
00039 
00040    We will use the peeking approach until libxproc.a works for other
00041    processes.  */
00042 
00043 #include "defs.h"
00044 
00045 #include <sys/types.h>
00046 #include <signal.h>
00047 #include "gdb_string.h"
00048 
00049 #include "bfd.h"
00050 #include "symtab.h"
00051 #include "symfile.h"
00052 #include "objfiles.h"
00053 #include "target.h"
00054 #include "inferior.h"
00055 #include "gdbthread.h"
00056 #include "solist.h"
00057 #include "solib.h"
00058 
00059 #ifdef USE_LDR_ROUTINES
00060 # include <loader.h>
00061 #endif
00062 
00063 #ifndef USE_LDR_ROUTINES
00064 /* Definition of runtime loader structures, found by experimentation.  */
00065 #define RLD_CONTEXT_ADDRESS     0x3ffc0000000
00066 
00067 /* Per-module information structure referenced by ldr_context_t.head.  */
00068 
00069 typedef struct
00070   {
00071     CORE_ADDR next;
00072     CORE_ADDR previous;
00073     CORE_ADDR unknown1;
00074     CORE_ADDR module_name;
00075     CORE_ADDR modinfo_addr;     /* Used by next_link_map_member() to detect
00076                                    the end of the shared module list.  */
00077     long module_id;
00078     CORE_ADDR unknown2;
00079     CORE_ADDR unknown3;
00080     long region_count;
00081     CORE_ADDR regioninfo_addr;
00082   }
00083 ldr_module_info_t;
00084 
00085 /* Per-region structure referenced by ldr_module_info_t.regioninfo_addr.  */
00086 
00087 typedef struct
00088   {
00089     long unknown1;
00090     CORE_ADDR regionname_addr;
00091     long protection;
00092     CORE_ADDR vaddr;
00093     CORE_ADDR mapaddr;
00094     long size;
00095     long unknown2[5];
00096   }
00097 ldr_region_info_t;
00098 
00099 /* Structure at RLD_CONTEXT_ADDRESS specifying the start and finish addresses
00100    of the shared module list.  */
00101 
00102 typedef struct
00103   {
00104     CORE_ADDR unknown1;
00105     CORE_ADDR unknown2;
00106     CORE_ADDR head;
00107     CORE_ADDR tail;
00108   }
00109 ldr_context_t;
00110 #endif   /* !USE_LDR_ROUTINES */
00111 
00112 /* Per-section information, stored in struct lm_info.secs.  */
00113 
00114 struct lm_sec
00115   {
00116     CORE_ADDR offset;           /* difference between default and actual
00117                                    virtual addresses of section .name */
00118     CORE_ADDR nameaddr;         /* address in inferior of section name */
00119     const char *name;           /* name of section, null if not fetched */
00120   };
00121 
00122 /* Per-module information, stored in struct so_list.lm_info.  */
00123 
00124 struct lm_info
00125   {
00126     int isloader;               /* whether the module is /sbin/loader */
00127     int nsecs;                  /* length of .secs */
00128     struct lm_sec secs[1];      /* variable-length array of sections, sorted
00129                                    by name */
00130   };
00131 
00132 /* Context for iterating through the inferior's shared module list.  */
00133 
00134 struct read_map_ctxt
00135   {
00136 #ifdef USE_LDR_ROUTINES
00137     ldr_process_t proc;
00138     ldr_module_t next;
00139 #else
00140     CORE_ADDR next;             /* next element in module list */
00141     CORE_ADDR tail;             /* last element in module list */
00142 #endif
00143   };
00144 
00145 /* Forward declaration for this module's autoinit function.  */
00146 
00147 extern void _initialize_osf_solib (void);
00148 
00149 #ifdef USE_LDR_ROUTINES
00150 # if 0
00151 /* This routine is intended to be called by ldr_* routines to read memory from
00152    the current target.  Usage:
00153 
00154      ldr_process = ldr_core_process ();
00155      ldr_set_core_reader (ldr_read_memory);
00156      ldr_xdetach (ldr_process);
00157      ldr_xattach (ldr_process);
00158 
00159    ldr_core_process() and ldr_read_memory() are neither documented nor
00160    declared in system header files.  They work with OSF/1 2.x, and they might
00161    work with later versions as well.  */
00162 
00163 static int
00164 ldr_read_memory (CORE_ADDR memaddr, char *myaddr, int len, int readstring)
00165 {
00166   int result;
00167   char *buffer;
00168 
00169   if (readstring)
00170     {
00171       target_read_string (memaddr, &buffer, len, &result);
00172       if (result == 0)
00173         strcpy (myaddr, buffer);
00174       xfree (buffer);
00175     }
00176   else
00177     result = target_read_memory (memaddr, myaddr, len);
00178 
00179   if (result != 0)
00180     result = -result;
00181   return result;
00182 }
00183 # endif   /* 0 */
00184 #endif   /* USE_LDR_ROUTINES */
00185 
00186 /* Comparison for qsort() and bsearch(): return -1, 0, or 1 according to
00187    whether lm_sec *P1's name is lexically less than, equal to, or greater
00188    than that of *P2.  */
00189 
00190 static int
00191 lm_sec_cmp (const void *p1, const void *p2)
00192 {
00193   const struct lm_sec *lms1 = p1, *lms2 = p2;
00194 
00195   return strcmp (lms1->name, lms2->name);
00196 }
00197 
00198 /* Sort LMI->secs so that osf_relocate_section_addresses() can binary-search
00199    it.  */
00200 
00201 static void
00202 lm_secs_sort (struct lm_info *lmi)
00203 {
00204   qsort (lmi->secs, lmi->nsecs, sizeof *lmi->secs, lm_sec_cmp);
00205 }
00206 
00207 /* Populate name fields of LMI->secs.  */
00208 
00209 static void
00210 fetch_sec_names (struct lm_info *lmi)
00211 {
00212 #ifndef USE_LDR_ROUTINES
00213   int i, errcode;
00214   struct lm_sec *lms;
00215   char *name;
00216 
00217   for (i = 0; i < lmi->nsecs; i++)
00218     {
00219       lms = lmi->secs + i;
00220       target_read_string (lms->nameaddr, &name, PATH_MAX, &errcode);
00221       if (errcode != 0)
00222         {
00223           warning (_("unable to read shared sec name at 0x%lx"),
00224                    lms->nameaddr);
00225           name = xstrdup ("");
00226         }
00227       lms->name = name;
00228     }
00229   lm_secs_sort (lmi);
00230 #endif
00231 }
00232 
00233 /* target_so_ops callback.  Adjust SEC's addresses after it's been mapped into
00234    the process.  */
00235 
00236 static void
00237 osf_relocate_section_addresses (struct so_list *so,
00238                                 struct target_section *sec)
00239 {
00240   struct lm_info *lmi;
00241   struct lm_sec lms_key, *lms;
00242 
00243   /* Fetch SO's section names if we haven't done so already.  */
00244   lmi = so->lm_info;
00245   if (lmi->nsecs && !lmi->secs[0].name)
00246     fetch_sec_names (lmi);
00247 
00248   /* Binary-search for offset information corresponding to SEC.  */
00249   lms_key.name = sec->the_bfd_section->name;
00250   lms = bsearch (&lms_key, lmi->secs, lmi->nsecs, sizeof *lms, lm_sec_cmp);
00251   if (lms)
00252     {
00253       sec->addr += lms->offset;
00254       sec->endaddr += lms->offset;
00255     }
00256 }
00257 
00258 /* target_so_ops callback.  Free parts of SO allocated by this file.  */
00259 
00260 static void
00261 osf_free_so (struct so_list *so)
00262 {
00263   int i;
00264   const char *name;
00265 
00266   for (i = 0; i < so->lm_info->nsecs; i++)
00267     {
00268       name = so->lm_info->secs[i].name;
00269       if (name)
00270         xfree ((void *) name);
00271     }
00272   xfree (so->lm_info);
00273 }
00274 
00275 /* target_so_ops callback.  Discard information accumulated by this file and
00276    not freed by osf_free_so().  */
00277 
00278 static void
00279 osf_clear_solib (void)
00280 {
00281   return;
00282 }
00283 
00284 /* target_so_ops callback.  Prepare to handle shared libraries after the
00285    inferior process has been created but before it's executed any
00286    instructions.
00287 
00288    For a statically bound executable, the inferior's first instruction is the
00289    one at "_start", or a similar text label.  No further processing is needed
00290    in that case.
00291 
00292    For a dynamically bound executable, this first instruction is somewhere
00293    in the rld, and the actual user executable is not yet mapped in.
00294    We continue the inferior again, rld then maps in the actual user
00295    executable and any needed shared libraries and then sends
00296    itself a SIGTRAP.
00297 
00298    At that point we discover the names of all shared libraries and
00299    read their symbols in.
00300 
00301    FIXME
00302 
00303    This code does not properly handle hitting breakpoints which the
00304    user might have set in the rld itself.  Proper handling would have
00305    to check if the SIGTRAP happened due to a kill call.
00306 
00307    Also, what if child has exit()ed?  Must exit loop somehow.  */
00308 
00309 static void
00310 osf_solib_create_inferior_hook (int from_tty)
00311 {
00312   struct inferior *inf;
00313   struct thread_info *tp;
00314 
00315   inf = current_inferior ();
00316 
00317   /* If we are attaching to the inferior, the shared libraries
00318      have already been mapped, so nothing more to do.  */
00319   if (inf->attach_flag)
00320     return;
00321 
00322   /* Nothing to do for statically bound executables.  */
00323 
00324   if (symfile_objfile == NULL
00325       || symfile_objfile->obfd == NULL
00326       || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
00327     return;
00328 
00329   /* Now run the target.  It will eventually get a SIGTRAP, at
00330      which point all of the libraries will have been mapped in and we
00331      can go groveling around in the rld structures to find
00332      out what we need to know about them.
00333      
00334      If debugging from a core file, we cannot resume the execution
00335      of the inferior.  But this is actually not an issue, because
00336      shared libraries have already been mapped anyways, which means
00337      we have nothing more to do.  */
00338   if (!target_can_run (&current_target))
00339     return;
00340 
00341   tp = inferior_thread ();
00342   clear_proceed_status ();
00343   inf->control.stop_soon = STOP_QUIETLY;
00344   tp->suspend.stop_signal = GDB_SIGNAL_0;
00345   do
00346     {
00347       target_resume (minus_one_ptid, 0, tp->suspend.stop_signal);
00348       wait_for_inferior ();
00349     }
00350   while (tp->suspend.stop_signal != GDB_SIGNAL_TRAP);
00351 
00352   /*  solib_add will call reinit_frame_cache.
00353      But we are stopped in the runtime loader and we do not have symbols
00354      for the runtime loader.  So heuristic_proc_start will be called
00355      and will put out an annoying warning.
00356      Delaying the resetting of stop_soon until after symbol loading
00357      suppresses the warning.  */
00358   solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
00359   inf->control.stop_soon = NO_STOP_QUIETLY;
00360 }
00361 
00362 /* target_so_ops callback.  Do additional symbol handling, lookup, etc. after
00363    symbols for a shared object have been loaded.  */
00364 
00365 static void
00366 osf_special_symbol_handling (void)
00367 {
00368   return;
00369 }
00370 
00371 /* Initialize CTXT in preparation for iterating through the inferior's module
00372    list using read_map().  Return success.  */
00373 
00374 static int
00375 open_map (struct read_map_ctxt *ctxt)
00376 {
00377 #ifdef USE_LDR_ROUTINES
00378   /* Note: As originally written, ldr_my_process() was used to obtain
00379      the value for ctxt->proc.  This is incorrect, however, since
00380      ldr_my_process() retrieves the "unique identifier" associated
00381      with the current process (i.e. GDB) and not the one being
00382      debugged.  Presumably, the pid of the process being debugged is
00383      compatible with the "unique identifier" used by the ldr_
00384      routines, so we use that.  */
00385   ctxt->proc = ptid_get_pid (inferior_ptid);
00386   if (ldr_xattach (ctxt->proc) != 0)
00387     return 0;
00388   ctxt->next = LDR_NULL_MODULE;
00389 #else
00390   CORE_ADDR ldr_context_addr, prev, next;
00391   ldr_context_t ldr_context;
00392 
00393   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
00394                           (char *) &ldr_context_addr,
00395                           sizeof (CORE_ADDR)) != 0)
00396     return 0;
00397   if (target_read_memory (ldr_context_addr,
00398                           (char *) &ldr_context,
00399                           sizeof (ldr_context_t)) != 0)
00400     return 0;
00401   ctxt->next = ldr_context.head;
00402   ctxt->tail = ldr_context.tail;
00403 #endif
00404   return 1;
00405 }
00406 
00407 /* Initialize SO to have module NAME, /sbin/loader indicator ISLOADR, and
00408    space for NSECS sections.  */
00409 
00410 static void
00411 init_so (struct so_list *so, char *name, int isloader, int nsecs)
00412 {
00413   int namelen, i;
00414 
00415   /* solib.c requires various fields to be initialized to 0.  */
00416   memset (so, 0, sizeof *so);
00417 
00418   /* Copy the name.  */
00419   namelen = strlen (name);
00420   if (namelen >= SO_NAME_MAX_PATH_SIZE)
00421     namelen = SO_NAME_MAX_PATH_SIZE - 1;
00422 
00423   memcpy (so->so_original_name, name, namelen);
00424   so->so_original_name[namelen] = '\0';
00425   memcpy (so->so_name, so->so_original_name, namelen + 1);
00426 
00427   /* Allocate section space.  */
00428   so->lm_info = xmalloc (sizeof (struct lm_info)
00429                          + (nsecs - 1) * sizeof (struct lm_sec));
00430   so->lm_info->isloader = isloader;
00431   so->lm_info->nsecs = nsecs;
00432   for (i = 0; i < nsecs; i++)
00433     so->lm_info->secs[i].name = NULL;
00434 }
00435 
00436 /* Initialize SO's section SECIDX with name address NAMEADDR, name string
00437    NAME, default virtual address VADDR, and actual virtual address
00438    MAPADDR.  */
00439 
00440 static void
00441 init_sec (struct so_list *so, int secidx, CORE_ADDR nameaddr,
00442           const char *name, CORE_ADDR vaddr, CORE_ADDR mapaddr)
00443 {
00444   struct lm_sec *lms;
00445 
00446   lms = so->lm_info->secs + secidx;
00447   lms->nameaddr = nameaddr;
00448   lms->name = name;
00449   lms->offset = mapaddr - vaddr;
00450 }
00451 
00452 /* If there are more elements starting at CTXT in inferior's module list,
00453    store the next element in SO, advance CTXT to the next element, and return
00454    1, else return 0.  */
00455 
00456 static int
00457 read_map (struct read_map_ctxt *ctxt, struct so_list *so)
00458 {
00459   ldr_module_info_t minf;
00460   ldr_region_info_t rinf;
00461 
00462 #ifdef USE_LDR_ROUTINES
00463   size_t size;
00464   ldr_region_t i;
00465 
00466   /* Retrieve the next element.  */
00467   if (ldr_next_module (ctxt->proc, &ctxt->next) != 0)
00468     return 0;
00469   if (ctxt->next == LDR_NULL_MODULE)
00470     return 0;
00471   if (ldr_inq_module (ctxt->proc, ctxt->next, &minf, sizeof minf, &size) != 0)
00472     return 0;
00473 
00474   /* Initialize the module name and section count.  */
00475   init_so (so, minf.lmi_name, 0, minf.lmi_nregion);
00476 
00477   /* Retrieve section names and offsets.  */
00478   for (i = 0; i < minf.lmi_nregion; i++)
00479     {
00480       if (ldr_inq_region (ctxt->proc, ctxt->next, i, &rinf,
00481                           sizeof rinf, &size) != 0)
00482         goto err;
00483       init_sec (so, (int) i, 0, xstrdup (rinf.lri_name),
00484                 (CORE_ADDR) rinf.lri_vaddr, (CORE_ADDR) rinf.lri_mapaddr);
00485     }
00486   lm_secs_sort (so->lm_info);
00487 #else
00488   char *name;
00489   int errcode, i;
00490 
00491   /* Retrieve the next element.  */
00492   if (!ctxt->next)
00493     return 0;
00494   if (target_read_memory (ctxt->next, (char *) &minf, sizeof minf) != 0)
00495     return 0;
00496   if (ctxt->next == ctxt->tail)
00497     ctxt->next = 0;
00498   else
00499     ctxt->next = minf.next;
00500 
00501   /* Initialize the module name and section count.  */
00502   target_read_string (minf.module_name, &name, PATH_MAX, &errcode);
00503   if (errcode != 0)
00504     return 0;
00505   init_so (so, name, !minf.modinfo_addr, minf.region_count);
00506   xfree (name);
00507 
00508   /* Retrieve section names and offsets.  */
00509   for (i = 0; i < minf.region_count; i++)
00510     {
00511       if (target_read_memory (minf.regioninfo_addr + i * sizeof rinf,
00512                               (char *) &rinf, sizeof rinf) != 0)
00513         goto err;
00514       init_sec (so, i, rinf.regionname_addr, NULL, rinf.vaddr, rinf.mapaddr);
00515     }
00516 #endif   /* !USE_LDR_ROUTINES */
00517   return 1;
00518 
00519  err:
00520   osf_free_so (so);
00521   return 0;
00522 }
00523 
00524 /* Free resources allocated by open_map (CTXT).  */
00525 
00526 static void
00527 close_map (struct read_map_ctxt *ctxt)
00528 {
00529 #ifdef USE_LDR_ROUTINES
00530   ldr_xdetach (ctxt->proc);
00531 #endif
00532 }
00533 
00534 /* target_so_ops callback.  Return a list of shared objects currently loaded
00535    in the inferior.  */
00536 
00537 static struct so_list *
00538 osf_current_sos (void)
00539 {
00540   struct so_list *head = NULL, *tail = NULL, *newtail, so;
00541   struct read_map_ctxt ctxt;
00542   int skipped_main;
00543 
00544   if (!open_map (&ctxt))
00545     return NULL;
00546 
00547   /* Read subsequent elements.  */
00548   for (skipped_main = 0;;)
00549     {
00550       if (!read_map (&ctxt, &so))
00551         break;
00552 
00553       /* Skip the main program module, which is first in the list after
00554          /sbin/loader.  */
00555       if (!so.lm_info->isloader && !skipped_main)
00556         {
00557           osf_free_so (&so);
00558           skipped_main = 1;
00559           continue;
00560         }
00561 
00562       newtail = xmalloc (sizeof *newtail);
00563       if (!head)
00564         head = newtail;
00565       else
00566         tail->next = newtail;
00567       tail = newtail;
00568 
00569       memcpy (tail, &so, sizeof so);
00570       tail->next = NULL;
00571     }
00572 
00573   close_map (&ctxt);
00574   return head;
00575 }
00576 
00577 /* target_so_ops callback.  Attempt to locate and open the main symbol
00578    file.  */
00579 
00580 static int
00581 osf_open_symbol_file_object (void *from_ttyp)
00582 {
00583   struct read_map_ctxt ctxt;
00584   struct so_list so;
00585   int found;
00586 
00587   if (symfile_objfile)
00588     if (!query (_("Attempt to reload symbols from process? ")))
00589       return 0;
00590 
00591   /* The first module after /sbin/loader is the main program.  */
00592   if (!open_map (&ctxt))
00593     return 0;
00594   for (found = 0; !found;)
00595     {
00596       if (!read_map (&ctxt, &so))
00597         break;
00598       found = !so.lm_info->isloader;
00599       osf_free_so (&so);
00600     }
00601   close_map (&ctxt);
00602 
00603   if (found)
00604     symbol_file_add_main (so.so_name, *(int *) from_ttyp);
00605   return found;
00606 }
00607 
00608 /* target_so_ops callback.  Return whether PC is in the dynamic linker.  */
00609 
00610 static int
00611 osf_in_dynsym_resolve_code (CORE_ADDR pc)
00612 {
00613   /* This function currently always return False.  This is a temporary
00614      solution which only consequence is to introduce a minor incovenience
00615      for the user: When stepping inside a subprogram located in a shared
00616      library, gdb might stop inside the dynamic loader code instead of
00617      inside the subprogram itself.  See the explanations in infrun.c about
00618      the in_solib_dynsym_resolve_code() function for more details.  */
00619   return 0;
00620 }
00621 
00622 static struct target_so_ops osf_so_ops;
00623 
00624 void
00625 _initialize_osf_solib (void)
00626 {
00627   osf_so_ops.relocate_section_addresses = osf_relocate_section_addresses;
00628   osf_so_ops.free_so = osf_free_so;
00629   osf_so_ops.clear_solib = osf_clear_solib;
00630   osf_so_ops.solib_create_inferior_hook = osf_solib_create_inferior_hook;
00631   osf_so_ops.special_symbol_handling = osf_special_symbol_handling;
00632   osf_so_ops.current_sos = osf_current_sos;
00633   osf_so_ops.open_symbol_file_object = osf_open_symbol_file_object;
00634   osf_so_ops.in_dynsym_resolve_code = osf_in_dynsym_resolve_code;
00635   osf_so_ops.bfd_open = solib_bfd_open;
00636 
00637   /* FIXME: Don't do this here.  *_gdbarch_init() should set so_ops.  */
00638   current_target_so_ops = &osf_so_ops;
00639 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines