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