Commit | Line | Data |
---|---|---|
3151305a ILT |
1 | // ehframe.cc -- handle exception frame sections for gold |
2 | ||
4b95cf5c | 3 | // Copyright (C) 2006-2014 Free Software Foundation, Inc. |
3151305a ILT |
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 | #include "gold.h" | |
24 | ||
730cdc88 ILT |
25 | #include <cstring> |
26 | #include <algorithm> | |
27 | ||
3151305a ILT |
28 | #include "elfcpp.h" |
29 | #include "dwarf.h" | |
730cdc88 ILT |
30 | #include "symtab.h" |
31 | #include "reloc.h" | |
3151305a ILT |
32 | #include "ehframe.h" |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
37 | // This file handles generation of the exception frame header that | |
38 | // gcc's runtime support libraries use to find unwind information at | |
730cdc88 ILT |
39 | // runtime. This file also handles discarding duplicate exception |
40 | // frame information. | |
3151305a ILT |
41 | |
42 | // The exception frame header starts with four bytes: | |
43 | ||
44 | // 0: The version number, currently 1. | |
45 | ||
46 | // 1: The encoding of the pointer to the exception frames. This can | |
47 | // be any DWARF unwind encoding (DW_EH_PE_*). It is normally a 4 | |
48 | // byte PC relative offset (DW_EH_PE_pcrel | DW_EH_PE_sdata4). | |
49 | ||
50 | // 2: The encoding of the count of the number of FDE pointers in the | |
51 | // lookup table. This can be any DWARF unwind encoding, and in | |
52 | // particular can be DW_EH_PE_omit if the count is omitted. It is | |
53 | // normally a 4 byte unsigned count (DW_EH_PE_udata4). | |
54 | ||
55 | // 3: The encoding of the lookup table entries. Currently gcc's | |
56 | // libraries will only support DW_EH_PE_datarel | DW_EH_PE_sdata4, | |
57 | // which means that the values are 4 byte offsets from the start of | |
58 | // the table. | |
59 | ||
60 | // The exception frame header is followed by a pointer to the contents | |
61 | // of the exception frame section (.eh_frame). This pointer is | |
62 | // encoded as specified in the byte at offset 1 of the header (i.e., | |
63 | // it is normally a 4 byte PC relative offset). | |
64 | ||
65 | // If there is a lookup table, this is followed by the count of the | |
66 | // number of FDE pointers, encoded as specified in the byte at offset | |
67 | // 2 of the header (i.e., normally a 4 byte unsigned integer). | |
68 | ||
69 | // This is followed by the table, which should start at an 4-byte | |
70 | // aligned address in memory. Each entry in the table is 8 bytes. | |
71 | // Each entry represents an FDE. The first four bytes of each entry | |
72 | // are an offset to the starting PC for the FDE. The last four bytes | |
73 | // of each entry are an offset to the FDE data. The offsets are from | |
74 | // the start of the exception frame header information. The entries | |
75 | // are in sorted order by starting PC. | |
76 | ||
3151305a ILT |
77 | const int eh_frame_hdr_size = 4; |
78 | ||
79 | // Construct the exception frame header. | |
80 | ||
730cdc88 ILT |
81 | Eh_frame_hdr::Eh_frame_hdr(Output_section* eh_frame_section, |
82 | const Eh_frame* eh_frame_data) | |
3151305a | 83 | : Output_section_data(4), |
730cdc88 ILT |
84 | eh_frame_section_(eh_frame_section), |
85 | eh_frame_data_(eh_frame_data), | |
86 | fde_offsets_(), | |
cafdd569 | 87 | any_unrecognized_eh_frame_sections_(false) |
3151305a ILT |
88 | { |
89 | } | |
90 | ||
27bc2bce | 91 | // Set the size of the exception frame header. |
3151305a ILT |
92 | |
93 | void | |
27bc2bce | 94 | Eh_frame_hdr::set_final_data_size() |
3151305a | 95 | { |
2ea97941 | 96 | unsigned int data_size = eh_frame_hdr_size + 4; |
730cdc88 ILT |
97 | if (!this->any_unrecognized_eh_frame_sections_) |
98 | { | |
99 | unsigned int fde_count = this->eh_frame_data_->fde_count(); | |
100 | if (fde_count != 0) | |
2ea97941 | 101 | data_size += 4 + 8 * fde_count; |
730cdc88 ILT |
102 | this->fde_offsets_.reserve(fde_count); |
103 | } | |
2ea97941 | 104 | this->set_data_size(data_size); |
3151305a ILT |
105 | } |
106 | ||
9b547ce6 | 107 | // Write the data to the file. |
3151305a ILT |
108 | |
109 | void | |
110 | Eh_frame_hdr::do_write(Output_file* of) | |
730cdc88 | 111 | { |
8851ecca | 112 | switch (parameters->size_and_endianness()) |
730cdc88 | 113 | { |
730cdc88 | 114 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
115 | case Parameters::TARGET_32_LITTLE: |
116 | this->do_sized_write<32, false>(of); | |
117 | break; | |
730cdc88 | 118 | #endif |
730cdc88 | 119 | #ifdef HAVE_TARGET_32_BIG |
8851ecca ILT |
120 | case Parameters::TARGET_32_BIG: |
121 | this->do_sized_write<32, true>(of); | |
122 | break; | |
730cdc88 | 123 | #endif |
730cdc88 | 124 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
125 | case Parameters::TARGET_64_LITTLE: |
126 | this->do_sized_write<64, false>(of); | |
127 | break; | |
730cdc88 | 128 | #endif |
730cdc88 | 129 | #ifdef HAVE_TARGET_64_BIG |
8851ecca ILT |
130 | case Parameters::TARGET_64_BIG: |
131 | this->do_sized_write<64, true>(of); | |
132 | break; | |
730cdc88 | 133 | #endif |
8851ecca ILT |
134 | default: |
135 | gold_unreachable(); | |
730cdc88 | 136 | } |
730cdc88 ILT |
137 | } |
138 | ||
139 | // Write the data to the file with the right endianness. | |
140 | ||
141 | template<int size, bool big_endian> | |
142 | void | |
143 | Eh_frame_hdr::do_sized_write(Output_file* of) | |
3151305a ILT |
144 | { |
145 | const off_t off = this->offset(); | |
146 | const off_t oview_size = this->data_size(); | |
147 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
148 | ||
149 | // Version number. | |
150 | oview[0] = 1; | |
151 | ||
152 | // Write out a 4 byte PC relative offset to the address of the | |
153 | // .eh_frame section. | |
154 | oview[1] = elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4; | |
155 | uint64_t eh_frame_address = this->eh_frame_section_->address(); | |
156 | uint64_t eh_frame_hdr_address = this->address(); | |
157 | uint64_t eh_frame_offset = (eh_frame_address - | |
158 | (eh_frame_hdr_address + 4)); | |
730cdc88 ILT |
159 | elfcpp::Swap<32, big_endian>::writeval(oview + 4, eh_frame_offset); |
160 | ||
161 | if (this->any_unrecognized_eh_frame_sections_ | |
162 | || this->fde_offsets_.empty()) | |
163 | { | |
164 | // There are no FDEs, or we didn't recognize the format of the | |
165 | // some of the .eh_frame sections, so we can't write out the | |
166 | // sorted table. | |
167 | oview[2] = elfcpp::DW_EH_PE_omit; | |
168 | oview[3] = elfcpp::DW_EH_PE_omit; | |
169 | ||
170 | gold_assert(oview_size == 8); | |
171 | } | |
3151305a | 172 | else |
730cdc88 ILT |
173 | { |
174 | oview[2] = elfcpp::DW_EH_PE_udata4; | |
175 | oview[3] = elfcpp::DW_EH_PE_datarel | elfcpp::DW_EH_PE_sdata4; | |
176 | ||
177 | elfcpp::Swap<32, big_endian>::writeval(oview + 8, | |
178 | this->fde_offsets_.size()); | |
179 | ||
180 | // We have the offsets of the FDEs in the .eh_frame section. We | |
181 | // couldn't easily get the PC values before, as they depend on | |
182 | // relocations which are, of course, target specific. This code | |
183 | // is run after all those relocations have been applied to the | |
184 | // output file. Here we read the output file again to find the | |
185 | // PC values. Then we sort the list and write it out. | |
3151305a | 186 | |
730cdc88 ILT |
187 | Fde_addresses<size> fde_addresses(this->fde_offsets_.size()); |
188 | this->get_fde_addresses<size, big_endian>(of, &this->fde_offsets_, | |
189 | &fde_addresses); | |
3151305a | 190 | |
730cdc88 ILT |
191 | std::sort(fde_addresses.begin(), fde_addresses.end(), |
192 | Fde_address_compare<size>()); | |
193 | ||
194 | typename elfcpp::Elf_types<size>::Elf_Addr output_address; | |
195 | output_address = this->address(); | |
196 | ||
197 | unsigned char* pfde = oview + 12; | |
198 | for (typename Fde_addresses<size>::iterator p = fde_addresses.begin(); | |
199 | p != fde_addresses.end(); | |
200 | ++p) | |
201 | { | |
202 | elfcpp::Swap<32, big_endian>::writeval(pfde, | |
203 | p->first - output_address); | |
204 | elfcpp::Swap<32, big_endian>::writeval(pfde + 4, | |
205 | p->second - output_address); | |
206 | pfde += 8; | |
207 | } | |
208 | ||
209 | gold_assert(pfde - oview == oview_size); | |
210 | } | |
3151305a ILT |
211 | |
212 | of->write_output_view(off, oview_size, oview); | |
213 | } | |
214 | ||
730cdc88 ILT |
215 | // Given the offset FDE_OFFSET of an FDE in the .eh_frame section, and |
216 | // the contents of the .eh_frame section EH_FRAME_CONTENTS, where the | |
217 | // FDE's encoding is FDE_ENCODING, return the output address of the | |
218 | // FDE's PC. | |
219 | ||
220 | template<int size, bool big_endian> | |
221 | typename elfcpp::Elf_types<size>::Elf_Addr | |
4117d768 ILT |
222 | Eh_frame_hdr::get_fde_pc( |
223 | typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address, | |
224 | const unsigned char* eh_frame_contents, | |
8383303e | 225 | section_offset_type fde_offset, |
4117d768 | 226 | unsigned char fde_encoding) |
730cdc88 ILT |
227 | { |
228 | // The FDE starts with a 4 byte length and a 4 byte offset to the | |
229 | // CIE. The PC follows. | |
230 | const unsigned char* p = eh_frame_contents + fde_offset + 8; | |
231 | ||
232 | typename elfcpp::Elf_types<size>::Elf_Addr pc; | |
233 | bool is_signed = (fde_encoding & elfcpp::DW_EH_PE_signed) != 0; | |
234 | int pc_size = fde_encoding & 7; | |
235 | if (pc_size == elfcpp::DW_EH_PE_absptr) | |
236 | { | |
237 | if (size == 32) | |
238 | pc_size = elfcpp::DW_EH_PE_udata4; | |
239 | else if (size == 64) | |
240 | pc_size = elfcpp::DW_EH_PE_udata8; | |
241 | else | |
242 | gold_unreachable(); | |
243 | } | |
244 | ||
245 | switch (pc_size) | |
246 | { | |
247 | case elfcpp::DW_EH_PE_udata2: | |
248 | pc = elfcpp::Swap<16, big_endian>::readval(p); | |
249 | if (is_signed) | |
250 | pc = (pc ^ 0x8000) - 0x8000; | |
251 | break; | |
252 | ||
253 | case elfcpp::DW_EH_PE_udata4: | |
254 | pc = elfcpp::Swap<32, big_endian>::readval(p); | |
255 | if (size > 32 && is_signed) | |
256 | pc = (pc ^ 0x80000000) - 0x80000000; | |
257 | break; | |
258 | ||
259 | case elfcpp::DW_EH_PE_udata8: | |
260 | gold_assert(size == 64); | |
261 | pc = elfcpp::Swap_unaligned<64, big_endian>::readval(p); | |
262 | break; | |
263 | ||
264 | default: | |
4117d768 ILT |
265 | // All other cases were rejected in Eh_frame::read_cie. |
266 | gold_unreachable(); | |
267 | } | |
268 | ||
02d7cd44 | 269 | switch (fde_encoding & 0x70) |
4117d768 ILT |
270 | { |
271 | case 0: | |
272 | break; | |
273 | ||
274 | case elfcpp::DW_EH_PE_pcrel: | |
275 | pc += eh_frame_address + fde_offset + 8; | |
276 | break; | |
277 | ||
02d7cd44 ILT |
278 | case elfcpp::DW_EH_PE_datarel: |
279 | pc += parameters->target().ehframe_datarel_base(); | |
280 | break; | |
281 | ||
4117d768 ILT |
282 | default: |
283 | // If other cases arise, then we have to handle them, or we have | |
284 | // to reject them by returning false in Eh_frame::read_cie. | |
730cdc88 ILT |
285 | gold_unreachable(); |
286 | } | |
287 | ||
02d7cd44 ILT |
288 | gold_assert((fde_encoding & elfcpp::DW_EH_PE_indirect) == 0); |
289 | ||
730cdc88 ILT |
290 | return pc; |
291 | } | |
292 | ||
293 | // Given an array of FDE offsets in the .eh_frame section, return an | |
294 | // array of offsets from the exception frame header to the FDE's | |
295 | // output PC and to the output address of the FDE itself. We get the | |
296 | // FDE's PC by actually looking in the .eh_frame section we just wrote | |
297 | // to the output file. | |
298 | ||
299 | template<int size, bool big_endian> | |
300 | void | |
301 | Eh_frame_hdr::get_fde_addresses(Output_file* of, | |
302 | const Fde_offsets* fde_offsets, | |
303 | Fde_addresses<size>* fde_addresses) | |
304 | { | |
305 | typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address; | |
306 | eh_frame_address = this->eh_frame_section_->address(); | |
307 | off_t eh_frame_offset = this->eh_frame_section_->offset(); | |
308 | off_t eh_frame_size = this->eh_frame_section_->data_size(); | |
309 | const unsigned char* eh_frame_contents = of->get_input_view(eh_frame_offset, | |
310 | eh_frame_size); | |
311 | ||
312 | for (Fde_offsets::const_iterator p = fde_offsets->begin(); | |
313 | p != fde_offsets->end(); | |
314 | ++p) | |
315 | { | |
316 | typename elfcpp::Elf_types<size>::Elf_Addr fde_pc; | |
4117d768 ILT |
317 | fde_pc = this->get_fde_pc<size, big_endian>(eh_frame_address, |
318 | eh_frame_contents, | |
730cdc88 ILT |
319 | p->first, p->second); |
320 | fde_addresses->push_back(fde_pc, eh_frame_address + p->first); | |
321 | } | |
322 | ||
323 | of->free_input_view(eh_frame_offset, eh_frame_size, eh_frame_contents); | |
324 | } | |
325 | ||
326 | // Class Fde. | |
327 | ||
328 | // Write the FDE to OVIEW starting at OFFSET. CIE_OFFSET is the | |
9860cbcf CC |
329 | // offset of the CIE in OVIEW. OUTPUT_OFFSET is the offset of the |
330 | // Eh_frame section within the output section. FDE_ENCODING is the | |
331 | // encoding, from the CIE. ADDRALIGN is the required alignment. | |
332 | // ADDRESS is the virtual address of OVIEW. Record the FDE pc for | |
333 | // EH_FRAME_HDR. Return the new offset. | |
730cdc88 ILT |
334 | |
335 | template<int size, bool big_endian> | |
8383303e | 336 | section_offset_type |
9860cbcf CC |
337 | Fde::write(unsigned char* oview, section_offset_type output_offset, |
338 | section_offset_type offset, uint64_t address, unsigned int addralign, | |
07a60597 ILT |
339 | section_offset_type cie_offset, unsigned char fde_encoding, |
340 | Eh_frame_hdr* eh_frame_hdr) | |
730cdc88 | 341 | { |
935e8877 ILT |
342 | gold_assert((offset & (addralign - 1)) == 0); |
343 | ||
2ea97941 | 344 | size_t length = this->contents_.length(); |
730cdc88 | 345 | |
935e8877 ILT |
346 | // We add 8 when getting the aligned length to account for the |
347 | // length word and the CIE offset. | |
2ea97941 | 348 | size_t aligned_full_length = align_address(length + 8, addralign); |
935e8877 | 349 | |
730cdc88 ILT |
350 | // Write the length of the FDE as a 32-bit word. The length word |
351 | // does not include the four bytes of the length word itself, but it | |
352 | // does include the offset to the CIE. | |
353 | elfcpp::Swap<32, big_endian>::writeval(oview + offset, | |
935e8877 | 354 | aligned_full_length - 4); |
730cdc88 ILT |
355 | |
356 | // Write the offset to the CIE as a 32-bit word. This is the | |
357 | // difference between the address of the offset word itself and the | |
358 | // CIE address. | |
359 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, | |
360 | offset + 4 - cie_offset); | |
361 | ||
362 | // Copy the rest of the FDE. Note that this is run before | |
363 | // relocation processing is done on this section, so the relocations | |
364 | // will later be applied to the FDE data. | |
2ea97941 | 365 | memcpy(oview + offset + 8, this->contents_.data(), length); |
730cdc88 | 366 | |
07a60597 ILT |
367 | // If this FDE is associated with a PLT, fill in the PLT's address |
368 | // and size. | |
369 | if (this->object_ == NULL) | |
370 | { | |
371 | gold_assert(memcmp(oview + offset + 8, "\0\0\0\0\0\0\0\0", 8) == 0); | |
9d5781f8 AM |
372 | uint64_t paddress; |
373 | off_t psize; | |
374 | parameters->target().plt_fde_location(this->u_.from_linker.plt, | |
375 | oview + offset + 8, | |
376 | &paddress, &psize); | |
377 | uint64_t poffset = paddress - (address + offset + 8); | |
07a60597 | 378 | int32_t spoffset = static_cast<int32_t>(poffset); |
07a60597 ILT |
379 | uint32_t upsize = static_cast<uint32_t>(psize); |
380 | if (static_cast<uint64_t>(static_cast<int64_t>(spoffset)) != poffset | |
381 | || static_cast<off_t>(upsize) != psize) | |
382 | gold_warning(_("overflow in PLT unwind data; " | |
383 | "unwinding through PLT may fail")); | |
384 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 8, spoffset); | |
385 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 12, upsize); | |
386 | } | |
387 | ||
2ea97941 ILT |
388 | if (aligned_full_length > length + 8) |
389 | memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8)); | |
935e8877 | 390 | |
730cdc88 ILT |
391 | // Tell the exception frame header about this FDE. |
392 | if (eh_frame_hdr != NULL) | |
9860cbcf | 393 | eh_frame_hdr->record_fde(output_offset + offset, fde_encoding); |
730cdc88 | 394 | |
935e8877 | 395 | return offset + aligned_full_length; |
730cdc88 ILT |
396 | } |
397 | ||
398 | // Class Cie. | |
399 | ||
400 | // Destructor. | |
401 | ||
402 | Cie::~Cie() | |
403 | { | |
404 | for (std::vector<Fde*>::iterator p = this->fdes_.begin(); | |
405 | p != this->fdes_.end(); | |
406 | ++p) | |
407 | delete *p; | |
408 | } | |
409 | ||
410 | // Set the output offset of a CIE. Return the new output offset. | |
411 | ||
8383303e ILT |
412 | section_offset_type |
413 | Cie::set_output_offset(section_offset_type output_offset, | |
414 | unsigned int addralign, | |
730cdc88 ILT |
415 | Merge_map* merge_map) |
416 | { | |
417 | size_t length = this->contents_.length(); | |
935e8877 | 418 | |
730cdc88 ILT |
419 | // Add 4 for length and 4 for zero CIE identifier tag. |
420 | length += 8; | |
421 | ||
07a60597 ILT |
422 | if (this->object_ != NULL) |
423 | { | |
424 | // Add a mapping so that relocations are applied correctly. | |
425 | merge_map->add_mapping(this->object_, this->shndx_, this->input_offset_, | |
426 | length, output_offset); | |
427 | } | |
730cdc88 | 428 | |
935e8877 ILT |
429 | length = align_address(length, addralign); |
430 | ||
730cdc88 ILT |
431 | for (std::vector<Fde*>::const_iterator p = this->fdes_.begin(); |
432 | p != this->fdes_.end(); | |
433 | ++p) | |
434 | { | |
435 | (*p)->add_mapping(output_offset + length, merge_map); | |
436 | ||
437 | size_t fde_length = (*p)->length(); | |
935e8877 | 438 | fde_length = align_address(fde_length, addralign); |
730cdc88 ILT |
439 | length += fde_length; |
440 | } | |
441 | ||
442 | return output_offset + length; | |
443 | } | |
444 | ||
9860cbcf CC |
445 | // Write the CIE to OVIEW starting at OFFSET. OUTPUT_OFFSET is the |
446 | // offset of the Eh_frame section within the output section. Round up | |
447 | // the bytes to ADDRALIGN. ADDRESS is the virtual address of OVIEW. | |
9d5781f8 AM |
448 | // EH_FRAME_HDR is the exception frame header for FDE recording. |
449 | // POST_FDES stashes FDEs created after mappings were done, for later | |
450 | // writing. Return the new offset. | |
730cdc88 ILT |
451 | |
452 | template<int size, bool big_endian> | |
8383303e | 453 | section_offset_type |
9860cbcf CC |
454 | Cie::write(unsigned char* oview, section_offset_type output_offset, |
455 | section_offset_type offset, uint64_t address, | |
456 | unsigned int addralign, Eh_frame_hdr* eh_frame_hdr, | |
457 | Post_fdes* post_fdes) | |
730cdc88 | 458 | { |
935e8877 ILT |
459 | gold_assert((offset & (addralign - 1)) == 0); |
460 | ||
8383303e | 461 | section_offset_type cie_offset = offset; |
730cdc88 ILT |
462 | |
463 | size_t length = this->contents_.length(); | |
464 | ||
935e8877 ILT |
465 | // We add 8 when getting the aligned length to account for the |
466 | // length word and the CIE tag. | |
467 | size_t aligned_full_length = align_address(length + 8, addralign); | |
468 | ||
730cdc88 ILT |
469 | // Write the length of the CIE as a 32-bit word. The length word |
470 | // does not include the four bytes of the length word itself. | |
935e8877 ILT |
471 | elfcpp::Swap<32, big_endian>::writeval(oview + offset, |
472 | aligned_full_length - 4); | |
730cdc88 ILT |
473 | |
474 | // Write the tag which marks this as a CIE: a 32-bit zero. | |
475 | elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, 0); | |
476 | ||
477 | // Write out the CIE data. | |
478 | memcpy(oview + offset + 8, this->contents_.data(), length); | |
935e8877 ILT |
479 | |
480 | if (aligned_full_length > length + 8) | |
481 | memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8)); | |
482 | ||
483 | offset += aligned_full_length; | |
730cdc88 ILT |
484 | |
485 | // Write out the associated FDEs. | |
486 | unsigned char fde_encoding = this->fde_encoding_; | |
487 | for (std::vector<Fde*>::const_iterator p = this->fdes_.begin(); | |
488 | p != this->fdes_.end(); | |
489 | ++p) | |
9d5781f8 AM |
490 | { |
491 | if ((*p)->post_map()) | |
4bead2d5 | 492 | post_fdes->push_back(Post_fde(*p, cie_offset, fde_encoding)); |
9d5781f8 | 493 | else |
9860cbcf CC |
494 | offset = (*p)->write<size, big_endian>(oview, output_offset, offset, |
495 | address, addralign, cie_offset, | |
9d5781f8 AM |
496 | fde_encoding, eh_frame_hdr); |
497 | } | |
730cdc88 ILT |
498 | |
499 | return offset; | |
500 | } | |
501 | ||
502 | // We track all the CIEs we see, and merge them when possible. This | |
503 | // works because each FDE holds an offset to the relevant CIE: we | |
504 | // rewrite the FDEs to point to the merged CIE. This is worthwhile | |
505 | // because in a typical C++ program many FDEs in many different object | |
506 | // files will use the same CIE. | |
507 | ||
508 | // An equality operator for Cie. | |
509 | ||
510 | bool | |
511 | operator==(const Cie& cie1, const Cie& cie2) | |
512 | { | |
513 | return (cie1.personality_name_ == cie2.personality_name_ | |
514 | && cie1.contents_ == cie2.contents_); | |
515 | } | |
516 | ||
517 | // A less-than operator for Cie. | |
518 | ||
519 | bool | |
520 | operator<(const Cie& cie1, const Cie& cie2) | |
521 | { | |
522 | if (cie1.personality_name_ != cie2.personality_name_) | |
523 | return cie1.personality_name_ < cie2.personality_name_; | |
524 | return cie1.contents_ < cie2.contents_; | |
525 | } | |
526 | ||
527 | // Class Eh_frame. | |
528 | ||
529 | Eh_frame::Eh_frame() | |
530 | : Output_section_data(Output_data::default_alignment()), | |
531 | eh_frame_hdr_(NULL), | |
532 | cie_offsets_(), | |
533 | unmergeable_cie_offsets_(), | |
1d6531cf ILT |
534 | merge_map_(), |
535 | mappings_are_done_(false), | |
536 | final_data_size_(0) | |
730cdc88 ILT |
537 | { |
538 | } | |
539 | ||
540 | // Skip an LEB128, updating *PP to point to the next character. | |
541 | // Return false if we ran off the end of the string. | |
542 | ||
543 | bool | |
544 | Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend) | |
545 | { | |
546 | const unsigned char* p; | |
547 | for (p = *pp; p < pend; ++p) | |
548 | { | |
549 | if ((*p & 0x80) == 0) | |
550 | { | |
551 | *pp = p + 1; | |
552 | return true; | |
553 | } | |
554 | } | |
555 | return false; | |
556 | } | |
557 | ||
558 | // Add input section SHNDX in OBJECT to an exception frame section. | |
559 | // SYMBOLS is the contents of the symbol table section (size | |
560 | // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size | |
561 | // SYMBOL_NAMES_SIZE). RELOC_SHNDX is the index of a relocation | |
562 | // section applying to SHNDX, or 0 if none, or -1U if more than one. | |
563 | // RELOC_TYPE is the type of the reloc section if there is one, either | |
564 | // SHT_REL or SHT_RELA. We try to parse the input exception frame | |
565 | // data into our data structures. If we can't do it, we return false | |
566 | // to mean that the section should be handled as a normal input | |
567 | // section. | |
568 | ||
569 | template<int size, bool big_endian> | |
570 | bool | |
571 | Eh_frame::add_ehframe_input_section( | |
6fa2a40b | 572 | Sized_relobj_file<size, big_endian>* object, |
730cdc88 | 573 | const unsigned char* symbols, |
8383303e | 574 | section_size_type symbols_size, |
730cdc88 | 575 | const unsigned char* symbol_names, |
8383303e | 576 | section_size_type symbol_names_size, |
730cdc88 ILT |
577 | unsigned int shndx, |
578 | unsigned int reloc_shndx, | |
579 | unsigned int reloc_type) | |
580 | { | |
581 | // Get the section contents. | |
8383303e | 582 | section_size_type contents_len; |
730cdc88 ILT |
583 | const unsigned char* pcontents = object->section_contents(shndx, |
584 | &contents_len, | |
585 | false); | |
586 | if (contents_len == 0) | |
587 | return false; | |
588 | ||
589 | // If this is the marker section for the end of the data, then | |
590 | // return false to force it to be handled as an ordinary input | |
591 | // section. If we don't do this, we won't correctly handle the case | |
592 | // of unrecognized .eh_frame sections. | |
593 | if (contents_len == 4 | |
594 | && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0) | |
595 | return false; | |
596 | ||
597 | New_cies new_cies; | |
598 | if (!this->do_add_ehframe_input_section(object, symbols, symbols_size, | |
599 | symbol_names, symbol_names_size, | |
600 | shndx, reloc_shndx, | |
601 | reloc_type, pcontents, | |
602 | contents_len, &new_cies)) | |
603 | { | |
5edd166e ILT |
604 | if (this->eh_frame_hdr_ != NULL) |
605 | this->eh_frame_hdr_->found_unrecognized_eh_frame_section(); | |
730cdc88 ILT |
606 | |
607 | for (New_cies::iterator p = new_cies.begin(); | |
608 | p != new_cies.end(); | |
609 | ++p) | |
610 | delete p->first; | |
611 | ||
612 | return false; | |
613 | } | |
614 | ||
615 | // Now that we know we are using this section, record any new CIEs | |
616 | // that we found. | |
617 | for (New_cies::const_iterator p = new_cies.begin(); | |
618 | p != new_cies.end(); | |
619 | ++p) | |
620 | { | |
730cdc88 | 621 | if (p->second) |
1cac254c | 622 | this->cie_offsets_.insert(p->first); |
730cdc88 | 623 | else |
1cac254c | 624 | this->unmergeable_cie_offsets_.push_back(p->first); |
730cdc88 ILT |
625 | } |
626 | ||
627 | return true; | |
628 | } | |
629 | ||
630 | // The bulk of the implementation of add_ehframe_input_section. | |
631 | ||
632 | template<int size, bool big_endian> | |
633 | bool | |
634 | Eh_frame::do_add_ehframe_input_section( | |
6fa2a40b | 635 | Sized_relobj_file<size, big_endian>* object, |
730cdc88 | 636 | const unsigned char* symbols, |
8383303e | 637 | section_size_type symbols_size, |
730cdc88 | 638 | const unsigned char* symbol_names, |
8383303e | 639 | section_size_type symbol_names_size, |
730cdc88 ILT |
640 | unsigned int shndx, |
641 | unsigned int reloc_shndx, | |
642 | unsigned int reloc_type, | |
643 | const unsigned char* pcontents, | |
8383303e | 644 | section_size_type contents_len, |
730cdc88 ILT |
645 | New_cies* new_cies) |
646 | { | |
730cdc88 ILT |
647 | Track_relocs<size, big_endian> relocs; |
648 | ||
649 | const unsigned char* p = pcontents; | |
650 | const unsigned char* pend = p + contents_len; | |
651 | ||
652 | // Get the contents of the reloc section if any. | |
653 | if (!relocs.initialize(object, reloc_shndx, reloc_type)) | |
654 | return false; | |
655 | ||
656 | // Keep track of which CIEs are at which offsets. | |
657 | Offsets_to_cie cies; | |
658 | ||
659 | while (p < pend) | |
660 | { | |
661 | if (pend - p < 4) | |
662 | return false; | |
663 | ||
664 | // There shouldn't be any relocations here. | |
665 | if (relocs.advance(p + 4 - pcontents) > 0) | |
666 | return false; | |
667 | ||
668 | unsigned int len = elfcpp::Swap<32, big_endian>::readval(p); | |
669 | p += 4; | |
670 | if (len == 0) | |
671 | { | |
672 | // We should only find a zero-length entry at the end of the | |
673 | // section. | |
674 | if (p < pend) | |
675 | return false; | |
676 | break; | |
677 | } | |
678 | // We don't support a 64-bit .eh_frame. | |
679 | if (len == 0xffffffff) | |
680 | return false; | |
681 | if (static_cast<unsigned int>(pend - p) < len) | |
682 | return false; | |
683 | ||
684 | const unsigned char* const pentend = p + len; | |
685 | ||
686 | if (pend - p < 4) | |
687 | return false; | |
688 | if (relocs.advance(p + 4 - pcontents) > 0) | |
689 | return false; | |
690 | ||
691 | unsigned int id = elfcpp::Swap<32, big_endian>::readval(p); | |
692 | p += 4; | |
693 | ||
694 | if (id == 0) | |
695 | { | |
696 | // CIE. | |
697 | if (!this->read_cie(object, shndx, symbols, symbols_size, | |
698 | symbol_names, symbol_names_size, | |
699 | pcontents, p, pentend, &relocs, &cies, | |
700 | new_cies)) | |
701 | return false; | |
702 | } | |
703 | else | |
704 | { | |
705 | // FDE. | |
706 | if (!this->read_fde(object, shndx, symbols, symbols_size, | |
707 | pcontents, id, p, pentend, &relocs, &cies)) | |
708 | return false; | |
709 | } | |
710 | ||
711 | p = pentend; | |
712 | } | |
713 | ||
714 | return true; | |
715 | } | |
716 | ||
717 | // Read a CIE. Return false if we can't parse the information. | |
718 | ||
719 | template<int size, bool big_endian> | |
720 | bool | |
6fa2a40b | 721 | Eh_frame::read_cie(Sized_relobj_file<size, big_endian>* object, |
730cdc88 ILT |
722 | unsigned int shndx, |
723 | const unsigned char* symbols, | |
8383303e | 724 | section_size_type symbols_size, |
730cdc88 | 725 | const unsigned char* symbol_names, |
8383303e | 726 | section_size_type symbol_names_size, |
730cdc88 ILT |
727 | const unsigned char* pcontents, |
728 | const unsigned char* pcie, | |
ca09d69a | 729 | const unsigned char* pcieend, |
730cdc88 ILT |
730 | Track_relocs<size, big_endian>* relocs, |
731 | Offsets_to_cie* cies, | |
732 | New_cies* new_cies) | |
733 | { | |
734 | bool mergeable = true; | |
735 | ||
736 | // We need to find the personality routine if there is one, since we | |
737 | // can only merge CIEs which use the same routine. We also need to | |
738 | // find the FDE encoding if there is one, so that we can read the PC | |
739 | // from the FDE. | |
740 | ||
741 | const unsigned char* p = pcie; | |
742 | ||
743 | if (pcieend - p < 1) | |
744 | return false; | |
745 | unsigned char version = *p++; | |
746 | if (version != 1 && version != 3) | |
747 | return false; | |
748 | ||
749 | const unsigned char* paug = p; | |
750 | const void* paugendv = memchr(p, '\0', pcieend - p); | |
751 | const unsigned char* paugend = static_cast<const unsigned char*>(paugendv); | |
752 | if (paugend == NULL) | |
753 | return false; | |
754 | p = paugend + 1; | |
755 | ||
756 | if (paug[0] == 'e' && paug[1] == 'h') | |
757 | { | |
758 | // This is a CIE from gcc before version 3.0. We can't merge | |
759 | // these. We can still read the FDEs. | |
760 | mergeable = false; | |
761 | paug += 2; | |
762 | if (*paug != '\0') | |
763 | return false; | |
764 | if (pcieend - p < size / 8) | |
765 | return false; | |
766 | p += size / 8; | |
767 | } | |
768 | ||
769 | // Skip the code alignment. | |
770 | if (!skip_leb128(&p, pcieend)) | |
771 | return false; | |
772 | ||
773 | // Skip the data alignment. | |
774 | if (!skip_leb128(&p, pcieend)) | |
775 | return false; | |
776 | ||
777 | // Skip the return column. | |
778 | if (version == 1) | |
779 | { | |
780 | if (pcieend - p < 1) | |
781 | return false; | |
782 | ++p; | |
783 | } | |
784 | else | |
785 | { | |
786 | if (!skip_leb128(&p, pcieend)) | |
787 | return false; | |
788 | } | |
789 | ||
790 | if (*paug == 'z') | |
791 | { | |
792 | ++paug; | |
793 | // Skip the augmentation size. | |
794 | if (!skip_leb128(&p, pcieend)) | |
795 | return false; | |
796 | } | |
797 | ||
798 | unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr; | |
799 | int per_offset = -1; | |
800 | while (*paug != '\0') | |
801 | { | |
802 | switch (*paug) | |
803 | { | |
804 | case 'L': // LSDA encoding. | |
805 | if (pcieend - p < 1) | |
806 | return false; | |
807 | ++p; | |
808 | break; | |
809 | ||
810 | case 'R': // FDE encoding. | |
811 | if (pcieend - p < 1) | |
812 | return false; | |
813 | fde_encoding = *p; | |
814 | switch (fde_encoding & 7) | |
815 | { | |
816 | case elfcpp::DW_EH_PE_absptr: | |
817 | case elfcpp::DW_EH_PE_udata2: | |
818 | case elfcpp::DW_EH_PE_udata4: | |
819 | case elfcpp::DW_EH_PE_udata8: | |
820 | break; | |
821 | default: | |
4117d768 ILT |
822 | // We don't expect to see any other cases here, and |
823 | // we're not prepared to handle them. | |
730cdc88 ILT |
824 | return false; |
825 | } | |
826 | ++p; | |
827 | break; | |
828 | ||
829 | case 'S': | |
830 | break; | |
831 | ||
832 | case 'P': | |
833 | // Personality encoding. | |
834 | { | |
835 | if (pcieend - p < 1) | |
836 | return false; | |
837 | unsigned char per_encoding = *p; | |
838 | ++p; | |
839 | ||
840 | if ((per_encoding & 0x60) == 0x60) | |
841 | return false; | |
842 | unsigned int per_width; | |
843 | switch (per_encoding & 7) | |
844 | { | |
845 | case elfcpp::DW_EH_PE_udata2: | |
846 | per_width = 2; | |
847 | break; | |
848 | case elfcpp::DW_EH_PE_udata4: | |
849 | per_width = 4; | |
850 | break; | |
851 | case elfcpp::DW_EH_PE_udata8: | |
852 | per_width = 8; | |
853 | break; | |
854 | case elfcpp::DW_EH_PE_absptr: | |
855 | per_width = size / 8; | |
856 | break; | |
857 | default: | |
858 | return false; | |
859 | } | |
860 | ||
861 | if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned) | |
862 | { | |
863 | unsigned int len = p - pcie; | |
864 | len += per_width - 1; | |
865 | len &= ~ (per_width - 1); | |
866 | if (static_cast<unsigned int>(pcieend - p) < len) | |
867 | return false; | |
868 | p += len; | |
869 | } | |
870 | ||
871 | per_offset = p - pcontents; | |
872 | ||
873 | if (static_cast<unsigned int>(pcieend - p) < per_width) | |
874 | return false; | |
875 | p += per_width; | |
876 | } | |
877 | break; | |
878 | ||
879 | default: | |
880 | return false; | |
881 | } | |
882 | ||
883 | ++paug; | |
884 | } | |
885 | ||
886 | const char* personality_name = ""; | |
887 | if (per_offset != -1) | |
888 | { | |
889 | if (relocs->advance(per_offset) > 0) | |
890 | return false; | |
891 | if (relocs->next_offset() != per_offset) | |
892 | return false; | |
893 | ||
894 | unsigned int personality_symndx = relocs->next_symndx(); | |
895 | if (personality_symndx == -1U) | |
896 | return false; | |
897 | ||
898 | if (personality_symndx < object->local_symbol_count()) | |
899 | { | |
900 | // We can only merge this CIE if the personality routine is | |
901 | // a global symbol. We can still read the FDEs. | |
902 | mergeable = false; | |
903 | } | |
904 | else | |
905 | { | |
906 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
907 | if (personality_symndx >= symbols_size / sym_size) | |
908 | return false; | |
909 | elfcpp::Sym<size, big_endian> sym(symbols | |
910 | + (personality_symndx * sym_size)); | |
911 | unsigned int name_offset = sym.get_st_name(); | |
912 | if (name_offset >= symbol_names_size) | |
913 | return false; | |
914 | personality_name = (reinterpret_cast<const char*>(symbol_names) | |
915 | + name_offset); | |
916 | } | |
917 | ||
918 | int r = relocs->advance(per_offset + 1); | |
919 | gold_assert(r == 1); | |
920 | } | |
921 | ||
922 | if (relocs->advance(pcieend - pcontents) > 0) | |
923 | return false; | |
924 | ||
925 | Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding, | |
926 | personality_name, pcie, pcieend - pcie); | |
927 | Cie* cie_pointer = NULL; | |
928 | if (mergeable) | |
929 | { | |
930 | Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie); | |
931 | if (find_cie != this->cie_offsets_.end()) | |
1cac254c | 932 | cie_pointer = *find_cie; |
730cdc88 ILT |
933 | else |
934 | { | |
935 | // See if we already saw this CIE in this object file. | |
936 | for (New_cies::const_iterator pc = new_cies->begin(); | |
937 | pc != new_cies->end(); | |
938 | ++pc) | |
939 | { | |
940 | if (*(pc->first) == cie) | |
941 | { | |
942 | cie_pointer = pc->first; | |
943 | break; | |
944 | } | |
945 | } | |
946 | } | |
947 | } | |
948 | ||
949 | if (cie_pointer == NULL) | |
950 | { | |
951 | cie_pointer = new Cie(cie); | |
952 | new_cies->push_back(std::make_pair(cie_pointer, mergeable)); | |
953 | } | |
954 | else | |
955 | { | |
956 | // We are deleting this CIE. Record that in our mapping from | |
957 | // input sections to the output section. At this point we don't | |
958 | // know for sure that we are doing a special mapping for this | |
959 | // input section, but that's OK--if we don't do a special | |
960 | // mapping, nobody will ever ask for the mapping we add here. | |
961 | this->merge_map_.add_mapping(object, shndx, (pcie - 8) - pcontents, | |
962 | pcieend - (pcie - 8), -1); | |
963 | } | |
964 | ||
965 | // Record this CIE plus the offset in the input section. | |
966 | cies->insert(std::make_pair(pcie - pcontents, cie_pointer)); | |
967 | ||
968 | return true; | |
969 | } | |
970 | ||
971 | // Read an FDE. Return false if we can't parse the information. | |
972 | ||
973 | template<int size, bool big_endian> | |
974 | bool | |
6fa2a40b | 975 | Eh_frame::read_fde(Sized_relobj_file<size, big_endian>* object, |
730cdc88 ILT |
976 | unsigned int shndx, |
977 | const unsigned char* symbols, | |
8383303e | 978 | section_size_type symbols_size, |
730cdc88 | 979 | const unsigned char* pcontents, |
2ea97941 | 980 | unsigned int offset, |
730cdc88 | 981 | const unsigned char* pfde, |
ca09d69a | 982 | const unsigned char* pfdeend, |
730cdc88 ILT |
983 | Track_relocs<size, big_endian>* relocs, |
984 | Offsets_to_cie* cies) | |
985 | { | |
2ea97941 | 986 | // OFFSET is the distance between the 4 bytes before PFDE to the |
730cdc88 ILT |
987 | // start of the CIE. The offset we recorded for the CIE is 8 bytes |
988 | // after the start of the CIE--after the length and the zero tag. | |
2ea97941 | 989 | unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8; |
730cdc88 ILT |
990 | Offsets_to_cie::const_iterator pcie = cies->find(cie_offset); |
991 | if (pcie == cies->end()) | |
992 | return false; | |
993 | Cie* cie = pcie->second; | |
994 | ||
995 | // The FDE should start with a reloc to the start of the code which | |
996 | // it describes. | |
997 | if (relocs->advance(pfde - pcontents) > 0) | |
998 | return false; | |
999 | ||
1000 | if (relocs->next_offset() != pfde - pcontents) | |
1001 | return false; | |
1002 | ||
1003 | unsigned int symndx = relocs->next_symndx(); | |
1004 | if (symndx == -1U) | |
1005 | return false; | |
1006 | ||
1007 | // There can be another reloc in the FDE, if the CIE specifies an | |
1008 | // LSDA (language specific data area). We currently don't care. We | |
1009 | // will care later if we want to optimize the LSDA from an absolute | |
1010 | // pointer to a PC relative offset when generating a shared library. | |
1011 | relocs->advance(pfdeend - pcontents); | |
1012 | ||
1013 | unsigned int fde_shndx; | |
1014 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
1015 | if (symndx >= symbols_size / sym_size) | |
1016 | return false; | |
1017 | elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size); | |
d491d34e ILT |
1018 | bool is_ordinary; |
1019 | fde_shndx = object->adjust_sym_shndx(symndx, sym.get_st_shndx(), | |
1020 | &is_ordinary); | |
730cdc88 | 1021 | |
d491d34e ILT |
1022 | if (is_ordinary |
1023 | && fde_shndx != elfcpp::SHN_UNDEF | |
730cdc88 ILT |
1024 | && fde_shndx < object->shnum() |
1025 | && !object->is_section_included(fde_shndx)) | |
1026 | { | |
1027 | // This FDE applies to a section which we are discarding. We | |
1028 | // can discard this FDE. | |
1029 | this->merge_map_.add_mapping(object, shndx, (pfde - 8) - pcontents, | |
1030 | pfdeend - (pfde - 8), -1); | |
1031 | return true; | |
1032 | } | |
1033 | ||
1034 | cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents, | |
1035 | pfde, pfdeend - pfde)); | |
1036 | ||
1037 | return true; | |
1038 | } | |
1039 | ||
07a60597 ILT |
1040 | // Add unwind information for a PLT. |
1041 | ||
1042 | void | |
1043 | Eh_frame::add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data, | |
1044 | size_t cie_length, const unsigned char* fde_data, | |
1045 | size_t fde_length) | |
1046 | { | |
1047 | Cie cie(NULL, 0, 0, elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4, "", | |
1048 | cie_data, cie_length); | |
1049 | Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie); | |
1050 | Cie* pcie; | |
1051 | if (find_cie != this->cie_offsets_.end()) | |
1052 | pcie = *find_cie; | |
1053 | else | |
1054 | { | |
9d5781f8 | 1055 | gold_assert(!this->mappings_are_done_); |
07a60597 ILT |
1056 | pcie = new Cie(cie); |
1057 | this->cie_offsets_.insert(pcie); | |
1058 | } | |
1059 | ||
9d5781f8 | 1060 | Fde* fde = new Fde(plt, fde_data, fde_length, this->mappings_are_done_); |
07a60597 | 1061 | pcie->add_fde(fde); |
9d5781f8 AM |
1062 | |
1063 | if (this->mappings_are_done_) | |
1064 | this->final_data_size_ += align_address(fde_length + 8, this->addralign()); | |
07a60597 ILT |
1065 | } |
1066 | ||
730cdc88 ILT |
1067 | // Return the number of FDEs. |
1068 | ||
1069 | unsigned int | |
1070 | Eh_frame::fde_count() const | |
1071 | { | |
1072 | unsigned int ret = 0; | |
1073 | for (Unmergeable_cie_offsets::const_iterator p = | |
1074 | this->unmergeable_cie_offsets_.begin(); | |
1075 | p != this->unmergeable_cie_offsets_.end(); | |
1076 | ++p) | |
1cac254c | 1077 | ret += (*p)->fde_count(); |
730cdc88 ILT |
1078 | for (Cie_offsets::const_iterator p = this->cie_offsets_.begin(); |
1079 | p != this->cie_offsets_.end(); | |
1080 | ++p) | |
1cac254c | 1081 | ret += (*p)->fde_count(); |
730cdc88 ILT |
1082 | return ret; |
1083 | } | |
1084 | ||
1085 | // Set the final data size. | |
1086 | ||
1087 | void | |
27bc2bce | 1088 | Eh_frame::set_final_data_size() |
730cdc88 | 1089 | { |
1d6531cf ILT |
1090 | // We can be called more than once if Layout::set_segment_offsets |
1091 | // finds a better mapping. We don't want to add all the mappings | |
1092 | // again. | |
1093 | if (this->mappings_are_done_) | |
1094 | { | |
1095 | this->set_data_size(this->final_data_size_); | |
1096 | return; | |
1097 | } | |
1098 | ||
9860cbcf CC |
1099 | section_offset_type output_start = 0; |
1100 | if (this->is_offset_valid()) | |
1101 | output_start = this->offset() - this->output_section()->offset(); | |
1102 | section_offset_type output_offset = output_start; | |
730cdc88 ILT |
1103 | |
1104 | for (Unmergeable_cie_offsets::iterator p = | |
1105 | this->unmergeable_cie_offsets_.begin(); | |
1106 | p != this->unmergeable_cie_offsets_.end(); | |
1107 | ++p) | |
2ea97941 ILT |
1108 | output_offset = (*p)->set_output_offset(output_offset, |
1109 | this->addralign(), | |
1110 | &this->merge_map_); | |
730cdc88 ILT |
1111 | |
1112 | for (Cie_offsets::iterator p = this->cie_offsets_.begin(); | |
1113 | p != this->cie_offsets_.end(); | |
1114 | ++p) | |
2ea97941 ILT |
1115 | output_offset = (*p)->set_output_offset(output_offset, |
1116 | this->addralign(), | |
1117 | &this->merge_map_); | |
730cdc88 | 1118 | |
1d6531cf | 1119 | this->mappings_are_done_ = true; |
9860cbcf | 1120 | this->final_data_size_ = output_offset - output_start; |
1d6531cf | 1121 | |
2ea97941 | 1122 | gold_assert((output_offset & (this->addralign() - 1)) == 0); |
9860cbcf | 1123 | this->set_data_size(this->final_data_size_); |
730cdc88 ILT |
1124 | } |
1125 | ||
1126 | // Return an output offset for an input offset. | |
1127 | ||
1128 | bool | |
1129 | Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx, | |
2ea97941 | 1130 | section_offset_type offset, |
8383303e | 1131 | section_offset_type* poutput) const |
730cdc88 | 1132 | { |
2ea97941 | 1133 | return this->merge_map_.get_output_offset(object, shndx, offset, poutput); |
730cdc88 ILT |
1134 | } |
1135 | ||
a9a60db6 ILT |
1136 | // Return whether this is the merge section for an input section. |
1137 | ||
1138 | bool | |
1139 | Eh_frame::do_is_merge_section_for(const Relobj* object, | |
1140 | unsigned int shndx) const | |
1141 | { | |
1142 | return this->merge_map_.is_merge_section_for(object, shndx); | |
1143 | } | |
1144 | ||
730cdc88 ILT |
1145 | // Write the data to the output file. |
1146 | ||
1147 | void | |
1148 | Eh_frame::do_write(Output_file* of) | |
1149 | { | |
2ea97941 | 1150 | const off_t offset = this->offset(); |
730cdc88 | 1151 | const off_t oview_size = this->data_size(); |
2ea97941 | 1152 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
730cdc88 | 1153 | |
8851ecca | 1154 | switch (parameters->size_and_endianness()) |
730cdc88 | 1155 | { |
730cdc88 | 1156 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
1157 | case Parameters::TARGET_32_LITTLE: |
1158 | this->do_sized_write<32, false>(oview); | |
1159 | break; | |
730cdc88 | 1160 | #endif |
730cdc88 | 1161 | #ifdef HAVE_TARGET_32_BIG |
8851ecca ILT |
1162 | case Parameters::TARGET_32_BIG: |
1163 | this->do_sized_write<32, true>(oview); | |
1164 | break; | |
730cdc88 | 1165 | #endif |
730cdc88 | 1166 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
1167 | case Parameters::TARGET_64_LITTLE: |
1168 | this->do_sized_write<64, false>(oview); | |
1169 | break; | |
730cdc88 | 1170 | #endif |
730cdc88 | 1171 | #ifdef HAVE_TARGET_64_BIG |
8851ecca ILT |
1172 | case Parameters::TARGET_64_BIG: |
1173 | this->do_sized_write<64, true>(oview); | |
1174 | break; | |
730cdc88 | 1175 | #endif |
8851ecca ILT |
1176 | default: |
1177 | gold_unreachable(); | |
730cdc88 | 1178 | } |
730cdc88 | 1179 | |
2ea97941 | 1180 | of->write_output_view(offset, oview_size, oview); |
730cdc88 ILT |
1181 | } |
1182 | ||
1183 | // Write the data to the output file--template version. | |
1184 | ||
1185 | template<int size, bool big_endian> | |
1186 | void | |
1187 | Eh_frame::do_sized_write(unsigned char* oview) | |
1188 | { | |
07a60597 | 1189 | uint64_t address = this->address(); |
2ea97941 | 1190 | unsigned int addralign = this->addralign(); |
8383303e | 1191 | section_offset_type o = 0; |
9860cbcf | 1192 | const off_t output_offset = this->offset() - this->output_section()->offset(); |
9d5781f8 | 1193 | Post_fdes post_fdes; |
730cdc88 ILT |
1194 | for (Unmergeable_cie_offsets::iterator p = |
1195 | this->unmergeable_cie_offsets_.begin(); | |
1196 | p != this->unmergeable_cie_offsets_.end(); | |
1197 | ++p) | |
9860cbcf CC |
1198 | o = (*p)->write<size, big_endian>(oview, output_offset, o, address, |
1199 | addralign, this->eh_frame_hdr_, | |
1200 | &post_fdes); | |
730cdc88 ILT |
1201 | for (Cie_offsets::iterator p = this->cie_offsets_.begin(); |
1202 | p != this->cie_offsets_.end(); | |
1203 | ++p) | |
9860cbcf CC |
1204 | o = (*p)->write<size, big_endian>(oview, output_offset, o, address, |
1205 | addralign, this->eh_frame_hdr_, | |
1206 | &post_fdes); | |
9d5781f8 AM |
1207 | for (Post_fdes::iterator p = post_fdes.begin(); |
1208 | p != post_fdes.end(); | |
1209 | ++p) | |
9860cbcf CC |
1210 | o = (*p).fde->write<size, big_endian>(oview, output_offset, o, address, |
1211 | addralign, (*p).cie_offset, | |
4bead2d5 AM |
1212 | (*p).fde_encoding, |
1213 | this->eh_frame_hdr_); | |
730cdc88 ILT |
1214 | } |
1215 | ||
1216 | #ifdef HAVE_TARGET_32_LITTLE | |
1217 | template | |
1218 | bool | |
1219 | Eh_frame::add_ehframe_input_section<32, false>( | |
6fa2a40b | 1220 | Sized_relobj_file<32, false>* object, |
730cdc88 | 1221 | const unsigned char* symbols, |
8383303e | 1222 | section_size_type symbols_size, |
730cdc88 | 1223 | const unsigned char* symbol_names, |
8383303e | 1224 | section_size_type symbol_names_size, |
730cdc88 ILT |
1225 | unsigned int shndx, |
1226 | unsigned int reloc_shndx, | |
1227 | unsigned int reloc_type); | |
1228 | #endif | |
1229 | ||
1230 | #ifdef HAVE_TARGET_32_BIG | |
1231 | template | |
1232 | bool | |
1233 | Eh_frame::add_ehframe_input_section<32, true>( | |
6fa2a40b | 1234 | Sized_relobj_file<32, true>* object, |
730cdc88 | 1235 | const unsigned char* symbols, |
8383303e | 1236 | section_size_type symbols_size, |
730cdc88 | 1237 | const unsigned char* symbol_names, |
8383303e | 1238 | section_size_type symbol_names_size, |
730cdc88 ILT |
1239 | unsigned int shndx, |
1240 | unsigned int reloc_shndx, | |
1241 | unsigned int reloc_type); | |
1242 | #endif | |
1243 | ||
1244 | #ifdef HAVE_TARGET_64_LITTLE | |
1245 | template | |
1246 | bool | |
1247 | Eh_frame::add_ehframe_input_section<64, false>( | |
6fa2a40b | 1248 | Sized_relobj_file<64, false>* object, |
730cdc88 | 1249 | const unsigned char* symbols, |
8383303e | 1250 | section_size_type symbols_size, |
730cdc88 | 1251 | const unsigned char* symbol_names, |
8383303e | 1252 | section_size_type symbol_names_size, |
730cdc88 ILT |
1253 | unsigned int shndx, |
1254 | unsigned int reloc_shndx, | |
1255 | unsigned int reloc_type); | |
1256 | #endif | |
1257 | ||
1258 | #ifdef HAVE_TARGET_64_BIG | |
1259 | template | |
1260 | bool | |
1261 | Eh_frame::add_ehframe_input_section<64, true>( | |
6fa2a40b | 1262 | Sized_relobj_file<64, true>* object, |
730cdc88 | 1263 | const unsigned char* symbols, |
8383303e | 1264 | section_size_type symbols_size, |
730cdc88 | 1265 | const unsigned char* symbol_names, |
8383303e | 1266 | section_size_type symbol_names_size, |
730cdc88 ILT |
1267 | unsigned int shndx, |
1268 | unsigned int reloc_shndx, | |
1269 | unsigned int reloc_type); | |
1270 | #endif | |
1271 | ||
3151305a | 1272 | } // End namespace gold. |