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