* inferior.h (read_pc, write_pc): Remove.
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23
24 #include "elf/external.h"
25 #include "elf/common.h"
26 #include "elf/mips.h"
27
28 #include "symtab.h"
29 #include "bfd.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "gdbcore.h"
33 #include "target.h"
34 #include "inferior.h"
35 #include "regcache.h"
36 #include "gdbthread.h"
37
38 #include "gdb_assert.h"
39
40 #include "solist.h"
41 #include "solib.h"
42 #include "solib-svr4.h"
43
44 #include "bfd-target.h"
45 #include "elf-bfd.h"
46 #include "exec.h"
47 #include "auxv.h"
48 #include "exceptions.h"
49
50 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
51 static int svr4_have_link_map_offsets (void);
52
53 /* Link map info to include in an allocated so_list entry */
54
55 struct lm_info
56 {
57 /* Pointer to copy of link map from inferior. The type is char *
58 rather than void *, so that we may use byte offsets to find the
59 various fields without the need for a cast. */
60 gdb_byte *lm;
61
62 /* Amount by which addresses in the binary should be relocated to
63 match the inferior. This could most often be taken directly
64 from lm, but when prelinking is involved and the prelink base
65 address changes, we may need a different offset, we want to
66 warn about the difference and compute it only once. */
67 CORE_ADDR l_addr;
68
69 /* The target location of lm. */
70 CORE_ADDR lm_addr;
71 };
72
73 /* On SVR4 systems, a list of symbols in the dynamic linker where
74 GDB can try to place a breakpoint to monitor shared library
75 events.
76
77 If none of these symbols are found, or other errors occur, then
78 SVR4 systems will fall back to using a symbol as the "startup
79 mapping complete" breakpoint address. */
80
81 static char *solib_break_names[] =
82 {
83 "r_debug_state",
84 "_r_debug_state",
85 "_dl_debug_state",
86 "rtld_db_dlactivity",
87 "_rtld_debug_state",
88
89 NULL
90 };
91
92 static char *bkpt_names[] =
93 {
94 "_start",
95 "__start",
96 "main",
97 NULL
98 };
99
100 static char *main_name_list[] =
101 {
102 "main_$main",
103 NULL
104 };
105
106 /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
107 the same shared library. */
108
109 static int
110 svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
111 {
112 if (strcmp (gdb_so_name, inferior_so_name) == 0)
113 return 1;
114
115 /* On Solaris, when starting inferior we think that dynamic linker is
116 /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
117 contains /lib/ld.so.1. Sometimes one file is a link to another, but
118 sometimes they have identical content, but are not linked to each
119 other. We don't restrict this check for Solaris, but the chances
120 of running into this situation elsewhere are very low. */
121 if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
122 && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
123 return 1;
124
125 /* Similarly, we observed the same issue with sparc64, but with
126 different locations. */
127 if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
128 && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
129 return 1;
130
131 return 0;
132 }
133
134 static int
135 svr4_same (struct so_list *gdb, struct so_list *inferior)
136 {
137 return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
138 }
139
140 /* link map access functions */
141
142 static CORE_ADDR
143 LM_ADDR_FROM_LINK_MAP (struct so_list *so)
144 {
145 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
146 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
147
148 return extract_typed_address (so->lm_info->lm + lmo->l_addr_offset,
149 ptr_type);
150 }
151
152 static int
153 HAS_LM_DYNAMIC_FROM_LINK_MAP (void)
154 {
155 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
156
157 return lmo->l_ld_offset >= 0;
158 }
159
160 static CORE_ADDR
161 LM_DYNAMIC_FROM_LINK_MAP (struct so_list *so)
162 {
163 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
164 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
165
166 return extract_typed_address (so->lm_info->lm + lmo->l_ld_offset,
167 ptr_type);
168 }
169
170 static CORE_ADDR
171 LM_ADDR_CHECK (struct so_list *so, bfd *abfd)
172 {
173 if (so->lm_info->l_addr == (CORE_ADDR)-1)
174 {
175 struct bfd_section *dyninfo_sect;
176 CORE_ADDR l_addr, l_dynaddr, dynaddr, align = 0x1000;
177
178 l_addr = LM_ADDR_FROM_LINK_MAP (so);
179
180 if (! abfd || ! HAS_LM_DYNAMIC_FROM_LINK_MAP ())
181 goto set_addr;
182
183 l_dynaddr = LM_DYNAMIC_FROM_LINK_MAP (so);
184
185 dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
186 if (dyninfo_sect == NULL)
187 goto set_addr;
188
189 dynaddr = bfd_section_vma (abfd, dyninfo_sect);
190
191 if (dynaddr + l_addr != l_dynaddr)
192 {
193 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
194 {
195 Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
196 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
197 int i;
198
199 align = 1;
200
201 for (i = 0; i < ehdr->e_phnum; i++)
202 if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
203 align = phdr[i].p_align;
204 }
205
206 /* Turn it into a mask. */
207 align--;
208
209 /* If the changes match the alignment requirements, we
210 assume we're using a core file that was generated by the
211 same binary, just prelinked with a different base offset.
212 If it doesn't match, we may have a different binary, the
213 same binary with the dynamic table loaded at an unrelated
214 location, or anything, really. To avoid regressions,
215 don't adjust the base offset in the latter case, although
216 odds are that, if things really changed, debugging won't
217 quite work. */
218 if ((l_addr & align) == ((l_dynaddr - dynaddr) & align))
219 {
220 l_addr = l_dynaddr - dynaddr;
221
222 warning (_(".dynamic section for \"%s\" "
223 "is not at the expected address"), so->so_name);
224 warning (_("difference appears to be caused by prelink, "
225 "adjusting expectations"));
226 }
227 else
228 warning (_(".dynamic section for \"%s\" "
229 "is not at the expected address "
230 "(wrong library or version mismatch?)"), so->so_name);
231 }
232
233 set_addr:
234 so->lm_info->l_addr = l_addr;
235 }
236
237 return so->lm_info->l_addr;
238 }
239
240 static CORE_ADDR
241 LM_NEXT (struct so_list *so)
242 {
243 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
244 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
245
246 return extract_typed_address (so->lm_info->lm + lmo->l_next_offset,
247 ptr_type);
248 }
249
250 static CORE_ADDR
251 LM_NAME (struct so_list *so)
252 {
253 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
254 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
255
256 return extract_typed_address (so->lm_info->lm + lmo->l_name_offset,
257 ptr_type);
258 }
259
260 static int
261 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
262 {
263 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
264 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
265
266 /* Assume that everything is a library if the dynamic loader was loaded
267 late by a static executable. */
268 if (bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL)
269 return 0;
270
271 return extract_typed_address (so->lm_info->lm + lmo->l_prev_offset,
272 ptr_type) == 0;
273 }
274
275 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
276
277 /* Validity flag for debug_loader_offset. */
278 static int debug_loader_offset_p;
279
280 /* Load address for the dynamic linker, inferred. */
281 static CORE_ADDR debug_loader_offset;
282
283 /* Name of the dynamic linker, valid if debug_loader_offset_p. */
284 static char *debug_loader_name;
285
286 /* Load map address for the main executable. */
287 static CORE_ADDR main_lm_addr;
288
289 /* Local function prototypes */
290
291 static int match_main (char *);
292
293 static CORE_ADDR bfd_lookup_symbol (bfd *, char *);
294
295 /*
296
297 LOCAL FUNCTION
298
299 bfd_lookup_symbol -- lookup the value for a specific symbol
300
301 SYNOPSIS
302
303 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
304
305 DESCRIPTION
306
307 An expensive way to lookup the value of a single symbol for
308 bfd's that are only temporary anyway. This is used by the
309 shared library support to find the address of the debugger
310 notification routine in the shared library.
311
312 The returned symbol may be in a code or data section; functions
313 will normally be in a code section, but may be in a data section
314 if this architecture uses function descriptors.
315
316 Note that 0 is specifically allowed as an error return (no
317 such symbol).
318 */
319
320 static CORE_ADDR
321 bfd_lookup_symbol (bfd *abfd, char *symname)
322 {
323 long storage_needed;
324 asymbol *sym;
325 asymbol **symbol_table;
326 unsigned int number_of_symbols;
327 unsigned int i;
328 struct cleanup *back_to;
329 CORE_ADDR symaddr = 0;
330
331 storage_needed = bfd_get_symtab_upper_bound (abfd);
332
333 if (storage_needed > 0)
334 {
335 symbol_table = (asymbol **) xmalloc (storage_needed);
336 back_to = make_cleanup (xfree, symbol_table);
337 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
338
339 for (i = 0; i < number_of_symbols; i++)
340 {
341 sym = *symbol_table++;
342 if (strcmp (sym->name, symname) == 0
343 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
344 {
345 /* BFD symbols are section relative. */
346 symaddr = sym->value + sym->section->vma;
347 break;
348 }
349 }
350 do_cleanups (back_to);
351 }
352
353 if (symaddr)
354 return symaddr;
355
356 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
357 have to check the dynamic string table too. */
358
359 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
360
361 if (storage_needed > 0)
362 {
363 symbol_table = (asymbol **) xmalloc (storage_needed);
364 back_to = make_cleanup (xfree, symbol_table);
365 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
366
367 for (i = 0; i < number_of_symbols; i++)
368 {
369 sym = *symbol_table++;
370
371 if (strcmp (sym->name, symname) == 0
372 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
373 {
374 /* BFD symbols are section relative. */
375 symaddr = sym->value + sym->section->vma;
376 break;
377 }
378 }
379 do_cleanups (back_to);
380 }
381
382 return symaddr;
383 }
384
385
386 /* Read program header TYPE from inferior memory. The header is found
387 by scanning the OS auxillary vector.
388
389 Return a pointer to allocated memory holding the program header contents,
390 or NULL on failure. If sucessful, and unless P_SECT_SIZE is NULL, the
391 size of those contents is returned to P_SECT_SIZE. Likewise, the target
392 architecture size (32-bit or 64-bit) is returned to P_ARCH_SIZE. */
393
394 static gdb_byte *
395 read_program_header (int type, int *p_sect_size, int *p_arch_size)
396 {
397 CORE_ADDR at_phdr, at_phent, at_phnum;
398 int arch_size, sect_size;
399 CORE_ADDR sect_addr;
400 gdb_byte *buf;
401
402 /* Get required auxv elements from target. */
403 if (target_auxv_search (&current_target, AT_PHDR, &at_phdr) <= 0)
404 return 0;
405 if (target_auxv_search (&current_target, AT_PHENT, &at_phent) <= 0)
406 return 0;
407 if (target_auxv_search (&current_target, AT_PHNUM, &at_phnum) <= 0)
408 return 0;
409 if (!at_phdr || !at_phnum)
410 return 0;
411
412 /* Determine ELF architecture type. */
413 if (at_phent == sizeof (Elf32_External_Phdr))
414 arch_size = 32;
415 else if (at_phent == sizeof (Elf64_External_Phdr))
416 arch_size = 64;
417 else
418 return 0;
419
420 /* Find .dynamic section via the PT_DYNAMIC PHDR. */
421 if (arch_size == 32)
422 {
423 Elf32_External_Phdr phdr;
424 int i;
425
426 /* Search for requested PHDR. */
427 for (i = 0; i < at_phnum; i++)
428 {
429 if (target_read_memory (at_phdr + i * sizeof (phdr),
430 (gdb_byte *)&phdr, sizeof (phdr)))
431 return 0;
432
433 if (extract_unsigned_integer ((gdb_byte *)phdr.p_type, 4) == type)
434 break;
435 }
436
437 if (i == at_phnum)
438 return 0;
439
440 /* Retrieve address and size. */
441 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, 4);
442 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, 4);
443 }
444 else
445 {
446 Elf64_External_Phdr phdr;
447 int i;
448
449 /* Search for requested PHDR. */
450 for (i = 0; i < at_phnum; i++)
451 {
452 if (target_read_memory (at_phdr + i * sizeof (phdr),
453 (gdb_byte *)&phdr, sizeof (phdr)))
454 return 0;
455
456 if (extract_unsigned_integer ((gdb_byte *)phdr.p_type, 4) == type)
457 break;
458 }
459
460 if (i == at_phnum)
461 return 0;
462
463 /* Retrieve address and size. */
464 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr, 8);
465 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, 8);
466 }
467
468 /* Read in requested program header. */
469 buf = xmalloc (sect_size);
470 if (target_read_memory (sect_addr, buf, sect_size))
471 {
472 xfree (buf);
473 return NULL;
474 }
475
476 if (p_arch_size)
477 *p_arch_size = arch_size;
478 if (p_sect_size)
479 *p_sect_size = sect_size;
480
481 return buf;
482 }
483
484
485 /* Return program interpreter string. */
486 static gdb_byte *
487 find_program_interpreter (void)
488 {
489 gdb_byte *buf = NULL;
490
491 /* If we have an exec_bfd, use its section table. */
492 if (exec_bfd
493 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
494 {
495 struct bfd_section *interp_sect;
496
497 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
498 if (interp_sect != NULL)
499 {
500 CORE_ADDR sect_addr = bfd_section_vma (exec_bfd, interp_sect);
501 int sect_size = bfd_section_size (exec_bfd, interp_sect);
502
503 buf = xmalloc (sect_size);
504 bfd_get_section_contents (exec_bfd, interp_sect, buf, 0, sect_size);
505 }
506 }
507
508 /* If we didn't find it, use the target auxillary vector. */
509 if (!buf)
510 buf = read_program_header (PT_INTERP, NULL, NULL);
511
512 return buf;
513 }
514
515
516 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
517 returned and the corresponding PTR is set. */
518
519 static int
520 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
521 {
522 int arch_size, step, sect_size;
523 long dyn_tag;
524 CORE_ADDR dyn_ptr, dyn_addr;
525 gdb_byte *bufend, *bufstart, *buf;
526 Elf32_External_Dyn *x_dynp_32;
527 Elf64_External_Dyn *x_dynp_64;
528 struct bfd_section *sect;
529
530 if (abfd == NULL)
531 return 0;
532 arch_size = bfd_get_arch_size (abfd);
533 if (arch_size == -1)
534 return 0;
535
536 /* Find the start address of the .dynamic section. */
537 sect = bfd_get_section_by_name (abfd, ".dynamic");
538 if (sect == NULL)
539 return 0;
540 dyn_addr = bfd_section_vma (abfd, sect);
541
542 /* Read in .dynamic from the BFD. We will get the actual value
543 from memory later. */
544 sect_size = bfd_section_size (abfd, sect);
545 buf = bufstart = alloca (sect_size);
546 if (!bfd_get_section_contents (abfd, sect,
547 buf, 0, sect_size))
548 return 0;
549
550 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
551 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
552 : sizeof (Elf64_External_Dyn);
553 for (bufend = buf + sect_size;
554 buf < bufend;
555 buf += step)
556 {
557 if (arch_size == 32)
558 {
559 x_dynp_32 = (Elf32_External_Dyn *) buf;
560 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
561 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
562 }
563 else
564 {
565 x_dynp_64 = (Elf64_External_Dyn *) buf;
566 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
567 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
568 }
569 if (dyn_tag == DT_NULL)
570 return 0;
571 if (dyn_tag == dyntag)
572 {
573 /* If requested, try to read the runtime value of this .dynamic
574 entry. */
575 if (ptr)
576 {
577 struct type *ptr_type;
578 gdb_byte ptr_buf[8];
579 CORE_ADDR ptr_addr;
580
581 ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
582 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
583 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
584 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
585 *ptr = dyn_ptr;
586 }
587 return 1;
588 }
589 }
590
591 return 0;
592 }
593
594 /* Scan for DYNTAG in .dynamic section of the target's main executable,
595 found by consulting the OS auxillary vector. If DYNTAG is found 1 is
596 returned and the corresponding PTR is set. */
597
598 static int
599 scan_dyntag_auxv (int dyntag, CORE_ADDR *ptr)
600 {
601 int sect_size, arch_size, step;
602 long dyn_tag;
603 CORE_ADDR dyn_ptr;
604 gdb_byte *bufend, *bufstart, *buf;
605
606 /* Read in .dynamic section. */
607 buf = bufstart = read_program_header (PT_DYNAMIC, &sect_size, &arch_size);
608 if (!buf)
609 return 0;
610
611 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
612 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
613 : sizeof (Elf64_External_Dyn);
614 for (bufend = buf + sect_size;
615 buf < bufend;
616 buf += step)
617 {
618 if (arch_size == 32)
619 {
620 Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
621 dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, 4);
622 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, 4);
623 }
624 else
625 {
626 Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
627 dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag, 8);
628 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr, 8);
629 }
630 if (dyn_tag == DT_NULL)
631 break;
632
633 if (dyn_tag == dyntag)
634 {
635 if (ptr)
636 *ptr = dyn_ptr;
637
638 xfree (bufstart);
639 return 1;
640 }
641 }
642
643 xfree (bufstart);
644 return 0;
645 }
646
647
648 /*
649
650 LOCAL FUNCTION
651
652 elf_locate_base -- locate the base address of dynamic linker structs
653 for SVR4 elf targets.
654
655 SYNOPSIS
656
657 CORE_ADDR elf_locate_base (void)
658
659 DESCRIPTION
660
661 For SVR4 elf targets the address of the dynamic linker's runtime
662 structure is contained within the dynamic info section in the
663 executable file. The dynamic section is also mapped into the
664 inferior address space. Because the runtime loader fills in the
665 real address before starting the inferior, we have to read in the
666 dynamic info section from the inferior address space.
667 If there are any errors while trying to find the address, we
668 silently return 0, otherwise the found address is returned.
669
670 */
671
672 static CORE_ADDR
673 elf_locate_base (void)
674 {
675 struct minimal_symbol *msymbol;
676 CORE_ADDR dyn_ptr;
677
678 /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
679 instead of DT_DEBUG, although they sometimes contain an unused
680 DT_DEBUG. */
681 if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr)
682 || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr))
683 {
684 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
685 gdb_byte *pbuf;
686 int pbuf_size = TYPE_LENGTH (ptr_type);
687 pbuf = alloca (pbuf_size);
688 /* DT_MIPS_RLD_MAP contains a pointer to the address
689 of the dynamic link structure. */
690 if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
691 return 0;
692 return extract_typed_address (pbuf, ptr_type);
693 }
694
695 /* Find DT_DEBUG. */
696 if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr)
697 || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr))
698 return dyn_ptr;
699
700 /* This may be a static executable. Look for the symbol
701 conventionally named _r_debug, as a last resort. */
702 msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
703 if (msymbol != NULL)
704 return SYMBOL_VALUE_ADDRESS (msymbol);
705
706 /* DT_DEBUG entry not found. */
707 return 0;
708 }
709
710 /*
711
712 LOCAL FUNCTION
713
714 locate_base -- locate the base address of dynamic linker structs
715
716 SYNOPSIS
717
718 CORE_ADDR locate_base (void)
719
720 DESCRIPTION
721
722 For both the SunOS and SVR4 shared library implementations, if the
723 inferior executable has been linked dynamically, there is a single
724 address somewhere in the inferior's data space which is the key to
725 locating all of the dynamic linker's runtime structures. This
726 address is the value of the debug base symbol. The job of this
727 function is to find and return that address, or to return 0 if there
728 is no such address (the executable is statically linked for example).
729
730 For SunOS, the job is almost trivial, since the dynamic linker and
731 all of it's structures are statically linked to the executable at
732 link time. Thus the symbol for the address we are looking for has
733 already been added to the minimal symbol table for the executable's
734 objfile at the time the symbol file's symbols were read, and all we
735 have to do is look it up there. Note that we explicitly do NOT want
736 to find the copies in the shared library.
737
738 The SVR4 version is a bit more complicated because the address
739 is contained somewhere in the dynamic info section. We have to go
740 to a lot more work to discover the address of the debug base symbol.
741 Because of this complexity, we cache the value we find and return that
742 value on subsequent invocations. Note there is no copy in the
743 executable symbol tables.
744
745 */
746
747 static CORE_ADDR
748 locate_base (void)
749 {
750 /* Check to see if we have a currently valid address, and if so, avoid
751 doing all this work again and just return the cached address. If
752 we have no cached address, try to locate it in the dynamic info
753 section for ELF executables. There's no point in doing any of this
754 though if we don't have some link map offsets to work with. */
755
756 if (debug_base == 0 && svr4_have_link_map_offsets ())
757 {
758 if (exec_bfd != NULL
759 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
760 debug_base = elf_locate_base ();
761 }
762 return (debug_base);
763 }
764
765 /* Find the first element in the inferior's dynamic link map, and
766 return its address in the inferior.
767
768 FIXME: Perhaps we should validate the info somehow, perhaps by
769 checking r_version for a known version number, or r_state for
770 RT_CONSISTENT. */
771
772 static CORE_ADDR
773 solib_svr4_r_map (void)
774 {
775 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
776 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
777
778 return read_memory_typed_address (debug_base + lmo->r_map_offset, ptr_type);
779 }
780
781 /* Find r_brk from the inferior's debug base. */
782
783 static CORE_ADDR
784 solib_svr4_r_brk (void)
785 {
786 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
787 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
788
789 return read_memory_typed_address (debug_base + lmo->r_brk_offset, ptr_type);
790 }
791
792 /* Find the link map for the dynamic linker (if it is not in the
793 normal list of loaded shared objects). */
794
795 static CORE_ADDR
796 solib_svr4_r_ldsomap (void)
797 {
798 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
799 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
800 ULONGEST version;
801
802 /* Check version, and return zero if `struct r_debug' doesn't have
803 the r_ldsomap member. */
804 version = read_memory_unsigned_integer (debug_base + lmo->r_version_offset,
805 lmo->r_version_size);
806 if (version < 2 || lmo->r_ldsomap_offset == -1)
807 return 0;
808
809 return read_memory_typed_address (debug_base + lmo->r_ldsomap_offset,
810 ptr_type);
811 }
812
813 /*
814
815 LOCAL FUNCTION
816
817 open_symbol_file_object
818
819 SYNOPSIS
820
821 void open_symbol_file_object (void *from_tty)
822
823 DESCRIPTION
824
825 If no open symbol file, attempt to locate and open the main symbol
826 file. On SVR4 systems, this is the first link map entry. If its
827 name is here, we can open it. Useful when attaching to a process
828 without first loading its symbol file.
829
830 If FROM_TTYP dereferences to a non-zero integer, allow messages to
831 be printed. This parameter is a pointer rather than an int because
832 open_symbol_file_object() is called via catch_errors() and
833 catch_errors() requires a pointer argument. */
834
835 static int
836 open_symbol_file_object (void *from_ttyp)
837 {
838 CORE_ADDR lm, l_name;
839 char *filename;
840 int errcode;
841 int from_tty = *(int *)from_ttyp;
842 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
843 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
844 int l_name_size = TYPE_LENGTH (ptr_type);
845 gdb_byte *l_name_buf = xmalloc (l_name_size);
846 struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
847
848 if (symfile_objfile)
849 if (!query (_("Attempt to reload symbols from process? ")))
850 return 0;
851
852 /* Always locate the debug struct, in case it has moved. */
853 debug_base = 0;
854 if (locate_base () == 0)
855 return 0; /* failed somehow... */
856
857 /* First link map member should be the executable. */
858 lm = solib_svr4_r_map ();
859 if (lm == 0)
860 return 0; /* failed somehow... */
861
862 /* Read address of name from target memory to GDB. */
863 read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size);
864
865 /* Convert the address to host format. */
866 l_name = extract_typed_address (l_name_buf, ptr_type);
867
868 /* Free l_name_buf. */
869 do_cleanups (cleanups);
870
871 if (l_name == 0)
872 return 0; /* No filename. */
873
874 /* Now fetch the filename from target memory. */
875 target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
876 make_cleanup (xfree, filename);
877
878 if (errcode)
879 {
880 warning (_("failed to read exec filename from attached file: %s"),
881 safe_strerror (errcode));
882 return 0;
883 }
884
885 /* Have a pathname: read the symbol file. */
886 symbol_file_add_main (filename, from_tty);
887
888 return 1;
889 }
890
891 /* If no shared library information is available from the dynamic
892 linker, build a fallback list from other sources. */
893
894 static struct so_list *
895 svr4_default_sos (void)
896 {
897 struct so_list *head = NULL;
898 struct so_list **link_ptr = &head;
899
900 if (debug_loader_offset_p)
901 {
902 struct so_list *new = XZALLOC (struct so_list);
903
904 new->lm_info = xmalloc (sizeof (struct lm_info));
905
906 /* Nothing will ever check the cached copy of the link
907 map if we set l_addr. */
908 new->lm_info->l_addr = debug_loader_offset;
909 new->lm_info->lm_addr = 0;
910 new->lm_info->lm = NULL;
911
912 strncpy (new->so_name, debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
913 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
914 strcpy (new->so_original_name, new->so_name);
915
916 *link_ptr = new;
917 link_ptr = &new->next;
918 }
919
920 return head;
921 }
922
923 /* LOCAL FUNCTION
924
925 current_sos -- build a list of currently loaded shared objects
926
927 SYNOPSIS
928
929 struct so_list *current_sos ()
930
931 DESCRIPTION
932
933 Build a list of `struct so_list' objects describing the shared
934 objects currently loaded in the inferior. This list does not
935 include an entry for the main executable file.
936
937 Note that we only gather information directly available from the
938 inferior --- we don't examine any of the shared library files
939 themselves. The declaration of `struct so_list' says which fields
940 we provide values for. */
941
942 static struct so_list *
943 svr4_current_sos (void)
944 {
945 CORE_ADDR lm;
946 struct so_list *head = 0;
947 struct so_list **link_ptr = &head;
948 CORE_ADDR ldsomap = 0;
949
950 /* Always locate the debug struct, in case it has moved. */
951 debug_base = 0;
952 locate_base ();
953
954 /* If we can't find the dynamic linker's base structure, this
955 must not be a dynamically linked executable. Hmm. */
956 if (! debug_base)
957 return svr4_default_sos ();
958
959 /* Walk the inferior's link map list, and build our list of
960 `struct so_list' nodes. */
961 lm = solib_svr4_r_map ();
962
963 while (lm)
964 {
965 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
966 struct so_list *new = XZALLOC (struct so_list);
967 struct cleanup *old_chain = make_cleanup (xfree, new);
968
969 new->lm_info = xmalloc (sizeof (struct lm_info));
970 make_cleanup (xfree, new->lm_info);
971
972 new->lm_info->l_addr = (CORE_ADDR)-1;
973 new->lm_info->lm_addr = lm;
974 new->lm_info->lm = xzalloc (lmo->link_map_size);
975 make_cleanup (xfree, new->lm_info->lm);
976
977 read_memory (lm, new->lm_info->lm, lmo->link_map_size);
978
979 lm = LM_NEXT (new);
980
981 /* For SVR4 versions, the first entry in the link map is for the
982 inferior executable, so we must ignore it. For some versions of
983 SVR4, it has no name. For others (Solaris 2.3 for example), it
984 does have a name, so we can no longer use a missing name to
985 decide when to ignore it. */
986 if (IGNORE_FIRST_LINK_MAP_ENTRY (new) && ldsomap == 0)
987 {
988 main_lm_addr = new->lm_info->lm_addr;
989 free_so (new);
990 }
991 else
992 {
993 int errcode;
994 char *buffer;
995
996 /* Extract this shared object's name. */
997 target_read_string (LM_NAME (new), &buffer,
998 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
999 if (errcode != 0)
1000 warning (_("Can't read pathname for load map: %s."),
1001 safe_strerror (errcode));
1002 else
1003 {
1004 strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
1005 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1006 strcpy (new->so_original_name, new->so_name);
1007 }
1008 xfree (buffer);
1009
1010 /* If this entry has no name, or its name matches the name
1011 for the main executable, don't include it in the list. */
1012 if (! new->so_name[0]
1013 || match_main (new->so_name))
1014 free_so (new);
1015 else
1016 {
1017 new->next = 0;
1018 *link_ptr = new;
1019 link_ptr = &new->next;
1020 }
1021 }
1022
1023 /* On Solaris, the dynamic linker is not in the normal list of
1024 shared objects, so make sure we pick it up too. Having
1025 symbol information for the dynamic linker is quite crucial
1026 for skipping dynamic linker resolver code. */
1027 if (lm == 0 && ldsomap == 0)
1028 lm = ldsomap = solib_svr4_r_ldsomap ();
1029
1030 discard_cleanups (old_chain);
1031 }
1032
1033 if (head == NULL)
1034 return svr4_default_sos ();
1035
1036 return head;
1037 }
1038
1039 /* Get the address of the link_map for a given OBJFILE. */
1040
1041 CORE_ADDR
1042 svr4_fetch_objfile_link_map (struct objfile *objfile)
1043 {
1044 struct so_list *so;
1045
1046 /* Cause svr4_current_sos() to be run if it hasn't been already. */
1047 if (main_lm_addr == 0)
1048 solib_add (NULL, 0, &current_target, auto_solib_add);
1049
1050 /* svr4_current_sos() will set main_lm_addr for the main executable. */
1051 if (objfile == symfile_objfile)
1052 return main_lm_addr;
1053
1054 /* The other link map addresses may be found by examining the list
1055 of shared libraries. */
1056 for (so = master_so_list (); so; so = so->next)
1057 if (so->objfile == objfile)
1058 return so->lm_info->lm_addr;
1059
1060 /* Not found! */
1061 return 0;
1062 }
1063
1064 /* On some systems, the only way to recognize the link map entry for
1065 the main executable file is by looking at its name. Return
1066 non-zero iff SONAME matches one of the known main executable names. */
1067
1068 static int
1069 match_main (char *soname)
1070 {
1071 char **mainp;
1072
1073 for (mainp = main_name_list; *mainp != NULL; mainp++)
1074 {
1075 if (strcmp (soname, *mainp) == 0)
1076 return (1);
1077 }
1078
1079 return (0);
1080 }
1081
1082 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1083 SVR4 run time loader. */
1084 static CORE_ADDR interp_text_sect_low;
1085 static CORE_ADDR interp_text_sect_high;
1086 static CORE_ADDR interp_plt_sect_low;
1087 static CORE_ADDR interp_plt_sect_high;
1088
1089 int
1090 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
1091 {
1092 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
1093 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
1094 || in_plt_section (pc, NULL));
1095 }
1096
1097 /* Given an executable's ABFD and target, compute the entry-point
1098 address. */
1099
1100 static CORE_ADDR
1101 exec_entry_point (struct bfd *abfd, struct target_ops *targ)
1102 {
1103 /* KevinB wrote ... for most targets, the address returned by
1104 bfd_get_start_address() is the entry point for the start
1105 function. But, for some targets, bfd_get_start_address() returns
1106 the address of a function descriptor from which the entry point
1107 address may be extracted. This address is extracted by
1108 gdbarch_convert_from_func_ptr_addr(). The method
1109 gdbarch_convert_from_func_ptr_addr() is the merely the identify
1110 function for targets which don't use function descriptors. */
1111 return gdbarch_convert_from_func_ptr_addr (target_gdbarch,
1112 bfd_get_start_address (abfd),
1113 targ);
1114 }
1115
1116 /*
1117
1118 LOCAL FUNCTION
1119
1120 enable_break -- arrange for dynamic linker to hit breakpoint
1121
1122 SYNOPSIS
1123
1124 int enable_break (void)
1125
1126 DESCRIPTION
1127
1128 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1129 debugger interface, support for arranging for the inferior to hit
1130 a breakpoint after mapping in the shared libraries. This function
1131 enables that breakpoint.
1132
1133 For SunOS, there is a special flag location (in_debugger) which we
1134 set to 1. When the dynamic linker sees this flag set, it will set
1135 a breakpoint at a location known only to itself, after saving the
1136 original contents of that place and the breakpoint address itself,
1137 in it's own internal structures. When we resume the inferior, it
1138 will eventually take a SIGTRAP when it runs into the breakpoint.
1139 We handle this (in a different place) by restoring the contents of
1140 the breakpointed location (which is only known after it stops),
1141 chasing around to locate the shared libraries that have been
1142 loaded, then resuming.
1143
1144 For SVR4, the debugger interface structure contains a member (r_brk)
1145 which is statically initialized at the time the shared library is
1146 built, to the offset of a function (_r_debug_state) which is guaran-
1147 teed to be called once before mapping in a library, and again when
1148 the mapping is complete. At the time we are examining this member,
1149 it contains only the unrelocated offset of the function, so we have
1150 to do our own relocation. Later, when the dynamic linker actually
1151 runs, it relocates r_brk to be the actual address of _r_debug_state().
1152
1153 The debugger interface structure also contains an enumeration which
1154 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1155 depending upon whether or not the library is being mapped or unmapped,
1156 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1157 */
1158
1159 static int
1160 enable_break (void)
1161 {
1162 struct minimal_symbol *msymbol;
1163 char **bkpt_namep;
1164 asection *interp_sect;
1165 gdb_byte *interp_name;
1166 CORE_ADDR sym_addr;
1167
1168 /* First, remove all the solib event breakpoints. Their addresses
1169 may have changed since the last time we ran the program. */
1170 remove_solib_event_breakpoints ();
1171
1172 interp_text_sect_low = interp_text_sect_high = 0;
1173 interp_plt_sect_low = interp_plt_sect_high = 0;
1174
1175 /* If we already have a shared library list in the target, and
1176 r_debug contains r_brk, set the breakpoint there - this should
1177 mean r_brk has already been relocated. Assume the dynamic linker
1178 is the object containing r_brk. */
1179
1180 solib_add (NULL, 0, &current_target, auto_solib_add);
1181 sym_addr = 0;
1182 if (debug_base && solib_svr4_r_map () != 0)
1183 sym_addr = solib_svr4_r_brk ();
1184
1185 if (sym_addr != 0)
1186 {
1187 struct obj_section *os;
1188
1189 sym_addr = gdbarch_addr_bits_remove
1190 (target_gdbarch, gdbarch_convert_from_func_ptr_addr (target_gdbarch,
1191 sym_addr,
1192 &current_target));
1193
1194 os = find_pc_section (sym_addr);
1195 if (os != NULL)
1196 {
1197 /* Record the relocated start and end address of the dynamic linker
1198 text and plt section for svr4_in_dynsym_resolve_code. */
1199 bfd *tmp_bfd;
1200 CORE_ADDR load_addr;
1201
1202 tmp_bfd = os->objfile->obfd;
1203 load_addr = ANOFFSET (os->objfile->section_offsets,
1204 os->objfile->sect_index_text);
1205
1206 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
1207 if (interp_sect)
1208 {
1209 interp_text_sect_low =
1210 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1211 interp_text_sect_high =
1212 interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1213 }
1214 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1215 if (interp_sect)
1216 {
1217 interp_plt_sect_low =
1218 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1219 interp_plt_sect_high =
1220 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1221 }
1222
1223 create_solib_event_breakpoint (sym_addr);
1224 return 1;
1225 }
1226 }
1227
1228 /* Find the program interpreter; if not found, warn the user and drop
1229 into the old breakpoint at symbol code. */
1230 interp_name = find_program_interpreter ();
1231 if (interp_name)
1232 {
1233 CORE_ADDR load_addr = 0;
1234 int load_addr_found = 0;
1235 int loader_found_in_list = 0;
1236 struct so_list *so;
1237 bfd *tmp_bfd = NULL;
1238 struct target_ops *tmp_bfd_target;
1239 volatile struct gdb_exception ex;
1240
1241 sym_addr = 0;
1242
1243 /* Now we need to figure out where the dynamic linker was
1244 loaded so that we can load its symbols and place a breakpoint
1245 in the dynamic linker itself.
1246
1247 This address is stored on the stack. However, I've been unable
1248 to find any magic formula to find it for Solaris (appears to
1249 be trivial on GNU/Linux). Therefore, we have to try an alternate
1250 mechanism to find the dynamic linker's base address. */
1251
1252 TRY_CATCH (ex, RETURN_MASK_ALL)
1253 {
1254 tmp_bfd = solib_bfd_open (interp_name);
1255 }
1256 if (tmp_bfd == NULL)
1257 goto bkpt_at_symbol;
1258
1259 /* Now convert the TMP_BFD into a target. That way target, as
1260 well as BFD operations can be used. Note that closing the
1261 target will also close the underlying bfd. */
1262 tmp_bfd_target = target_bfd_reopen (tmp_bfd);
1263
1264 /* On a running target, we can get the dynamic linker's base
1265 address from the shared library table. */
1266 so = master_so_list ();
1267 while (so)
1268 {
1269 if (svr4_same_1 (interp_name, so->so_original_name))
1270 {
1271 load_addr_found = 1;
1272 loader_found_in_list = 1;
1273 load_addr = LM_ADDR_CHECK (so, tmp_bfd);
1274 break;
1275 }
1276 so = so->next;
1277 }
1278
1279 /* If we were not able to find the base address of the loader
1280 from our so_list, then try using the AT_BASE auxilliary entry. */
1281 if (!load_addr_found)
1282 if (target_auxv_search (&current_target, AT_BASE, &load_addr) > 0)
1283 load_addr_found = 1;
1284
1285 /* Otherwise we find the dynamic linker's base address by examining
1286 the current pc (which should point at the entry point for the
1287 dynamic linker) and subtracting the offset of the entry point.
1288
1289 This is more fragile than the previous approaches, but is a good
1290 fallback method because it has actually been working well in
1291 most cases. */
1292 if (!load_addr_found)
1293 {
1294 struct regcache *regcache = get_thread_regcache (inferior_ptid);
1295 load_addr = (regcache_read_pc (regcache)
1296 - exec_entry_point (tmp_bfd, tmp_bfd_target));
1297 }
1298
1299 if (!loader_found_in_list)
1300 {
1301 debug_loader_name = xstrdup (interp_name);
1302 debug_loader_offset_p = 1;
1303 debug_loader_offset = load_addr;
1304 solib_add (NULL, 0, &current_target, auto_solib_add);
1305 }
1306
1307 /* Record the relocated start and end address of the dynamic linker
1308 text and plt section for svr4_in_dynsym_resolve_code. */
1309 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
1310 if (interp_sect)
1311 {
1312 interp_text_sect_low =
1313 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1314 interp_text_sect_high =
1315 interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1316 }
1317 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1318 if (interp_sect)
1319 {
1320 interp_plt_sect_low =
1321 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1322 interp_plt_sect_high =
1323 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1324 }
1325
1326 /* Now try to set a breakpoint in the dynamic linker. */
1327 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1328 {
1329 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1330 if (sym_addr != 0)
1331 break;
1332 }
1333
1334 if (sym_addr != 0)
1335 /* Convert 'sym_addr' from a function pointer to an address.
1336 Because we pass tmp_bfd_target instead of the current
1337 target, this will always produce an unrelocated value. */
1338 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch,
1339 sym_addr,
1340 tmp_bfd_target);
1341
1342 /* We're done with both the temporary bfd and target. Remember,
1343 closing the target closes the underlying bfd. */
1344 target_close (tmp_bfd_target, 0);
1345
1346 if (sym_addr != 0)
1347 {
1348 create_solib_event_breakpoint (load_addr + sym_addr);
1349 xfree (interp_name);
1350 return 1;
1351 }
1352
1353 /* For whatever reason we couldn't set a breakpoint in the dynamic
1354 linker. Warn and drop into the old code. */
1355 bkpt_at_symbol:
1356 xfree (interp_name);
1357 warning (_("Unable to find dynamic linker breakpoint function.\n"
1358 "GDB will be unable to debug shared library initializers\n"
1359 "and track explicitly loaded dynamic code."));
1360 }
1361
1362 /* Scan through the lists of symbols, trying to look up the symbol and
1363 set a breakpoint there. Terminate loop when we/if we succeed. */
1364
1365 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1366 {
1367 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1368 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1369 {
1370 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1371 return 1;
1372 }
1373 }
1374
1375 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1376 {
1377 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1378 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1379 {
1380 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1381 return 1;
1382 }
1383 }
1384 return 0;
1385 }
1386
1387 /*
1388
1389 LOCAL FUNCTION
1390
1391 special_symbol_handling -- additional shared library symbol handling
1392
1393 SYNOPSIS
1394
1395 void special_symbol_handling ()
1396
1397 DESCRIPTION
1398
1399 Once the symbols from a shared object have been loaded in the usual
1400 way, we are called to do any system specific symbol handling that
1401 is needed.
1402
1403 For SunOS4, this consisted of grunging around in the dynamic
1404 linkers structures to find symbol definitions for "common" symbols
1405 and adding them to the minimal symbol table for the runtime common
1406 objfile.
1407
1408 However, for SVR4, there's nothing to do.
1409
1410 */
1411
1412 static void
1413 svr4_special_symbol_handling (void)
1414 {
1415 }
1416
1417 /* Relocate the main executable. This function should be called upon
1418 stopping the inferior process at the entry point to the program.
1419 The entry point from BFD is compared to the PC and if they are
1420 different, the main executable is relocated by the proper amount.
1421
1422 As written it will only attempt to relocate executables which
1423 lack interpreter sections. It seems likely that only dynamic
1424 linker executables will get relocated, though it should work
1425 properly for a position-independent static executable as well. */
1426
1427 static void
1428 svr4_relocate_main_executable (void)
1429 {
1430 asection *interp_sect;
1431 struct regcache *regcache = get_thread_regcache (inferior_ptid);
1432 CORE_ADDR pc = regcache_read_pc (regcache);
1433
1434 /* Decide if the objfile needs to be relocated. As indicated above,
1435 we will only be here when execution is stopped at the beginning
1436 of the program. Relocation is necessary if the address at which
1437 we are presently stopped differs from the start address stored in
1438 the executable AND there's no interpreter section. The condition
1439 regarding the interpreter section is very important because if
1440 there *is* an interpreter section, execution will begin there
1441 instead. When there is an interpreter section, the start address
1442 is (presumably) used by the interpreter at some point to start
1443 execution of the program.
1444
1445 If there is an interpreter, it is normal for it to be set to an
1446 arbitrary address at the outset. The job of finding it is
1447 handled in enable_break().
1448
1449 So, to summarize, relocations are necessary when there is no
1450 interpreter section and the start address obtained from the
1451 executable is different from the address at which GDB is
1452 currently stopped.
1453
1454 [ The astute reader will note that we also test to make sure that
1455 the executable in question has the DYNAMIC flag set. It is my
1456 opinion that this test is unnecessary (undesirable even). It
1457 was added to avoid inadvertent relocation of an executable
1458 whose e_type member in the ELF header is not ET_DYN. There may
1459 be a time in the future when it is desirable to do relocations
1460 on other types of files as well in which case this condition
1461 should either be removed or modified to accomodate the new file
1462 type. (E.g, an ET_EXEC executable which has been built to be
1463 position-independent could safely be relocated by the OS if
1464 desired. It is true that this violates the ABI, but the ABI
1465 has been known to be bent from time to time.) - Kevin, Nov 2000. ]
1466 */
1467
1468 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1469 if (interp_sect == NULL
1470 && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0
1471 && (exec_entry_point (exec_bfd, &exec_ops) != pc))
1472 {
1473 struct cleanup *old_chain;
1474 struct section_offsets *new_offsets;
1475 int i, changed;
1476 CORE_ADDR displacement;
1477
1478 /* It is necessary to relocate the objfile. The amount to
1479 relocate by is simply the address at which we are stopped
1480 minus the starting address from the executable.
1481
1482 We relocate all of the sections by the same amount. This
1483 behavior is mandated by recent editions of the System V ABI.
1484 According to the System V Application Binary Interface,
1485 Edition 4.1, page 5-5:
1486
1487 ... Though the system chooses virtual addresses for
1488 individual processes, it maintains the segments' relative
1489 positions. Because position-independent code uses relative
1490 addressesing between segments, the difference between
1491 virtual addresses in memory must match the difference
1492 between virtual addresses in the file. The difference
1493 between the virtual address of any segment in memory and
1494 the corresponding virtual address in the file is thus a
1495 single constant value for any one executable or shared
1496 object in a given process. This difference is the base
1497 address. One use of the base address is to relocate the
1498 memory image of the program during dynamic linking.
1499
1500 The same language also appears in Edition 4.0 of the System V
1501 ABI and is left unspecified in some of the earlier editions. */
1502
1503 displacement = pc - exec_entry_point (exec_bfd, &exec_ops);
1504 changed = 0;
1505
1506 new_offsets = xcalloc (symfile_objfile->num_sections,
1507 sizeof (struct section_offsets));
1508 old_chain = make_cleanup (xfree, new_offsets);
1509
1510 for (i = 0; i < symfile_objfile->num_sections; i++)
1511 {
1512 if (displacement != ANOFFSET (symfile_objfile->section_offsets, i))
1513 changed = 1;
1514 new_offsets->offsets[i] = displacement;
1515 }
1516
1517 if (changed)
1518 objfile_relocate (symfile_objfile, new_offsets);
1519
1520 do_cleanups (old_chain);
1521 }
1522 }
1523
1524 /*
1525
1526 GLOBAL FUNCTION
1527
1528 svr4_solib_create_inferior_hook -- shared library startup support
1529
1530 SYNOPSIS
1531
1532 void svr4_solib_create_inferior_hook ()
1533
1534 DESCRIPTION
1535
1536 When gdb starts up the inferior, it nurses it along (through the
1537 shell) until it is ready to execute it's first instruction. At this
1538 point, this function gets called via expansion of the macro
1539 SOLIB_CREATE_INFERIOR_HOOK.
1540
1541 For SunOS executables, this first instruction is typically the
1542 one at "_start", or a similar text label, regardless of whether
1543 the executable is statically or dynamically linked. The runtime
1544 startup code takes care of dynamically linking in any shared
1545 libraries, once gdb allows the inferior to continue.
1546
1547 For SVR4 executables, this first instruction is either the first
1548 instruction in the dynamic linker (for dynamically linked
1549 executables) or the instruction at "start" for statically linked
1550 executables. For dynamically linked executables, the system
1551 first exec's /lib/libc.so.N, which contains the dynamic linker,
1552 and starts it running. The dynamic linker maps in any needed
1553 shared libraries, maps in the actual user executable, and then
1554 jumps to "start" in the user executable.
1555
1556 For both SunOS shared libraries, and SVR4 shared libraries, we
1557 can arrange to cooperate with the dynamic linker to discover the
1558 names of shared libraries that are dynamically linked, and the
1559 base addresses to which they are linked.
1560
1561 This function is responsible for discovering those names and
1562 addresses, and saving sufficient information about them to allow
1563 their symbols to be read at a later time.
1564
1565 FIXME
1566
1567 Between enable_break() and disable_break(), this code does not
1568 properly handle hitting breakpoints which the user might have
1569 set in the startup code or in the dynamic linker itself. Proper
1570 handling will probably have to wait until the implementation is
1571 changed to use the "breakpoint handler function" method.
1572
1573 Also, what if child has exit()ed? Must exit loop somehow.
1574 */
1575
1576 static void
1577 svr4_solib_create_inferior_hook (void)
1578 {
1579 struct inferior *inf;
1580 struct thread_info *tp;
1581
1582 /* Relocate the main executable if necessary. */
1583 svr4_relocate_main_executable ();
1584
1585 if (!svr4_have_link_map_offsets ())
1586 return;
1587
1588 if (!enable_break ())
1589 return;
1590
1591 #if defined(_SCO_DS)
1592 /* SCO needs the loop below, other systems should be using the
1593 special shared library breakpoints and the shared library breakpoint
1594 service routine.
1595
1596 Now run the target. It will eventually hit the breakpoint, at
1597 which point all of the libraries will have been mapped in and we
1598 can go groveling around in the dynamic linker structures to find
1599 out what we need to know about them. */
1600
1601 inf = current_inferior ();
1602 tp = inferior_thread ();
1603
1604 clear_proceed_status ();
1605 inf->stop_soon = STOP_QUIETLY;
1606 tp->stop_signal = TARGET_SIGNAL_0;
1607 do
1608 {
1609 target_resume (pid_to_ptid (-1), 0, tp->stop_signal);
1610 wait_for_inferior (0);
1611 }
1612 while (tp->stop_signal != TARGET_SIGNAL_TRAP);
1613 inf->stop_soon = NO_STOP_QUIETLY;
1614 #endif /* defined(_SCO_DS) */
1615 }
1616
1617 static void
1618 svr4_clear_solib (void)
1619 {
1620 debug_base = 0;
1621 debug_loader_offset_p = 0;
1622 debug_loader_offset = 0;
1623 xfree (debug_loader_name);
1624 debug_loader_name = NULL;
1625 main_lm_addr = 0;
1626 }
1627
1628 static void
1629 svr4_free_so (struct so_list *so)
1630 {
1631 xfree (so->lm_info->lm);
1632 xfree (so->lm_info);
1633 }
1634
1635
1636 /* Clear any bits of ADDR that wouldn't fit in a target-format
1637 data pointer. "Data pointer" here refers to whatever sort of
1638 address the dynamic linker uses to manage its sections. At the
1639 moment, we don't support shared libraries on any processors where
1640 code and data pointers are different sizes.
1641
1642 This isn't really the right solution. What we really need here is
1643 a way to do arithmetic on CORE_ADDR values that respects the
1644 natural pointer/address correspondence. (For example, on the MIPS,
1645 converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
1646 sign-extend the value. There, simply truncating the bits above
1647 gdbarch_ptr_bit, as we do below, is no good.) This should probably
1648 be a new gdbarch method or something. */
1649 static CORE_ADDR
1650 svr4_truncate_ptr (CORE_ADDR addr)
1651 {
1652 if (gdbarch_ptr_bit (target_gdbarch) == sizeof (CORE_ADDR) * 8)
1653 /* We don't need to truncate anything, and the bit twiddling below
1654 will fail due to overflow problems. */
1655 return addr;
1656 else
1657 return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch)) - 1);
1658 }
1659
1660
1661 static void
1662 svr4_relocate_section_addresses (struct so_list *so,
1663 struct section_table *sec)
1664 {
1665 sec->addr = svr4_truncate_ptr (sec->addr + LM_ADDR_CHECK (so,
1666 sec->bfd));
1667 sec->endaddr = svr4_truncate_ptr (sec->endaddr + LM_ADDR_CHECK (so,
1668 sec->bfd));
1669 }
1670 \f
1671
1672 /* Architecture-specific operations. */
1673
1674 /* Per-architecture data key. */
1675 static struct gdbarch_data *solib_svr4_data;
1676
1677 struct solib_svr4_ops
1678 {
1679 /* Return a description of the layout of `struct link_map'. */
1680 struct link_map_offsets *(*fetch_link_map_offsets)(void);
1681 };
1682
1683 /* Return a default for the architecture-specific operations. */
1684
1685 static void *
1686 solib_svr4_init (struct obstack *obstack)
1687 {
1688 struct solib_svr4_ops *ops;
1689
1690 ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops);
1691 ops->fetch_link_map_offsets = NULL;
1692 return ops;
1693 }
1694
1695 /* Set the architecture-specific `struct link_map_offsets' fetcher for
1696 GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
1697
1698 void
1699 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
1700 struct link_map_offsets *(*flmo) (void))
1701 {
1702 struct solib_svr4_ops *ops = gdbarch_data (gdbarch, solib_svr4_data);
1703
1704 ops->fetch_link_map_offsets = flmo;
1705
1706 set_solib_ops (gdbarch, &svr4_so_ops);
1707 }
1708
1709 /* Fetch a link_map_offsets structure using the architecture-specific
1710 `struct link_map_offsets' fetcher. */
1711
1712 static struct link_map_offsets *
1713 svr4_fetch_link_map_offsets (void)
1714 {
1715 struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch, solib_svr4_data);
1716
1717 gdb_assert (ops->fetch_link_map_offsets);
1718 return ops->fetch_link_map_offsets ();
1719 }
1720
1721 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
1722
1723 static int
1724 svr4_have_link_map_offsets (void)
1725 {
1726 struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch, solib_svr4_data);
1727 return (ops->fetch_link_map_offsets != NULL);
1728 }
1729 \f
1730
1731 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
1732 `struct r_debug' and a `struct link_map' that are binary compatible
1733 with the origional SVR4 implementation. */
1734
1735 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1736 for an ILP32 SVR4 system. */
1737
1738 struct link_map_offsets *
1739 svr4_ilp32_fetch_link_map_offsets (void)
1740 {
1741 static struct link_map_offsets lmo;
1742 static struct link_map_offsets *lmp = NULL;
1743
1744 if (lmp == NULL)
1745 {
1746 lmp = &lmo;
1747
1748 lmo.r_version_offset = 0;
1749 lmo.r_version_size = 4;
1750 lmo.r_map_offset = 4;
1751 lmo.r_brk_offset = 8;
1752 lmo.r_ldsomap_offset = 20;
1753
1754 /* Everything we need is in the first 20 bytes. */
1755 lmo.link_map_size = 20;
1756 lmo.l_addr_offset = 0;
1757 lmo.l_name_offset = 4;
1758 lmo.l_ld_offset = 8;
1759 lmo.l_next_offset = 12;
1760 lmo.l_prev_offset = 16;
1761 }
1762
1763 return lmp;
1764 }
1765
1766 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1767 for an LP64 SVR4 system. */
1768
1769 struct link_map_offsets *
1770 svr4_lp64_fetch_link_map_offsets (void)
1771 {
1772 static struct link_map_offsets lmo;
1773 static struct link_map_offsets *lmp = NULL;
1774
1775 if (lmp == NULL)
1776 {
1777 lmp = &lmo;
1778
1779 lmo.r_version_offset = 0;
1780 lmo.r_version_size = 4;
1781 lmo.r_map_offset = 8;
1782 lmo.r_brk_offset = 16;
1783 lmo.r_ldsomap_offset = 40;
1784
1785 /* Everything we need is in the first 40 bytes. */
1786 lmo.link_map_size = 40;
1787 lmo.l_addr_offset = 0;
1788 lmo.l_name_offset = 8;
1789 lmo.l_ld_offset = 16;
1790 lmo.l_next_offset = 24;
1791 lmo.l_prev_offset = 32;
1792 }
1793
1794 return lmp;
1795 }
1796 \f
1797
1798 struct target_so_ops svr4_so_ops;
1799
1800 /* Lookup global symbol for ELF DSOs linked with -Bsymbolic. Those DSOs have a
1801 different rule for symbol lookup. The lookup begins here in the DSO, not in
1802 the main executable. */
1803
1804 static struct symbol *
1805 elf_lookup_lib_symbol (const struct objfile *objfile,
1806 const char *name,
1807 const char *linkage_name,
1808 const domain_enum domain)
1809 {
1810 if (objfile->obfd == NULL
1811 || scan_dyntag (DT_SYMBOLIC, objfile->obfd, NULL) != 1)
1812 return NULL;
1813
1814 return lookup_global_symbol_from_objfile
1815 (objfile, name, linkage_name, domain);
1816 }
1817
1818 extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */
1819
1820 void
1821 _initialize_svr4_solib (void)
1822 {
1823 solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init);
1824
1825 svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
1826 svr4_so_ops.free_so = svr4_free_so;
1827 svr4_so_ops.clear_solib = svr4_clear_solib;
1828 svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
1829 svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
1830 svr4_so_ops.current_sos = svr4_current_sos;
1831 svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
1832 svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
1833 svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol;
1834 svr4_so_ops.same = svr4_same;
1835 }
This page took 0.0654 seconds and 5 git commands to generate.