]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/solib-frv.c
gdb: move store/extract integer functions to extract-store-integer.{c,h}
[thirdparty/binutils-gdb.git] / gdb / solib-frv.c
CommitLineData
c4d10515 1/* Handle FR-V (FDPIC) shared libraries for GDB, the GNU Debugger.
1d506c26 2 Copyright (C) 2004-2024 Free Software Foundation, Inc.
c4d10515
KB
3
4 This file is part of GDB.
5
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c4d10515
KB
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c4d10515
KB
18
19
ec452525 20#include "extract-store-integer.h"
c4d10515 21#include "gdbcore.h"
cb5c8c39 22#include "solib.h"
c4d10515
KB
23#include "solist.h"
24#include "frv-tdep.h"
25#include "objfiles.h"
26#include "symtab.h"
c4d10515 27#include "elf/frv.h"
cbb099e8 28#include "gdb_bfd.h"
99d9c3b9 29#include "inferior.h"
c4d10515 30
c4d10515
KB
31/* FR-V pointers are four bytes wide. */
32enum { FRV_PTR_SIZE = 4 };
33
34/* Representation of loadmap and related structs for the FR-V FDPIC ABI. */
35
36/* External versions; the size and alignment of the fields should be
37 the same as those on the target. When loaded, the placement of
38 the bits in each field will be the same as on the target. */
e2b7c966
KB
39typedef gdb_byte ext_Elf32_Half[2];
40typedef gdb_byte ext_Elf32_Addr[4];
41typedef gdb_byte ext_Elf32_Word[4];
c4d10515
KB
42
43struct ext_elf32_fdpic_loadseg
44{
45 /* Core address to which the segment is mapped. */
46 ext_Elf32_Addr addr;
47 /* VMA recorded in the program header. */
48 ext_Elf32_Addr p_vaddr;
49 /* Size of this segment in memory. */
50 ext_Elf32_Word p_memsz;
51};
52
53struct ext_elf32_fdpic_loadmap {
54 /* Protocol version number, must be zero. */
55 ext_Elf32_Half version;
56 /* Number of segments in this map. */
57 ext_Elf32_Half nsegs;
58 /* The actual memory map. */
59 struct ext_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
60};
61
62/* Internal versions; the types are GDB types and the data in each
63 of the fields is (or will be) decoded from the external struct
64 for ease of consumption. */
65struct int_elf32_fdpic_loadseg
66{
67 /* Core address to which the segment is mapped. */
68 CORE_ADDR addr;
69 /* VMA recorded in the program header. */
70 CORE_ADDR p_vaddr;
71 /* Size of this segment in memory. */
72 long p_memsz;
73};
74
75struct int_elf32_fdpic_loadmap {
76 /* Protocol version number, must be zero. */
77 int version;
78 /* Number of segments in this map. */
79 int nsegs;
80 /* The actual memory map. */
81 struct int_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
82};
83
84/* Given address LDMADDR, fetch and decode the loadmap at that address.
85 Return NULL if there is a problem reading the target memory or if
86 there doesn't appear to be a loadmap at the given address. The
87 allocated space (representing the loadmap) returned by this
88 function may be freed via a single call to xfree(). */
89
90static struct int_elf32_fdpic_loadmap *
91fetch_loadmap (CORE_ADDR ldmaddr)
92{
99d9c3b9 93 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
c4d10515
KB
94 struct ext_elf32_fdpic_loadmap ext_ldmbuf_partial;
95 struct ext_elf32_fdpic_loadmap *ext_ldmbuf;
96 struct int_elf32_fdpic_loadmap *int_ldmbuf;
97 int ext_ldmbuf_size, int_ldmbuf_size;
98 int version, seg, nsegs;
99
100 /* Fetch initial portion of the loadmap. */
e2b7c966 101 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
dda83cd7 102 sizeof ext_ldmbuf_partial))
c4d10515
KB
103 {
104 /* Problem reading the target's memory. */
105 return NULL;
106 }
107
108 /* Extract the version. */
e2b7c966 109 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
dda83cd7 110 sizeof ext_ldmbuf_partial.version,
e17a4113 111 byte_order);
c4d10515
KB
112 if (version != 0)
113 {
114 /* We only handle version 0. */
115 return NULL;
116 }
117
118 /* Extract the number of segments. */
e2b7c966 119 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
dda83cd7 120 sizeof ext_ldmbuf_partial.nsegs,
e17a4113 121 byte_order);
c4d10515 122
9bc7b6c6
KB
123 if (nsegs <= 0)
124 return NULL;
125
c4d10515
KB
126 /* Allocate space for the complete (external) loadmap. */
127 ext_ldmbuf_size = sizeof (struct ext_elf32_fdpic_loadmap)
dda83cd7 128 + (nsegs - 1) * sizeof (struct ext_elf32_fdpic_loadseg);
224c3ddb 129 ext_ldmbuf = (struct ext_elf32_fdpic_loadmap *) xmalloc (ext_ldmbuf_size);
c4d10515
KB
130
131 /* Copy over the portion of the loadmap that's already been read. */
132 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
133
134 /* Read the rest of the loadmap from the target. */
135 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
dda83cd7
SM
136 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
137 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
c4d10515
KB
138 {
139 /* Couldn't read rest of the loadmap. */
140 xfree (ext_ldmbuf);
141 return NULL;
142 }
143
144 /* Allocate space into which to put information extract from the
145 external loadsegs. I.e, allocate the internal loadsegs. */
146 int_ldmbuf_size = sizeof (struct int_elf32_fdpic_loadmap)
dda83cd7 147 + (nsegs - 1) * sizeof (struct int_elf32_fdpic_loadseg);
224c3ddb 148 int_ldmbuf = (struct int_elf32_fdpic_loadmap *) xmalloc (int_ldmbuf_size);
c4d10515
KB
149
150 /* Place extracted information in internal structs. */
151 int_ldmbuf->version = version;
152 int_ldmbuf->nsegs = nsegs;
153 for (seg = 0; seg < nsegs; seg++)
154 {
155 int_ldmbuf->segs[seg].addr
e2b7c966 156 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
dda83cd7 157 sizeof (ext_ldmbuf->segs[seg].addr),
e17a4113 158 byte_order);
c4d10515 159 int_ldmbuf->segs[seg].p_vaddr
e2b7c966 160 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
dda83cd7 161 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
e17a4113 162 byte_order);
c4d10515 163 int_ldmbuf->segs[seg].p_memsz
e2b7c966 164 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
dda83cd7 165 sizeof (ext_ldmbuf->segs[seg].p_memsz),
e17a4113 166 byte_order);
c4d10515
KB
167 }
168
d5c560f7 169 xfree (ext_ldmbuf);
c4d10515
KB
170 return int_ldmbuf;
171}
172
173/* External link_map and elf32_fdpic_loadaddr struct definitions. */
174
e2b7c966 175typedef gdb_byte ext_ptr[4];
c4d10515
KB
176
177struct ext_elf32_fdpic_loadaddr
178{
179 ext_ptr map; /* struct elf32_fdpic_loadmap *map; */
180 ext_ptr got_value; /* void *got_value; */
181};
182
183struct ext_link_map
184{
185 struct ext_elf32_fdpic_loadaddr l_addr;
186
187 /* Absolute file name object was found in. */
188 ext_ptr l_name; /* char *l_name; */
189
190 /* Dynamic section of the shared object. */
191 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
192
193 /* Chain of loaded objects. */
194 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
195};
196
c378eb4e 197/* Link map info to include in an allocated so_list entry. */
c4d10515 198
e3b63a79 199struct lm_info_frv final : public lm_info
af43057b 200{
4023ae76
SM
201 ~lm_info_frv ()
202 {
203 xfree (this->map);
204 xfree (this->dyn_syms);
205 xfree (this->dyn_relocs);
206 }
af43057b
SM
207
208 /* The loadmap, digested into an easier to use form. */
4023ae76 209 int_elf32_fdpic_loadmap *map = NULL;
af43057b 210 /* The GOT address for this link map entry. */
4023ae76 211 CORE_ADDR got_value = 0;
af43057b 212 /* The link map address, needed for frv_fetch_objfile_link_map(). */
4023ae76 213 CORE_ADDR lm_addr = 0;
af43057b
SM
214
215 /* Cached dynamic symbol table and dynamic relocs initialized and
216 used only by find_canonical_descriptor_in_load_object().
217
218 Note: kevinb/2004-02-26: It appears that calls to
219 bfd_canonicalize_dynamic_reloc() will use the same symbols as
220 those supplied to the first call to this function. Therefore,
221 it's important to NOT free the asymbol ** data structure
222 supplied to the first call. Thus the caching of the dynamic
223 symbols (dyn_syms) is critical for correct operation. The
224 caching of the dynamic relocations could be dispensed with. */
4023ae76
SM
225 asymbol **dyn_syms = NULL;
226 arelent **dyn_relocs = NULL;
227 int dyn_reloc_count = 0; /* Number of dynamic relocs. */
af43057b 228};
c4d10515
KB
229
230/* The load map, got value, etc. are not available from the chain
231 of loaded shared objects. ``main_executable_lm_info'' provides
232 a way to get at this information so that it doesn't need to be
233 frequently recomputed. Initialized by frv_relocate_main_executable(). */
d0e449a1 234static lm_info_frv *main_executable_lm_info;
c4d10515
KB
235
236static void frv_relocate_main_executable (void);
237static CORE_ADDR main_got (void);
238static int enable_break2 (void);
239
6cedf3bc 240/* Implement the "open_symbol_file_object" solib_ops method. */
c4d10515
KB
241
242static int
bf469271 243open_symbol_file_object (int from_tty)
c4d10515
KB
244{
245 /* Unimplemented. */
246 return 0;
247}
248
249/* Cached value for lm_base(), below. */
250static CORE_ADDR lm_base_cache = 0;
251
186993b4
KB
252/* Link map address for main module. */
253static CORE_ADDR main_lm_addr = 0;
254
c4d10515
KB
255/* Return the address from which the link map chain may be found. On
256 the FR-V, this may be found in a number of ways. Assuming that the
257 main executable has already been relocated, the easiest way to find
258 this value is to look up the address of _GLOBAL_OFFSET_TABLE_. A
259 pointer to the start of the link map will be located at the word found
260 at _GLOBAL_OFFSET_TABLE_ + 8. (This is part of the dynamic linker
261 reserve area mandated by the ABI.) */
262
263static CORE_ADDR
264lm_base (void)
265{
99d9c3b9 266 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
3b7344d5 267 struct bound_minimal_symbol got_sym;
c4d10515 268 CORE_ADDR addr;
e2b7c966 269 gdb_byte buf[FRV_PTR_SIZE];
c4d10515 270
89a7ee67
KB
271 /* One of our assumptions is that the main executable has been relocated.
272 Bail out if this has not happened. (Note that post_create_inferior()
273 in infcmd.c will call solib_add prior to solib_create_inferior_hook().
274 If we allow this to happen, lm_base_cache will be initialized with
275 a bogus value. */
276 if (main_executable_lm_info == 0)
277 return 0;
278
c4d10515
KB
279 /* If we already have a cached value, return it. */
280 if (lm_base_cache)
281 return lm_base_cache;
282
283 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
dda83cd7 284 current_program_space->symfile_object_file);
3b7344d5 285 if (got_sym.minsym == 0)
c4d10515 286 {
e26d0dab 287 solib_debug_printf ("_GLOBAL_OFFSET_TABLE_ not found.");
c4d10515
KB
288 return 0;
289 }
290
4aeddc50 291 addr = got_sym.value_address () + 8;
c4d10515 292
e26d0dab
SM
293 solib_debug_printf ("_GLOBAL_OFFSET_TABLE_ + 8 = %s",
294 hex_string_custom (addr, 8));
c4d10515
KB
295
296 if (target_read_memory (addr, buf, sizeof buf) != 0)
297 return 0;
e17a4113 298 lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
c4d10515 299
e26d0dab
SM
300 solib_debug_printf ("lm_base_cache = %s",
301 hex_string_custom (lm_base_cache, 8));
c4d10515
KB
302
303 return lm_base_cache;
304}
305
306
6cedf3bc 307/* Implement the "current_sos" solib_ops method. */
c4d10515 308
7b323785 309static intrusive_list<solib>
8971d278 310frv_current_sos ()
c4d10515 311{
99d9c3b9 312 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
c4d10515 313 CORE_ADDR lm_addr, mgot;
7b323785 314 intrusive_list<solib> sos;
c4d10515 315
7c699b81
KB
316 /* Make sure that the main executable has been relocated. This is
317 required in order to find the address of the global offset table,
318 which in turn is used to find the link map info. (See lm_base()
319 for details.)
320
321 Note that the relocation of the main executable is also performed
4d1eb6b4 322 by solib_create_inferior_hook(), however, in the case of core
7c699b81 323 files, this hook is called too late in order to be of benefit to
4d1eb6b4 324 solib_add. solib_add eventually calls this this function,
7c699b81 325 frv_current_sos, and also precedes the call to
4d1eb6b4 326 solib_create_inferior_hook(). (See post_create_inferior() in
7c699b81 327 infcmd.c.) */
6fdf95ae
SM
328 if (main_executable_lm_info == 0
329 && current_program_space->core_bfd () != nullptr)
7c699b81
KB
330 frv_relocate_main_executable ();
331
332 /* Fetch the GOT corresponding to the main executable. */
c4d10515
KB
333 mgot = main_got ();
334
335 /* Locate the address of the first link map struct. */
336 lm_addr = lm_base ();
337
b021a221 338 /* We have at least one link map entry. Fetch the lot of them,
c4d10515
KB
339 building the solist chain. */
340 while (lm_addr)
341 {
342 struct ext_link_map lm_buf;
343 CORE_ADDR got_addr;
344
e26d0dab
SM
345 solib_debug_printf ("reading link_map entry at %s",
346 hex_string_custom (lm_addr, 8));
c4d10515 347
3e43a32a
MS
348 if (target_read_memory (lm_addr, (gdb_byte *) &lm_buf,
349 sizeof (lm_buf)) != 0)
c4d10515 350 {
3e43a32a
MS
351 warning (_("frv_current_sos: Unable to read link map entry. "
352 "Shared object chain may be incomplete."));
c4d10515
KB
353 break;
354 }
355
356 got_addr
e2b7c966 357 = extract_unsigned_integer (lm_buf.l_addr.got_value,
e17a4113
UW
358 sizeof (lm_buf.l_addr.got_value),
359 byte_order);
c4d10515
KB
360 /* If the got_addr is the same as mgotr, then we're looking at the
361 entry for the main executable. By convention, we don't include
362 this in the list of shared objects. */
363 if (got_addr != mgot)
364 {
c4d10515 365 struct int_elf32_fdpic_loadmap *loadmap;
c4d10515
KB
366 CORE_ADDR addr;
367
368 /* Fetch the load map address. */
e2b7c966 369 addr = extract_unsigned_integer (lm_buf.l_addr.map,
e17a4113
UW
370 sizeof lm_buf.l_addr.map,
371 byte_order);
c4d10515
KB
372 loadmap = fetch_loadmap (addr);
373 if (loadmap == NULL)
374 {
3e43a32a
MS
375 warning (_("frv_current_sos: Unable to fetch load map. "
376 "Shared object chain may be incomplete."));
c4d10515
KB
377 break;
378 }
379
7b323785 380 solib *sop = new solib;
6b62451a 381 auto li = std::make_unique<lm_info_frv> ();
d0e449a1
SM
382 li->map = loadmap;
383 li->got_value = got_addr;
384 li->lm_addr = lm_addr;
c4d10515 385 /* Fetch the name. */
e2b7c966 386 addr = extract_unsigned_integer (lm_buf.l_name,
e17a4113
UW
387 sizeof (lm_buf.l_name),
388 byte_order);
66920317
TT
389 gdb::unique_xmalloc_ptr<char> name_buf
390 = target_read_string (addr, SO_NAME_MAX_PATH_SIZE - 1);
c4d10515 391
e26d0dab
SM
392 solib_debug_printf ("name = %s", name_buf.get ());
393
66920317
TT
394 if (name_buf == nullptr)
395 warning (_("Can't read pathname for link map entry."));
c4d10515
KB
396 else
397 {
98107b0b
SM
398 sop->so_name = name_buf.get ();
399 sop->so_original_name = sop->so_name;
c4d10515
KB
400 }
401
8971d278 402 sos.push_back (*sop);
c4d10515 403 }
186993b4
KB
404 else
405 {
406 main_lm_addr = lm_addr;
407 }
c4d10515 408
e17a4113
UW
409 lm_addr = extract_unsigned_integer (lm_buf.l_next,
410 sizeof (lm_buf.l_next), byte_order);
c4d10515
KB
411 }
412
413 enable_break2 ();
414
8971d278 415 return sos;
c4d10515
KB
416}
417
418
419/* Return 1 if PC lies in the dynamic symbol resolution code of the
420 run time loader. */
421
422static CORE_ADDR interp_text_sect_low;
423static CORE_ADDR interp_text_sect_high;
424static CORE_ADDR interp_plt_sect_low;
425static CORE_ADDR interp_plt_sect_high;
426
427static int
428frv_in_dynsym_resolve_code (CORE_ADDR pc)
429{
430 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
431 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
3e5d3a5a 432 || in_plt_section (pc));
c4d10515
KB
433}
434
435/* Given a loadmap and an address, return the displacement needed
436 to relocate the address. */
437
63807e1d 438static CORE_ADDR
c4d10515 439displacement_from_map (struct int_elf32_fdpic_loadmap *map,
dda83cd7 440 CORE_ADDR addr)
c4d10515
KB
441{
442 int seg;
443
444 for (seg = 0; seg < map->nsegs; seg++)
445 {
446 if (map->segs[seg].p_vaddr <= addr
dda83cd7 447 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
c4d10515
KB
448 {
449 return map->segs[seg].addr - map->segs[seg].p_vaddr;
450 }
451 }
452
453 return 0;
454}
455
456/* Print a warning about being unable to set the dynamic linker
457 breakpoint. */
458
459static void
460enable_break_failure_warning (void)
461{
8a3fe4f8 462 warning (_("Unable to find dynamic linker breakpoint function.\n"
dda83cd7 463 "GDB will be unable to debug shared library initializers\n"
8a3fe4f8 464 "and track explicitly loaded dynamic code."));
c4d10515
KB
465}
466
7f86f058 467/* Arrange for dynamic linker to hit breakpoint.
c4d10515
KB
468
469 The dynamic linkers has, as part of its debugger interface, support
470 for arranging for the inferior to hit a breakpoint after mapping in
471 the shared libraries. This function enables that breakpoint.
472
473 On the FR-V, using the shared library (FDPIC) ABI, the symbol
474 _dl_debug_addr points to the r_debug struct which contains
475 a field called r_brk. r_brk is the address of the function
476 descriptor upon which a breakpoint must be placed. Being a
477 function descriptor, we must extract the entry point in order
478 to set the breakpoint.
479
480 Our strategy will be to get the .interp section from the
481 executable. This section will provide us with the name of the
482 interpreter. We'll open the interpreter and then look up
483 the address of _dl_debug_addr. We then relocate this address
484 using the interpreter's loadmap. Once the relocated address
485 is known, we fetch the value (address) corresponding to r_brk
486 and then use that value to fetch the entry point of the function
7f86f058 487 we're interested in. */
c4d10515 488
c4d10515
KB
489static int enable_break2_done = 0;
490
491static int
492enable_break2 (void)
493{
99d9c3b9 494 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
c4d10515
KB
495 asection *interp_sect;
496
cb7db0f2 497 if (enable_break2_done)
c4d10515
KB
498 return 1;
499
c4d10515
KB
500 interp_text_sect_low = interp_text_sect_high = 0;
501 interp_plt_sect_low = interp_plt_sect_high = 0;
502
503 /* Find the .interp section; if not found, warn the user and drop
504 into the old breakpoint at symbol code. */
7e10abd1
TT
505 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
506 ".interp");
c4d10515
KB
507 if (interp_sect)
508 {
509 unsigned int interp_sect_size;
001f13d8 510 char *buf;
c4d10515
KB
511 int status;
512 CORE_ADDR addr, interp_loadmap_addr;
e2b7c966 513 gdb_byte addr_buf[FRV_PTR_SIZE];
c4d10515
KB
514 struct int_elf32_fdpic_loadmap *ldm;
515
516 /* Read the contents of the .interp section into a local buffer;
dda83cd7 517 the contents specify the dynamic linker this program uses. */
fd361982 518 interp_sect_size = bfd_section_size (interp_sect);
224c3ddb 519 buf = (char *) alloca (interp_sect_size);
7e10abd1
TT
520 bfd_get_section_contents (current_program_space->exec_bfd (),
521 interp_sect, buf, 0, interp_sect_size);
c4d10515
KB
522
523 /* Now we need to figure out where the dynamic linker was
dda83cd7
SM
524 loaded so that we can load its symbols and place a breakpoint
525 in the dynamic linker itself.
c4d10515 526
dda83cd7
SM
527 This address is stored on the stack. However, I've been unable
528 to find any magic formula to find it for Solaris (appears to
529 be trivial on GNU/Linux). Therefore, we have to try an alternate
530 mechanism to find the dynamic linker's base address. */
c4d10515 531
192b62ce 532 gdb_bfd_ref_ptr tmp_bfd;
a70b8144 533 try
dda83cd7
SM
534 {
535 tmp_bfd = solib_bfd_open (buf);
536 }
230d2906 537 catch (const gdb_exception &ex)
492d29ea
PA
538 {
539 }
492d29ea 540
c4d10515
KB
541 if (tmp_bfd == NULL)
542 {
543 enable_break_failure_warning ();
544 return 0;
545 }
546
99d9c3b9 547 status = frv_fdpic_loadmap_addresses (current_inferior ()->arch (),
dda83cd7 548 &interp_loadmap_addr, 0);
c4d10515
KB
549 if (status < 0)
550 {
8a3fe4f8 551 warning (_("Unable to determine dynamic linker loadmap address."));
c4d10515 552 enable_break_failure_warning ();
c4d10515
KB
553 return 0;
554 }
555
e26d0dab
SM
556 solib_debug_printf ("interp_loadmap_addr = %s",
557 hex_string_custom (interp_loadmap_addr, 8));
c4d10515
KB
558
559 ldm = fetch_loadmap (interp_loadmap_addr);
560 if (ldm == NULL)
561 {
8a3fe4f8 562 warning (_("Unable to load dynamic linker loadmap at address %s."),
dda83cd7 563 hex_string_custom (interp_loadmap_addr, 8));
c4d10515 564 enable_break_failure_warning ();
c4d10515
KB
565 return 0;
566 }
567
568 /* Record the relocated start and end address of the dynamic linker
dda83cd7 569 text and plt section for svr4_in_dynsym_resolve_code. */
192b62ce 570 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
c4d10515
KB
571 if (interp_sect)
572 {
fd361982 573 interp_text_sect_low = bfd_section_vma (interp_sect);
c4d10515
KB
574 interp_text_sect_low
575 += displacement_from_map (ldm, interp_text_sect_low);
576 interp_text_sect_high
fd361982 577 = interp_text_sect_low + bfd_section_size (interp_sect);
c4d10515 578 }
192b62ce 579 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
c4d10515
KB
580 if (interp_sect)
581 {
fd361982 582 interp_plt_sect_low = bfd_section_vma (interp_sect);
c4d10515
KB
583 interp_plt_sect_low
584 += displacement_from_map (ldm, interp_plt_sect_low);
585 interp_plt_sect_high =
fd361982 586 interp_plt_sect_low + bfd_section_size (interp_sect);
c4d10515
KB
587 }
588
6b3a2759
TT
589 addr = (gdb_bfd_lookup_symbol
590 (tmp_bfd.get (),
591 [] (const asymbol *sym)
592 {
593 return strcmp (sym->name, "_dl_debug_addr") == 0;
594 }));
cb457ae2 595
c4d10515
KB
596 if (addr == 0)
597 {
3e43a32a
MS
598 warning (_("Could not find symbol _dl_debug_addr "
599 "in dynamic linker"));
c4d10515 600 enable_break_failure_warning ();
c4d10515
KB
601 return 0;
602 }
603
e26d0dab
SM
604 solib_debug_printf ("_dl_debug_addr (prior to relocation) = %s",
605 hex_string_custom (addr, 8));
c4d10515
KB
606
607 addr += displacement_from_map (ldm, addr);
608
e26d0dab
SM
609 solib_debug_printf ("_dl_debug_addr (after relocation) = %s",
610 hex_string_custom (addr, 8));
c4d10515
KB
611
612 /* Fetch the address of the r_debug struct. */
613 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
614 {
3e43a32a
MS
615 warning (_("Unable to fetch contents of _dl_debug_addr "
616 "(at address %s) from dynamic linker"),
dda83cd7 617 hex_string_custom (addr, 8));
c4d10515 618 }
e17a4113 619 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
c4d10515 620
e26d0dab
SM
621 solib_debug_printf ("_dl_debug_addr[0..3] = %s",
622 hex_string_custom (addr, 8));
cb7db0f2
MF
623
624 /* If it's zero, then the ldso hasn't initialized yet, and so
dda83cd7 625 there are no shared libs yet loaded. */
cb7db0f2
MF
626 if (addr == 0)
627 {
e26d0dab 628 solib_debug_printf ("ldso not yet initialized");
cb7db0f2
MF
629 /* Do not warn, but mark to run again. */
630 return 0;
631 }
632
c4d10515 633 /* Fetch the r_brk field. It's 8 bytes from the start of
dda83cd7 634 _dl_debug_addr. */
c4d10515
KB
635 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
636 {
3e43a32a
MS
637 warning (_("Unable to fetch _dl_debug_addr->r_brk "
638 "(at address %s) from dynamic linker"),
dda83cd7 639 hex_string_custom (addr + 8, 8));
c4d10515 640 enable_break_failure_warning ();
c4d10515
KB
641 return 0;
642 }
e17a4113 643 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
c4d10515
KB
644
645 /* Now fetch the function entry point. */
646 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
647 {
3e43a32a
MS
648 warning (_("Unable to fetch _dl_debug_addr->.r_brk entry point "
649 "(at address %s) from dynamic linker"),
dda83cd7 650 hex_string_custom (addr, 8));
c4d10515 651 enable_break_failure_warning ();
c4d10515
KB
652 return 0;
653 }
e17a4113 654 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
c4d10515 655
192b62ce 656 /* We're done with the loadmap. */
c4d10515
KB
657 xfree (ldm);
658
cb7db0f2 659 /* Remove all the solib event breakpoints. Their addresses
dda83cd7 660 may have changed since the last time we ran the program. */
cb7db0f2
MF
661 remove_solib_event_breakpoints ();
662
c4d10515 663 /* Now (finally!) create the solib breakpoint. */
99d9c3b9 664 create_solib_event_breakpoint (current_inferior ()->arch (), addr);
c4d10515 665
cb7db0f2
MF
666 enable_break2_done = 1;
667
c4d10515
KB
668 return 1;
669 }
670
671 /* Tell the user we couldn't set a dynamic linker breakpoint. */
672 enable_break_failure_warning ();
673
674 /* Failure return. */
675 return 0;
676}
677
678static int
679enable_break (void)
680{
681 asection *interp_sect;
d56e56aa 682 CORE_ADDR entry_point;
c4d10515 683
a42d7dd8 684 if (current_program_space->symfile_object_file == NULL)
c4d10515 685 {
e26d0dab 686 solib_debug_printf ("No symbol file found.");
abd0a5fa
JK
687 return 0;
688 }
c4d10515 689
d56e56aa 690 if (!entry_point_address_query (&entry_point))
abd0a5fa 691 {
e26d0dab 692 solib_debug_printf ("Symbol file has no entry point.");
abd0a5fa 693 return 0;
c4d10515 694 }
abd0a5fa
JK
695
696 /* Check for the presence of a .interp section. If there is no
697 such section, the executable is statically linked. */
698
7e10abd1
TT
699 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
700 ".interp");
abd0a5fa
JK
701
702 if (interp_sect == NULL)
c4d10515 703 {
e26d0dab 704 solib_debug_printf ("No .interp section found.");
abd0a5fa 705 return 0;
c4d10515
KB
706 }
707
99d9c3b9 708 create_solib_event_breakpoint (current_inferior ()->arch (), entry_point);
abd0a5fa 709
e26d0dab
SM
710 solib_debug_printf ("solib event breakpoint placed at entry point: %s",
711 hex_string_custom (entry_point, 8));
c4d10515
KB
712 return 1;
713}
714
c4d10515
KB
715static void
716frv_relocate_main_executable (void)
717{
718 int status;
9bc7b6c6 719 CORE_ADDR exec_addr, interp_addr;
c4d10515 720 struct int_elf32_fdpic_loadmap *ldm;
c4d10515 721 int changed;
c4d10515 722
99d9c3b9 723 status = frv_fdpic_loadmap_addresses (current_inferior ()->arch (),
dda83cd7 724 &interp_addr, &exec_addr);
c4d10515 725
9bc7b6c6 726 if (status < 0 || (exec_addr == 0 && interp_addr == 0))
c4d10515
KB
727 {
728 /* Not using FDPIC ABI, so do nothing. */
729 return;
730 }
731
732 /* Fetch the loadmap located at ``exec_addr''. */
733 ldm = fetch_loadmap (exec_addr);
734 if (ldm == NULL)
8a3fe4f8 735 error (_("Unable to load the executable's loadmap."));
c4d10515 736
4023ae76
SM
737 delete main_executable_lm_info;
738 main_executable_lm_info = new lm_info_frv;
c4d10515
KB
739 main_executable_lm_info->map = ldm;
740
a42d7dd8
TT
741 objfile *objf = current_program_space->symfile_object_file;
742 section_offsets new_offsets (objf->section_offsets.size ());
c4d10515
KB
743 changed = 0;
744
5250cbc8 745 for (obj_section *osect : objf->sections ())
c4d10515
KB
746 {
747 CORE_ADDR orig_addr, addr, offset;
748 int osect_idx;
749 int seg;
750
9ed8433a 751 osect_idx = osect - objf->sections_start;
c4d10515
KB
752
753 /* Current address of section. */
0c1bcd23 754 addr = osect->addr ();
c4d10515 755 /* Offset from where this section started. */
a42d7dd8 756 offset = objf->section_offsets[osect_idx];
c4d10515
KB
757 /* Original address prior to any past relocations. */
758 orig_addr = addr - offset;
759
760 for (seg = 0; seg < ldm->nsegs; seg++)
761 {
762 if (ldm->segs[seg].p_vaddr <= orig_addr
763 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
764 {
6a053cb1 765 new_offsets[osect_idx]
c4d10515
KB
766 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
767
6a053cb1 768 if (new_offsets[osect_idx] != offset)
c4d10515
KB
769 changed = 1;
770 break;
771 }
772 }
773 }
774
775 if (changed)
a42d7dd8 776 objfile_relocate (objf, new_offsets);
c4d10515 777
a42d7dd8
TT
778 /* Now that OBJF has been relocated, we can compute the GOT value
779 and stash it away. */
c4d10515
KB
780 main_executable_lm_info->got_value = main_got ();
781}
782
7f86f058 783/* Implement the "create_inferior_hook" target_solib_ops method.
c4d10515 784
7f86f058
PA
785 For the FR-V shared library ABI (FDPIC), the main executable needs
786 to be relocated. The shared library breakpoints also need to be
787 enabled. */
c4d10515
KB
788
789static void
268a4a75 790frv_solib_create_inferior_hook (int from_tty)
c4d10515
KB
791{
792 /* Relocate main executable. */
793 frv_relocate_main_executable ();
794
795 /* Enable shared library breakpoints. */
796 if (!enable_break ())
797 {
8a3fe4f8 798 warning (_("shared library handler failed to enable breakpoint"));
c4d10515
KB
799 return;
800 }
801}
802
803static void
581b34c2 804frv_clear_solib (program_space *pspace)
c4d10515
KB
805{
806 lm_base_cache = 0;
c4d10515 807 enable_break2_done = 0;
186993b4 808 main_lm_addr = 0;
4023ae76
SM
809
810 delete main_executable_lm_info;
811 main_executable_lm_info = NULL;
c4d10515
KB
812}
813
c4d10515 814static void
7b323785 815frv_relocate_section_addresses (solib &so, target_section *sec)
c4d10515
KB
816{
817 int seg;
7ad0a42e 818 auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
d0e449a1 819 int_elf32_fdpic_loadmap *map = li->map;
c4d10515
KB
820
821 for (seg = 0; seg < map->nsegs; seg++)
822 {
823 if (map->segs[seg].p_vaddr <= sec->addr
dda83cd7 824 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
c4d10515
KB
825 {
826 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
433759f7 827
c4d10515
KB
828 sec->addr += displ;
829 sec->endaddr += displ;
830 break;
831 }
832 }
833}
834
835/* Return the GOT address associated with the main executable. Return
836 0 if it can't be found. */
837
838static CORE_ADDR
839main_got (void)
840{
3b7344d5 841 struct bound_minimal_symbol got_sym;
c4d10515 842
a42d7dd8
TT
843 objfile *objf = current_program_space->symfile_object_file;
844 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL, objf);
3b7344d5 845 if (got_sym.minsym == 0)
c4d10515
KB
846 return 0;
847
4aeddc50 848 return got_sym.value_address ();
c4d10515
KB
849}
850
851/* Find the global pointer for the given function address ADDR. */
852
853CORE_ADDR
854frv_fdpic_find_global_pointer (CORE_ADDR addr)
855{
7b323785 856 for (const solib &so : current_program_space->solibs ())
c4d10515
KB
857 {
858 int seg;
8971d278 859 auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
d0e449a1 860 int_elf32_fdpic_loadmap *map = li->map;
c4d10515
KB
861
862 for (seg = 0; seg < map->nsegs; seg++)
863 {
864 if (map->segs[seg].addr <= addr
865 && addr < map->segs[seg].addr + map->segs[seg].p_memsz)
d0e449a1 866 return li->got_value;
c4d10515 867 }
c4d10515
KB
868 }
869
7a9dd1b2 870 /* Didn't find it in any of the shared objects. So assume it's in the
c4d10515
KB
871 main executable. */
872 return main_got ();
873}
874
875/* Forward declarations for frv_fdpic_find_canonical_descriptor(). */
876static CORE_ADDR find_canonical_descriptor_in_load_object
d0e449a1 877 (CORE_ADDR, CORE_ADDR, const char *, bfd *, lm_info_frv *);
c4d10515
KB
878
879/* Given a function entry point, attempt to find the canonical descriptor
880 associated with that entry point. Return 0 if no canonical descriptor
881 could be found. */
882
883CORE_ADDR
884frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
885{
0d5cff50 886 const char *name;
c4d10515
KB
887 CORE_ADDR addr;
888 CORE_ADDR got_value;
c4d10515 889 struct symbol *sym;
c4d10515
KB
890
891 /* Fetch the corresponding global pointer for the entry point. */
892 got_value = frv_fdpic_find_global_pointer (entry_point);
893
894 /* Attempt to find the name of the function. If the name is available,
895 it'll be used as an aid in finding matching functions in the dynamic
896 symbol table. */
897 sym = find_pc_function (entry_point);
898 if (sym == 0)
899 name = 0;
900 else
987012b8 901 name = sym->linkage_name ();
c4d10515
KB
902
903 /* Check the main executable. */
a42d7dd8 904 objfile *objf = current_program_space->symfile_object_file;
c4d10515 905 addr = find_canonical_descriptor_in_load_object
98badbfd 906 (entry_point, got_value, name, objf->obfd.get (),
c4d10515
KB
907 main_executable_lm_info);
908
909 /* If descriptor not found via main executable, check each load object
910 in list of shared objects. */
911 if (addr == 0)
912 {
7b323785 913 for (const solib &so : current_program_space->solibs ())
c4d10515 914 {
8971d278 915 auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
d0e449a1 916
c4d10515 917 addr = find_canonical_descriptor_in_load_object
8971d278 918 (entry_point, got_value, name, so.abfd.get(), li);
c4d10515
KB
919
920 if (addr != 0)
921 break;
c4d10515
KB
922 }
923 }
924
925 return addr;
926}
927
928static CORE_ADDR
929find_canonical_descriptor_in_load_object
0d5cff50 930 (CORE_ADDR entry_point, CORE_ADDR got_value, const char *name, bfd *abfd,
d0e449a1 931 lm_info_frv *lm)
c4d10515 932{
99d9c3b9 933 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
c4d10515
KB
934 arelent *rel;
935 unsigned int i;
936 CORE_ADDR addr = 0;
937
938 /* Nothing to do if no bfd. */
939 if (abfd == 0)
940 return 0;
941
35e08e03
KB
942 /* Nothing to do if no link map. */
943 if (lm == 0)
944 return 0;
945
c4d10515
KB
946 /* We want to scan the dynamic relocs for R_FRV_FUNCDESC relocations.
947 (More about this later.) But in order to fetch the relocs, we
948 need to first fetch the dynamic symbols. These symbols need to
949 be cached due to the way that bfd_canonicalize_dynamic_reloc()
950 works. (See the comments in the declaration of struct lm_info
951 for more information.) */
952 if (lm->dyn_syms == NULL)
953 {
954 long storage_needed;
955 unsigned int number_of_symbols;
956
957 /* Determine amount of space needed to hold the dynamic symbol table. */
958 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
959
960 /* If there are no dynamic symbols, there's nothing to do. */
961 if (storage_needed <= 0)
962 return 0;
963
964 /* Allocate space for the dynamic symbol table. */
965 lm->dyn_syms = (asymbol **) xmalloc (storage_needed);
966
967 /* Fetch the dynamic symbol table. */
968 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, lm->dyn_syms);
969
970 if (number_of_symbols == 0)
971 return 0;
972 }
973
974 /* Fetch the dynamic relocations if not already cached. */
975 if (lm->dyn_relocs == NULL)
976 {
977 long storage_needed;
978
979 /* Determine amount of space needed to hold the dynamic relocs. */
980 storage_needed = bfd_get_dynamic_reloc_upper_bound (abfd);
981
982 /* Bail out if there are no dynamic relocs. */
983 if (storage_needed <= 0)
984 return 0;
985
986 /* Allocate space for the relocs. */
987 lm->dyn_relocs = (arelent **) xmalloc (storage_needed);
988
989 /* Fetch the dynamic relocs. */
990 lm->dyn_reloc_count
991 = bfd_canonicalize_dynamic_reloc (abfd, lm->dyn_relocs, lm->dyn_syms);
992 }
993
994 /* Search the dynamic relocs. */
995 for (i = 0; i < lm->dyn_reloc_count; i++)
996 {
997 rel = lm->dyn_relocs[i];
998
999 /* Relocs of interest are those which meet the following
dda83cd7 1000 criteria:
c4d10515
KB
1001
1002 - the names match (assuming the caller could provide
1003 a name which matches ``entry_point'').
1004 - the relocation type must be R_FRV_FUNCDESC. Relocs
1005 of this type are used (by the dynamic linker) to
1006 look up the address of a canonical descriptor (allocating
1007 it if need be) and initializing the GOT entry referred
1008 to by the offset to the address of the descriptor.
1009
1010 These relocs of interest may be used to obtain a
1011 candidate descriptor by first adjusting the reloc's
1012 address according to the link map and then dereferencing
1013 this address (which is a GOT entry) to obtain a descriptor
1014 address. */
1015 if ((name == 0 || strcmp (name, (*rel->sym_ptr_ptr)->name) == 0)
dda83cd7 1016 && rel->howto->type == R_FRV_FUNCDESC)
c4d10515 1017 {
e2b7c966 1018 gdb_byte buf [FRV_PTR_SIZE];
c4d10515
KB
1019
1020 /* Compute address of address of candidate descriptor. */
1021 addr = rel->address + displacement_from_map (lm->map, rel->address);
1022
1023 /* Fetch address of candidate descriptor. */
1024 if (target_read_memory (addr, buf, sizeof buf) != 0)
1025 continue;
e17a4113 1026 addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
c4d10515
KB
1027
1028 /* Check for matching entry point. */
1029 if (target_read_memory (addr, buf, sizeof buf) != 0)
1030 continue;
e17a4113
UW
1031 if (extract_unsigned_integer (buf, sizeof buf, byte_order)
1032 != entry_point)
c4d10515
KB
1033 continue;
1034
1035 /* Check for matching got value. */
1036 if (target_read_memory (addr + 4, buf, sizeof buf) != 0)
1037 continue;
e17a4113
UW
1038 if (extract_unsigned_integer (buf, sizeof buf, byte_order)
1039 != got_value)
c4d10515
KB
1040 continue;
1041
1042 /* Match was successful! Exit loop. */
1043 break;
1044 }
1045 }
1046
1047 return addr;
1048}
1049
186993b4
KB
1050/* Given an objfile, return the address of its link map. This value is
1051 needed for TLS support. */
1052CORE_ADDR
1053frv_fetch_objfile_link_map (struct objfile *objfile)
1054{
186993b4
KB
1055 /* Cause frv_current_sos() to be run if it hasn't been already. */
1056 if (main_lm_addr == 0)
e696b3ad 1057 solib_add (0, 0, 1);
186993b4
KB
1058
1059 /* frv_current_sos() will set main_lm_addr for the main executable. */
a42d7dd8 1060 if (objfile == current_program_space->symfile_object_file)
186993b4
KB
1061 return main_lm_addr;
1062
1063 /* The other link map addresses may be found by examining the list
1064 of shared libraries. */
7b323785 1065 for (const solib &so : current_program_space->solibs ())
186993b4 1066 {
8971d278 1067 auto *li = gdb::checked_static_cast<lm_info_frv *> (so.lm_info.get ());
d0e449a1 1068
8971d278 1069 if (so.objfile == objfile)
d0e449a1 1070 return li->lm_addr;
186993b4
KB
1071 }
1072
1073 /* Not found! */
1074 return 0;
1075}
1076
6cedf3bc 1077const solib_ops frv_so_ops =
549dfc51
TT
1078{
1079 frv_relocate_section_addresses,
549dfc51
TT
1080 nullptr,
1081 frv_clear_solib,
1082 frv_solib_create_inferior_hook,
1083 frv_current_sos,
1084 open_symbol_file_object,
1085 frv_in_dynsym_resolve_code,
1086 solib_bfd_open,
1087};