solib.c relocation improvements
[binutils-gdb.git] / gdb / solib-svr4.c
1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 98, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #define _SYSCALL32 /* for Sparc64 cross Sparc32 */
23 #include "defs.h"
24
25
26 #include <sys/types.h>
27 #include <signal.h>
28 #include "gdb_string.h"
29 #include <sys/param.h>
30 #include <fcntl.h>
31
32 #ifndef SVR4_SHARED_LIBS
33 /* SunOS shared libs need the nlist structure. */
34 #include <a.out.h>
35 #else
36 #include "elf/external.h"
37 #endif
38
39 #ifdef HAVE_LINK_H
40 #include <link.h>
41 #endif
42
43 #include "symtab.h"
44 #include "bfd.h"
45 #include "symfile.h"
46 #include "objfiles.h"
47 #include "gdbcore.h"
48 #include "command.h"
49 #include "target.h"
50 #include "frame.h"
51 #include "gdb_regex.h"
52 #include "inferior.h"
53 #include "environ.h"
54 #include "language.h"
55 #include "gdbcmd.h"
56
57 #include "solist.h"
58 #include "solib-svr4.h"
59
60 /* Link map info to include in an allocated so_list entry */
61
62 struct lm_info
63 {
64 /* Pointer to copy of link map from inferior. The type is char *
65 rather than void *, so that we may use byte offsets to find the
66 various fields without the need for a cast. */
67 char *lm;
68 };
69
70 /* On SVR4 systems, a list of symbols in the dynamic linker where
71 GDB can try to place a breakpoint to monitor shared library
72 events.
73
74 If none of these symbols are found, or other errors occur, then
75 SVR4 systems will fall back to using a symbol as the "startup
76 mapping complete" breakpoint address. */
77
78 #ifdef SVR4_SHARED_LIBS
79 static char *solib_break_names[] =
80 {
81 "r_debug_state",
82 "_r_debug_state",
83 "_dl_debug_state",
84 "rtld_db_dlactivity",
85 NULL
86 };
87 #endif
88
89 #define BKPT_AT_SYMBOL 1
90
91 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
92 static char *bkpt_names[] =
93 {
94 #ifdef SOLIB_BKPT_NAME
95 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
96 #endif
97 "_start",
98 "main",
99 NULL
100 };
101 #endif
102
103 /* Symbols which are used to locate the base of the link map structures. */
104
105 #ifndef SVR4_SHARED_LIBS
106 static char *debug_base_symbols[] =
107 {
108 "_DYNAMIC",
109 "_DYNAMIC__MGC",
110 NULL
111 };
112 #endif
113
114 static char *main_name_list[] =
115 {
116 "main_$main",
117 NULL
118 };
119
120
121 /* Fetch (and possibly build) an appropriate link_map_offsets structure
122 for native targets using struct definitions from link.h. */
123
124 struct link_map_offsets *
125 default_svr4_fetch_link_map_offsets (void)
126 {
127 #ifdef HAVE_LINK_H
128 static struct link_map_offsets lmo;
129 static struct link_map_offsets *lmp = 0;
130 #if defined (HAVE_STRUCT_LINK_MAP32)
131 static struct link_map_offsets lmo32;
132 static struct link_map_offsets *lmp32 = 0;
133 #endif
134
135 #ifndef offsetof
136 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
137 #endif
138 #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER))
139
140 if (lmp == 0)
141 {
142 lmp = &lmo;
143
144 #ifdef SVR4_SHARED_LIBS
145 lmo.r_debug_size = sizeof (struct r_debug);
146
147 lmo.r_map_offset = offsetof (struct r_debug, r_map);
148 lmo.r_map_size = fieldsize (struct r_debug, r_map);
149
150 lmo.link_map_size = sizeof (struct link_map);
151
152 lmo.l_addr_offset = offsetof (struct link_map, l_addr);
153 lmo.l_addr_size = fieldsize (struct link_map, l_addr);
154
155 lmo.l_next_offset = offsetof (struct link_map, l_next);
156 lmo.l_next_size = fieldsize (struct link_map, l_next);
157
158 lmo.l_prev_offset = offsetof (struct link_map, l_prev);
159 lmo.l_prev_size = fieldsize (struct link_map, l_prev);
160
161 lmo.l_name_offset = offsetof (struct link_map, l_name);
162 lmo.l_name_size = fieldsize (struct link_map, l_name);
163 #else /* !SVR4_SHARED_LIBS */
164 lmo.link_map_size = sizeof (struct link_map);
165
166 lmo.l_addr_offset = offsetof (struct link_map, lm_addr);
167 lmo.l_addr_size = fieldsize (struct link_map, lm_addr);
168
169 lmo.l_next_offset = offsetof (struct link_map, lm_next);
170 lmo.l_next_size = fieldsize (struct link_map, lm_next);
171
172 lmo.l_name_offset = offsetof (struct link_map, lm_name);
173 lmo.l_name_size = fieldsize (struct link_map, lm_name);
174 #endif /* SVR4_SHARED_LIBS */
175 }
176
177 #if defined (HAVE_STRUCT_LINK_MAP32)
178 if (lmp32 == 0)
179 {
180 lmp32 = &lmo32;
181
182 lmo32.r_debug_size = sizeof (struct r_debug32);
183
184 lmo32.r_map_offset = offsetof (struct r_debug32, r_map);
185 lmo32.r_map_size = fieldsize (struct r_debug32, r_map);
186
187 lmo32.link_map_size = sizeof (struct link_map32);
188
189 lmo32.l_addr_offset = offsetof (struct link_map32, l_addr);
190 lmo32.l_addr_size = fieldsize (struct link_map32, l_addr);
191
192 lmo32.l_next_offset = offsetof (struct link_map32, l_next);
193 lmo32.l_next_size = fieldsize (struct link_map32, l_next);
194
195 lmo32.l_prev_offset = offsetof (struct link_map32, l_prev);
196 lmo32.l_prev_size = fieldsize (struct link_map32, l_prev);
197
198 lmo32.l_name_offset = offsetof (struct link_map32, l_name);
199 lmo32.l_name_size = fieldsize (struct link_map32, l_name);
200 }
201 #endif /* defined (HAVE_STRUCT_LINK_MAP32) */
202
203 #if defined (HAVE_STRUCT_LINK_MAP32)
204 if (bfd_get_arch_size (exec_bfd) == 32)
205 return lmp32;
206 else
207 #endif
208 return lmp;
209
210 #else
211
212 internal_error ("default_svr4_fetch_link_map_offsets called without HAVE_LINK_H defined.");
213 return 0;
214
215 #endif /* HAVE_LINK_H */
216 }
217
218 /* Macro to extract an address from a solib structure.
219 When GDB is configured for some 32-bit targets (e.g. Solaris 2.7
220 sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is
221 64 bits. We have to extract only the significant bits of addresses
222 to get the right address when accessing the core file BFD. */
223
224 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
225 extract_address (&(MEMBER), sizeof (MEMBER))
226
227 /* local data declarations */
228
229 #ifndef SVR4_SHARED_LIBS
230
231 /* NOTE: converted the macros LM_ADDR, LM_NEXT, LM_NAME and
232 IGNORE_FIRST_LINK_MAP_ENTRY into functions (see below).
233 MVS, June 2000 */
234
235 static struct link_dynamic dynamic_copy;
236 static struct link_dynamic_2 ld_2_copy;
237 static struct ld_debug debug_copy;
238 static CORE_ADDR debug_addr;
239 static CORE_ADDR flag_addr;
240
241 #endif /* !SVR4_SHARED_LIBS */
242
243 /* link map access functions */
244
245 static CORE_ADDR
246 LM_ADDR (struct so_list *so)
247 {
248 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
249
250 return extract_address (so->lm_info->lm + lmo->l_addr_offset, lmo->l_addr_size);
251 }
252
253 static CORE_ADDR
254 LM_NEXT (struct so_list *so)
255 {
256 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
257
258 return extract_address (so->lm_info->lm + lmo->l_next_offset, lmo->l_next_size);
259 }
260
261 static CORE_ADDR
262 LM_NAME (struct so_list *so)
263 {
264 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
265
266 return extract_address (so->lm_info->lm + lmo->l_name_offset, lmo->l_name_size);
267 }
268
269 #ifndef SVR4_SHARED_LIBS
270
271 static int
272 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
273 {
274 return 0;
275 }
276
277 #else /* SVR4_SHARED_LIBS */
278
279 static int
280 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
281 {
282 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
283
284 return extract_address (so->lm_info->lm + lmo->l_prev_offset,
285 lmo->l_prev_size) == 0;
286 }
287
288 #endif /* !SVR4_SHARED_LIBS */
289
290
291 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
292 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
293
294 /* Local function prototypes */
295
296 static int match_main (char *);
297
298 /* If non-zero, this is a prefix that will be added to the front of the name
299 shared libraries with an absolute filename for loading. */
300 static char *solib_absolute_prefix = NULL;
301
302 /* If non-empty, this is a search path for loading non-absolute shared library
303 symbol files. This takes precedence over the environment variables PATH
304 and LD_LIBRARY_PATH. */
305 static char *solib_search_path = NULL;
306
307
308 #ifndef SVR4_SHARED_LIBS
309
310 /* Allocate the runtime common object file. */
311
312 static void
313 allocate_rt_common_objfile (void)
314 {
315 struct objfile *objfile;
316 struct objfile *last_one;
317
318 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
319 memset (objfile, 0, sizeof (struct objfile));
320 objfile->md = NULL;
321 obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0,
322 xmalloc, free);
323 obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc,
324 free);
325 obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc,
326 free);
327 obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc,
328 free);
329 objfile->name = mstrsave (objfile->md, "rt_common");
330
331 /* Add this file onto the tail of the linked list of other such files. */
332
333 objfile->next = NULL;
334 if (object_files == NULL)
335 object_files = objfile;
336 else
337 {
338 for (last_one = object_files;
339 last_one->next;
340 last_one = last_one->next);
341 last_one->next = objfile;
342 }
343
344 rt_common_objfile = objfile;
345 }
346
347 /* Read all dynamically loaded common symbol definitions from the inferior
348 and put them into the minimal symbol table for the runtime common
349 objfile. */
350
351 static void
352 solib_add_common_symbols (CORE_ADDR rtc_symp)
353 {
354 struct rtc_symb inferior_rtc_symb;
355 struct nlist inferior_rtc_nlist;
356 int len;
357 char *name;
358
359 /* Remove any runtime common symbols from previous runs. */
360
361 if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count)
362 {
363 obstack_free (&rt_common_objfile->symbol_obstack, 0);
364 obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0,
365 xmalloc, free);
366 rt_common_objfile->minimal_symbol_count = 0;
367 rt_common_objfile->msymbols = NULL;
368 }
369
370 init_minimal_symbol_collection ();
371 make_cleanup_discard_minimal_symbols ();
372
373 while (rtc_symp)
374 {
375 read_memory (rtc_symp,
376 (char *) &inferior_rtc_symb,
377 sizeof (inferior_rtc_symb));
378 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp),
379 (char *) &inferior_rtc_nlist,
380 sizeof (inferior_rtc_nlist));
381 if (inferior_rtc_nlist.n_type == N_COMM)
382 {
383 /* FIXME: The length of the symbol name is not available, but in the
384 current implementation the common symbol is allocated immediately
385 behind the name of the symbol. */
386 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
387
388 name = xmalloc (len);
389 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name),
390 name, len);
391
392 /* Allocate the runtime common objfile if necessary. */
393 if (rt_common_objfile == NULL)
394 allocate_rt_common_objfile ();
395
396 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
397 mst_bss, rt_common_objfile);
398 free (name);
399 }
400 rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next);
401 }
402
403 /* Install any minimal symbols that have been collected as the current
404 minimal symbols for the runtime common objfile. */
405
406 install_minimal_symbols (rt_common_objfile);
407 }
408
409 #endif /* SVR4_SHARED_LIBS */
410
411
412 #ifdef SVR4_SHARED_LIBS
413
414 static CORE_ADDR bfd_lookup_symbol (bfd *, char *);
415
416 /*
417
418 LOCAL FUNCTION
419
420 bfd_lookup_symbol -- lookup the value for a specific symbol
421
422 SYNOPSIS
423
424 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
425
426 DESCRIPTION
427
428 An expensive way to lookup the value of a single symbol for
429 bfd's that are only temporary anyway. This is used by the
430 shared library support to find the address of the debugger
431 interface structures in the shared library.
432
433 Note that 0 is specifically allowed as an error return (no
434 such symbol).
435 */
436
437 static CORE_ADDR
438 bfd_lookup_symbol (bfd *abfd, char *symname)
439 {
440 unsigned int storage_needed;
441 asymbol *sym;
442 asymbol **symbol_table;
443 unsigned int number_of_symbols;
444 unsigned int i;
445 struct cleanup *back_to;
446 CORE_ADDR symaddr = 0;
447
448 storage_needed = bfd_get_symtab_upper_bound (abfd);
449
450 if (storage_needed > 0)
451 {
452 symbol_table = (asymbol **) xmalloc (storage_needed);
453 back_to = make_cleanup (free, (PTR) symbol_table);
454 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
455
456 for (i = 0; i < number_of_symbols; i++)
457 {
458 sym = *symbol_table++;
459 if (STREQ (sym->name, symname))
460 {
461 /* Bfd symbols are section relative. */
462 symaddr = sym->value + sym->section->vma;
463 break;
464 }
465 }
466 do_cleanups (back_to);
467 }
468
469 if (symaddr)
470 return symaddr;
471
472 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
473 have to check the dynamic string table too. */
474
475 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
476
477 if (storage_needed > 0)
478 {
479 symbol_table = (asymbol **) xmalloc (storage_needed);
480 back_to = make_cleanup (free, (PTR) symbol_table);
481 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
482
483 for (i = 0; i < number_of_symbols; i++)
484 {
485 sym = *symbol_table++;
486 if (STREQ (sym->name, symname))
487 {
488 /* Bfd symbols are section relative. */
489 symaddr = sym->value + sym->section->vma;
490 break;
491 }
492 }
493 do_cleanups (back_to);
494 }
495
496 return symaddr;
497 }
498
499 #ifdef HANDLE_SVR4_EXEC_EMULATORS
500
501 /*
502 Solaris BCP (the part of Solaris which allows it to run SunOS4
503 a.out files) throws in another wrinkle. Solaris does not fill
504 in the usual a.out link map structures when running BCP programs,
505 the only way to get at them is via groping around in the dynamic
506 linker.
507 The dynamic linker and it's structures are located in the shared
508 C library, which gets run as the executable's "interpreter" by
509 the kernel.
510
511 Note that we can assume nothing about the process state at the time
512 we need to find these structures. We may be stopped on the first
513 instruction of the interpreter (C shared library), the first
514 instruction of the executable itself, or somewhere else entirely
515 (if we attached to the process for example).
516 */
517
518 static char *debug_base_symbols[] =
519 {
520 "r_debug", /* Solaris 2.3 */
521 "_r_debug", /* Solaris 2.1, 2.2 */
522 NULL
523 };
524
525 static int look_for_base (int, CORE_ADDR);
526
527 /*
528
529 LOCAL FUNCTION
530
531 look_for_base -- examine file for each mapped address segment
532
533 SYNOPSYS
534
535 static int look_for_base (int fd, CORE_ADDR baseaddr)
536
537 DESCRIPTION
538
539 This function is passed to proc_iterate_over_mappings, which
540 causes it to get called once for each mapped address space, with
541 an open file descriptor for the file mapped to that space, and the
542 base address of that mapped space.
543
544 Our job is to find the debug base symbol in the file that this
545 fd is open on, if it exists, and if so, initialize the dynamic
546 linker structure base address debug_base.
547
548 Note that this is a computationally expensive proposition, since
549 we basically have to open a bfd on every call, so we specifically
550 avoid opening the exec file.
551 */
552
553 static int
554 look_for_base (int fd, CORE_ADDR baseaddr)
555 {
556 bfd *interp_bfd;
557 CORE_ADDR address = 0;
558 char **symbolp;
559
560 /* If the fd is -1, then there is no file that corresponds to this
561 mapped memory segment, so skip it. Also, if the fd corresponds
562 to the exec file, skip it as well. */
563
564 if (fd == -1
565 || (exec_bfd != NULL
566 && fdmatch (fileno ((FILE *) (exec_bfd->iostream)), fd)))
567 {
568 return (0);
569 }
570
571 /* Try to open whatever random file this fd corresponds to. Note that
572 we have no way currently to find the filename. Don't gripe about
573 any problems we might have, just fail. */
574
575 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
576 {
577 return (0);
578 }
579 if (!bfd_check_format (interp_bfd, bfd_object))
580 {
581 /* FIXME-leak: on failure, might not free all memory associated with
582 interp_bfd. */
583 bfd_close (interp_bfd);
584 return (0);
585 }
586
587 /* Now try to find our debug base symbol in this file, which we at
588 least know to be a valid ELF executable or shared library. */
589
590 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
591 {
592 address = bfd_lookup_symbol (interp_bfd, *symbolp);
593 if (address != 0)
594 {
595 break;
596 }
597 }
598 if (address == 0)
599 {
600 /* FIXME-leak: on failure, might not free all memory associated with
601 interp_bfd. */
602 bfd_close (interp_bfd);
603 return (0);
604 }
605
606 /* Eureka! We found the symbol. But now we may need to relocate it
607 by the base address. If the symbol's value is less than the base
608 address of the shared library, then it hasn't yet been relocated
609 by the dynamic linker, and we have to do it ourself. FIXME: Note
610 that we make the assumption that the first segment that corresponds
611 to the shared library has the base address to which the library
612 was relocated. */
613
614 if (address < baseaddr)
615 {
616 address += baseaddr;
617 }
618 debug_base = address;
619 /* FIXME-leak: on failure, might not free all memory associated with
620 interp_bfd. */
621 bfd_close (interp_bfd);
622 return (1);
623 }
624 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
625
626 /*
627
628 LOCAL FUNCTION
629
630 elf_locate_base -- locate the base address of dynamic linker structs
631 for SVR4 elf targets.
632
633 SYNOPSIS
634
635 CORE_ADDR elf_locate_base (void)
636
637 DESCRIPTION
638
639 For SVR4 elf targets the address of the dynamic linker's runtime
640 structure is contained within the dynamic info section in the
641 executable file. The dynamic section is also mapped into the
642 inferior address space. Because the runtime loader fills in the
643 real address before starting the inferior, we have to read in the
644 dynamic info section from the inferior address space.
645 If there are any errors while trying to find the address, we
646 silently return 0, otherwise the found address is returned.
647
648 */
649
650 static CORE_ADDR
651 elf_locate_base (void)
652 {
653 sec_ptr dyninfo_sect;
654 int dyninfo_sect_size;
655 CORE_ADDR dyninfo_addr;
656 char *buf;
657 char *bufend;
658 int arch_size;
659
660 /* Find the start address of the .dynamic section. */
661 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
662 if (dyninfo_sect == NULL)
663 return 0;
664 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
665
666 /* Read in .dynamic section, silently ignore errors. */
667 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
668 buf = alloca (dyninfo_sect_size);
669 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
670 return 0;
671
672 /* Find the DT_DEBUG entry in the the .dynamic section.
673 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
674 no DT_DEBUG entries. */
675
676 arch_size = bfd_get_arch_size (exec_bfd);
677 if (arch_size == -1) /* failure */
678 return 0;
679
680 if (arch_size == 32)
681 { /* 32-bit elf */
682 for (bufend = buf + dyninfo_sect_size;
683 buf < bufend;
684 buf += sizeof (Elf32_External_Dyn))
685 {
686 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *) buf;
687 long dyn_tag;
688 CORE_ADDR dyn_ptr;
689
690 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
691 if (dyn_tag == DT_NULL)
692 break;
693 else if (dyn_tag == DT_DEBUG)
694 {
695 dyn_ptr = bfd_h_get_32 (exec_bfd,
696 (bfd_byte *) x_dynp->d_un.d_ptr);
697 return dyn_ptr;
698 }
699 #ifdef DT_MIPS_RLD_MAP
700 else if (dyn_tag == DT_MIPS_RLD_MAP)
701 {
702 char *pbuf;
703
704 pbuf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
705 /* DT_MIPS_RLD_MAP contains a pointer to the address
706 of the dynamic link structure. */
707 dyn_ptr = bfd_h_get_32 (exec_bfd,
708 (bfd_byte *) x_dynp->d_un.d_ptr);
709 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
710 return 0;
711 return extract_unsigned_integer (pbuf, sizeof (pbuf));
712 }
713 #endif
714 }
715 }
716 else /* 64-bit elf */
717 {
718 for (bufend = buf + dyninfo_sect_size;
719 buf < bufend;
720 buf += sizeof (Elf64_External_Dyn))
721 {
722 Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *) buf;
723 long dyn_tag;
724 CORE_ADDR dyn_ptr;
725
726 dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
727 if (dyn_tag == DT_NULL)
728 break;
729 else if (dyn_tag == DT_DEBUG)
730 {
731 dyn_ptr = bfd_h_get_64 (exec_bfd,
732 (bfd_byte *) x_dynp->d_un.d_ptr);
733 return dyn_ptr;
734 }
735 }
736 }
737
738 /* DT_DEBUG entry not found. */
739 return 0;
740 }
741
742 #endif /* SVR4_SHARED_LIBS */
743
744 /*
745
746 LOCAL FUNCTION
747
748 locate_base -- locate the base address of dynamic linker structs
749
750 SYNOPSIS
751
752 CORE_ADDR locate_base (void)
753
754 DESCRIPTION
755
756 For both the SunOS and SVR4 shared library implementations, if the
757 inferior executable has been linked dynamically, there is a single
758 address somewhere in the inferior's data space which is the key to
759 locating all of the dynamic linker's runtime structures. This
760 address is the value of the debug base symbol. The job of this
761 function is to find and return that address, or to return 0 if there
762 is no such address (the executable is statically linked for example).
763
764 For SunOS, the job is almost trivial, since the dynamic linker and
765 all of it's structures are statically linked to the executable at
766 link time. Thus the symbol for the address we are looking for has
767 already been added to the minimal symbol table for the executable's
768 objfile at the time the symbol file's symbols were read, and all we
769 have to do is look it up there. Note that we explicitly do NOT want
770 to find the copies in the shared library.
771
772 The SVR4 version is a bit more complicated because the address
773 is contained somewhere in the dynamic info section. We have to go
774 to a lot more work to discover the address of the debug base symbol.
775 Because of this complexity, we cache the value we find and return that
776 value on subsequent invocations. Note there is no copy in the
777 executable symbol tables.
778
779 */
780
781 static CORE_ADDR
782 locate_base (void)
783 {
784
785 #ifndef SVR4_SHARED_LIBS
786
787 struct minimal_symbol *msymbol;
788 CORE_ADDR address = 0;
789 char **symbolp;
790
791 /* For SunOS, we want to limit the search for the debug base symbol to the
792 executable being debugged, since there is a duplicate named symbol in the
793 shared library. We don't want the shared library versions. */
794
795 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
796 {
797 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
798 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
799 {
800 address = SYMBOL_VALUE_ADDRESS (msymbol);
801 return (address);
802 }
803 }
804 return (0);
805
806 #else /* SVR4_SHARED_LIBS */
807
808 /* Check to see if we have a currently valid address, and if so, avoid
809 doing all this work again and just return the cached address. If
810 we have no cached address, try to locate it in the dynamic info
811 section for ELF executables. */
812
813 if (debug_base == 0)
814 {
815 if (exec_bfd != NULL
816 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
817 debug_base = elf_locate_base ();
818 #ifdef HANDLE_SVR4_EXEC_EMULATORS
819 /* Try it the hard way for emulated executables. */
820 else if (inferior_pid != 0 && target_has_execution)
821 proc_iterate_over_mappings (look_for_base);
822 #endif
823 }
824 return (debug_base);
825
826 #endif /* !SVR4_SHARED_LIBS */
827
828 }
829
830 /*
831
832 LOCAL FUNCTION
833
834 first_link_map_member -- locate first member in dynamic linker's map
835
836 SYNOPSIS
837
838 static CORE_ADDR first_link_map_member (void)
839
840 DESCRIPTION
841
842 Find the first element in the inferior's dynamic link map, and
843 return its address in the inferior. This function doesn't copy the
844 link map entry itself into our address space; current_sos actually
845 does the reading. */
846
847 static CORE_ADDR
848 first_link_map_member (void)
849 {
850 CORE_ADDR lm = 0;
851
852 #ifndef SVR4_SHARED_LIBS
853
854 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
855 if (dynamic_copy.ld_version >= 2)
856 {
857 /* It is a version that we can deal with, so read in the secondary
858 structure and find the address of the link map list from it. */
859 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2),
860 (char *) &ld_2_copy, sizeof (struct link_dynamic_2));
861 lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded);
862 }
863
864 #else /* SVR4_SHARED_LIBS */
865 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
866 char *r_map_buf = xmalloc (lmo->r_map_size);
867 struct cleanup *cleanups = make_cleanup (free, r_map_buf);
868
869 read_memory (debug_base + lmo->r_map_offset, r_map_buf, lmo->r_map_size);
870
871 lm = extract_address (r_map_buf, lmo->r_map_size);
872
873 /* FIXME: Perhaps we should validate the info somehow, perhaps by
874 checking r_version for a known version number, or r_state for
875 RT_CONSISTENT. */
876
877 do_cleanups (cleanups);
878
879 #endif /* !SVR4_SHARED_LIBS */
880
881 return (lm);
882 }
883
884 #ifdef SVR4_SHARED_LIBS
885 /*
886
887 LOCAL FUNCTION
888
889 open_symbol_file_object
890
891 SYNOPSIS
892
893 void open_symbol_file_object (void *from_tty)
894
895 DESCRIPTION
896
897 If no open symbol file, attempt to locate and open the main symbol
898 file. On SVR4 systems, this is the first link map entry. If its
899 name is here, we can open it. Useful when attaching to a process
900 without first loading its symbol file.
901
902 If FROM_TTYP dereferences to a non-zero integer, allow messages to
903 be printed. This parameter is a pointer rather than an int because
904 open_symbol_file_object() is called via catch_errors() and
905 catch_errors() requires a pointer argument. */
906
907 static int
908 open_symbol_file_object (void *from_ttyp)
909 {
910 CORE_ADDR lm, l_name;
911 char *filename;
912 int errcode;
913 int from_tty = *(int *)from_ttyp;
914 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
915 char *l_name_buf = xmalloc (lmo->l_name_size);
916 struct cleanup *cleanups = make_cleanup (free, l_name_buf);
917
918 if (symfile_objfile)
919 if (!query ("Attempt to reload symbols from process? "))
920 return 0;
921
922 if ((debug_base = locate_base ()) == 0)
923 return 0; /* failed somehow... */
924
925 /* First link map member should be the executable. */
926 if ((lm = first_link_map_member ()) == 0)
927 return 0; /* failed somehow... */
928
929 /* Read address of name from target memory to GDB. */
930 read_memory (lm + lmo->l_name_offset, l_name_buf, lmo->l_name_size);
931
932 /* Convert the address to host format. */
933 l_name = extract_address (l_name_buf, lmo->l_name_size);
934
935 /* Free l_name_buf. */
936 do_cleanups (cleanups);
937
938 if (l_name == 0)
939 return 0; /* No filename. */
940
941 /* Now fetch the filename from target memory. */
942 target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
943
944 if (errcode)
945 {
946 warning ("failed to read exec filename from attached file: %s",
947 safe_strerror (errcode));
948 return 0;
949 }
950
951 make_cleanup (free, filename);
952 /* Have a pathname: read the symbol file. */
953 symbol_file_command (filename, from_tty);
954
955 return 1;
956 }
957 #else
958
959 static int
960 open_symbol_file_object (int *from_ttyp)
961 {
962 return 1;
963 }
964
965 #endif /* SVR4_SHARED_LIBS */
966
967
968 /* LOCAL FUNCTION
969
970 current_sos -- build a list of currently loaded shared objects
971
972 SYNOPSIS
973
974 struct so_list *current_sos ()
975
976 DESCRIPTION
977
978 Build a list of `struct so_list' objects describing the shared
979 objects currently loaded in the inferior. This list does not
980 include an entry for the main executable file.
981
982 Note that we only gather information directly available from the
983 inferior --- we don't examine any of the shared library files
984 themselves. The declaration of `struct so_list' says which fields
985 we provide values for. */
986
987 static struct so_list *
988 svr4_current_sos (void)
989 {
990 CORE_ADDR lm;
991 struct so_list *head = 0;
992 struct so_list **link_ptr = &head;
993
994 /* Make sure we've looked up the inferior's dynamic linker's base
995 structure. */
996 if (! debug_base)
997 {
998 debug_base = locate_base ();
999
1000 /* If we can't find the dynamic linker's base structure, this
1001 must not be a dynamically linked executable. Hmm. */
1002 if (! debug_base)
1003 return 0;
1004 }
1005
1006 /* Walk the inferior's link map list, and build our list of
1007 `struct so_list' nodes. */
1008 lm = first_link_map_member ();
1009 while (lm)
1010 {
1011 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
1012 struct so_list *new
1013 = (struct so_list *) xmalloc (sizeof (struct so_list));
1014 struct cleanup *old_chain = make_cleanup (free, new);
1015
1016 memset (new, 0, sizeof (*new));
1017
1018 new->lm_info = xmalloc (sizeof (struct lm_info));
1019 make_cleanup (free, new->lm_info);
1020
1021 new->lm_info->lm = xmalloc (lmo->link_map_size);
1022 make_cleanup (free, new->lm_info->lm);
1023 memset (new->lm_info->lm, 0, lmo->link_map_size);
1024
1025 read_memory (lm, new->lm_info->lm, lmo->link_map_size);
1026
1027 lm = LM_NEXT (new);
1028
1029 /* For SVR4 versions, the first entry in the link map is for the
1030 inferior executable, so we must ignore it. For some versions of
1031 SVR4, it has no name. For others (Solaris 2.3 for example), it
1032 does have a name, so we can no longer use a missing name to
1033 decide when to ignore it. */
1034 if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
1035 free_so (new);
1036 else
1037 {
1038 int errcode;
1039 char *buffer;
1040
1041 /* Extract this shared object's name. */
1042 target_read_string (LM_NAME (new), &buffer,
1043 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
1044 if (errcode != 0)
1045 {
1046 warning ("current_sos: Can't read pathname for load map: %s\n",
1047 safe_strerror (errcode));
1048 }
1049 else
1050 {
1051 strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
1052 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1053 free (buffer);
1054 strcpy (new->so_original_name, new->so_name);
1055 }
1056
1057 /* If this entry has no name, or its name matches the name
1058 for the main executable, don't include it in the list. */
1059 if (! new->so_name[0]
1060 || match_main (new->so_name))
1061 free_so (new);
1062 else
1063 {
1064 new->next = 0;
1065 *link_ptr = new;
1066 link_ptr = &new->next;
1067 }
1068 }
1069
1070 discard_cleanups (old_chain);
1071 }
1072
1073 return head;
1074 }
1075
1076
1077 /* On some systems, the only way to recognize the link map entry for
1078 the main executable file is by looking at its name. Return
1079 non-zero iff SONAME matches one of the known main executable names. */
1080
1081 static int
1082 match_main (char *soname)
1083 {
1084 char **mainp;
1085
1086 for (mainp = main_name_list; *mainp != NULL; mainp++)
1087 {
1088 if (strcmp (soname, *mainp) == 0)
1089 return (1);
1090 }
1091
1092 return (0);
1093 }
1094
1095
1096 #ifdef SVR4_SHARED_LIBS
1097
1098 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1099 SVR4 run time loader. */
1100
1101 static CORE_ADDR interp_text_sect_low;
1102 static CORE_ADDR interp_text_sect_high;
1103 static CORE_ADDR interp_plt_sect_low;
1104 static CORE_ADDR interp_plt_sect_high;
1105
1106 int
1107 in_svr4_dynsym_resolve_code (CORE_ADDR pc)
1108 {
1109 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
1110 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
1111 || in_plt_section (pc, NULL));
1112 }
1113 #endif
1114
1115 /*
1116
1117 LOCAL FUNCTION
1118
1119 disable_break -- remove the "mapping changed" breakpoint
1120
1121 SYNOPSIS
1122
1123 static int disable_break ()
1124
1125 DESCRIPTION
1126
1127 Removes the breakpoint that gets hit when the dynamic linker
1128 completes a mapping change.
1129
1130 */
1131
1132 #ifndef SVR4_SHARED_LIBS
1133
1134 static int
1135 disable_break (void)
1136 {
1137 int status = 1;
1138
1139 int in_debugger = 0;
1140
1141 /* Read the debugger structure from the inferior to retrieve the
1142 address of the breakpoint and the original contents of the
1143 breakpoint address. Remove the breakpoint by writing the original
1144 contents back. */
1145
1146 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1147
1148 /* Set `in_debugger' to zero now. */
1149
1150 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1151
1152 breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr);
1153 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1154 sizeof (debug_copy.ldd_bp_inst));
1155
1156 /* For the SVR4 version, we always know the breakpoint address. For the
1157 SunOS version we don't know it until the above code is executed.
1158 Grumble if we are stopped anywhere besides the breakpoint address. */
1159
1160 if (stop_pc != breakpoint_addr)
1161 {
1162 warning ("stopped at unknown breakpoint while handling shared libraries");
1163 }
1164
1165 return (status);
1166 }
1167
1168 #endif /* #ifdef SVR4_SHARED_LIBS */
1169
1170 /*
1171
1172 LOCAL FUNCTION
1173
1174 enable_break -- arrange for dynamic linker to hit breakpoint
1175
1176 SYNOPSIS
1177
1178 int enable_break (void)
1179
1180 DESCRIPTION
1181
1182 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1183 debugger interface, support for arranging for the inferior to hit
1184 a breakpoint after mapping in the shared libraries. This function
1185 enables that breakpoint.
1186
1187 For SunOS, there is a special flag location (in_debugger) which we
1188 set to 1. When the dynamic linker sees this flag set, it will set
1189 a breakpoint at a location known only to itself, after saving the
1190 original contents of that place and the breakpoint address itself,
1191 in it's own internal structures. When we resume the inferior, it
1192 will eventually take a SIGTRAP when it runs into the breakpoint.
1193 We handle this (in a different place) by restoring the contents of
1194 the breakpointed location (which is only known after it stops),
1195 chasing around to locate the shared libraries that have been
1196 loaded, then resuming.
1197
1198 For SVR4, the debugger interface structure contains a member (r_brk)
1199 which is statically initialized at the time the shared library is
1200 built, to the offset of a function (_r_debug_state) which is guaran-
1201 teed to be called once before mapping in a library, and again when
1202 the mapping is complete. At the time we are examining this member,
1203 it contains only the unrelocated offset of the function, so we have
1204 to do our own relocation. Later, when the dynamic linker actually
1205 runs, it relocates r_brk to be the actual address of _r_debug_state().
1206
1207 The debugger interface structure also contains an enumeration which
1208 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1209 depending upon whether or not the library is being mapped or unmapped,
1210 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1211 */
1212
1213 static int
1214 enable_break (void)
1215 {
1216 int success = 0;
1217
1218 #ifndef SVR4_SHARED_LIBS
1219
1220 int j;
1221 int in_debugger;
1222
1223 /* Get link_dynamic structure */
1224
1225 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1226 sizeof (dynamic_copy));
1227 if (j)
1228 {
1229 /* unreadable */
1230 return (0);
1231 }
1232
1233 /* Calc address of debugger interface structure */
1234
1235 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1236
1237 /* Calc address of `in_debugger' member of debugger interface structure */
1238
1239 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1240 (char *) &debug_copy);
1241
1242 /* Write a value of 1 to this member. */
1243
1244 in_debugger = 1;
1245 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1246 success = 1;
1247
1248 #else /* SVR4_SHARED_LIBS */
1249
1250 #ifdef BKPT_AT_SYMBOL
1251
1252 struct minimal_symbol *msymbol;
1253 char **bkpt_namep;
1254 asection *interp_sect;
1255
1256 /* First, remove all the solib event breakpoints. Their addresses
1257 may have changed since the last time we ran the program. */
1258 remove_solib_event_breakpoints ();
1259
1260 #ifdef SVR4_SHARED_LIBS
1261 interp_text_sect_low = interp_text_sect_high = 0;
1262 interp_plt_sect_low = interp_plt_sect_high = 0;
1263
1264 /* Find the .interp section; if not found, warn the user and drop
1265 into the old breakpoint at symbol code. */
1266 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1267 if (interp_sect)
1268 {
1269 unsigned int interp_sect_size;
1270 char *buf;
1271 CORE_ADDR load_addr;
1272 bfd *tmp_bfd;
1273 CORE_ADDR sym_addr = 0;
1274
1275 /* Read the contents of the .interp section into a local buffer;
1276 the contents specify the dynamic linker this program uses. */
1277 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1278 buf = alloca (interp_sect_size);
1279 bfd_get_section_contents (exec_bfd, interp_sect,
1280 buf, 0, interp_sect_size);
1281
1282 /* Now we need to figure out where the dynamic linker was
1283 loaded so that we can load its symbols and place a breakpoint
1284 in the dynamic linker itself.
1285
1286 This address is stored on the stack. However, I've been unable
1287 to find any magic formula to find it for Solaris (appears to
1288 be trivial on GNU/Linux). Therefore, we have to try an alternate
1289 mechanism to find the dynamic linker's base address. */
1290 tmp_bfd = bfd_openr (buf, gnutarget);
1291 if (tmp_bfd == NULL)
1292 goto bkpt_at_symbol;
1293
1294 /* Make sure the dynamic linker's really a useful object. */
1295 if (!bfd_check_format (tmp_bfd, bfd_object))
1296 {
1297 warning ("Unable to grok dynamic linker %s as an object file", buf);
1298 bfd_close (tmp_bfd);
1299 goto bkpt_at_symbol;
1300 }
1301
1302 /* We find the dynamic linker's base address by examining the
1303 current pc (which point at the entry point for the dynamic
1304 linker) and subtracting the offset of the entry point. */
1305 load_addr = read_pc () - tmp_bfd->start_address;
1306
1307 /* Record the relocated start and end address of the dynamic linker
1308 text and plt section for in_svr4_dynsym_resolve_code. */
1309 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
1310 if (interp_sect)
1311 {
1312 interp_text_sect_low =
1313 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1314 interp_text_sect_high =
1315 interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1316 }
1317 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1318 if (interp_sect)
1319 {
1320 interp_plt_sect_low =
1321 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1322 interp_plt_sect_high =
1323 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1324 }
1325
1326 /* Now try to set a breakpoint in the dynamic linker. */
1327 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1328 {
1329 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1330 if (sym_addr != 0)
1331 break;
1332 }
1333
1334 /* We're done with the temporary bfd. */
1335 bfd_close (tmp_bfd);
1336
1337 if (sym_addr != 0)
1338 {
1339 create_solib_event_breakpoint (load_addr + sym_addr);
1340 return 1;
1341 }
1342
1343 /* For whatever reason we couldn't set a breakpoint in the dynamic
1344 linker. Warn and drop into the old code. */
1345 bkpt_at_symbol:
1346 warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
1347 }
1348 #endif
1349
1350 /* Scan through the list of symbols, trying to look up the symbol and
1351 set a breakpoint there. Terminate loop when we/if we succeed. */
1352
1353 breakpoint_addr = 0;
1354 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1355 {
1356 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1357 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1358 {
1359 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1360 return 1;
1361 }
1362 }
1363
1364 /* Nothing good happened. */
1365 success = 0;
1366
1367 #endif /* BKPT_AT_SYMBOL */
1368
1369 #endif /* !SVR4_SHARED_LIBS */
1370
1371 return (success);
1372 }
1373
1374 /*
1375
1376 LOCAL FUNCTION
1377
1378 special_symbol_handling -- additional shared library symbol handling
1379
1380 SYNOPSIS
1381
1382 void special_symbol_handling ()
1383
1384 DESCRIPTION
1385
1386 Once the symbols from a shared object have been loaded in the usual
1387 way, we are called to do any system specific symbol handling that
1388 is needed.
1389
1390 For SunOS4, this consists of grunging around in the dynamic
1391 linkers structures to find symbol definitions for "common" symbols
1392 and adding them to the minimal symbol table for the runtime common
1393 objfile.
1394
1395 */
1396
1397 static void
1398 svr4_special_symbol_handling (void)
1399 {
1400 #ifndef SVR4_SHARED_LIBS
1401 int j;
1402
1403 if (debug_addr == 0)
1404 {
1405 /* Get link_dynamic structure */
1406
1407 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1408 sizeof (dynamic_copy));
1409 if (j)
1410 {
1411 /* unreadable */
1412 return;
1413 }
1414
1415 /* Calc address of debugger interface structure */
1416 /* FIXME, this needs work for cross-debugging of core files
1417 (byteorder, size, alignment, etc). */
1418
1419 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1420 }
1421
1422 /* Read the debugger structure from the inferior, just to make sure
1423 we have a current copy. */
1424
1425 j = target_read_memory (debug_addr, (char *) &debug_copy,
1426 sizeof (debug_copy));
1427 if (j)
1428 return; /* unreadable */
1429
1430 /* Get common symbol definitions for the loaded object. */
1431
1432 if (debug_copy.ldd_cp)
1433 {
1434 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp));
1435 }
1436
1437 #endif /* !SVR4_SHARED_LIBS */
1438 }
1439
1440 /*
1441
1442 GLOBAL FUNCTION
1443
1444 svr4_solib_create_inferior_hook -- shared library startup support
1445
1446 SYNOPSIS
1447
1448 void svr4_solib_create_inferior_hook()
1449
1450 DESCRIPTION
1451
1452 When gdb starts up the inferior, it nurses it along (through the
1453 shell) until it is ready to execute it's first instruction. At this
1454 point, this function gets called via expansion of the macro
1455 SOLIB_CREATE_INFERIOR_HOOK.
1456
1457 For SunOS executables, this first instruction is typically the
1458 one at "_start", or a similar text label, regardless of whether
1459 the executable is statically or dynamically linked. The runtime
1460 startup code takes care of dynamically linking in any shared
1461 libraries, once gdb allows the inferior to continue.
1462
1463 For SVR4 executables, this first instruction is either the first
1464 instruction in the dynamic linker (for dynamically linked
1465 executables) or the instruction at "start" for statically linked
1466 executables. For dynamically linked executables, the system
1467 first exec's /lib/libc.so.N, which contains the dynamic linker,
1468 and starts it running. The dynamic linker maps in any needed
1469 shared libraries, maps in the actual user executable, and then
1470 jumps to "start" in the user executable.
1471
1472 For both SunOS shared libraries, and SVR4 shared libraries, we
1473 can arrange to cooperate with the dynamic linker to discover the
1474 names of shared libraries that are dynamically linked, and the
1475 base addresses to which they are linked.
1476
1477 This function is responsible for discovering those names and
1478 addresses, and saving sufficient information about them to allow
1479 their symbols to be read at a later time.
1480
1481 FIXME
1482
1483 Between enable_break() and disable_break(), this code does not
1484 properly handle hitting breakpoints which the user might have
1485 set in the startup code or in the dynamic linker itself. Proper
1486 handling will probably have to wait until the implementation is
1487 changed to use the "breakpoint handler function" method.
1488
1489 Also, what if child has exit()ed? Must exit loop somehow.
1490 */
1491
1492 void
1493 svr4_solib_create_inferior_hook (void)
1494 {
1495 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1496 yet. In fact, in the case of a SunOS4 executable being run on
1497 Solaris, we can't get it yet. current_sos will get it when it needs
1498 it. */
1499 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1500 if ((debug_base = locate_base ()) == 0)
1501 {
1502 /* Can't find the symbol or the executable is statically linked. */
1503 return;
1504 }
1505 #endif
1506
1507 if (!enable_break ())
1508 {
1509 warning ("shared library handler failed to enable breakpoint");
1510 return;
1511 }
1512
1513 #if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS)
1514 /* SCO and SunOS need the loop below, other systems should be using the
1515 special shared library breakpoints and the shared library breakpoint
1516 service routine.
1517
1518 Now run the target. It will eventually hit the breakpoint, at
1519 which point all of the libraries will have been mapped in and we
1520 can go groveling around in the dynamic linker structures to find
1521 out what we need to know about them. */
1522
1523 clear_proceed_status ();
1524 stop_soon_quietly = 1;
1525 stop_signal = TARGET_SIGNAL_0;
1526 do
1527 {
1528 target_resume (-1, 0, stop_signal);
1529 wait_for_inferior ();
1530 }
1531 while (stop_signal != TARGET_SIGNAL_TRAP);
1532 stop_soon_quietly = 0;
1533
1534 #if !defined(_SCO_DS)
1535 /* We are now either at the "mapping complete" breakpoint (or somewhere
1536 else, a condition we aren't prepared to deal with anyway), so adjust
1537 the PC as necessary after a breakpoint, disable the breakpoint, and
1538 add any shared libraries that were mapped in. */
1539
1540 if (DECR_PC_AFTER_BREAK)
1541 {
1542 stop_pc -= DECR_PC_AFTER_BREAK;
1543 write_register (PC_REGNUM, stop_pc);
1544 }
1545
1546 if (!disable_break ())
1547 {
1548 warning ("shared library handler failed to disable breakpoint");
1549 }
1550
1551 if (auto_solib_add)
1552 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1553 #endif /* ! _SCO_DS */
1554 #endif
1555 }
1556
1557 static void
1558 svr4_clear_solib (void)
1559 {
1560 debug_base = 0;
1561 }
1562
1563 static void
1564 svr4_free_so (struct so_list *so)
1565 {
1566 free (so->lm_info->lm);
1567 free (so->lm_info);
1568 }
1569
1570 static void
1571 svr4_relocate_section_addresses (struct so_list *so,
1572 struct section_table *sec)
1573 {
1574 sec->addr += LM_ADDR (so);
1575 sec->endaddr += LM_ADDR (so);
1576 }
1577
1578 static struct target_so_ops svr4_so_ops;
1579
1580 void
1581 _initialize_svr4_solib (void)
1582 {
1583 svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
1584 svr4_so_ops.free_so = svr4_free_so;
1585 svr4_so_ops.clear_solib = svr4_clear_solib;
1586 svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
1587 svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
1588 svr4_so_ops.current_sos = svr4_current_sos;
1589 svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
1590
1591 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
1592 current_target_so_ops = &svr4_so_ops;
1593 }
1594