Use 'a' operand code for prefetch instruction.
[official-gcc.git] / gcc / unwind-dw2-fde-glibc.c
blob8e84b400161c938bc5352efe939e1d7cf0f3f708
1 /* Copyright (C) 2001 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 #include "auto-host.h" /* For HAVE_LD_EH_FRAME_HDR. */
33 #include "tconfig.h"
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <link.h>
37 #include "tsystem.h"
38 #include "dwarf2.h"
39 #include "unwind.h"
40 #define NO_BASE_OF_ENCODED_VALUE
41 #include "unwind-pe.h"
42 #include "unwind-dw2-fde.h"
43 #include "gthr.h"
45 #if defined(HAVE_LD_EH_FRAME_HDR) \
46 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
47 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
49 static fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
51 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
52 #include "unwind-dw2-fde.c"
53 #undef _Unwind_Find_FDE
55 #ifndef PT_GNU_EH_FRAME
56 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
57 #endif
59 struct unw_eh_callback_data
61 _Unwind_Ptr pc;
62 void *tbase;
63 void *dbase;
64 void *func;
65 fde *ret;
68 struct unw_eh_frame_hdr
70 unsigned char version;
71 unsigned char eh_frame_ptr_enc;
72 unsigned char fde_count_enc;
73 unsigned char table_enc;
76 /* Like base_of_encoded_value, but take the base from a struct object
77 instead of an _Unwind_Context. */
79 static _Unwind_Ptr
80 base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
82 if (encoding == DW_EH_PE_omit)
83 return 0;
85 switch (encoding & 0x70)
87 case DW_EH_PE_absptr:
88 case DW_EH_PE_pcrel:
89 case DW_EH_PE_aligned:
90 return 0;
92 case DW_EH_PE_textrel:
93 return (_Unwind_Ptr) data->tbase;
94 case DW_EH_PE_datarel:
95 return (_Unwind_Ptr) data->dbase;
97 abort ();
100 static int
101 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
103 struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
104 const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
105 long n, match;
106 _Unwind_Ptr load_base;
107 const unsigned char *p;
108 const struct unw_eh_frame_hdr *hdr;
109 _Unwind_Ptr eh_frame;
110 struct object ob;
112 /* Make sure struct dl_phdr_info is at least as big as we need. */
113 if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
114 + sizeof (info->dlpi_phnum))
115 return -1;
117 match = 0;
118 phdr = info->dlpi_phdr;
119 load_base = info->dlpi_addr;
120 p_eh_frame_hdr = NULL;
121 p_dynamic = NULL;
123 /* See if PC falls into one of the loaded segments. Find the eh_frame
124 segment at the same time. */
125 for (n = info->dlpi_phnum; --n >= 0; phdr++)
127 if (phdr->p_type == PT_LOAD)
129 _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
130 if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
131 match = 1;
133 else if (phdr->p_type == PT_GNU_EH_FRAME)
134 p_eh_frame_hdr = phdr;
135 else if (phdr->p_type == PT_DYNAMIC)
136 p_dynamic = phdr;
138 if (!match || !p_eh_frame_hdr)
139 return 0;
141 /* Read .eh_frame_hdr header. */
142 hdr = (const struct unw_eh_frame_hdr *)
143 (p_eh_frame_hdr->p_vaddr + load_base);
144 if (hdr->version != 1)
145 return 1;
147 #ifdef CRT_GET_RFIB_DATA
148 # ifdef __i386__
149 data->dbase = NULL;
150 if (p_dynamic)
152 /* For dynamicly linked executables and shared libraries,
153 DT_PLTGOT is the gp value for that object. */
154 ElfW(Dyn) *dyn = (ElfW(Dyn) *)(p_dynamic->p_vaddr + load_base);
155 for (; dyn->d_tag != DT_NULL ; dyn++)
156 if (dyn->d_tag == DT_PLTGOT)
158 /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
159 data->dbase = (void *) dyn->d_un.d_ptr;
160 break;
163 # else
164 # error What is DW_EH_PE_datarel base on this platform?
165 # endif
166 #endif
167 #ifdef CRT_GET_RFIB_TEXT
168 # error What is DW_EH_PE_textrel base on this platform?
169 #endif
171 p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
172 base_from_cb_data (hdr->eh_frame_ptr_enc,
173 data),
174 (const unsigned char *) (hdr + 1),
175 &eh_frame);
177 /* We require here specific table encoding to speed things up.
178 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
179 as base, not the processor specific DW_EH_PE_datarel. */
180 if (hdr->fde_count_enc != DW_EH_PE_omit
181 && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
183 _Unwind_Ptr fde_count;
185 p = read_encoded_value_with_base (hdr->fde_count_enc,
186 base_from_cb_data (hdr->fde_count_enc,
187 data),
188 p, &fde_count);
189 /* Shouldn't happen. */
190 if (fde_count == 0)
191 return 1;
192 if ((((_Unwind_Ptr) p) & 3) == 0)
194 struct fde_table {
195 signed initial_loc __attribute__ ((mode (SI)));
196 signed fde __attribute__ ((mode (SI)));
198 const struct fde_table *table = (const struct fde_table *) p;
199 size_t lo, hi, mid;
200 _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
201 fde *f;
202 unsigned int f_enc, f_enc_size;
203 _Unwind_Ptr range;
205 mid = fde_count - 1;
206 if (data->pc < table[0].initial_loc + data_base)
207 return 1;
208 else if (data->pc < table[mid].initial_loc + data_base)
210 lo = 0;
211 hi = mid;
213 while (lo < hi)
215 mid = (lo + hi) / 2;
216 if (data->pc < table[mid].initial_loc + data_base)
217 hi = mid;
218 else if (data->pc >= table[mid + 1].initial_loc + data_base)
219 lo = mid + 1;
220 else
221 break;
224 if (lo >= hi)
225 __gxx_abort ();
228 f = (fde *) (table[mid].fde + data_base);
229 f_enc = get_fde_encoding (f);
230 f_enc_size = size_of_encoded_value (f_enc);
231 read_encoded_value_with_base (f_enc & 0x0f, 0,
232 &f->pc_begin[f_enc_size], &range);
233 if (data->pc < table[mid].initial_loc + data_base + range)
234 data->ret = f;
235 data->func = (void *) (table[mid].initial_loc + data_base);
236 return 1;
240 /* We have no sorted search table, so need to go the slow way.
241 As soon as GLIBC will provide API so to notify that a library has been
242 removed, we could cache this (and thus use search_object). */
243 ob.pc_begin = NULL;
244 ob.tbase = data->tbase;
245 ob.dbase = data->dbase;
246 ob.u.single = (fde *) eh_frame;
247 ob.s.i = 0;
248 ob.s.b.mixed_encoding = 1; /* Need to assume worst case. */
249 data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
250 if (data->ret != NULL)
252 unsigned int encoding = get_fde_encoding (data->ret);
253 read_encoded_value_with_base (encoding,
254 base_from_cb_data (encoding, data),
255 data->ret->pc_begin,
256 (_Unwind_Ptr *)&data->func);
258 return 1;
261 fde *
262 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
264 struct unw_eh_callback_data data;
265 fde *ret;
267 ret = _Unwind_Find_registered_FDE (pc, bases);
268 if (ret != NULL)
269 return ret;
271 data.pc = (_Unwind_Ptr) pc;
272 data.tbase = NULL;
273 data.dbase = NULL;
274 data.func = NULL;
275 data.ret = NULL;
277 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
278 return NULL;
280 if (data.ret)
282 bases->tbase = data.tbase;
283 bases->dbase = data.dbase;
284 bases->func = data.func;
286 return data.ret;
289 #else
290 /* Prevent multiple include of header files. */
291 #define _Unwind_Find_FDE _Unwind_Find_FDE
292 #include "unwind-dw2-fde.c"
293 #endif