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