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