1 /* Handle FR-V (FDPIC) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_string.h"
34 #include "exceptions.h"
36 /* Flag which indicates whether internal debug messages should be printed. */
37 static int solib_frv_debug
;
39 /* FR-V pointers are four bytes wide. */
40 enum { FRV_PTR_SIZE
= 4 };
42 /* Representation of loadmap and related structs for the FR-V FDPIC ABI. */
44 /* External versions; the size and alignment of the fields should be
45 the same as those on the target. When loaded, the placement of
46 the bits in each field will be the same as on the target. */
47 typedef gdb_byte ext_Elf32_Half
[2];
48 typedef gdb_byte ext_Elf32_Addr
[4];
49 typedef gdb_byte ext_Elf32_Word
[4];
51 struct ext_elf32_fdpic_loadseg
53 /* Core address to which the segment is mapped. */
55 /* VMA recorded in the program header. */
56 ext_Elf32_Addr p_vaddr
;
57 /* Size of this segment in memory. */
58 ext_Elf32_Word p_memsz
;
61 struct ext_elf32_fdpic_loadmap
{
62 /* Protocol version number, must be zero. */
63 ext_Elf32_Half version
;
64 /* Number of segments in this map. */
66 /* The actual memory map. */
67 struct ext_elf32_fdpic_loadseg segs
[1 /* nsegs, actually */];
70 /* Internal versions; the types are GDB types and the data in each
71 of the fields is (or will be) decoded from the external struct
72 for ease of consumption. */
73 struct int_elf32_fdpic_loadseg
75 /* Core address to which the segment is mapped. */
77 /* VMA recorded in the program header. */
79 /* Size of this segment in memory. */
83 struct int_elf32_fdpic_loadmap
{
84 /* Protocol version number, must be zero. */
86 /* Number of segments in this map. */
88 /* The actual memory map. */
89 struct int_elf32_fdpic_loadseg segs
[1 /* nsegs, actually */];
92 /* Given address LDMADDR, fetch and decode the loadmap at that address.
93 Return NULL if there is a problem reading the target memory or if
94 there doesn't appear to be a loadmap at the given address. The
95 allocated space (representing the loadmap) returned by this
96 function may be freed via a single call to xfree(). */
98 static struct int_elf32_fdpic_loadmap
*
99 fetch_loadmap (CORE_ADDR ldmaddr
)
101 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
102 struct ext_elf32_fdpic_loadmap ext_ldmbuf_partial
;
103 struct ext_elf32_fdpic_loadmap
*ext_ldmbuf
;
104 struct int_elf32_fdpic_loadmap
*int_ldmbuf
;
105 int ext_ldmbuf_size
, int_ldmbuf_size
;
106 int version
, seg
, nsegs
;
108 /* Fetch initial portion of the loadmap. */
109 if (target_read_memory (ldmaddr
, (gdb_byte
*) &ext_ldmbuf_partial
,
110 sizeof ext_ldmbuf_partial
))
112 /* Problem reading the target's memory. */
116 /* Extract the version. */
117 version
= extract_unsigned_integer (ext_ldmbuf_partial
.version
,
118 sizeof ext_ldmbuf_partial
.version
,
122 /* We only handle version 0. */
126 /* Extract the number of segments. */
127 nsegs
= extract_unsigned_integer (ext_ldmbuf_partial
.nsegs
,
128 sizeof ext_ldmbuf_partial
.nsegs
,
134 /* Allocate space for the complete (external) loadmap. */
135 ext_ldmbuf_size
= sizeof (struct ext_elf32_fdpic_loadmap
)
136 + (nsegs
- 1) * sizeof (struct ext_elf32_fdpic_loadseg
);
137 ext_ldmbuf
= xmalloc (ext_ldmbuf_size
);
139 /* Copy over the portion of the loadmap that's already been read. */
140 memcpy (ext_ldmbuf
, &ext_ldmbuf_partial
, sizeof ext_ldmbuf_partial
);
142 /* Read the rest of the loadmap from the target. */
143 if (target_read_memory (ldmaddr
+ sizeof ext_ldmbuf_partial
,
144 (gdb_byte
*) ext_ldmbuf
+ sizeof ext_ldmbuf_partial
,
145 ext_ldmbuf_size
- sizeof ext_ldmbuf_partial
))
147 /* Couldn't read rest of the loadmap. */
152 /* Allocate space into which to put information extract from the
153 external loadsegs. I.e, allocate the internal loadsegs. */
154 int_ldmbuf_size
= sizeof (struct int_elf32_fdpic_loadmap
)
155 + (nsegs
- 1) * sizeof (struct int_elf32_fdpic_loadseg
);
156 int_ldmbuf
= xmalloc (int_ldmbuf_size
);
158 /* Place extracted information in internal structs. */
159 int_ldmbuf
->version
= version
;
160 int_ldmbuf
->nsegs
= nsegs
;
161 for (seg
= 0; seg
< nsegs
; seg
++)
163 int_ldmbuf
->segs
[seg
].addr
164 = extract_unsigned_integer (ext_ldmbuf
->segs
[seg
].addr
,
165 sizeof (ext_ldmbuf
->segs
[seg
].addr
),
167 int_ldmbuf
->segs
[seg
].p_vaddr
168 = extract_unsigned_integer (ext_ldmbuf
->segs
[seg
].p_vaddr
,
169 sizeof (ext_ldmbuf
->segs
[seg
].p_vaddr
),
171 int_ldmbuf
->segs
[seg
].p_memsz
172 = extract_unsigned_integer (ext_ldmbuf
->segs
[seg
].p_memsz
,
173 sizeof (ext_ldmbuf
->segs
[seg
].p_memsz
),
181 /* External link_map and elf32_fdpic_loadaddr struct definitions. */
183 typedef gdb_byte ext_ptr
[4];
185 struct ext_elf32_fdpic_loadaddr
187 ext_ptr map
; /* struct elf32_fdpic_loadmap *map; */
188 ext_ptr got_value
; /* void *got_value; */
193 struct ext_elf32_fdpic_loadaddr l_addr
;
195 /* Absolute file name object was found in. */
196 ext_ptr l_name
; /* char *l_name; */
198 /* Dynamic section of the shared object. */
199 ext_ptr l_ld
; /* ElfW(Dyn) *l_ld; */
201 /* Chain of loaded objects. */
202 ext_ptr l_next
, l_prev
; /* struct link_map *l_next, *l_prev; */
205 /* Link map info to include in an allocated so_list entry. */
209 /* The loadmap, digested into an easier to use form. */
210 struct int_elf32_fdpic_loadmap
*map
;
211 /* The GOT address for this link map entry. */
213 /* The link map address, needed for frv_fetch_objfile_link_map(). */
216 /* Cached dynamic symbol table and dynamic relocs initialized and
217 used only by find_canonical_descriptor_in_load_object().
219 Note: kevinb/2004-02-26: It appears that calls to
220 bfd_canonicalize_dynamic_reloc() will use the same symbols as
221 those supplied to the first call to this function. Therefore,
222 it's important to NOT free the asymbol ** data structure
223 supplied to the first call. Thus the caching of the dynamic
224 symbols (dyn_syms) is critical for correct operation. The
225 caching of the dynamic relocations could be dispensed with. */
227 arelent
**dyn_relocs
;
228 int dyn_reloc_count
; /* Number of dynamic relocs. */
232 /* The load map, got value, etc. are not available from the chain
233 of loaded shared objects. ``main_executable_lm_info'' provides
234 a way to get at this information so that it doesn't need to be
235 frequently recomputed. Initialized by frv_relocate_main_executable(). */
236 static struct lm_info
*main_executable_lm_info
;
238 static void frv_relocate_main_executable (void);
239 static CORE_ADDR
main_got (void);
240 static int enable_break2 (void);
242 /* Lookup the value for a specific symbol.
244 An expensive way to lookup the value of a single symbol for
245 bfd's that are only temporary anyway. This is used by the
246 shared library support to find the address of the debugger
247 interface structures in the shared library.
249 Note that 0 is specifically allowed as an error return (no
253 bfd_lookup_symbol (bfd
*abfd
, char *symname
)
257 asymbol
**symbol_table
;
258 unsigned int number_of_symbols
;
260 struct cleanup
*back_to
;
261 CORE_ADDR symaddr
= 0;
263 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
265 if (storage_needed
> 0)
267 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
268 back_to
= make_cleanup (xfree
, symbol_table
);
269 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
271 for (i
= 0; i
< number_of_symbols
; i
++)
273 sym
= *symbol_table
++;
274 if (strcmp (sym
->name
, symname
) == 0)
276 /* Bfd symbols are section relative. */
277 symaddr
= sym
->value
+ sym
->section
->vma
;
281 do_cleanups (back_to
);
287 /* Look for the symbol in the dynamic string table too. */
289 storage_needed
= bfd_get_dynamic_symtab_upper_bound (abfd
);
291 if (storage_needed
> 0)
293 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
294 back_to
= make_cleanup (xfree
, symbol_table
);
295 number_of_symbols
= bfd_canonicalize_dynamic_symtab (abfd
, symbol_table
);
297 for (i
= 0; i
< number_of_symbols
; i
++)
299 sym
= *symbol_table
++;
300 if (strcmp (sym
->name
, symname
) == 0)
302 /* Bfd symbols are section relative. */
303 symaddr
= sym
->value
+ sym
->section
->vma
;
307 do_cleanups (back_to
);
313 /* Implement the "open_symbol_file_object" target_so_ops method. */
316 open_symbol_file_object (void *from_ttyp
)
322 /* Cached value for lm_base(), below. */
323 static CORE_ADDR lm_base_cache
= 0;
325 /* Link map address for main module. */
326 static CORE_ADDR main_lm_addr
= 0;
328 /* Return the address from which the link map chain may be found. On
329 the FR-V, this may be found in a number of ways. Assuming that the
330 main executable has already been relocated, the easiest way to find
331 this value is to look up the address of _GLOBAL_OFFSET_TABLE_. A
332 pointer to the start of the link map will be located at the word found
333 at _GLOBAL_OFFSET_TABLE_ + 8. (This is part of the dynamic linker
334 reserve area mandated by the ABI.) */
339 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
340 struct minimal_symbol
*got_sym
;
342 gdb_byte buf
[FRV_PTR_SIZE
];
344 /* One of our assumptions is that the main executable has been relocated.
345 Bail out if this has not happened. (Note that post_create_inferior()
346 in infcmd.c will call solib_add prior to solib_create_inferior_hook().
347 If we allow this to happen, lm_base_cache will be initialized with
349 if (main_executable_lm_info
== 0)
352 /* If we already have a cached value, return it. */
354 return lm_base_cache
;
356 got_sym
= lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL
,
361 fprintf_unfiltered (gdb_stdlog
,
362 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
366 addr
= SYMBOL_VALUE_ADDRESS (got_sym
) + 8;
369 fprintf_unfiltered (gdb_stdlog
,
370 "lm_base: _GLOBAL_OFFSET_TABLE_ + 8 = %s\n",
371 hex_string_custom (addr
, 8));
373 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
375 lm_base_cache
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
378 fprintf_unfiltered (gdb_stdlog
,
379 "lm_base: lm_base_cache = %s\n",
380 hex_string_custom (lm_base_cache
, 8));
382 return lm_base_cache
;
386 /* Implement the "current_sos" target_so_ops method. */
388 static struct so_list
*
389 frv_current_sos (void)
391 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
392 CORE_ADDR lm_addr
, mgot
;
393 struct so_list
*sos_head
= NULL
;
394 struct so_list
**sos_next_ptr
= &sos_head
;
396 /* Make sure that the main executable has been relocated. This is
397 required in order to find the address of the global offset table,
398 which in turn is used to find the link map info. (See lm_base()
401 Note that the relocation of the main executable is also performed
402 by SOLIB_CREATE_INFERIOR_HOOK(), however, in the case of core
403 files, this hook is called too late in order to be of benefit to
404 SOLIB_ADD. SOLIB_ADD eventually calls this this function,
405 frv_current_sos, and also precedes the call to
406 SOLIB_CREATE_INFERIOR_HOOK(). (See post_create_inferior() in
408 if (main_executable_lm_info
== 0 && core_bfd
!= NULL
)
409 frv_relocate_main_executable ();
411 /* Fetch the GOT corresponding to the main executable. */
414 /* Locate the address of the first link map struct. */
415 lm_addr
= lm_base ();
417 /* We have at least one link map entry. Fetch the lot of them,
418 building the solist chain. */
421 struct ext_link_map lm_buf
;
425 fprintf_unfiltered (gdb_stdlog
,
426 "current_sos: reading link_map entry at %s\n",
427 hex_string_custom (lm_addr
, 8));
429 if (target_read_memory (lm_addr
, (gdb_byte
*) &lm_buf
,
430 sizeof (lm_buf
)) != 0)
432 warning (_("frv_current_sos: Unable to read link map entry. "
433 "Shared object chain may be incomplete."));
438 = extract_unsigned_integer (lm_buf
.l_addr
.got_value
,
439 sizeof (lm_buf
.l_addr
.got_value
),
441 /* If the got_addr is the same as mgotr, then we're looking at the
442 entry for the main executable. By convention, we don't include
443 this in the list of shared objects. */
444 if (got_addr
!= mgot
)
448 struct int_elf32_fdpic_loadmap
*loadmap
;
452 /* Fetch the load map address. */
453 addr
= extract_unsigned_integer (lm_buf
.l_addr
.map
,
454 sizeof lm_buf
.l_addr
.map
,
456 loadmap
= fetch_loadmap (addr
);
459 warning (_("frv_current_sos: Unable to fetch load map. "
460 "Shared object chain may be incomplete."));
464 sop
= xcalloc (1, sizeof (struct so_list
));
465 sop
->lm_info
= xcalloc (1, sizeof (struct lm_info
));
466 sop
->lm_info
->map
= loadmap
;
467 sop
->lm_info
->got_value
= got_addr
;
468 sop
->lm_info
->lm_addr
= lm_addr
;
469 /* Fetch the name. */
470 addr
= extract_unsigned_integer (lm_buf
.l_name
,
471 sizeof (lm_buf
.l_name
),
473 target_read_string (addr
, &name_buf
, SO_NAME_MAX_PATH_SIZE
- 1,
477 fprintf_unfiltered (gdb_stdlog
, "current_sos: name = %s\n",
481 warning (_("Can't read pathname for link map entry: %s."),
482 safe_strerror (errcode
));
485 strncpy (sop
->so_name
, name_buf
, SO_NAME_MAX_PATH_SIZE
- 1);
486 sop
->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
488 strcpy (sop
->so_original_name
, sop
->so_name
);
492 sos_next_ptr
= &sop
->next
;
496 main_lm_addr
= lm_addr
;
499 lm_addr
= extract_unsigned_integer (lm_buf
.l_next
,
500 sizeof (lm_buf
.l_next
), byte_order
);
509 /* Return 1 if PC lies in the dynamic symbol resolution code of the
512 static CORE_ADDR interp_text_sect_low
;
513 static CORE_ADDR interp_text_sect_high
;
514 static CORE_ADDR interp_plt_sect_low
;
515 static CORE_ADDR interp_plt_sect_high
;
518 frv_in_dynsym_resolve_code (CORE_ADDR pc
)
520 return ((pc
>= interp_text_sect_low
&& pc
< interp_text_sect_high
)
521 || (pc
>= interp_plt_sect_low
&& pc
< interp_plt_sect_high
)
522 || in_plt_section (pc
, NULL
));
525 /* Given a loadmap and an address, return the displacement needed
526 to relocate the address. */
529 displacement_from_map (struct int_elf32_fdpic_loadmap
*map
,
534 for (seg
= 0; seg
< map
->nsegs
; seg
++)
536 if (map
->segs
[seg
].p_vaddr
<= addr
537 && addr
< map
->segs
[seg
].p_vaddr
+ map
->segs
[seg
].p_memsz
)
539 return map
->segs
[seg
].addr
- map
->segs
[seg
].p_vaddr
;
546 /* Print a warning about being unable to set the dynamic linker
550 enable_break_failure_warning (void)
552 warning (_("Unable to find dynamic linker breakpoint function.\n"
553 "GDB will be unable to debug shared library initializers\n"
554 "and track explicitly loaded dynamic code."));
557 /* Arrange for dynamic linker to hit breakpoint.
559 The dynamic linkers has, as part of its debugger interface, support
560 for arranging for the inferior to hit a breakpoint after mapping in
561 the shared libraries. This function enables that breakpoint.
563 On the FR-V, using the shared library (FDPIC) ABI, the symbol
564 _dl_debug_addr points to the r_debug struct which contains
565 a field called r_brk. r_brk is the address of the function
566 descriptor upon which a breakpoint must be placed. Being a
567 function descriptor, we must extract the entry point in order
568 to set the breakpoint.
570 Our strategy will be to get the .interp section from the
571 executable. This section will provide us with the name of the
572 interpreter. We'll open the interpreter and then look up
573 the address of _dl_debug_addr. We then relocate this address
574 using the interpreter's loadmap. Once the relocated address
575 is known, we fetch the value (address) corresponding to r_brk
576 and then use that value to fetch the entry point of the function
577 we're interested in. */
579 static int enable_break2_done
= 0;
584 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
587 asection
*interp_sect
;
589 if (enable_break2_done
)
592 interp_text_sect_low
= interp_text_sect_high
= 0;
593 interp_plt_sect_low
= interp_plt_sect_high
= 0;
595 /* Find the .interp section; if not found, warn the user and drop
596 into the old breakpoint at symbol code. */
597 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
600 unsigned int interp_sect_size
;
604 CORE_ADDR addr
, interp_loadmap_addr
;
605 gdb_byte addr_buf
[FRV_PTR_SIZE
];
606 struct int_elf32_fdpic_loadmap
*ldm
;
607 volatile struct gdb_exception ex
;
609 /* Read the contents of the .interp section into a local buffer;
610 the contents specify the dynamic linker this program uses. */
611 interp_sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
612 buf
= alloca (interp_sect_size
);
613 bfd_get_section_contents (exec_bfd
, interp_sect
,
614 buf
, 0, interp_sect_size
);
616 /* Now we need to figure out where the dynamic linker was
617 loaded so that we can load its symbols and place a breakpoint
618 in the dynamic linker itself.
620 This address is stored on the stack. However, I've been unable
621 to find any magic formula to find it for Solaris (appears to
622 be trivial on GNU/Linux). Therefore, we have to try an alternate
623 mechanism to find the dynamic linker's base address. */
625 TRY_CATCH (ex
, RETURN_MASK_ALL
)
627 tmp_bfd
= solib_bfd_open (buf
);
631 enable_break_failure_warning ();
635 status
= frv_fdpic_loadmap_addresses (target_gdbarch
,
636 &interp_loadmap_addr
, 0);
639 warning (_("Unable to determine dynamic linker loadmap address."));
640 enable_break_failure_warning ();
646 fprintf_unfiltered (gdb_stdlog
,
647 "enable_break: interp_loadmap_addr = %s\n",
648 hex_string_custom (interp_loadmap_addr
, 8));
650 ldm
= fetch_loadmap (interp_loadmap_addr
);
653 warning (_("Unable to load dynamic linker loadmap at address %s."),
654 hex_string_custom (interp_loadmap_addr
, 8));
655 enable_break_failure_warning ();
660 /* Record the relocated start and end address of the dynamic linker
661 text and plt section for svr4_in_dynsym_resolve_code. */
662 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
666 = bfd_section_vma (tmp_bfd
, interp_sect
);
668 += displacement_from_map (ldm
, interp_text_sect_low
);
669 interp_text_sect_high
670 = interp_text_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
672 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
675 interp_plt_sect_low
=
676 bfd_section_vma (tmp_bfd
, interp_sect
);
678 += displacement_from_map (ldm
, interp_plt_sect_low
);
679 interp_plt_sect_high
=
680 interp_plt_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
683 addr
= bfd_lookup_symbol (tmp_bfd
, "_dl_debug_addr");
686 warning (_("Could not find symbol _dl_debug_addr "
687 "in dynamic linker"));
688 enable_break_failure_warning ();
694 fprintf_unfiltered (gdb_stdlog
,
695 "enable_break: _dl_debug_addr "
696 "(prior to relocation) = %s\n",
697 hex_string_custom (addr
, 8));
699 addr
+= displacement_from_map (ldm
, addr
);
702 fprintf_unfiltered (gdb_stdlog
,
703 "enable_break: _dl_debug_addr "
704 "(after relocation) = %s\n",
705 hex_string_custom (addr
, 8));
707 /* Fetch the address of the r_debug struct. */
708 if (target_read_memory (addr
, addr_buf
, sizeof addr_buf
) != 0)
710 warning (_("Unable to fetch contents of _dl_debug_addr "
711 "(at address %s) from dynamic linker"),
712 hex_string_custom (addr
, 8));
714 addr
= extract_unsigned_integer (addr_buf
, sizeof addr_buf
, byte_order
);
717 fprintf_unfiltered (gdb_stdlog
,
718 "enable_break: _dl_debug_addr[0..3] = %s\n",
719 hex_string_custom (addr
, 8));
721 /* If it's zero, then the ldso hasn't initialized yet, and so
722 there are no shared libs yet loaded. */
726 fprintf_unfiltered (gdb_stdlog
,
727 "enable_break: ldso not yet initialized\n");
728 /* Do not warn, but mark to run again. */
732 /* Fetch the r_brk field. It's 8 bytes from the start of
734 if (target_read_memory (addr
+ 8, addr_buf
, sizeof addr_buf
) != 0)
736 warning (_("Unable to fetch _dl_debug_addr->r_brk "
737 "(at address %s) from dynamic linker"),
738 hex_string_custom (addr
+ 8, 8));
739 enable_break_failure_warning ();
743 addr
= extract_unsigned_integer (addr_buf
, sizeof addr_buf
, byte_order
);
745 /* Now fetch the function entry point. */
746 if (target_read_memory (addr
, addr_buf
, sizeof addr_buf
) != 0)
748 warning (_("Unable to fetch _dl_debug_addr->.r_brk entry point "
749 "(at address %s) from dynamic linker"),
750 hex_string_custom (addr
, 8));
751 enable_break_failure_warning ();
755 addr
= extract_unsigned_integer (addr_buf
, sizeof addr_buf
, byte_order
);
757 /* We're done with the temporary bfd. */
760 /* We're also done with the loadmap. */
763 /* Remove all the solib event breakpoints. Their addresses
764 may have changed since the last time we ran the program. */
765 remove_solib_event_breakpoints ();
767 /* Now (finally!) create the solib breakpoint. */
768 create_solib_event_breakpoint (target_gdbarch
, addr
);
770 enable_break2_done
= 1;
775 /* Tell the user we couldn't set a dynamic linker breakpoint. */
776 enable_break_failure_warning ();
778 /* Failure return. */
785 asection
*interp_sect
;
787 if (symfile_objfile
== NULL
)
790 fprintf_unfiltered (gdb_stdlog
,
791 "enable_break: No symbol file found.\n");
795 if (!symfile_objfile
->ei
.entry_point_p
)
798 fprintf_unfiltered (gdb_stdlog
,
799 "enable_break: Symbol file has no entry point.\n");
803 /* Check for the presence of a .interp section. If there is no
804 such section, the executable is statically linked. */
806 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
808 if (interp_sect
== NULL
)
811 fprintf_unfiltered (gdb_stdlog
,
812 "enable_break: No .interp section found.\n");
816 create_solib_event_breakpoint (target_gdbarch
,
817 symfile_objfile
->ei
.entry_point
);
820 fprintf_unfiltered (gdb_stdlog
,
821 "enable_break: solib event breakpoint "
822 "placed at entry point: %s\n",
823 hex_string_custom (symfile_objfile
->ei
.entry_point
,
828 /* Implement the "special_symbol_handling" target_so_ops method. */
831 frv_special_symbol_handling (void)
833 /* Nothing needed for FRV. */
837 frv_relocate_main_executable (void)
840 CORE_ADDR exec_addr
, interp_addr
;
841 struct int_elf32_fdpic_loadmap
*ldm
;
842 struct cleanup
*old_chain
;
843 struct section_offsets
*new_offsets
;
845 struct obj_section
*osect
;
847 status
= frv_fdpic_loadmap_addresses (target_gdbarch
,
848 &interp_addr
, &exec_addr
);
850 if (status
< 0 || (exec_addr
== 0 && interp_addr
== 0))
852 /* Not using FDPIC ABI, so do nothing. */
856 /* Fetch the loadmap located at ``exec_addr''. */
857 ldm
= fetch_loadmap (exec_addr
);
859 error (_("Unable to load the executable's loadmap."));
861 if (main_executable_lm_info
)
862 xfree (main_executable_lm_info
);
863 main_executable_lm_info
= xcalloc (1, sizeof (struct lm_info
));
864 main_executable_lm_info
->map
= ldm
;
866 new_offsets
= xcalloc (symfile_objfile
->num_sections
,
867 sizeof (struct section_offsets
));
868 old_chain
= make_cleanup (xfree
, new_offsets
);
871 ALL_OBJFILE_OSECTIONS (symfile_objfile
, osect
)
873 CORE_ADDR orig_addr
, addr
, offset
;
877 osect_idx
= osect
->the_bfd_section
->index
;
879 /* Current address of section. */
880 addr
= obj_section_addr (osect
);
881 /* Offset from where this section started. */
882 offset
= ANOFFSET (symfile_objfile
->section_offsets
, osect_idx
);
883 /* Original address prior to any past relocations. */
884 orig_addr
= addr
- offset
;
886 for (seg
= 0; seg
< ldm
->nsegs
; seg
++)
888 if (ldm
->segs
[seg
].p_vaddr
<= orig_addr
889 && orig_addr
< ldm
->segs
[seg
].p_vaddr
+ ldm
->segs
[seg
].p_memsz
)
891 new_offsets
->offsets
[osect_idx
]
892 = ldm
->segs
[seg
].addr
- ldm
->segs
[seg
].p_vaddr
;
894 if (new_offsets
->offsets
[osect_idx
] != offset
)
902 objfile_relocate (symfile_objfile
, new_offsets
);
904 do_cleanups (old_chain
);
906 /* Now that symfile_objfile has been relocated, we can compute the
907 GOT value and stash it away. */
908 main_executable_lm_info
->got_value
= main_got ();
911 /* Implement the "create_inferior_hook" target_solib_ops method.
913 For the FR-V shared library ABI (FDPIC), the main executable needs
914 to be relocated. The shared library breakpoints also need to be
918 frv_solib_create_inferior_hook (int from_tty
)
920 /* Relocate main executable. */
921 frv_relocate_main_executable ();
923 /* Enable shared library breakpoints. */
924 if (!enable_break ())
926 warning (_("shared library handler failed to enable breakpoint"));
932 frv_clear_solib (void)
935 enable_break2_done
= 0;
937 if (main_executable_lm_info
!= 0)
939 xfree (main_executable_lm_info
->map
);
940 xfree (main_executable_lm_info
->dyn_syms
);
941 xfree (main_executable_lm_info
->dyn_relocs
);
942 xfree (main_executable_lm_info
);
943 main_executable_lm_info
= 0;
948 frv_free_so (struct so_list
*so
)
950 xfree (so
->lm_info
->map
);
951 xfree (so
->lm_info
->dyn_syms
);
952 xfree (so
->lm_info
->dyn_relocs
);
957 frv_relocate_section_addresses (struct so_list
*so
,
958 struct target_section
*sec
)
961 struct int_elf32_fdpic_loadmap
*map
;
963 map
= so
->lm_info
->map
;
965 for (seg
= 0; seg
< map
->nsegs
; seg
++)
967 if (map
->segs
[seg
].p_vaddr
<= sec
->addr
968 && sec
->addr
< map
->segs
[seg
].p_vaddr
+ map
->segs
[seg
].p_memsz
)
970 CORE_ADDR displ
= map
->segs
[seg
].addr
- map
->segs
[seg
].p_vaddr
;
973 sec
->endaddr
+= displ
;
979 /* Return the GOT address associated with the main executable. Return
980 0 if it can't be found. */
985 struct minimal_symbol
*got_sym
;
987 got_sym
= lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_",
988 NULL
, symfile_objfile
);
992 return SYMBOL_VALUE_ADDRESS (got_sym
);
995 /* Find the global pointer for the given function address ADDR. */
998 frv_fdpic_find_global_pointer (CORE_ADDR addr
)
1002 so
= master_so_list ();
1006 struct int_elf32_fdpic_loadmap
*map
;
1008 map
= so
->lm_info
->map
;
1010 for (seg
= 0; seg
< map
->nsegs
; seg
++)
1012 if (map
->segs
[seg
].addr
<= addr
1013 && addr
< map
->segs
[seg
].addr
+ map
->segs
[seg
].p_memsz
)
1014 return so
->lm_info
->got_value
;
1020 /* Didn't find it in any of the shared objects. So assume it's in the
1025 /* Forward declarations for frv_fdpic_find_canonical_descriptor(). */
1026 static CORE_ADDR find_canonical_descriptor_in_load_object
1027 (CORE_ADDR
, CORE_ADDR
, char *, bfd
*, struct lm_info
*);
1029 /* Given a function entry point, attempt to find the canonical descriptor
1030 associated with that entry point. Return 0 if no canonical descriptor
1034 frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point
)
1038 CORE_ADDR got_value
;
1039 struct int_elf32_fdpic_loadmap
*ldm
= 0;
1042 CORE_ADDR exec_loadmap_addr
;
1044 /* Fetch the corresponding global pointer for the entry point. */
1045 got_value
= frv_fdpic_find_global_pointer (entry_point
);
1047 /* Attempt to find the name of the function. If the name is available,
1048 it'll be used as an aid in finding matching functions in the dynamic
1050 sym
= find_pc_function (entry_point
);
1054 name
= SYMBOL_LINKAGE_NAME (sym
);
1056 /* Check the main executable. */
1057 addr
= find_canonical_descriptor_in_load_object
1058 (entry_point
, got_value
, name
, symfile_objfile
->obfd
,
1059 main_executable_lm_info
);
1061 /* If descriptor not found via main executable, check each load object
1062 in list of shared objects. */
1067 so
= master_so_list ();
1070 addr
= find_canonical_descriptor_in_load_object
1071 (entry_point
, got_value
, name
, so
->abfd
, so
->lm_info
);
1084 find_canonical_descriptor_in_load_object
1085 (CORE_ADDR entry_point
, CORE_ADDR got_value
, char *name
, bfd
*abfd
,
1088 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
1093 /* Nothing to do if no bfd. */
1097 /* Nothing to do if no link map. */
1101 /* We want to scan the dynamic relocs for R_FRV_FUNCDESC relocations.
1102 (More about this later.) But in order to fetch the relocs, we
1103 need to first fetch the dynamic symbols. These symbols need to
1104 be cached due to the way that bfd_canonicalize_dynamic_reloc()
1105 works. (See the comments in the declaration of struct lm_info
1106 for more information.) */
1107 if (lm
->dyn_syms
== NULL
)
1109 long storage_needed
;
1110 unsigned int number_of_symbols
;
1112 /* Determine amount of space needed to hold the dynamic symbol table. */
1113 storage_needed
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1115 /* If there are no dynamic symbols, there's nothing to do. */
1116 if (storage_needed
<= 0)
1119 /* Allocate space for the dynamic symbol table. */
1120 lm
->dyn_syms
= (asymbol
**) xmalloc (storage_needed
);
1122 /* Fetch the dynamic symbol table. */
1123 number_of_symbols
= bfd_canonicalize_dynamic_symtab (abfd
, lm
->dyn_syms
);
1125 if (number_of_symbols
== 0)
1129 /* Fetch the dynamic relocations if not already cached. */
1130 if (lm
->dyn_relocs
== NULL
)
1132 long storage_needed
;
1134 /* Determine amount of space needed to hold the dynamic relocs. */
1135 storage_needed
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1137 /* Bail out if there are no dynamic relocs. */
1138 if (storage_needed
<= 0)
1141 /* Allocate space for the relocs. */
1142 lm
->dyn_relocs
= (arelent
**) xmalloc (storage_needed
);
1144 /* Fetch the dynamic relocs. */
1146 = bfd_canonicalize_dynamic_reloc (abfd
, lm
->dyn_relocs
, lm
->dyn_syms
);
1149 /* Search the dynamic relocs. */
1150 for (i
= 0; i
< lm
->dyn_reloc_count
; i
++)
1152 rel
= lm
->dyn_relocs
[i
];
1154 /* Relocs of interest are those which meet the following
1157 - the names match (assuming the caller could provide
1158 a name which matches ``entry_point'').
1159 - the relocation type must be R_FRV_FUNCDESC. Relocs
1160 of this type are used (by the dynamic linker) to
1161 look up the address of a canonical descriptor (allocating
1162 it if need be) and initializing the GOT entry referred
1163 to by the offset to the address of the descriptor.
1165 These relocs of interest may be used to obtain a
1166 candidate descriptor by first adjusting the reloc's
1167 address according to the link map and then dereferencing
1168 this address (which is a GOT entry) to obtain a descriptor
1170 if ((name
== 0 || strcmp (name
, (*rel
->sym_ptr_ptr
)->name
) == 0)
1171 && rel
->howto
->type
== R_FRV_FUNCDESC
)
1173 gdb_byte buf
[FRV_PTR_SIZE
];
1175 /* Compute address of address of candidate descriptor. */
1176 addr
= rel
->address
+ displacement_from_map (lm
->map
, rel
->address
);
1178 /* Fetch address of candidate descriptor. */
1179 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
1181 addr
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
1183 /* Check for matching entry point. */
1184 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
1186 if (extract_unsigned_integer (buf
, sizeof buf
, byte_order
)
1190 /* Check for matching got value. */
1191 if (target_read_memory (addr
+ 4, buf
, sizeof buf
) != 0)
1193 if (extract_unsigned_integer (buf
, sizeof buf
, byte_order
)
1197 /* Match was successful! Exit loop. */
1205 /* Given an objfile, return the address of its link map. This value is
1206 needed for TLS support. */
1208 frv_fetch_objfile_link_map (struct objfile
*objfile
)
1212 /* Cause frv_current_sos() to be run if it hasn't been already. */
1213 if (main_lm_addr
== 0)
1214 solib_add (0, 0, 0, 1);
1216 /* frv_current_sos() will set main_lm_addr for the main executable. */
1217 if (objfile
== symfile_objfile
)
1218 return main_lm_addr
;
1220 /* The other link map addresses may be found by examining the list
1221 of shared libraries. */
1222 for (so
= master_so_list (); so
; so
= so
->next
)
1224 if (so
->objfile
== objfile
)
1225 return so
->lm_info
->lm_addr
;
1232 struct target_so_ops frv_so_ops
;
1234 /* Provide a prototype to silence -Wmissing-prototypes. */
1235 extern initialize_file_ftype _initialize_frv_solib
;
1238 _initialize_frv_solib (void)
1240 frv_so_ops
.relocate_section_addresses
= frv_relocate_section_addresses
;
1241 frv_so_ops
.free_so
= frv_free_so
;
1242 frv_so_ops
.clear_solib
= frv_clear_solib
;
1243 frv_so_ops
.solib_create_inferior_hook
= frv_solib_create_inferior_hook
;
1244 frv_so_ops
.special_symbol_handling
= frv_special_symbol_handling
;
1245 frv_so_ops
.current_sos
= frv_current_sos
;
1246 frv_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
1247 frv_so_ops
.in_dynsym_resolve_code
= frv_in_dynsym_resolve_code
;
1248 frv_so_ops
.bfd_open
= solib_bfd_open
;
1250 /* Debug this file's internals. */
1251 add_setshow_zinteger_cmd ("solib-frv", class_maintenance
,
1252 &solib_frv_debug
, _("\
1253 Set internal debugging of shared library code for FR-V."), _("\
1254 Show internal debugging of shared library code for FR-V."), _("\
1255 When non-zero, FR-V solib specific internal debugging is enabled."),
1257 NULL
, /* FIXME: i18n: */
1258 &setdebuglist
, &showdebuglist
);