unwind-dw2-fde-glibc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright (C) 2001-2019 Free Software Foundation, Inc.
  2. Contributed by Jakub Jelinek <jakub@redhat.com>.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
  16. segment and dl_iterate_phdr to avoid register/deregister calls at
  17. DSO load/unload. */
  18. #ifdef _LIBC
  19. # include <shlib-compat.h>
  20. #endif
  21. #if !defined _LIBC || SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2_5)
  22. #include <link.h>
  23. #include <stddef.h>
  24. #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
  25. #include <unwind-dw2-fde.c>
  26. #undef _Unwind_Find_FDE
  27. extern fde * _Unwind_Find_registered_FDE (void *pc,
  28. struct dwarf_eh_bases *bases);
  29. extern fde * _Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
  30. struct unw_eh_callback_data
  31. {
  32. _Unwind_Ptr pc;
  33. void *tbase;
  34. void *dbase;
  35. void *func;
  36. fde *ret;
  37. };
  38. struct unw_eh_frame_hdr
  39. {
  40. unsigned char version;
  41. unsigned char eh_frame_ptr_enc;
  42. unsigned char fde_count_enc;
  43. unsigned char table_enc;
  44. };
  45. /* Like base_of_encoded_value, but take the base from a struct object
  46. instead of an _Unwind_Context. */
  47. static _Unwind_Ptr
  48. base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
  49. {
  50. if (encoding == DW_EH_PE_omit)
  51. return 0;
  52. switch (encoding & 0x70)
  53. {
  54. case DW_EH_PE_absptr:
  55. case DW_EH_PE_pcrel:
  56. case DW_EH_PE_aligned:
  57. return 0;
  58. case DW_EH_PE_textrel:
  59. return (_Unwind_Ptr) data->tbase;
  60. case DW_EH_PE_datarel:
  61. return (_Unwind_Ptr) data->dbase;
  62. }
  63. abort ();
  64. }
  65. static int
  66. _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
  67. {
  68. struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
  69. const ElfW(Phdr) *phdr, *p_eh_frame_hdr;
  70. const ElfW(Phdr) *p_dynamic __attribute__ ((unused));
  71. long n, match;
  72. _Unwind_Ptr load_base;
  73. const unsigned char *p;
  74. const struct unw_eh_frame_hdr *hdr;
  75. _Unwind_Ptr eh_frame;
  76. struct object ob;
  77. /* Make sure struct dl_phdr_info is at least as big as we need. */
  78. if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
  79. + sizeof (info->dlpi_phnum))
  80. return -1;
  81. match = 0;
  82. phdr = info->dlpi_phdr;
  83. load_base = info->dlpi_addr;
  84. p_eh_frame_hdr = NULL;
  85. p_dynamic = NULL;
  86. /* See if PC falls into one of the loaded segments. Find the eh_frame
  87. segment at the same time. */
  88. for (n = info->dlpi_phnum; --n >= 0; phdr++)
  89. {
  90. if (phdr->p_type == PT_LOAD)
  91. {
  92. _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
  93. if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
  94. match = 1;
  95. }
  96. else if (phdr->p_type == PT_GNU_EH_FRAME)
  97. p_eh_frame_hdr = phdr;
  98. else if (phdr->p_type == PT_DYNAMIC)
  99. p_dynamic = phdr;
  100. }
  101. if (!match || !p_eh_frame_hdr)
  102. return 0;
  103. /* Read .eh_frame_hdr header. */
  104. hdr = (const struct unw_eh_frame_hdr *)
  105. (p_eh_frame_hdr->p_vaddr + load_base);
  106. if (hdr->version != 1)
  107. return 1;
  108. #ifdef CRT_GET_RFIB_DATA
  109. # ifdef __i386__
  110. data->dbase = NULL;
  111. if (p_dynamic)
  112. {
  113. /* For dynamicly linked executables and shared libraries,
  114. DT_PLTGOT is the gp value for that object. */
  115. ElfW(Dyn) *dyn = (ElfW(Dyn) *)(p_dynamic->p_vaddr + load_base);
  116. for (; dyn->d_tag != DT_NULL ; dyn++)
  117. if (dyn->d_tag == DT_PLTGOT)
  118. {
  119. /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
  120. data->dbase = (void *) dyn->d_un.d_ptr;
  121. break;
  122. }
  123. }
  124. # else
  125. # error What is DW_EH_PE_datarel base on this platform?
  126. # endif
  127. #endif
  128. #ifdef CRT_GET_RFIB_TEXT
  129. # error What is DW_EH_PE_textrel base on this platform?
  130. #endif
  131. p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
  132. base_from_cb_data (hdr->eh_frame_ptr_enc,
  133. data),
  134. (const unsigned char *) (hdr + 1),
  135. &eh_frame);
  136. /* We require here specific table encoding to speed things up.
  137. Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
  138. as base, not the processor specific DW_EH_PE_datarel. */
  139. if (hdr->fde_count_enc != DW_EH_PE_omit
  140. && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
  141. {
  142. _Unwind_Ptr fde_count;
  143. p = read_encoded_value_with_base (hdr->fde_count_enc,
  144. base_from_cb_data (hdr->fde_count_enc,
  145. data),
  146. p, &fde_count);
  147. /* Shouldn't happen. */
  148. if (fde_count == 0)
  149. return 1;
  150. if ((((_Unwind_Ptr) p) & 3) == 0)
  151. {
  152. struct fde_table {
  153. signed initial_loc __attribute__ ((mode (SI)));
  154. signed fde __attribute__ ((mode (SI)));
  155. };
  156. const struct fde_table *table = (const struct fde_table *) p;
  157. size_t lo, hi, mid;
  158. _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
  159. fde *f;
  160. unsigned int f_enc, f_enc_size;
  161. _Unwind_Ptr range;
  162. mid = fde_count - 1;
  163. if (data->pc < table[0].initial_loc + data_base)
  164. return 1;
  165. else if (data->pc < table[mid].initial_loc + data_base)
  166. {
  167. lo = 0;
  168. hi = mid;
  169. while (lo < hi)
  170. {
  171. mid = (lo + hi) / 2;
  172. if (data->pc < table[mid].initial_loc + data_base)
  173. hi = mid;
  174. else if (data->pc >= table[mid + 1].initial_loc + data_base)
  175. lo = mid + 1;
  176. else
  177. break;
  178. }
  179. if (lo >= hi)
  180. __gxx_abort ();
  181. }
  182. f = (fde *) (table[mid].fde + data_base);
  183. f_enc = get_fde_encoding (f);
  184. f_enc_size = size_of_encoded_value (f_enc);
  185. read_encoded_value_with_base (f_enc & 0x0f, 0,
  186. &f->pc_begin[f_enc_size], &range);
  187. if (data->pc < table[mid].initial_loc + data_base + range)
  188. data->ret = f;
  189. data->func = (void *) (table[mid].initial_loc + data_base);
  190. return 1;
  191. }
  192. }
  193. /* We have no sorted search table, so need to go the slow way.
  194. As soon as GLIBC will provide API so to notify that a library has been
  195. removed, we could cache this (and thus use search_object). */
  196. ob.pc_begin = NULL;
  197. ob.tbase = data->tbase;
  198. ob.dbase = data->dbase;
  199. ob.u.single = (fde *) eh_frame;
  200. ob.s.i = 0;
  201. ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
  202. data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
  203. if (data->ret != NULL)
  204. {
  205. unsigned int encoding = get_fde_encoding (data->ret);
  206. _Unwind_Ptr func;
  207. read_encoded_value_with_base (encoding,
  208. base_from_cb_data (encoding, data),
  209. data->ret->pc_begin, &func);
  210. data->func = (void *) func;
  211. }
  212. return 1;
  213. }
  214. # ifdef _LIBC
  215. # define dl_iterate_phdr __dl_iterate_phdr
  216. # endif
  217. fde *
  218. _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
  219. {
  220. struct unw_eh_callback_data data;
  221. fde *ret;
  222. ret = _Unwind_Find_registered_FDE (pc, bases);
  223. if (ret != NULL)
  224. return ret;
  225. data.pc = (_Unwind_Ptr) pc;
  226. data.tbase = NULL;
  227. data.dbase = NULL;
  228. data.func = NULL;
  229. data.ret = NULL;
  230. if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
  231. return NULL;
  232. if (data.ret)
  233. {
  234. bases->tbase = data.tbase;
  235. bases->dbase = data.dbase;
  236. bases->func = data.func;
  237. }
  238. return data.ret;
  239. }
  240. #endif