* d10v-dis.c: Fix formatting.
[deliverable/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.
3 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23/* When handling shared libraries, GDB has to find out the pathnames
24 of all shared libraries that are currently loaded (to read in their
25 symbols) and where the shared libraries are loaded in memory
26 (to relocate them properly from their prelinked addresses to the
27 current load address).
28
29 Under OSF/1 there are two possibilities to get at this information:
30
31 1) Peek around in the runtime loader structures.
32 These are not documented, and they are not defined in the system
33 header files. The definitions below were obtained by experimentation,
34 but they seem stable enough.
35
36 2) Use the libxproc.a library, which contains the equivalent ldr_*
37 routines. The library is documented in Tru64 5.x, but as of 5.1, it
38 only allows a process to examine itself. On earlier versions, it
39 may require that the GDB executable be dynamically linked and that
40 NAT_CLIBS include -lxproc -Wl,-expect_unresolved,ldr_process_context
41 for GDB and all applications that are using libgdb.
42
43 We will use the peeking approach until libxproc.a works for other
44 processes. */
45
46#include "defs.h"
47
48#include <sys/types.h>
49#include <signal.h>
50#include "gdb_string.h"
51
52#include "bfd.h"
53#include "symtab.h"
54#include "symfile.h"
55#include "objfiles.h"
56#include "target.h"
57#include "inferior.h"
58#include "solist.h"
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;
195 return strcmp (lms1->name, lms2->name);
196}
197
198/* Sort LMI->secs so that osf_relocate_section_addresses() can binary-search
199 it. */
200
201static void
202lm_secs_sort (struct lm_info *lmi)
203{
204 qsort (lmi->secs, lmi->nsecs, sizeof *lmi->secs, lm_sec_cmp);
205}
206
207/* Populate name fields of LMI->secs. */
208
209static void
210fetch_sec_names (struct lm_info *lmi)
211{
212#ifndef USE_LDR_ROUTINES
213 int i, errcode;
214 struct lm_sec *lms;
215 char *name;
216
217 for (i = 0; i < lmi->nsecs; i++)
218 {
219 lms = lmi->secs + i;
220 target_read_string (lms->nameaddr, &name, PATH_MAX, &errcode);
221 if (errcode != 0)
222 {
223 warning ("unable to read shared sec name at 0x%lx", lms->nameaddr);
224 name = xstrdup ("");
225 }
226 lms->name = name;
227 }
228 lm_secs_sort (lmi);
229#endif
230}
231
232/* target_so_ops callback. Adjust SEC's addresses after it's been mapped into
233 the process. */
234
235static void
236osf_relocate_section_addresses (struct so_list *so,
237 struct section_table *sec)
238{
239 struct lm_info *lmi;
240 struct lm_sec lms_key, *lms;
241
242 /* Fetch SO's section names if we haven't done so already. */
243 lmi = so->lm_info;
244 if (lmi->nsecs && !lmi->secs[0].name)
245 fetch_sec_names (lmi);
246
247 /* Binary-search for offset information corresponding to SEC. */
248 lms_key.name = sec->the_bfd_section->name;
249 lms = bsearch (&lms_key, lmi->secs, lmi->nsecs, sizeof *lms, lm_sec_cmp);
250 if (lms)
251 {
252 sec->addr += lms->offset;
253 sec->endaddr += lms->offset;
254 }
255}
256
257/* target_so_ops callback. Free parts of SO allocated by this file. */
258
259static void
260osf_free_so (struct so_list *so)
261{
262 int i;
263 const char *name;
264
265 for (i = 0; i < so->lm_info->nsecs; i++)
266 {
267 name = so->lm_info->secs[i].name;
268 if (name)
269 xfree ((void *) name);
270 }
271 xfree (so->lm_info);
272}
273
274/* target_so_ops callback. Discard information accumulated by this file and
275 not freed by osf_free_so(). */
276
277static void
278osf_clear_solib (void)
279{
280 return;
281}
282
283/* target_so_ops callback. Prepare to handle shared libraries after the
284 inferior process has been created but before it's executed any
285 instructions.
286
287 For a statically bound executable, the inferior's first instruction is the
288 one at "_start", or a similar text label. No further processing is needed
289 in that case.
290
291 For a dynamically bound executable, this first instruction is somewhere
292 in the rld, and the actual user executable is not yet mapped in.
293 We continue the inferior again, rld then maps in the actual user
294 executable and any needed shared libraries and then sends
295 itself a SIGTRAP.
296
297 At that point we discover the names of all shared libraries and
298 read their symbols in.
299
300 FIXME
301
302 This code does not properly handle hitting breakpoints which the
303 user might have set in the rld itself. Proper handling would have
304 to check if the SIGTRAP happened due to a kill call.
305
306 Also, what if child has exit()ed? Must exit loop somehow. */
307
308static void
309osf_solib_create_inferior_hook (void)
310{
311 /* Nothing to do for statically bound executables. */
312
313 if (symfile_objfile == NULL
314 || symfile_objfile->obfd == NULL
315 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
316 return;
317
318 /* Now run the target. It will eventually get a SIGTRAP, at
319 which point all of the libraries will have been mapped in and we
320 can go groveling around in the rld structures to find
321 out what we need to know about them. */
322
323 clear_proceed_status ();
324 stop_soon_quietly = 1;
325 stop_signal = TARGET_SIGNAL_0;
326 do
327 {
328 target_resume (minus_one_ptid, 0, stop_signal);
329 wait_for_inferior ();
330 }
331 while (stop_signal != TARGET_SIGNAL_TRAP);
332
333 /* solib_add will call reinit_frame_cache.
334 But we are stopped in the runtime loader and we do not have symbols
335 for the runtime loader. So heuristic_proc_start will be called
336 and will put out an annoying warning.
337 Delaying the resetting of stop_soon_quietly until after symbol loading
338 suppresses the warning. */
339 if (auto_solib_add)
340 solib_add ((char *) 0, 0, (struct target_ops *) 0);
341 stop_soon_quietly = 0;
342
343 /* Enable breakpoints disabled (unnecessarily) by clear_solib(). */
344 re_enable_breakpoints_in_shlibs ();
345}
346
347/* target_so_ops callback. Do additional symbol handling, lookup, etc. after
348 symbols for a shared object have been loaded. */
349
350static void
351osf_special_symbol_handling (void)
352{
353 return;
354}
355
356/* Initialize CTXT in preparation for iterating through the inferior's module
357 list using read_map(). Return success. */
358
359static int
360open_map (struct read_map_ctxt *ctxt)
361{
362#ifdef USE_LDR_ROUTINES
363 ctxt->proc = ldr_my_process ();
364 if (ldr_xattach (ctxt->proc) != 0)
365 return 0;
366 ctxt->next = LDR_NULL_MODULE;
367#else
368 CORE_ADDR ldr_context_addr, prev, next;
369 ldr_context_t ldr_context;
370
371 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
372 (char *) &ldr_context_addr,
373 sizeof (CORE_ADDR)) != 0)
374 return 0;
375 if (target_read_memory (ldr_context_addr,
376 (char *) &ldr_context,
377 sizeof (ldr_context_t)) != 0)
378 return 0;
379 ctxt->next = ldr_context.head;
380 ctxt->tail = ldr_context.tail;
381#endif
382 return 1;
383}
384
385/* Initialize SO to have module NAME, /sbin/loader indicator ISLOADR, and
386 space for NSECS sections. */
387
388static void
389init_so (struct so_list *so, char *name, int isloader, int nsecs)
390{
391 int namelen, i;
392
393 /* solib.c requires various fields to be initialized to 0. */
394 memset (so, 0, sizeof *so);
395
396 /* Copy the name. */
397 namelen = strlen (name);
398 if (namelen >= SO_NAME_MAX_PATH_SIZE)
399 namelen = SO_NAME_MAX_PATH_SIZE - 1;
400
401 memcpy (so->so_original_name, name, namelen);
402 so->so_original_name[namelen] = '\0';
403 memcpy (so->so_name, so->so_original_name, namelen + 1);
404
405 /* Allocate section space. */
406 so->lm_info = xmalloc ((unsigned) &(((struct lm_info *)0)->secs) +
407 nsecs * sizeof *so->lm_info);
408 so->lm_info->isloader = isloader;
409 so->lm_info->nsecs = nsecs;
410 for (i = 0; i < nsecs; i++)
411 so->lm_info->secs[i].name = NULL;
412}
413
414/* Initialize SO's section SECIDX with name address NAMEADDR, name string
415 NAME, default virtual address VADDR, and actual virtual address
416 MAPADDR. */
417
418static void
419init_sec (struct so_list *so, int secidx, CORE_ADDR nameaddr,
420 const char *name, CORE_ADDR vaddr, CORE_ADDR mapaddr)
421{
422 struct lm_sec *lms;
423
424 lms = so->lm_info->secs + secidx;
425 lms->nameaddr = nameaddr;
426 lms->name = name;
427 lms->offset = mapaddr - vaddr;
428}
429
430/* If there are more elements starting at CTXT in inferior's module list,
431 store the next element in SO, advance CTXT to the next element, and return
432 1, else return 0. */
433
434static int
435read_map (struct read_map_ctxt *ctxt, struct so_list *so)
436{
437 ldr_module_info_t minf;
438 ldr_region_info_t rinf;
439
440#ifdef USE_LDR_ROUTINES
441 size_t size;
442 ldr_region_t i;
443
444 /* Retrieve the next element. */
445 if (ldr_next_module (ctxt->proc, &ctxt->next) != 0)
446 return 0;
447 if (ctxt->next == LDR_NULL_MODULE)
448 return 0;
449 if (ldr_inq_module (ctxt->proc, ctxt->next, &minf, sizeof minf, &size) != 0)
450 return 0;
451
452 /* Initialize the module name and section count. */
453 init_so (so, minf.lmi_name, 0, minf.lmi_nregion);
454
455 /* Retrieve section names and offsets. */
456 for (i = 0; i < minf.lmi_nregion; i++)
457 {
458 if (ldr_inq_region (ctxt->proc, ctxt->next, i, &rinf,
459 sizeof rinf, &size) != 0)
460 goto err;
461 init_sec (so, (int) i, 0, xstrdup (rinf.lri_name),
462 (CORE_ADDR) rinf.lri_vaddr, (CORE_ADDR) rinf.lri_mapaddr);
463 }
464 lm_secs_sort (so->lm_info);
465#else
466 char *name;
467 int errcode, i;
468
469 /* Retrieve the next element. */
470 if (!ctxt->next)
471 return 0;
472 if (target_read_memory (ctxt->next, (char *) &minf, sizeof minf) != 0)
473 return 0;
474 if (ctxt->next == ctxt->tail)
475 ctxt->next = 0;
476 else
477 ctxt->next = minf.next;
478
479 /* Initialize the module name and section count. */
480 target_read_string (minf.module_name, &name, PATH_MAX, &errcode);
481 if (errcode != 0)
482 return 0;
483 init_so (so, name, !minf.modinfo_addr, minf.region_count);
484 xfree (name);
485
486 /* Retrieve section names and offsets. */
487 for (i = 0; i < minf.region_count; i++)
488 {
489 if (target_read_memory (minf.regioninfo_addr + i * sizeof rinf,
490 (char *) &rinf, sizeof rinf) != 0)
491 goto err;
492 init_sec (so, i, rinf.regionname_addr, NULL, rinf.vaddr, rinf.mapaddr);
493 }
494#endif /* !USE_LDR_ROUTINES */
495 return 1;
496
497 err:
498 osf_free_so (so);
499 return 0;
500}
501
502/* Free resources allocated by open_map (CTXT). */
503
504static void
505close_map (struct read_map_ctxt *ctxt)
506{
507#ifdef USE_LDR_ROUTINES
508 ldr_xdetach (ctxt->proc);
509#endif
510}
511
512/* target_so_ops callback. Return a list of shared objects currently loaded
513 in the inferior. */
514
515static struct so_list *
516osf_current_sos (void)
517{
518 struct so_list *head = NULL, *tail, *newtail, so;
519 struct read_map_ctxt ctxt;
520 int skipped_main;
521
522 if (!open_map (&ctxt))
523 return NULL;
524
525 /* Read subsequent elements. */
526 for (skipped_main = 0;;)
527 {
528 if (!read_map (&ctxt, &so))
529 break;
530
531 /* Skip the main program module, which is first in the list after
532 /sbin/loader. */
533 if (!so.lm_info->isloader && !skipped_main)
534 {
535 osf_free_so (&so);
536 skipped_main = 1;
537 continue;
538 }
539
540 newtail = xmalloc (sizeof *newtail);
541 if (!head)
542 head = newtail;
543 else
544 tail->next = newtail;
545 tail = newtail;
546
547 memcpy (tail, &so, sizeof so);
548 tail->next = NULL;
549 }
550
551 done:
552 close_map (&ctxt);
553 return head;
554}
555
556/* target_so_ops callback. Attempt to locate and open the main symbol
557 file. */
558
559static int
560osf_open_symbol_file_object (void *from_ttyp)
561{
562 struct read_map_ctxt ctxt;
563 struct so_list so;
564 int found;
565
566 if (symfile_objfile)
567 if (!query ("Attempt to reload symbols from process? "))
568 return 0;
569
570 /* The first module after /sbin/loader is the main program. */
571 if (!open_map (&ctxt))
572 return 0;
573 for (found = 0; !found;)
574 {
575 if (!read_map (&ctxt, &so))
576 break;
577 found = !so.lm_info->isloader;
578 osf_free_so (&so);
579 }
580 close_map (&ctxt);
581
582 if (found)
583 symbol_file_add_main (so.so_name, *(int *) from_ttyp);
584 return found;
585}
586
587/* target_so_ops callback. Return whether PC is in the dynamic linker. */
588
589static int
590osf_in_dynsym_resolve_code (CORE_ADDR pc)
591{
b184b287
JB
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. */
a1cd1908
ND
598 return 0;
599}
600
601static struct target_so_ops osf_so_ops;
602
603void
604_initialize_osf_solib (void)
605{
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;
614
615 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
616 current_target_so_ops = &osf_so_ops;
617}
This page took 0.07656 seconds and 4 git commands to generate.