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