Rename 32- and 64-bit Intel files from "i386" to "x86"
[deliverable/binutils-gdb.git] / gold / ehframe.cc
CommitLineData
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
34namespace 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
77const int eh_frame_hdr_size = 4;
78
79// Construct the exception frame header.
80
730cdc88
ILT
81Eh_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
93void
27bc2bce 94Eh_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
109void
110Eh_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
141template<int size, bool big_endian>
142void
143Eh_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
220template<int size, bool big_endian>
221typename elfcpp::Elf_types<size>::Elf_Addr
4117d768
ILT
222Eh_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
299template<int size, bool big_endian>
300void
301Eh_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
329// offset of the CIE in OVIEW. FDE_ENCODING is the encoding, from the
07a60597
ILT
330// CIE. ADDRALIGN is the required alignment. ADDRESS is the virtual
331// address of OVIEW. Record the FDE pc for EH_FRAME_HDR. Return the
332// new offset.
730cdc88
ILT
333
334template<int size, bool big_endian>
8383303e
ILT
335section_offset_type
336Fde::write(unsigned char* oview, section_offset_type offset,
07a60597
ILT
337 uint64_t address, unsigned int addralign,
338 section_offset_type cie_offset, unsigned char fde_encoding,
339 Eh_frame_hdr* eh_frame_hdr)
730cdc88 340{
935e8877
ILT
341 gold_assert((offset & (addralign - 1)) == 0);
342
2ea97941 343 size_t length = this->contents_.length();
730cdc88 344
935e8877
ILT
345 // We add 8 when getting the aligned length to account for the
346 // length word and the CIE offset.
2ea97941 347 size_t aligned_full_length = align_address(length + 8, addralign);
935e8877 348
730cdc88
ILT
349 // Write the length of the FDE as a 32-bit word. The length word
350 // does not include the four bytes of the length word itself, but it
351 // does include the offset to the CIE.
352 elfcpp::Swap<32, big_endian>::writeval(oview + offset,
935e8877 353 aligned_full_length - 4);
730cdc88
ILT
354
355 // Write the offset to the CIE as a 32-bit word. This is the
356 // difference between the address of the offset word itself and the
357 // CIE address.
358 elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4,
359 offset + 4 - cie_offset);
360
361 // Copy the rest of the FDE. Note that this is run before
362 // relocation processing is done on this section, so the relocations
363 // will later be applied to the FDE data.
2ea97941 364 memcpy(oview + offset + 8, this->contents_.data(), length);
730cdc88 365
07a60597
ILT
366 // If this FDE is associated with a PLT, fill in the PLT's address
367 // and size.
368 if (this->object_ == NULL)
369 {
370 gold_assert(memcmp(oview + offset + 8, "\0\0\0\0\0\0\0\0", 8) == 0);
9d5781f8
AM
371 uint64_t paddress;
372 off_t psize;
373 parameters->target().plt_fde_location(this->u_.from_linker.plt,
374 oview + offset + 8,
375 &paddress, &psize);
376 uint64_t poffset = paddress - (address + offset + 8);
07a60597 377 int32_t spoffset = static_cast<int32_t>(poffset);
07a60597
ILT
378 uint32_t upsize = static_cast<uint32_t>(psize);
379 if (static_cast<uint64_t>(static_cast<int64_t>(spoffset)) != poffset
380 || static_cast<off_t>(upsize) != psize)
381 gold_warning(_("overflow in PLT unwind data; "
382 "unwinding through PLT may fail"));
383 elfcpp::Swap<32, big_endian>::writeval(oview + offset + 8, spoffset);
384 elfcpp::Swap<32, big_endian>::writeval(oview + offset + 12, upsize);
385 }
386
2ea97941
ILT
387 if (aligned_full_length > length + 8)
388 memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
935e8877 389
730cdc88
ILT
390 // Tell the exception frame header about this FDE.
391 if (eh_frame_hdr != NULL)
392 eh_frame_hdr->record_fde(offset, fde_encoding);
393
935e8877 394 return offset + aligned_full_length;
730cdc88
ILT
395}
396
397// Class Cie.
398
399// Destructor.
400
401Cie::~Cie()
402{
403 for (std::vector<Fde*>::iterator p = this->fdes_.begin();
404 p != this->fdes_.end();
405 ++p)
406 delete *p;
407}
408
409// Set the output offset of a CIE. Return the new output offset.
410
8383303e
ILT
411section_offset_type
412Cie::set_output_offset(section_offset_type output_offset,
413 unsigned int addralign,
730cdc88
ILT
414 Merge_map* merge_map)
415{
416 size_t length = this->contents_.length();
935e8877 417
730cdc88
ILT
418 // Add 4 for length and 4 for zero CIE identifier tag.
419 length += 8;
420
07a60597
ILT
421 if (this->object_ != NULL)
422 {
423 // Add a mapping so that relocations are applied correctly.
424 merge_map->add_mapping(this->object_, this->shndx_, this->input_offset_,
425 length, output_offset);
426 }
730cdc88 427
935e8877
ILT
428 length = align_address(length, addralign);
429
730cdc88
ILT
430 for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
431 p != this->fdes_.end();
432 ++p)
433 {
434 (*p)->add_mapping(output_offset + length, merge_map);
435
436 size_t fde_length = (*p)->length();
935e8877 437 fde_length = align_address(fde_length, addralign);
730cdc88
ILT
438 length += fde_length;
439 }
440
441 return output_offset + length;
442}
443
9d5781f8
AM
444// Write the CIE to OVIEW starting at OFFSET. Round up the bytes to
445// ADDRALIGN. ADDRESS is the virtual address of OVIEW.
446// EH_FRAME_HDR is the exception frame header for FDE recording.
447// POST_FDES stashes FDEs created after mappings were done, for later
448// writing. Return the new offset.
730cdc88
ILT
449
450template<int size, bool big_endian>
8383303e
ILT
451section_offset_type
452Cie::write(unsigned char* oview, section_offset_type offset,
07a60597 453 uint64_t address, unsigned int addralign,
9d5781f8 454 Eh_frame_hdr* eh_frame_hdr, Post_fdes* post_fdes)
730cdc88 455{
935e8877
ILT
456 gold_assert((offset & (addralign - 1)) == 0);
457
8383303e 458 section_offset_type cie_offset = offset;
730cdc88
ILT
459
460 size_t length = this->contents_.length();
461
935e8877
ILT
462 // We add 8 when getting the aligned length to account for the
463 // length word and the CIE tag.
464 size_t aligned_full_length = align_address(length + 8, addralign);
465
730cdc88
ILT
466 // Write the length of the CIE as a 32-bit word. The length word
467 // does not include the four bytes of the length word itself.
935e8877
ILT
468 elfcpp::Swap<32, big_endian>::writeval(oview + offset,
469 aligned_full_length - 4);
730cdc88
ILT
470
471 // Write the tag which marks this as a CIE: a 32-bit zero.
472 elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, 0);
473
474 // Write out the CIE data.
475 memcpy(oview + offset + 8, this->contents_.data(), length);
935e8877
ILT
476
477 if (aligned_full_length > length + 8)
478 memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
479
480 offset += aligned_full_length;
730cdc88
ILT
481
482 // Write out the associated FDEs.
483 unsigned char fde_encoding = this->fde_encoding_;
484 for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
485 p != this->fdes_.end();
486 ++p)
9d5781f8
AM
487 {
488 if ((*p)->post_map())
4bead2d5 489 post_fdes->push_back(Post_fde(*p, cie_offset, fde_encoding));
9d5781f8
AM
490 else
491 offset = (*p)->write<size, big_endian>(oview, offset, address,
492 addralign, cie_offset,
493 fde_encoding, eh_frame_hdr);
494 }
730cdc88
ILT
495
496 return offset;
497}
498
499// We track all the CIEs we see, and merge them when possible. This
500// works because each FDE holds an offset to the relevant CIE: we
501// rewrite the FDEs to point to the merged CIE. This is worthwhile
502// because in a typical C++ program many FDEs in many different object
503// files will use the same CIE.
504
505// An equality operator for Cie.
506
507bool
508operator==(const Cie& cie1, const Cie& cie2)
509{
510 return (cie1.personality_name_ == cie2.personality_name_
511 && cie1.contents_ == cie2.contents_);
512}
513
514// A less-than operator for Cie.
515
516bool
517operator<(const Cie& cie1, const Cie& cie2)
518{
519 if (cie1.personality_name_ != cie2.personality_name_)
520 return cie1.personality_name_ < cie2.personality_name_;
521 return cie1.contents_ < cie2.contents_;
522}
523
524// Class Eh_frame.
525
526Eh_frame::Eh_frame()
527 : Output_section_data(Output_data::default_alignment()),
528 eh_frame_hdr_(NULL),
529 cie_offsets_(),
530 unmergeable_cie_offsets_(),
1d6531cf
ILT
531 merge_map_(),
532 mappings_are_done_(false),
533 final_data_size_(0)
730cdc88
ILT
534{
535}
536
537// Skip an LEB128, updating *PP to point to the next character.
538// Return false if we ran off the end of the string.
539
540bool
541Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend)
542{
543 const unsigned char* p;
544 for (p = *pp; p < pend; ++p)
545 {
546 if ((*p & 0x80) == 0)
547 {
548 *pp = p + 1;
549 return true;
550 }
551 }
552 return false;
553}
554
555// Add input section SHNDX in OBJECT to an exception frame section.
556// SYMBOLS is the contents of the symbol table section (size
557// SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
558// SYMBOL_NAMES_SIZE). RELOC_SHNDX is the index of a relocation
559// section applying to SHNDX, or 0 if none, or -1U if more than one.
560// RELOC_TYPE is the type of the reloc section if there is one, either
561// SHT_REL or SHT_RELA. We try to parse the input exception frame
562// data into our data structures. If we can't do it, we return false
563// to mean that the section should be handled as a normal input
564// section.
565
566template<int size, bool big_endian>
567bool
568Eh_frame::add_ehframe_input_section(
6fa2a40b 569 Sized_relobj_file<size, big_endian>* object,
730cdc88 570 const unsigned char* symbols,
8383303e 571 section_size_type symbols_size,
730cdc88 572 const unsigned char* symbol_names,
8383303e 573 section_size_type symbol_names_size,
730cdc88
ILT
574 unsigned int shndx,
575 unsigned int reloc_shndx,
576 unsigned int reloc_type)
577{
578 // Get the section contents.
8383303e 579 section_size_type contents_len;
730cdc88
ILT
580 const unsigned char* pcontents = object->section_contents(shndx,
581 &contents_len,
582 false);
583 if (contents_len == 0)
584 return false;
585
586 // If this is the marker section for the end of the data, then
587 // return false to force it to be handled as an ordinary input
588 // section. If we don't do this, we won't correctly handle the case
589 // of unrecognized .eh_frame sections.
590 if (contents_len == 4
591 && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0)
592 return false;
593
594 New_cies new_cies;
595 if (!this->do_add_ehframe_input_section(object, symbols, symbols_size,
596 symbol_names, symbol_names_size,
597 shndx, reloc_shndx,
598 reloc_type, pcontents,
599 contents_len, &new_cies))
600 {
5edd166e
ILT
601 if (this->eh_frame_hdr_ != NULL)
602 this->eh_frame_hdr_->found_unrecognized_eh_frame_section();
730cdc88
ILT
603
604 for (New_cies::iterator p = new_cies.begin();
605 p != new_cies.end();
606 ++p)
607 delete p->first;
608
609 return false;
610 }
611
612 // Now that we know we are using this section, record any new CIEs
613 // that we found.
614 for (New_cies::const_iterator p = new_cies.begin();
615 p != new_cies.end();
616 ++p)
617 {
730cdc88 618 if (p->second)
1cac254c 619 this->cie_offsets_.insert(p->first);
730cdc88 620 else
1cac254c 621 this->unmergeable_cie_offsets_.push_back(p->first);
730cdc88
ILT
622 }
623
624 return true;
625}
626
627// The bulk of the implementation of add_ehframe_input_section.
628
629template<int size, bool big_endian>
630bool
631Eh_frame::do_add_ehframe_input_section(
6fa2a40b 632 Sized_relobj_file<size, big_endian>* object,
730cdc88 633 const unsigned char* symbols,
8383303e 634 section_size_type symbols_size,
730cdc88 635 const unsigned char* symbol_names,
8383303e 636 section_size_type symbol_names_size,
730cdc88
ILT
637 unsigned int shndx,
638 unsigned int reloc_shndx,
639 unsigned int reloc_type,
640 const unsigned char* pcontents,
8383303e 641 section_size_type contents_len,
730cdc88
ILT
642 New_cies* new_cies)
643{
730cdc88
ILT
644 Track_relocs<size, big_endian> relocs;
645
646 const unsigned char* p = pcontents;
647 const unsigned char* pend = p + contents_len;
648
649 // Get the contents of the reloc section if any.
650 if (!relocs.initialize(object, reloc_shndx, reloc_type))
651 return false;
652
653 // Keep track of which CIEs are at which offsets.
654 Offsets_to_cie cies;
655
656 while (p < pend)
657 {
658 if (pend - p < 4)
659 return false;
660
661 // There shouldn't be any relocations here.
662 if (relocs.advance(p + 4 - pcontents) > 0)
663 return false;
664
665 unsigned int len = elfcpp::Swap<32, big_endian>::readval(p);
666 p += 4;
667 if (len == 0)
668 {
669 // We should only find a zero-length entry at the end of the
670 // section.
671 if (p < pend)
672 return false;
673 break;
674 }
675 // We don't support a 64-bit .eh_frame.
676 if (len == 0xffffffff)
677 return false;
678 if (static_cast<unsigned int>(pend - p) < len)
679 return false;
680
681 const unsigned char* const pentend = p + len;
682
683 if (pend - p < 4)
684 return false;
685 if (relocs.advance(p + 4 - pcontents) > 0)
686 return false;
687
688 unsigned int id = elfcpp::Swap<32, big_endian>::readval(p);
689 p += 4;
690
691 if (id == 0)
692 {
693 // CIE.
694 if (!this->read_cie(object, shndx, symbols, symbols_size,
695 symbol_names, symbol_names_size,
696 pcontents, p, pentend, &relocs, &cies,
697 new_cies))
698 return false;
699 }
700 else
701 {
702 // FDE.
703 if (!this->read_fde(object, shndx, symbols, symbols_size,
704 pcontents, id, p, pentend, &relocs, &cies))
705 return false;
706 }
707
708 p = pentend;
709 }
710
711 return true;
712}
713
714// Read a CIE. Return false if we can't parse the information.
715
716template<int size, bool big_endian>
717bool
6fa2a40b 718Eh_frame::read_cie(Sized_relobj_file<size, big_endian>* object,
730cdc88
ILT
719 unsigned int shndx,
720 const unsigned char* symbols,
8383303e 721 section_size_type symbols_size,
730cdc88 722 const unsigned char* symbol_names,
8383303e 723 section_size_type symbol_names_size,
730cdc88
ILT
724 const unsigned char* pcontents,
725 const unsigned char* pcie,
ca09d69a 726 const unsigned char* pcieend,
730cdc88
ILT
727 Track_relocs<size, big_endian>* relocs,
728 Offsets_to_cie* cies,
729 New_cies* new_cies)
730{
731 bool mergeable = true;
732
733 // We need to find the personality routine if there is one, since we
734 // can only merge CIEs which use the same routine. We also need to
735 // find the FDE encoding if there is one, so that we can read the PC
736 // from the FDE.
737
738 const unsigned char* p = pcie;
739
740 if (pcieend - p < 1)
741 return false;
742 unsigned char version = *p++;
743 if (version != 1 && version != 3)
744 return false;
745
746 const unsigned char* paug = p;
747 const void* paugendv = memchr(p, '\0', pcieend - p);
748 const unsigned char* paugend = static_cast<const unsigned char*>(paugendv);
749 if (paugend == NULL)
750 return false;
751 p = paugend + 1;
752
753 if (paug[0] == 'e' && paug[1] == 'h')
754 {
755 // This is a CIE from gcc before version 3.0. We can't merge
756 // these. We can still read the FDEs.
757 mergeable = false;
758 paug += 2;
759 if (*paug != '\0')
760 return false;
761 if (pcieend - p < size / 8)
762 return false;
763 p += size / 8;
764 }
765
766 // Skip the code alignment.
767 if (!skip_leb128(&p, pcieend))
768 return false;
769
770 // Skip the data alignment.
771 if (!skip_leb128(&p, pcieend))
772 return false;
773
774 // Skip the return column.
775 if (version == 1)
776 {
777 if (pcieend - p < 1)
778 return false;
779 ++p;
780 }
781 else
782 {
783 if (!skip_leb128(&p, pcieend))
784 return false;
785 }
786
787 if (*paug == 'z')
788 {
789 ++paug;
790 // Skip the augmentation size.
791 if (!skip_leb128(&p, pcieend))
792 return false;
793 }
794
795 unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr;
796 int per_offset = -1;
797 while (*paug != '\0')
798 {
799 switch (*paug)
800 {
801 case 'L': // LSDA encoding.
802 if (pcieend - p < 1)
803 return false;
804 ++p;
805 break;
806
807 case 'R': // FDE encoding.
808 if (pcieend - p < 1)
809 return false;
810 fde_encoding = *p;
811 switch (fde_encoding & 7)
812 {
813 case elfcpp::DW_EH_PE_absptr:
814 case elfcpp::DW_EH_PE_udata2:
815 case elfcpp::DW_EH_PE_udata4:
816 case elfcpp::DW_EH_PE_udata8:
817 break;
818 default:
4117d768
ILT
819 // We don't expect to see any other cases here, and
820 // we're not prepared to handle them.
730cdc88
ILT
821 return false;
822 }
823 ++p;
824 break;
825
826 case 'S':
827 break;
828
829 case 'P':
830 // Personality encoding.
831 {
832 if (pcieend - p < 1)
833 return false;
834 unsigned char per_encoding = *p;
835 ++p;
836
837 if ((per_encoding & 0x60) == 0x60)
838 return false;
839 unsigned int per_width;
840 switch (per_encoding & 7)
841 {
842 case elfcpp::DW_EH_PE_udata2:
843 per_width = 2;
844 break;
845 case elfcpp::DW_EH_PE_udata4:
846 per_width = 4;
847 break;
848 case elfcpp::DW_EH_PE_udata8:
849 per_width = 8;
850 break;
851 case elfcpp::DW_EH_PE_absptr:
852 per_width = size / 8;
853 break;
854 default:
855 return false;
856 }
857
858 if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned)
859 {
860 unsigned int len = p - pcie;
861 len += per_width - 1;
862 len &= ~ (per_width - 1);
863 if (static_cast<unsigned int>(pcieend - p) < len)
864 return false;
865 p += len;
866 }
867
868 per_offset = p - pcontents;
869
870 if (static_cast<unsigned int>(pcieend - p) < per_width)
871 return false;
872 p += per_width;
873 }
874 break;
875
876 default:
877 return false;
878 }
879
880 ++paug;
881 }
882
883 const char* personality_name = "";
884 if (per_offset != -1)
885 {
886 if (relocs->advance(per_offset) > 0)
887 return false;
888 if (relocs->next_offset() != per_offset)
889 return false;
890
891 unsigned int personality_symndx = relocs->next_symndx();
892 if (personality_symndx == -1U)
893 return false;
894
895 if (personality_symndx < object->local_symbol_count())
896 {
897 // We can only merge this CIE if the personality routine is
898 // a global symbol. We can still read the FDEs.
899 mergeable = false;
900 }
901 else
902 {
903 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
904 if (personality_symndx >= symbols_size / sym_size)
905 return false;
906 elfcpp::Sym<size, big_endian> sym(symbols
907 + (personality_symndx * sym_size));
908 unsigned int name_offset = sym.get_st_name();
909 if (name_offset >= symbol_names_size)
910 return false;
911 personality_name = (reinterpret_cast<const char*>(symbol_names)
912 + name_offset);
913 }
914
915 int r = relocs->advance(per_offset + 1);
916 gold_assert(r == 1);
917 }
918
919 if (relocs->advance(pcieend - pcontents) > 0)
920 return false;
921
922 Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding,
923 personality_name, pcie, pcieend - pcie);
924 Cie* cie_pointer = NULL;
925 if (mergeable)
926 {
927 Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
928 if (find_cie != this->cie_offsets_.end())
1cac254c 929 cie_pointer = *find_cie;
730cdc88
ILT
930 else
931 {
932 // See if we already saw this CIE in this object file.
933 for (New_cies::const_iterator pc = new_cies->begin();
934 pc != new_cies->end();
935 ++pc)
936 {
937 if (*(pc->first) == cie)
938 {
939 cie_pointer = pc->first;
940 break;
941 }
942 }
943 }
944 }
945
946 if (cie_pointer == NULL)
947 {
948 cie_pointer = new Cie(cie);
949 new_cies->push_back(std::make_pair(cie_pointer, mergeable));
950 }
951 else
952 {
953 // We are deleting this CIE. Record that in our mapping from
954 // input sections to the output section. At this point we don't
955 // know for sure that we are doing a special mapping for this
956 // input section, but that's OK--if we don't do a special
957 // mapping, nobody will ever ask for the mapping we add here.
958 this->merge_map_.add_mapping(object, shndx, (pcie - 8) - pcontents,
959 pcieend - (pcie - 8), -1);
960 }
961
962 // Record this CIE plus the offset in the input section.
963 cies->insert(std::make_pair(pcie - pcontents, cie_pointer));
964
965 return true;
966}
967
968// Read an FDE. Return false if we can't parse the information.
969
970template<int size, bool big_endian>
971bool
6fa2a40b 972Eh_frame::read_fde(Sized_relobj_file<size, big_endian>* object,
730cdc88
ILT
973 unsigned int shndx,
974 const unsigned char* symbols,
8383303e 975 section_size_type symbols_size,
730cdc88 976 const unsigned char* pcontents,
2ea97941 977 unsigned int offset,
730cdc88 978 const unsigned char* pfde,
ca09d69a 979 const unsigned char* pfdeend,
730cdc88
ILT
980 Track_relocs<size, big_endian>* relocs,
981 Offsets_to_cie* cies)
982{
2ea97941 983 // OFFSET is the distance between the 4 bytes before PFDE to the
730cdc88
ILT
984 // start of the CIE. The offset we recorded for the CIE is 8 bytes
985 // after the start of the CIE--after the length and the zero tag.
2ea97941 986 unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8;
730cdc88
ILT
987 Offsets_to_cie::const_iterator pcie = cies->find(cie_offset);
988 if (pcie == cies->end())
989 return false;
990 Cie* cie = pcie->second;
991
992 // The FDE should start with a reloc to the start of the code which
993 // it describes.
994 if (relocs->advance(pfde - pcontents) > 0)
995 return false;
996
997 if (relocs->next_offset() != pfde - pcontents)
998 return false;
999
1000 unsigned int symndx = relocs->next_symndx();
1001 if (symndx == -1U)
1002 return false;
1003
1004 // There can be another reloc in the FDE, if the CIE specifies an
1005 // LSDA (language specific data area). We currently don't care. We
1006 // will care later if we want to optimize the LSDA from an absolute
1007 // pointer to a PC relative offset when generating a shared library.
1008 relocs->advance(pfdeend - pcontents);
1009
1010 unsigned int fde_shndx;
1011 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1012 if (symndx >= symbols_size / sym_size)
1013 return false;
1014 elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size);
d491d34e
ILT
1015 bool is_ordinary;
1016 fde_shndx = object->adjust_sym_shndx(symndx, sym.get_st_shndx(),
1017 &is_ordinary);
730cdc88 1018
d491d34e
ILT
1019 if (is_ordinary
1020 && fde_shndx != elfcpp::SHN_UNDEF
730cdc88
ILT
1021 && fde_shndx < object->shnum()
1022 && !object->is_section_included(fde_shndx))
1023 {
1024 // This FDE applies to a section which we are discarding. We
1025 // can discard this FDE.
1026 this->merge_map_.add_mapping(object, shndx, (pfde - 8) - pcontents,
1027 pfdeend - (pfde - 8), -1);
1028 return true;
1029 }
1030
1031 cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents,
1032 pfde, pfdeend - pfde));
1033
1034 return true;
1035}
1036
07a60597
ILT
1037// Add unwind information for a PLT.
1038
1039void
1040Eh_frame::add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data,
1041 size_t cie_length, const unsigned char* fde_data,
1042 size_t fde_length)
1043{
1044 Cie cie(NULL, 0, 0, elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4, "",
1045 cie_data, cie_length);
1046 Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
1047 Cie* pcie;
1048 if (find_cie != this->cie_offsets_.end())
1049 pcie = *find_cie;
1050 else
1051 {
9d5781f8 1052 gold_assert(!this->mappings_are_done_);
07a60597
ILT
1053 pcie = new Cie(cie);
1054 this->cie_offsets_.insert(pcie);
1055 }
1056
9d5781f8 1057 Fde* fde = new Fde(plt, fde_data, fde_length, this->mappings_are_done_);
07a60597 1058 pcie->add_fde(fde);
9d5781f8
AM
1059
1060 if (this->mappings_are_done_)
1061 this->final_data_size_ += align_address(fde_length + 8, this->addralign());
07a60597
ILT
1062}
1063
730cdc88
ILT
1064// Return the number of FDEs.
1065
1066unsigned int
1067Eh_frame::fde_count() const
1068{
1069 unsigned int ret = 0;
1070 for (Unmergeable_cie_offsets::const_iterator p =
1071 this->unmergeable_cie_offsets_.begin();
1072 p != this->unmergeable_cie_offsets_.end();
1073 ++p)
1cac254c 1074 ret += (*p)->fde_count();
730cdc88
ILT
1075 for (Cie_offsets::const_iterator p = this->cie_offsets_.begin();
1076 p != this->cie_offsets_.end();
1077 ++p)
1cac254c 1078 ret += (*p)->fde_count();
730cdc88
ILT
1079 return ret;
1080}
1081
1082// Set the final data size.
1083
1084void
27bc2bce 1085Eh_frame::set_final_data_size()
730cdc88 1086{
1d6531cf
ILT
1087 // We can be called more than once if Layout::set_segment_offsets
1088 // finds a better mapping. We don't want to add all the mappings
1089 // again.
1090 if (this->mappings_are_done_)
1091 {
1092 this->set_data_size(this->final_data_size_);
1093 return;
1094 }
1095
2ea97941 1096 section_offset_type output_offset = 0;
730cdc88
ILT
1097
1098 for (Unmergeable_cie_offsets::iterator p =
1099 this->unmergeable_cie_offsets_.begin();
1100 p != this->unmergeable_cie_offsets_.end();
1101 ++p)
2ea97941
ILT
1102 output_offset = (*p)->set_output_offset(output_offset,
1103 this->addralign(),
1104 &this->merge_map_);
730cdc88
ILT
1105
1106 for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1107 p != this->cie_offsets_.end();
1108 ++p)
2ea97941
ILT
1109 output_offset = (*p)->set_output_offset(output_offset,
1110 this->addralign(),
1111 &this->merge_map_);
730cdc88 1112
1d6531cf 1113 this->mappings_are_done_ = true;
2ea97941 1114 this->final_data_size_ = output_offset;
1d6531cf 1115
2ea97941
ILT
1116 gold_assert((output_offset & (this->addralign() - 1)) == 0);
1117 this->set_data_size(output_offset);
730cdc88
ILT
1118}
1119
1120// Return an output offset for an input offset.
1121
1122bool
1123Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx,
2ea97941 1124 section_offset_type offset,
8383303e 1125 section_offset_type* poutput) const
730cdc88 1126{
2ea97941 1127 return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
730cdc88
ILT
1128}
1129
a9a60db6
ILT
1130// Return whether this is the merge section for an input section.
1131
1132bool
1133Eh_frame::do_is_merge_section_for(const Relobj* object,
1134 unsigned int shndx) const
1135{
1136 return this->merge_map_.is_merge_section_for(object, shndx);
1137}
1138
730cdc88
ILT
1139// Write the data to the output file.
1140
1141void
1142Eh_frame::do_write(Output_file* of)
1143{
2ea97941 1144 const off_t offset = this->offset();
730cdc88 1145 const off_t oview_size = this->data_size();
2ea97941 1146 unsigned char* const oview = of->get_output_view(offset, oview_size);
730cdc88 1147
8851ecca 1148 switch (parameters->size_and_endianness())
730cdc88 1149 {
730cdc88 1150#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1151 case Parameters::TARGET_32_LITTLE:
1152 this->do_sized_write<32, false>(oview);
1153 break;
730cdc88 1154#endif
730cdc88 1155#ifdef HAVE_TARGET_32_BIG
8851ecca
ILT
1156 case Parameters::TARGET_32_BIG:
1157 this->do_sized_write<32, true>(oview);
1158 break;
730cdc88 1159#endif
730cdc88 1160#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1161 case Parameters::TARGET_64_LITTLE:
1162 this->do_sized_write<64, false>(oview);
1163 break;
730cdc88 1164#endif
730cdc88 1165#ifdef HAVE_TARGET_64_BIG
8851ecca
ILT
1166 case Parameters::TARGET_64_BIG:
1167 this->do_sized_write<64, true>(oview);
1168 break;
730cdc88 1169#endif
8851ecca
ILT
1170 default:
1171 gold_unreachable();
730cdc88 1172 }
730cdc88 1173
2ea97941 1174 of->write_output_view(offset, oview_size, oview);
730cdc88
ILT
1175}
1176
1177// Write the data to the output file--template version.
1178
1179template<int size, bool big_endian>
1180void
1181Eh_frame::do_sized_write(unsigned char* oview)
1182{
07a60597 1183 uint64_t address = this->address();
2ea97941 1184 unsigned int addralign = this->addralign();
8383303e 1185 section_offset_type o = 0;
9d5781f8 1186 Post_fdes post_fdes;
730cdc88
ILT
1187 for (Unmergeable_cie_offsets::iterator p =
1188 this->unmergeable_cie_offsets_.begin();
1189 p != this->unmergeable_cie_offsets_.end();
1190 ++p)
07a60597 1191 o = (*p)->write<size, big_endian>(oview, o, address, addralign,
9d5781f8 1192 this->eh_frame_hdr_, &post_fdes);
730cdc88
ILT
1193 for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1194 p != this->cie_offsets_.end();
1195 ++p)
07a60597 1196 o = (*p)->write<size, big_endian>(oview, o, address, addralign,
9d5781f8
AM
1197 this->eh_frame_hdr_, &post_fdes);
1198 for (Post_fdes::iterator p = post_fdes.begin();
1199 p != post_fdes.end();
1200 ++p)
4bead2d5
AM
1201 o = (*p).fde->write<size, big_endian>(oview, o, address, addralign,
1202 (*p).cie_offset,
1203 (*p).fde_encoding,
1204 this->eh_frame_hdr_);
730cdc88
ILT
1205}
1206
1207#ifdef HAVE_TARGET_32_LITTLE
1208template
1209bool
1210Eh_frame::add_ehframe_input_section<32, false>(
6fa2a40b 1211 Sized_relobj_file<32, false>* object,
730cdc88 1212 const unsigned char* symbols,
8383303e 1213 section_size_type symbols_size,
730cdc88 1214 const unsigned char* symbol_names,
8383303e 1215 section_size_type symbol_names_size,
730cdc88
ILT
1216 unsigned int shndx,
1217 unsigned int reloc_shndx,
1218 unsigned int reloc_type);
1219#endif
1220
1221#ifdef HAVE_TARGET_32_BIG
1222template
1223bool
1224Eh_frame::add_ehframe_input_section<32, true>(
6fa2a40b 1225 Sized_relobj_file<32, true>* object,
730cdc88 1226 const unsigned char* symbols,
8383303e 1227 section_size_type symbols_size,
730cdc88 1228 const unsigned char* symbol_names,
8383303e 1229 section_size_type symbol_names_size,
730cdc88
ILT
1230 unsigned int shndx,
1231 unsigned int reloc_shndx,
1232 unsigned int reloc_type);
1233#endif
1234
1235#ifdef HAVE_TARGET_64_LITTLE
1236template
1237bool
1238Eh_frame::add_ehframe_input_section<64, false>(
6fa2a40b 1239 Sized_relobj_file<64, false>* object,
730cdc88 1240 const unsigned char* symbols,
8383303e 1241 section_size_type symbols_size,
730cdc88 1242 const unsigned char* symbol_names,
8383303e 1243 section_size_type symbol_names_size,
730cdc88
ILT
1244 unsigned int shndx,
1245 unsigned int reloc_shndx,
1246 unsigned int reloc_type);
1247#endif
1248
1249#ifdef HAVE_TARGET_64_BIG
1250template
1251bool
1252Eh_frame::add_ehframe_input_section<64, true>(
6fa2a40b 1253 Sized_relobj_file<64, true>* object,
730cdc88 1254 const unsigned char* symbols,
8383303e 1255 section_size_type symbols_size,
730cdc88 1256 const unsigned char* symbol_names,
8383303e 1257 section_size_type symbol_names_size,
730cdc88
ILT
1258 unsigned int shndx,
1259 unsigned int reloc_shndx,
1260 unsigned int reloc_type);
1261#endif
1262
3151305a 1263} // End namespace gold.
This page took 0.547164 seconds and 4 git commands to generate.