solib-dsbt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
  2. Copyright (C) 2010-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 "objfiles.h"
  20. #include "symtab.h"
  21. #include "language.h"
  22. #include "command.h"
  23. #include "gdbcmd.h"
  24. #include "elf-bfd.h"
  25. #include "gdb_bfd.h"
  26. #define GOT_MODULE_OFFSET 4
  27. /* Flag which indicates whether internal debug messages should be printed. */
  28. static unsigned int solib_dsbt_debug = 0;
  29. /* TIC6X pointers are four bytes wide. */
  30. enum { TIC6X_PTR_SIZE = 4 };
  31. /* Representation of loadmap and related structs for the TIC6X DSBT. */
  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_dsbt_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_dsbt_loadmap {
  48. /* Protocol version number, must be zero. */
  49. ext_Elf32_Word version;
  50. /* A pointer to the DSBT table; the DSBT size and the index of this
  51. module. */
  52. ext_Elf32_Word dsbt_table_ptr;
  53. ext_Elf32_Word dsbt_size;
  54. ext_Elf32_Word dsbt_index;
  55. /* Number of segments in this map. */
  56. ext_Elf32_Word nsegs;
  57. /* The actual memory map. */
  58. struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
  59. };
  60. /* Internal versions; the types are GDB types and the data in each
  61. of the fields is (or will be) decoded from the external struct
  62. for ease of consumption. */
  63. struct int_elf32_dsbt_loadseg
  64. {
  65. /* Core address to which the segment is mapped. */
  66. CORE_ADDR addr;
  67. /* VMA recorded in the program header. */
  68. CORE_ADDR p_vaddr;
  69. /* Size of this segment in memory. */
  70. long p_memsz;
  71. };
  72. struct int_elf32_dsbt_loadmap
  73. {
  74. /* Protocol version number, must be zero. */
  75. int version;
  76. CORE_ADDR dsbt_table_ptr;
  77. /* A pointer to the DSBT table; the DSBT size and the index of this
  78. module. */
  79. int dsbt_size, dsbt_index;
  80. /* Number of segments in this map. */
  81. int nsegs;
  82. /* The actual memory map. */
  83. struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
  84. };
  85. /* External link_map and elf32_dsbt_loadaddr struct definitions. */
  86. typedef gdb_byte ext_ptr[4];
  87. struct ext_elf32_dsbt_loadaddr
  88. {
  89. ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
  90. };
  91. struct ext_link_map
  92. {
  93. struct ext_elf32_dsbt_loadaddr l_addr;
  94. /* Absolute file name object was found in. */
  95. ext_ptr l_name; /* char *l_name; */
  96. /* Dynamic section of the shared object. */
  97. ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
  98. /* Chain of loaded objects. */
  99. ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
  100. };
  101. /* Link map info to include in an allocated so_list entry */
  102. struct lm_info_dsbt : public lm_info_base
  103. {
  104. ~lm_info_dsbt ()
  105. {
  106. xfree (this->map);
  107. }
  108. /* The loadmap, digested into an easier to use form. */
  109. int_elf32_dsbt_loadmap *map = NULL;
  110. };
  111. /* Per pspace dsbt specific data. */
  112. struct dsbt_info
  113. {
  114. /* The load map, got value, etc. are not available from the chain
  115. of loaded shared objects. ``main_executable_lm_info'' provides
  116. a way to get at this information so that it doesn't need to be
  117. frequently recomputed. Initialized by dsbt_relocate_main_executable. */
  118. struct lm_info_dsbt *main_executable_lm_info = nullptr;
  119. /* Load maps for the main executable and the interpreter. These are obtained
  120. from ptrace. They are the starting point for getting into the program,
  121. and are required to find the solib list with the individual load maps for
  122. each module. */
  123. struct int_elf32_dsbt_loadmap *exec_loadmap = nullptr;
  124. struct int_elf32_dsbt_loadmap *interp_loadmap = nullptr;
  125. /* Cached value for lm_base, below. */
  126. CORE_ADDR lm_base_cache = 0;
  127. /* Link map address for main module. */
  128. CORE_ADDR main_lm_addr = 0;
  129. CORE_ADDR interp_text_sect_low = 0;
  130. CORE_ADDR interp_text_sect_high = 0;
  131. CORE_ADDR interp_plt_sect_low = 0;
  132. CORE_ADDR interp_plt_sect_high = 0;
  133. };
  134. /* Per-program-space data key. */
  135. static program_space_key<dsbt_info> solib_dsbt_pspace_data;
  136. /* Get the current dsbt data. If none is found yet, add it now. This
  137. function always returns a valid object. */
  138. static struct dsbt_info *
  139. get_dsbt_info (void)
  140. {
  141. struct dsbt_info *info;
  142. info = solib_dsbt_pspace_data.get (current_program_space);
  143. if (info != NULL)
  144. return info;
  145. return solib_dsbt_pspace_data.emplace (current_program_space);
  146. }
  147. static void
  148. dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
  149. {
  150. int i;
  151. if (map == NULL)
  152. gdb_printf ("(null)\n");
  153. else if (map->version != 0)
  154. gdb_printf (_("Unsupported map version: %d\n"), map->version);
  155. else
  156. {
  157. gdb_printf ("version %d\n", map->version);
  158. for (i = 0; i < map->nsegs; i++)
  159. gdb_printf ("%s:%s -> %s:%s\n",
  160. print_core_address (target_gdbarch (),
  161. map->segs[i].p_vaddr),
  162. print_core_address (target_gdbarch (),
  163. map->segs[i].p_vaddr
  164. + map->segs[i].p_memsz),
  165. print_core_address (target_gdbarch (), map->segs[i].addr),
  166. print_core_address (target_gdbarch (), map->segs[i].addr
  167. + map->segs[i].p_memsz));
  168. }
  169. }
  170. /* Decode int_elf32_dsbt_loadmap from BUF. */
  171. static struct int_elf32_dsbt_loadmap *
  172. decode_loadmap (const gdb_byte *buf)
  173. {
  174. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  175. const struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
  176. struct int_elf32_dsbt_loadmap *int_ldmbuf;
  177. int version, seg, nsegs;
  178. int int_ldmbuf_size;
  179. ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
  180. /* Extract the version. */
  181. version = extract_unsigned_integer (ext_ldmbuf->version,
  182. sizeof ext_ldmbuf->version,
  183. byte_order);
  184. if (version != 0)
  185. {
  186. /* We only handle version 0. */
  187. return NULL;
  188. }
  189. /* Extract the number of segments. */
  190. nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
  191. sizeof ext_ldmbuf->nsegs,
  192. byte_order);
  193. if (nsegs <= 0)
  194. return NULL;
  195. /* Allocate space into which to put information extract from the
  196. external loadsegs. I.e, allocate the internal loadsegs. */
  197. int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
  198. + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
  199. int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
  200. /* Place extracted information in internal structs. */
  201. int_ldmbuf->version = version;
  202. int_ldmbuf->nsegs = nsegs;
  203. for (seg = 0; seg < nsegs; seg++)
  204. {
  205. int_ldmbuf->segs[seg].addr
  206. = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
  207. sizeof (ext_ldmbuf->segs[seg].addr),
  208. byte_order);
  209. int_ldmbuf->segs[seg].p_vaddr
  210. = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
  211. sizeof (ext_ldmbuf->segs[seg].p_vaddr),
  212. byte_order);
  213. int_ldmbuf->segs[seg].p_memsz
  214. = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
  215. sizeof (ext_ldmbuf->segs[seg].p_memsz),
  216. byte_order);
  217. }
  218. return int_ldmbuf;
  219. }
  220. static struct dsbt_info *get_dsbt_info (void);
  221. /* Interrogate the Linux kernel to find out where the program was loaded.
  222. There are two load maps; one for the executable and one for the
  223. interpreter (only in the case of a dynamically linked executable). */
  224. static void
  225. dsbt_get_initial_loadmaps (void)
  226. {
  227. struct dsbt_info *info = get_dsbt_info ();
  228. gdb::optional<gdb::byte_vector> buf
  229. = target_read_alloc (current_inferior ()->top_target (),
  230. TARGET_OBJECT_FDPIC, "exec");
  231. if (!buf || buf->empty ())
  232. {
  233. info->exec_loadmap = NULL;
  234. error (_("Error reading DSBT exec loadmap"));
  235. }
  236. info->exec_loadmap = decode_loadmap (buf->data ());
  237. if (solib_dsbt_debug)
  238. dsbt_print_loadmap (info->exec_loadmap);
  239. buf = target_read_alloc (current_inferior ()->top_target (),
  240. TARGET_OBJECT_FDPIC, "exec");
  241. if (!buf || buf->empty ())
  242. {
  243. info->interp_loadmap = NULL;
  244. error (_("Error reading DSBT interp loadmap"));
  245. }
  246. info->interp_loadmap = decode_loadmap (buf->data ());
  247. if (solib_dsbt_debug)
  248. dsbt_print_loadmap (info->interp_loadmap);
  249. }
  250. /* Given address LDMADDR, fetch and decode the loadmap at that address.
  251. Return NULL if there is a problem reading the target memory or if
  252. there doesn't appear to be a loadmap at the given address. The
  253. allocated space (representing the loadmap) returned by this
  254. function may be freed via a single call to xfree. */
  255. static struct int_elf32_dsbt_loadmap *
  256. fetch_loadmap (CORE_ADDR ldmaddr)
  257. {
  258. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  259. struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
  260. struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
  261. struct int_elf32_dsbt_loadmap *int_ldmbuf;
  262. int ext_ldmbuf_size, int_ldmbuf_size;
  263. int version, seg, nsegs;
  264. /* Fetch initial portion of the loadmap. */
  265. if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
  266. sizeof ext_ldmbuf_partial))
  267. {
  268. /* Problem reading the target's memory. */
  269. return NULL;
  270. }
  271. /* Extract the version. */
  272. version = extract_unsigned_integer (ext_ldmbuf_partial.version,
  273. sizeof ext_ldmbuf_partial.version,
  274. byte_order);
  275. if (version != 0)
  276. {
  277. /* We only handle version 0. */
  278. return NULL;
  279. }
  280. /* Extract the number of segments. */
  281. nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
  282. sizeof ext_ldmbuf_partial.nsegs,
  283. byte_order);
  284. if (nsegs <= 0)
  285. return NULL;
  286. /* Allocate space for the complete (external) loadmap. */
  287. ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
  288. + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
  289. ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size);
  290. /* Copy over the portion of the loadmap that's already been read. */
  291. memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
  292. /* Read the rest of the loadmap from the target. */
  293. if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
  294. (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
  295. ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
  296. {
  297. /* Couldn't read rest of the loadmap. */
  298. xfree (ext_ldmbuf);
  299. return NULL;
  300. }
  301. /* Allocate space into which to put information extract from the
  302. external loadsegs. I.e, allocate the internal loadsegs. */
  303. int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
  304. + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
  305. int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
  306. /* Place extracted information in internal structs. */
  307. int_ldmbuf->version = version;
  308. int_ldmbuf->nsegs = nsegs;
  309. for (seg = 0; seg < nsegs; seg++)
  310. {
  311. int_ldmbuf->segs[seg].addr
  312. = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
  313. sizeof (ext_ldmbuf->segs[seg].addr),
  314. byte_order);
  315. int_ldmbuf->segs[seg].p_vaddr
  316. = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
  317. sizeof (ext_ldmbuf->segs[seg].p_vaddr),
  318. byte_order);
  319. int_ldmbuf->segs[seg].p_memsz
  320. = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
  321. sizeof (ext_ldmbuf->segs[seg].p_memsz),
  322. byte_order);
  323. }
  324. xfree (ext_ldmbuf);
  325. return int_ldmbuf;
  326. }
  327. static void dsbt_relocate_main_executable (void);
  328. static int enable_break (void);
  329. /* See solist.h. */
  330. static int
  331. open_symbol_file_object (int from_tty)
  332. {
  333. /* Unimplemented. */
  334. return 0;
  335. }
  336. /* Given a loadmap and an address, return the displacement needed
  337. to relocate the address. */
  338. static CORE_ADDR
  339. displacement_from_map (struct int_elf32_dsbt_loadmap *map,
  340. CORE_ADDR addr)
  341. {
  342. int seg;
  343. for (seg = 0; seg < map->nsegs; seg++)
  344. if (map->segs[seg].p_vaddr <= addr
  345. && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
  346. return map->segs[seg].addr - map->segs[seg].p_vaddr;
  347. return 0;
  348. }
  349. /* Return the address from which the link map chain may be found. On
  350. DSBT, a pointer to the start of the link map will be located at the
  351. word found at base of GOT + GOT_MODULE_OFFSET.
  352. The base of GOT may be found in a number of ways. Assuming that the
  353. main executable has already been relocated,
  354. 1 The easiest way to find this value is to look up the address of
  355. _GLOBAL_OFFSET_TABLE_.
  356. 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
  357. address of Global Offset Table. .*/
  358. static CORE_ADDR
  359. lm_base (void)
  360. {
  361. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  362. struct bound_minimal_symbol got_sym;
  363. CORE_ADDR addr;
  364. gdb_byte buf[TIC6X_PTR_SIZE];
  365. struct dsbt_info *info = get_dsbt_info ();
  366. /* One of our assumptions is that the main executable has been relocated.
  367. Bail out if this has not happened. (Note that post_create_inferior
  368. in infcmd.c will call solib_add prior to solib_create_inferior_hook.
  369. If we allow this to happen, lm_base_cache will be initialized with
  370. a bogus value. */
  371. if (info->main_executable_lm_info == 0)
  372. return 0;
  373. /* If we already have a cached value, return it. */
  374. if (info->lm_base_cache)
  375. return info->lm_base_cache;
  376. got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
  377. current_program_space->symfile_object_file);
  378. if (got_sym.minsym != 0)
  379. {
  380. addr = BMSYMBOL_VALUE_ADDRESS (got_sym);
  381. if (solib_dsbt_debug)
  382. gdb_printf (gdb_stdlog,
  383. "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
  384. (unsigned int) addr);
  385. }
  386. else if (gdb_bfd_scan_elf_dyntag (DT_PLTGOT,
  387. current_program_space->exec_bfd (),
  388. &addr, NULL))
  389. {
  390. struct int_elf32_dsbt_loadmap *ldm;
  391. dsbt_get_initial_loadmaps ();
  392. ldm = info->exec_loadmap;
  393. addr += displacement_from_map (ldm, addr);
  394. if (solib_dsbt_debug)
  395. gdb_printf (gdb_stdlog,
  396. "lm_base: get addr %x by DT_PLTGOT.\n",
  397. (unsigned int) addr);
  398. }
  399. else
  400. {
  401. if (solib_dsbt_debug)
  402. gdb_printf (gdb_stdlog,
  403. "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
  404. return 0;
  405. }
  406. addr += GOT_MODULE_OFFSET;
  407. if (solib_dsbt_debug)
  408. gdb_printf (gdb_stdlog,
  409. "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
  410. GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
  411. if (target_read_memory (addr, buf, sizeof buf) != 0)
  412. return 0;
  413. info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
  414. if (solib_dsbt_debug)
  415. gdb_printf (gdb_stdlog,
  416. "lm_base: lm_base_cache = %s\n",
  417. hex_string_custom (info->lm_base_cache, 8));
  418. return info->lm_base_cache;
  419. }
  420. /* Build a list of `struct so_list' objects describing the shared
  421. objects currently loaded in the inferior. This list does not
  422. include an entry for the main executable file.
  423. Note that we only gather information directly available from the
  424. inferior --- we don't examine any of the shared library files
  425. themselves. The declaration of `struct so_list' says which fields
  426. we provide values for. */
  427. static struct so_list *
  428. dsbt_current_sos (void)
  429. {
  430. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  431. CORE_ADDR lm_addr;
  432. struct so_list *sos_head = NULL;
  433. struct so_list **sos_next_ptr = &sos_head;
  434. struct dsbt_info *info = get_dsbt_info ();
  435. /* Make sure that the main executable has been relocated. This is
  436. required in order to find the address of the global offset table,
  437. which in turn is used to find the link map info. (See lm_base
  438. for details.)
  439. Note that the relocation of the main executable is also performed
  440. by solib_create_inferior_hook, however, in the case of core
  441. files, this hook is called too late in order to be of benefit to
  442. solib_add. solib_add eventually calls this function,
  443. dsbt_current_sos, and also precedes the call to
  444. solib_create_inferior_hook. (See post_create_inferior in
  445. infcmd.c.) */
  446. if (info->main_executable_lm_info == 0 && core_bfd != NULL)
  447. dsbt_relocate_main_executable ();
  448. /* Locate the address of the first link map struct. */
  449. lm_addr = lm_base ();
  450. /* We have at least one link map entry. Fetch the lot of them,
  451. building the solist chain. */
  452. while (lm_addr)
  453. {
  454. struct ext_link_map lm_buf;
  455. ext_Elf32_Word indexword;
  456. CORE_ADDR map_addr;
  457. int dsbt_index;
  458. int ret;
  459. if (solib_dsbt_debug)
  460. gdb_printf (gdb_stdlog,
  461. "current_sos: reading link_map entry at %s\n",
  462. hex_string_custom (lm_addr, 8));
  463. ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
  464. if (ret)
  465. {
  466. warning (_("dsbt_current_sos: Unable to read link map entry."
  467. " Shared object chain may be incomplete."));
  468. break;
  469. }
  470. /* Fetch the load map address. */
  471. map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
  472. sizeof lm_buf.l_addr.map,
  473. byte_order);
  474. ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
  475. sizeof indexword);
  476. if (ret)
  477. {
  478. warning (_("dsbt_current_sos: Unable to read dsbt index."
  479. " Shared object chain may be incomplete."));
  480. break;
  481. }
  482. dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
  483. byte_order);
  484. /* If the DSBT index is zero, then we're looking at the entry
  485. for the main executable. By convention, we don't include
  486. this in the list of shared objects. */
  487. if (dsbt_index != 0)
  488. {
  489. struct int_elf32_dsbt_loadmap *loadmap;
  490. struct so_list *sop;
  491. CORE_ADDR addr;
  492. loadmap = fetch_loadmap (map_addr);
  493. if (loadmap == NULL)
  494. {
  495. warning (_("dsbt_current_sos: Unable to fetch load map."
  496. " Shared object chain may be incomplete."));
  497. break;
  498. }
  499. sop = XCNEW (struct so_list);
  500. lm_info_dsbt *li = new lm_info_dsbt;
  501. sop->lm_info = li;
  502. li->map = loadmap;
  503. /* Fetch the name. */
  504. addr = extract_unsigned_integer (lm_buf.l_name,
  505. sizeof (lm_buf.l_name),
  506. byte_order);
  507. gdb::unique_xmalloc_ptr<char> name_buf
  508. = target_read_string (addr, SO_NAME_MAX_PATH_SIZE - 1);
  509. if (name_buf == nullptr)
  510. warning (_("Can't read pathname for link map entry."));
  511. else
  512. {
  513. if (solib_dsbt_debug)
  514. gdb_printf (gdb_stdlog, "current_sos: name = %s\n",
  515. name_buf.get ());
  516. strncpy (sop->so_name, name_buf.get (), SO_NAME_MAX_PATH_SIZE - 1);
  517. sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  518. strcpy (sop->so_original_name, sop->so_name);
  519. }
  520. *sos_next_ptr = sop;
  521. sos_next_ptr = &sop->next;
  522. }
  523. else
  524. {
  525. info->main_lm_addr = lm_addr;
  526. }
  527. lm_addr = extract_unsigned_integer (lm_buf.l_next,
  528. sizeof (lm_buf.l_next), byte_order);
  529. }
  530. return sos_head;
  531. }
  532. /* Return 1 if PC lies in the dynamic symbol resolution code of the
  533. run time loader. */
  534. static int
  535. dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
  536. {
  537. struct dsbt_info *info = get_dsbt_info ();
  538. return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
  539. || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
  540. || in_plt_section (pc));
  541. }
  542. /* Print a warning about being unable to set the dynamic linker
  543. breakpoint. */
  544. static void
  545. enable_break_failure_warning (void)
  546. {
  547. warning (_("Unable to find dynamic linker breakpoint function.\n"
  548. "GDB will be unable to debug shared library initializers\n"
  549. "and track explicitly loaded dynamic code."));
  550. }
  551. /* Helper function for gdb_bfd_lookup_symbol. */
  552. static int
  553. cmp_name (const asymbol *sym, const void *data)
  554. {
  555. return (strcmp (sym->name, (const char *) data) == 0);
  556. }
  557. /* The dynamic linkers has, as part of its debugger interface, support
  558. for arranging for the inferior to hit a breakpoint after mapping in
  559. the shared libraries. This function enables that breakpoint.
  560. On the TIC6X, using the shared library (DSBT), GDB can try to place
  561. a breakpoint on '_dl_debug_state' to monitor the shared library
  562. event. */
  563. static int
  564. enable_break (void)
  565. {
  566. asection *interp_sect;
  567. struct dsbt_info *info;
  568. if (current_program_space->exec_bfd () == NULL)
  569. return 0;
  570. if (!target_has_execution ())
  571. return 0;
  572. info = get_dsbt_info ();
  573. info->interp_text_sect_low = 0;
  574. info->interp_text_sect_high = 0;
  575. info->interp_plt_sect_low = 0;
  576. info->interp_plt_sect_high = 0;
  577. /* Find the .interp section; if not found, warn the user and drop
  578. into the old breakpoint at symbol code. */
  579. interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
  580. ".interp");
  581. if (interp_sect)
  582. {
  583. unsigned int interp_sect_size;
  584. char *buf;
  585. CORE_ADDR addr;
  586. struct int_elf32_dsbt_loadmap *ldm;
  587. int ret;
  588. /* Read the contents of the .interp section into a local buffer;
  589. the contents specify the dynamic linker this program uses. */
  590. interp_sect_size = bfd_section_size (interp_sect);
  591. buf = (char *) alloca (interp_sect_size);
  592. bfd_get_section_contents (current_program_space->exec_bfd (),
  593. interp_sect, buf, 0, interp_sect_size);
  594. /* Now we need to figure out where the dynamic linker was
  595. loaded so that we can load its symbols and place a breakpoint
  596. in the dynamic linker itself. */
  597. gdb_bfd_ref_ptr tmp_bfd;
  598. try
  599. {
  600. tmp_bfd = solib_bfd_open (buf);
  601. }
  602. catch (const gdb_exception &ex)
  603. {
  604. }
  605. if (tmp_bfd == NULL)
  606. {
  607. enable_break_failure_warning ();
  608. return 0;
  609. }
  610. dsbt_get_initial_loadmaps ();
  611. ldm = info->interp_loadmap;
  612. /* Record the relocated start and end address of the dynamic linker
  613. text and plt section for dsbt_in_dynsym_resolve_code. */
  614. interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
  615. if (interp_sect)
  616. {
  617. info->interp_text_sect_low = bfd_section_vma (interp_sect);
  618. info->interp_text_sect_low
  619. += displacement_from_map (ldm, info->interp_text_sect_low);
  620. info->interp_text_sect_high
  621. = info->interp_text_sect_low + bfd_section_size (interp_sect);
  622. }
  623. interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
  624. if (interp_sect)
  625. {
  626. info->interp_plt_sect_low = bfd_section_vma (interp_sect);
  627. info->interp_plt_sect_low
  628. += displacement_from_map (ldm, info->interp_plt_sect_low);
  629. info->interp_plt_sect_high
  630. = info->interp_plt_sect_low + bfd_section_size (interp_sect);
  631. }
  632. addr = gdb_bfd_lookup_symbol (tmp_bfd.get (), cmp_name,
  633. "_dl_debug_state");
  634. if (addr != 0)
  635. {
  636. if (solib_dsbt_debug)
  637. gdb_printf (gdb_stdlog,
  638. "enable_break: _dl_debug_state (prior to relocation) = %s\n",
  639. hex_string_custom (addr, 8));
  640. addr += displacement_from_map (ldm, addr);
  641. if (solib_dsbt_debug)
  642. gdb_printf (gdb_stdlog,
  643. "enable_break: _dl_debug_state (after relocation) = %s\n",
  644. hex_string_custom (addr, 8));
  645. /* Now (finally!) create the solib breakpoint. */
  646. create_solib_event_breakpoint (target_gdbarch (), addr);
  647. ret = 1;
  648. }
  649. else
  650. {
  651. if (solib_dsbt_debug)
  652. gdb_printf (gdb_stdlog,
  653. "enable_break: _dl_debug_state is not found\n");
  654. ret = 0;
  655. }
  656. /* We're done with the loadmap. */
  657. xfree (ldm);
  658. return ret;
  659. }
  660. /* Tell the user we couldn't set a dynamic linker breakpoint. */
  661. enable_break_failure_warning ();
  662. /* Failure return. */
  663. return 0;
  664. }
  665. static void
  666. dsbt_relocate_main_executable (void)
  667. {
  668. struct int_elf32_dsbt_loadmap *ldm;
  669. int changed;
  670. struct obj_section *osect;
  671. struct dsbt_info *info = get_dsbt_info ();
  672. dsbt_get_initial_loadmaps ();
  673. ldm = info->exec_loadmap;
  674. delete info->main_executable_lm_info;
  675. info->main_executable_lm_info = new lm_info_dsbt;
  676. info->main_executable_lm_info->map = ldm;
  677. objfile *objf = current_program_space->symfile_object_file;
  678. section_offsets new_offsets (objf->section_offsets.size ());
  679. changed = 0;
  680. ALL_OBJFILE_OSECTIONS (objf, osect)
  681. {
  682. CORE_ADDR orig_addr, addr, offset;
  683. int osect_idx;
  684. int seg;
  685. osect_idx = osect - objf->sections;
  686. /* Current address of section. */
  687. addr = osect->addr ();
  688. /* Offset from where this section started. */
  689. offset = objf->section_offsets[osect_idx];
  690. /* Original address prior to any past relocations. */
  691. orig_addr = addr - offset;
  692. for (seg = 0; seg < ldm->nsegs; seg++)
  693. {
  694. if (ldm->segs[seg].p_vaddr <= orig_addr
  695. && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
  696. {
  697. new_offsets[osect_idx]
  698. = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
  699. if (new_offsets[osect_idx] != offset)
  700. changed = 1;
  701. break;
  702. }
  703. }
  704. }
  705. if (changed)
  706. objfile_relocate (objf, new_offsets);
  707. /* Now that OBJF has been relocated, we can compute the GOT value
  708. and stash it away. */
  709. }
  710. /* When gdb starts up the inferior, it nurses it along (through the
  711. shell) until it is ready to execute it's first instruction. At this
  712. point, this function gets called via solib_create_inferior_hook.
  713. For the DSBT shared library, the main executable needs to be relocated.
  714. The shared library breakpoints also need to be enabled. */
  715. static void
  716. dsbt_solib_create_inferior_hook (int from_tty)
  717. {
  718. /* Relocate main executable. */
  719. dsbt_relocate_main_executable ();
  720. /* Enable shared library breakpoints. */
  721. if (!enable_break ())
  722. {
  723. warning (_("shared library handler failed to enable breakpoint"));
  724. return;
  725. }
  726. }
  727. static void
  728. dsbt_clear_solib (void)
  729. {
  730. struct dsbt_info *info = get_dsbt_info ();
  731. info->lm_base_cache = 0;
  732. info->main_lm_addr = 0;
  733. delete info->main_executable_lm_info;
  734. info->main_executable_lm_info = NULL;
  735. }
  736. static void
  737. dsbt_free_so (struct so_list *so)
  738. {
  739. lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
  740. delete li;
  741. }
  742. static void
  743. dsbt_relocate_section_addresses (struct so_list *so,
  744. struct target_section *sec)
  745. {
  746. int seg;
  747. lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
  748. int_elf32_dsbt_loadmap *map = li->map;
  749. for (seg = 0; seg < map->nsegs; seg++)
  750. {
  751. if (map->segs[seg].p_vaddr <= sec->addr
  752. && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
  753. {
  754. CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
  755. sec->addr += displ;
  756. sec->endaddr += displ;
  757. break;
  758. }
  759. }
  760. }
  761. static void
  762. show_dsbt_debug (struct ui_file *file, int from_tty,
  763. struct cmd_list_element *c, const char *value)
  764. {
  765. gdb_printf (file, _("solib-dsbt debugging is %s.\n"), value);
  766. }
  767. struct target_so_ops dsbt_so_ops;
  768. void _initialize_dsbt_solib ();
  769. void
  770. _initialize_dsbt_solib ()
  771. {
  772. dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
  773. dsbt_so_ops.free_so = dsbt_free_so;
  774. dsbt_so_ops.clear_solib = dsbt_clear_solib;
  775. dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
  776. dsbt_so_ops.current_sos = dsbt_current_sos;
  777. dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
  778. dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
  779. dsbt_so_ops.bfd_open = solib_bfd_open;
  780. /* Debug this file's internals. */
  781. add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
  782. &solib_dsbt_debug, _("\
  783. Set internal debugging of shared library code for DSBT ELF."), _("\
  784. Show internal debugging of shared library code for DSBT ELF."), _("\
  785. When non-zero, DSBT solib specific internal debugging is enabled."),
  786. NULL,
  787. show_dsbt_debug,
  788. &setdebuglist, &showdebuglist);
  789. }