Commit | Line | Data |
---|---|---|
3151305a ILT |
1 | // ehframe.h -- handle exception frame sections for gold -*- C++ -*- |
2 | ||
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. | |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #ifndef GOLD_EHFRAME_H | |
24 | #define GOLD_EHFRAME_H | |
25 | ||
1cac254c ILT |
26 | #include <map> |
27 | #include <set> | |
28 | #include <vector> | |
29 | ||
3151305a | 30 | #include "output.h" |
730cdc88 | 31 | #include "merge.h" |
3151305a ILT |
32 | |
33 | namespace gold | |
34 | { | |
35 | ||
730cdc88 ILT |
36 | template<int size, bool big_endian> |
37 | class Track_relocs; | |
38 | ||
39 | class Eh_frame; | |
40 | ||
3151305a ILT |
41 | // This class manages the .eh_frame_hdr section, which holds the data |
42 | // for the PT_GNU_EH_FRAME segment. gcc's unwind support code uses | |
43 | // the PT_GNU_EH_FRAME segment to find the list of FDEs. This saves | |
44 | // the time required to register the exception handlers at startup | |
45 | // time and when a shared object is loaded, and the time required to | |
46 | // deregister the exception handlers when a shared object is unloaded. | |
47 | ||
48 | // FIXME: gcc supports using storing a sorted lookup table for the | |
49 | // FDEs in the PT_GNU_EH_FRAME segment, but we do not yet generate | |
50 | // that. | |
51 | ||
52 | class Eh_frame_hdr : public Output_section_data | |
53 | { | |
54 | public: | |
730cdc88 ILT |
55 | Eh_frame_hdr(Output_section* eh_frame_section, const Eh_frame*); |
56 | ||
57 | // Record that we found an unrecognized .eh_frame section. | |
58 | void | |
59 | found_unrecognized_eh_frame_section() | |
60 | { this->any_unrecognized_eh_frame_sections_ = true; } | |
61 | ||
62 | // Record an FDE. | |
63 | void | |
8383303e | 64 | record_fde(section_offset_type fde_offset, unsigned char fde_encoding) |
730cdc88 ILT |
65 | { |
66 | if (!this->any_unrecognized_eh_frame_sections_) | |
67 | this->fde_offsets_.push_back(std::make_pair(fde_offset, fde_encoding)); | |
68 | } | |
3151305a ILT |
69 | |
70 | // Set the final data size. | |
71 | void | |
27bc2bce | 72 | set_final_data_size(); |
3151305a ILT |
73 | |
74 | // Write the data to the file. | |
75 | void | |
76 | do_write(Output_file*); | |
77 | ||
78 | private: | |
730cdc88 ILT |
79 | // Write the data to the file with the right endianness. |
80 | template<int size, bool big_endian> | |
81 | void | |
82 | do_sized_write(Output_file*); | |
83 | ||
84 | // The data we record for one FDE: the offset of the FDE within the | |
85 | // .eh_frame section, and the FDE encoding. | |
8383303e | 86 | typedef std::pair<section_offset_type, unsigned char> Fde_offset; |
730cdc88 ILT |
87 | |
88 | // The list of information we record for an FDE. | |
89 | typedef std::vector<Fde_offset> Fde_offsets; | |
90 | ||
91 | // When writing out the header, we convert the FDE offsets into FDE | |
92 | // addresses. This is a list of pairs of the offset from the header | |
93 | // to the FDE PC and to the FDE itself. | |
94 | template<int size> | |
95 | class Fde_addresses | |
96 | { | |
97 | public: | |
98 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
99 | typedef typename std::pair<Address, Address> Fde_address; | |
100 | typedef typename std::vector<Fde_address> Fde_address_list; | |
101 | typedef typename Fde_address_list::iterator iterator; | |
102 | ||
103 | Fde_addresses(unsigned int reserve) | |
104 | : fde_addresses_() | |
105 | { this->fde_addresses_.reserve(reserve); } | |
106 | ||
107 | void | |
108 | push_back(Address pc_address, Address fde_address) | |
109 | { | |
110 | this->fde_addresses_.push_back(std::make_pair(pc_address, fde_address)); | |
111 | } | |
112 | ||
113 | iterator | |
114 | begin() | |
115 | { return this->fde_addresses_.begin(); } | |
116 | ||
117 | iterator | |
118 | end() | |
119 | { return this->fde_addresses_.end(); } | |
120 | ||
121 | private: | |
122 | Fde_address_list fde_addresses_; | |
123 | }; | |
124 | ||
125 | // Compare Fde_address objects. | |
126 | template<int size> | |
127 | struct Fde_address_compare | |
128 | { | |
129 | bool | |
130 | operator()(const typename Fde_addresses<size>::Fde_address& f1, | |
131 | const typename Fde_addresses<size>::Fde_address& f2) const | |
132 | { return f1.first < f2.first; } | |
133 | }; | |
134 | ||
135 | // Return the PC to which an FDE refers. | |
136 | template<int size, bool big_endian> | |
137 | typename elfcpp::Elf_types<size>::Elf_Addr | |
4117d768 ILT |
138 | get_fde_pc(typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address, |
139 | const unsigned char* eh_frame_contents, | |
8383303e | 140 | section_offset_type fde_offset, unsigned char fde_encoding); |
730cdc88 ILT |
141 | |
142 | // Convert Fde_offsets to Fde_addresses. | |
143 | template<int size, bool big_endian> | |
144 | void | |
145 | get_fde_addresses(Output_file* of, | |
146 | const Fde_offsets* fde_offsets, | |
147 | Fde_addresses<size>* fde_addresses); | |
148 | ||
3151305a ILT |
149 | // The .eh_frame section. |
150 | Output_section* eh_frame_section_; | |
730cdc88 ILT |
151 | // The .eh_frame section data. |
152 | const Eh_frame* eh_frame_data_; | |
153 | // Data from the FDEs in the .eh_frame sections. | |
154 | Fde_offsets fde_offsets_; | |
155 | // Whether we found any .eh_frame sections which we could not | |
156 | // process. | |
157 | bool any_unrecognized_eh_frame_sections_; | |
158 | }; | |
159 | ||
160 | // This class holds an FDE. | |
161 | ||
162 | class Fde | |
163 | { | |
164 | public: | |
8383303e | 165 | Fde(Relobj* object, unsigned int shndx, section_offset_type input_offset, |
730cdc88 ILT |
166 | const unsigned char* contents, size_t length) |
167 | : object_(object), shndx_(shndx), input_offset_(input_offset), | |
168 | contents_(reinterpret_cast<const char*>(contents), length) | |
169 | { } | |
170 | ||
171 | // Return the length of this FDE. Add 4 for the length and 4 for | |
172 | // the offset to the CIE. | |
173 | size_t | |
174 | length() const | |
175 | { return this->contents_.length() + 8; } | |
176 | ||
177 | // Add a mapping for this FDE to MERGE_MAP. | |
178 | void | |
8383303e | 179 | add_mapping(section_offset_type output_offset, Merge_map* merge_map) const |
730cdc88 ILT |
180 | { |
181 | merge_map->add_mapping(this->object_, this->shndx_, | |
182 | this->input_offset_, this->length(), | |
183 | output_offset); | |
184 | } | |
185 | ||
186 | // Write the FDE to OVIEW starting at OFFSET. FDE_ENCODING is the | |
187 | // encoding, from the CIE. Record the FDE in EH_FRAME_HDR. Return | |
188 | // the new offset. | |
189 | template<int size, bool big_endian> | |
8383303e ILT |
190 | section_offset_type |
191 | write(unsigned char* oview, section_offset_type offset, | |
192 | section_offset_type cie_offset, unsigned char fde_encoding, | |
193 | Eh_frame_hdr* eh_frame_hdr); | |
730cdc88 ILT |
194 | |
195 | private: | |
196 | // The object in which this FDE was seen. | |
197 | Relobj* object_; | |
198 | // Input section index for this FDE. | |
199 | unsigned int shndx_; | |
200 | // Offset within the input section for this FDE. | |
8383303e | 201 | section_offset_type input_offset_; |
730cdc88 ILT |
202 | // FDE data. |
203 | std::string contents_; | |
204 | }; | |
205 | ||
206 | // This class holds a CIE. | |
207 | ||
208 | class Cie | |
209 | { | |
210 | public: | |
8383303e | 211 | Cie(Relobj* object, unsigned int shndx, section_offset_type input_offset, |
730cdc88 ILT |
212 | unsigned char fde_encoding, const char* personality_name, |
213 | const unsigned char* contents, size_t length) | |
214 | : object_(object), | |
215 | shndx_(shndx), | |
216 | input_offset_(input_offset), | |
217 | fde_encoding_(fde_encoding), | |
218 | personality_name_(personality_name), | |
219 | fdes_(), | |
220 | contents_(reinterpret_cast<const char*>(contents), length) | |
221 | { } | |
222 | ||
223 | ~Cie(); | |
224 | ||
225 | // We permit copying a CIE when there are no FDEs. This is | |
226 | // convenient in the code which creates them. | |
227 | Cie(const Cie& cie) | |
228 | : object_(cie.object_), | |
229 | shndx_(cie.shndx_), | |
230 | input_offset_(cie.input_offset_), | |
231 | fde_encoding_(cie.fde_encoding_), | |
232 | personality_name_(cie.personality_name_), | |
233 | fdes_(), | |
234 | contents_(cie.contents_) | |
235 | { gold_assert(cie.fdes_.empty()); } | |
236 | ||
237 | // Add an FDE associated with this CIE. | |
238 | void | |
239 | add_fde(Fde* fde) | |
240 | { this->fdes_.push_back(fde); } | |
241 | ||
242 | // Return the number of FDEs. | |
243 | unsigned int | |
244 | fde_count() const | |
245 | { return this->fdes_.size(); } | |
246 | ||
247 | // Set the output offset of this CIE to OUTPUT_OFFSET. It will be | |
248 | // followed by all its FDEs. ADDRALIGN is the required address | |
249 | // alignment, typically 4 or 8. This updates MERGE_MAP with the | |
250 | // mapping. It returns the new output offset. | |
8383303e ILT |
251 | section_offset_type |
252 | set_output_offset(section_offset_type output_offset, unsigned int addralign, | |
253 | Merge_map*); | |
730cdc88 ILT |
254 | |
255 | // Write the CIE to OVIEW starting at OFFSET. EH_FRAME_HDR is the | |
256 | // exception frame header for FDE recording. Return the new offset. | |
257 | template<int size, bool big_endian> | |
8383303e ILT |
258 | section_offset_type |
259 | write(unsigned char* oview, section_offset_type offset, | |
260 | Eh_frame_hdr* eh_frame_hdr); | |
730cdc88 ILT |
261 | |
262 | friend bool operator<(const Cie&, const Cie&); | |
263 | friend bool operator==(const Cie&, const Cie&); | |
264 | ||
265 | private: | |
266 | // The class is not assignable. | |
267 | Cie& operator=(const Cie&); | |
268 | ||
269 | // The object in which this CIE was first seen. | |
270 | Relobj* object_; | |
271 | // Input section index for this CIE. | |
272 | unsigned int shndx_; | |
273 | // Offset within the input section for this CIE. | |
8383303e | 274 | section_offset_type input_offset_; |
730cdc88 ILT |
275 | // The encoding of the FDE. This is a DW_EH_PE code. |
276 | unsigned char fde_encoding_; | |
277 | // The name of the personality routine. This will be the name of a | |
278 | // global symbol, or will be the empty string. | |
279 | std::string personality_name_; | |
280 | // List of FDEs. | |
281 | std::vector<Fde*> fdes_; | |
282 | // CIE data. | |
283 | std::string contents_; | |
284 | }; | |
285 | ||
286 | extern bool operator<(const Cie&, const Cie&); | |
287 | extern bool operator==(const Cie&, const Cie&); | |
288 | ||
289 | // This class manages .eh_frame sections. It discards duplicate | |
290 | // exception information. | |
291 | ||
292 | class Eh_frame : public Output_section_data | |
293 | { | |
294 | public: | |
295 | Eh_frame(); | |
296 | ||
297 | // Record the associated Eh_frame_hdr, if any. | |
298 | void | |
299 | set_eh_frame_hdr(Eh_frame_hdr* hdr) | |
300 | { this->eh_frame_hdr_ = hdr; } | |
301 | ||
302 | // Add the input section SHNDX in OBJECT. SYMBOLS is the contents | |
303 | // of the symbol table section (size SYMBOLS_SIZE), SYMBOL_NAMES is | |
304 | // the symbol names section (size SYMBOL_NAMES_SIZE). RELOC_SHNDX | |
305 | // is the relocation section if any (0 for none, -1U for multiple). | |
306 | // RELOC_TYPE is the type of the relocation section if any. This | |
307 | // returns whether the section was incorporated into the .eh_frame | |
308 | // data. | |
309 | template<int size, bool big_endian> | |
310 | bool | |
311 | add_ehframe_input_section(Sized_relobj<size, big_endian>* object, | |
312 | const unsigned char* symbols, | |
8383303e | 313 | section_size_type symbols_size, |
730cdc88 | 314 | const unsigned char* symbol_names, |
8383303e | 315 | section_size_type symbol_names_size, |
730cdc88 ILT |
316 | unsigned int shndx, unsigned int reloc_shndx, |
317 | unsigned int reloc_type); | |
318 | ||
319 | // Return the number of FDEs. | |
320 | unsigned int | |
321 | fde_count() const; | |
322 | ||
323 | // Set the final data size. | |
324 | void | |
27bc2bce | 325 | set_final_data_size(); |
730cdc88 ILT |
326 | |
327 | // Return the output address for an input address. | |
328 | bool | |
8383303e ILT |
329 | do_output_offset(const Relobj*, unsigned int shndx, |
330 | section_offset_type offset, | |
331 | section_offset_type* poutput) const; | |
730cdc88 | 332 | |
a9a60db6 ILT |
333 | // Return whether this is the merge section for an input section. |
334 | bool | |
335 | do_is_merge_section_for(const Relobj*, unsigned int shndx) const; | |
336 | ||
730cdc88 ILT |
337 | // Write the data to the file. |
338 | void | |
339 | do_write(Output_file*); | |
340 | ||
341 | private: | |
342 | // The comparison routine for the CIE map. | |
343 | struct Cie_less | |
344 | { | |
345 | bool | |
346 | operator()(const Cie* cie1, const Cie* cie2) const | |
347 | { return *cie1 < *cie2; } | |
348 | }; | |
349 | ||
1cac254c ILT |
350 | // A set of unique CIEs. |
351 | typedef std::set<Cie*, Cie_less> Cie_offsets; | |
730cdc88 | 352 | |
1cac254c ILT |
353 | // A list of unmergeable CIEs. |
354 | typedef std::vector<Cie*> Unmergeable_cie_offsets; | |
730cdc88 ILT |
355 | |
356 | // A mapping from offsets to CIEs. This is used while reading an | |
357 | // input section. | |
358 | typedef std::map<uint64_t, Cie*> Offsets_to_cie; | |
359 | ||
360 | // A list of CIEs, and a bool indicating whether the CIE is | |
361 | // mergeable. | |
362 | typedef std::vector<std::pair<Cie*, bool> > New_cies; | |
363 | ||
364 | // Skip an LEB128. | |
365 | static bool | |
366 | skip_leb128(const unsigned char**, const unsigned char*); | |
367 | ||
368 | // The implementation of add_ehframe_input_section. | |
369 | template<int size, bool big_endian> | |
370 | bool | |
371 | do_add_ehframe_input_section(Sized_relobj<size, big_endian>* object, | |
372 | const unsigned char* symbols, | |
8383303e | 373 | section_size_type symbols_size, |
730cdc88 | 374 | const unsigned char* symbol_names, |
8383303e | 375 | section_size_type symbol_names_size, |
730cdc88 ILT |
376 | unsigned int shndx, |
377 | unsigned int reloc_shndx, | |
378 | unsigned int reloc_type, | |
379 | const unsigned char* pcontents, | |
8383303e | 380 | section_size_type contents_len, |
730cdc88 ILT |
381 | New_cies*); |
382 | ||
383 | // Read a CIE. | |
384 | template<int size, bool big_endian> | |
385 | bool | |
386 | read_cie(Sized_relobj<size, big_endian>* object, | |
387 | unsigned int shndx, | |
388 | const unsigned char* symbols, | |
8383303e | 389 | section_size_type symbols_size, |
730cdc88 | 390 | const unsigned char* symbol_names, |
8383303e | 391 | section_size_type symbol_names_size, |
730cdc88 ILT |
392 | const unsigned char* pcontents, |
393 | const unsigned char* pcie, | |
394 | const unsigned char *pcieend, | |
395 | Track_relocs<size, big_endian>* relocs, | |
396 | Offsets_to_cie* cies, | |
397 | New_cies* new_cies); | |
398 | ||
399 | // Read an FDE. | |
400 | template<int size, bool big_endian> | |
401 | bool | |
402 | read_fde(Sized_relobj<size, big_endian>* object, | |
403 | unsigned int shndx, | |
404 | const unsigned char* symbols, | |
8383303e | 405 | section_size_type symbols_size, |
730cdc88 ILT |
406 | const unsigned char* pcontents, |
407 | unsigned int offset, | |
408 | const unsigned char* pfde, | |
409 | const unsigned char *pfdeend, | |
410 | Track_relocs<size, big_endian>* relocs, | |
411 | Offsets_to_cie* cies); | |
412 | ||
413 | // Template version of write function. | |
414 | template<int size, bool big_endian> | |
415 | void | |
416 | do_sized_write(unsigned char* oview); | |
417 | ||
418 | // The exception frame header, if any. | |
419 | Eh_frame_hdr* eh_frame_hdr_; | |
420 | // A mapping from all unique CIEs to their offset in the output | |
421 | // file. | |
422 | Cie_offsets cie_offsets_; | |
423 | // A mapping from unmergeable CIEs to their offset in the output | |
424 | // file. | |
425 | Unmergeable_cie_offsets unmergeable_cie_offsets_; | |
426 | // A mapping from input sections to the output section. | |
427 | Merge_map merge_map_; | |
3151305a ILT |
428 | }; |
429 | ||
430 | } // End namespace gold. | |
431 | ||
432 | #endif // !defined(GOLD_EHFRAME_H) |