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