]> Git Repo - binutils.git/blob - gdb/solib-osf.c
* win32-nat.c (do_initial_win32_stuff): Set inferior_ptid.
[binutils.git] / gdb / solib-osf.c
1 /* Handle OSF/1, Digital UNIX, and Tru64 shared libraries
2    for GDB, the GNU Debugger.
3    Copyright (C) 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2007, 2008
4    Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 /* When handling shared libraries, GDB has to find out the pathnames
22    of all shared libraries that are currently loaded (to read in their
23    symbols) and where the shared libraries are loaded in memory
24    (to relocate them properly from their prelinked addresses to the
25    current load address).
26
27    Under OSF/1 there are two possibilities to get at this information:
28
29    1) Peek around in the runtime loader structures.
30    These are not documented, and they are not defined in the system
31    header files. The definitions below were obtained by experimentation,
32    but they seem stable enough.
33
34    2) Use the libxproc.a library, which contains the equivalent ldr_*
35    routines.  The library is documented in Tru64 5.x, but as of 5.1, it
36    only allows a process to examine itself.  On earlier versions, it
37    may require that the GDB executable be dynamically linked and that
38    NAT_CLIBS include -lxproc -Wl,-expect_unresolved,ldr_process_context
39    for GDB and all applications that are using libgdb.
40
41    We will use the peeking approach until libxproc.a works for other
42    processes.  */
43
44 #include "defs.h"
45
46 #include <sys/types.h>
47 #include <signal.h>
48 #include "gdb_string.h"
49
50 #include "bfd.h"
51 #include "symtab.h"
52 #include "symfile.h"
53 #include "objfiles.h"
54 #include "target.h"
55 #include "inferior.h"
56 #include "gdbthread.h"
57 #include "solist.h"
58
59 #ifdef USE_LDR_ROUTINES
60 # include <loader.h>
61 #endif
62
63 #ifndef USE_LDR_ROUTINES
64 /* Definition of runtime loader structures, found by experimentation.  */
65 #define RLD_CONTEXT_ADDRESS     0x3ffc0000000
66
67 /* Per-module information structure referenced by ldr_context_t.head.  */
68
69 typedef struct
70   {
71     CORE_ADDR next;
72     CORE_ADDR previous;
73     CORE_ADDR unknown1;
74     CORE_ADDR module_name;
75     CORE_ADDR modinfo_addr;     /* used by next_link_map_member() to detect
76                                    the end of the shared module list */
77     long module_id;
78     CORE_ADDR unknown2;
79     CORE_ADDR unknown3;
80     long region_count;
81     CORE_ADDR regioninfo_addr;
82   }
83 ldr_module_info_t;
84
85 /* Per-region structure referenced by ldr_module_info_t.regioninfo_addr.  */
86
87 typedef struct
88   {
89     long unknown1;
90     CORE_ADDR regionname_addr;
91     long protection;
92     CORE_ADDR vaddr;
93     CORE_ADDR mapaddr;
94     long size;
95     long unknown2[5];
96   }
97 ldr_region_info_t;
98
99 /* Structure at RLD_CONTEXT_ADDRESS specifying the start and finish addresses
100    of the shared module list.  */
101
102 typedef struct
103   {
104     CORE_ADDR unknown1;
105     CORE_ADDR unknown2;
106     CORE_ADDR head;
107     CORE_ADDR tail;
108   }
109 ldr_context_t;
110 #endif   /* !USE_LDR_ROUTINES */
111
112 /* Per-section information, stored in struct lm_info.secs.  */
113
114 struct lm_sec
115   {
116     CORE_ADDR offset;           /* difference between default and actual
117                                    virtual addresses of section .name */
118     CORE_ADDR nameaddr;         /* address in inferior of section name */
119     const char *name;           /* name of section, null if not fetched */
120   };
121
122 /* Per-module information, stored in struct so_list.lm_info.  */
123
124 struct lm_info
125   {
126     int isloader;               /* whether the module is /sbin/loader */
127     int nsecs;                  /* length of .secs */
128     struct lm_sec secs[1];      /* variable-length array of sections, sorted
129                                    by name */
130   };
131
132 /* Context for iterating through the inferior's shared module list.  */
133
134 struct read_map_ctxt
135   {
136 #ifdef USE_LDR_ROUTINES
137     ldr_process_t proc;
138     ldr_module_t next;
139 #else
140     CORE_ADDR next;             /* next element in module list */
141     CORE_ADDR tail;             /* last element in module list */
142 #endif
143   };
144
145 /* Forward declaration for this module's autoinit function.  */
146
147 extern void _initialize_osf_solib (void);
148
149 #ifdef USE_LDR_ROUTINES
150 # if 0
151 /* This routine is intended to be called by ldr_* routines to read memory from
152    the current target.  Usage:
153
154      ldr_process = ldr_core_process ();
155      ldr_set_core_reader (ldr_read_memory);
156      ldr_xdetach (ldr_process);
157      ldr_xattach (ldr_process);
158
159    ldr_core_process() and ldr_read_memory() are neither documented nor
160    declared in system header files.  They work with OSF/1 2.x, and they might
161    work with later versions as well.  */
162
163 static int
164 ldr_read_memory (CORE_ADDR memaddr, char *myaddr, int len, int readstring)
165 {
166   int result;
167   char *buffer;
168
169   if (readstring)
170     {
171       target_read_string (memaddr, &buffer, len, &result);
172       if (result == 0)
173         strcpy (myaddr, buffer);
174       xfree (buffer);
175     }
176   else
177     result = target_read_memory (memaddr, myaddr, len);
178
179   if (result != 0)
180     result = -result;
181   return result;
182 }
183 # endif   /* 0 */
184 #endif   /* USE_LDR_ROUTINES */
185
186 /* Comparison for qsort() and bsearch(): return -1, 0, or 1 according to
187    whether lm_sec *P1's name is lexically less than, equal to, or greater
188    than that of *P2.  */
189
190 static int
191 lm_sec_cmp (const void *p1, const void *p2)
192 {
193   const struct lm_sec *lms1 = p1, *lms2 = p2;
194   return strcmp (lms1->name, lms2->name);
195 }
196
197 /* Sort LMI->secs so that osf_relocate_section_addresses() can binary-search
198    it.  */
199
200 static void
201 lm_secs_sort (struct lm_info *lmi)
202 {
203   qsort (lmi->secs, lmi->nsecs, sizeof *lmi->secs, lm_sec_cmp);
204 }
205
206 /* Populate name fields of LMI->secs.  */
207
208 static void
209 fetch_sec_names (struct lm_info *lmi)
210 {
211 #ifndef USE_LDR_ROUTINES
212   int i, errcode;
213   struct lm_sec *lms;
214   char *name;
215
216   for (i = 0; i < lmi->nsecs; i++)
217     {
218       lms = lmi->secs + i;
219       target_read_string (lms->nameaddr, &name, PATH_MAX, &errcode);
220       if (errcode != 0)
221         {
222           warning (_("unable to read shared sec name at 0x%lx"), lms->nameaddr);
223           name = xstrdup ("");
224         }
225       lms->name = name;
226     }
227   lm_secs_sort (lmi);
228 #endif
229 }
230
231 /* target_so_ops callback.  Adjust SEC's addresses after it's been mapped into
232    the process.  */
233
234 static void
235 osf_relocate_section_addresses (struct so_list *so,
236                                 struct section_table *sec)
237 {
238   struct lm_info *lmi;
239   struct lm_sec lms_key, *lms;
240
241   /* Fetch SO's section names if we haven't done so already.  */
242   lmi = so->lm_info;
243   if (lmi->nsecs && !lmi->secs[0].name)
244     fetch_sec_names (lmi);
245
246   /* Binary-search for offset information corresponding to SEC.  */
247   lms_key.name = sec->the_bfd_section->name;
248   lms = bsearch (&lms_key, lmi->secs, lmi->nsecs, sizeof *lms, lm_sec_cmp);
249   if (lms)
250     {
251       sec->addr += lms->offset;
252       sec->endaddr += lms->offset;
253     }
254 }
255
256 /* target_so_ops callback.  Free parts of SO allocated by this file.  */
257
258 static void
259 osf_free_so (struct so_list *so)
260 {
261   int i;
262   const char *name;
263
264   for (i = 0; i < so->lm_info->nsecs; i++)
265     {
266       name = so->lm_info->secs[i].name;
267       if (name)
268         xfree ((void *) name);
269     }
270   xfree (so->lm_info);
271 }
272
273 /* target_so_ops callback.  Discard information accumulated by this file and
274    not freed by osf_free_so().  */
275
276 static void
277 osf_clear_solib (void)
278 {
279   return;
280 }
281
282 /* target_so_ops callback.  Prepare to handle shared libraries after the
283    inferior process has been created but before it's executed any
284    instructions.
285
286    For a statically bound executable, the inferior's first instruction is the
287    one at "_start", or a similar text label. No further processing is needed
288    in that case.
289
290    For a dynamically bound executable, this first instruction is somewhere
291    in the rld, and the actual user executable is not yet mapped in.
292    We continue the inferior again, rld then maps in the actual user
293    executable and any needed shared libraries and then sends
294    itself a SIGTRAP.
295
296    At that point we discover the names of all shared libraries and
297    read their symbols in.
298
299    FIXME
300
301    This code does not properly handle hitting breakpoints which the
302    user might have set in the rld itself.  Proper handling would have
303    to check if the SIGTRAP happened due to a kill call.
304
305    Also, what if child has exit()ed?  Must exit loop somehow.  */
306
307 static void
308 osf_solib_create_inferior_hook (void)
309 {
310   struct inferior *inf;
311   struct thread_info *tp;
312
313   inf = current_inferior ();
314
315   /* If we are attaching to the inferior, the shared libraries
316      have already been mapped, so nothing more to do.  */
317   if (inf->attach_flag)
318     return;
319
320   /* Nothing to do for statically bound executables.  */
321
322   if (symfile_objfile == NULL
323       || symfile_objfile->obfd == NULL
324       || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
325     return;
326
327   /* Now run the target.  It will eventually get a SIGTRAP, at
328      which point all of the libraries will have been mapped in and we
329      can go groveling around in the rld structures to find
330      out what we need to know about them.
331      
332      If debugging from a core file, we cannot resume the execution
333      of the inferior.  But this is actually not an issue, because
334      shared libraries have already been mapped anyways, which means
335      we have nothing more to do.  */
336   if (!target_can_run (&current_target))
337     return;
338
339   tp = inferior_thread ();
340   clear_proceed_status ();
341   inf->stop_soon = STOP_QUIETLY;
342   tp->stop_signal = TARGET_SIGNAL_0;
343   do
344     {
345       target_resume (minus_one_ptid, 0, tp->stop_signal);
346       wait_for_inferior (0);
347     }
348   while (tp->stop_signal != TARGET_SIGNAL_TRAP);
349
350   /*  solib_add will call reinit_frame_cache.
351      But we are stopped in the runtime loader and we do not have symbols
352      for the runtime loader. So heuristic_proc_start will be called
353      and will put out an annoying warning.
354      Delaying the resetting of stop_soon until after symbol loading
355      suppresses the warning.  */
356   solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
357   inf->stop_soon = NO_STOP_QUIETLY;
358 }
359
360 /* target_so_ops callback.  Do additional symbol handling, lookup, etc. after
361    symbols for a shared object have been loaded.  */
362
363 static void
364 osf_special_symbol_handling (void)
365 {
366   return;
367 }
368
369 /* Initialize CTXT in preparation for iterating through the inferior's module
370    list using read_map().  Return success.  */
371
372 static int
373 open_map (struct read_map_ctxt *ctxt)
374 {
375 #ifdef USE_LDR_ROUTINES
376   /* Note: As originally written, ldr_my_process() was used to obtain
377      the value for ctxt->proc.  This is incorrect, however, since
378      ldr_my_process() retrieves the "unique identifier" associated
379      with the current process (i.e. GDB) and not the one being
380      debugged.  Presumably, the pid of the process being debugged is
381      compatible with the "unique identifier" used by the ldr_
382      routines, so we use that.  */
383   ctxt->proc = ptid_get_pid (inferior_ptid);
384   if (ldr_xattach (ctxt->proc) != 0)
385     return 0;
386   ctxt->next = LDR_NULL_MODULE;
387 #else
388   CORE_ADDR ldr_context_addr, prev, next;
389   ldr_context_t ldr_context;
390
391   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
392                           (char *) &ldr_context_addr,
393                           sizeof (CORE_ADDR)) != 0)
394     return 0;
395   if (target_read_memory (ldr_context_addr,
396                           (char *) &ldr_context,
397                           sizeof (ldr_context_t)) != 0)
398     return 0;
399   ctxt->next = ldr_context.head;
400   ctxt->tail = ldr_context.tail;
401 #endif
402   return 1;
403 }
404
405 /* Initialize SO to have module NAME, /sbin/loader indicator ISLOADR, and
406    space for NSECS sections.  */
407
408 static void
409 init_so (struct so_list *so, char *name, int isloader, int nsecs)
410 {
411   int namelen, i;
412
413   /* solib.c requires various fields to be initialized to 0.  */
414   memset (so, 0, sizeof *so);
415
416   /* Copy the name.  */
417   namelen = strlen (name);
418   if (namelen >= SO_NAME_MAX_PATH_SIZE)
419     namelen = SO_NAME_MAX_PATH_SIZE - 1;
420
421   memcpy (so->so_original_name, name, namelen);
422   so->so_original_name[namelen] = '\0';
423   memcpy (so->so_name, so->so_original_name, namelen + 1);
424
425   /* Allocate section space.  */
426   so->lm_info = xmalloc ((unsigned) &(((struct lm_info *)0)->secs) +
427                          nsecs * sizeof *so->lm_info);
428   so->lm_info->isloader = isloader;
429   so->lm_info->nsecs = nsecs;
430   for (i = 0; i < nsecs; i++)
431     so->lm_info->secs[i].name = NULL;
432 }
433
434 /* Initialize SO's section SECIDX with name address NAMEADDR, name string
435    NAME, default virtual address VADDR, and actual virtual address
436    MAPADDR.  */
437
438 static void
439 init_sec (struct so_list *so, int secidx, CORE_ADDR nameaddr,
440           const char *name, CORE_ADDR vaddr, CORE_ADDR mapaddr)
441 {
442   struct lm_sec *lms;
443
444   lms = so->lm_info->secs + secidx;
445   lms->nameaddr = nameaddr;
446   lms->name = name;
447   lms->offset = mapaddr - vaddr;
448 }
449
450 /* If there are more elements starting at CTXT in inferior's module list,
451    store the next element in SO, advance CTXT to the next element, and return
452    1, else return 0.  */
453
454 static int
455 read_map (struct read_map_ctxt *ctxt, struct so_list *so)
456 {
457   ldr_module_info_t minf;
458   ldr_region_info_t rinf;
459
460 #ifdef USE_LDR_ROUTINES
461   size_t size;
462   ldr_region_t i;
463
464   /* Retrieve the next element.  */
465   if (ldr_next_module (ctxt->proc, &ctxt->next) != 0)
466     return 0;
467   if (ctxt->next == LDR_NULL_MODULE)
468     return 0;
469   if (ldr_inq_module (ctxt->proc, ctxt->next, &minf, sizeof minf, &size) != 0)
470     return 0;
471
472   /* Initialize the module name and section count.  */
473   init_so (so, minf.lmi_name, 0, minf.lmi_nregion);
474
475   /* Retrieve section names and offsets.  */
476   for (i = 0; i < minf.lmi_nregion; i++)
477     {
478       if (ldr_inq_region (ctxt->proc, ctxt->next, i, &rinf,
479                           sizeof rinf, &size) != 0)
480         goto err;
481       init_sec (so, (int) i, 0, xstrdup (rinf.lri_name),
482                 (CORE_ADDR) rinf.lri_vaddr, (CORE_ADDR) rinf.lri_mapaddr);
483     }
484   lm_secs_sort (so->lm_info);
485 #else
486   char *name;
487   int errcode, i;
488
489   /* Retrieve the next element.  */
490   if (!ctxt->next)
491     return 0;
492   if (target_read_memory (ctxt->next, (char *) &minf, sizeof minf) != 0)
493     return 0;
494   if (ctxt->next == ctxt->tail)
495     ctxt->next = 0;
496   else
497     ctxt->next = minf.next;
498
499   /* Initialize the module name and section count.  */
500   target_read_string (minf.module_name, &name, PATH_MAX, &errcode);
501   if (errcode != 0)
502     return 0;
503   init_so (so, name, !minf.modinfo_addr, minf.region_count);
504   xfree (name);
505
506   /* Retrieve section names and offsets.  */
507   for (i = 0; i < minf.region_count; i++)
508     {
509       if (target_read_memory (minf.regioninfo_addr + i * sizeof rinf,
510                               (char *) &rinf, sizeof rinf) != 0)
511         goto err;
512       init_sec (so, i, rinf.regionname_addr, NULL, rinf.vaddr, rinf.mapaddr);
513     }
514 #endif   /* !USE_LDR_ROUTINES */
515   return 1;
516
517  err:
518   osf_free_so (so);
519   return 0;
520 }
521
522 /* Free resources allocated by open_map (CTXT).  */
523
524 static void
525 close_map (struct read_map_ctxt *ctxt)
526 {
527 #ifdef USE_LDR_ROUTINES
528   ldr_xdetach (ctxt->proc);
529 #endif
530 }
531
532 /* target_so_ops callback.  Return a list of shared objects currently loaded
533    in the inferior.  */
534
535 static struct so_list *
536 osf_current_sos (void)
537 {
538   struct so_list *head = NULL, *tail, *newtail, so;
539   struct read_map_ctxt ctxt;
540   int skipped_main;
541
542   if (!open_map (&ctxt))
543     return NULL;
544
545   /* Read subsequent elements.  */
546   for (skipped_main = 0;;)
547     {
548       if (!read_map (&ctxt, &so))
549         break;
550
551       /* Skip the main program module, which is first in the list after
552          /sbin/loader.  */
553       if (!so.lm_info->isloader && !skipped_main)
554         {
555           osf_free_so (&so);
556           skipped_main = 1;
557           continue;
558         }
559
560       newtail = xmalloc (sizeof *newtail);
561       if (!head)
562         head = newtail;
563       else
564         tail->next = newtail;
565       tail = newtail;
566
567       memcpy (tail, &so, sizeof so);
568       tail->next = NULL;
569     }
570
571   close_map (&ctxt);
572   return head;
573 }
574
575 /* target_so_ops callback.  Attempt to locate and open the main symbol
576    file.  */
577
578 static int
579 osf_open_symbol_file_object (void *from_ttyp)
580 {
581   struct read_map_ctxt ctxt;
582   struct so_list so;
583   int found;
584
585   if (symfile_objfile)
586     if (!query ("Attempt to reload symbols from process? "))
587       return 0;
588
589   /* The first module after /sbin/loader is the main program.  */
590   if (!open_map (&ctxt))
591     return 0;
592   for (found = 0; !found;)
593     {
594       if (!read_map (&ctxt, &so))
595         break;
596       found = !so.lm_info->isloader;
597       osf_free_so (&so);
598     }
599   close_map (&ctxt);
600
601   if (found)
602     symbol_file_add_main (so.so_name, *(int *) from_ttyp);
603   return found;
604 }
605
606 /* target_so_ops callback.  Return whether PC is in the dynamic linker.  */
607
608 static int
609 osf_in_dynsym_resolve_code (CORE_ADDR pc)
610 {
611   /* This function currently always return False. This is a temporary
612      solution which only consequence is to introduce a minor incovenience
613      for the user: When stepping inside a subprogram located in a shared
614      library, gdb might stop inside the dynamic loader code instead of
615      inside the subprogram itself. See the explanations in infrun.c about
616      the in_solib_dynsym_resolve_code() function for more details. */
617   return 0;
618 }
619
620 static struct target_so_ops osf_so_ops;
621
622 void
623 _initialize_osf_solib (void)
624 {
625   osf_so_ops.relocate_section_addresses = osf_relocate_section_addresses;
626   osf_so_ops.free_so = osf_free_so;
627   osf_so_ops.clear_solib = osf_clear_solib;
628   osf_so_ops.solib_create_inferior_hook = osf_solib_create_inferior_hook;
629   osf_so_ops.special_symbol_handling = osf_special_symbol_handling;
630   osf_so_ops.current_sos = osf_current_sos;
631   osf_so_ops.open_symbol_file_object = osf_open_symbol_file_object;
632   osf_so_ops.in_dynsym_resolve_code = osf_in_dynsym_resolve_code;
633
634   /* FIXME: Don't do this here.  *_gdbarch_init() should set so_ops. */
635   current_target_so_ops = &osf_so_ops;
636 }
This page took 0.061812 seconds and 4 git commands to generate.