solib-frv.c 34 KB

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