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