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