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