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