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