Simplified Chinese translations for libc.
[glibc.git] / sysdeps / generic / unwind-dw2-fde-glibc.c
blob746e26b99a66b0efd7ce90ba7e566e07c4777cc4
1 /* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of GNU CC.
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.
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.
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. */
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. */
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. */
32 #ifdef _LIBC
33 # include <shlib-compat.h>
34 #endif
36 #if !defined _LIBC || SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2_5)
38 #include <link.h>
39 #include <stddef.h>
41 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
43 #include <unwind-dw2-fde.c>
45 #undef _Unwind_Find_FDE
47 extern fde * _Unwind_Find_registered_FDE (void *pc,
48 struct dwarf_eh_bases *bases);
49 extern fde * _Unwind_Find_FDE (void *, struct dwarf_eh_bases *);
51 struct unw_eh_callback_data
53 _Unwind_Ptr pc;
54 void *tbase;
55 void *dbase;
56 void *func;
57 fde *ret;
60 struct unw_eh_frame_hdr
62 unsigned char version;
63 unsigned char eh_frame_ptr_enc;
64 unsigned char fde_count_enc;
65 unsigned char table_enc;
68 /* Like base_of_encoded_value, but take the base from a struct object
69 instead of an _Unwind_Context. */
71 static _Unwind_Ptr
72 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
74 if (encoding == DW_EH_PE_omit)
75 return 0;
77 switch (encoding & 0x70)
79 case DW_EH_PE_absptr:
80 case DW_EH_PE_pcrel:
81 case DW_EH_PE_aligned:
82 return 0;
84 case DW_EH_PE_textrel:
85 return (_Unwind_Ptr) data->tbase;
86 case DW_EH_PE_datarel:
87 return (_Unwind_Ptr) data->dbase;
89 abort ();
92 static int
93 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
95 struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
96 const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
97 long n, match;
98 _Unwind_Ptr load_base;
99 const unsigned char *p;
100 const struct unw_eh_frame_hdr *hdr;
101 _Unwind_Ptr eh_frame;
102 struct object ob;
104 /* Make sure struct dl_phdr_info is at least as big as we need. */
105 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
106 + sizeof (info->dlpi_phnum))
107 return -1;
109 match = 0;
110 phdr = info->dlpi_phdr;
111 load_base = info->dlpi_addr;
112 p_eh_frame_hdr = NULL;
113 p_dynamic = NULL;
115 /* See if PC falls into one of the loaded segments. Find the eh_frame
116 segment at the same time. */
117 for (n = info->dlpi_phnum; --n >= 0; phdr++)
119 if (phdr->p_type == PT_LOAD)
121 _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
122 if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
123 match = 1;
125 else if (phdr->p_type == PT_GNU_EH_FRAME)
126 p_eh_frame_hdr = phdr;
127 else if (phdr->p_type == PT_DYNAMIC)
128 p_dynamic = phdr;
130 if (!match || !p_eh_frame_hdr)
131 return 0;
133 /* Read .eh_frame_hdr header. */
134 hdr = (const struct unw_eh_frame_hdr *)
135 (p_eh_frame_hdr->p_vaddr + load_base);
136 if (hdr->version != 1)
137 return 1;
139 #ifdef CRT_GET_RFIB_DATA
140 # ifdef __i386__
141 data->dbase = NULL;
142 if (p_dynamic)
144 /* For dynamicly linked executables and shared libraries,
145 DT_PLTGOT is the gp value for that object. */
146 ElfW(Dyn) *dyn = (ElfW(Dyn) *)(p_dynamic->p_vaddr + load_base);
147 for (; dyn->d_tag != DT_NULL ; dyn++)
148 if (dyn->d_tag == DT_PLTGOT)
150 /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
151 data->dbase = (void *) dyn->d_un.d_ptr;
152 break;
155 # else
156 # error What is DW_EH_PE_datarel base on this platform?
157 # endif
158 #endif
159 #ifdef CRT_GET_RFIB_TEXT
160 # error What is DW_EH_PE_textrel base on this platform?
161 #endif
163 p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
164 base_from_cb_data (hdr->eh_frame_ptr_enc,
165 data),
166 (const unsigned char *) (hdr + 1),
167 &eh_frame);
169 /* We require here specific table encoding to speed things up.
170 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
171 as base, not the processor specific DW_EH_PE_datarel. */
172 if (hdr->fde_count_enc != DW_EH_PE_omit
173 && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
175 _Unwind_Ptr fde_count;
177 p = read_encoded_value_with_base (hdr->fde_count_enc,
178 base_from_cb_data (hdr->fde_count_enc,
179 data),
180 p, &fde_count);
181 /* Shouldn't happen. */
182 if (fde_count == 0)
183 return 1;
184 if ((((_Unwind_Ptr) p) & 3) == 0)
186 struct fde_table {
187 signed initial_loc __attribute__ ((mode (SI)));
188 signed fde __attribute__ ((mode (SI)));
190 const struct fde_table *table = (const struct fde_table *) p;
191 size_t lo, hi, mid;
192 _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
193 fde *f;
194 unsigned int f_enc, f_enc_size;
195 _Unwind_Ptr range;
197 mid = fde_count - 1;
198 if (data->pc < table[0].initial_loc + data_base)
199 return 1;
200 else if (data->pc < table[mid].initial_loc + data_base)
202 lo = 0;
203 hi = mid;
205 while (lo < hi)
207 mid = (lo + hi) / 2;
208 if (data->pc < table[mid].initial_loc + data_base)
209 hi = mid;
210 else if (data->pc >= table[mid + 1].initial_loc + data_base)
211 lo = mid + 1;
212 else
213 break;
216 if (lo >= hi)
217 __gxx_abort ();
220 f = (fde *) (table[mid].fde + data_base);
221 f_enc = get_fde_encoding (f);
222 f_enc_size = size_of_encoded_value (f_enc);
223 read_encoded_value_with_base (f_enc & 0x0f, 0,
224 &f->pc_begin[f_enc_size], &range);
225 if (data->pc < table[mid].initial_loc + data_base + range)
226 data->ret = f;
227 data->func = (void *) (table[mid].initial_loc + data_base);
228 return 1;
232 /* We have no sorted search table, so need to go the slow way.
233 As soon as GLIBC will provide API so to notify that a library has been
234 removed, we could cache this (and thus use search_object). */
235 ob.pc_begin = NULL;
236 ob.tbase = data->tbase;
237 ob.dbase = data->dbase;
238 ob.u.single = (fde *) eh_frame;
239 ob.s.i = 0;
240 ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
241 data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
242 if (data->ret != NULL)
244 unsigned int encoding = get_fde_encoding (data->ret);
245 read_encoded_value_with_base (encoding,
246 base_from_cb_data (encoding, data),
247 data->ret->pc_begin,
248 (_Unwind_Ptr *)&data->func);
250 return 1;
253 # ifdef _LIBC
254 # define dl_iterate_phdr __dl_iterate_phdr
255 # endif
257 fde *
258 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
260 struct unw_eh_callback_data data;
261 fde *ret;
263 ret = _Unwind_Find_registered_FDE (pc, bases);
264 if (ret != NULL)
265 return ret;
267 data.pc = (_Unwind_Ptr) pc;
268 data.tbase = NULL;
269 data.dbase = NULL;
270 data.func = NULL;
271 data.ret = NULL;
273 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
274 return NULL;
276 if (data.ret)
278 bases->tbase = data.tbase;
279 bases->dbase = data.dbase;
280 bases->func = data.func;
282 return data.ret;
285 #endif