2011-08-09 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / solib-frv.c
1 /* Handle FR-V (FDPIC) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "inferior.h"
24 #include "gdbcore.h"
25 #include "solib.h"
26 #include "solist.h"
27 #include "frv-tdep.h"
28 #include "objfiles.h"
29 #include "symtab.h"
30 #include "language.h"
31 #include "command.h"
32 #include "gdbcmd.h"
33 #include "elf/frv.h"
34 #include "exceptions.h"
35
36 /* Flag which indicates whether internal debug messages should be printed. */
37 static int solib_frv_debug;
38
39 /* FR-V pointers are four bytes wide. */
40 enum { FRV_PTR_SIZE = 4 };
41
42 /* Representation of loadmap and related structs for the FR-V FDPIC ABI. */
43
44 /* External versions; the size and alignment of the fields should be
45 the same as those on the target. When loaded, the placement of
46 the bits in each field will be the same as on the target. */
47 typedef gdb_byte ext_Elf32_Half[2];
48 typedef gdb_byte ext_Elf32_Addr[4];
49 typedef gdb_byte ext_Elf32_Word[4];
50
51 struct ext_elf32_fdpic_loadseg
52 {
53 /* Core address to which the segment is mapped. */
54 ext_Elf32_Addr addr;
55 /* VMA recorded in the program header. */
56 ext_Elf32_Addr p_vaddr;
57 /* Size of this segment in memory. */
58 ext_Elf32_Word p_memsz;
59 };
60
61 struct ext_elf32_fdpic_loadmap {
62 /* Protocol version number, must be zero. */
63 ext_Elf32_Half version;
64 /* Number of segments in this map. */
65 ext_Elf32_Half nsegs;
66 /* The actual memory map. */
67 struct ext_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
68 };
69
70 /* Internal versions; the types are GDB types and the data in each
71 of the fields is (or will be) decoded from the external struct
72 for ease of consumption. */
73 struct int_elf32_fdpic_loadseg
74 {
75 /* Core address to which the segment is mapped. */
76 CORE_ADDR addr;
77 /* VMA recorded in the program header. */
78 CORE_ADDR p_vaddr;
79 /* Size of this segment in memory. */
80 long p_memsz;
81 };
82
83 struct int_elf32_fdpic_loadmap {
84 /* Protocol version number, must be zero. */
85 int version;
86 /* Number of segments in this map. */
87 int nsegs;
88 /* The actual memory map. */
89 struct int_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
90 };
91
92 /* Given address LDMADDR, fetch and decode the loadmap at that address.
93 Return NULL if there is a problem reading the target memory or if
94 there doesn't appear to be a loadmap at the given address. The
95 allocated space (representing the loadmap) returned by this
96 function may be freed via a single call to xfree(). */
97
98 static struct int_elf32_fdpic_loadmap *
99 fetch_loadmap (CORE_ADDR ldmaddr)
100 {
101 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
102 struct ext_elf32_fdpic_loadmap ext_ldmbuf_partial;
103 struct ext_elf32_fdpic_loadmap *ext_ldmbuf;
104 struct int_elf32_fdpic_loadmap *int_ldmbuf;
105 int ext_ldmbuf_size, int_ldmbuf_size;
106 int version, seg, nsegs;
107
108 /* Fetch initial portion of the loadmap. */
109 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
110 sizeof ext_ldmbuf_partial))
111 {
112 /* Problem reading the target's memory. */
113 return NULL;
114 }
115
116 /* Extract the version. */
117 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
118 sizeof ext_ldmbuf_partial.version,
119 byte_order);
120 if (version != 0)
121 {
122 /* We only handle version 0. */
123 return NULL;
124 }
125
126 /* Extract the number of segments. */
127 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
128 sizeof ext_ldmbuf_partial.nsegs,
129 byte_order);
130
131 if (nsegs <= 0)
132 return NULL;
133
134 /* Allocate space for the complete (external) loadmap. */
135 ext_ldmbuf_size = sizeof (struct ext_elf32_fdpic_loadmap)
136 + (nsegs - 1) * sizeof (struct ext_elf32_fdpic_loadseg);
137 ext_ldmbuf = xmalloc (ext_ldmbuf_size);
138
139 /* Copy over the portion of the loadmap that's already been read. */
140 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
141
142 /* Read the rest of the loadmap from the target. */
143 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
144 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
145 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
146 {
147 /* Couldn't read rest of the loadmap. */
148 xfree (ext_ldmbuf);
149 return NULL;
150 }
151
152 /* Allocate space into which to put information extract from the
153 external loadsegs. I.e, allocate the internal loadsegs. */
154 int_ldmbuf_size = sizeof (struct int_elf32_fdpic_loadmap)
155 + (nsegs - 1) * sizeof (struct int_elf32_fdpic_loadseg);
156 int_ldmbuf = xmalloc (int_ldmbuf_size);
157
158 /* Place extracted information in internal structs. */
159 int_ldmbuf->version = version;
160 int_ldmbuf->nsegs = nsegs;
161 for (seg = 0; seg < nsegs; seg++)
162 {
163 int_ldmbuf->segs[seg].addr
164 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
165 sizeof (ext_ldmbuf->segs[seg].addr),
166 byte_order);
167 int_ldmbuf->segs[seg].p_vaddr
168 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
169 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
170 byte_order);
171 int_ldmbuf->segs[seg].p_memsz
172 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
173 sizeof (ext_ldmbuf->segs[seg].p_memsz),
174 byte_order);
175 }
176
177 xfree (ext_ldmbuf);
178 return int_ldmbuf;
179 }
180
181 /* External link_map and elf32_fdpic_loadaddr struct definitions. */
182
183 typedef gdb_byte ext_ptr[4];
184
185 struct ext_elf32_fdpic_loadaddr
186 {
187 ext_ptr map; /* struct elf32_fdpic_loadmap *map; */
188 ext_ptr got_value; /* void *got_value; */
189 };
190
191 struct ext_link_map
192 {
193 struct ext_elf32_fdpic_loadaddr l_addr;
194
195 /* Absolute file name object was found in. */
196 ext_ptr l_name; /* char *l_name; */
197
198 /* Dynamic section of the shared object. */
199 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
200
201 /* Chain of loaded objects. */
202 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
203 };
204
205 /* Link map info to include in an allocated so_list entry. */
206
207 struct lm_info
208 {
209 /* The loadmap, digested into an easier to use form. */
210 struct int_elf32_fdpic_loadmap *map;
211 /* The GOT address for this link map entry. */
212 CORE_ADDR got_value;
213 /* The link map address, needed for frv_fetch_objfile_link_map(). */
214 CORE_ADDR lm_addr;
215
216 /* Cached dynamic symbol table and dynamic relocs initialized and
217 used only by find_canonical_descriptor_in_load_object().
218
219 Note: kevinb/2004-02-26: It appears that calls to
220 bfd_canonicalize_dynamic_reloc() will use the same symbols as
221 those supplied to the first call to this function. Therefore,
222 it's important to NOT free the asymbol ** data structure
223 supplied to the first call. Thus the caching of the dynamic
224 symbols (dyn_syms) is critical for correct operation. The
225 caching of the dynamic relocations could be dispensed with. */
226 asymbol **dyn_syms;
227 arelent **dyn_relocs;
228 int dyn_reloc_count; /* Number of dynamic relocs. */
229
230 };
231
232 /* The load map, got value, etc. are not available from the chain
233 of loaded shared objects. ``main_executable_lm_info'' provides
234 a way to get at this information so that it doesn't need to be
235 frequently recomputed. Initialized by frv_relocate_main_executable(). */
236 static struct lm_info *main_executable_lm_info;
237
238 static void frv_relocate_main_executable (void);
239 static CORE_ADDR main_got (void);
240 static int enable_break2 (void);
241
242 /* Lookup the value for a specific symbol.
243
244 An expensive way to lookup the value of a single symbol for
245 bfd's that are only temporary anyway. This is used by the
246 shared library support to find the address of the debugger
247 interface structures in the shared library.
248
249 Note that 0 is specifically allowed as an error return (no
250 such symbol). */
251
252 static CORE_ADDR
253 bfd_lookup_symbol (bfd *abfd, char *symname)
254 {
255 long storage_needed;
256 asymbol *sym;
257 asymbol **symbol_table;
258 unsigned int number_of_symbols;
259 unsigned int i;
260 struct cleanup *back_to;
261 CORE_ADDR symaddr = 0;
262
263 storage_needed = bfd_get_symtab_upper_bound (abfd);
264
265 if (storage_needed > 0)
266 {
267 symbol_table = (asymbol **) xmalloc (storage_needed);
268 back_to = make_cleanup (xfree, symbol_table);
269 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
270
271 for (i = 0; i < number_of_symbols; i++)
272 {
273 sym = *symbol_table++;
274 if (strcmp (sym->name, symname) == 0)
275 {
276 /* Bfd symbols are section relative. */
277 symaddr = sym->value + sym->section->vma;
278 break;
279 }
280 }
281 do_cleanups (back_to);
282 }
283
284 if (symaddr)
285 return symaddr;
286
287 /* Look for the symbol in the dynamic string table too. */
288
289 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
290
291 if (storage_needed > 0)
292 {
293 symbol_table = (asymbol **) xmalloc (storage_needed);
294 back_to = make_cleanup (xfree, symbol_table);
295 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
296
297 for (i = 0; i < number_of_symbols; i++)
298 {
299 sym = *symbol_table++;
300 if (strcmp (sym->name, symname) == 0)
301 {
302 /* Bfd symbols are section relative. */
303 symaddr = sym->value + sym->section->vma;
304 break;
305 }
306 }
307 do_cleanups (back_to);
308 }
309
310 return symaddr;
311 }
312
313 /* Implement the "open_symbol_file_object" target_so_ops method. */
314
315 static int
316 open_symbol_file_object (void *from_ttyp)
317 {
318 /* Unimplemented. */
319 return 0;
320 }
321
322 /* Cached value for lm_base(), below. */
323 static CORE_ADDR lm_base_cache = 0;
324
325 /* Link map address for main module. */
326 static CORE_ADDR main_lm_addr = 0;
327
328 /* Return the address from which the link map chain may be found. On
329 the FR-V, this may be found in a number of ways. Assuming that the
330 main executable has already been relocated, the easiest way to find
331 this value is to look up the address of _GLOBAL_OFFSET_TABLE_. A
332 pointer to the start of the link map will be located at the word found
333 at _GLOBAL_OFFSET_TABLE_ + 8. (This is part of the dynamic linker
334 reserve area mandated by the ABI.) */
335
336 static CORE_ADDR
337 lm_base (void)
338 {
339 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
340 struct minimal_symbol *got_sym;
341 CORE_ADDR addr;
342 gdb_byte buf[FRV_PTR_SIZE];
343
344 /* One of our assumptions is that the main executable has been relocated.
345 Bail out if this has not happened. (Note that post_create_inferior()
346 in infcmd.c will call solib_add prior to solib_create_inferior_hook().
347 If we allow this to happen, lm_base_cache will be initialized with
348 a bogus value. */
349 if (main_executable_lm_info == 0)
350 return 0;
351
352 /* If we already have a cached value, return it. */
353 if (lm_base_cache)
354 return lm_base_cache;
355
356 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
357 symfile_objfile);
358 if (got_sym == 0)
359 {
360 if (solib_frv_debug)
361 fprintf_unfiltered (gdb_stdlog,
362 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
363 return 0;
364 }
365
366 addr = SYMBOL_VALUE_ADDRESS (got_sym) + 8;
367
368 if (solib_frv_debug)
369 fprintf_unfiltered (gdb_stdlog,
370 "lm_base: _GLOBAL_OFFSET_TABLE_ + 8 = %s\n",
371 hex_string_custom (addr, 8));
372
373 if (target_read_memory (addr, buf, sizeof buf) != 0)
374 return 0;
375 lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
376
377 if (solib_frv_debug)
378 fprintf_unfiltered (gdb_stdlog,
379 "lm_base: lm_base_cache = %s\n",
380 hex_string_custom (lm_base_cache, 8));
381
382 return lm_base_cache;
383 }
384
385
386 /* Implement the "current_sos" target_so_ops method. */
387
388 static struct so_list *
389 frv_current_sos (void)
390 {
391 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
392 CORE_ADDR lm_addr, mgot;
393 struct so_list *sos_head = NULL;
394 struct so_list **sos_next_ptr = &sos_head;
395
396 /* Make sure that the main executable has been relocated. This is
397 required in order to find the address of the global offset table,
398 which in turn is used to find the link map info. (See lm_base()
399 for details.)
400
401 Note that the relocation of the main executable is also performed
402 by SOLIB_CREATE_INFERIOR_HOOK(), however, in the case of core
403 files, this hook is called too late in order to be of benefit to
404 SOLIB_ADD. SOLIB_ADD eventually calls this this function,
405 frv_current_sos, and also precedes the call to
406 SOLIB_CREATE_INFERIOR_HOOK(). (See post_create_inferior() in
407 infcmd.c.) */
408 if (main_executable_lm_info == 0 && core_bfd != NULL)
409 frv_relocate_main_executable ();
410
411 /* Fetch the GOT corresponding to the main executable. */
412 mgot = main_got ();
413
414 /* Locate the address of the first link map struct. */
415 lm_addr = lm_base ();
416
417 /* We have at least one link map entry. Fetch the lot of them,
418 building the solist chain. */
419 while (lm_addr)
420 {
421 struct ext_link_map lm_buf;
422 CORE_ADDR got_addr;
423
424 if (solib_frv_debug)
425 fprintf_unfiltered (gdb_stdlog,
426 "current_sos: reading link_map entry at %s\n",
427 hex_string_custom (lm_addr, 8));
428
429 if (target_read_memory (lm_addr, (gdb_byte *) &lm_buf,
430 sizeof (lm_buf)) != 0)
431 {
432 warning (_("frv_current_sos: Unable to read link map entry. "
433 "Shared object chain may be incomplete."));
434 break;
435 }
436
437 got_addr
438 = extract_unsigned_integer (lm_buf.l_addr.got_value,
439 sizeof (lm_buf.l_addr.got_value),
440 byte_order);
441 /* If the got_addr is the same as mgotr, then we're looking at the
442 entry for the main executable. By convention, we don't include
443 this in the list of shared objects. */
444 if (got_addr != mgot)
445 {
446 int errcode;
447 char *name_buf;
448 struct int_elf32_fdpic_loadmap *loadmap;
449 struct so_list *sop;
450 CORE_ADDR addr;
451
452 /* Fetch the load map address. */
453 addr = extract_unsigned_integer (lm_buf.l_addr.map,
454 sizeof lm_buf.l_addr.map,
455 byte_order);
456 loadmap = fetch_loadmap (addr);
457 if (loadmap == NULL)
458 {
459 warning (_("frv_current_sos: Unable to fetch load map. "
460 "Shared object chain may be incomplete."));
461 break;
462 }
463
464 sop = xcalloc (1, sizeof (struct so_list));
465 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
466 sop->lm_info->map = loadmap;
467 sop->lm_info->got_value = got_addr;
468 sop->lm_info->lm_addr = lm_addr;
469 /* Fetch the name. */
470 addr = extract_unsigned_integer (lm_buf.l_name,
471 sizeof (lm_buf.l_name),
472 byte_order);
473 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
474 &errcode);
475
476 if (solib_frv_debug)
477 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
478 name_buf);
479
480 if (errcode != 0)
481 warning (_("Can't read pathname for link map entry: %s."),
482 safe_strerror (errcode));
483 else
484 {
485 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
486 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
487 xfree (name_buf);
488 strcpy (sop->so_original_name, sop->so_name);
489 }
490
491 *sos_next_ptr = sop;
492 sos_next_ptr = &sop->next;
493 }
494 else
495 {
496 main_lm_addr = lm_addr;
497 }
498
499 lm_addr = extract_unsigned_integer (lm_buf.l_next,
500 sizeof (lm_buf.l_next), byte_order);
501 }
502
503 enable_break2 ();
504
505 return sos_head;
506 }
507
508
509 /* Return 1 if PC lies in the dynamic symbol resolution code of the
510 run time loader. */
511
512 static CORE_ADDR interp_text_sect_low;
513 static CORE_ADDR interp_text_sect_high;
514 static CORE_ADDR interp_plt_sect_low;
515 static CORE_ADDR interp_plt_sect_high;
516
517 static int
518 frv_in_dynsym_resolve_code (CORE_ADDR pc)
519 {
520 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
521 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
522 || in_plt_section (pc, NULL));
523 }
524
525 /* Given a loadmap and an address, return the displacement needed
526 to relocate the address. */
527
528 static CORE_ADDR
529 displacement_from_map (struct int_elf32_fdpic_loadmap *map,
530 CORE_ADDR addr)
531 {
532 int seg;
533
534 for (seg = 0; seg < map->nsegs; seg++)
535 {
536 if (map->segs[seg].p_vaddr <= addr
537 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
538 {
539 return map->segs[seg].addr - map->segs[seg].p_vaddr;
540 }
541 }
542
543 return 0;
544 }
545
546 /* Print a warning about being unable to set the dynamic linker
547 breakpoint. */
548
549 static void
550 enable_break_failure_warning (void)
551 {
552 warning (_("Unable to find dynamic linker breakpoint function.\n"
553 "GDB will be unable to debug shared library initializers\n"
554 "and track explicitly loaded dynamic code."));
555 }
556
557 /* Arrange for dynamic linker to hit breakpoint.
558
559 The dynamic linkers has, as part of its debugger interface, support
560 for arranging for the inferior to hit a breakpoint after mapping in
561 the shared libraries. This function enables that breakpoint.
562
563 On the FR-V, using the shared library (FDPIC) ABI, the symbol
564 _dl_debug_addr points to the r_debug struct which contains
565 a field called r_brk. r_brk is the address of the function
566 descriptor upon which a breakpoint must be placed. Being a
567 function descriptor, we must extract the entry point in order
568 to set the breakpoint.
569
570 Our strategy will be to get the .interp section from the
571 executable. This section will provide us with the name of the
572 interpreter. We'll open the interpreter and then look up
573 the address of _dl_debug_addr. We then relocate this address
574 using the interpreter's loadmap. Once the relocated address
575 is known, we fetch the value (address) corresponding to r_brk
576 and then use that value to fetch the entry point of the function
577 we're interested in. */
578
579 static int enable_break2_done = 0;
580
581 static int
582 enable_break2 (void)
583 {
584 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
585 int success = 0;
586 char **bkpt_namep;
587 asection *interp_sect;
588
589 if (enable_break2_done)
590 return 1;
591
592 interp_text_sect_low = interp_text_sect_high = 0;
593 interp_plt_sect_low = interp_plt_sect_high = 0;
594
595 /* Find the .interp section; if not found, warn the user and drop
596 into the old breakpoint at symbol code. */
597 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
598 if (interp_sect)
599 {
600 unsigned int interp_sect_size;
601 gdb_byte *buf;
602 bfd *tmp_bfd = NULL;
603 int status;
604 CORE_ADDR addr, interp_loadmap_addr;
605 gdb_byte addr_buf[FRV_PTR_SIZE];
606 struct int_elf32_fdpic_loadmap *ldm;
607 volatile struct gdb_exception ex;
608
609 /* Read the contents of the .interp section into a local buffer;
610 the contents specify the dynamic linker this program uses. */
611 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
612 buf = alloca (interp_sect_size);
613 bfd_get_section_contents (exec_bfd, interp_sect,
614 buf, 0, interp_sect_size);
615
616 /* Now we need to figure out where the dynamic linker was
617 loaded so that we can load its symbols and place a breakpoint
618 in the dynamic linker itself.
619
620 This address is stored on the stack. However, I've been unable
621 to find any magic formula to find it for Solaris (appears to
622 be trivial on GNU/Linux). Therefore, we have to try an alternate
623 mechanism to find the dynamic linker's base address. */
624
625 TRY_CATCH (ex, RETURN_MASK_ALL)
626 {
627 tmp_bfd = solib_bfd_open (buf);
628 }
629 if (tmp_bfd == NULL)
630 {
631 enable_break_failure_warning ();
632 return 0;
633 }
634
635 status = frv_fdpic_loadmap_addresses (target_gdbarch,
636 &interp_loadmap_addr, 0);
637 if (status < 0)
638 {
639 warning (_("Unable to determine dynamic linker loadmap address."));
640 enable_break_failure_warning ();
641 bfd_close (tmp_bfd);
642 return 0;
643 }
644
645 if (solib_frv_debug)
646 fprintf_unfiltered (gdb_stdlog,
647 "enable_break: interp_loadmap_addr = %s\n",
648 hex_string_custom (interp_loadmap_addr, 8));
649
650 ldm = fetch_loadmap (interp_loadmap_addr);
651 if (ldm == NULL)
652 {
653 warning (_("Unable to load dynamic linker loadmap at address %s."),
654 hex_string_custom (interp_loadmap_addr, 8));
655 enable_break_failure_warning ();
656 bfd_close (tmp_bfd);
657 return 0;
658 }
659
660 /* Record the relocated start and end address of the dynamic linker
661 text and plt section for svr4_in_dynsym_resolve_code. */
662 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
663 if (interp_sect)
664 {
665 interp_text_sect_low
666 = bfd_section_vma (tmp_bfd, interp_sect);
667 interp_text_sect_low
668 += displacement_from_map (ldm, interp_text_sect_low);
669 interp_text_sect_high
670 = interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
671 }
672 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
673 if (interp_sect)
674 {
675 interp_plt_sect_low =
676 bfd_section_vma (tmp_bfd, interp_sect);
677 interp_plt_sect_low
678 += displacement_from_map (ldm, interp_plt_sect_low);
679 interp_plt_sect_high =
680 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
681 }
682
683 addr = bfd_lookup_symbol (tmp_bfd, "_dl_debug_addr");
684 if (addr == 0)
685 {
686 warning (_("Could not find symbol _dl_debug_addr "
687 "in dynamic linker"));
688 enable_break_failure_warning ();
689 bfd_close (tmp_bfd);
690 return 0;
691 }
692
693 if (solib_frv_debug)
694 fprintf_unfiltered (gdb_stdlog,
695 "enable_break: _dl_debug_addr "
696 "(prior to relocation) = %s\n",
697 hex_string_custom (addr, 8));
698
699 addr += displacement_from_map (ldm, addr);
700
701 if (solib_frv_debug)
702 fprintf_unfiltered (gdb_stdlog,
703 "enable_break: _dl_debug_addr "
704 "(after relocation) = %s\n",
705 hex_string_custom (addr, 8));
706
707 /* Fetch the address of the r_debug struct. */
708 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
709 {
710 warning (_("Unable to fetch contents of _dl_debug_addr "
711 "(at address %s) from dynamic linker"),
712 hex_string_custom (addr, 8));
713 }
714 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
715
716 if (solib_frv_debug)
717 fprintf_unfiltered (gdb_stdlog,
718 "enable_break: _dl_debug_addr[0..3] = %s\n",
719 hex_string_custom (addr, 8));
720
721 /* If it's zero, then the ldso hasn't initialized yet, and so
722 there are no shared libs yet loaded. */
723 if (addr == 0)
724 {
725 if (solib_frv_debug)
726 fprintf_unfiltered (gdb_stdlog,
727 "enable_break: ldso not yet initialized\n");
728 /* Do not warn, but mark to run again. */
729 return 0;
730 }
731
732 /* Fetch the r_brk field. It's 8 bytes from the start of
733 _dl_debug_addr. */
734 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
735 {
736 warning (_("Unable to fetch _dl_debug_addr->r_brk "
737 "(at address %s) from dynamic linker"),
738 hex_string_custom (addr + 8, 8));
739 enable_break_failure_warning ();
740 bfd_close (tmp_bfd);
741 return 0;
742 }
743 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
744
745 /* Now fetch the function entry point. */
746 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
747 {
748 warning (_("Unable to fetch _dl_debug_addr->.r_brk entry point "
749 "(at address %s) from dynamic linker"),
750 hex_string_custom (addr, 8));
751 enable_break_failure_warning ();
752 bfd_close (tmp_bfd);
753 return 0;
754 }
755 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
756
757 /* We're done with the temporary bfd. */
758 bfd_close (tmp_bfd);
759
760 /* We're also done with the loadmap. */
761 xfree (ldm);
762
763 /* Remove all the solib event breakpoints. Their addresses
764 may have changed since the last time we ran the program. */
765 remove_solib_event_breakpoints ();
766
767 /* Now (finally!) create the solib breakpoint. */
768 create_solib_event_breakpoint (target_gdbarch, addr);
769
770 enable_break2_done = 1;
771
772 return 1;
773 }
774
775 /* Tell the user we couldn't set a dynamic linker breakpoint. */
776 enable_break_failure_warning ();
777
778 /* Failure return. */
779 return 0;
780 }
781
782 static int
783 enable_break (void)
784 {
785 asection *interp_sect;
786
787 if (symfile_objfile == NULL)
788 {
789 if (solib_frv_debug)
790 fprintf_unfiltered (gdb_stdlog,
791 "enable_break: No symbol file found.\n");
792 return 0;
793 }
794
795 if (!symfile_objfile->ei.entry_point_p)
796 {
797 if (solib_frv_debug)
798 fprintf_unfiltered (gdb_stdlog,
799 "enable_break: Symbol file has no entry point.\n");
800 return 0;
801 }
802
803 /* Check for the presence of a .interp section. If there is no
804 such section, the executable is statically linked. */
805
806 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
807
808 if (interp_sect == NULL)
809 {
810 if (solib_frv_debug)
811 fprintf_unfiltered (gdb_stdlog,
812 "enable_break: No .interp section found.\n");
813 return 0;
814 }
815
816 create_solib_event_breakpoint (target_gdbarch,
817 symfile_objfile->ei.entry_point);
818
819 if (solib_frv_debug)
820 fprintf_unfiltered (gdb_stdlog,
821 "enable_break: solib event breakpoint "
822 "placed at entry point: %s\n",
823 hex_string_custom (symfile_objfile->ei.entry_point,
824 8));
825 return 1;
826 }
827
828 /* Implement the "special_symbol_handling" target_so_ops method. */
829
830 static void
831 frv_special_symbol_handling (void)
832 {
833 /* Nothing needed for FRV. */
834 }
835
836 static void
837 frv_relocate_main_executable (void)
838 {
839 int status;
840 CORE_ADDR exec_addr, interp_addr;
841 struct int_elf32_fdpic_loadmap *ldm;
842 struct cleanup *old_chain;
843 struct section_offsets *new_offsets;
844 int changed;
845 struct obj_section *osect;
846
847 status = frv_fdpic_loadmap_addresses (target_gdbarch,
848 &interp_addr, &exec_addr);
849
850 if (status < 0 || (exec_addr == 0 && interp_addr == 0))
851 {
852 /* Not using FDPIC ABI, so do nothing. */
853 return;
854 }
855
856 /* Fetch the loadmap located at ``exec_addr''. */
857 ldm = fetch_loadmap (exec_addr);
858 if (ldm == NULL)
859 error (_("Unable to load the executable's loadmap."));
860
861 if (main_executable_lm_info)
862 xfree (main_executable_lm_info);
863 main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
864 main_executable_lm_info->map = ldm;
865
866 new_offsets = xcalloc (symfile_objfile->num_sections,
867 sizeof (struct section_offsets));
868 old_chain = make_cleanup (xfree, new_offsets);
869 changed = 0;
870
871 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
872 {
873 CORE_ADDR orig_addr, addr, offset;
874 int osect_idx;
875 int seg;
876
877 osect_idx = osect->the_bfd_section->index;
878
879 /* Current address of section. */
880 addr = obj_section_addr (osect);
881 /* Offset from where this section started. */
882 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
883 /* Original address prior to any past relocations. */
884 orig_addr = addr - offset;
885
886 for (seg = 0; seg < ldm->nsegs; seg++)
887 {
888 if (ldm->segs[seg].p_vaddr <= orig_addr
889 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
890 {
891 new_offsets->offsets[osect_idx]
892 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
893
894 if (new_offsets->offsets[osect_idx] != offset)
895 changed = 1;
896 break;
897 }
898 }
899 }
900
901 if (changed)
902 objfile_relocate (symfile_objfile, new_offsets);
903
904 do_cleanups (old_chain);
905
906 /* Now that symfile_objfile has been relocated, we can compute the
907 GOT value and stash it away. */
908 main_executable_lm_info->got_value = main_got ();
909 }
910
911 /* Implement the "create_inferior_hook" target_solib_ops method.
912
913 For the FR-V shared library ABI (FDPIC), the main executable needs
914 to be relocated. The shared library breakpoints also need to be
915 enabled. */
916
917 static void
918 frv_solib_create_inferior_hook (int from_tty)
919 {
920 /* Relocate main executable. */
921 frv_relocate_main_executable ();
922
923 /* Enable shared library breakpoints. */
924 if (!enable_break ())
925 {
926 warning (_("shared library handler failed to enable breakpoint"));
927 return;
928 }
929 }
930
931 static void
932 frv_clear_solib (void)
933 {
934 lm_base_cache = 0;
935 enable_break2_done = 0;
936 main_lm_addr = 0;
937 if (main_executable_lm_info != 0)
938 {
939 xfree (main_executable_lm_info->map);
940 xfree (main_executable_lm_info->dyn_syms);
941 xfree (main_executable_lm_info->dyn_relocs);
942 xfree (main_executable_lm_info);
943 main_executable_lm_info = 0;
944 }
945 }
946
947 static void
948 frv_free_so (struct so_list *so)
949 {
950 xfree (so->lm_info->map);
951 xfree (so->lm_info->dyn_syms);
952 xfree (so->lm_info->dyn_relocs);
953 xfree (so->lm_info);
954 }
955
956 static void
957 frv_relocate_section_addresses (struct so_list *so,
958 struct target_section *sec)
959 {
960 int seg;
961 struct int_elf32_fdpic_loadmap *map;
962
963 map = so->lm_info->map;
964
965 for (seg = 0; seg < map->nsegs; seg++)
966 {
967 if (map->segs[seg].p_vaddr <= sec->addr
968 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
969 {
970 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
971
972 sec->addr += displ;
973 sec->endaddr += displ;
974 break;
975 }
976 }
977 }
978
979 /* Return the GOT address associated with the main executable. Return
980 0 if it can't be found. */
981
982 static CORE_ADDR
983 main_got (void)
984 {
985 struct minimal_symbol *got_sym;
986
987 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_",
988 NULL, symfile_objfile);
989 if (got_sym == 0)
990 return 0;
991
992 return SYMBOL_VALUE_ADDRESS (got_sym);
993 }
994
995 /* Find the global pointer for the given function address ADDR. */
996
997 CORE_ADDR
998 frv_fdpic_find_global_pointer (CORE_ADDR addr)
999 {
1000 struct so_list *so;
1001
1002 so = master_so_list ();
1003 while (so)
1004 {
1005 int seg;
1006 struct int_elf32_fdpic_loadmap *map;
1007
1008 map = so->lm_info->map;
1009
1010 for (seg = 0; seg < map->nsegs; seg++)
1011 {
1012 if (map->segs[seg].addr <= addr
1013 && addr < map->segs[seg].addr + map->segs[seg].p_memsz)
1014 return so->lm_info->got_value;
1015 }
1016
1017 so = so->next;
1018 }
1019
1020 /* Didn't find it in any of the shared objects. So assume it's in the
1021 main executable. */
1022 return main_got ();
1023 }
1024
1025 /* Forward declarations for frv_fdpic_find_canonical_descriptor(). */
1026 static CORE_ADDR find_canonical_descriptor_in_load_object
1027 (CORE_ADDR, CORE_ADDR, char *, bfd *, struct lm_info *);
1028
1029 /* Given a function entry point, attempt to find the canonical descriptor
1030 associated with that entry point. Return 0 if no canonical descriptor
1031 could be found. */
1032
1033 CORE_ADDR
1034 frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
1035 {
1036 char *name;
1037 CORE_ADDR addr;
1038 CORE_ADDR got_value;
1039 struct int_elf32_fdpic_loadmap *ldm = 0;
1040 struct symbol *sym;
1041 int status;
1042 CORE_ADDR exec_loadmap_addr;
1043
1044 /* Fetch the corresponding global pointer for the entry point. */
1045 got_value = frv_fdpic_find_global_pointer (entry_point);
1046
1047 /* Attempt to find the name of the function. If the name is available,
1048 it'll be used as an aid in finding matching functions in the dynamic
1049 symbol table. */
1050 sym = find_pc_function (entry_point);
1051 if (sym == 0)
1052 name = 0;
1053 else
1054 name = SYMBOL_LINKAGE_NAME (sym);
1055
1056 /* Check the main executable. */
1057 addr = find_canonical_descriptor_in_load_object
1058 (entry_point, got_value, name, symfile_objfile->obfd,
1059 main_executable_lm_info);
1060
1061 /* If descriptor not found via main executable, check each load object
1062 in list of shared objects. */
1063 if (addr == 0)
1064 {
1065 struct so_list *so;
1066
1067 so = master_so_list ();
1068 while (so)
1069 {
1070 addr = find_canonical_descriptor_in_load_object
1071 (entry_point, got_value, name, so->abfd, so->lm_info);
1072
1073 if (addr != 0)
1074 break;
1075
1076 so = so->next;
1077 }
1078 }
1079
1080 return addr;
1081 }
1082
1083 static CORE_ADDR
1084 find_canonical_descriptor_in_load_object
1085 (CORE_ADDR entry_point, CORE_ADDR got_value, char *name, bfd *abfd,
1086 struct lm_info *lm)
1087 {
1088 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
1089 arelent *rel;
1090 unsigned int i;
1091 CORE_ADDR addr = 0;
1092
1093 /* Nothing to do if no bfd. */
1094 if (abfd == 0)
1095 return 0;
1096
1097 /* Nothing to do if no link map. */
1098 if (lm == 0)
1099 return 0;
1100
1101 /* We want to scan the dynamic relocs for R_FRV_FUNCDESC relocations.
1102 (More about this later.) But in order to fetch the relocs, we
1103 need to first fetch the dynamic symbols. These symbols need to
1104 be cached due to the way that bfd_canonicalize_dynamic_reloc()
1105 works. (See the comments in the declaration of struct lm_info
1106 for more information.) */
1107 if (lm->dyn_syms == NULL)
1108 {
1109 long storage_needed;
1110 unsigned int number_of_symbols;
1111
1112 /* Determine amount of space needed to hold the dynamic symbol table. */
1113 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
1114
1115 /* If there are no dynamic symbols, there's nothing to do. */
1116 if (storage_needed <= 0)
1117 return 0;
1118
1119 /* Allocate space for the dynamic symbol table. */
1120 lm->dyn_syms = (asymbol **) xmalloc (storage_needed);
1121
1122 /* Fetch the dynamic symbol table. */
1123 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, lm->dyn_syms);
1124
1125 if (number_of_symbols == 0)
1126 return 0;
1127 }
1128
1129 /* Fetch the dynamic relocations if not already cached. */
1130 if (lm->dyn_relocs == NULL)
1131 {
1132 long storage_needed;
1133
1134 /* Determine amount of space needed to hold the dynamic relocs. */
1135 storage_needed = bfd_get_dynamic_reloc_upper_bound (abfd);
1136
1137 /* Bail out if there are no dynamic relocs. */
1138 if (storage_needed <= 0)
1139 return 0;
1140
1141 /* Allocate space for the relocs. */
1142 lm->dyn_relocs = (arelent **) xmalloc (storage_needed);
1143
1144 /* Fetch the dynamic relocs. */
1145 lm->dyn_reloc_count
1146 = bfd_canonicalize_dynamic_reloc (abfd, lm->dyn_relocs, lm->dyn_syms);
1147 }
1148
1149 /* Search the dynamic relocs. */
1150 for (i = 0; i < lm->dyn_reloc_count; i++)
1151 {
1152 rel = lm->dyn_relocs[i];
1153
1154 /* Relocs of interest are those which meet the following
1155 criteria:
1156
1157 - the names match (assuming the caller could provide
1158 a name which matches ``entry_point'').
1159 - the relocation type must be R_FRV_FUNCDESC. Relocs
1160 of this type are used (by the dynamic linker) to
1161 look up the address of a canonical descriptor (allocating
1162 it if need be) and initializing the GOT entry referred
1163 to by the offset to the address of the descriptor.
1164
1165 These relocs of interest may be used to obtain a
1166 candidate descriptor by first adjusting the reloc's
1167 address according to the link map and then dereferencing
1168 this address (which is a GOT entry) to obtain a descriptor
1169 address. */
1170 if ((name == 0 || strcmp (name, (*rel->sym_ptr_ptr)->name) == 0)
1171 && rel->howto->type == R_FRV_FUNCDESC)
1172 {
1173 gdb_byte buf [FRV_PTR_SIZE];
1174
1175 /* Compute address of address of candidate descriptor. */
1176 addr = rel->address + displacement_from_map (lm->map, rel->address);
1177
1178 /* Fetch address of candidate descriptor. */
1179 if (target_read_memory (addr, buf, sizeof buf) != 0)
1180 continue;
1181 addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
1182
1183 /* Check for matching entry point. */
1184 if (target_read_memory (addr, buf, sizeof buf) != 0)
1185 continue;
1186 if (extract_unsigned_integer (buf, sizeof buf, byte_order)
1187 != entry_point)
1188 continue;
1189
1190 /* Check for matching got value. */
1191 if (target_read_memory (addr + 4, buf, sizeof buf) != 0)
1192 continue;
1193 if (extract_unsigned_integer (buf, sizeof buf, byte_order)
1194 != got_value)
1195 continue;
1196
1197 /* Match was successful! Exit loop. */
1198 break;
1199 }
1200 }
1201
1202 return addr;
1203 }
1204
1205 /* Given an objfile, return the address of its link map. This value is
1206 needed for TLS support. */
1207 CORE_ADDR
1208 frv_fetch_objfile_link_map (struct objfile *objfile)
1209 {
1210 struct so_list *so;
1211
1212 /* Cause frv_current_sos() to be run if it hasn't been already. */
1213 if (main_lm_addr == 0)
1214 solib_add (0, 0, 0, 1);
1215
1216 /* frv_current_sos() will set main_lm_addr for the main executable. */
1217 if (objfile == symfile_objfile)
1218 return main_lm_addr;
1219
1220 /* The other link map addresses may be found by examining the list
1221 of shared libraries. */
1222 for (so = master_so_list (); so; so = so->next)
1223 {
1224 if (so->objfile == objfile)
1225 return so->lm_info->lm_addr;
1226 }
1227
1228 /* Not found! */
1229 return 0;
1230 }
1231
1232 struct target_so_ops frv_so_ops;
1233
1234 /* Provide a prototype to silence -Wmissing-prototypes. */
1235 extern initialize_file_ftype _initialize_frv_solib;
1236
1237 void
1238 _initialize_frv_solib (void)
1239 {
1240 frv_so_ops.relocate_section_addresses = frv_relocate_section_addresses;
1241 frv_so_ops.free_so = frv_free_so;
1242 frv_so_ops.clear_solib = frv_clear_solib;
1243 frv_so_ops.solib_create_inferior_hook = frv_solib_create_inferior_hook;
1244 frv_so_ops.special_symbol_handling = frv_special_symbol_handling;
1245 frv_so_ops.current_sos = frv_current_sos;
1246 frv_so_ops.open_symbol_file_object = open_symbol_file_object;
1247 frv_so_ops.in_dynsym_resolve_code = frv_in_dynsym_resolve_code;
1248 frv_so_ops.bfd_open = solib_bfd_open;
1249
1250 /* Debug this file's internals. */
1251 add_setshow_zinteger_cmd ("solib-frv", class_maintenance,
1252 &solib_frv_debug, _("\
1253 Set internal debugging of shared library code for FR-V."), _("\
1254 Show internal debugging of shared library code for FR-V."), _("\
1255 When non-zero, FR-V solib specific internal debugging is enabled."),
1256 NULL,
1257 NULL, /* FIXME: i18n: */
1258 &setdebuglist, &showdebuglist);
1259 }
This page took 0.055302 seconds and 5 git commands to generate.