Update
[gdb.git] / gdb / solib-osf.c
blob02cc4db4c057b716d9692f12e7c451616f79fd1e
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.
6 This file is part of GDB.
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.
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.
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/>. */
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).
27 Under OSF/1 there are two possibilities to get at this information:
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.
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.
41 We will use the peeking approach until libxproc.a works for other
42 processes. */
44 #include "defs.h"
46 #include <sys/types.h>
47 #include <signal.h>
48 #include "gdb_string.h"
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 "solist.h"
58 #ifdef USE_LDR_ROUTINES
59 # include <loader.h>
60 #endif
62 #ifndef USE_LDR_ROUTINES
63 /* Definition of runtime loader structures, found by experimentation. */
64 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
66 /* Per-module information structure referenced by ldr_context_t.head. */
68 typedef struct
70 CORE_ADDR next;
71 CORE_ADDR previous;
72 CORE_ADDR unknown1;
73 CORE_ADDR module_name;
74 CORE_ADDR modinfo_addr; /* used by next_link_map_member() to detect
75 the end of the shared module list */
76 long module_id;
77 CORE_ADDR unknown2;
78 CORE_ADDR unknown3;
79 long region_count;
80 CORE_ADDR regioninfo_addr;
82 ldr_module_info_t;
84 /* Per-region structure referenced by ldr_module_info_t.regioninfo_addr. */
86 typedef struct
88 long unknown1;
89 CORE_ADDR regionname_addr;
90 long protection;
91 CORE_ADDR vaddr;
92 CORE_ADDR mapaddr;
93 long size;
94 long unknown2[5];
96 ldr_region_info_t;
98 /* Structure at RLD_CONTEXT_ADDRESS specifying the start and finish addresses
99 of the shared module list. */
101 typedef struct
103 CORE_ADDR unknown1;
104 CORE_ADDR unknown2;
105 CORE_ADDR head;
106 CORE_ADDR tail;
108 ldr_context_t;
109 #endif /* !USE_LDR_ROUTINES */
111 /* Per-section information, stored in struct lm_info.secs. */
113 struct lm_sec
115 CORE_ADDR offset; /* difference between default and actual
116 virtual addresses of section .name */
117 CORE_ADDR nameaddr; /* address in inferior of section name */
118 const char *name; /* name of section, null if not fetched */
121 /* Per-module information, stored in struct so_list.lm_info. */
123 struct lm_info
125 int isloader; /* whether the module is /sbin/loader */
126 int nsecs; /* length of .secs */
127 struct lm_sec secs[1]; /* variable-length array of sections, sorted
128 by name */
131 /* Context for iterating through the inferior's shared module list. */
133 struct read_map_ctxt
135 #ifdef USE_LDR_ROUTINES
136 ldr_process_t proc;
137 ldr_module_t next;
138 #else
139 CORE_ADDR next; /* next element in module list */
140 CORE_ADDR tail; /* last element in module list */
141 #endif
144 /* Forward declaration for this module's autoinit function. */
146 extern void _initialize_osf_solib (void);
148 #ifdef USE_LDR_ROUTINES
149 # if 0
150 /* This routine is intended to be called by ldr_* routines to read memory from
151 the current target. Usage:
153 ldr_process = ldr_core_process ();
154 ldr_set_core_reader (ldr_read_memory);
155 ldr_xdetach (ldr_process);
156 ldr_xattach (ldr_process);
158 ldr_core_process() and ldr_read_memory() are neither documented nor
159 declared in system header files. They work with OSF/1 2.x, and they might
160 work with later versions as well. */
162 static int
163 ldr_read_memory (CORE_ADDR memaddr, char *myaddr, int len, int readstring)
165 int result;
166 char *buffer;
168 if (readstring)
170 target_read_string (memaddr, &buffer, len, &result);
171 if (result == 0)
172 strcpy (myaddr, buffer);
173 xfree (buffer);
175 else
176 result = target_read_memory (memaddr, myaddr, len);
178 if (result != 0)
179 result = -result;
180 return result;
182 # endif /* 0 */
183 #endif /* USE_LDR_ROUTINES */
185 /* Comparison for qsort() and bsearch(): return -1, 0, or 1 according to
186 whether lm_sec *P1's name is lexically less than, equal to, or greater
187 than that of *P2. */
189 static int
190 lm_sec_cmp (const void *p1, const void *p2)
192 const struct lm_sec *lms1 = p1, *lms2 = p2;
193 return strcmp (lms1->name, lms2->name);
196 /* Sort LMI->secs so that osf_relocate_section_addresses() can binary-search
197 it. */
199 static void
200 lm_secs_sort (struct lm_info *lmi)
202 qsort (lmi->secs, lmi->nsecs, sizeof *lmi->secs, lm_sec_cmp);
205 /* Populate name fields of LMI->secs. */
207 static void
208 fetch_sec_names (struct lm_info *lmi)
210 #ifndef USE_LDR_ROUTINES
211 int i, errcode;
212 struct lm_sec *lms;
213 char *name;
215 for (i = 0; i < lmi->nsecs; i++)
217 lms = lmi->secs + i;
218 target_read_string (lms->nameaddr, &name, PATH_MAX, &errcode);
219 if (errcode != 0)
221 warning (_("unable to read shared sec name at 0x%lx"), lms->nameaddr);
222 name = xstrdup ("");
224 lms->name = name;
226 lm_secs_sort (lmi);
227 #endif
230 /* target_so_ops callback. Adjust SEC's addresses after it's been mapped into
231 the process. */
233 static void
234 osf_relocate_section_addresses (struct so_list *so,
235 struct section_table *sec)
237 struct lm_info *lmi;
238 struct lm_sec lms_key, *lms;
240 /* Fetch SO's section names if we haven't done so already. */
241 lmi = so->lm_info;
242 if (lmi->nsecs && !lmi->secs[0].name)
243 fetch_sec_names (lmi);
245 /* Binary-search for offset information corresponding to SEC. */
246 lms_key.name = sec->the_bfd_section->name;
247 lms = bsearch (&lms_key, lmi->secs, lmi->nsecs, sizeof *lms, lm_sec_cmp);
248 if (lms)
250 sec->addr += lms->offset;
251 sec->endaddr += lms->offset;
255 /* target_so_ops callback. Free parts of SO allocated by this file. */
257 static void
258 osf_free_so (struct so_list *so)
260 int i;
261 const char *name;
263 for (i = 0; i < so->lm_info->nsecs; i++)
265 name = so->lm_info->secs[i].name;
266 if (name)
267 xfree ((void *) name);
269 xfree (so->lm_info);
272 /* target_so_ops callback. Discard information accumulated by this file and
273 not freed by osf_free_so(). */
275 static void
276 osf_clear_solib (void)
278 return;
281 /* target_so_ops callback. Prepare to handle shared libraries after the
282 inferior process has been created but before it's executed any
283 instructions.
285 For a statically bound executable, the inferior's first instruction is the
286 one at "_start", or a similar text label. No further processing is needed
287 in that case.
289 For a dynamically bound executable, this first instruction is somewhere
290 in the rld, and the actual user executable is not yet mapped in.
291 We continue the inferior again, rld then maps in the actual user
292 executable and any needed shared libraries and then sends
293 itself a SIGTRAP.
295 At that point we discover the names of all shared libraries and
296 read their symbols in.
298 FIXME
300 This code does not properly handle hitting breakpoints which the
301 user might have set in the rld itself. Proper handling would have
302 to check if the SIGTRAP happened due to a kill call.
304 Also, what if child has exit()ed? Must exit loop somehow. */
306 static void
307 osf_solib_create_inferior_hook (void)
309 /* Nothing to do for statically bound executables. */
311 if (symfile_objfile == NULL
312 || symfile_objfile->obfd == NULL
313 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
314 return;
316 /* Now run the target. It will eventually get a SIGTRAP, at
317 which point all of the libraries will have been mapped in and we
318 can go groveling around in the rld structures to find
319 out what we need to know about them. */
321 clear_proceed_status ();
322 stop_soon = STOP_QUIETLY;
323 stop_signal = TARGET_SIGNAL_0;
326 target_resume (minus_one_ptid, 0, stop_signal);
327 wait_for_inferior (0);
329 while (stop_signal != TARGET_SIGNAL_TRAP);
331 /* solib_add will call reinit_frame_cache.
332 But we are stopped in the runtime loader and we do not have symbols
333 for the runtime loader. So heuristic_proc_start will be called
334 and will put out an annoying warning.
335 Delaying the resetting of stop_soon until after symbol loading
336 suppresses the warning. */
337 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
338 stop_soon = NO_STOP_QUIETLY;
341 /* target_so_ops callback. Do additional symbol handling, lookup, etc. after
342 symbols for a shared object have been loaded. */
344 static void
345 osf_special_symbol_handling (void)
347 return;
350 /* Initialize CTXT in preparation for iterating through the inferior's module
351 list using read_map(). Return success. */
353 static int
354 open_map (struct read_map_ctxt *ctxt)
356 #ifdef USE_LDR_ROUTINES
357 /* Note: As originally written, ldr_my_process() was used to obtain
358 the value for ctxt->proc. This is incorrect, however, since
359 ldr_my_process() retrieves the "unique identifier" associated
360 with the current process (i.e. GDB) and not the one being
361 debugged. Presumably, the pid of the process being debugged is
362 compatible with the "unique identifier" used by the ldr_
363 routines, so we use that. */
364 ctxt->proc = ptid_get_pid (inferior_ptid);
365 if (ldr_xattach (ctxt->proc) != 0)
366 return 0;
367 ctxt->next = LDR_NULL_MODULE;
368 #else
369 CORE_ADDR ldr_context_addr, prev, next;
370 ldr_context_t ldr_context;
372 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
373 (char *) &ldr_context_addr,
374 sizeof (CORE_ADDR)) != 0)
375 return 0;
376 if (target_read_memory (ldr_context_addr,
377 (char *) &ldr_context,
378 sizeof (ldr_context_t)) != 0)
379 return 0;
380 ctxt->next = ldr_context.head;
381 ctxt->tail = ldr_context.tail;
382 #endif
383 return 1;
386 /* Initialize SO to have module NAME, /sbin/loader indicator ISLOADR, and
387 space for NSECS sections. */
389 static void
390 init_so (struct so_list *so, char *name, int isloader, int nsecs)
392 int namelen, i;
394 /* solib.c requires various fields to be initialized to 0. */
395 memset (so, 0, sizeof *so);
397 /* Copy the name. */
398 namelen = strlen (name);
399 if (namelen >= SO_NAME_MAX_PATH_SIZE)
400 namelen = SO_NAME_MAX_PATH_SIZE - 1;
402 memcpy (so->so_original_name, name, namelen);
403 so->so_original_name[namelen] = '\0';
404 memcpy (so->so_name, so->so_original_name, namelen + 1);
406 /* Allocate section space. */
407 so->lm_info = xmalloc ((unsigned) &(((struct lm_info *)0)->secs) +
408 nsecs * sizeof *so->lm_info);
409 so->lm_info->isloader = isloader;
410 so->lm_info->nsecs = nsecs;
411 for (i = 0; i < nsecs; i++)
412 so->lm_info->secs[i].name = NULL;
415 /* Initialize SO's section SECIDX with name address NAMEADDR, name string
416 NAME, default virtual address VADDR, and actual virtual address
417 MAPADDR. */
419 static void
420 init_sec (struct so_list *so, int secidx, CORE_ADDR nameaddr,
421 const char *name, CORE_ADDR vaddr, CORE_ADDR mapaddr)
423 struct lm_sec *lms;
425 lms = so->lm_info->secs + secidx;
426 lms->nameaddr = nameaddr;
427 lms->name = name;
428 lms->offset = mapaddr - vaddr;
431 /* If there are more elements starting at CTXT in inferior's module list,
432 store the next element in SO, advance CTXT to the next element, and return
433 1, else return 0. */
435 static int
436 read_map (struct read_map_ctxt *ctxt, struct so_list *so)
438 ldr_module_info_t minf;
439 ldr_region_info_t rinf;
441 #ifdef USE_LDR_ROUTINES
442 size_t size;
443 ldr_region_t i;
445 /* Retrieve the next element. */
446 if (ldr_next_module (ctxt->proc, &ctxt->next) != 0)
447 return 0;
448 if (ctxt->next == LDR_NULL_MODULE)
449 return 0;
450 if (ldr_inq_module (ctxt->proc, ctxt->next, &minf, sizeof minf, &size) != 0)
451 return 0;
453 /* Initialize the module name and section count. */
454 init_so (so, minf.lmi_name, 0, minf.lmi_nregion);
456 /* Retrieve section names and offsets. */
457 for (i = 0; i < minf.lmi_nregion; i++)
459 if (ldr_inq_region (ctxt->proc, ctxt->next, i, &rinf,
460 sizeof rinf, &size) != 0)
461 goto err;
462 init_sec (so, (int) i, 0, xstrdup (rinf.lri_name),
463 (CORE_ADDR) rinf.lri_vaddr, (CORE_ADDR) rinf.lri_mapaddr);
465 lm_secs_sort (so->lm_info);
466 #else
467 char *name;
468 int errcode, i;
470 /* Retrieve the next element. */
471 if (!ctxt->next)
472 return 0;
473 if (target_read_memory (ctxt->next, (char *) &minf, sizeof minf) != 0)
474 return 0;
475 if (ctxt->next == ctxt->tail)
476 ctxt->next = 0;
477 else
478 ctxt->next = minf.next;
480 /* Initialize the module name and section count. */
481 target_read_string (minf.module_name, &name, PATH_MAX, &errcode);
482 if (errcode != 0)
483 return 0;
484 init_so (so, name, !minf.modinfo_addr, minf.region_count);
485 xfree (name);
487 /* Retrieve section names and offsets. */
488 for (i = 0; i < minf.region_count; i++)
490 if (target_read_memory (minf.regioninfo_addr + i * sizeof rinf,
491 (char *) &rinf, sizeof rinf) != 0)
492 goto err;
493 init_sec (so, i, rinf.regionname_addr, NULL, rinf.vaddr, rinf.mapaddr);
495 #endif /* !USE_LDR_ROUTINES */
496 return 1;
498 err:
499 osf_free_so (so);
500 return 0;
503 /* Free resources allocated by open_map (CTXT). */
505 static void
506 close_map (struct read_map_ctxt *ctxt)
508 #ifdef USE_LDR_ROUTINES
509 ldr_xdetach (ctxt->proc);
510 #endif
513 /* target_so_ops callback. Return a list of shared objects currently loaded
514 in the inferior. */
516 static struct so_list *
517 osf_current_sos (void)
519 struct so_list *head = NULL, *tail, *newtail, so;
520 struct read_map_ctxt ctxt;
521 int skipped_main;
523 if (!open_map (&ctxt))
524 return NULL;
526 /* Read subsequent elements. */
527 for (skipped_main = 0;;)
529 if (!read_map (&ctxt, &so))
530 break;
532 /* Skip the main program module, which is first in the list after
533 /sbin/loader. */
534 if (!so.lm_info->isloader && !skipped_main)
536 osf_free_so (&so);
537 skipped_main = 1;
538 continue;
541 newtail = xmalloc (sizeof *newtail);
542 if (!head)
543 head = newtail;
544 else
545 tail->next = newtail;
546 tail = newtail;
548 memcpy (tail, &so, sizeof so);
549 tail->next = NULL;
552 close_map (&ctxt);
553 return head;
556 /* target_so_ops callback. Attempt to locate and open the main symbol
557 file. */
559 static int
560 osf_open_symbol_file_object (void *from_ttyp)
562 struct read_map_ctxt ctxt;
563 struct so_list so;
564 int found;
566 if (symfile_objfile)
567 if (!query ("Attempt to reload symbols from process? "))
568 return 0;
570 /* The first module after /sbin/loader is the main program. */
571 if (!open_map (&ctxt))
572 return 0;
573 for (found = 0; !found;)
575 if (!read_map (&ctxt, &so))
576 break;
577 found = !so.lm_info->isloader;
578 osf_free_so (&so);
580 close_map (&ctxt);
582 if (found)
583 symbol_file_add_main (so.so_name, *(int *) from_ttyp);
584 return found;
587 /* target_so_ops callback. Return whether PC is in the dynamic linker. */
589 static int
590 osf_in_dynsym_resolve_code (CORE_ADDR pc)
592 /* This function currently always return False. This is a temporary
593 solution which only consequence is to introduce a minor incovenience
594 for the user: When stepping inside a subprogram located in a shared
595 library, gdb might stop inside the dynamic loader code instead of
596 inside the subprogram itself. See the explanations in infrun.c about
597 the IN_SOLIB_DYNSYM_RESOLVE_CODE macro for more details. */
598 return 0;
601 static struct target_so_ops osf_so_ops;
603 void
604 _initialize_osf_solib (void)
606 osf_so_ops.relocate_section_addresses = osf_relocate_section_addresses;
607 osf_so_ops.free_so = osf_free_so;
608 osf_so_ops.clear_solib = osf_clear_solib;
609 osf_so_ops.solib_create_inferior_hook = osf_solib_create_inferior_hook;
610 osf_so_ops.special_symbol_handling = osf_special_symbol_handling;
611 osf_so_ops.current_sos = osf_current_sos;
612 osf_so_ops.open_symbol_file_object = osf_open_symbol_file_object;
613 osf_so_ops.in_dynsym_resolve_code = osf_in_dynsym_resolve_code;
615 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
616 current_target_so_ops = &osf_so_ops;