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