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