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