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