No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / solib-osf.c
blob7a3e62535363b14aacc66ba68d8113833bef4be1
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
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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
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).
29 Under OSF/1 there are two possibilities to get at this information:
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.
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.
43 We will use the peeking approach until libxproc.a works for other
44 processes. */
46 #include "defs.h"
48 #include <sys/types.h>
49 #include <signal.h>
50 #include "gdb_string.h"
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"
60 #ifdef USE_LDR_ROUTINES
61 # include <loader.h>
62 #endif
64 #ifndef USE_LDR_ROUTINES
65 /* Definition of runtime loader structures, found by experimentation. */
66 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
68 /* Per-module information structure referenced by ldr_context_t.head. */
70 typedef struct
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;
84 ldr_module_info_t;
86 /* Per-region structure referenced by ldr_module_info_t.regioninfo_addr. */
88 typedef struct
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];
98 ldr_region_info_t;
100 /* Structure at RLD_CONTEXT_ADDRESS specifying the start and finish addresses
101 of the shared module list. */
103 typedef struct
105 CORE_ADDR unknown1;
106 CORE_ADDR unknown2;
107 CORE_ADDR head;
108 CORE_ADDR tail;
110 ldr_context_t;
111 #endif /* !USE_LDR_ROUTINES */
113 /* Per-section information, stored in struct lm_info.secs. */
115 struct lm_sec
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 */
123 /* Per-module information, stored in struct so_list.lm_info. */
125 struct lm_info
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 */
133 /* Context for iterating through the inferior's shared module list. */
135 struct read_map_ctxt
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
146 /* Forward declaration for this module's autoinit function. */
148 extern void _initialize_osf_solib (void);
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:
155 ldr_process = ldr_core_process ();
156 ldr_set_core_reader (ldr_read_memory);
157 ldr_xdetach (ldr_process);
158 ldr_xattach (ldr_process);
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. */
164 static int
165 ldr_read_memory (CORE_ADDR memaddr, char *myaddr, int len, int readstring)
167 int result;
168 char *buffer;
170 if (readstring)
172 target_read_string (memaddr, &buffer, len, &result);
173 if (result == 0)
174 strcpy (myaddr, buffer);
175 xfree (buffer);
177 else
178 result = target_read_memory (memaddr, myaddr, len);
180 if (result != 0)
181 result = -result;
182 return result;
184 # endif /* 0 */
185 #endif /* USE_LDR_ROUTINES */
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. */
191 static int
192 lm_sec_cmp (const void *p1, const void *p2)
194 const struct lm_sec *lms1 = p1, *lms2 = p2;
195 return strcmp (lms1->name, lms2->name);
198 /* Sort LMI->secs so that osf_relocate_section_addresses() can binary-search
199 it. */
201 static void
202 lm_secs_sort (struct lm_info *lmi)
204 qsort (lmi->secs, lmi->nsecs, sizeof *lmi->secs, lm_sec_cmp);
207 /* Populate name fields of LMI->secs. */
209 static void
210 fetch_sec_names (struct lm_info *lmi)
212 #ifndef USE_LDR_ROUTINES
213 int i, errcode;
214 struct lm_sec *lms;
215 char *name;
217 for (i = 0; i < lmi->nsecs; i++)
219 lms = lmi->secs + i;
220 target_read_string (lms->nameaddr, &name, PATH_MAX, &errcode);
221 if (errcode != 0)
223 warning (_("unable to read shared sec name at 0x%lx"), lms->nameaddr);
224 name = xstrdup ("");
226 lms->name = name;
228 lm_secs_sort (lmi);
229 #endif
232 /* target_so_ops callback. Adjust SEC's addresses after it's been mapped into
233 the process. */
235 static void
236 osf_relocate_section_addresses (struct so_list *so,
237 struct section_table *sec)
239 struct lm_info *lmi;
240 struct lm_sec lms_key, *lms;
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);
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)
252 sec->addr += lms->offset;
253 sec->endaddr += lms->offset;
257 /* target_so_ops callback. Free parts of SO allocated by this file. */
259 static void
260 osf_free_so (struct so_list *so)
262 int i;
263 const char *name;
265 for (i = 0; i < so->lm_info->nsecs; i++)
267 name = so->lm_info->secs[i].name;
268 if (name)
269 xfree ((void *) name);
271 xfree (so->lm_info);
274 /* target_so_ops callback. Discard information accumulated by this file and
275 not freed by osf_free_so(). */
277 static void
278 osf_clear_solib (void)
280 return;
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.
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.
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.
297 At that point we discover the names of all shared libraries and
298 read their symbols in.
300 FIXME
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.
306 Also, what if child has exit()ed? Must exit loop somehow. */
308 static void
309 osf_solib_create_inferior_hook (void)
311 /* Nothing to do for statically bound executables. */
313 if (symfile_objfile == NULL
314 || symfile_objfile->obfd == NULL
315 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
316 return;
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. */
323 clear_proceed_status ();
324 stop_soon = STOP_QUIETLY;
325 stop_signal = TARGET_SIGNAL_0;
328 target_resume (minus_one_ptid, 0, stop_signal);
329 wait_for_inferior ();
331 while (stop_signal != TARGET_SIGNAL_TRAP);
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 until after symbol loading
338 suppresses the warning. */
339 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
340 stop_soon = NO_STOP_QUIETLY;
342 /* Enable breakpoints disabled (unnecessarily) by clear_solib(). */
343 re_enable_breakpoints_in_shlibs ();
346 /* target_so_ops callback. Do additional symbol handling, lookup, etc. after
347 symbols for a shared object have been loaded. */
349 static void
350 osf_special_symbol_handling (void)
352 return;
355 /* Initialize CTXT in preparation for iterating through the inferior's module
356 list using read_map(). Return success. */
358 static int
359 open_map (struct read_map_ctxt *ctxt)
361 #ifdef USE_LDR_ROUTINES
362 /* Note: As originally written, ldr_my_process() was used to obtain
363 the value for ctxt->proc. This is incorrect, however, since
364 ldr_my_process() retrieves the "unique identifier" associated
365 with the current process (i.e. GDB) and not the one being
366 debugged. Presumably, the pid of the process being debugged is
367 compatible with the "unique identifier" used by the ldr_
368 routines, so we use that. */
369 ctxt->proc = ptid_get_pid (inferior_ptid);
370 if (ldr_xattach (ctxt->proc) != 0)
371 return 0;
372 ctxt->next = LDR_NULL_MODULE;
373 #else
374 CORE_ADDR ldr_context_addr, prev, next;
375 ldr_context_t ldr_context;
377 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
378 (char *) &ldr_context_addr,
379 sizeof (CORE_ADDR)) != 0)
380 return 0;
381 if (target_read_memory (ldr_context_addr,
382 (char *) &ldr_context,
383 sizeof (ldr_context_t)) != 0)
384 return 0;
385 ctxt->next = ldr_context.head;
386 ctxt->tail = ldr_context.tail;
387 #endif
388 return 1;
391 /* Initialize SO to have module NAME, /sbin/loader indicator ISLOADR, and
392 space for NSECS sections. */
394 static void
395 init_so (struct so_list *so, char *name, int isloader, int nsecs)
397 int namelen, i;
399 /* solib.c requires various fields to be initialized to 0. */
400 memset (so, 0, sizeof *so);
402 /* Copy the name. */
403 namelen = strlen (name);
404 if (namelen >= SO_NAME_MAX_PATH_SIZE)
405 namelen = SO_NAME_MAX_PATH_SIZE - 1;
407 memcpy (so->so_original_name, name, namelen);
408 so->so_original_name[namelen] = '\0';
409 memcpy (so->so_name, so->so_original_name, namelen + 1);
411 /* Allocate section space. */
412 so->lm_info = xmalloc ((unsigned) &(((struct lm_info *)0)->secs) +
413 nsecs * sizeof *so->lm_info);
414 so->lm_info->isloader = isloader;
415 so->lm_info->nsecs = nsecs;
416 for (i = 0; i < nsecs; i++)
417 so->lm_info->secs[i].name = NULL;
420 /* Initialize SO's section SECIDX with name address NAMEADDR, name string
421 NAME, default virtual address VADDR, and actual virtual address
422 MAPADDR. */
424 static void
425 init_sec (struct so_list *so, int secidx, CORE_ADDR nameaddr,
426 const char *name, CORE_ADDR vaddr, CORE_ADDR mapaddr)
428 struct lm_sec *lms;
430 lms = so->lm_info->secs + secidx;
431 lms->nameaddr = nameaddr;
432 lms->name = name;
433 lms->offset = mapaddr - vaddr;
436 /* If there are more elements starting at CTXT in inferior's module list,
437 store the next element in SO, advance CTXT to the next element, and return
438 1, else return 0. */
440 static int
441 read_map (struct read_map_ctxt *ctxt, struct so_list *so)
443 ldr_module_info_t minf;
444 ldr_region_info_t rinf;
446 #ifdef USE_LDR_ROUTINES
447 size_t size;
448 ldr_region_t i;
450 /* Retrieve the next element. */
451 if (ldr_next_module (ctxt->proc, &ctxt->next) != 0)
452 return 0;
453 if (ctxt->next == LDR_NULL_MODULE)
454 return 0;
455 if (ldr_inq_module (ctxt->proc, ctxt->next, &minf, sizeof minf, &size) != 0)
456 return 0;
458 /* Initialize the module name and section count. */
459 init_so (so, minf.lmi_name, 0, minf.lmi_nregion);
461 /* Retrieve section names and offsets. */
462 for (i = 0; i < minf.lmi_nregion; i++)
464 if (ldr_inq_region (ctxt->proc, ctxt->next, i, &rinf,
465 sizeof rinf, &size) != 0)
466 goto err;
467 init_sec (so, (int) i, 0, xstrdup (rinf.lri_name),
468 (CORE_ADDR) rinf.lri_vaddr, (CORE_ADDR) rinf.lri_mapaddr);
470 lm_secs_sort (so->lm_info);
471 #else
472 char *name;
473 int errcode, i;
475 /* Retrieve the next element. */
476 if (!ctxt->next)
477 return 0;
478 if (target_read_memory (ctxt->next, (char *) &minf, sizeof minf) != 0)
479 return 0;
480 if (ctxt->next == ctxt->tail)
481 ctxt->next = 0;
482 else
483 ctxt->next = minf.next;
485 /* Initialize the module name and section count. */
486 target_read_string (minf.module_name, &name, PATH_MAX, &errcode);
487 if (errcode != 0)
488 return 0;
489 init_so (so, name, !minf.modinfo_addr, minf.region_count);
490 xfree (name);
492 /* Retrieve section names and offsets. */
493 for (i = 0; i < minf.region_count; i++)
495 if (target_read_memory (minf.regioninfo_addr + i * sizeof rinf,
496 (char *) &rinf, sizeof rinf) != 0)
497 goto err;
498 init_sec (so, i, rinf.regionname_addr, NULL, rinf.vaddr, rinf.mapaddr);
500 #endif /* !USE_LDR_ROUTINES */
501 return 1;
503 err:
504 osf_free_so (so);
505 return 0;
508 /* Free resources allocated by open_map (CTXT). */
510 static void
511 close_map (struct read_map_ctxt *ctxt)
513 #ifdef USE_LDR_ROUTINES
514 ldr_xdetach (ctxt->proc);
515 #endif
518 /* target_so_ops callback. Return a list of shared objects currently loaded
519 in the inferior. */
521 static struct so_list *
522 osf_current_sos (void)
524 struct so_list *head = NULL, *tail, *newtail, so;
525 struct read_map_ctxt ctxt;
526 int skipped_main;
528 if (!open_map (&ctxt))
529 return NULL;
531 /* Read subsequent elements. */
532 for (skipped_main = 0;;)
534 if (!read_map (&ctxt, &so))
535 break;
537 /* Skip the main program module, which is first in the list after
538 /sbin/loader. */
539 if (!so.lm_info->isloader && !skipped_main)
541 osf_free_so (&so);
542 skipped_main = 1;
543 continue;
546 newtail = xmalloc (sizeof *newtail);
547 if (!head)
548 head = newtail;
549 else
550 tail->next = newtail;
551 tail = newtail;
553 memcpy (tail, &so, sizeof so);
554 tail->next = NULL;
557 close_map (&ctxt);
558 return head;
561 /* target_so_ops callback. Attempt to locate and open the main symbol
562 file. */
564 static int
565 osf_open_symbol_file_object (void *from_ttyp)
567 struct read_map_ctxt ctxt;
568 struct so_list so;
569 int found;
571 if (symfile_objfile)
572 if (!query ("Attempt to reload symbols from process? "))
573 return 0;
575 /* The first module after /sbin/loader is the main program. */
576 if (!open_map (&ctxt))
577 return 0;
578 for (found = 0; !found;)
580 if (!read_map (&ctxt, &so))
581 break;
582 found = !so.lm_info->isloader;
583 osf_free_so (&so);
585 close_map (&ctxt);
587 if (found)
588 symbol_file_add_main (so.so_name, *(int *) from_ttyp);
589 return found;
592 /* target_so_ops callback. Return whether PC is in the dynamic linker. */
594 static int
595 osf_in_dynsym_resolve_code (CORE_ADDR pc)
597 /* This function currently always return False. This is a temporary
598 solution which only consequence is to introduce a minor incovenience
599 for the user: When stepping inside a subprogram located in a shared
600 library, gdb might stop inside the dynamic loader code instead of
601 inside the subprogram itself. See the explanations in infrun.c about
602 the IN_SOLIB_DYNSYM_RESOLVE_CODE macro for more details. */
603 return 0;
606 static struct target_so_ops osf_so_ops;
608 void
609 _initialize_osf_solib (void)
611 osf_so_ops.relocate_section_addresses = osf_relocate_section_addresses;
612 osf_so_ops.free_so = osf_free_so;
613 osf_so_ops.clear_solib = osf_clear_solib;
614 osf_so_ops.solib_create_inferior_hook = osf_solib_create_inferior_hook;
615 osf_so_ops.special_symbol_handling = osf_special_symbol_handling;
616 osf_so_ops.current_sos = osf_current_sos;
617 osf_so_ops.open_symbol_file_object = osf_open_symbol_file_object;
618 osf_so_ops.in_dynsym_resolve_code = osf_in_dynsym_resolve_code;
620 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
621 current_target_so_ops = &osf_so_ops;