]>
Commit | Line | Data |
---|---|---|
a01da83b | 1 | /* Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
275b60d6 JJ |
2 | Contributed by Jakub Jelinek <jakub@redhat.com>. |
3 | ||
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* As a special exception, if you link this library with other files, | |
22 | some of which are compiled with GCC, to produce an executable, | |
23 | this library does not by itself cause the resulting executable | |
24 | to be covered by the GNU General Public License. | |
25 | This exception does not however invalidate any other reasons why | |
26 | the executable file might be covered by the GNU General Public License. */ | |
27 | ||
28 | /* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF | |
29 | segment and dl_iterate_phdr to avoid register/deregister calls at | |
30 | DSO load/unload. */ | |
31 | ||
703b0080 JDA |
32 | #ifndef _GNU_SOURCE |
33 | #define _GNU_SOURCE 1 | |
34 | #endif | |
35 | ||
275b60d6 JJ |
36 | #include "auto-host.h" /* For HAVE_LD_EH_FRAME_HDR. */ |
37 | #include "tconfig.h" | |
72c7c913 | 38 | #ifndef inhibit_libc |
275b60d6 JJ |
39 | #include <stddef.h> |
40 | #include <stdlib.h> | |
41 | #include <link.h> | |
72c7c913 | 42 | #endif |
275b60d6 JJ |
43 | #include "tsystem.h" |
44 | #include "dwarf2.h" | |
45 | #include "unwind.h" | |
46 | #define NO_BASE_OF_ENCODED_VALUE | |
47 | #include "unwind-pe.h" | |
48 | #include "unwind-dw2-fde.h" | |
49 | #include "gthr.h" | |
50 | ||
72c7c913 | 51 | #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \ |
275b60d6 JJ |
52 | && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \ |
53 | || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG))) | |
54 | ||
55 | static fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases); | |
56 | ||
57 | #define _Unwind_Find_FDE _Unwind_Find_registered_FDE | |
58 | #include "unwind-dw2-fde.c" | |
59 | #undef _Unwind_Find_FDE | |
60 | ||
61 | #ifndef PT_GNU_EH_FRAME | |
62 | #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550) | |
63 | #endif | |
64 | ||
65 | struct unw_eh_callback_data | |
66 | { | |
67 | _Unwind_Ptr pc; | |
68 | void *tbase; | |
69 | void *dbase; | |
70 | void *func; | |
71 | fde *ret; | |
72 | }; | |
73 | ||
74 | struct unw_eh_frame_hdr | |
75 | { | |
76 | unsigned char version; | |
77 | unsigned char eh_frame_ptr_enc; | |
78 | unsigned char fde_count_enc; | |
79 | unsigned char table_enc; | |
80 | }; | |
81 | ||
82 | /* Like base_of_encoded_value, but take the base from a struct object | |
83 | instead of an _Unwind_Context. */ | |
84 | ||
85 | static _Unwind_Ptr | |
86 | base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data) | |
87 | { | |
88 | if (encoding == DW_EH_PE_omit) | |
89 | return 0; | |
90 | ||
91 | switch (encoding & 0x70) | |
92 | { | |
93 | case DW_EH_PE_absptr: | |
94 | case DW_EH_PE_pcrel: | |
95 | case DW_EH_PE_aligned: | |
96 | return 0; | |
97 | ||
98 | case DW_EH_PE_textrel: | |
99 | return (_Unwind_Ptr) data->tbase; | |
100 | case DW_EH_PE_datarel: | |
101 | return (_Unwind_Ptr) data->dbase; | |
102 | } | |
103 | abort (); | |
104 | } | |
105 | ||
106 | static int | |
107 | _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr) | |
108 | { | |
109 | struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr; | |
110 | const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic; | |
111 | long n, match; | |
112 | _Unwind_Ptr load_base; | |
113 | const unsigned char *p; | |
114 | const struct unw_eh_frame_hdr *hdr; | |
115 | _Unwind_Ptr eh_frame; | |
116 | struct object ob; | |
117 | ||
118 | /* Make sure struct dl_phdr_info is at least as big as we need. */ | |
119 | if (size < offsetof (struct dl_phdr_info, dlpi_phnum) | |
120 | + sizeof (info->dlpi_phnum)) | |
121 | return -1; | |
122 | ||
123 | match = 0; | |
124 | phdr = info->dlpi_phdr; | |
125 | load_base = info->dlpi_addr; | |
126 | p_eh_frame_hdr = NULL; | |
127 | p_dynamic = NULL; | |
128 | ||
129 | /* See if PC falls into one of the loaded segments. Find the eh_frame | |
130 | segment at the same time. */ | |
131 | for (n = info->dlpi_phnum; --n >= 0; phdr++) | |
132 | { | |
133 | if (phdr->p_type == PT_LOAD) | |
134 | { | |
135 | _Unwind_Ptr vaddr = phdr->p_vaddr + load_base; | |
136 | if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz) | |
137 | match = 1; | |
138 | } | |
139 | else if (phdr->p_type == PT_GNU_EH_FRAME) | |
140 | p_eh_frame_hdr = phdr; | |
141 | else if (phdr->p_type == PT_DYNAMIC) | |
142 | p_dynamic = phdr; | |
143 | } | |
144 | if (!match || !p_eh_frame_hdr) | |
145 | return 0; | |
146 | ||
147 | /* Read .eh_frame_hdr header. */ | |
148 | hdr = (const struct unw_eh_frame_hdr *) | |
149 | (p_eh_frame_hdr->p_vaddr + load_base); | |
150 | if (hdr->version != 1) | |
151 | return 1; | |
152 | ||
153 | #ifdef CRT_GET_RFIB_DATA | |
154 | # ifdef __i386__ | |
155 | data->dbase = NULL; | |
156 | if (p_dynamic) | |
157 | { | |
158 | /* For dynamicly linked executables and shared libraries, | |
159 | DT_PLTGOT is the gp value for that object. */ | |
a01da83b | 160 | ElfW(Dyn) *dyn = (ElfW(Dyn) *) (p_dynamic->p_vaddr + load_base); |
275b60d6 JJ |
161 | for (; dyn->d_tag != DT_NULL ; dyn++) |
162 | if (dyn->d_tag == DT_PLTGOT) | |
163 | { | |
164 | /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */ | |
165 | data->dbase = (void *) dyn->d_un.d_ptr; | |
166 | break; | |
167 | } | |
168 | } | |
169 | # else | |
170 | # error What is DW_EH_PE_datarel base on this platform? | |
171 | # endif | |
172 | #endif | |
173 | #ifdef CRT_GET_RFIB_TEXT | |
174 | # error What is DW_EH_PE_textrel base on this platform? | |
175 | #endif | |
176 | ||
177 | p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc, | |
178 | base_from_cb_data (hdr->eh_frame_ptr_enc, | |
179 | data), | |
180 | (const unsigned char *) (hdr + 1), | |
181 | &eh_frame); | |
182 | ||
183 | /* We require here specific table encoding to speed things up. | |
184 | Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start | |
185 | as base, not the processor specific DW_EH_PE_datarel. */ | |
186 | if (hdr->fde_count_enc != DW_EH_PE_omit | |
187 | && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4)) | |
188 | { | |
189 | _Unwind_Ptr fde_count; | |
190 | ||
191 | p = read_encoded_value_with_base (hdr->fde_count_enc, | |
192 | base_from_cb_data (hdr->fde_count_enc, | |
193 | data), | |
194 | p, &fde_count); | |
195 | /* Shouldn't happen. */ | |
196 | if (fde_count == 0) | |
197 | return 1; | |
198 | if ((((_Unwind_Ptr) p) & 3) == 0) | |
199 | { | |
200 | struct fde_table { | |
201 | signed initial_loc __attribute__ ((mode (SI))); | |
202 | signed fde __attribute__ ((mode (SI))); | |
203 | }; | |
204 | const struct fde_table *table = (const struct fde_table *) p; | |
205 | size_t lo, hi, mid; | |
206 | _Unwind_Ptr data_base = (_Unwind_Ptr) hdr; | |
207 | fde *f; | |
208 | unsigned int f_enc, f_enc_size; | |
209 | _Unwind_Ptr range; | |
210 | ||
211 | mid = fde_count - 1; | |
212 | if (data->pc < table[0].initial_loc + data_base) | |
213 | return 1; | |
214 | else if (data->pc < table[mid].initial_loc + data_base) | |
215 | { | |
216 | lo = 0; | |
217 | hi = mid; | |
218 | ||
219 | while (lo < hi) | |
220 | { | |
221 | mid = (lo + hi) / 2; | |
222 | if (data->pc < table[mid].initial_loc + data_base) | |
223 | hi = mid; | |
224 | else if (data->pc >= table[mid + 1].initial_loc + data_base) | |
225 | lo = mid + 1; | |
226 | else | |
227 | break; | |
228 | } | |
229 | ||
230 | if (lo >= hi) | |
231 | __gxx_abort (); | |
232 | } | |
233 | ||
234 | f = (fde *) (table[mid].fde + data_base); | |
235 | f_enc = get_fde_encoding (f); | |
236 | f_enc_size = size_of_encoded_value (f_enc); | |
237 | read_encoded_value_with_base (f_enc & 0x0f, 0, | |
238 | &f->pc_begin[f_enc_size], &range); | |
239 | if (data->pc < table[mid].initial_loc + data_base + range) | |
240 | data->ret = f; | |
241 | data->func = (void *) (table[mid].initial_loc + data_base); | |
242 | return 1; | |
243 | } | |
244 | } | |
245 | ||
246 | /* We have no sorted search table, so need to go the slow way. | |
247 | As soon as GLIBC will provide API so to notify that a library has been | |
248 | removed, we could cache this (and thus use search_object). */ | |
249 | ob.pc_begin = NULL; | |
250 | ob.tbase = data->tbase; | |
251 | ob.dbase = data->dbase; | |
252 | ob.u.single = (fde *) eh_frame; | |
253 | ob.s.i = 0; | |
254 | ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */ | |
255 | data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc); | |
256 | if (data->ret != NULL) | |
257 | { | |
258 | unsigned int encoding = get_fde_encoding (data->ret); | |
259 | read_encoded_value_with_base (encoding, | |
260 | base_from_cb_data (encoding, data), | |
261 | data->ret->pc_begin, | |
262 | (_Unwind_Ptr *)&data->func); | |
263 | } | |
264 | return 1; | |
265 | } | |
266 | ||
267 | fde * | |
268 | _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases) | |
269 | { | |
270 | struct unw_eh_callback_data data; | |
271 | fde *ret; | |
272 | ||
273 | ret = _Unwind_Find_registered_FDE (pc, bases); | |
274 | if (ret != NULL) | |
275 | return ret; | |
276 | ||
277 | data.pc = (_Unwind_Ptr) pc; | |
278 | data.tbase = NULL; | |
279 | data.dbase = NULL; | |
280 | data.func = NULL; | |
281 | data.ret = NULL; | |
282 | ||
283 | if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0) | |
284 | return NULL; | |
285 | ||
286 | if (data.ret) | |
287 | { | |
288 | bases->tbase = data.tbase; | |
289 | bases->dbase = data.dbase; | |
290 | bases->func = data.func; | |
291 | } | |
292 | return data.ret; | |
293 | } | |
294 | ||
295 | #else | |
5f77a9c5 ZW |
296 | /* Prevent multiple include of header files. */ |
297 | #define _Unwind_Find_FDE _Unwind_Find_FDE | |
275b60d6 JJ |
298 | #include "unwind-dw2-fde.c" |
299 | #endif |