Add basic exception frame header, plus test.
[external/binutils.git] / gold / ehframe.cc
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
25 #include "elfcpp.h"
26 #include "dwarf.h"
27 #include "ehframe.h"
28
29 namespace gold
30 {
31
32 // This file handles generation of the exception frame header that
33 // gcc's runtime support libraries use to find unwind information at
34 // runtime.
35
36 // The exception frame header starts with four bytes:
37
38 // 0: The version number, currently 1.
39
40 // 1: The encoding of the pointer to the exception frames.  This can
41 //    be any DWARF unwind encoding (DW_EH_PE_*).  It is normally a 4
42 //    byte PC relative offset (DW_EH_PE_pcrel | DW_EH_PE_sdata4).
43
44 // 2: The encoding of the count of the number of FDE pointers in the
45 //    lookup table.  This can be any DWARF unwind encoding, and in
46 //    particular can be DW_EH_PE_omit if the count is omitted.  It is
47 //    normally a 4 byte unsigned count (DW_EH_PE_udata4).
48
49 // 3: The encoding of the lookup table entries.  Currently gcc's
50 //    libraries will only support DW_EH_PE_datarel | DW_EH_PE_sdata4,
51 //    which means that the values are 4 byte offsets from the start of
52 //    the table.
53
54 // The exception frame header is followed by a pointer to the contents
55 // of the exception frame section (.eh_frame).  This pointer is
56 // encoded as specified in the byte at offset 1 of the header (i.e.,
57 // it is normally a 4 byte PC relative offset).
58
59 // If there is a lookup table, this is followed by the count of the
60 // number of FDE pointers, encoded as specified in the byte at offset
61 // 2 of the header (i.e., normally a 4 byte unsigned integer).
62
63 // This is followed by the table, which should start at an 4-byte
64 // aligned address in memory.  Each entry in the table is 8 bytes.
65 // Each entry represents an FDE.  The first four bytes of each entry
66 // are an offset to the starting PC for the FDE.  The last four bytes
67 // of each entry are an offset to the FDE data.  The offsets are from
68 // the start of the exception frame header information.  The entries
69 // are in sorted order by starting PC.
70
71 // FIXME: We currently always generate an empty exception frame
72 // header.
73
74 const int eh_frame_hdr_size = 4;
75
76 // Construct the exception frame header.
77
78 Eh_frame_hdr::Eh_frame_hdr(const Target* target,
79                            Output_section* eh_frame_section)
80   : Output_section_data(4),
81     target_(target), eh_frame_section_(eh_frame_section)
82 {
83 }
84
85 // Set the final address and size of the exception frame header.
86
87 void
88 Eh_frame_hdr::do_set_address(uint64_t, off_t)
89 {
90   this->set_data_size(eh_frame_hdr_size + 4);
91 }
92
93 // Write the data to the flie.
94
95 void
96 Eh_frame_hdr::do_write(Output_file* of)
97 {
98   const off_t off = this->offset();
99   const off_t oview_size = this->data_size();
100   unsigned char* const oview = of->get_output_view(off, oview_size);
101
102   // Version number.
103   oview[0] = 1;
104
105   // Write out a 4 byte PC relative offset to the address of the
106   // .eh_frame section.
107   oview[1] = elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4;
108   uint64_t eh_frame_address = this->eh_frame_section_->address();
109   uint64_t eh_frame_hdr_address = this->address();
110   uint64_t eh_frame_offset = (eh_frame_address -
111                               (eh_frame_hdr_address + 4));
112   if (this->target_->is_big_endian())
113     elfcpp::Swap<32, true>::writeval(oview + 4, eh_frame_offset);
114   else
115     elfcpp::Swap<32, false>::writeval(oview + 4, eh_frame_offset);
116
117   // We don't currently write out the sorted table.
118   oview[2] = elfcpp::DW_EH_PE_omit;
119   oview[3] = elfcpp::DW_EH_PE_omit;
120
121   gold_assert(oview_size == 8);
122
123   of->write_output_view(off, oview_size, oview);
124 }
125
126 } // End namespace gold.