OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [gold/] [ehframe.cc] - Blame information for rev 299

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
// ehframe.cc -- handle exception frame sections for gold
2
 
3
// Copyright 2006, 2007, 2008 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
 
25
#include <cstring>
26
#include <algorithm>
27
 
28
#include "elfcpp.h"
29
#include "dwarf.h"
30
#include "symtab.h"
31
#include "reloc.h"
32
#include "ehframe.h"
33
 
34
namespace 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
39
// runtime.  This file also handles discarding duplicate exception
40
// frame information.
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
 
77
const int eh_frame_hdr_size = 4;
78
 
79
// Construct the exception frame header.
80
 
81
Eh_frame_hdr::Eh_frame_hdr(Output_section* eh_frame_section,
82
                           const Eh_frame* eh_frame_data)
83
  : Output_section_data(4),
84
    eh_frame_section_(eh_frame_section),
85
    eh_frame_data_(eh_frame_data),
86
    fde_offsets_(),
87
    any_unrecognized_eh_frame_sections_(false)
88
{
89
}
90
 
91
// Set the size of the exception frame header.
92
 
93
void
94
Eh_frame_hdr::set_final_data_size()
95
{
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);
105
}
106
 
107
// Write the data to the flie.
108
 
109
void
110
Eh_frame_hdr::do_write(Output_file* of)
111
{
112
  switch (parameters->size_and_endianness())
113
    {
114
#ifdef HAVE_TARGET_32_LITTLE
115
    case Parameters::TARGET_32_LITTLE:
116
      this->do_sized_write<32, false>(of);
117
      break;
118
#endif
119
#ifdef HAVE_TARGET_32_BIG
120
    case Parameters::TARGET_32_BIG:
121
      this->do_sized_write<32, true>(of);
122
      break;
123
#endif
124
#ifdef HAVE_TARGET_64_LITTLE
125
    case Parameters::TARGET_64_LITTLE:
126
      this->do_sized_write<64, false>(of);
127
      break;
128
#endif
129
#ifdef HAVE_TARGET_64_BIG
130
    case Parameters::TARGET_64_BIG:
131
      this->do_sized_write<64, true>(of);
132
      break;
133
#endif
134
    default:
135
      gold_unreachable();
136
    }
137
}
138
 
139
// Write the data to the file with the right endianness.
140
 
141
template<int size, bool big_endian>
142
void
143
Eh_frame_hdr::do_sized_write(Output_file* of)
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));
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
    }
172
  else
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.
186
 
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);
190
 
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
    }
211
 
212
  of->write_output_view(off, oview_size, oview);
213
}
214
 
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
 
220
template<int size, bool big_endian>
221
typename elfcpp::Elf_types<size>::Elf_Addr
222
Eh_frame_hdr::get_fde_pc(
223
    typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
224
    const unsigned char* eh_frame_contents,
225
    section_offset_type fde_offset,
226
    unsigned char fde_encoding)
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:
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.
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
 
293
template<int size, bool big_endian>
294
void
295
Eh_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;
311
      fde_pc = this->get_fde_pc<size, big_endian>(eh_frame_address,
312
                                                  eh_frame_contents,
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
324
// CIE.  ADDRALIGN is the required alignment.  Record the FDE pc for
325
// EH_FRAME_HDR.  Return the new offset.
326
 
327
template<int size, bool big_endian>
328
section_offset_type
329
Fde::write(unsigned char* oview, section_offset_type offset,
330
           unsigned int addralign, section_offset_type cie_offset,
331
           unsigned char fde_encoding, Eh_frame_hdr* eh_frame_hdr)
332
{
333
  gold_assert((offset & (addralign - 1)) == 0);
334
 
335
  size_t length = this->contents_.length();
336
 
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
 
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,
345
                                         aligned_full_length - 4);
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
 
358
  if (aligned_full_length > length + 8)
359
    memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
360
 
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
 
365
  return offset + aligned_full_length;
366
}
367
 
368
// Class Cie.
369
 
370
// Destructor.
371
 
372
Cie::~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
 
382
section_offset_type
383
Cie::set_output_offset(section_offset_type output_offset,
384
                       unsigned int addralign,
385
                       Merge_map* merge_map)
386
{
387
  size_t length = this->contents_.length();
388
 
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
 
395
  length = align_address(length, addralign);
396
 
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();
404
      fde_length = align_address(fde_length, addralign);
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
412
// recording.  Round up the bytes to ADDRALIGN.  Return the new
413
// offset.
414
 
415
template<int size, bool big_endian>
416
section_offset_type
417
Cie::write(unsigned char* oview, section_offset_type offset,
418
           unsigned int addralign, Eh_frame_hdr* eh_frame_hdr)
419
{
420
  gold_assert((offset & (addralign - 1)) == 0);
421
 
422
  section_offset_type cie_offset = offset;
423
 
424
  size_t length = this->contents_.length();
425
 
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
 
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.
432
  elfcpp::Swap<32, big_endian>::writeval(oview + offset,
433
                                         aligned_full_length - 4);
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);
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;
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)
451
    offset = (*p)->write<size, big_endian>(oview, offset, addralign,
452
                                           cie_offset, fde_encoding,
453
                                           eh_frame_hdr);
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
 
466
bool
467
operator==(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
 
475
bool
476
operator<(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
 
485
Eh_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
    mappings_are_done_(false),
492
    final_data_size_(0)
493
{
494
}
495
 
496
// Skip an LEB128, updating *PP to point to the next character.
497
// Return false if we ran off the end of the string.
498
 
499
bool
500
Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend)
501
{
502
  const unsigned char* p;
503
  for (p = *pp; p < pend; ++p)
504
    {
505
      if ((*p & 0x80) == 0)
506
        {
507
          *pp = p + 1;
508
          return true;
509
        }
510
    }
511
  return false;
512
}
513
 
514
// Add input section SHNDX in OBJECT to an exception frame section.
515
// SYMBOLS is the contents of the symbol table section (size
516
// SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
517
// SYMBOL_NAMES_SIZE).  RELOC_SHNDX is the index of a relocation
518
// section applying to SHNDX, or 0 if none, or -1U if more than one.
519
// RELOC_TYPE is the type of the reloc section if there is one, either
520
// SHT_REL or SHT_RELA.  We try to parse the input exception frame
521
// data into our data structures.  If we can't do it, we return false
522
// to mean that the section should be handled as a normal input
523
// section.
524
 
525
template<int size, bool big_endian>
526
bool
527
Eh_frame::add_ehframe_input_section(
528
    Sized_relobj<size, big_endian>* object,
529
    const unsigned char* symbols,
530
    section_size_type symbols_size,
531
    const unsigned char* symbol_names,
532
    section_size_type symbol_names_size,
533
    unsigned int shndx,
534
    unsigned int reloc_shndx,
535
    unsigned int reloc_type)
536
{
537
  // Get the section contents.
538
  section_size_type contents_len;
539
  const unsigned char* pcontents = object->section_contents(shndx,
540
                                                            &contents_len,
541
                                                            false);
542
  if (contents_len == 0)
543
    return false;
544
 
545
  // If this is the marker section for the end of the data, then
546
  // return false to force it to be handled as an ordinary input
547
  // section.  If we don't do this, we won't correctly handle the case
548
  // of unrecognized .eh_frame sections.
549
  if (contents_len == 4
550
      && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0)
551
    return false;
552
 
553
  New_cies new_cies;
554
  if (!this->do_add_ehframe_input_section(object, symbols, symbols_size,
555
                                          symbol_names, symbol_names_size,
556
                                          shndx, reloc_shndx,
557
                                          reloc_type, pcontents,
558
                                          contents_len, &new_cies))
559
    {
560
      if (this->eh_frame_hdr_ != NULL)
561
        this->eh_frame_hdr_->found_unrecognized_eh_frame_section();
562
 
563
      for (New_cies::iterator p = new_cies.begin();
564
           p != new_cies.end();
565
           ++p)
566
        delete p->first;
567
 
568
      return false;
569
    }
570
 
571
  // Now that we know we are using this section, record any new CIEs
572
  // that we found.
573
  for (New_cies::const_iterator p = new_cies.begin();
574
       p != new_cies.end();
575
       ++p)
576
    {
577
      if (p->second)
578
        this->cie_offsets_.insert(p->first);
579
      else
580
        this->unmergeable_cie_offsets_.push_back(p->first);
581
    }
582
 
583
  return true;
584
}
585
 
586
// The bulk of the implementation of add_ehframe_input_section.
587
 
588
template<int size, bool big_endian>
589
bool
590
Eh_frame::do_add_ehframe_input_section(
591
    Sized_relobj<size, big_endian>* object,
592
    const unsigned char* symbols,
593
    section_size_type symbols_size,
594
    const unsigned char* symbol_names,
595
    section_size_type symbol_names_size,
596
    unsigned int shndx,
597
    unsigned int reloc_shndx,
598
    unsigned int reloc_type,
599
    const unsigned char* pcontents,
600
    section_size_type contents_len,
601
    New_cies* new_cies)
602
{
603
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
604
  Track_relocs<size, big_endian> relocs;
605
 
606
  const unsigned char* p = pcontents;
607
  const unsigned char* pend = p + contents_len;
608
 
609
  // Get the contents of the reloc section if any.
610
  if (!relocs.initialize(object, reloc_shndx, reloc_type))
611
    return false;
612
 
613
  // Keep track of which CIEs are at which offsets.
614
  Offsets_to_cie cies;
615
 
616
  while (p < pend)
617
    {
618
      if (pend - p < 4)
619
        return false;
620
 
621
      // There shouldn't be any relocations here.
622
      if (relocs.advance(p + 4 - pcontents) > 0)
623
        return false;
624
 
625
      unsigned int len = elfcpp::Swap<32, big_endian>::readval(p);
626
      p += 4;
627
      if (len == 0)
628
        {
629
          // We should only find a zero-length entry at the end of the
630
          // section.
631
          if (p < pend)
632
            return false;
633
          break;
634
        }
635
      // We don't support a 64-bit .eh_frame.
636
      if (len == 0xffffffff)
637
        return false;
638
      if (static_cast<unsigned int>(pend - p) < len)
639
        return false;
640
 
641
      const unsigned char* const pentend = p + len;
642
 
643
      if (pend - p < 4)
644
        return false;
645
      if (relocs.advance(p + 4 - pcontents) > 0)
646
        return false;
647
 
648
      unsigned int id = elfcpp::Swap<32, big_endian>::readval(p);
649
      p += 4;
650
 
651
      if (id == 0)
652
        {
653
          // CIE.
654
          if (!this->read_cie(object, shndx, symbols, symbols_size,
655
                              symbol_names, symbol_names_size,
656
                              pcontents, p, pentend, &relocs, &cies,
657
                              new_cies))
658
            return false;
659
        }
660
      else
661
        {
662
          // FDE.
663
          if (!this->read_fde(object, shndx, symbols, symbols_size,
664
                              pcontents, id, p, pentend, &relocs, &cies))
665
            return false;
666
        }
667
 
668
      p = pentend;
669
    }
670
 
671
  return true;
672
}
673
 
674
// Read a CIE.  Return false if we can't parse the information.
675
 
676
template<int size, bool big_endian>
677
bool
678
Eh_frame::read_cie(Sized_relobj<size, big_endian>* object,
679
                   unsigned int shndx,
680
                   const unsigned char* symbols,
681
                   section_size_type symbols_size,
682
                   const unsigned char* symbol_names,
683
                   section_size_type symbol_names_size,
684
                   const unsigned char* pcontents,
685
                   const unsigned char* pcie,
686
                   const unsigned char *pcieend,
687
                   Track_relocs<size, big_endian>* relocs,
688
                   Offsets_to_cie* cies,
689
                   New_cies* new_cies)
690
{
691
  bool mergeable = true;
692
 
693
  // We need to find the personality routine if there is one, since we
694
  // can only merge CIEs which use the same routine.  We also need to
695
  // find the FDE encoding if there is one, so that we can read the PC
696
  // from the FDE.
697
 
698
  const unsigned char* p = pcie;
699
 
700
  if (pcieend - p < 1)
701
    return false;
702
  unsigned char version = *p++;
703
  if (version != 1 && version != 3)
704
    return false;
705
 
706
  const unsigned char* paug = p;
707
  const void* paugendv = memchr(p, '\0', pcieend - p);
708
  const unsigned char* paugend = static_cast<const unsigned char*>(paugendv);
709
  if (paugend == NULL)
710
    return false;
711
  p = paugend + 1;
712
 
713
  if (paug[0] == 'e' && paug[1] == 'h')
714
    {
715
      // This is a CIE from gcc before version 3.0.  We can't merge
716
      // these.  We can still read the FDEs.
717
      mergeable = false;
718
      paug += 2;
719
      if (*paug != '\0')
720
        return false;
721
      if (pcieend - p < size / 8)
722
        return false;
723
      p += size / 8;
724
    }
725
 
726
  // Skip the code alignment.
727
  if (!skip_leb128(&p, pcieend))
728
    return false;
729
 
730
  // Skip the data alignment.
731
  if (!skip_leb128(&p, pcieend))
732
    return false;
733
 
734
  // Skip the return column.
735
  if (version == 1)
736
    {
737
      if (pcieend - p < 1)
738
        return false;
739
      ++p;
740
    }
741
  else
742
    {
743
      if (!skip_leb128(&p, pcieend))
744
        return false;
745
    }
746
 
747
  if (*paug == 'z')
748
    {
749
      ++paug;
750
      // Skip the augmentation size.
751
      if (!skip_leb128(&p, pcieend))
752
        return false;
753
    }
754
 
755
  unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr;
756
  int per_offset = -1;
757
  while (*paug != '\0')
758
    {
759
      switch (*paug)
760
        {
761
        case 'L': // LSDA encoding.
762
          if (pcieend - p < 1)
763
            return false;
764
          ++p;
765
          break;
766
 
767
        case 'R': // FDE encoding.
768
          if (pcieend - p < 1)
769
            return false;
770
          fde_encoding = *p;
771
          switch (fde_encoding & 7)
772
            {
773
            case elfcpp::DW_EH_PE_absptr:
774
            case elfcpp::DW_EH_PE_udata2:
775
            case elfcpp::DW_EH_PE_udata4:
776
            case elfcpp::DW_EH_PE_udata8:
777
              break;
778
            default:
779
              // We don't expect to see any other cases here, and
780
              // we're not prepared to handle them.
781
              return false;
782
            }
783
          ++p;
784
          break;
785
 
786
        case 'S':
787
          break;
788
 
789
        case 'P':
790
          // Personality encoding.
791
          {
792
            if (pcieend - p < 1)
793
              return false;
794
            unsigned char per_encoding = *p;
795
            ++p;
796
 
797
            if ((per_encoding & 0x60) == 0x60)
798
              return false;
799
            unsigned int per_width;
800
            switch (per_encoding & 7)
801
              {
802
              case elfcpp::DW_EH_PE_udata2:
803
                per_width = 2;
804
                break;
805
              case elfcpp::DW_EH_PE_udata4:
806
                per_width = 4;
807
                break;
808
              case elfcpp::DW_EH_PE_udata8:
809
                per_width = 8;
810
                break;
811
              case elfcpp::DW_EH_PE_absptr:
812
                per_width = size / 8;
813
                break;
814
              default:
815
                return false;
816
              }
817
 
818
            if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned)
819
              {
820
                unsigned int len = p - pcie;
821
                len += per_width - 1;
822
                len &= ~ (per_width - 1);
823
                if (static_cast<unsigned int>(pcieend - p) < len)
824
                  return false;
825
                p += len;
826
              }
827
 
828
            per_offset = p - pcontents;
829
 
830
            if (static_cast<unsigned int>(pcieend - p) < per_width)
831
              return false;
832
            p += per_width;
833
          }
834
          break;
835
 
836
        default:
837
          return false;
838
        }
839
 
840
      ++paug;
841
    }
842
 
843
  const char* personality_name = "";
844
  if (per_offset != -1)
845
    {
846
      if (relocs->advance(per_offset) > 0)
847
        return false;
848
      if (relocs->next_offset() != per_offset)
849
        return false;
850
 
851
      unsigned int personality_symndx = relocs->next_symndx();
852
      if (personality_symndx == -1U)
853
        return false;
854
 
855
      if (personality_symndx < object->local_symbol_count())
856
        {
857
          // We can only merge this CIE if the personality routine is
858
          // a global symbol.  We can still read the FDEs.
859
          mergeable = false;
860
        }
861
      else
862
        {
863
          const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
864
          if (personality_symndx >= symbols_size / sym_size)
865
            return false;
866
          elfcpp::Sym<size, big_endian> sym(symbols
867
                                            + (personality_symndx * sym_size));
868
          unsigned int name_offset = sym.get_st_name();
869
          if (name_offset >= symbol_names_size)
870
            return false;
871
          personality_name = (reinterpret_cast<const char*>(symbol_names)
872
                              + name_offset);
873
        }
874
 
875
      int r = relocs->advance(per_offset + 1);
876
      gold_assert(r == 1);
877
    }
878
 
879
  if (relocs->advance(pcieend - pcontents) > 0)
880
    return false;
881
 
882
  Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding,
883
          personality_name, pcie, pcieend - pcie);
884
  Cie* cie_pointer = NULL;
885
  if (mergeable)
886
    {
887
      Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
888
      if (find_cie != this->cie_offsets_.end())
889
        cie_pointer = *find_cie;
890
      else
891
        {
892
          // See if we already saw this CIE in this object file.
893
          for (New_cies::const_iterator pc = new_cies->begin();
894
               pc != new_cies->end();
895
               ++pc)
896
            {
897
              if (*(pc->first) == cie)
898
                {
899
                  cie_pointer = pc->first;
900
                  break;
901
                }
902
            }
903
        }
904
    }
905
 
906
  if (cie_pointer == NULL)
907
    {
908
      cie_pointer = new Cie(cie);
909
      new_cies->push_back(std::make_pair(cie_pointer, mergeable));
910
    }
911
  else
912
    {
913
      // We are deleting this CIE.  Record that in our mapping from
914
      // input sections to the output section.  At this point we don't
915
      // know for sure that we are doing a special mapping for this
916
      // input section, but that's OK--if we don't do a special
917
      // mapping, nobody will ever ask for the mapping we add here.
918
      this->merge_map_.add_mapping(object, shndx, (pcie - 8) - pcontents,
919
                                   pcieend - (pcie - 8), -1);
920
    }
921
 
922
  // Record this CIE plus the offset in the input section.
923
  cies->insert(std::make_pair(pcie - pcontents, cie_pointer));
924
 
925
  return true;
926
}
927
 
928
// Read an FDE.  Return false if we can't parse the information.
929
 
930
template<int size, bool big_endian>
931
bool
932
Eh_frame::read_fde(Sized_relobj<size, big_endian>* object,
933
                   unsigned int shndx,
934
                   const unsigned char* symbols,
935
                   section_size_type symbols_size,
936
                   const unsigned char* pcontents,
937
                   unsigned int offset,
938
                   const unsigned char* pfde,
939
                   const unsigned char *pfdeend,
940
                   Track_relocs<size, big_endian>* relocs,
941
                   Offsets_to_cie* cies)
942
{
943
  // OFFSET is the distance between the 4 bytes before PFDE to the
944
  // start of the CIE.  The offset we recorded for the CIE is 8 bytes
945
  // after the start of the CIE--after the length and the zero tag.
946
  unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8;
947
  Offsets_to_cie::const_iterator pcie = cies->find(cie_offset);
948
  if (pcie == cies->end())
949
    return false;
950
  Cie* cie = pcie->second;
951
 
952
  // The FDE should start with a reloc to the start of the code which
953
  // it describes.
954
  if (relocs->advance(pfde - pcontents) > 0)
955
    return false;
956
 
957
  if (relocs->next_offset() != pfde - pcontents)
958
    return false;
959
 
960
  unsigned int symndx = relocs->next_symndx();
961
  if (symndx == -1U)
962
    return false;
963
 
964
  // There can be another reloc in the FDE, if the CIE specifies an
965
  // LSDA (language specific data area).  We currently don't care.  We
966
  // will care later if we want to optimize the LSDA from an absolute
967
  // pointer to a PC relative offset when generating a shared library.
968
  relocs->advance(pfdeend - pcontents);
969
 
970
  unsigned int fde_shndx;
971
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
972
  if (symndx >= symbols_size / sym_size)
973
    return false;
974
  elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size);
975
  bool is_ordinary;
976
  fde_shndx = object->adjust_sym_shndx(symndx, sym.get_st_shndx(),
977
                                       &is_ordinary);
978
 
979
  if (is_ordinary
980
      && fde_shndx != elfcpp::SHN_UNDEF
981
      && fde_shndx < object->shnum()
982
      && !object->is_section_included(fde_shndx))
983
    {
984
      // This FDE applies to a section which we are discarding.  We
985
      // can discard this FDE.
986
      this->merge_map_.add_mapping(object, shndx, (pfde - 8) - pcontents,
987
                                   pfdeend - (pfde - 8), -1);
988
      return true;
989
    }
990
 
991
  cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents,
992
                       pfde, pfdeend - pfde));
993
 
994
  return true;
995
}
996
 
997
// Return the number of FDEs.
998
 
999
unsigned int
1000
Eh_frame::fde_count() const
1001
{
1002
  unsigned int ret = 0;
1003
  for (Unmergeable_cie_offsets::const_iterator p =
1004
         this->unmergeable_cie_offsets_.begin();
1005
       p != this->unmergeable_cie_offsets_.end();
1006
       ++p)
1007
    ret += (*p)->fde_count();
1008
  for (Cie_offsets::const_iterator p = this->cie_offsets_.begin();
1009
       p != this->cie_offsets_.end();
1010
       ++p)
1011
    ret += (*p)->fde_count();
1012
  return ret;
1013
}
1014
 
1015
// Set the final data size.
1016
 
1017
void
1018
Eh_frame::set_final_data_size()
1019
{
1020
  // We can be called more than once if Layout::set_segment_offsets
1021
  // finds a better mapping.  We don't want to add all the mappings
1022
  // again.
1023
  if (this->mappings_are_done_)
1024
    {
1025
      this->set_data_size(this->final_data_size_);
1026
      return;
1027
    }
1028
 
1029
  section_offset_type output_offset = 0;
1030
 
1031
  for (Unmergeable_cie_offsets::iterator p =
1032
         this->unmergeable_cie_offsets_.begin();
1033
       p != this->unmergeable_cie_offsets_.end();
1034
       ++p)
1035
    output_offset = (*p)->set_output_offset(output_offset,
1036
                                            this->addralign(),
1037
                                            &this->merge_map_);
1038
 
1039
  for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1040
       p != this->cie_offsets_.end();
1041
       ++p)
1042
    output_offset = (*p)->set_output_offset(output_offset,
1043
                                            this->addralign(),
1044
                                            &this->merge_map_);
1045
 
1046
  this->mappings_are_done_ = true;
1047
  this->final_data_size_ = output_offset;
1048
 
1049
  gold_assert((output_offset & (this->addralign() - 1)) == 0);
1050
  this->set_data_size(output_offset);
1051
}
1052
 
1053
// Return an output offset for an input offset.
1054
 
1055
bool
1056
Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx,
1057
                           section_offset_type offset,
1058
                           section_offset_type* poutput) const
1059
{
1060
  return this->merge_map_.get_output_offset(object, shndx, offset, poutput);
1061
}
1062
 
1063
// Return whether this is the merge section for an input section.
1064
 
1065
bool
1066
Eh_frame::do_is_merge_section_for(const Relobj* object,
1067
                                  unsigned int shndx) const
1068
{
1069
  return this->merge_map_.is_merge_section_for(object, shndx);
1070
}
1071
 
1072
// Write the data to the output file.
1073
 
1074
void
1075
Eh_frame::do_write(Output_file* of)
1076
{
1077
  const off_t offset = this->offset();
1078
  const off_t oview_size = this->data_size();
1079
  unsigned char* const oview = of->get_output_view(offset, oview_size);
1080
 
1081
  switch (parameters->size_and_endianness())
1082
    {
1083
#ifdef HAVE_TARGET_32_LITTLE
1084
    case Parameters::TARGET_32_LITTLE:
1085
      this->do_sized_write<32, false>(oview);
1086
      break;
1087
#endif
1088
#ifdef HAVE_TARGET_32_BIG
1089
    case Parameters::TARGET_32_BIG:
1090
      this->do_sized_write<32, true>(oview);
1091
      break;
1092
#endif
1093
#ifdef HAVE_TARGET_64_LITTLE
1094
    case Parameters::TARGET_64_LITTLE:
1095
      this->do_sized_write<64, false>(oview);
1096
      break;
1097
#endif
1098
#ifdef HAVE_TARGET_64_BIG
1099
    case Parameters::TARGET_64_BIG:
1100
      this->do_sized_write<64, true>(oview);
1101
      break;
1102
#endif
1103
    default:
1104
      gold_unreachable();
1105
    }
1106
 
1107
  of->write_output_view(offset, oview_size, oview);
1108
}
1109
 
1110
// Write the data to the output file--template version.
1111
 
1112
template<int size, bool big_endian>
1113
void
1114
Eh_frame::do_sized_write(unsigned char* oview)
1115
{
1116
  unsigned int addralign = this->addralign();
1117
  section_offset_type o = 0;
1118
  for (Unmergeable_cie_offsets::iterator p =
1119
         this->unmergeable_cie_offsets_.begin();
1120
       p != this->unmergeable_cie_offsets_.end();
1121
       ++p)
1122
    o = (*p)->write<size, big_endian>(oview, o, addralign,
1123
                                      this->eh_frame_hdr_);
1124
  for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1125
       p != this->cie_offsets_.end();
1126
       ++p)
1127
    o = (*p)->write<size, big_endian>(oview, o, addralign,
1128
                                      this->eh_frame_hdr_);
1129
}
1130
 
1131
#ifdef HAVE_TARGET_32_LITTLE
1132
template
1133
bool
1134
Eh_frame::add_ehframe_input_section<32, false>(
1135
    Sized_relobj<32, false>* object,
1136
    const unsigned char* symbols,
1137
    section_size_type symbols_size,
1138
    const unsigned char* symbol_names,
1139
    section_size_type symbol_names_size,
1140
    unsigned int shndx,
1141
    unsigned int reloc_shndx,
1142
    unsigned int reloc_type);
1143
#endif
1144
 
1145
#ifdef HAVE_TARGET_32_BIG
1146
template
1147
bool
1148
Eh_frame::add_ehframe_input_section<32, true>(
1149
    Sized_relobj<32, true>* object,
1150
    const unsigned char* symbols,
1151
    section_size_type symbols_size,
1152
    const unsigned char* symbol_names,
1153
    section_size_type symbol_names_size,
1154
    unsigned int shndx,
1155
    unsigned int reloc_shndx,
1156
    unsigned int reloc_type);
1157
#endif
1158
 
1159
#ifdef HAVE_TARGET_64_LITTLE
1160
template
1161
bool
1162
Eh_frame::add_ehframe_input_section<64, false>(
1163
    Sized_relobj<64, false>* object,
1164
    const unsigned char* symbols,
1165
    section_size_type symbols_size,
1166
    const unsigned char* symbol_names,
1167
    section_size_type symbol_names_size,
1168
    unsigned int shndx,
1169
    unsigned int reloc_shndx,
1170
    unsigned int reloc_type);
1171
#endif
1172
 
1173
#ifdef HAVE_TARGET_64_BIG
1174
template
1175
bool
1176
Eh_frame::add_ehframe_input_section<64, true>(
1177
    Sized_relobj<64, true>* object,
1178
    const unsigned char* symbols,
1179
    section_size_type symbols_size,
1180
    const unsigned char* symbol_names,
1181
    section_size_type symbol_names_size,
1182
    unsigned int shndx,
1183
    unsigned int reloc_shndx,
1184
    unsigned int reloc_type);
1185
#endif
1186
 
1187
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.