1 /* Handle FR-V (FDPIC) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
34 /* FR-V pointers are four bytes wide. */
35 enum { FRV_PTR_SIZE
= 4 };
37 /* Representation of loadmap and related structs for the FR-V FDPIC ABI. */
39 /* External versions; the size and alignment of the fields should be
40 the same as those on the target. When loaded, the placement of
41 the bits in each field will be the same as on the target. */
42 typedef gdb_byte ext_Elf32_Half
[2];
43 typedef gdb_byte ext_Elf32_Addr
[4];
44 typedef gdb_byte ext_Elf32_Word
[4];
46 struct ext_elf32_fdpic_loadseg
48 /* Core address to which the segment is mapped. */
50 /* VMA recorded in the program header. */
51 ext_Elf32_Addr p_vaddr
;
52 /* Size of this segment in memory. */
53 ext_Elf32_Word p_memsz
;
56 struct ext_elf32_fdpic_loadmap
{
57 /* Protocol version number, must be zero. */
58 ext_Elf32_Half version
;
59 /* Number of segments in this map. */
61 /* The actual memory map. */
62 struct ext_elf32_fdpic_loadseg segs
[1 /* nsegs, actually */];
65 /* Internal versions; the types are GDB types and the data in each
66 of the fields is (or will be) decoded from the external struct
67 for ease of consumption. */
68 struct int_elf32_fdpic_loadseg
70 /* Core address to which the segment is mapped. */
72 /* VMA recorded in the program header. */
74 /* Size of this segment in memory. */
78 struct int_elf32_fdpic_loadmap
{
79 /* Protocol version number, must be zero. */
81 /* Number of segments in this map. */
83 /* The actual memory map. */
84 struct int_elf32_fdpic_loadseg segs
[1 /* nsegs, actually */];
87 /* Given address LDMADDR, fetch and decode the loadmap at that address.
88 Return NULL if there is a problem reading the target memory or if
89 there doesn't appear to be a loadmap at the given address. The
90 allocated space (representing the loadmap) returned by this
91 function may be freed via a single call to xfree(). */
93 static struct int_elf32_fdpic_loadmap
*
94 fetch_loadmap (CORE_ADDR ldmaddr
)
96 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
97 struct ext_elf32_fdpic_loadmap ext_ldmbuf_partial
;
98 struct ext_elf32_fdpic_loadmap
*ext_ldmbuf
;
99 struct int_elf32_fdpic_loadmap
*int_ldmbuf
;
100 int ext_ldmbuf_size
, int_ldmbuf_size
;
101 int version
, seg
, nsegs
;
103 /* Fetch initial portion of the loadmap. */
104 if (target_read_memory (ldmaddr
, (gdb_byte
*) &ext_ldmbuf_partial
,
105 sizeof ext_ldmbuf_partial
))
107 /* Problem reading the target's memory. */
111 /* Extract the version. */
112 version
= extract_unsigned_integer (ext_ldmbuf_partial
.version
,
113 sizeof ext_ldmbuf_partial
.version
,
117 /* We only handle version 0. */
121 /* Extract the number of segments. */
122 nsegs
= extract_unsigned_integer (ext_ldmbuf_partial
.nsegs
,
123 sizeof ext_ldmbuf_partial
.nsegs
,
129 /* Allocate space for the complete (external) loadmap. */
130 ext_ldmbuf_size
= sizeof (struct ext_elf32_fdpic_loadmap
)
131 + (nsegs
- 1) * sizeof (struct ext_elf32_fdpic_loadseg
);
132 ext_ldmbuf
= (struct ext_elf32_fdpic_loadmap
*) xmalloc (ext_ldmbuf_size
);
134 /* Copy over the portion of the loadmap that's already been read. */
135 memcpy (ext_ldmbuf
, &ext_ldmbuf_partial
, sizeof ext_ldmbuf_partial
);
137 /* Read the rest of the loadmap from the target. */
138 if (target_read_memory (ldmaddr
+ sizeof ext_ldmbuf_partial
,
139 (gdb_byte
*) ext_ldmbuf
+ sizeof ext_ldmbuf_partial
,
140 ext_ldmbuf_size
- sizeof ext_ldmbuf_partial
))
142 /* Couldn't read rest of the loadmap. */
147 /* Allocate space into which to put information extract from the
148 external loadsegs. I.e, allocate the internal loadsegs. */
149 int_ldmbuf_size
= sizeof (struct int_elf32_fdpic_loadmap
)
150 + (nsegs
- 1) * sizeof (struct int_elf32_fdpic_loadseg
);
151 int_ldmbuf
= (struct int_elf32_fdpic_loadmap
*) xmalloc (int_ldmbuf_size
);
153 /* Place extracted information in internal structs. */
154 int_ldmbuf
->version
= version
;
155 int_ldmbuf
->nsegs
= nsegs
;
156 for (seg
= 0; seg
< nsegs
; seg
++)
158 int_ldmbuf
->segs
[seg
].addr
159 = extract_unsigned_integer (ext_ldmbuf
->segs
[seg
].addr
,
160 sizeof (ext_ldmbuf
->segs
[seg
].addr
),
162 int_ldmbuf
->segs
[seg
].p_vaddr
163 = extract_unsigned_integer (ext_ldmbuf
->segs
[seg
].p_vaddr
,
164 sizeof (ext_ldmbuf
->segs
[seg
].p_vaddr
),
166 int_ldmbuf
->segs
[seg
].p_memsz
167 = extract_unsigned_integer (ext_ldmbuf
->segs
[seg
].p_memsz
,
168 sizeof (ext_ldmbuf
->segs
[seg
].p_memsz
),
176 /* External link_map and elf32_fdpic_loadaddr struct definitions. */
178 typedef gdb_byte ext_ptr
[4];
180 struct ext_elf32_fdpic_loadaddr
182 ext_ptr map
; /* struct elf32_fdpic_loadmap *map; */
183 ext_ptr got_value
; /* void *got_value; */
188 struct ext_elf32_fdpic_loadaddr l_addr
;
190 /* Absolute file name object was found in. */
191 ext_ptr l_name
; /* char *l_name; */
193 /* Dynamic section of the shared object. */
194 ext_ptr l_ld
; /* ElfW(Dyn) *l_ld; */
196 /* Chain of loaded objects. */
197 ext_ptr l_next
, l_prev
; /* struct link_map *l_next, *l_prev; */
200 /* Link map info to include in an allocated so_list entry. */
202 struct lm_info_frv
: public lm_info_base
207 xfree (this->dyn_syms
);
208 xfree (this->dyn_relocs
);
211 /* The loadmap, digested into an easier to use form. */
212 int_elf32_fdpic_loadmap
*map
= NULL
;
213 /* The GOT address for this link map entry. */
214 CORE_ADDR got_value
= 0;
215 /* The link map address, needed for frv_fetch_objfile_link_map(). */
216 CORE_ADDR lm_addr
= 0;
218 /* Cached dynamic symbol table and dynamic relocs initialized and
219 used only by find_canonical_descriptor_in_load_object().
221 Note: kevinb/2004-02-26: It appears that calls to
222 bfd_canonicalize_dynamic_reloc() will use the same symbols as
223 those supplied to the first call to this function. Therefore,
224 it's important to NOT free the asymbol ** data structure
225 supplied to the first call. Thus the caching of the dynamic
226 symbols (dyn_syms) is critical for correct operation. The
227 caching of the dynamic relocations could be dispensed with. */
228 asymbol
**dyn_syms
= NULL
;
229 arelent
**dyn_relocs
= NULL
;
230 int dyn_reloc_count
= 0; /* Number of dynamic relocs. */
233 /* The load map, got value, etc. are not available from the chain
234 of loaded shared objects. ``main_executable_lm_info'' provides
235 a way to get at this information so that it doesn't need to be
236 frequently recomputed. Initialized by frv_relocate_main_executable(). */
237 static lm_info_frv
*main_executable_lm_info
;
239 static void frv_relocate_main_executable (void);
240 static CORE_ADDR
main_got (void);
241 static int enable_break2 (void);
243 /* Implement the "open_symbol_file_object" target_so_ops method. */
246 open_symbol_file_object (int from_tty
)
252 /* Cached value for lm_base(), below. */
253 static CORE_ADDR lm_base_cache
= 0;
255 /* Link map address for main module. */
256 static CORE_ADDR main_lm_addr
= 0;
258 /* Return the address from which the link map chain may be found. On
259 the FR-V, this may be found in a number of ways. Assuming that the
260 main executable has already been relocated, the easiest way to find
261 this value is to look up the address of _GLOBAL_OFFSET_TABLE_. A
262 pointer to the start of the link map will be located at the word found
263 at _GLOBAL_OFFSET_TABLE_ + 8. (This is part of the dynamic linker
264 reserve area mandated by the ABI.) */
269 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
270 struct bound_minimal_symbol got_sym
;
272 gdb_byte buf
[FRV_PTR_SIZE
];
274 /* One of our assumptions is that the main executable has been relocated.
275 Bail out if this has not happened. (Note that post_create_inferior()
276 in infcmd.c will call solib_add prior to solib_create_inferior_hook().
277 If we allow this to happen, lm_base_cache will be initialized with
279 if (main_executable_lm_info
== 0)
282 /* If we already have a cached value, return it. */
284 return lm_base_cache
;
286 got_sym
= lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL
,
287 current_program_space
->symfile_object_file
);
288 if (got_sym
.minsym
== 0)
290 solib_debug_printf ("_GLOBAL_OFFSET_TABLE_ not found.");
294 addr
= got_sym
.value_address () + 8;
296 solib_debug_printf ("_GLOBAL_OFFSET_TABLE_ + 8 = %s",
297 hex_string_custom (addr
, 8));
299 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
301 lm_base_cache
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
303 solib_debug_printf ("lm_base_cache = %s",
304 hex_string_custom (lm_base_cache
, 8));
306 return lm_base_cache
;
310 /* Implement the "current_sos" target_so_ops method. */
312 static struct so_list
*
313 frv_current_sos (void)
315 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
316 CORE_ADDR lm_addr
, mgot
;
317 struct so_list
*sos_head
= NULL
;
318 struct so_list
**sos_next_ptr
= &sos_head
;
320 /* Make sure that the main executable has been relocated. This is
321 required in order to find the address of the global offset table,
322 which in turn is used to find the link map info. (See lm_base()
325 Note that the relocation of the main executable is also performed
326 by solib_create_inferior_hook(), however, in the case of core
327 files, this hook is called too late in order to be of benefit to
328 solib_add. solib_add eventually calls this this function,
329 frv_current_sos, and also precedes the call to
330 solib_create_inferior_hook(). (See post_create_inferior() in
332 if (main_executable_lm_info
== 0 && core_bfd
!= NULL
)
333 frv_relocate_main_executable ();
335 /* Fetch the GOT corresponding to the main executable. */
338 /* Locate the address of the first link map struct. */
339 lm_addr
= lm_base ();
341 /* We have at least one link map entry. Fetch the lot of them,
342 building the solist chain. */
345 struct ext_link_map lm_buf
;
348 solib_debug_printf ("reading link_map entry at %s",
349 hex_string_custom (lm_addr
, 8));
351 if (target_read_memory (lm_addr
, (gdb_byte
*) &lm_buf
,
352 sizeof (lm_buf
)) != 0)
354 warning (_("frv_current_sos: Unable to read link map entry. "
355 "Shared object chain may be incomplete."));
360 = extract_unsigned_integer (lm_buf
.l_addr
.got_value
,
361 sizeof (lm_buf
.l_addr
.got_value
),
363 /* If the got_addr is the same as mgotr, then we're looking at the
364 entry for the main executable. By convention, we don't include
365 this in the list of shared objects. */
366 if (got_addr
!= mgot
)
368 struct int_elf32_fdpic_loadmap
*loadmap
;
372 /* Fetch the load map address. */
373 addr
= extract_unsigned_integer (lm_buf
.l_addr
.map
,
374 sizeof lm_buf
.l_addr
.map
,
376 loadmap
= fetch_loadmap (addr
);
379 warning (_("frv_current_sos: Unable to fetch load map. "
380 "Shared object chain may be incomplete."));
384 sop
= XCNEW (struct so_list
);
385 lm_info_frv
*li
= new lm_info_frv
;
388 li
->got_value
= got_addr
;
389 li
->lm_addr
= lm_addr
;
390 /* Fetch the name. */
391 addr
= extract_unsigned_integer (lm_buf
.l_name
,
392 sizeof (lm_buf
.l_name
),
394 gdb::unique_xmalloc_ptr
<char> name_buf
395 = target_read_string (addr
, SO_NAME_MAX_PATH_SIZE
- 1);
397 solib_debug_printf ("name = %s", name_buf
.get ());
399 if (name_buf
== nullptr)
400 warning (_("Can't read pathname for link map entry."));
403 strncpy (sop
->so_name
, name_buf
.get (),
404 SO_NAME_MAX_PATH_SIZE
- 1);
405 sop
->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
406 strcpy (sop
->so_original_name
, sop
->so_name
);
410 sos_next_ptr
= &sop
->next
;
414 main_lm_addr
= lm_addr
;
417 lm_addr
= extract_unsigned_integer (lm_buf
.l_next
,
418 sizeof (lm_buf
.l_next
), byte_order
);
427 /* Return 1 if PC lies in the dynamic symbol resolution code of the
430 static CORE_ADDR interp_text_sect_low
;
431 static CORE_ADDR interp_text_sect_high
;
432 static CORE_ADDR interp_plt_sect_low
;
433 static CORE_ADDR interp_plt_sect_high
;
436 frv_in_dynsym_resolve_code (CORE_ADDR pc
)
438 return ((pc
>= interp_text_sect_low
&& pc
< interp_text_sect_high
)
439 || (pc
>= interp_plt_sect_low
&& pc
< interp_plt_sect_high
)
440 || in_plt_section (pc
));
443 /* Given a loadmap and an address, return the displacement needed
444 to relocate the address. */
447 displacement_from_map (struct int_elf32_fdpic_loadmap
*map
,
452 for (seg
= 0; seg
< map
->nsegs
; seg
++)
454 if (map
->segs
[seg
].p_vaddr
<= addr
455 && addr
< map
->segs
[seg
].p_vaddr
+ map
->segs
[seg
].p_memsz
)
457 return map
->segs
[seg
].addr
- map
->segs
[seg
].p_vaddr
;
464 /* Print a warning about being unable to set the dynamic linker
468 enable_break_failure_warning (void)
470 warning (_("Unable to find dynamic linker breakpoint function.\n"
471 "GDB will be unable to debug shared library initializers\n"
472 "and track explicitly loaded dynamic code."));
475 /* Arrange for dynamic linker to hit breakpoint.
477 The dynamic linkers has, as part of its debugger interface, support
478 for arranging for the inferior to hit a breakpoint after mapping in
479 the shared libraries. This function enables that breakpoint.
481 On the FR-V, using the shared library (FDPIC) ABI, the symbol
482 _dl_debug_addr points to the r_debug struct which contains
483 a field called r_brk. r_brk is the address of the function
484 descriptor upon which a breakpoint must be placed. Being a
485 function descriptor, we must extract the entry point in order
486 to set the breakpoint.
488 Our strategy will be to get the .interp section from the
489 executable. This section will provide us with the name of the
490 interpreter. We'll open the interpreter and then look up
491 the address of _dl_debug_addr. We then relocate this address
492 using the interpreter's loadmap. Once the relocated address
493 is known, we fetch the value (address) corresponding to r_brk
494 and then use that value to fetch the entry point of the function
495 we're interested in. */
497 static int enable_break2_done
= 0;
502 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
503 asection
*interp_sect
;
505 if (enable_break2_done
)
508 interp_text_sect_low
= interp_text_sect_high
= 0;
509 interp_plt_sect_low
= interp_plt_sect_high
= 0;
511 /* Find the .interp section; if not found, warn the user and drop
512 into the old breakpoint at symbol code. */
513 interp_sect
= bfd_get_section_by_name (current_program_space
->exec_bfd (),
517 unsigned int interp_sect_size
;
520 CORE_ADDR addr
, interp_loadmap_addr
;
521 gdb_byte addr_buf
[FRV_PTR_SIZE
];
522 struct int_elf32_fdpic_loadmap
*ldm
;
524 /* Read the contents of the .interp section into a local buffer;
525 the contents specify the dynamic linker this program uses. */
526 interp_sect_size
= bfd_section_size (interp_sect
);
527 buf
= (char *) alloca (interp_sect_size
);
528 bfd_get_section_contents (current_program_space
->exec_bfd (),
529 interp_sect
, buf
, 0, interp_sect_size
);
531 /* Now we need to figure out where the dynamic linker was
532 loaded so that we can load its symbols and place a breakpoint
533 in the dynamic linker itself.
535 This address is stored on the stack. However, I've been unable
536 to find any magic formula to find it for Solaris (appears to
537 be trivial on GNU/Linux). Therefore, we have to try an alternate
538 mechanism to find the dynamic linker's base address. */
540 gdb_bfd_ref_ptr tmp_bfd
;
543 tmp_bfd
= solib_bfd_open (buf
);
545 catch (const gdb_exception
&ex
)
551 enable_break_failure_warning ();
555 status
= frv_fdpic_loadmap_addresses (target_gdbarch (),
556 &interp_loadmap_addr
, 0);
559 warning (_("Unable to determine dynamic linker loadmap address."));
560 enable_break_failure_warning ();
564 solib_debug_printf ("interp_loadmap_addr = %s",
565 hex_string_custom (interp_loadmap_addr
, 8));
567 ldm
= fetch_loadmap (interp_loadmap_addr
);
570 warning (_("Unable to load dynamic linker loadmap at address %s."),
571 hex_string_custom (interp_loadmap_addr
, 8));
572 enable_break_failure_warning ();
576 /* Record the relocated start and end address of the dynamic linker
577 text and plt section for svr4_in_dynsym_resolve_code. */
578 interp_sect
= bfd_get_section_by_name (tmp_bfd
.get (), ".text");
581 interp_text_sect_low
= bfd_section_vma (interp_sect
);
583 += displacement_from_map (ldm
, interp_text_sect_low
);
584 interp_text_sect_high
585 = interp_text_sect_low
+ bfd_section_size (interp_sect
);
587 interp_sect
= bfd_get_section_by_name (tmp_bfd
.get (), ".plt");
590 interp_plt_sect_low
= bfd_section_vma (interp_sect
);
592 += displacement_from_map (ldm
, interp_plt_sect_low
);
593 interp_plt_sect_high
=
594 interp_plt_sect_low
+ bfd_section_size (interp_sect
);
597 addr
= (gdb_bfd_lookup_symbol
599 [] (const asymbol
*sym
)
601 return strcmp (sym
->name
, "_dl_debug_addr") == 0;
606 warning (_("Could not find symbol _dl_debug_addr "
607 "in dynamic linker"));
608 enable_break_failure_warning ();
612 solib_debug_printf ("_dl_debug_addr (prior to relocation) = %s",
613 hex_string_custom (addr
, 8));
615 addr
+= displacement_from_map (ldm
, addr
);
617 solib_debug_printf ("_dl_debug_addr (after relocation) = %s",
618 hex_string_custom (addr
, 8));
620 /* Fetch the address of the r_debug struct. */
621 if (target_read_memory (addr
, addr_buf
, sizeof addr_buf
) != 0)
623 warning (_("Unable to fetch contents of _dl_debug_addr "
624 "(at address %s) from dynamic linker"),
625 hex_string_custom (addr
, 8));
627 addr
= extract_unsigned_integer (addr_buf
, sizeof addr_buf
, byte_order
);
629 solib_debug_printf ("_dl_debug_addr[0..3] = %s",
630 hex_string_custom (addr
, 8));
632 /* If it's zero, then the ldso hasn't initialized yet, and so
633 there are no shared libs yet loaded. */
636 solib_debug_printf ("ldso not yet initialized");
637 /* Do not warn, but mark to run again. */
641 /* Fetch the r_brk field. It's 8 bytes from the start of
643 if (target_read_memory (addr
+ 8, addr_buf
, sizeof addr_buf
) != 0)
645 warning (_("Unable to fetch _dl_debug_addr->r_brk "
646 "(at address %s) from dynamic linker"),
647 hex_string_custom (addr
+ 8, 8));
648 enable_break_failure_warning ();
651 addr
= extract_unsigned_integer (addr_buf
, sizeof addr_buf
, byte_order
);
653 /* Now fetch the function entry point. */
654 if (target_read_memory (addr
, addr_buf
, sizeof addr_buf
) != 0)
656 warning (_("Unable to fetch _dl_debug_addr->.r_brk entry point "
657 "(at address %s) from dynamic linker"),
658 hex_string_custom (addr
, 8));
659 enable_break_failure_warning ();
662 addr
= extract_unsigned_integer (addr_buf
, sizeof addr_buf
, byte_order
);
664 /* We're done with the loadmap. */
667 /* Remove all the solib event breakpoints. Their addresses
668 may have changed since the last time we ran the program. */
669 remove_solib_event_breakpoints ();
671 /* Now (finally!) create the solib breakpoint. */
672 create_solib_event_breakpoint (target_gdbarch (), addr
);
674 enable_break2_done
= 1;
679 /* Tell the user we couldn't set a dynamic linker breakpoint. */
680 enable_break_failure_warning ();
682 /* Failure return. */
689 asection
*interp_sect
;
690 CORE_ADDR entry_point
;
692 if (current_program_space
->symfile_object_file
== NULL
)
694 solib_debug_printf ("No symbol file found.");
698 if (!entry_point_address_query (&entry_point
))
700 solib_debug_printf ("Symbol file has no entry point.");
704 /* Check for the presence of a .interp section. If there is no
705 such section, the executable is statically linked. */
707 interp_sect
= bfd_get_section_by_name (current_program_space
->exec_bfd (),
710 if (interp_sect
== NULL
)
712 solib_debug_printf ("No .interp section found.");
716 create_solib_event_breakpoint (target_gdbarch (), entry_point
);
718 solib_debug_printf ("solib event breakpoint placed at entry point: %s",
719 hex_string_custom (entry_point
, 8));
724 frv_relocate_main_executable (void)
727 CORE_ADDR exec_addr
, interp_addr
;
728 struct int_elf32_fdpic_loadmap
*ldm
;
730 struct obj_section
*osect
;
732 status
= frv_fdpic_loadmap_addresses (target_gdbarch (),
733 &interp_addr
, &exec_addr
);
735 if (status
< 0 || (exec_addr
== 0 && interp_addr
== 0))
737 /* Not using FDPIC ABI, so do nothing. */
741 /* Fetch the loadmap located at ``exec_addr''. */
742 ldm
= fetch_loadmap (exec_addr
);
744 error (_("Unable to load the executable's loadmap."));
746 delete main_executable_lm_info
;
747 main_executable_lm_info
= new lm_info_frv
;
748 main_executable_lm_info
->map
= ldm
;
750 objfile
*objf
= current_program_space
->symfile_object_file
;
751 section_offsets
new_offsets (objf
->section_offsets
.size ());
754 ALL_OBJFILE_OSECTIONS (objf
, osect
)
756 CORE_ADDR orig_addr
, addr
, offset
;
760 osect_idx
= osect
- objf
->sections_start
;
762 /* Current address of section. */
763 addr
= osect
->addr ();
764 /* Offset from where this section started. */
765 offset
= objf
->section_offsets
[osect_idx
];
766 /* Original address prior to any past relocations. */
767 orig_addr
= addr
- offset
;
769 for (seg
= 0; seg
< ldm
->nsegs
; seg
++)
771 if (ldm
->segs
[seg
].p_vaddr
<= orig_addr
772 && orig_addr
< ldm
->segs
[seg
].p_vaddr
+ ldm
->segs
[seg
].p_memsz
)
774 new_offsets
[osect_idx
]
775 = ldm
->segs
[seg
].addr
- ldm
->segs
[seg
].p_vaddr
;
777 if (new_offsets
[osect_idx
] != offset
)
785 objfile_relocate (objf
, new_offsets
);
787 /* Now that OBJF has been relocated, we can compute the GOT value
788 and stash it away. */
789 main_executable_lm_info
->got_value
= main_got ();
792 /* Implement the "create_inferior_hook" target_solib_ops method.
794 For the FR-V shared library ABI (FDPIC), the main executable needs
795 to be relocated. The shared library breakpoints also need to be
799 frv_solib_create_inferior_hook (int from_tty
)
801 /* Relocate main executable. */
802 frv_relocate_main_executable ();
804 /* Enable shared library breakpoints. */
805 if (!enable_break ())
807 warning (_("shared library handler failed to enable breakpoint"));
813 frv_clear_solib (void)
816 enable_break2_done
= 0;
819 delete main_executable_lm_info
;
820 main_executable_lm_info
= NULL
;
824 frv_free_so (struct so_list
*so
)
826 lm_info_frv
*li
= (lm_info_frv
*) so
->lm_info
;
832 frv_relocate_section_addresses (struct so_list
*so
,
833 struct target_section
*sec
)
836 lm_info_frv
*li
= (lm_info_frv
*) so
->lm_info
;
837 int_elf32_fdpic_loadmap
*map
= li
->map
;
839 for (seg
= 0; seg
< map
->nsegs
; seg
++)
841 if (map
->segs
[seg
].p_vaddr
<= sec
->addr
842 && sec
->addr
< map
->segs
[seg
].p_vaddr
+ map
->segs
[seg
].p_memsz
)
844 CORE_ADDR displ
= map
->segs
[seg
].addr
- map
->segs
[seg
].p_vaddr
;
847 sec
->endaddr
+= displ
;
853 /* Return the GOT address associated with the main executable. Return
854 0 if it can't be found. */
859 struct bound_minimal_symbol got_sym
;
861 objfile
*objf
= current_program_space
->symfile_object_file
;
862 got_sym
= lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL
, objf
);
863 if (got_sym
.minsym
== 0)
866 return got_sym
.value_address ();
869 /* Find the global pointer for the given function address ADDR. */
872 frv_fdpic_find_global_pointer (CORE_ADDR addr
)
874 for (struct so_list
*so
: current_program_space
->solibs ())
877 lm_info_frv
*li
= (lm_info_frv
*) so
->lm_info
;
878 int_elf32_fdpic_loadmap
*map
= li
->map
;
880 for (seg
= 0; seg
< map
->nsegs
; seg
++)
882 if (map
->segs
[seg
].addr
<= addr
883 && addr
< map
->segs
[seg
].addr
+ map
->segs
[seg
].p_memsz
)
884 return li
->got_value
;
888 /* Didn't find it in any of the shared objects. So assume it's in the
893 /* Forward declarations for frv_fdpic_find_canonical_descriptor(). */
894 static CORE_ADDR find_canonical_descriptor_in_load_object
895 (CORE_ADDR
, CORE_ADDR
, const char *, bfd
*, lm_info_frv
*);
897 /* Given a function entry point, attempt to find the canonical descriptor
898 associated with that entry point. Return 0 if no canonical descriptor
902 frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point
)
909 /* Fetch the corresponding global pointer for the entry point. */
910 got_value
= frv_fdpic_find_global_pointer (entry_point
);
912 /* Attempt to find the name of the function. If the name is available,
913 it'll be used as an aid in finding matching functions in the dynamic
915 sym
= find_pc_function (entry_point
);
919 name
= sym
->linkage_name ();
921 /* Check the main executable. */
922 objfile
*objf
= current_program_space
->symfile_object_file
;
923 addr
= find_canonical_descriptor_in_load_object
924 (entry_point
, got_value
, name
, objf
->obfd
.get (),
925 main_executable_lm_info
);
927 /* If descriptor not found via main executable, check each load object
928 in list of shared objects. */
931 for (struct so_list
*so
: current_program_space
->solibs ())
933 lm_info_frv
*li
= (lm_info_frv
*) so
->lm_info
;
935 addr
= find_canonical_descriptor_in_load_object
936 (entry_point
, got_value
, name
, so
->abfd
, li
);
947 find_canonical_descriptor_in_load_object
948 (CORE_ADDR entry_point
, CORE_ADDR got_value
, const char *name
, bfd
*abfd
,
951 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
956 /* Nothing to do if no bfd. */
960 /* Nothing to do if no link map. */
964 /* We want to scan the dynamic relocs for R_FRV_FUNCDESC relocations.
965 (More about this later.) But in order to fetch the relocs, we
966 need to first fetch the dynamic symbols. These symbols need to
967 be cached due to the way that bfd_canonicalize_dynamic_reloc()
968 works. (See the comments in the declaration of struct lm_info
969 for more information.) */
970 if (lm
->dyn_syms
== NULL
)
973 unsigned int number_of_symbols
;
975 /* Determine amount of space needed to hold the dynamic symbol table. */
976 storage_needed
= bfd_get_dynamic_symtab_upper_bound (abfd
);
978 /* If there are no dynamic symbols, there's nothing to do. */
979 if (storage_needed
<= 0)
982 /* Allocate space for the dynamic symbol table. */
983 lm
->dyn_syms
= (asymbol
**) xmalloc (storage_needed
);
985 /* Fetch the dynamic symbol table. */
986 number_of_symbols
= bfd_canonicalize_dynamic_symtab (abfd
, lm
->dyn_syms
);
988 if (number_of_symbols
== 0)
992 /* Fetch the dynamic relocations if not already cached. */
993 if (lm
->dyn_relocs
== NULL
)
997 /* Determine amount of space needed to hold the dynamic relocs. */
998 storage_needed
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1000 /* Bail out if there are no dynamic relocs. */
1001 if (storage_needed
<= 0)
1004 /* Allocate space for the relocs. */
1005 lm
->dyn_relocs
= (arelent
**) xmalloc (storage_needed
);
1007 /* Fetch the dynamic relocs. */
1009 = bfd_canonicalize_dynamic_reloc (abfd
, lm
->dyn_relocs
, lm
->dyn_syms
);
1012 /* Search the dynamic relocs. */
1013 for (i
= 0; i
< lm
->dyn_reloc_count
; i
++)
1015 rel
= lm
->dyn_relocs
[i
];
1017 /* Relocs of interest are those which meet the following
1020 - the names match (assuming the caller could provide
1021 a name which matches ``entry_point'').
1022 - the relocation type must be R_FRV_FUNCDESC. Relocs
1023 of this type are used (by the dynamic linker) to
1024 look up the address of a canonical descriptor (allocating
1025 it if need be) and initializing the GOT entry referred
1026 to by the offset to the address of the descriptor.
1028 These relocs of interest may be used to obtain a
1029 candidate descriptor by first adjusting the reloc's
1030 address according to the link map and then dereferencing
1031 this address (which is a GOT entry) to obtain a descriptor
1033 if ((name
== 0 || strcmp (name
, (*rel
->sym_ptr_ptr
)->name
) == 0)
1034 && rel
->howto
->type
== R_FRV_FUNCDESC
)
1036 gdb_byte buf
[FRV_PTR_SIZE
];
1038 /* Compute address of address of candidate descriptor. */
1039 addr
= rel
->address
+ displacement_from_map (lm
->map
, rel
->address
);
1041 /* Fetch address of candidate descriptor. */
1042 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
1044 addr
= extract_unsigned_integer (buf
, sizeof buf
, byte_order
);
1046 /* Check for matching entry point. */
1047 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
1049 if (extract_unsigned_integer (buf
, sizeof buf
, byte_order
)
1053 /* Check for matching got value. */
1054 if (target_read_memory (addr
+ 4, buf
, sizeof buf
) != 0)
1056 if (extract_unsigned_integer (buf
, sizeof buf
, byte_order
)
1060 /* Match was successful! Exit loop. */
1068 /* Given an objfile, return the address of its link map. This value is
1069 needed for TLS support. */
1071 frv_fetch_objfile_link_map (struct objfile
*objfile
)
1073 /* Cause frv_current_sos() to be run if it hasn't been already. */
1074 if (main_lm_addr
== 0)
1075 solib_add (0, 0, 1);
1077 /* frv_current_sos() will set main_lm_addr for the main executable. */
1078 if (objfile
== current_program_space
->symfile_object_file
)
1079 return main_lm_addr
;
1081 /* The other link map addresses may be found by examining the list
1082 of shared libraries. */
1083 for (struct so_list
*so
: current_program_space
->solibs ())
1085 lm_info_frv
*li
= (lm_info_frv
*) so
->lm_info
;
1087 if (so
->objfile
== objfile
)
1095 const struct target_so_ops frv_so_ops
=
1097 frv_relocate_section_addresses
,
1101 frv_solib_create_inferior_hook
,
1103 open_symbol_file_object
,
1104 frv_in_dynsym_resolve_code
,