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