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
46 #include <sys/types.h>
48 #include "gdb_string.h"
56 #include "gdbthread.h"
59 #ifdef USE_LDR_ROUTINES
63 #ifndef USE_LDR_ROUTINES
64 /* Definition of runtime loader structures, found by experimentation. */
65 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
67 /* Per-module information structure referenced by ldr_context_t.head. */
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 */
81 CORE_ADDR regioninfo_addr
;
85 /* Per-region structure referenced by ldr_module_info_t.regioninfo_addr. */
90 CORE_ADDR regionname_addr
;
99 /* Structure at RLD_CONTEXT_ADDRESS specifying the start and finish addresses
100 of the shared module list. */
110 #endif /* !USE_LDR_ROUTINES */
112 /* Per-section information, stored in struct lm_info.secs. */
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 */
122 /* Per-module information, stored in struct so_list.lm_info. */
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
132 /* Context for iterating through the inferior's shared module list. */
136 #ifdef USE_LDR_ROUTINES
140 CORE_ADDR next
; /* next element in module list */
141 CORE_ADDR tail
; /* last element in module list */
145 /* Forward declaration for this module's autoinit function. */
147 extern void _initialize_osf_solib (void);
149 #ifdef USE_LDR_ROUTINES
151 /* This routine is intended to be called by ldr_* routines to read memory from
152 the current target. Usage:
154 ldr_process = ldr_core_process ();
155 ldr_set_core_reader (ldr_read_memory);
156 ldr_xdetach (ldr_process);
157 ldr_xattach (ldr_process);
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. */
164 ldr_read_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int readstring
)
171 target_read_string (memaddr
, &buffer
, len
, &result
);
173 strcpy (myaddr
, buffer
);
177 result
= target_read_memory (memaddr
, myaddr
, len
);
184 #endif /* USE_LDR_ROUTINES */
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
191 lm_sec_cmp (const void *p1
, const void *p2
)
193 const struct lm_sec
*lms1
= p1
, *lms2
= p2
;
194 return strcmp (lms1
->name
, lms2
->name
);
197 /* Sort LMI->secs so that osf_relocate_section_addresses() can binary-search
201 lm_secs_sort (struct lm_info
*lmi
)
203 qsort (lmi
->secs
, lmi
->nsecs
, sizeof *lmi
->secs
, lm_sec_cmp
);
206 /* Populate name fields of LMI->secs. */
209 fetch_sec_names (struct lm_info
*lmi
)
211 #ifndef USE_LDR_ROUTINES
216 for (i
= 0; i
< lmi
->nsecs
; i
++)
219 target_read_string (lms
->nameaddr
, &name
, PATH_MAX
, &errcode
);
222 warning (_("unable to read shared sec name at 0x%lx"), lms
->nameaddr
);
231 /* target_so_ops callback. Adjust SEC's addresses after it's been mapped into
235 osf_relocate_section_addresses (struct so_list
*so
,
236 struct section_table
*sec
)
239 struct lm_sec lms_key
, *lms
;
241 /* Fetch SO's section names if we haven't done so already. */
243 if (lmi
->nsecs
&& !lmi
->secs
[0].name
)
244 fetch_sec_names (lmi
);
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
);
251 sec
->addr
+= lms
->offset
;
252 sec
->endaddr
+= lms
->offset
;
256 /* target_so_ops callback. Free parts of SO allocated by this file. */
259 osf_free_so (struct so_list
*so
)
264 for (i
= 0; i
< so
->lm_info
->nsecs
; i
++)
266 name
= so
->lm_info
->secs
[i
].name
;
268 xfree ((void *) name
);
273 /* target_so_ops callback. Discard information accumulated by this file and
274 not freed by osf_free_so(). */
277 osf_clear_solib (void)
282 /* target_so_ops callback. Prepare to handle shared libraries after the
283 inferior process has been created but before it's executed any
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
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
296 At that point we discover the names of all shared libraries and
297 read their symbols in.
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.
305 Also, what if child has exit()ed? Must exit loop somehow. */
308 osf_solib_create_inferior_hook (void)
310 struct inferior
*inf
;
311 struct thread_info
*tp
;
313 /* If we are attaching to the inferior, the shared libraries
314 have already been mapped, so nothing more to do. */
318 /* Nothing to do for statically bound executables. */
320 if (symfile_objfile
== NULL
321 || symfile_objfile
->obfd
== NULL
322 || ((bfd_get_file_flags (symfile_objfile
->obfd
) & DYNAMIC
) == 0))
325 /* Now run the target. It will eventually get a SIGTRAP, at
326 which point all of the libraries will have been mapped in and we
327 can go groveling around in the rld structures to find
328 out what we need to know about them.
330 If debugging from a core file, we cannot resume the execution
331 of the inferior. But this is actually not an issue, because
332 shared libraries have already been mapped anyways, which means
333 we have nothing more to do. */
334 if (!target_can_run (¤t_target
))
337 inf
= current_inferior ();
338 tp
= inferior_thread ();
339 clear_proceed_status ();
340 inf
->stop_soon
= STOP_QUIETLY
;
341 tp
->stop_signal
= TARGET_SIGNAL_0
;
344 target_resume (minus_one_ptid
, 0, tp
->stop_signal
);
345 wait_for_inferior (0);
347 while (tp
->stop_signal
!= TARGET_SIGNAL_TRAP
);
349 /* solib_add will call reinit_frame_cache.
350 But we are stopped in the runtime loader and we do not have symbols
351 for the runtime loader. So heuristic_proc_start will be called
352 and will put out an annoying warning.
353 Delaying the resetting of stop_soon until after symbol loading
354 suppresses the warning. */
355 solib_add ((char *) 0, 0, (struct target_ops
*) 0, auto_solib_add
);
356 inf
->stop_soon
= NO_STOP_QUIETLY
;
359 /* target_so_ops callback. Do additional symbol handling, lookup, etc. after
360 symbols for a shared object have been loaded. */
363 osf_special_symbol_handling (void)
368 /* Initialize CTXT in preparation for iterating through the inferior's module
369 list using read_map(). Return success. */
372 open_map (struct read_map_ctxt
*ctxt
)
374 #ifdef USE_LDR_ROUTINES
375 /* Note: As originally written, ldr_my_process() was used to obtain
376 the value for ctxt->proc. This is incorrect, however, since
377 ldr_my_process() retrieves the "unique identifier" associated
378 with the current process (i.e. GDB) and not the one being
379 debugged. Presumably, the pid of the process being debugged is
380 compatible with the "unique identifier" used by the ldr_
381 routines, so we use that. */
382 ctxt
->proc
= ptid_get_pid (inferior_ptid
);
383 if (ldr_xattach (ctxt
->proc
) != 0)
385 ctxt
->next
= LDR_NULL_MODULE
;
387 CORE_ADDR ldr_context_addr
, prev
, next
;
388 ldr_context_t ldr_context
;
390 if (target_read_memory ((CORE_ADDR
) RLD_CONTEXT_ADDRESS
,
391 (char *) &ldr_context_addr
,
392 sizeof (CORE_ADDR
)) != 0)
394 if (target_read_memory (ldr_context_addr
,
395 (char *) &ldr_context
,
396 sizeof (ldr_context_t
)) != 0)
398 ctxt
->next
= ldr_context
.head
;
399 ctxt
->tail
= ldr_context
.tail
;
404 /* Initialize SO to have module NAME, /sbin/loader indicator ISLOADR, and
405 space for NSECS sections. */
408 init_so (struct so_list
*so
, char *name
, int isloader
, int nsecs
)
412 /* solib.c requires various fields to be initialized to 0. */
413 memset (so
, 0, sizeof *so
);
416 namelen
= strlen (name
);
417 if (namelen
>= SO_NAME_MAX_PATH_SIZE
)
418 namelen
= SO_NAME_MAX_PATH_SIZE
- 1;
420 memcpy (so
->so_original_name
, name
, namelen
);
421 so
->so_original_name
[namelen
] = '\0';
422 memcpy (so
->so_name
, so
->so_original_name
, namelen
+ 1);
424 /* Allocate section space. */
425 so
->lm_info
= xmalloc ((unsigned) &(((struct lm_info
*)0)->secs
) +
426 nsecs
* sizeof *so
->lm_info
);
427 so
->lm_info
->isloader
= isloader
;
428 so
->lm_info
->nsecs
= nsecs
;
429 for (i
= 0; i
< nsecs
; i
++)
430 so
->lm_info
->secs
[i
].name
= NULL
;
433 /* Initialize SO's section SECIDX with name address NAMEADDR, name string
434 NAME, default virtual address VADDR, and actual virtual address
438 init_sec (struct so_list
*so
, int secidx
, CORE_ADDR nameaddr
,
439 const char *name
, CORE_ADDR vaddr
, CORE_ADDR mapaddr
)
443 lms
= so
->lm_info
->secs
+ secidx
;
444 lms
->nameaddr
= nameaddr
;
446 lms
->offset
= mapaddr
- vaddr
;
449 /* If there are more elements starting at CTXT in inferior's module list,
450 store the next element in SO, advance CTXT to the next element, and return
454 read_map (struct read_map_ctxt
*ctxt
, struct so_list
*so
)
456 ldr_module_info_t minf
;
457 ldr_region_info_t rinf
;
459 #ifdef USE_LDR_ROUTINES
463 /* Retrieve the next element. */
464 if (ldr_next_module (ctxt
->proc
, &ctxt
->next
) != 0)
466 if (ctxt
->next
== LDR_NULL_MODULE
)
468 if (ldr_inq_module (ctxt
->proc
, ctxt
->next
, &minf
, sizeof minf
, &size
) != 0)
471 /* Initialize the module name and section count. */
472 init_so (so
, minf
.lmi_name
, 0, minf
.lmi_nregion
);
474 /* Retrieve section names and offsets. */
475 for (i
= 0; i
< minf
.lmi_nregion
; i
++)
477 if (ldr_inq_region (ctxt
->proc
, ctxt
->next
, i
, &rinf
,
478 sizeof rinf
, &size
) != 0)
480 init_sec (so
, (int) i
, 0, xstrdup (rinf
.lri_name
),
481 (CORE_ADDR
) rinf
.lri_vaddr
, (CORE_ADDR
) rinf
.lri_mapaddr
);
483 lm_secs_sort (so
->lm_info
);
488 /* Retrieve the next element. */
491 if (target_read_memory (ctxt
->next
, (char *) &minf
, sizeof minf
) != 0)
493 if (ctxt
->next
== ctxt
->tail
)
496 ctxt
->next
= minf
.next
;
498 /* Initialize the module name and section count. */
499 target_read_string (minf
.module_name
, &name
, PATH_MAX
, &errcode
);
502 init_so (so
, name
, !minf
.modinfo_addr
, minf
.region_count
);
505 /* Retrieve section names and offsets. */
506 for (i
= 0; i
< minf
.region_count
; i
++)
508 if (target_read_memory (minf
.regioninfo_addr
+ i
* sizeof rinf
,
509 (char *) &rinf
, sizeof rinf
) != 0)
511 init_sec (so
, i
, rinf
.regionname_addr
, NULL
, rinf
.vaddr
, rinf
.mapaddr
);
513 #endif /* !USE_LDR_ROUTINES */
521 /* Free resources allocated by open_map (CTXT). */
524 close_map (struct read_map_ctxt
*ctxt
)
526 #ifdef USE_LDR_ROUTINES
527 ldr_xdetach (ctxt
->proc
);
531 /* target_so_ops callback. Return a list of shared objects currently loaded
534 static struct so_list
*
535 osf_current_sos (void)
537 struct so_list
*head
= NULL
, *tail
, *newtail
, so
;
538 struct read_map_ctxt ctxt
;
541 if (!open_map (&ctxt
))
544 /* Read subsequent elements. */
545 for (skipped_main
= 0;;)
547 if (!read_map (&ctxt
, &so
))
550 /* Skip the main program module, which is first in the list after
552 if (!so
.lm_info
->isloader
&& !skipped_main
)
559 newtail
= xmalloc (sizeof *newtail
);
563 tail
->next
= newtail
;
566 memcpy (tail
, &so
, sizeof so
);
574 /* target_so_ops callback. Attempt to locate and open the main symbol
578 osf_open_symbol_file_object (void *from_ttyp
)
580 struct read_map_ctxt ctxt
;
585 if (!query ("Attempt to reload symbols from process? "))
588 /* The first module after /sbin/loader is the main program. */
589 if (!open_map (&ctxt
))
591 for (found
= 0; !found
;)
593 if (!read_map (&ctxt
, &so
))
595 found
= !so
.lm_info
->isloader
;
601 symbol_file_add_main (so
.so_name
, *(int *) from_ttyp
);
605 /* target_so_ops callback. Return whether PC is in the dynamic linker. */
608 osf_in_dynsym_resolve_code (CORE_ADDR pc
)
610 /* This function currently always return False. This is a temporary
611 solution which only consequence is to introduce a minor incovenience
612 for the user: When stepping inside a subprogram located in a shared
613 library, gdb might stop inside the dynamic loader code instead of
614 inside the subprogram itself. See the explanations in infrun.c about
615 the in_solib_dynsym_resolve_code() function for more details. */
619 static struct target_so_ops osf_so_ops
;
622 _initialize_osf_solib (void)
624 osf_so_ops
.relocate_section_addresses
= osf_relocate_section_addresses
;
625 osf_so_ops
.free_so
= osf_free_so
;
626 osf_so_ops
.clear_solib
= osf_clear_solib
;
627 osf_so_ops
.solib_create_inferior_hook
= osf_solib_create_inferior_hook
;
628 osf_so_ops
.special_symbol_handling
= osf_special_symbol_handling
;
629 osf_so_ops
.current_sos
= osf_current_sos
;
630 osf_so_ops
.open_symbol_file_object
= osf_open_symbol_file_object
;
631 osf_so_ops
.in_dynsym_resolve_code
= osf_in_dynsym_resolve_code
;
633 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
634 current_target_so_ops
= &osf_so_ops
;