Gtables.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (c) 2001-2005 Hewlett-Packard Development Company, L.P.
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include <assert.h>
  22. #include <stdlib.h>
  23. #include <stddef.h>
  24. #include "unwind_i.h"
  25. #ifdef HAVE_IA64INTRIN_H
  26. # include <ia64intrin.h>
  27. #endif
  28. extern unw_addr_space_t _ULia64_local_addr_space;
  29. struct ia64_table_entry
  30. {
  31. uint64_t start_offset;
  32. uint64_t end_offset;
  33. uint64_t info_offset;
  34. };
  35. #ifdef UNW_LOCAL_ONLY
  36. static inline int
  37. is_local_addr_space (unw_addr_space_t as)
  38. {
  39. return 1;
  40. }
  41. static inline int
  42. read_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *valp, void *arg)
  43. {
  44. *valp = *(unw_word_t *) addr;
  45. return 0;
  46. }
  47. #else /* !UNW_LOCAL_ONLY */
  48. static inline int
  49. is_local_addr_space (unw_addr_space_t as)
  50. {
  51. return as == unw_local_addr_space;
  52. }
  53. static inline int
  54. read_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *valp, void *arg)
  55. {
  56. unw_accessors_t *a = unw_get_accessors (as);
  57. return (*a->access_mem) (as, addr, valp, 0, arg);
  58. }
  59. /* Helper macro for reading an ia64_table_entry from remote memory. */
  60. #define remote_read(addr, member) \
  61. (*a->access_mem) (as, (addr) + offsetof (struct ia64_table_entry, \
  62. member), &member, 0, arg)
  63. /* Lookup an unwind-table entry in remote memory. Returns 1 if an
  64. entry is found, 0 if no entry is found, negative if an error
  65. occurred reading remote memory. */
  66. static int
  67. remote_lookup (unw_addr_space_t as,
  68. unw_word_t table, size_t table_size, unw_word_t rel_ip,
  69. struct ia64_table_entry *e, void *arg)
  70. {
  71. unw_word_t e_addr = 0, start_offset, end_offset, info_offset;
  72. unw_accessors_t *a = unw_get_accessors (as);
  73. unsigned long lo, hi, mid;
  74. int ret;
  75. /* do a binary search for right entry: */
  76. for (lo = 0, hi = table_size / sizeof (struct ia64_table_entry); lo < hi;)
  77. {
  78. mid = (lo + hi) / 2;
  79. e_addr = table + mid * sizeof (struct ia64_table_entry);
  80. if ((ret = remote_read (e_addr, start_offset)) < 0)
  81. return ret;
  82. if (rel_ip < start_offset)
  83. hi = mid;
  84. else
  85. {
  86. if ((ret = remote_read (e_addr, end_offset)) < 0)
  87. return ret;
  88. if (rel_ip >= end_offset)
  89. lo = mid + 1;
  90. else
  91. break;
  92. }
  93. }
  94. if (rel_ip < start_offset || rel_ip >= end_offset)
  95. return 0;
  96. e->start_offset = start_offset;
  97. e->end_offset = end_offset;
  98. if ((ret = remote_read (e_addr, info_offset)) < 0)
  99. return ret;
  100. e->info_offset = info_offset;
  101. return 1;
  102. }
  103. HIDDEN void
  104. tdep_put_unwind_info (unw_addr_space_t as, unw_proc_info_t *pi, void *arg)
  105. {
  106. if (!pi->unwind_info)
  107. return;
  108. if (is_local_addr_space (as))
  109. {
  110. free (pi->unwind_info);
  111. pi->unwind_info = NULL;
  112. }
  113. }
  114. PROTECTED unw_word_t
  115. _Uia64_find_dyn_list (unw_addr_space_t as, unw_dyn_info_t *di, void *arg)
  116. {
  117. unw_word_t hdr_addr, info_addr, hdr, directives, pers, cookie, off;
  118. unw_word_t start_offset, end_offset, info_offset, segbase;
  119. struct ia64_table_entry *e;
  120. size_t table_size;
  121. unw_word_t gp = di->gp;
  122. int ret;
  123. switch (di->format)
  124. {
  125. case UNW_INFO_FORMAT_DYNAMIC:
  126. default:
  127. return 0;
  128. case UNW_INFO_FORMAT_TABLE:
  129. e = (struct ia64_table_entry *) di->u.ti.table_data;
  130. table_size = di->u.ti.table_len * sizeof (di->u.ti.table_data[0]);
  131. segbase = di->u.ti.segbase;
  132. if (table_size < sizeof (struct ia64_table_entry))
  133. return 0;
  134. start_offset = e[0].start_offset;
  135. end_offset = e[0].end_offset;
  136. info_offset = e[0].info_offset;
  137. break;
  138. case UNW_INFO_FORMAT_REMOTE_TABLE:
  139. {
  140. unw_accessors_t *a = unw_get_accessors (as);
  141. unw_word_t e_addr = di->u.rti.table_data;
  142. table_size = di->u.rti.table_len * sizeof (unw_word_t);
  143. segbase = di->u.rti.segbase;
  144. if (table_size < sizeof (struct ia64_table_entry))
  145. return 0;
  146. if ( (ret = remote_read (e_addr, start_offset) < 0)
  147. || (ret = remote_read (e_addr, end_offset) < 0)
  148. || (ret = remote_read (e_addr, info_offset) < 0))
  149. return ret;
  150. }
  151. break;
  152. }
  153. if (start_offset != end_offset)
  154. /* dyn-list entry cover a zero-length "procedure" and should be
  155. first entry (note: technically a binary could contain code
  156. below the segment base, but this doesn't happen for normal
  157. binaries and certainly doesn't happen when libunwind is a
  158. separate shared object. For weird cases, the application may
  159. have to provide its own (slower) version of this routine. */
  160. return 0;
  161. hdr_addr = info_offset + segbase;
  162. info_addr = hdr_addr + 8;
  163. /* read the header word: */
  164. if ((ret = read_mem (as, hdr_addr, &hdr, arg)) < 0)
  165. return ret;
  166. if (IA64_UNW_VER (hdr) != 1
  167. || IA64_UNW_FLAG_EHANDLER (hdr) || IA64_UNW_FLAG_UHANDLER (hdr))
  168. /* dyn-list entry must be version 1 and doesn't have ehandler
  169. or uhandler */
  170. return 0;
  171. if (IA64_UNW_LENGTH (hdr) != 1)
  172. /* dyn-list entry must consist of a single word of NOP directives */
  173. return 0;
  174. if ( ((ret = read_mem (as, info_addr, &directives, arg)) < 0)
  175. || ((ret = read_mem (as, info_addr + 0x08, &pers, arg)) < 0)
  176. || ((ret = read_mem (as, info_addr + 0x10, &cookie, arg)) < 0)
  177. || ((ret = read_mem (as, info_addr + 0x18, &off, arg)) < 0))
  178. return 0;
  179. if (directives != 0 || pers != 0
  180. || (!as->big_endian && cookie != 0x7473696c2d6e7964ULL)
  181. || ( as->big_endian && cookie != 0x64796e2d6c697374ULL))
  182. return 0;
  183. /* OK, we ran the gauntlet and found it: */
  184. return off + gp;
  185. }
  186. #endif /* !UNW_LOCAL_ONLY */
  187. static inline const struct ia64_table_entry *
  188. lookup (struct ia64_table_entry *table, size_t table_size, unw_word_t rel_ip)
  189. {
  190. const struct ia64_table_entry *e = 0;
  191. unsigned long lo, hi, mid;
  192. /* do a binary search for right entry: */
  193. for (lo = 0, hi = table_size / sizeof (struct ia64_table_entry); lo < hi;)
  194. {
  195. mid = (lo + hi) / 2;
  196. e = table + mid;
  197. if (rel_ip < e->start_offset)
  198. hi = mid;
  199. else if (rel_ip >= e->end_offset)
  200. lo = mid + 1;
  201. else
  202. break;
  203. }
  204. if (rel_ip < e->start_offset || rel_ip >= e->end_offset)
  205. return NULL;
  206. return e;
  207. }
  208. PROTECTED int
  209. unw_search_ia64_unwind_table (unw_addr_space_t as, unw_word_t ip,
  210. unw_dyn_info_t *di, unw_proc_info_t *pi,
  211. int need_unwind_info, void *arg)
  212. {
  213. unw_word_t addr, hdr_addr, info_addr, info_end_addr, hdr, *wp;
  214. const struct ia64_table_entry *e = NULL;
  215. unw_word_t handler_offset, segbase = 0;
  216. struct ia64_table_entry ent;
  217. int ret, is_local;
  218. assert ((di->format == UNW_INFO_FORMAT_TABLE
  219. || di->format == UNW_INFO_FORMAT_REMOTE_TABLE)
  220. && (ip >= di->start_ip && ip < di->end_ip));
  221. pi->flags = 0;
  222. pi->unwind_info = 0;
  223. pi->handler = 0;
  224. if (likely (di->format == UNW_INFO_FORMAT_TABLE))
  225. {
  226. segbase = di->u.ti.segbase;
  227. e = lookup ((struct ia64_table_entry *) di->u.ti.table_data,
  228. di->u.ti.table_len * sizeof (unw_word_t),
  229. ip - segbase);
  230. }
  231. #ifndef UNW_LOCAL_ONLY
  232. else
  233. {
  234. segbase = di->u.rti.segbase;
  235. if ((ret = remote_lookup (as, di->u.rti.table_data,
  236. di->u.rti.table_len * sizeof (unw_word_t),
  237. ip - segbase, &ent, arg)) < 0)
  238. return ret;
  239. if (ret)
  240. e = &ent;
  241. }
  242. #endif
  243. if (!e)
  244. {
  245. /* IP is inside this table's range, but there is no explicit
  246. unwind info => use default conventions (i.e., this is NOT an
  247. error). */
  248. memset (pi, 0, sizeof (*pi));
  249. pi->start_ip = 0;
  250. pi->end_ip = 0;
  251. pi->gp = di->gp;
  252. pi->lsda = 0;
  253. return 0;
  254. }
  255. pi->start_ip = e->start_offset + segbase;
  256. pi->end_ip = e->end_offset + segbase;
  257. hdr_addr = e->info_offset + segbase;
  258. info_addr = hdr_addr + 8;
  259. /* Read the header word. Note: the actual unwind-info is always
  260. assumed to reside in memory, independent of whether di->format is
  261. UNW_INFO_FORMAT_TABLE or UNW_INFO_FORMAT_REMOTE_TABLE. */
  262. if ((ret = read_mem (as, hdr_addr, &hdr, arg)) < 0)
  263. return ret;
  264. if (IA64_UNW_VER (hdr) != 1)
  265. {
  266. Debug (1, "Unknown header version %ld (hdr word=0x%lx @ 0x%lx)\n",
  267. IA64_UNW_VER (hdr), (unsigned long) hdr,
  268. (unsigned long) hdr_addr);
  269. return -UNW_EBADVERSION;
  270. }
  271. info_end_addr = info_addr + 8 * IA64_UNW_LENGTH (hdr);
  272. is_local = is_local_addr_space (as);
  273. /* If we must have the unwind-info, return it. Also, if we are in
  274. the local address-space, return the unwind-info because it's so
  275. cheap to do so and it may come in handy later on. */
  276. if (need_unwind_info || is_local)
  277. {
  278. pi->unwind_info_size = 8 * IA64_UNW_LENGTH (hdr);
  279. if (is_local)
  280. pi->unwind_info = (void *) (uintptr_t) info_addr;
  281. else
  282. {
  283. /* Internalize unwind info. Note: since we're doing this
  284. only for non-local address spaces, there is no
  285. signal-safety issue and it is OK to use malloc()/free(). */
  286. pi->unwind_info = malloc (8 * IA64_UNW_LENGTH (hdr));
  287. if (!pi->unwind_info)
  288. return -UNW_ENOMEM;
  289. wp = (unw_word_t *) pi->unwind_info;
  290. for (addr = info_addr; addr < info_end_addr; addr += 8, ++wp)
  291. {
  292. if ((ret = read_mem (as, addr, wp, arg)) < 0)
  293. {
  294. free (pi->unwind_info);
  295. return ret;
  296. }
  297. }
  298. }
  299. }
  300. if (IA64_UNW_FLAG_EHANDLER (hdr) || IA64_UNW_FLAG_UHANDLER (hdr))
  301. {
  302. /* read the personality routine address (address is gp-relative): */
  303. if ((ret = read_mem (as, info_end_addr, &handler_offset, arg)) < 0)
  304. return ret;
  305. Debug (4, "handler ptr @ offset=%lx, gp=%lx\n", handler_offset, di->gp);
  306. if ((read_mem (as, handler_offset + di->gp, &pi->handler, arg)) < 0)
  307. return ret;
  308. }
  309. pi->lsda = info_end_addr + 8;
  310. pi->gp = di->gp;
  311. pi->format = di->format;
  312. return 0;
  313. }
  314. #ifndef UNW_REMOTE_ONLY
  315. # if defined(HAVE_DL_ITERATE_PHDR)
  316. # include <link.h>
  317. # include <stdlib.h>
  318. # if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 2) \
  319. || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && !defined(DT_CONFIG))
  320. # error You need GLIBC 2.2.4 or later on IA-64 Linux
  321. # endif
  322. # if defined(HAVE_GETUNWIND)
  323. extern unsigned long getunwind (void *buf, size_t len);
  324. # else /* HAVE_GETUNWIND */
  325. # include <unistd.h>
  326. # include <sys/syscall.h>
  327. # ifndef __NR_getunwind
  328. # define __NR_getunwind 1215
  329. # endif
  330. static unsigned long
  331. getunwind (void *buf, size_t len)
  332. {
  333. return syscall (SYS_getunwind, buf, len);
  334. }
  335. # endif /* HAVE_GETUNWIND */
  336. static unw_dyn_info_t kernel_table;
  337. static int
  338. get_kernel_table (unw_dyn_info_t *di)
  339. {
  340. struct ia64_table_entry *ktab, *etab;
  341. size_t size;
  342. Debug (16, "getting kernel table");
  343. size = getunwind (NULL, 0);
  344. ktab = sos_alloc (size);
  345. if (!ktab)
  346. {
  347. Dprintf (__FILE__".%s: failed to allocate %zu bytes",
  348. __FUNCTION__, size);
  349. return -UNW_ENOMEM;
  350. }
  351. getunwind (ktab, size);
  352. /* Determine length of kernel's unwind table & relocate its entries. */
  353. for (etab = ktab; etab->start_offset; ++etab)
  354. etab->info_offset += (uint64_t) ktab;
  355. di->format = UNW_INFO_FORMAT_TABLE;
  356. di->gp = 0;
  357. di->start_ip = ktab[0].start_offset;
  358. di->end_ip = etab[-1].end_offset;
  359. di->u.ti.name_ptr = (unw_word_t) "<kernel>";
  360. di->u.ti.segbase = 0;
  361. di->u.ti.table_len = ((char *) etab - (char *) ktab) / sizeof (unw_word_t);
  362. di->u.ti.table_data = (unw_word_t *) ktab;
  363. Debug (16, "found table `%s': [%lx-%lx) segbase=%lx len=%lu\n",
  364. (char *) di->u.ti.name_ptr, di->start_ip, di->end_ip,
  365. di->u.ti.segbase, di->u.ti.table_len);
  366. return 0;
  367. }
  368. # ifndef UNW_LOCAL_ONLY
  369. /* This is exported for the benefit of libunwind-ptrace.a. */
  370. PROTECTED int
  371. _Uia64_get_kernel_table (unw_dyn_info_t *di)
  372. {
  373. int ret;
  374. if (!kernel_table.u.ti.table_data)
  375. if ((ret = get_kernel_table (&kernel_table)) < 0)
  376. return ret;
  377. memcpy (di, &kernel_table, sizeof (*di));
  378. return 0;
  379. }
  380. # endif /* !UNW_LOCAL_ONLY */
  381. static inline unsigned long
  382. current_gp (void)
  383. {
  384. # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
  385. register unsigned long gp __asm__("gp");
  386. return gp;
  387. # elif HAVE_IA64INTRIN_H
  388. return __getReg (_IA64_REG_GP);
  389. # else
  390. # error Implement me.
  391. # endif
  392. }
  393. static int
  394. callback (struct dl_phdr_info *info, size_t size, void *ptr)
  395. {
  396. unw_dyn_info_t *di = ptr;
  397. const Elf64_Phdr *phdr, *p_unwind, *p_dynamic, *p_text;
  398. long n;
  399. Elf64_Addr load_base, segbase = 0;
  400. /* Make sure struct dl_phdr_info is at least as big as we need. */
  401. if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
  402. + sizeof (info->dlpi_phnum))
  403. return -1;
  404. Debug (16, "checking `%s' (load_base=%lx)\n",
  405. info->dlpi_name, info->dlpi_addr);
  406. phdr = info->dlpi_phdr;
  407. load_base = info->dlpi_addr;
  408. p_text = NULL;
  409. p_unwind = NULL;
  410. p_dynamic = NULL;
  411. /* See if PC falls into one of the loaded segments. Find the unwind
  412. segment at the same time. */
  413. for (n = info->dlpi_phnum; --n >= 0; phdr++)
  414. {
  415. if (phdr->p_type == PT_LOAD)
  416. {
  417. Elf64_Addr vaddr = phdr->p_vaddr + load_base;
  418. if (di->u.ti.segbase >= vaddr
  419. && di->u.ti.segbase < vaddr + phdr->p_memsz)
  420. p_text = phdr;
  421. }
  422. else if (phdr->p_type == PT_IA_64_UNWIND)
  423. p_unwind = phdr;
  424. else if (phdr->p_type == PT_DYNAMIC)
  425. p_dynamic = phdr;
  426. }
  427. if (!p_text || !p_unwind)
  428. return 0;
  429. if (likely (p_unwind->p_vaddr >= p_text->p_vaddr
  430. && p_unwind->p_vaddr < p_text->p_vaddr + p_text->p_memsz))
  431. /* normal case: unwind table is inside text segment */
  432. segbase = p_text->p_vaddr + load_base;
  433. else
  434. {
  435. /* Special case: unwind table is in some other segment; this
  436. happens for the Linux kernel's gate DSO, for example. */
  437. phdr = info->dlpi_phdr;
  438. for (n = info->dlpi_phnum; --n >= 0; phdr++)
  439. {
  440. if (phdr->p_type == PT_LOAD && p_unwind->p_vaddr >= phdr->p_vaddr
  441. && p_unwind->p_vaddr < phdr->p_vaddr + phdr->p_memsz)
  442. {
  443. segbase = phdr->p_vaddr + load_base;
  444. break;
  445. }
  446. }
  447. }
  448. if (p_dynamic)
  449. {
  450. /* For dynamicly linked executables and shared libraries,
  451. DT_PLTGOT is the gp value for that object. */
  452. Elf64_Dyn *dyn = (Elf64_Dyn *)(p_dynamic->p_vaddr + load_base);
  453. for (; dyn->d_tag != DT_NULL; ++dyn)
  454. if (dyn->d_tag == DT_PLTGOT)
  455. {
  456. /* On IA-64, _DYNAMIC is writable and GLIBC has relocated it. */
  457. di->gp = dyn->d_un.d_ptr;
  458. break;
  459. }
  460. }
  461. else
  462. /* Otherwise this is a static executable with no _DYNAMIC.
  463. The gp is constant program-wide. */
  464. di->gp = current_gp();
  465. di->format = UNW_INFO_FORMAT_TABLE;
  466. di->start_ip = p_text->p_vaddr + load_base;
  467. di->end_ip = p_text->p_vaddr + load_base + p_text->p_memsz;
  468. di->u.ti.name_ptr = (unw_word_t) info->dlpi_name;
  469. di->u.ti.table_data = (void *) (p_unwind->p_vaddr + load_base);
  470. di->u.ti.table_len = p_unwind->p_memsz / sizeof (unw_word_t);
  471. di->u.ti.segbase = segbase;
  472. Debug (16, "found table `%s': segbase=%lx, len=%lu, gp=%lx, "
  473. "table_data=%p\n", (char *) di->u.ti.name_ptr, di->u.ti.segbase,
  474. di->u.ti.table_len, di->gp, di->u.ti.table_data);
  475. return 1;
  476. }
  477. # ifdef HAVE_DL_PHDR_REMOVALS_COUNTER
  478. static inline int
  479. validate_cache (unw_addr_space_t as)
  480. {
  481. /* Note: we don't need to serialize here with respect to
  482. dl_iterate_phdr() because if somebody were to remove an object
  483. that is required to complete the unwind on whose behalf we're
  484. validating the cache here, we'd be hosed anyhow. What we're
  485. guarding against here is the case where library FOO gets mapped,
  486. unwind info for FOO gets cached, FOO gets unmapped, BAR gets
  487. mapped in the place where FOO was and then we unwind across a
  488. function in FOO. Since no thread can execute in BAR before FOO
  489. has been removed, we are guaranteed that
  490. dl_phdr_removals_counter() would have been incremented before we
  491. get here. */
  492. unsigned long long removals = dl_phdr_removals_counter ();
  493. if (removals == as->shared_object_removals)
  494. return 1;
  495. as->shared_object_removals = removals;
  496. unw_flush_cache (as, 0, 0);
  497. return -1;
  498. }
  499. # else /* !HAVE_DL_PHDR_REMOVALS_COUNTER */
  500. /* Check whether any phdrs have been removed since we last flushed the
  501. cache. If so we flush the cache and return -1, if not, we do
  502. nothing and return 1. */
  503. static int
  504. check_callback (struct dl_phdr_info *info, size_t size, void *ptr)
  505. {
  506. # ifdef HAVE_STRUCT_DL_PHDR_INFO_DLPI_SUBS
  507. unw_addr_space_t as = ptr;
  508. if (size <
  509. offsetof (struct dl_phdr_info, dlpi_subs) + sizeof (info->dlpi_subs))
  510. /* It would be safer to flush the cache here, but that would
  511. disable caching for older libc's which would be incompatible
  512. with the behavior of older versions of libunwind so we return 1
  513. instead and hope nobody runs into stale cache info... */
  514. return 1;
  515. if (info->dlpi_subs == as->shared_object_removals)
  516. return 1;
  517. as->shared_object_removals = info->dlpi_subs;
  518. unw_flush_cache (as, 0, 0);
  519. return -1; /* indicate that there were removals */
  520. # else
  521. return 1;
  522. # endif
  523. }
  524. static inline int
  525. validate_cache (unw_addr_space_t as)
  526. {
  527. intrmask_t saved_mask;
  528. int ret;
  529. SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &saved_mask);
  530. ret = dl_iterate_phdr (check_callback, as);
  531. SIGPROCMASK (SIG_SETMASK, &saved_mask, NULL);
  532. return ret;
  533. }
  534. # endif /* HAVE_DL_PHDR_REMOVALS_COUNTER */
  535. # elif defined(HAVE_DLMODINFO)
  536. /* Support for HP-UX-style dlmodinfo() */
  537. # include <dlfcn.h>
  538. static inline int
  539. validate_cache (unw_addr_space_t as)
  540. {
  541. return 1;
  542. }
  543. # endif /* !HAVE_DLMODINFO */
  544. HIDDEN int
  545. tdep_find_proc_info (unw_addr_space_t as, unw_word_t ip,
  546. unw_proc_info_t *pi, int need_unwind_info, void *arg)
  547. {
  548. # if defined(HAVE_DL_ITERATE_PHDR)
  549. unw_dyn_info_t di, *dip = &di;
  550. intrmask_t saved_mask;
  551. int ret;
  552. di.u.ti.segbase = ip; /* this is cheap... */
  553. SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &saved_mask);
  554. ret = dl_iterate_phdr (callback, &di);
  555. SIGPROCMASK (SIG_SETMASK, &saved_mask, NULL);
  556. if (ret <= 0)
  557. {
  558. if (!kernel_table.u.ti.table_data)
  559. {
  560. if ((ret = get_kernel_table (&kernel_table)) < 0)
  561. return ret;
  562. }
  563. if (ip < kernel_table.start_ip || ip >= kernel_table.end_ip)
  564. return -UNW_ENOINFO;
  565. dip = &kernel_table;
  566. }
  567. # elif defined(HAVE_DLMODINFO)
  568. # define UNWIND_TBL_32BIT 0x8000000000000000
  569. struct load_module_desc lmd;
  570. unw_dyn_info_t di, *dip = &di;
  571. struct unwind_header
  572. {
  573. uint64_t header_version;
  574. uint64_t start_offset;
  575. uint64_t end_offset;
  576. }
  577. *uhdr;
  578. if (!dlmodinfo (ip, &lmd, sizeof (lmd), NULL, 0, 0))
  579. return -UNW_ENOINFO;
  580. di.format = UNW_INFO_FORMAT_TABLE;
  581. di.start_ip = lmd.text_base;
  582. di.end_ip = lmd.text_base + lmd.text_size;
  583. di.gp = lmd.linkage_ptr;
  584. di.u.ti.name_ptr = 0; /* no obvious table-name available */
  585. di.u.ti.segbase = lmd.text_base;
  586. uhdr = (struct unwind_header *) lmd.unwind_base;
  587. if ((uhdr->header_version & ~UNWIND_TBL_32BIT) != 1
  588. && (uhdr->header_version & ~UNWIND_TBL_32BIT) != 2)
  589. {
  590. Debug (1, "encountered unknown unwind header version %ld\n",
  591. (long) (uhdr->header_version & ~UNWIND_TBL_32BIT));
  592. return -UNW_EBADVERSION;
  593. }
  594. if (uhdr->header_version & UNWIND_TBL_32BIT)
  595. {
  596. Debug (1, "32-bit unwind tables are not supported yet\n");
  597. return -UNW_EINVAL;
  598. }
  599. di.u.ti.table_data = (unw_word_t *) (di.u.ti.segbase + uhdr->start_offset);
  600. di.u.ti.table_len = ((uhdr->end_offset - uhdr->start_offset)
  601. / sizeof (unw_word_t));
  602. Debug (16, "found table `%s': segbase=%lx, len=%lu, gp=%lx, "
  603. "table_data=%p\n", (char *) di.u.ti.name_ptr, di.u.ti.segbase,
  604. di.u.ti.table_len, di.gp, di.u.ti.table_data);
  605. # endif
  606. /* now search the table: */
  607. return tdep_search_unwind_table (as, ip, dip, pi, need_unwind_info, arg);
  608. }
  609. /* Returns 1 if the cache is up-to-date or -1 if the cache contained
  610. stale data and had to be flushed. */
  611. HIDDEN int
  612. ia64_local_validate_cache (unw_addr_space_t as, void *arg)
  613. {
  614. return validate_cache (as);
  615. }
  616. #endif /* !UNW_REMOTE_ONLY */