1 /* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
2 Contributed by Jakub Jelinek <jakub@redhat.com>.
4 This file is part of GCC.
6 GCC 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)
11 GCC 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 GCC; 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
36 #include "auto-host.h" /* For HAVE_LD_EH_FRAME_HDR. */
42 #include "coretypes.h"
46 #define NO_BASE_OF_ENCODED_VALUE
47 #include "unwind-pe.h"
48 #include "unwind-dw2-fde.h"
51 #if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
52 && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
53 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
55 static fde
* _Unwind_Find_registered_FDE (void *pc
, struct dwarf_eh_bases
*bases
);
57 #define _Unwind_Find_FDE _Unwind_Find_registered_FDE
58 #include "unwind-dw2-fde.c"
59 #undef _Unwind_Find_FDE
61 #ifndef PT_GNU_EH_FRAME
62 #define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
65 struct unw_eh_callback_data
74 struct unw_eh_frame_hdr
76 unsigned char version
;
77 unsigned char eh_frame_ptr_enc
;
78 unsigned char fde_count_enc
;
79 unsigned char table_enc
;
82 /* Like base_of_encoded_value, but take the base from a struct
83 unw_eh_callback_data instead of an _Unwind_Context. */
86 base_from_cb_data (unsigned char encoding
, struct unw_eh_callback_data
*data
)
88 if (encoding
== DW_EH_PE_omit
)
91 switch (encoding
& 0x70)
95 case DW_EH_PE_aligned
:
98 case DW_EH_PE_textrel
:
99 return (_Unwind_Ptr
) data
->tbase
;
100 case DW_EH_PE_datarel
:
101 return (_Unwind_Ptr
) data
->dbase
;
107 _Unwind_IteratePhdrCallback (struct dl_phdr_info
*info
, size_t size
, void *ptr
)
109 struct unw_eh_callback_data
*data
= (struct unw_eh_callback_data
*) ptr
;
110 const ElfW(Phdr
) *phdr
, *p_eh_frame_hdr
, *p_dynamic
;
112 _Unwind_Ptr load_base
;
113 const unsigned char *p
;
114 const struct unw_eh_frame_hdr
*hdr
;
115 _Unwind_Ptr eh_frame
;
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
))
124 phdr
= info
->dlpi_phdr
;
125 load_base
= info
->dlpi_addr
;
126 p_eh_frame_hdr
= NULL
;
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
++)
133 if (phdr
->p_type
== PT_LOAD
)
135 _Unwind_Ptr vaddr
= phdr
->p_vaddr
+ load_base
;
136 if (data
->pc
>= vaddr
&& data
->pc
< vaddr
+ phdr
->p_memsz
)
139 else if (phdr
->p_type
== PT_GNU_EH_FRAME
)
140 p_eh_frame_hdr
= phdr
;
141 else if (phdr
->p_type
== PT_DYNAMIC
)
144 if (!match
|| !p_eh_frame_hdr
)
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)
153 #ifdef CRT_GET_RFIB_DATA
158 /* For dynamically linked executables and shared libraries,
159 DT_PLTGOT is the gp value for that object. */
160 ElfW(Dyn
) *dyn
= (ElfW(Dyn
) *) (p_dynamic
->p_vaddr
+ load_base
);
161 for (; dyn
->d_tag
!= DT_NULL
; dyn
++)
162 if (dyn
->d_tag
== DT_PLTGOT
)
164 /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it. */
165 data
->dbase
= (void *) dyn
->d_un
.d_ptr
;
170 # error What is DW_EH_PE_datarel base on this platform?
174 p
= read_encoded_value_with_base (hdr
->eh_frame_ptr_enc
,
175 base_from_cb_data (hdr
->eh_frame_ptr_enc
,
177 (const unsigned char *) (hdr
+ 1),
180 /* We require here specific table encoding to speed things up.
181 Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
182 as base, not the processor specific DW_EH_PE_datarel. */
183 if (hdr
->fde_count_enc
!= DW_EH_PE_omit
184 && hdr
->table_enc
== (DW_EH_PE_datarel
| DW_EH_PE_sdata4
))
186 _Unwind_Ptr fde_count
;
188 p
= read_encoded_value_with_base (hdr
->fde_count_enc
,
189 base_from_cb_data (hdr
->fde_count_enc
,
192 /* Shouldn't happen. */
195 if ((((_Unwind_Ptr
) p
) & 3) == 0)
198 signed initial_loc
__attribute__ ((mode (SI
)));
199 signed fde
__attribute__ ((mode (SI
)));
201 const struct fde_table
*table
= (const struct fde_table
*) p
;
203 _Unwind_Ptr data_base
= (_Unwind_Ptr
) hdr
;
205 unsigned int f_enc
, f_enc_size
;
209 if (data
->pc
< table
[0].initial_loc
+ data_base
)
211 else if (data
->pc
< table
[mid
].initial_loc
+ data_base
)
219 if (data
->pc
< table
[mid
].initial_loc
+ data_base
)
221 else if (data
->pc
>= table
[mid
+ 1].initial_loc
+ data_base
)
231 f
= (fde
*) (table
[mid
].fde
+ data_base
);
232 f_enc
= get_fde_encoding (f
);
233 f_enc_size
= size_of_encoded_value (f_enc
);
234 read_encoded_value_with_base (f_enc
& 0x0f, 0,
235 &f
->pc_begin
[f_enc_size
], &range
);
236 if (data
->pc
< table
[mid
].initial_loc
+ data_base
+ range
)
238 data
->func
= (void *) (table
[mid
].initial_loc
+ data_base
);
243 /* We have no sorted search table, so need to go the slow way.
244 As soon as GLIBC will provide API so to notify that a library has been
245 removed, we could cache this (and thus use search_object). */
247 ob
.tbase
= data
->tbase
;
248 ob
.dbase
= data
->dbase
;
249 ob
.u
.single
= (fde
*) eh_frame
;
251 ob
.s
.b
.mixed_encoding
= 1; /* Need to assume worst case. */
252 data
->ret
= linear_search_fdes (&ob
, (fde
*) eh_frame
, (void *) data
->pc
);
253 if (data
->ret
!= NULL
)
255 unsigned int encoding
= get_fde_encoding (data
->ret
);
256 read_encoded_value_with_base (encoding
,
257 base_from_cb_data (encoding
, data
),
259 (_Unwind_Ptr
*)&data
->func
);
265 _Unwind_Find_FDE (void *pc
, struct dwarf_eh_bases
*bases
)
267 struct unw_eh_callback_data data
;
270 ret
= _Unwind_Find_registered_FDE (pc
, bases
);
274 data
.pc
= (_Unwind_Ptr
) pc
;
280 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback
, &data
) < 0)
285 bases
->tbase
= data
.tbase
;
286 bases
->dbase
= data
.dbase
;
287 bases
->func
= data
.func
;
293 /* Prevent multiple include of header files. */
294 #define _Unwind_Find_FDE _Unwind_Find_FDE
295 #include "unwind-dw2-fde.c"