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