GDB (xrefs)
Loading...
Searching...
No Matches
/tmp/gdb-13.1/gdb/solib-frv.c
Go to the documentation of this file.
1/* Handle FR-V (FDPIC) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 3 of the License, or
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
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19
20#include "defs.h"
21#include "inferior.h"
22#include "gdbcore.h"
23#include "solib.h"
24#include "solist.h"
25#include "frv-tdep.h"
26#include "objfiles.h"
27#include "symtab.h"
28#include "language.h"
29#include "command.h"
30#include "gdbcmd.h"
31#include "elf/frv.h"
32#include "gdb_bfd.h"
33
34/* FR-V pointers are four bytes wide. */
35enum { FRV_PTR_SIZE = 4 };
36
37/* Representation of loadmap and related structs for the FR-V FDPIC ABI. */
38
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. */
42typedef gdb_byte ext_Elf32_Half[2];
43typedef gdb_byte ext_Elf32_Addr[4];
44typedef gdb_byte ext_Elf32_Word[4];
45
47{
48 /* Core address to which the segment is mapped. */
50 /* VMA recorded in the program header. */
52 /* Size of this segment in memory. */
54};
55
57 /* Protocol version number, must be zero. */
59 /* Number of segments in this map. */
61 /* The actual memory map. */
62 struct ext_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
63};
64
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. */
69{
70 /* Core address to which the segment is mapped. */
71 CORE_ADDR addr;
72 /* VMA recorded in the program header. */
73 CORE_ADDR p_vaddr;
74 /* Size of this segment in memory. */
75 long p_memsz;
76};
77
79 /* Protocol version number, must be zero. */
81 /* Number of segments in this map. */
82 int nsegs;
83 /* The actual memory map. */
84 struct int_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
85};
86
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(). */
92
93static struct int_elf32_fdpic_loadmap *
94fetch_loadmap (CORE_ADDR ldmaddr)
95{
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;
102
103 /* Fetch initial portion of the loadmap. */
104 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
105 sizeof ext_ldmbuf_partial))
106 {
107 /* Problem reading the target's memory. */
108 return NULL;
109 }
110
111 /* Extract the version. */
112 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
113 sizeof ext_ldmbuf_partial.version,
114 byte_order);
115 if (version != 0)
116 {
117 /* We only handle version 0. */
118 return NULL;
119 }
120
121 /* Extract the number of segments. */
122 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
123 sizeof ext_ldmbuf_partial.nsegs,
124 byte_order);
125
126 if (nsegs <= 0)
127 return NULL;
128
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);
133
134 /* Copy over the portion of the loadmap that's already been read. */
135 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
136
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))
141 {
142 /* Couldn't read rest of the loadmap. */
143 xfree (ext_ldmbuf);
144 return NULL;
145 }
146
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);
152
153 /* Place extracted information in internal structs. */
154 int_ldmbuf->version = version;
155 int_ldmbuf->nsegs = nsegs;
156 for (seg = 0; seg < nsegs; seg++)
157 {
158 int_ldmbuf->segs[seg].addr
159 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
160 sizeof (ext_ldmbuf->segs[seg].addr),
161 byte_order);
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),
165 byte_order);
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),
169 byte_order);
170 }
171
172 xfree (ext_ldmbuf);
173 return int_ldmbuf;
174}
175
176/* External link_map and elf32_fdpic_loadaddr struct definitions. */
177
178typedef gdb_byte ext_ptr[4];
179
181{
182 ext_ptr map; /* struct elf32_fdpic_loadmap *map; */
183 ext_ptr got_value; /* void *got_value; */
184};
185
187{
189
190 /* Absolute file name object was found in. */
191 ext_ptr l_name; /* char *l_name; */
192
193 /* Dynamic section of the shared object. */
194 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
195
196 /* Chain of loaded objects. */
197 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
198};
199
200/* Link map info to include in an allocated so_list entry. */
201
203{
205 {
206 xfree (this->map);
207 xfree (this->dyn_syms);
208 xfree (this->dyn_relocs);
209 }
210
211 /* The loadmap, digested into an easier to use form. */
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;
217
218 /* Cached dynamic symbol table and dynamic relocs initialized and
219 used only by find_canonical_descriptor_in_load_object().
220
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. */
231};
232
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(). */
238
239static void frv_relocate_main_executable (void);
240static CORE_ADDR main_got (void);
241static int enable_break2 (void);
242
243/* Implement the "open_symbol_file_object" target_so_ops method. */
244
245static int
247{
248 /* Unimplemented. */
249 return 0;
250}
251
252/* Cached value for lm_base(), below. */
253static CORE_ADDR lm_base_cache = 0;
254
255/* Link map address for main module. */
256static CORE_ADDR main_lm_addr = 0;
257
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.) */
265
266static CORE_ADDR
268{
269 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
270 struct bound_minimal_symbol got_sym;
271 CORE_ADDR addr;
272 gdb_byte buf[FRV_PTR_SIZE];
273
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
278 a bogus value. */
280 return 0;
281
282 /* If we already have a cached value, return it. */
283 if (lm_base_cache)
284 return lm_base_cache;
285
286 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
288 if (got_sym.minsym == 0)
289 {
290 solib_debug_printf ("_GLOBAL_OFFSET_TABLE_ not found.");
291 return 0;
292 }
293
294 addr = got_sym.value_address () + 8;
295
296 solib_debug_printf ("_GLOBAL_OFFSET_TABLE_ + 8 = %s",
297 hex_string_custom (addr, 8));
298
299 if (target_read_memory (addr, buf, sizeof buf) != 0)
300 return 0;
301 lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
302
303 solib_debug_printf ("lm_base_cache = %s",
304 hex_string_custom (lm_base_cache, 8));
305
306 return lm_base_cache;
307}
308
309
310/* Implement the "current_sos" target_so_ops method. */
311
312static struct so_list *
314{
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;
319
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()
323 for details.)
324
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
331 infcmd.c.) */
332 if (main_executable_lm_info == 0 && core_bfd != NULL)
334
335 /* Fetch the GOT corresponding to the main executable. */
336 mgot = main_got ();
337
338 /* Locate the address of the first link map struct. */
339 lm_addr = lm_base ();
340
341 /* We have at least one link map entry. Fetch the lot of them,
342 building the solist chain. */
343 while (lm_addr)
344 {
345 struct ext_link_map lm_buf;
346 CORE_ADDR got_addr;
347
348 solib_debug_printf ("reading link_map entry at %s",
349 hex_string_custom (lm_addr, 8));
350
351 if (target_read_memory (lm_addr, (gdb_byte *) &lm_buf,
352 sizeof (lm_buf)) != 0)
353 {
354 warning (_("frv_current_sos: Unable to read link map entry. "
355 "Shared object chain may be incomplete."));
356 break;
357 }
358
359 got_addr
361 sizeof (lm_buf.l_addr.got_value),
362 byte_order);
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)
367 {
368 struct int_elf32_fdpic_loadmap *loadmap;
369 struct so_list *sop;
370 CORE_ADDR addr;
371
372 /* Fetch the load map address. */
373 addr = extract_unsigned_integer (lm_buf.l_addr.map,
374 sizeof lm_buf.l_addr.map,
375 byte_order);
376 loadmap = fetch_loadmap (addr);
377 if (loadmap == NULL)
378 {
379 warning (_("frv_current_sos: Unable to fetch load map. "
380 "Shared object chain may be incomplete."));
381 break;
382 }
383
384 sop = XCNEW (struct so_list);
385 lm_info_frv *li = new lm_info_frv;
386 sop->lm_info = li;
387 li->map = loadmap;
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),
393 byte_order);
394 gdb::unique_xmalloc_ptr<char> name_buf
396
397 solib_debug_printf ("name = %s", name_buf.get ());
398
399 if (name_buf == nullptr)
400 warning (_("Can't read pathname for link map entry."));
401 else
402 {
403 strncpy (sop->so_name, name_buf.get (),
405 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
406 strcpy (sop->so_original_name, sop->so_name);
407 }
408
409 *sos_next_ptr = sop;
410 sos_next_ptr = &sop->next;
411 }
412 else
413 {
415 }
416
418 sizeof (lm_buf.l_next), byte_order);
419 }
420
421 enable_break2 ();
422
423 return sos_head;
424}
425
426
427/* Return 1 if PC lies in the dynamic symbol resolution code of the
428 run time loader. */
429
430static CORE_ADDR interp_text_sect_low;
431static CORE_ADDR interp_text_sect_high;
432static CORE_ADDR interp_plt_sect_low;
433static CORE_ADDR interp_plt_sect_high;
434
435static int
437{
438 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
440 || in_plt_section (pc));
441}
442
443/* Given a loadmap and an address, return the displacement needed
444 to relocate the address. */
445
446static CORE_ADDR
448 CORE_ADDR addr)
449{
450 int seg;
451
452 for (seg = 0; seg < map->nsegs; seg++)
453 {
454 if (map->segs[seg].p_vaddr <= addr
455 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
456 {
457 return map->segs[seg].addr - map->segs[seg].p_vaddr;
458 }
459 }
460
461 return 0;
462}
463
464/* Print a warning about being unable to set the dynamic linker
465 breakpoint. */
466
467static void
469{
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."));
473}
474
475/* Helper function for gdb_bfd_lookup_symbol. */
476
477static int
478cmp_name (const asymbol *sym, const void *data)
479{
480 return (strcmp (sym->name, (const char *) data) == 0);
481}
482
483/* Arrange for dynamic linker to hit breakpoint.
484
485 The dynamic linkers has, as part of its debugger interface, support
486 for arranging for the inferior to hit a breakpoint after mapping in
487 the shared libraries. This function enables that breakpoint.
488
489 On the FR-V, using the shared library (FDPIC) ABI, the symbol
490 _dl_debug_addr points to the r_debug struct which contains
491 a field called r_brk. r_brk is the address of the function
492 descriptor upon which a breakpoint must be placed. Being a
493 function descriptor, we must extract the entry point in order
494 to set the breakpoint.
495
496 Our strategy will be to get the .interp section from the
497 executable. This section will provide us with the name of the
498 interpreter. We'll open the interpreter and then look up
499 the address of _dl_debug_addr. We then relocate this address
500 using the interpreter's loadmap. Once the relocated address
501 is known, we fetch the value (address) corresponding to r_brk
502 and then use that value to fetch the entry point of the function
503 we're interested in. */
504
505static int enable_break2_done = 0;
506
507static int
509{
510 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
511 asection *interp_sect;
512
514 return 1;
515
518
519 /* Find the .interp section; if not found, warn the user and drop
520 into the old breakpoint at symbol code. */
521 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
522 ".interp");
523 if (interp_sect)
524 {
525 unsigned int interp_sect_size;
526 char *buf;
527 int status;
528 CORE_ADDR addr, interp_loadmap_addr;
529 gdb_byte addr_buf[FRV_PTR_SIZE];
530 struct int_elf32_fdpic_loadmap *ldm;
531
532 /* Read the contents of the .interp section into a local buffer;
533 the contents specify the dynamic linker this program uses. */
534 interp_sect_size = bfd_section_size (interp_sect);
535 buf = (char *) alloca (interp_sect_size);
536 bfd_get_section_contents (current_program_space->exec_bfd (),
537 interp_sect, buf, 0, interp_sect_size);
538
539 /* Now we need to figure out where the dynamic linker was
540 loaded so that we can load its symbols and place a breakpoint
541 in the dynamic linker itself.
542
543 This address is stored on the stack. However, I've been unable
544 to find any magic formula to find it for Solaris (appears to
545 be trivial on GNU/Linux). Therefore, we have to try an alternate
546 mechanism to find the dynamic linker's base address. */
547
548 gdb_bfd_ref_ptr tmp_bfd;
549 try
550 {
551 tmp_bfd = solib_bfd_open (buf);
552 }
553 catch (const gdb_exception &ex)
554 {
555 }
556
557 if (tmp_bfd == NULL)
558 {
560 return 0;
561 }
562
564 &interp_loadmap_addr, 0);
565 if (status < 0)
566 {
567 warning (_("Unable to determine dynamic linker loadmap address."));
569 return 0;
570 }
571
572 solib_debug_printf ("interp_loadmap_addr = %s",
573 hex_string_custom (interp_loadmap_addr, 8));
574
575 ldm = fetch_loadmap (interp_loadmap_addr);
576 if (ldm == NULL)
577 {
578 warning (_("Unable to load dynamic linker loadmap at address %s."),
579 hex_string_custom (interp_loadmap_addr, 8));
581 return 0;
582 }
583
584 /* Record the relocated start and end address of the dynamic linker
585 text and plt section for svr4_in_dynsym_resolve_code. */
586 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
587 if (interp_sect)
588 {
589 interp_text_sect_low = bfd_section_vma (interp_sect);
593 = interp_text_sect_low + bfd_section_size (interp_sect);
594 }
595 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
596 if (interp_sect)
597 {
598 interp_plt_sect_low = bfd_section_vma (interp_sect);
602 interp_plt_sect_low + bfd_section_size (interp_sect);
603 }
604
605 addr = gdb_bfd_lookup_symbol (tmp_bfd.get (), cmp_name, "_dl_debug_addr");
606
607 if (addr == 0)
608 {
609 warning (_("Could not find symbol _dl_debug_addr "
610 "in dynamic linker"));
612 return 0;
613 }
614
615 solib_debug_printf ("_dl_debug_addr (prior to relocation) = %s",
616 hex_string_custom (addr, 8));
617
618 addr += displacement_from_map (ldm, addr);
619
620 solib_debug_printf ("_dl_debug_addr (after relocation) = %s",
621 hex_string_custom (addr, 8));
622
623 /* Fetch the address of the r_debug struct. */
624 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
625 {
626 warning (_("Unable to fetch contents of _dl_debug_addr "
627 "(at address %s) from dynamic linker"),
628 hex_string_custom (addr, 8));
629 }
630 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
631
632 solib_debug_printf ("_dl_debug_addr[0..3] = %s",
633 hex_string_custom (addr, 8));
634
635 /* If it's zero, then the ldso hasn't initialized yet, and so
636 there are no shared libs yet loaded. */
637 if (addr == 0)
638 {
639 solib_debug_printf ("ldso not yet initialized");
640 /* Do not warn, but mark to run again. */
641 return 0;
642 }
643
644 /* Fetch the r_brk field. It's 8 bytes from the start of
645 _dl_debug_addr. */
646 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
647 {
648 warning (_("Unable to fetch _dl_debug_addr->r_brk "
649 "(at address %s) from dynamic linker"),
650 hex_string_custom (addr + 8, 8));
652 return 0;
653 }
654 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
655
656 /* Now fetch the function entry point. */
657 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
658 {
659 warning (_("Unable to fetch _dl_debug_addr->.r_brk entry point "
660 "(at address %s) from dynamic linker"),
661 hex_string_custom (addr, 8));
663 return 0;
664 }
665 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
666
667 /* We're done with the loadmap. */
668 xfree (ldm);
669
670 /* Remove all the solib event breakpoints. Their addresses
671 may have changed since the last time we ran the program. */
673
674 /* Now (finally!) create the solib breakpoint. */
676
678
679 return 1;
680 }
681
682 /* Tell the user we couldn't set a dynamic linker breakpoint. */
684
685 /* Failure return. */
686 return 0;
687}
688
689static int
691{
692 asection *interp_sect;
693 CORE_ADDR entry_point;
694
696 {
697 solib_debug_printf ("No symbol file found.");
698 return 0;
699 }
700
701 if (!entry_point_address_query (&entry_point))
702 {
703 solib_debug_printf ("Symbol file has no entry point.");
704 return 0;
705 }
706
707 /* Check for the presence of a .interp section. If there is no
708 such section, the executable is statically linked. */
709
710 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
711 ".interp");
712
713 if (interp_sect == NULL)
714 {
715 solib_debug_printf ("No .interp section found.");
716 return 0;
717 }
718
720
721 solib_debug_printf ("solib event breakpoint placed at entry point: %s",
722 hex_string_custom (entry_point, 8));
723 return 1;
724}
725
726static void
728{
729 int status;
730 CORE_ADDR exec_addr, interp_addr;
731 struct int_elf32_fdpic_loadmap *ldm;
732 int changed;
733 struct obj_section *osect;
734
736 &interp_addr, &exec_addr);
737
738 if (status < 0 || (exec_addr == 0 && interp_addr == 0))
739 {
740 /* Not using FDPIC ABI, so do nothing. */
741 return;
742 }
743
744 /* Fetch the loadmap located at ``exec_addr''. */
745 ldm = fetch_loadmap (exec_addr);
746 if (ldm == NULL)
747 error (_("Unable to load the executable's loadmap."));
748
752
754 section_offsets new_offsets (objf->section_offsets.size ());
755 changed = 0;
756
757 ALL_OBJFILE_OSECTIONS (objf, osect)
758 {
759 CORE_ADDR orig_addr, addr, offset;
760 int osect_idx;
761 int seg;
762
763 osect_idx = osect - objf->sections;
764
765 /* Current address of section. */
766 addr = osect->addr ();
767 /* Offset from where this section started. */
768 offset = objf->section_offsets[osect_idx];
769 /* Original address prior to any past relocations. */
770 orig_addr = addr - offset;
771
772 for (seg = 0; seg < ldm->nsegs; seg++)
773 {
774 if (ldm->segs[seg].p_vaddr <= orig_addr
775 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
776 {
777 new_offsets[osect_idx]
778 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
779
780 if (new_offsets[osect_idx] != offset)
781 changed = 1;
782 break;
783 }
784 }
785 }
786
787 if (changed)
788 objfile_relocate (objf, new_offsets);
789
790 /* Now that OBJF has been relocated, we can compute the GOT value
791 and stash it away. */
793}
794
795/* Implement the "create_inferior_hook" target_solib_ops method.
796
797 For the FR-V shared library ABI (FDPIC), the main executable needs
798 to be relocated. The shared library breakpoints also need to be
799 enabled. */
800
801static void
803{
804 /* Relocate main executable. */
806
807 /* Enable shared library breakpoints. */
808 if (!enable_break ())
809 {
810 warning (_("shared library handler failed to enable breakpoint"));
811 return;
812 }
813}
814
815static void
817{
818 lm_base_cache = 0;
820 main_lm_addr = 0;
821
824}
825
826static void
828{
829 lm_info_frv *li = (lm_info_frv *) so->lm_info;
830
831 delete li;
832}
833
834static void
836 struct target_section *sec)
837{
838 int seg;
839 lm_info_frv *li = (lm_info_frv *) so->lm_info;
840 int_elf32_fdpic_loadmap *map = li->map;
841
842 for (seg = 0; seg < map->nsegs; seg++)
843 {
844 if (map->segs[seg].p_vaddr <= sec->addr
845 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
846 {
847 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
848
849 sec->addr += displ;
850 sec->endaddr += displ;
851 break;
852 }
853 }
854}
855
856/* Return the GOT address associated with the main executable. Return
857 0 if it can't be found. */
858
859static CORE_ADDR
861{
862 struct bound_minimal_symbol got_sym;
863
865 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL, objf);
866 if (got_sym.minsym == 0)
867 return 0;
868
869 return got_sym.value_address ();
870}
871
872/* Find the global pointer for the given function address ADDR. */
873
874CORE_ADDR
876{
877 for (struct so_list *so : current_program_space->solibs ())
878 {
879 int seg;
880 lm_info_frv *li = (lm_info_frv *) so->lm_info;
881 int_elf32_fdpic_loadmap *map = li->map;
882
883 for (seg = 0; seg < map->nsegs; seg++)
884 {
885 if (map->segs[seg].addr <= addr
886 && addr < map->segs[seg].addr + map->segs[seg].p_memsz)
887 return li->got_value;
888 }
889 }
890
891 /* Didn't find it in any of the shared objects. So assume it's in the
892 main executable. */
893 return main_got ();
894}
895
896/* Forward declarations for frv_fdpic_find_canonical_descriptor(). */
898 (CORE_ADDR, CORE_ADDR, const char *, bfd *, lm_info_frv *);
899
900/* Given a function entry point, attempt to find the canonical descriptor
901 associated with that entry point. Return 0 if no canonical descriptor
902 could be found. */
903
904CORE_ADDR
906{
907 const char *name;
908 CORE_ADDR addr;
909 CORE_ADDR got_value;
910 struct symbol *sym;
911
912 /* Fetch the corresponding global pointer for the entry point. */
913 got_value = frv_fdpic_find_global_pointer (entry_point);
914
915 /* Attempt to find the name of the function. If the name is available,
916 it'll be used as an aid in finding matching functions in the dynamic
917 symbol table. */
918 sym = find_pc_function (entry_point);
919 if (sym == 0)
920 name = 0;
921 else
922 name = sym->linkage_name ();
923
924 /* Check the main executable. */
927 (entry_point, got_value, name, objf->obfd.get (),
929
930 /* If descriptor not found via main executable, check each load object
931 in list of shared objects. */
932 if (addr == 0)
933 {
934 for (struct so_list *so : current_program_space->solibs ())
935 {
936 lm_info_frv *li = (lm_info_frv *) so->lm_info;
937
939 (entry_point, got_value, name, so->abfd, li);
940
941 if (addr != 0)
942 break;
943 }
944 }
945
946 return addr;
947}
948
949static CORE_ADDR
951 (CORE_ADDR entry_point, CORE_ADDR got_value, const char *name, bfd *abfd,
952 lm_info_frv *lm)
953{
954 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
955 arelent *rel;
956 unsigned int i;
957 CORE_ADDR addr = 0;
958
959 /* Nothing to do if no bfd. */
960 if (abfd == 0)
961 return 0;
962
963 /* Nothing to do if no link map. */
964 if (lm == 0)
965 return 0;
966
967 /* We want to scan the dynamic relocs for R_FRV_FUNCDESC relocations.
968 (More about this later.) But in order to fetch the relocs, we
969 need to first fetch the dynamic symbols. These symbols need to
970 be cached due to the way that bfd_canonicalize_dynamic_reloc()
971 works. (See the comments in the declaration of struct lm_info
972 for more information.) */
973 if (lm->dyn_syms == NULL)
974 {
975 long storage_needed;
976 unsigned int number_of_symbols;
977
978 /* Determine amount of space needed to hold the dynamic symbol table. */
979 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
980
981 /* If there are no dynamic symbols, there's nothing to do. */
982 if (storage_needed <= 0)
983 return 0;
984
985 /* Allocate space for the dynamic symbol table. */
986 lm->dyn_syms = (asymbol **) xmalloc (storage_needed);
987
988 /* Fetch the dynamic symbol table. */
989 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, lm->dyn_syms);
990
991 if (number_of_symbols == 0)
992 return 0;
993 }
994
995 /* Fetch the dynamic relocations if not already cached. */
996 if (lm->dyn_relocs == NULL)
997 {
998 long storage_needed;
999
1000 /* Determine amount of space needed to hold the dynamic relocs. */
1001 storage_needed = bfd_get_dynamic_reloc_upper_bound (abfd);
1002
1003 /* Bail out if there are no dynamic relocs. */
1004 if (storage_needed <= 0)
1005 return 0;
1006
1007 /* Allocate space for the relocs. */
1008 lm->dyn_relocs = (arelent **) xmalloc (storage_needed);
1009
1010 /* Fetch the dynamic relocs. */
1011 lm->dyn_reloc_count
1012 = bfd_canonicalize_dynamic_reloc (abfd, lm->dyn_relocs, lm->dyn_syms);
1013 }
1014
1015 /* Search the dynamic relocs. */
1016 for (i = 0; i < lm->dyn_reloc_count; i++)
1017 {
1018 rel = lm->dyn_relocs[i];
1019
1020 /* Relocs of interest are those which meet the following
1021 criteria:
1022
1023 - the names match (assuming the caller could provide
1024 a name which matches ``entry_point'').
1025 - the relocation type must be R_FRV_FUNCDESC. Relocs
1026 of this type are used (by the dynamic linker) to
1027 look up the address of a canonical descriptor (allocating
1028 it if need be) and initializing the GOT entry referred
1029 to by the offset to the address of the descriptor.
1030
1031 These relocs of interest may be used to obtain a
1032 candidate descriptor by first adjusting the reloc's
1033 address according to the link map and then dereferencing
1034 this address (which is a GOT entry) to obtain a descriptor
1035 address. */
1036 if ((name == 0 || strcmp (name, (*rel->sym_ptr_ptr)->name) == 0)
1037 && rel->howto->type == R_FRV_FUNCDESC)
1038 {
1039 gdb_byte buf [FRV_PTR_SIZE];
1040
1041 /* Compute address of address of candidate descriptor. */
1042 addr = rel->address + displacement_from_map (lm->map, rel->address);
1043
1044 /* Fetch address of candidate descriptor. */
1045 if (target_read_memory (addr, buf, sizeof buf) != 0)
1046 continue;
1047 addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
1048
1049 /* Check for matching entry point. */
1050 if (target_read_memory (addr, buf, sizeof buf) != 0)
1051 continue;
1052 if (extract_unsigned_integer (buf, sizeof buf, byte_order)
1053 != entry_point)
1054 continue;
1055
1056 /* Check for matching got value. */
1057 if (target_read_memory (addr + 4, buf, sizeof buf) != 0)
1058 continue;
1059 if (extract_unsigned_integer (buf, sizeof buf, byte_order)
1060 != got_value)
1061 continue;
1062
1063 /* Match was successful! Exit loop. */
1064 break;
1065 }
1066 }
1067
1068 return addr;
1069}
1070
1071/* Given an objfile, return the address of its link map. This value is
1072 needed for TLS support. */
1073CORE_ADDR
1075{
1076 /* Cause frv_current_sos() to be run if it hasn't been already. */
1077 if (main_lm_addr == 0)
1078 solib_add (0, 0, 1);
1079
1080 /* frv_current_sos() will set main_lm_addr for the main executable. */
1082 return main_lm_addr;
1083
1084 /* The other link map addresses may be found by examining the list
1085 of shared libraries. */
1086 for (struct so_list *so : current_program_space->solibs ())
1087 {
1088 lm_info_frv *li = (lm_info_frv *) so->lm_info;
1089
1090 if (so->objfile == objfile)
1091 return li->lm_addr;
1092 }
1093
1094 /* Not found! */
1095 return 0;
1096}
1097
1099{
1102 nullptr,
1109};
const char *const name
Definition: aarch64-tdep.c:67
void * xmalloc(YYSIZE_T)
void xfree(void *)
if(!(yy_init))
Definition: ada-lex.c:1109
struct gdbarch * target_gdbarch(void)
Definition: arch-utils.c:1453
struct symbol * find_pc_function(CORE_ADDR pc)
Definition: blockframe.c:150
struct breakpoint * create_solib_event_breakpoint(struct gdbarch *gdbarch, CORE_ADDR address)
Definition: breakpoint.c:7764
void remove_solib_event_breakpoints(void)
Definition: breakpoint.c:7729
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition: defs.h:526
int frv_fdpic_loadmap_addresses(struct gdbarch *gdbarch, CORE_ADDR *interp_addr, CORE_ADDR *exec_addr)
Definition: frv-tdep.c:104
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
Definition: gdb_bfd.h:78
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1370
#define core_bfd
Definition: gdbcore.h:130
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition: gnu-nat.c:1791
struct bound_minimal_symbol lookup_minimal_symbol(const char *name, const char *sfile, struct objfile *objf)
Definition: minsyms.c:363
static CORE_ADDR lm_addr(struct so_list *so)
Definition: nto-tdep.c:246
void objfile_relocate(struct objfile *objfile, const section_offsets &new_offsets)
Definition: objfiles.c:716
int entry_point_address_query(CORE_ADDR *entry_p)
Definition: objfiles.c:358
#define ALL_OBJFILE_OSECTIONS(objfile, osect)
Definition: objfiles.h:130
static int in_plt_section(CORE_ADDR pc)
Definition: objfiles.h:901
struct program_space * current_program_space
Definition: progspace.c:39
CORE_ADDR frv_fetch_objfile_link_map(struct objfile *objfile)
Definition: solib-frv.c:1074
static CORE_ADDR lm_base(void)
Definition: solib-frv.c:267
static CORE_ADDR interp_text_sect_high
Definition: solib-frv.c:431
static CORE_ADDR displacement_from_map(struct int_elf32_fdpic_loadmap *map, CORE_ADDR addr)
Definition: solib-frv.c:447
static void frv_free_so(struct so_list *so)
Definition: solib-frv.c:827
static void frv_clear_solib(void)
Definition: solib-frv.c:816
static int enable_break2(void)
Definition: solib-frv.c:508
static lm_info_frv * main_executable_lm_info
Definition: solib-frv.c:237
CORE_ADDR frv_fdpic_find_global_pointer(CORE_ADDR addr)
Definition: solib-frv.c:875
static void enable_break_failure_warning(void)
Definition: solib-frv.c:468
static int cmp_name(const asymbol *sym, const void *data)
Definition: solib-frv.c:478
@ FRV_PTR_SIZE
Definition: solib-frv.c:35
static struct so_list * frv_current_sos(void)
Definition: solib-frv.c:313
static void frv_relocate_section_addresses(struct so_list *so, struct target_section *sec)
Definition: solib-frv.c:835
static int enable_break(void)
Definition: solib-frv.c:690
gdb_byte ext_Elf32_Addr[4]
Definition: solib-frv.c:43
static int frv_in_dynsym_resolve_code(CORE_ADDR pc)
Definition: solib-frv.c:436
CORE_ADDR frv_fdpic_find_canonical_descriptor(CORE_ADDR entry_point)
Definition: solib-frv.c:905
static void frv_solib_create_inferior_hook(int from_tty)
Definition: solib-frv.c:802
static int open_symbol_file_object(int from_tty)
Definition: solib-frv.c:246
static struct int_elf32_fdpic_loadmap * fetch_loadmap(CORE_ADDR ldmaddr)
Definition: solib-frv.c:94
static CORE_ADDR main_lm_addr
Definition: solib-frv.c:256
static void frv_relocate_main_executable(void)
Definition: solib-frv.c:727
static int enable_break2_done
Definition: solib-frv.c:505
static CORE_ADDR lm_base_cache
Definition: solib-frv.c:253
static CORE_ADDR interp_text_sect_low
Definition: solib-frv.c:430
const struct target_so_ops frv_so_ops
Definition: solib-frv.c:1098
static CORE_ADDR main_got(void)
Definition: solib-frv.c:860
static CORE_ADDR interp_plt_sect_high
Definition: solib-frv.c:433
gdb_byte ext_Elf32_Half[2]
Definition: solib-frv.c:42
gdb_byte ext_Elf32_Word[4]
Definition: solib-frv.c:44
static CORE_ADDR interp_plt_sect_low
Definition: solib-frv.c:432
gdb_byte ext_ptr[4]
Definition: solib-frv.c:178
static CORE_ADDR find_canonical_descriptor_in_load_object(CORE_ADDR, CORE_ADDR, const char *, bfd *, lm_info_frv *)
Definition: solib-frv.c:951
CORE_ADDR gdb_bfd_lookup_symbol(bfd *abfd, int(*match_sym)(const asymbol *, const void *), const void *data)
Definition: solib.c:1716
void solib_add(const char *pattern, int from_tty, int readsyms)
Definition: solib.c:983
gdb_bfd_ref_ptr solib_bfd_open(const char *pathname)
Definition: solib.c:449
#define solib_debug_printf(fmt,...)
Definition: solib.h:38
#define SO_NAME_MAX_PATH_SIZE
Definition: solist.h:22
CORE_ADDR value_address() const
Definition: minsyms.h:41
struct minimal_symbol * minsym
Definition: minsyms.h:49
ext_Elf32_Half version
Definition: solib-frv.c:58
struct ext_elf32_fdpic_loadseg segs[1]
Definition: solib-frv.c:62
ext_Elf32_Half nsegs
Definition: solib-frv.c:60
ext_Elf32_Addr p_vaddr
Definition: solib-frv.c:51
ext_Elf32_Addr addr
Definition: solib-frv.c:49
ext_Elf32_Word p_memsz
Definition: solib-frv.c:53
const char * linkage_name() const
Definition: symtab.h:459
struct int_elf32_fdpic_loadseg segs[1]
Definition: solib-frv.c:84
CORE_ADDR lm_addr
Definition: solib-frv.c:216
int dyn_reloc_count
Definition: solib-frv.c:230
CORE_ADDR got_value
Definition: solib-frv.c:214
arelent ** dyn_relocs
Definition: solib-frv.c:229
asymbol ** dyn_syms
Definition: solib-frv.c:228
int_elf32_fdpic_loadmap * map
Definition: solib-frv.c:212
CORE_ADDR addr() const
Definition: objfiles.h:822
CORE_ADDR offset() const
Definition: objfiles.h:810
struct obj_section * sections
Definition: objfiles.h:725
gdb_bfd_ref_ptr obfd
Definition: objfiles.h:653
::section_offsets section_offsets
Definition: objfiles.h:699
bfd * exec_bfd() const
Definition: progspace.h:264
struct objfile * symfile_object_file
Definition: progspace.h:353
so_list_range solibs() const
Definition: progspace.h:256
Definition: solist.h:35
char so_name[SO_NAME_MAX_PATH_SIZE]
Definition: solist.h:56
struct so_list * next
Definition: solist.h:40
char so_original_name[SO_NAME_MAX_PATH_SIZE]
Definition: solist.h:53
lm_info_base * lm_info
Definition: solist.h:46
CORE_ADDR endaddr
CORE_ADDR addr
std::vector< CORE_ADDR > section_offsets
Definition: symtab.h:1595
int target_read_string(CORE_ADDR addr, int len, int width, unsigned int fetchlimit, gdb::unique_xmalloc_ptr< gdb_byte > *buffer, int *bytes_read)
Definition: target.c:65
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:1771
const char version[]
Definition: version.c:2