1*fae548d3Szrj // ehframe.cc -- handle exception frame sections for gold
2*fae548d3Szrj 
3*fae548d3Szrj // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4*fae548d3Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*fae548d3Szrj 
6*fae548d3Szrj // This file is part of gold.
7*fae548d3Szrj 
8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify
9*fae548d3Szrj // it under the terms of the GNU General Public License as published by
10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or
11*fae548d3Szrj // (at your option) any later version.
12*fae548d3Szrj 
13*fae548d3Szrj // This program is distributed in the hope that it will be useful,
14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*fae548d3Szrj // GNU General Public License for more details.
17*fae548d3Szrj 
18*fae548d3Szrj // You should have received a copy of the GNU General Public License
19*fae548d3Szrj // along with this program; if not, write to the Free Software
20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*fae548d3Szrj // MA 02110-1301, USA.
22*fae548d3Szrj 
23*fae548d3Szrj #include "gold.h"
24*fae548d3Szrj 
25*fae548d3Szrj #include <cstring>
26*fae548d3Szrj #include <algorithm>
27*fae548d3Szrj 
28*fae548d3Szrj #include "elfcpp.h"
29*fae548d3Szrj #include "dwarf.h"
30*fae548d3Szrj #include "symtab.h"
31*fae548d3Szrj #include "reloc.h"
32*fae548d3Szrj #include "ehframe.h"
33*fae548d3Szrj 
34*fae548d3Szrj namespace gold
35*fae548d3Szrj {
36*fae548d3Szrj 
37*fae548d3Szrj // This file handles generation of the exception frame header that
38*fae548d3Szrj // gcc's runtime support libraries use to find unwind information at
39*fae548d3Szrj // runtime.  This file also handles discarding duplicate exception
40*fae548d3Szrj // frame information.
41*fae548d3Szrj 
42*fae548d3Szrj // The exception frame header starts with four bytes:
43*fae548d3Szrj 
44*fae548d3Szrj // 0: The version number, currently 1.
45*fae548d3Szrj 
46*fae548d3Szrj // 1: The encoding of the pointer to the exception frames.  This can
47*fae548d3Szrj //    be any DWARF unwind encoding (DW_EH_PE_*).  It is normally a 4
48*fae548d3Szrj //    byte PC relative offset (DW_EH_PE_pcrel | DW_EH_PE_sdata4).
49*fae548d3Szrj 
50*fae548d3Szrj // 2: The encoding of the count of the number of FDE pointers in the
51*fae548d3Szrj //    lookup table.  This can be any DWARF unwind encoding, and in
52*fae548d3Szrj //    particular can be DW_EH_PE_omit if the count is omitted.  It is
53*fae548d3Szrj //    normally a 4 byte unsigned count (DW_EH_PE_udata4).
54*fae548d3Szrj 
55*fae548d3Szrj // 3: The encoding of the lookup table entries.  Currently gcc's
56*fae548d3Szrj //    libraries will only support DW_EH_PE_datarel | DW_EH_PE_sdata4,
57*fae548d3Szrj //    which means that the values are 4 byte offsets from the start of
58*fae548d3Szrj //    the table.
59*fae548d3Szrj 
60*fae548d3Szrj // The exception frame header is followed by a pointer to the contents
61*fae548d3Szrj // of the exception frame section (.eh_frame).  This pointer is
62*fae548d3Szrj // encoded as specified in the byte at offset 1 of the header (i.e.,
63*fae548d3Szrj // it is normally a 4 byte PC relative offset).
64*fae548d3Szrj 
65*fae548d3Szrj // If there is a lookup table, this is followed by the count of the
66*fae548d3Szrj // number of FDE pointers, encoded as specified in the byte at offset
67*fae548d3Szrj // 2 of the header (i.e., normally a 4 byte unsigned integer).
68*fae548d3Szrj 
69*fae548d3Szrj // This is followed by the table, which should start at an 4-byte
70*fae548d3Szrj // aligned address in memory.  Each entry in the table is 8 bytes.
71*fae548d3Szrj // Each entry represents an FDE.  The first four bytes of each entry
72*fae548d3Szrj // are an offset to the starting PC for the FDE.  The last four bytes
73*fae548d3Szrj // of each entry are an offset to the FDE data.  The offsets are from
74*fae548d3Szrj // the start of the exception frame header information.  The entries
75*fae548d3Szrj // are in sorted order by starting PC.
76*fae548d3Szrj 
77*fae548d3Szrj const int eh_frame_hdr_size = 4;
78*fae548d3Szrj 
79*fae548d3Szrj // Construct the exception frame header.
80*fae548d3Szrj 
Eh_frame_hdr(Output_section * eh_frame_section,const Eh_frame * eh_frame_data)81*fae548d3Szrj Eh_frame_hdr::Eh_frame_hdr(Output_section* eh_frame_section,
82*fae548d3Szrj 			   const Eh_frame* eh_frame_data)
83*fae548d3Szrj   : Output_section_data(4),
84*fae548d3Szrj     eh_frame_section_(eh_frame_section),
85*fae548d3Szrj     eh_frame_data_(eh_frame_data),
86*fae548d3Szrj     fde_offsets_(),
87*fae548d3Szrj     any_unrecognized_eh_frame_sections_(false)
88*fae548d3Szrj {
89*fae548d3Szrj }
90*fae548d3Szrj 
91*fae548d3Szrj // Set the size of the exception frame header.
92*fae548d3Szrj 
93*fae548d3Szrj void
set_final_data_size()94*fae548d3Szrj Eh_frame_hdr::set_final_data_size()
95*fae548d3Szrj {
96*fae548d3Szrj   unsigned int data_size = eh_frame_hdr_size + 4;
97*fae548d3Szrj   if (!this->any_unrecognized_eh_frame_sections_)
98*fae548d3Szrj     {
99*fae548d3Szrj       unsigned int fde_count = this->eh_frame_data_->fde_count();
100*fae548d3Szrj       if (fde_count != 0)
101*fae548d3Szrj 	data_size += 4 + 8 * fde_count;
102*fae548d3Szrj       this->fde_offsets_.reserve(fde_count);
103*fae548d3Szrj     }
104*fae548d3Szrj   this->set_data_size(data_size);
105*fae548d3Szrj }
106*fae548d3Szrj 
107*fae548d3Szrj // Write the data to the file.
108*fae548d3Szrj 
109*fae548d3Szrj void
do_write(Output_file * of)110*fae548d3Szrj Eh_frame_hdr::do_write(Output_file* of)
111*fae548d3Szrj {
112*fae548d3Szrj   switch (parameters->size_and_endianness())
113*fae548d3Szrj     {
114*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
115*fae548d3Szrj     case Parameters::TARGET_32_LITTLE:
116*fae548d3Szrj       this->do_sized_write<32, false>(of);
117*fae548d3Szrj       break;
118*fae548d3Szrj #endif
119*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
120*fae548d3Szrj     case Parameters::TARGET_32_BIG:
121*fae548d3Szrj       this->do_sized_write<32, true>(of);
122*fae548d3Szrj       break;
123*fae548d3Szrj #endif
124*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
125*fae548d3Szrj     case Parameters::TARGET_64_LITTLE:
126*fae548d3Szrj       this->do_sized_write<64, false>(of);
127*fae548d3Szrj       break;
128*fae548d3Szrj #endif
129*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
130*fae548d3Szrj     case Parameters::TARGET_64_BIG:
131*fae548d3Szrj       this->do_sized_write<64, true>(of);
132*fae548d3Szrj       break;
133*fae548d3Szrj #endif
134*fae548d3Szrj     default:
135*fae548d3Szrj       gold_unreachable();
136*fae548d3Szrj     }
137*fae548d3Szrj }
138*fae548d3Szrj 
139*fae548d3Szrj // Write the data to the file with the right endianness.
140*fae548d3Szrj 
141*fae548d3Szrj template<int size, bool big_endian>
142*fae548d3Szrj void
do_sized_write(Output_file * of)143*fae548d3Szrj Eh_frame_hdr::do_sized_write(Output_file* of)
144*fae548d3Szrj {
145*fae548d3Szrj   const off_t off = this->offset();
146*fae548d3Szrj   const off_t oview_size = this->data_size();
147*fae548d3Szrj   unsigned char* const oview = of->get_output_view(off, oview_size);
148*fae548d3Szrj 
149*fae548d3Szrj   // Version number.
150*fae548d3Szrj   oview[0] = 1;
151*fae548d3Szrj 
152*fae548d3Szrj   // Write out a 4 byte PC relative offset to the address of the
153*fae548d3Szrj   // .eh_frame section.
154*fae548d3Szrj   oview[1] = elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4;
155*fae548d3Szrj   uint64_t eh_frame_address = this->eh_frame_section_->address();
156*fae548d3Szrj   uint64_t eh_frame_hdr_address = this->address();
157*fae548d3Szrj   uint64_t eh_frame_offset = (eh_frame_address -
158*fae548d3Szrj 			      (eh_frame_hdr_address + 4));
159*fae548d3Szrj   elfcpp::Swap<32, big_endian>::writeval(oview + 4, eh_frame_offset);
160*fae548d3Szrj 
161*fae548d3Szrj   if (this->any_unrecognized_eh_frame_sections_
162*fae548d3Szrj       || this->fde_offsets_.empty())
163*fae548d3Szrj     {
164*fae548d3Szrj       // There are no FDEs, or we didn't recognize the format of the
165*fae548d3Szrj       // some of the .eh_frame sections, so we can't write out the
166*fae548d3Szrj       // sorted table.
167*fae548d3Szrj       oview[2] = elfcpp::DW_EH_PE_omit;
168*fae548d3Szrj       oview[3] = elfcpp::DW_EH_PE_omit;
169*fae548d3Szrj 
170*fae548d3Szrj       gold_assert(oview_size == 8);
171*fae548d3Szrj     }
172*fae548d3Szrj   else
173*fae548d3Szrj     {
174*fae548d3Szrj       oview[2] = elfcpp::DW_EH_PE_udata4;
175*fae548d3Szrj       oview[3] = elfcpp::DW_EH_PE_datarel | elfcpp::DW_EH_PE_sdata4;
176*fae548d3Szrj 
177*fae548d3Szrj       elfcpp::Swap<32, big_endian>::writeval(oview + 8,
178*fae548d3Szrj 					     this->fde_offsets_.size());
179*fae548d3Szrj 
180*fae548d3Szrj       // We have the offsets of the FDEs in the .eh_frame section.  We
181*fae548d3Szrj       // couldn't easily get the PC values before, as they depend on
182*fae548d3Szrj       // relocations which are, of course, target specific.  This code
183*fae548d3Szrj       // is run after all those relocations have been applied to the
184*fae548d3Szrj       // output file.  Here we read the output file again to find the
185*fae548d3Szrj       // PC values.  Then we sort the list and write it out.
186*fae548d3Szrj 
187*fae548d3Szrj       Fde_addresses<size> fde_addresses(this->fde_offsets_.size());
188*fae548d3Szrj       this->get_fde_addresses<size, big_endian>(of, &this->fde_offsets_,
189*fae548d3Szrj 						&fde_addresses);
190*fae548d3Szrj 
191*fae548d3Szrj       std::sort(fde_addresses.begin(), fde_addresses.end(),
192*fae548d3Szrj 		Fde_address_compare<size>());
193*fae548d3Szrj 
194*fae548d3Szrj       typename elfcpp::Elf_types<size>::Elf_Addr output_address;
195*fae548d3Szrj       output_address = this->address();
196*fae548d3Szrj 
197*fae548d3Szrj       unsigned char* pfde = oview + 12;
198*fae548d3Szrj       for (typename Fde_addresses<size>::iterator p = fde_addresses.begin();
199*fae548d3Szrj 	   p != fde_addresses.end();
200*fae548d3Szrj 	   ++p)
201*fae548d3Szrj 	{
202*fae548d3Szrj 	  elfcpp::Swap<32, big_endian>::writeval(pfde,
203*fae548d3Szrj 						 p->first - output_address);
204*fae548d3Szrj 	  elfcpp::Swap<32, big_endian>::writeval(pfde + 4,
205*fae548d3Szrj 						 p->second - output_address);
206*fae548d3Szrj 	  pfde += 8;
207*fae548d3Szrj 	}
208*fae548d3Szrj 
209*fae548d3Szrj       gold_assert(pfde - oview == oview_size);
210*fae548d3Szrj     }
211*fae548d3Szrj 
212*fae548d3Szrj   of->write_output_view(off, oview_size, oview);
213*fae548d3Szrj }
214*fae548d3Szrj 
215*fae548d3Szrj // Given the offset FDE_OFFSET of an FDE in the .eh_frame section, and
216*fae548d3Szrj // the contents of the .eh_frame section EH_FRAME_CONTENTS, where the
217*fae548d3Szrj // FDE's encoding is FDE_ENCODING, return the output address of the
218*fae548d3Szrj // FDE's PC.
219*fae548d3Szrj 
220*fae548d3Szrj template<int size, bool big_endian>
221*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr
get_fde_pc(typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,const unsigned char * eh_frame_contents,section_offset_type fde_offset,unsigned char fde_encoding)222*fae548d3Szrj Eh_frame_hdr::get_fde_pc(
223*fae548d3Szrj     typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address,
224*fae548d3Szrj     const unsigned char* eh_frame_contents,
225*fae548d3Szrj     section_offset_type fde_offset,
226*fae548d3Szrj     unsigned char fde_encoding)
227*fae548d3Szrj {
228*fae548d3Szrj   // The FDE starts with a 4 byte length and a 4 byte offset to the
229*fae548d3Szrj   // CIE.  The PC follows.
230*fae548d3Szrj   const unsigned char* p = eh_frame_contents + fde_offset + 8;
231*fae548d3Szrj 
232*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_Addr pc;
233*fae548d3Szrj   bool is_signed = (fde_encoding & elfcpp::DW_EH_PE_signed) != 0;
234*fae548d3Szrj   int pc_size = fde_encoding & 7;
235*fae548d3Szrj   if (pc_size == elfcpp::DW_EH_PE_absptr)
236*fae548d3Szrj     {
237*fae548d3Szrj       if (size == 32)
238*fae548d3Szrj 	pc_size = elfcpp::DW_EH_PE_udata4;
239*fae548d3Szrj       else if (size == 64)
240*fae548d3Szrj 	pc_size = elfcpp::DW_EH_PE_udata8;
241*fae548d3Szrj       else
242*fae548d3Szrj 	gold_unreachable();
243*fae548d3Szrj     }
244*fae548d3Szrj 
245*fae548d3Szrj   switch (pc_size)
246*fae548d3Szrj     {
247*fae548d3Szrj     case elfcpp::DW_EH_PE_udata2:
248*fae548d3Szrj       pc = elfcpp::Swap<16, big_endian>::readval(p);
249*fae548d3Szrj       if (is_signed)
250*fae548d3Szrj 	pc = (pc ^ 0x8000) - 0x8000;
251*fae548d3Szrj       break;
252*fae548d3Szrj 
253*fae548d3Szrj     case elfcpp::DW_EH_PE_udata4:
254*fae548d3Szrj       pc = elfcpp::Swap<32, big_endian>::readval(p);
255*fae548d3Szrj       if (size > 32 && is_signed)
256*fae548d3Szrj 	pc = (pc ^ 0x80000000) - 0x80000000;
257*fae548d3Szrj       break;
258*fae548d3Szrj 
259*fae548d3Szrj     case elfcpp::DW_EH_PE_udata8:
260*fae548d3Szrj       gold_assert(size == 64);
261*fae548d3Szrj       pc = elfcpp::Swap_unaligned<64, big_endian>::readval(p);
262*fae548d3Szrj       break;
263*fae548d3Szrj 
264*fae548d3Szrj     default:
265*fae548d3Szrj       // All other cases were rejected in Eh_frame::read_cie.
266*fae548d3Szrj       gold_unreachable();
267*fae548d3Szrj     }
268*fae548d3Szrj 
269*fae548d3Szrj   switch (fde_encoding & 0x70)
270*fae548d3Szrj     {
271*fae548d3Szrj     case 0:
272*fae548d3Szrj       break;
273*fae548d3Szrj 
274*fae548d3Szrj     case elfcpp::DW_EH_PE_pcrel:
275*fae548d3Szrj       pc += eh_frame_address + fde_offset + 8;
276*fae548d3Szrj       break;
277*fae548d3Szrj 
278*fae548d3Szrj     case elfcpp::DW_EH_PE_datarel:
279*fae548d3Szrj       pc += parameters->target().ehframe_datarel_base();
280*fae548d3Szrj       break;
281*fae548d3Szrj 
282*fae548d3Szrj     default:
283*fae548d3Szrj       // If other cases arise, then we have to handle them, or we have
284*fae548d3Szrj       // to reject them by returning false in Eh_frame::read_cie.
285*fae548d3Szrj       gold_unreachable();
286*fae548d3Szrj     }
287*fae548d3Szrj 
288*fae548d3Szrj   gold_assert((fde_encoding & elfcpp::DW_EH_PE_indirect) == 0);
289*fae548d3Szrj 
290*fae548d3Szrj   return pc;
291*fae548d3Szrj }
292*fae548d3Szrj 
293*fae548d3Szrj // Given an array of FDE offsets in the .eh_frame section, return an
294*fae548d3Szrj // array of offsets from the exception frame header to the FDE's
295*fae548d3Szrj // output PC and to the output address of the FDE itself.  We get the
296*fae548d3Szrj // FDE's PC by actually looking in the .eh_frame section we just wrote
297*fae548d3Szrj // to the output file.
298*fae548d3Szrj 
299*fae548d3Szrj template<int size, bool big_endian>
300*fae548d3Szrj void
get_fde_addresses(Output_file * of,const Fde_offsets * fde_offsets,Fde_addresses<size> * fde_addresses)301*fae548d3Szrj Eh_frame_hdr::get_fde_addresses(Output_file* of,
302*fae548d3Szrj 				const Fde_offsets* fde_offsets,
303*fae548d3Szrj 				Fde_addresses<size>* fde_addresses)
304*fae548d3Szrj {
305*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_Addr eh_frame_address;
306*fae548d3Szrj   eh_frame_address = this->eh_frame_section_->address();
307*fae548d3Szrj   off_t eh_frame_offset = this->eh_frame_section_->offset();
308*fae548d3Szrj   off_t eh_frame_size = this->eh_frame_section_->data_size();
309*fae548d3Szrj   const unsigned char* eh_frame_contents = of->get_input_view(eh_frame_offset,
310*fae548d3Szrj 							      eh_frame_size);
311*fae548d3Szrj 
312*fae548d3Szrj   for (Fde_offsets::const_iterator p = fde_offsets->begin();
313*fae548d3Szrj        p != fde_offsets->end();
314*fae548d3Szrj        ++p)
315*fae548d3Szrj     {
316*fae548d3Szrj       typename elfcpp::Elf_types<size>::Elf_Addr fde_pc;
317*fae548d3Szrj       fde_pc = this->get_fde_pc<size, big_endian>(eh_frame_address,
318*fae548d3Szrj 						  eh_frame_contents,
319*fae548d3Szrj 						  p->first, p->second);
320*fae548d3Szrj       fde_addresses->push_back(fde_pc, eh_frame_address + p->first);
321*fae548d3Szrj     }
322*fae548d3Szrj 
323*fae548d3Szrj   of->free_input_view(eh_frame_offset, eh_frame_size, eh_frame_contents);
324*fae548d3Szrj }
325*fae548d3Szrj 
326*fae548d3Szrj // Class Fde.
327*fae548d3Szrj 
328*fae548d3Szrj // Write the FDE to OVIEW starting at OFFSET.  CIE_OFFSET is the
329*fae548d3Szrj // offset of the CIE in OVIEW.  OUTPUT_OFFSET is the offset of the
330*fae548d3Szrj // Eh_frame section within the output section.  FDE_ENCODING is the
331*fae548d3Szrj // encoding, from the CIE.  ADDRALIGN is the required alignment.
332*fae548d3Szrj // ADDRESS is the virtual address of OVIEW.  Record the FDE pc for
333*fae548d3Szrj // EH_FRAME_HDR.  Return the new offset.
334*fae548d3Szrj 
335*fae548d3Szrj template<int size, bool big_endian>
336*fae548d3Szrj section_offset_type
write(unsigned char * oview,section_offset_type output_offset,section_offset_type offset,uint64_t address,unsigned int addralign,section_offset_type cie_offset,unsigned char fde_encoding,Eh_frame_hdr * eh_frame_hdr)337*fae548d3Szrj Fde::write(unsigned char* oview, section_offset_type output_offset,
338*fae548d3Szrj 	   section_offset_type offset, uint64_t address, unsigned int addralign,
339*fae548d3Szrj 	   section_offset_type cie_offset, unsigned char fde_encoding,
340*fae548d3Szrj 	   Eh_frame_hdr* eh_frame_hdr)
341*fae548d3Szrj {
342*fae548d3Szrj   gold_assert((offset & (addralign - 1)) == 0);
343*fae548d3Szrj 
344*fae548d3Szrj   size_t length = this->contents_.length();
345*fae548d3Szrj 
346*fae548d3Szrj   // We add 8 when getting the aligned length to account for the
347*fae548d3Szrj   // length word and the CIE offset.
348*fae548d3Szrj   size_t aligned_full_length = align_address(length + 8, addralign);
349*fae548d3Szrj 
350*fae548d3Szrj   // Write the length of the FDE as a 32-bit word.  The length word
351*fae548d3Szrj   // does not include the four bytes of the length word itself, but it
352*fae548d3Szrj   // does include the offset to the CIE.
353*fae548d3Szrj   elfcpp::Swap<32, big_endian>::writeval(oview + offset,
354*fae548d3Szrj                                          aligned_full_length - 4);
355*fae548d3Szrj 
356*fae548d3Szrj   // Write the offset to the CIE as a 32-bit word.  This is the
357*fae548d3Szrj   // difference between the address of the offset word itself and the
358*fae548d3Szrj   // CIE address.
359*fae548d3Szrj   elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4,
360*fae548d3Szrj 					 offset + 4 - cie_offset);
361*fae548d3Szrj 
362*fae548d3Szrj   // Copy the rest of the FDE.  Note that this is run before
363*fae548d3Szrj   // relocation processing is done on this section, so the relocations
364*fae548d3Szrj   // will later be applied to the FDE data.
365*fae548d3Szrj   memcpy(oview + offset + 8, this->contents_.data(), length);
366*fae548d3Szrj 
367*fae548d3Szrj   // If this FDE is associated with a PLT, fill in the PLT's address
368*fae548d3Szrj   // and size.
369*fae548d3Szrj   if (this->object_ == NULL)
370*fae548d3Szrj     {
371*fae548d3Szrj       gold_assert(memcmp(oview + offset + 8, "\0\0\0\0\0\0\0\0", 8) == 0);
372*fae548d3Szrj       uint64_t paddress;
373*fae548d3Szrj       off_t psize;
374*fae548d3Szrj       parameters->target().plt_fde_location(this->u_.from_linker.plt,
375*fae548d3Szrj 					    oview + offset + 8,
376*fae548d3Szrj 					    &paddress, &psize);
377*fae548d3Szrj       uint64_t poffset = paddress - (address + offset + 8);
378*fae548d3Szrj       int32_t spoffset = static_cast<int32_t>(poffset);
379*fae548d3Szrj       uint32_t upsize = static_cast<uint32_t>(psize);
380*fae548d3Szrj       if (static_cast<uint64_t>(static_cast<int64_t>(spoffset)) != poffset
381*fae548d3Szrj 	  || static_cast<off_t>(upsize) != psize)
382*fae548d3Szrj 	gold_warning(_("overflow in PLT unwind data; "
383*fae548d3Szrj 		       "unwinding through PLT may fail"));
384*fae548d3Szrj       elfcpp::Swap<32, big_endian>::writeval(oview + offset + 8, spoffset);
385*fae548d3Szrj       elfcpp::Swap<32, big_endian>::writeval(oview + offset + 12, upsize);
386*fae548d3Szrj     }
387*fae548d3Szrj 
388*fae548d3Szrj   if (aligned_full_length > length + 8)
389*fae548d3Szrj     memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
390*fae548d3Szrj 
391*fae548d3Szrj   // Tell the exception frame header about this FDE.
392*fae548d3Szrj   if (eh_frame_hdr != NULL)
393*fae548d3Szrj     eh_frame_hdr->record_fde(output_offset + offset, fde_encoding);
394*fae548d3Szrj 
395*fae548d3Szrj   return offset + aligned_full_length;
396*fae548d3Szrj }
397*fae548d3Szrj 
398*fae548d3Szrj // Class Cie.
399*fae548d3Szrj 
400*fae548d3Szrj // Destructor.
401*fae548d3Szrj 
~Cie()402*fae548d3Szrj Cie::~Cie()
403*fae548d3Szrj {
404*fae548d3Szrj   for (std::vector<Fde*>::iterator p = this->fdes_.begin();
405*fae548d3Szrj        p != this->fdes_.end();
406*fae548d3Szrj        ++p)
407*fae548d3Szrj     delete *p;
408*fae548d3Szrj }
409*fae548d3Szrj 
410*fae548d3Szrj // Set the output offset of a CIE.  Return the new output offset.
411*fae548d3Szrj 
412*fae548d3Szrj section_offset_type
set_output_offset(section_offset_type output_offset,unsigned int addralign,Output_section_data * output_data)413*fae548d3Szrj Cie::set_output_offset(section_offset_type output_offset,
414*fae548d3Szrj 		       unsigned int addralign,
415*fae548d3Szrj 		       Output_section_data *output_data)
416*fae548d3Szrj {
417*fae548d3Szrj   size_t length = this->contents_.length();
418*fae548d3Szrj 
419*fae548d3Szrj   // Add 4 for length and 4 for zero CIE identifier tag.
420*fae548d3Szrj   length += 8;
421*fae548d3Szrj 
422*fae548d3Szrj   if (this->object_ != NULL)
423*fae548d3Szrj     {
424*fae548d3Szrj       // Add a mapping so that relocations are applied correctly.
425*fae548d3Szrj       this->object_->add_merge_mapping(output_data, this->shndx_,
426*fae548d3Szrj                                        this->input_offset_, length,
427*fae548d3Szrj                                        output_offset);
428*fae548d3Szrj     }
429*fae548d3Szrj 
430*fae548d3Szrj   length = align_address(length, addralign);
431*fae548d3Szrj 
432*fae548d3Szrj   for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
433*fae548d3Szrj        p != this->fdes_.end();
434*fae548d3Szrj        ++p)
435*fae548d3Szrj     {
436*fae548d3Szrj       (*p)->add_mapping(output_offset + length, output_data);
437*fae548d3Szrj 
438*fae548d3Szrj       size_t fde_length = (*p)->length();
439*fae548d3Szrj       fde_length = align_address(fde_length, addralign);
440*fae548d3Szrj       length += fde_length;
441*fae548d3Szrj     }
442*fae548d3Szrj 
443*fae548d3Szrj   return output_offset + length;
444*fae548d3Szrj }
445*fae548d3Szrj 
446*fae548d3Szrj // Write the CIE to OVIEW starting at OFFSET.  OUTPUT_OFFSET is the
447*fae548d3Szrj // offset of the Eh_frame section within the output section.  Round up
448*fae548d3Szrj // the bytes to ADDRALIGN.  ADDRESS is the virtual address of OVIEW.
449*fae548d3Szrj // EH_FRAME_HDR is the exception frame header for FDE recording.
450*fae548d3Szrj // POST_FDES stashes FDEs created after mappings were done, for later
451*fae548d3Szrj // writing.  Return the new offset.
452*fae548d3Szrj 
453*fae548d3Szrj template<int size, bool big_endian>
454*fae548d3Szrj section_offset_type
write(unsigned char * oview,section_offset_type output_offset,section_offset_type offset,uint64_t address,unsigned int addralign,Eh_frame_hdr * eh_frame_hdr,Post_fdes * post_fdes)455*fae548d3Szrj Cie::write(unsigned char* oview, section_offset_type output_offset,
456*fae548d3Szrj 	   section_offset_type offset, uint64_t address,
457*fae548d3Szrj 	   unsigned int addralign, Eh_frame_hdr* eh_frame_hdr,
458*fae548d3Szrj 	   Post_fdes* post_fdes)
459*fae548d3Szrj {
460*fae548d3Szrj   gold_assert((offset & (addralign - 1)) == 0);
461*fae548d3Szrj 
462*fae548d3Szrj   section_offset_type cie_offset = offset;
463*fae548d3Szrj 
464*fae548d3Szrj   size_t length = this->contents_.length();
465*fae548d3Szrj 
466*fae548d3Szrj   // We add 8 when getting the aligned length to account for the
467*fae548d3Szrj   // length word and the CIE tag.
468*fae548d3Szrj   size_t aligned_full_length = align_address(length + 8, addralign);
469*fae548d3Szrj 
470*fae548d3Szrj   // Write the length of the CIE as a 32-bit word.  The length word
471*fae548d3Szrj   // does not include the four bytes of the length word itself.
472*fae548d3Szrj   elfcpp::Swap<32, big_endian>::writeval(oview + offset,
473*fae548d3Szrj                                          aligned_full_length - 4);
474*fae548d3Szrj 
475*fae548d3Szrj   // Write the tag which marks this as a CIE: a 32-bit zero.
476*fae548d3Szrj   elfcpp::Swap<32, big_endian>::writeval(oview + offset + 4, 0);
477*fae548d3Szrj 
478*fae548d3Szrj   // Write out the CIE data.
479*fae548d3Szrj   memcpy(oview + offset + 8, this->contents_.data(), length);
480*fae548d3Szrj 
481*fae548d3Szrj   if (aligned_full_length > length + 8)
482*fae548d3Szrj     memset(oview + offset + length + 8, 0, aligned_full_length - (length + 8));
483*fae548d3Szrj 
484*fae548d3Szrj   offset += aligned_full_length;
485*fae548d3Szrj 
486*fae548d3Szrj   // Write out the associated FDEs.
487*fae548d3Szrj   unsigned char fde_encoding = this->fde_encoding_;
488*fae548d3Szrj   for (std::vector<Fde*>::const_iterator p = this->fdes_.begin();
489*fae548d3Szrj        p != this->fdes_.end();
490*fae548d3Szrj        ++p)
491*fae548d3Szrj     {
492*fae548d3Szrj       if ((*p)->post_map())
493*fae548d3Szrj 	post_fdes->push_back(Post_fde(*p, cie_offset, fde_encoding));
494*fae548d3Szrj       else
495*fae548d3Szrj 	offset = (*p)->write<size, big_endian>(oview, output_offset, offset,
496*fae548d3Szrj 					       address, addralign, cie_offset,
497*fae548d3Szrj 					       fde_encoding, eh_frame_hdr);
498*fae548d3Szrj     }
499*fae548d3Szrj 
500*fae548d3Szrj   return offset;
501*fae548d3Szrj }
502*fae548d3Szrj 
503*fae548d3Szrj // We track all the CIEs we see, and merge them when possible.  This
504*fae548d3Szrj // works because each FDE holds an offset to the relevant CIE: we
505*fae548d3Szrj // rewrite the FDEs to point to the merged CIE.  This is worthwhile
506*fae548d3Szrj // because in a typical C++ program many FDEs in many different object
507*fae548d3Szrj // files will use the same CIE.
508*fae548d3Szrj 
509*fae548d3Szrj // An equality operator for Cie.
510*fae548d3Szrj 
511*fae548d3Szrj bool
operator ==(const Cie & cie1,const Cie & cie2)512*fae548d3Szrj operator==(const Cie& cie1, const Cie& cie2)
513*fae548d3Szrj {
514*fae548d3Szrj   return (cie1.personality_name_ == cie2.personality_name_
515*fae548d3Szrj 	  && cie1.contents_ == cie2.contents_);
516*fae548d3Szrj }
517*fae548d3Szrj 
518*fae548d3Szrj // A less-than operator for Cie.
519*fae548d3Szrj 
520*fae548d3Szrj bool
operator <(const Cie & cie1,const Cie & cie2)521*fae548d3Szrj operator<(const Cie& cie1, const Cie& cie2)
522*fae548d3Szrj {
523*fae548d3Szrj   if (cie1.personality_name_ != cie2.personality_name_)
524*fae548d3Szrj     return cie1.personality_name_ < cie2.personality_name_;
525*fae548d3Szrj   return cie1.contents_ < cie2.contents_;
526*fae548d3Szrj }
527*fae548d3Szrj 
528*fae548d3Szrj // Class Eh_frame.
529*fae548d3Szrj 
Eh_frame()530*fae548d3Szrj Eh_frame::Eh_frame()
531*fae548d3Szrj   : Output_section_data(Output_data::default_alignment()),
532*fae548d3Szrj     eh_frame_hdr_(NULL),
533*fae548d3Szrj     cie_offsets_(),
534*fae548d3Szrj     unmergeable_cie_offsets_(),
535*fae548d3Szrj     mappings_are_done_(false),
536*fae548d3Szrj     final_data_size_(0)
537*fae548d3Szrj {
538*fae548d3Szrj }
539*fae548d3Szrj 
540*fae548d3Szrj // Skip an LEB128, updating *PP to point to the next character.
541*fae548d3Szrj // Return false if we ran off the end of the string.
542*fae548d3Szrj 
543*fae548d3Szrj bool
skip_leb128(const unsigned char ** pp,const unsigned char * pend)544*fae548d3Szrj Eh_frame::skip_leb128(const unsigned char** pp, const unsigned char* pend)
545*fae548d3Szrj {
546*fae548d3Szrj   const unsigned char* p;
547*fae548d3Szrj   for (p = *pp; p < pend; ++p)
548*fae548d3Szrj     {
549*fae548d3Szrj       if ((*p & 0x80) == 0)
550*fae548d3Szrj 	{
551*fae548d3Szrj 	  *pp = p + 1;
552*fae548d3Szrj 	  return true;
553*fae548d3Szrj 	}
554*fae548d3Szrj     }
555*fae548d3Szrj   return false;
556*fae548d3Szrj }
557*fae548d3Szrj 
558*fae548d3Szrj // Add input section SHNDX in OBJECT to an exception frame section.
559*fae548d3Szrj // SYMBOLS is the contents of the symbol table section (size
560*fae548d3Szrj // SYMBOLS_SIZE), SYMBOL_NAMES is the symbol names section (size
561*fae548d3Szrj // SYMBOL_NAMES_SIZE).  RELOC_SHNDX is the index of a relocation
562*fae548d3Szrj // section applying to SHNDX, or 0 if none, or -1U if more than one.
563*fae548d3Szrj // RELOC_TYPE is the type of the reloc section if there is one, either
564*fae548d3Szrj // SHT_REL or SHT_RELA.  We try to parse the input exception frame
565*fae548d3Szrj // data into our data structures.  If we can't do it, we return false
566*fae548d3Szrj // to mean that the section should be handled as a normal input
567*fae548d3Szrj // section.
568*fae548d3Szrj 
569*fae548d3Szrj template<int size, bool big_endian>
570*fae548d3Szrj Eh_frame::Eh_frame_section_disposition
add_ehframe_input_section(Sized_relobj_file<size,big_endian> * object,const unsigned char * symbols,section_size_type symbols_size,const unsigned char * symbol_names,section_size_type symbol_names_size,unsigned int shndx,unsigned int reloc_shndx,unsigned int reloc_type)571*fae548d3Szrj Eh_frame::add_ehframe_input_section(
572*fae548d3Szrj     Sized_relobj_file<size, big_endian>* object,
573*fae548d3Szrj     const unsigned char* symbols,
574*fae548d3Szrj     section_size_type symbols_size,
575*fae548d3Szrj     const unsigned char* symbol_names,
576*fae548d3Szrj     section_size_type symbol_names_size,
577*fae548d3Szrj     unsigned int shndx,
578*fae548d3Szrj     unsigned int reloc_shndx,
579*fae548d3Szrj     unsigned int reloc_type)
580*fae548d3Szrj {
581*fae548d3Szrj   // Get the section contents.
582*fae548d3Szrj   section_size_type contents_len;
583*fae548d3Szrj   const unsigned char* pcontents = object->section_contents(shndx,
584*fae548d3Szrj 							    &contents_len,
585*fae548d3Szrj 							    false);
586*fae548d3Szrj   if (contents_len == 0)
587*fae548d3Szrj     return EH_EMPTY_SECTION;
588*fae548d3Szrj 
589*fae548d3Szrj   // If this is the marker section for the end of the data, then
590*fae548d3Szrj   // return false to force it to be handled as an ordinary input
591*fae548d3Szrj   // section.  If we don't do this, we won't correctly handle the case
592*fae548d3Szrj   // of unrecognized .eh_frame sections.
593*fae548d3Szrj   if (contents_len == 4
594*fae548d3Szrj       && elfcpp::Swap<32, big_endian>::readval(pcontents) == 0)
595*fae548d3Szrj     return EH_END_MARKER_SECTION;
596*fae548d3Szrj 
597*fae548d3Szrj   New_cies new_cies;
598*fae548d3Szrj   if (!this->do_add_ehframe_input_section(object, symbols, symbols_size,
599*fae548d3Szrj 					  symbol_names, symbol_names_size,
600*fae548d3Szrj 					  shndx, reloc_shndx,
601*fae548d3Szrj 					  reloc_type, pcontents,
602*fae548d3Szrj 					  contents_len, &new_cies))
603*fae548d3Szrj     {
604*fae548d3Szrj       if (this->eh_frame_hdr_ != NULL)
605*fae548d3Szrj 	this->eh_frame_hdr_->found_unrecognized_eh_frame_section();
606*fae548d3Szrj 
607*fae548d3Szrj       for (New_cies::iterator p = new_cies.begin();
608*fae548d3Szrj 	   p != new_cies.end();
609*fae548d3Szrj 	   ++p)
610*fae548d3Szrj 	delete p->first;
611*fae548d3Szrj 
612*fae548d3Szrj       return EH_UNRECOGNIZED_SECTION;
613*fae548d3Szrj     }
614*fae548d3Szrj 
615*fae548d3Szrj   // Now that we know we are using this section, record any new CIEs
616*fae548d3Szrj   // that we found.
617*fae548d3Szrj   for (New_cies::const_iterator p = new_cies.begin();
618*fae548d3Szrj        p != new_cies.end();
619*fae548d3Szrj        ++p)
620*fae548d3Szrj     {
621*fae548d3Szrj       if (p->second)
622*fae548d3Szrj 	this->cie_offsets_.insert(p->first);
623*fae548d3Szrj       else
624*fae548d3Szrj 	this->unmergeable_cie_offsets_.push_back(p->first);
625*fae548d3Szrj     }
626*fae548d3Szrj 
627*fae548d3Szrj   return EH_OPTIMIZABLE_SECTION;
628*fae548d3Szrj }
629*fae548d3Szrj 
630*fae548d3Szrj // The bulk of the implementation of add_ehframe_input_section.
631*fae548d3Szrj 
632*fae548d3Szrj template<int size, bool big_endian>
633*fae548d3Szrj bool
do_add_ehframe_input_section(Sized_relobj_file<size,big_endian> * object,const unsigned char * symbols,section_size_type symbols_size,const unsigned char * symbol_names,section_size_type symbol_names_size,unsigned int shndx,unsigned int reloc_shndx,unsigned int reloc_type,const unsigned char * pcontents,section_size_type contents_len,New_cies * new_cies)634*fae548d3Szrj Eh_frame::do_add_ehframe_input_section(
635*fae548d3Szrj     Sized_relobj_file<size, big_endian>* object,
636*fae548d3Szrj     const unsigned char* symbols,
637*fae548d3Szrj     section_size_type symbols_size,
638*fae548d3Szrj     const unsigned char* symbol_names,
639*fae548d3Szrj     section_size_type symbol_names_size,
640*fae548d3Szrj     unsigned int shndx,
641*fae548d3Szrj     unsigned int reloc_shndx,
642*fae548d3Szrj     unsigned int reloc_type,
643*fae548d3Szrj     const unsigned char* pcontents,
644*fae548d3Szrj     section_size_type contents_len,
645*fae548d3Szrj     New_cies* new_cies)
646*fae548d3Szrj {
647*fae548d3Szrj   Track_relocs<size, big_endian> relocs;
648*fae548d3Szrj 
649*fae548d3Szrj   const unsigned char* p = pcontents;
650*fae548d3Szrj   const unsigned char* pend = p + contents_len;
651*fae548d3Szrj 
652*fae548d3Szrj   // Get the contents of the reloc section if any.
653*fae548d3Szrj   if (!relocs.initialize(object, reloc_shndx, reloc_type))
654*fae548d3Szrj     return false;
655*fae548d3Szrj 
656*fae548d3Szrj   // Keep track of which CIEs are at which offsets.
657*fae548d3Szrj   Offsets_to_cie cies;
658*fae548d3Szrj 
659*fae548d3Szrj   while (p < pend)
660*fae548d3Szrj     {
661*fae548d3Szrj       if (pend - p < 4)
662*fae548d3Szrj 	return false;
663*fae548d3Szrj 
664*fae548d3Szrj       // There shouldn't be any relocations here.
665*fae548d3Szrj       if (relocs.advance(p + 4 - pcontents) > 0)
666*fae548d3Szrj 	return false;
667*fae548d3Szrj 
668*fae548d3Szrj       unsigned int len = elfcpp::Swap<32, big_endian>::readval(p);
669*fae548d3Szrj       p += 4;
670*fae548d3Szrj       if (len == 0)
671*fae548d3Szrj 	{
672*fae548d3Szrj 	  // We should only find a zero-length entry at the end of the
673*fae548d3Szrj 	  // section.
674*fae548d3Szrj 	  if (p < pend)
675*fae548d3Szrj 	    return false;
676*fae548d3Szrj 	  break;
677*fae548d3Szrj 	}
678*fae548d3Szrj       // We don't support a 64-bit .eh_frame.
679*fae548d3Szrj       if (len == 0xffffffff)
680*fae548d3Szrj 	return false;
681*fae548d3Szrj       if (static_cast<unsigned int>(pend - p) < len)
682*fae548d3Szrj 	return false;
683*fae548d3Szrj 
684*fae548d3Szrj       const unsigned char* const pentend = p + len;
685*fae548d3Szrj 
686*fae548d3Szrj       if (pend - p < 4)
687*fae548d3Szrj 	return false;
688*fae548d3Szrj       if (relocs.advance(p + 4 - pcontents) > 0)
689*fae548d3Szrj 	return false;
690*fae548d3Szrj 
691*fae548d3Szrj       unsigned int id = elfcpp::Swap<32, big_endian>::readval(p);
692*fae548d3Szrj       p += 4;
693*fae548d3Szrj 
694*fae548d3Szrj       if (id == 0)
695*fae548d3Szrj 	{
696*fae548d3Szrj 	  // CIE.
697*fae548d3Szrj 	  if (!this->read_cie(object, shndx, symbols, symbols_size,
698*fae548d3Szrj 			      symbol_names, symbol_names_size,
699*fae548d3Szrj 			      pcontents, p, pentend, &relocs, &cies,
700*fae548d3Szrj 			      new_cies))
701*fae548d3Szrj 	    return false;
702*fae548d3Szrj 	}
703*fae548d3Szrj       else
704*fae548d3Szrj 	{
705*fae548d3Szrj 	  // FDE.
706*fae548d3Szrj 	  if (!this->read_fde(object, shndx, symbols, symbols_size,
707*fae548d3Szrj 			      pcontents, id, p, pentend, &relocs, &cies))
708*fae548d3Szrj 	    return false;
709*fae548d3Szrj 	}
710*fae548d3Szrj 
711*fae548d3Szrj       p = pentend;
712*fae548d3Szrj     }
713*fae548d3Szrj 
714*fae548d3Szrj   return true;
715*fae548d3Szrj }
716*fae548d3Szrj 
717*fae548d3Szrj // Read a CIE.  Return false if we can't parse the information.
718*fae548d3Szrj 
719*fae548d3Szrj template<int size, bool big_endian>
720*fae548d3Szrj bool
read_cie(Sized_relobj_file<size,big_endian> * object,unsigned int shndx,const unsigned char * symbols,section_size_type symbols_size,const unsigned char * symbol_names,section_size_type symbol_names_size,const unsigned char * pcontents,const unsigned char * pcie,const unsigned char * pcieend,Track_relocs<size,big_endian> * relocs,Offsets_to_cie * cies,New_cies * new_cies)721*fae548d3Szrj Eh_frame::read_cie(Sized_relobj_file<size, big_endian>* object,
722*fae548d3Szrj 		   unsigned int shndx,
723*fae548d3Szrj 		   const unsigned char* symbols,
724*fae548d3Szrj 		   section_size_type symbols_size,
725*fae548d3Szrj 		   const unsigned char* symbol_names,
726*fae548d3Szrj 		   section_size_type symbol_names_size,
727*fae548d3Szrj 		   const unsigned char* pcontents,
728*fae548d3Szrj 		   const unsigned char* pcie,
729*fae548d3Szrj 		   const unsigned char* pcieend,
730*fae548d3Szrj 		   Track_relocs<size, big_endian>* relocs,
731*fae548d3Szrj 		   Offsets_to_cie* cies,
732*fae548d3Szrj 		   New_cies* new_cies)
733*fae548d3Szrj {
734*fae548d3Szrj   bool mergeable = true;
735*fae548d3Szrj 
736*fae548d3Szrj   // We need to find the personality routine if there is one, since we
737*fae548d3Szrj   // can only merge CIEs which use the same routine.  We also need to
738*fae548d3Szrj   // find the FDE encoding if there is one, so that we can read the PC
739*fae548d3Szrj   // from the FDE.
740*fae548d3Szrj 
741*fae548d3Szrj   const unsigned char* p = pcie;
742*fae548d3Szrj 
743*fae548d3Szrj   if (pcieend - p < 1)
744*fae548d3Szrj     return false;
745*fae548d3Szrj   unsigned char version = *p++;
746*fae548d3Szrj   if (version != 1 && version != 3)
747*fae548d3Szrj     return false;
748*fae548d3Szrj 
749*fae548d3Szrj   const unsigned char* paug = p;
750*fae548d3Szrj   const void* paugendv = memchr(p, '\0', pcieend - p);
751*fae548d3Szrj   const unsigned char* paugend = static_cast<const unsigned char*>(paugendv);
752*fae548d3Szrj   if (paugend == NULL)
753*fae548d3Szrj     return false;
754*fae548d3Szrj   p = paugend + 1;
755*fae548d3Szrj 
756*fae548d3Szrj   if (paug[0] == 'e' && paug[1] == 'h')
757*fae548d3Szrj     {
758*fae548d3Szrj       // This is a CIE from gcc before version 3.0.  We can't merge
759*fae548d3Szrj       // these.  We can still read the FDEs.
760*fae548d3Szrj       mergeable = false;
761*fae548d3Szrj       paug += 2;
762*fae548d3Szrj       if (*paug != '\0')
763*fae548d3Szrj 	return false;
764*fae548d3Szrj       if (pcieend - p < size / 8)
765*fae548d3Szrj 	return false;
766*fae548d3Szrj       p += size / 8;
767*fae548d3Szrj     }
768*fae548d3Szrj 
769*fae548d3Szrj   // Skip the code alignment.
770*fae548d3Szrj   if (!skip_leb128(&p, pcieend))
771*fae548d3Szrj     return false;
772*fae548d3Szrj 
773*fae548d3Szrj   // Skip the data alignment.
774*fae548d3Szrj   if (!skip_leb128(&p, pcieend))
775*fae548d3Szrj     return false;
776*fae548d3Szrj 
777*fae548d3Szrj   // Skip the return column.
778*fae548d3Szrj   if (version == 1)
779*fae548d3Szrj     {
780*fae548d3Szrj       if (pcieend - p < 1)
781*fae548d3Szrj 	return false;
782*fae548d3Szrj       ++p;
783*fae548d3Szrj     }
784*fae548d3Szrj   else
785*fae548d3Szrj     {
786*fae548d3Szrj       if (!skip_leb128(&p, pcieend))
787*fae548d3Szrj 	return false;
788*fae548d3Szrj     }
789*fae548d3Szrj 
790*fae548d3Szrj   if (*paug == 'z')
791*fae548d3Szrj     {
792*fae548d3Szrj       ++paug;
793*fae548d3Szrj       // Skip the augmentation size.
794*fae548d3Szrj       if (!skip_leb128(&p, pcieend))
795*fae548d3Szrj 	return false;
796*fae548d3Szrj     }
797*fae548d3Szrj 
798*fae548d3Szrj   unsigned char fde_encoding = elfcpp::DW_EH_PE_absptr;
799*fae548d3Szrj   int per_offset = -1;
800*fae548d3Szrj   while (*paug != '\0')
801*fae548d3Szrj     {
802*fae548d3Szrj       switch (*paug)
803*fae548d3Szrj 	{
804*fae548d3Szrj 	case 'L': // LSDA encoding.
805*fae548d3Szrj 	  if (pcieend - p < 1)
806*fae548d3Szrj 	    return false;
807*fae548d3Szrj 	  ++p;
808*fae548d3Szrj 	  break;
809*fae548d3Szrj 
810*fae548d3Szrj 	case 'R': // FDE encoding.
811*fae548d3Szrj 	  if (pcieend - p < 1)
812*fae548d3Szrj 	    return false;
813*fae548d3Szrj 	  fde_encoding = *p;
814*fae548d3Szrj 	  switch (fde_encoding & 7)
815*fae548d3Szrj 	    {
816*fae548d3Szrj 	    case elfcpp::DW_EH_PE_absptr:
817*fae548d3Szrj 	    case elfcpp::DW_EH_PE_udata2:
818*fae548d3Szrj 	    case elfcpp::DW_EH_PE_udata4:
819*fae548d3Szrj 	    case elfcpp::DW_EH_PE_udata8:
820*fae548d3Szrj 	      break;
821*fae548d3Szrj 	    default:
822*fae548d3Szrj 	      // We don't expect to see any other cases here, and
823*fae548d3Szrj 	      // we're not prepared to handle them.
824*fae548d3Szrj 	      return false;
825*fae548d3Szrj 	    }
826*fae548d3Szrj 	  ++p;
827*fae548d3Szrj 	  break;
828*fae548d3Szrj 
829*fae548d3Szrj 	case 'S':
830*fae548d3Szrj 	  break;
831*fae548d3Szrj 
832*fae548d3Szrj 	case 'P':
833*fae548d3Szrj 	  // Personality encoding.
834*fae548d3Szrj 	  {
835*fae548d3Szrj 	    if (pcieend - p < 1)
836*fae548d3Szrj 	      return false;
837*fae548d3Szrj 	    unsigned char per_encoding = *p;
838*fae548d3Szrj 	    ++p;
839*fae548d3Szrj 
840*fae548d3Szrj 	    if ((per_encoding & 0x60) == 0x60)
841*fae548d3Szrj 	      return false;
842*fae548d3Szrj 	    unsigned int per_width;
843*fae548d3Szrj 	    switch (per_encoding & 7)
844*fae548d3Szrj 	      {
845*fae548d3Szrj 	      case elfcpp::DW_EH_PE_udata2:
846*fae548d3Szrj 		per_width = 2;
847*fae548d3Szrj 		break;
848*fae548d3Szrj 	      case elfcpp::DW_EH_PE_udata4:
849*fae548d3Szrj 		per_width = 4;
850*fae548d3Szrj 		break;
851*fae548d3Szrj 	      case elfcpp::DW_EH_PE_udata8:
852*fae548d3Szrj 		per_width = 8;
853*fae548d3Szrj 		break;
854*fae548d3Szrj 	      case elfcpp::DW_EH_PE_absptr:
855*fae548d3Szrj 		per_width = size / 8;
856*fae548d3Szrj 		break;
857*fae548d3Szrj 	      default:
858*fae548d3Szrj 		return false;
859*fae548d3Szrj 	      }
860*fae548d3Szrj 
861*fae548d3Szrj 	    if ((per_encoding & 0xf0) == elfcpp::DW_EH_PE_aligned)
862*fae548d3Szrj 	      {
863*fae548d3Szrj 		unsigned int len = p - pcie;
864*fae548d3Szrj 		len += per_width - 1;
865*fae548d3Szrj 		len &= ~ (per_width - 1);
866*fae548d3Szrj 		if (static_cast<unsigned int>(pcieend - p) < len)
867*fae548d3Szrj 		  return false;
868*fae548d3Szrj 		p += len;
869*fae548d3Szrj 	      }
870*fae548d3Szrj 
871*fae548d3Szrj 	    per_offset = p - pcontents;
872*fae548d3Szrj 
873*fae548d3Szrj 	    if (static_cast<unsigned int>(pcieend - p) < per_width)
874*fae548d3Szrj 	      return false;
875*fae548d3Szrj 	    p += per_width;
876*fae548d3Szrj 	  }
877*fae548d3Szrj 	  break;
878*fae548d3Szrj 
879*fae548d3Szrj 	default:
880*fae548d3Szrj 	  return false;
881*fae548d3Szrj 	}
882*fae548d3Szrj 
883*fae548d3Szrj       ++paug;
884*fae548d3Szrj     }
885*fae548d3Szrj 
886*fae548d3Szrj   const char* personality_name = "";
887*fae548d3Szrj   if (per_offset != -1)
888*fae548d3Szrj     {
889*fae548d3Szrj       if (relocs->advance(per_offset) > 0)
890*fae548d3Szrj 	return false;
891*fae548d3Szrj       if (relocs->next_offset() != per_offset)
892*fae548d3Szrj 	return false;
893*fae548d3Szrj 
894*fae548d3Szrj       unsigned int personality_symndx = relocs->next_symndx();
895*fae548d3Szrj       if (personality_symndx == -1U)
896*fae548d3Szrj 	return false;
897*fae548d3Szrj 
898*fae548d3Szrj       if (personality_symndx < object->local_symbol_count())
899*fae548d3Szrj 	{
900*fae548d3Szrj 	  // We can only merge this CIE if the personality routine is
901*fae548d3Szrj 	  // a global symbol.  We can still read the FDEs.
902*fae548d3Szrj 	  mergeable = false;
903*fae548d3Szrj 	}
904*fae548d3Szrj       else
905*fae548d3Szrj 	{
906*fae548d3Szrj 	  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
907*fae548d3Szrj 	  if (personality_symndx >= symbols_size / sym_size)
908*fae548d3Szrj 	    return false;
909*fae548d3Szrj 	  elfcpp::Sym<size, big_endian> sym(symbols
910*fae548d3Szrj 					    + (personality_symndx * sym_size));
911*fae548d3Szrj 	  unsigned int name_offset = sym.get_st_name();
912*fae548d3Szrj 	  if (name_offset >= symbol_names_size)
913*fae548d3Szrj 	    return false;
914*fae548d3Szrj 	  personality_name = (reinterpret_cast<const char*>(symbol_names)
915*fae548d3Szrj 			      + name_offset);
916*fae548d3Szrj 	}
917*fae548d3Szrj 
918*fae548d3Szrj       int r = relocs->advance(per_offset + 1);
919*fae548d3Szrj       gold_assert(r == 1);
920*fae548d3Szrj     }
921*fae548d3Szrj 
922*fae548d3Szrj   if (relocs->advance(pcieend - pcontents) > 0)
923*fae548d3Szrj     return false;
924*fae548d3Szrj 
925*fae548d3Szrj   Cie cie(object, shndx, (pcie - 8) - pcontents, fde_encoding,
926*fae548d3Szrj 	  personality_name, pcie, pcieend - pcie);
927*fae548d3Szrj   Cie* cie_pointer = NULL;
928*fae548d3Szrj   if (mergeable)
929*fae548d3Szrj     {
930*fae548d3Szrj       Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
931*fae548d3Szrj       if (find_cie != this->cie_offsets_.end())
932*fae548d3Szrj 	cie_pointer = *find_cie;
933*fae548d3Szrj       else
934*fae548d3Szrj 	{
935*fae548d3Szrj 	  // See if we already saw this CIE in this object file.
936*fae548d3Szrj 	  for (New_cies::const_iterator pc = new_cies->begin();
937*fae548d3Szrj 	       pc != new_cies->end();
938*fae548d3Szrj 	       ++pc)
939*fae548d3Szrj 	    {
940*fae548d3Szrj 	      if (*(pc->first) == cie)
941*fae548d3Szrj 		{
942*fae548d3Szrj 		  cie_pointer = pc->first;
943*fae548d3Szrj 		  break;
944*fae548d3Szrj 		}
945*fae548d3Szrj 	    }
946*fae548d3Szrj 	}
947*fae548d3Szrj     }
948*fae548d3Szrj 
949*fae548d3Szrj   if (cie_pointer == NULL)
950*fae548d3Szrj     {
951*fae548d3Szrj       cie_pointer = new Cie(cie);
952*fae548d3Szrj       new_cies->push_back(std::make_pair(cie_pointer, mergeable));
953*fae548d3Szrj     }
954*fae548d3Szrj   else
955*fae548d3Szrj     {
956*fae548d3Szrj       // We are deleting this CIE.  Record that in our mapping from
957*fae548d3Szrj       // input sections to the output section.  At this point we don't
958*fae548d3Szrj       // know for sure that we are doing a special mapping for this
959*fae548d3Szrj       // input section, but that's OK--if we don't do a special
960*fae548d3Szrj       // mapping, nobody will ever ask for the mapping we add here.
961*fae548d3Szrj       object->add_merge_mapping(this, shndx, (pcie - 8) - pcontents,
962*fae548d3Szrj                                 pcieend - (pcie - 8), -1);
963*fae548d3Szrj     }
964*fae548d3Szrj 
965*fae548d3Szrj   // Record this CIE plus the offset in the input section.
966*fae548d3Szrj   cies->insert(std::make_pair(pcie - pcontents, cie_pointer));
967*fae548d3Szrj 
968*fae548d3Szrj   return true;
969*fae548d3Szrj }
970*fae548d3Szrj 
971*fae548d3Szrj // Read an FDE.  Return false if we can't parse the information.
972*fae548d3Szrj 
973*fae548d3Szrj template<int size, bool big_endian>
974*fae548d3Szrj bool
read_fde(Sized_relobj_file<size,big_endian> * object,unsigned int shndx,const unsigned char * symbols,section_size_type symbols_size,const unsigned char * pcontents,unsigned int offset,const unsigned char * pfde,const unsigned char * pfdeend,Track_relocs<size,big_endian> * relocs,Offsets_to_cie * cies)975*fae548d3Szrj Eh_frame::read_fde(Sized_relobj_file<size, big_endian>* object,
976*fae548d3Szrj 		   unsigned int shndx,
977*fae548d3Szrj 		   const unsigned char* symbols,
978*fae548d3Szrj 		   section_size_type symbols_size,
979*fae548d3Szrj 		   const unsigned char* pcontents,
980*fae548d3Szrj 		   unsigned int offset,
981*fae548d3Szrj 		   const unsigned char* pfde,
982*fae548d3Szrj 		   const unsigned char* pfdeend,
983*fae548d3Szrj 		   Track_relocs<size, big_endian>* relocs,
984*fae548d3Szrj 		   Offsets_to_cie* cies)
985*fae548d3Szrj {
986*fae548d3Szrj   // OFFSET is the distance between the 4 bytes before PFDE to the
987*fae548d3Szrj   // start of the CIE.  The offset we recorded for the CIE is 8 bytes
988*fae548d3Szrj   // after the start of the CIE--after the length and the zero tag.
989*fae548d3Szrj   unsigned int cie_offset = (pfde - 4 - pcontents) - offset + 8;
990*fae548d3Szrj   Offsets_to_cie::const_iterator pcie = cies->find(cie_offset);
991*fae548d3Szrj   if (pcie == cies->end())
992*fae548d3Szrj     return false;
993*fae548d3Szrj   Cie* cie = pcie->second;
994*fae548d3Szrj 
995*fae548d3Szrj   int pc_size = 0;
996*fae548d3Szrj   switch (cie->fde_encoding() & 7)
997*fae548d3Szrj     {
998*fae548d3Szrj     case elfcpp::DW_EH_PE_udata2:
999*fae548d3Szrj       pc_size = 2;
1000*fae548d3Szrj       break;
1001*fae548d3Szrj     case elfcpp::DW_EH_PE_udata4:
1002*fae548d3Szrj       pc_size = 4;
1003*fae548d3Szrj       break;
1004*fae548d3Szrj     case elfcpp::DW_EH_PE_udata8:
1005*fae548d3Szrj       gold_assert(size == 64);
1006*fae548d3Szrj       pc_size = 8;
1007*fae548d3Szrj       break;
1008*fae548d3Szrj     case elfcpp::DW_EH_PE_absptr:
1009*fae548d3Szrj       pc_size = size == 32 ? 4 : 8;
1010*fae548d3Szrj       break;
1011*fae548d3Szrj     default:
1012*fae548d3Szrj       // All other cases were rejected in Eh_frame::read_cie.
1013*fae548d3Szrj       gold_unreachable();
1014*fae548d3Szrj     }
1015*fae548d3Szrj 
1016*fae548d3Szrj   // The FDE should start with a reloc to the start of the code which
1017*fae548d3Szrj   // it describes.
1018*fae548d3Szrj   if (relocs->advance(pfde - pcontents) > 0)
1019*fae548d3Szrj     return false;
1020*fae548d3Szrj   if (relocs->next_offset() != pfde - pcontents)
1021*fae548d3Szrj     {
1022*fae548d3Szrj       // In an object produced by a relocatable link, gold may have
1023*fae548d3Szrj       // discarded a COMDAT group in the previous link, but not the
1024*fae548d3Szrj       // corresponding FDEs. In that case, gold will have discarded
1025*fae548d3Szrj       // the relocations, so the FDE will have a non-relocatable zero
1026*fae548d3Szrj       // (regardless of whether the PC encoding is absolute, pc-relative,
1027*fae548d3Szrj       // or data-relative) instead of a pointer to the start of the code.
1028*fae548d3Szrj 
1029*fae548d3Szrj       uint64_t pc_value = 0;
1030*fae548d3Szrj       switch (pc_size)
1031*fae548d3Szrj 	{
1032*fae548d3Szrj 	case 2:
1033*fae548d3Szrj 	  pc_value = elfcpp::Swap<16, big_endian>::readval(pfde);
1034*fae548d3Szrj 	  break;
1035*fae548d3Szrj 	case 4:
1036*fae548d3Szrj 	  pc_value = elfcpp::Swap<32, big_endian>::readval(pfde);
1037*fae548d3Szrj 	  break;
1038*fae548d3Szrj 	case 8:
1039*fae548d3Szrj 	  pc_value = elfcpp::Swap_unaligned<64, big_endian>::readval(pfde);
1040*fae548d3Szrj 	  break;
1041*fae548d3Szrj 	default:
1042*fae548d3Szrj 	  gold_unreachable();
1043*fae548d3Szrj 	}
1044*fae548d3Szrj 
1045*fae548d3Szrj       if (pc_value == 0)
1046*fae548d3Szrj 	{
1047*fae548d3Szrj 	  // This FDE applies to a discarded function.  We
1048*fae548d3Szrj 	  // can discard this FDE.
1049*fae548d3Szrj 	  object->add_merge_mapping(this, shndx, (pfde - 8) - pcontents,
1050*fae548d3Szrj 				    pfdeend - (pfde - 8), -1);
1051*fae548d3Szrj 	  return true;
1052*fae548d3Szrj 	}
1053*fae548d3Szrj 
1054*fae548d3Szrj       // Otherwise, reject the FDE.
1055*fae548d3Szrj       return false;
1056*fae548d3Szrj     }
1057*fae548d3Szrj 
1058*fae548d3Szrj   unsigned int symndx = relocs->next_symndx();
1059*fae548d3Szrj   if (symndx == -1U)
1060*fae548d3Szrj     return false;
1061*fae548d3Szrj 
1062*fae548d3Szrj   // There can be another reloc in the FDE, if the CIE specifies an
1063*fae548d3Szrj   // LSDA (language specific data area).  We currently don't care.  We
1064*fae548d3Szrj   // will care later if we want to optimize the LSDA from an absolute
1065*fae548d3Szrj   // pointer to a PC relative offset when generating a shared library.
1066*fae548d3Szrj   relocs->advance(pfdeend - pcontents);
1067*fae548d3Szrj 
1068*fae548d3Szrj   // Find the section index for code that this FDE describes.
1069*fae548d3Szrj   // If we have discarded the section, we can also discard the FDE.
1070*fae548d3Szrj   unsigned int fde_shndx;
1071*fae548d3Szrj   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1072*fae548d3Szrj   if (symndx >= symbols_size / sym_size)
1073*fae548d3Szrj     return false;
1074*fae548d3Szrj   elfcpp::Sym<size, big_endian> sym(symbols + symndx * sym_size);
1075*fae548d3Szrj   bool is_ordinary;
1076*fae548d3Szrj   fde_shndx = object->adjust_sym_shndx(symndx, sym.get_st_shndx(),
1077*fae548d3Szrj 				       &is_ordinary);
1078*fae548d3Szrj   bool is_discarded = (is_ordinary
1079*fae548d3Szrj 		       && fde_shndx != elfcpp::SHN_UNDEF
1080*fae548d3Szrj 		       && fde_shndx < object->shnum()
1081*fae548d3Szrj 		       && !object->is_section_included(fde_shndx));
1082*fae548d3Szrj 
1083*fae548d3Szrj   // Fetch the address range field from the FDE. The offset and size
1084*fae548d3Szrj   // of the field depends on the PC encoding given in the CIE, but
1085*fae548d3Szrj   // it is always an absolute value. If the address range is 0, this
1086*fae548d3Szrj   // FDE corresponds to a function that was discarded during optimization
1087*fae548d3Szrj   // (too late to discard the corresponding FDE).
1088*fae548d3Szrj   uint64_t address_range = 0;
1089*fae548d3Szrj   switch (pc_size)
1090*fae548d3Szrj     {
1091*fae548d3Szrj     case 2:
1092*fae548d3Szrj       address_range = elfcpp::Swap<16, big_endian>::readval(pfde + 2);
1093*fae548d3Szrj       break;
1094*fae548d3Szrj     case 4:
1095*fae548d3Szrj       address_range = elfcpp::Swap<32, big_endian>::readval(pfde + 4);
1096*fae548d3Szrj       break;
1097*fae548d3Szrj     case 8:
1098*fae548d3Szrj       address_range = elfcpp::Swap_unaligned<64, big_endian>::readval(pfde + 8);
1099*fae548d3Szrj       break;
1100*fae548d3Szrj     default:
1101*fae548d3Szrj       gold_unreachable();
1102*fae548d3Szrj     }
1103*fae548d3Szrj 
1104*fae548d3Szrj   if (is_discarded || address_range == 0)
1105*fae548d3Szrj     {
1106*fae548d3Szrj       // This FDE applies to a discarded function.  We
1107*fae548d3Szrj       // can discard this FDE.
1108*fae548d3Szrj       object->add_merge_mapping(this, shndx, (pfde - 8) - pcontents,
1109*fae548d3Szrj                                 pfdeend - (pfde - 8), -1);
1110*fae548d3Szrj       return true;
1111*fae548d3Szrj     }
1112*fae548d3Szrj 
1113*fae548d3Szrj   cie->add_fde(new Fde(object, shndx, (pfde - 8) - pcontents,
1114*fae548d3Szrj 		       pfde, pfdeend - pfde));
1115*fae548d3Szrj 
1116*fae548d3Szrj   return true;
1117*fae548d3Szrj }
1118*fae548d3Szrj 
1119*fae548d3Szrj // Add unwind information for a PLT.
1120*fae548d3Szrj 
1121*fae548d3Szrj void
add_ehframe_for_plt(Output_data * plt,const unsigned char * cie_data,size_t cie_length,const unsigned char * fde_data,size_t fde_length)1122*fae548d3Szrj Eh_frame::add_ehframe_for_plt(Output_data* plt, const unsigned char* cie_data,
1123*fae548d3Szrj 			      size_t cie_length, const unsigned char* fde_data,
1124*fae548d3Szrj 			      size_t fde_length)
1125*fae548d3Szrj {
1126*fae548d3Szrj   Cie cie(NULL, 0, 0, elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4, "",
1127*fae548d3Szrj 	  cie_data, cie_length);
1128*fae548d3Szrj   Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
1129*fae548d3Szrj   Cie* pcie;
1130*fae548d3Szrj   if (find_cie != this->cie_offsets_.end())
1131*fae548d3Szrj     pcie = *find_cie;
1132*fae548d3Szrj   else
1133*fae548d3Szrj     {
1134*fae548d3Szrj       gold_assert(!this->mappings_are_done_);
1135*fae548d3Szrj       pcie = new Cie(cie);
1136*fae548d3Szrj       this->cie_offsets_.insert(pcie);
1137*fae548d3Szrj     }
1138*fae548d3Szrj 
1139*fae548d3Szrj   Fde* fde = new Fde(plt, fde_data, fde_length, this->mappings_are_done_);
1140*fae548d3Szrj   pcie->add_fde(fde);
1141*fae548d3Szrj 
1142*fae548d3Szrj   if (this->mappings_are_done_)
1143*fae548d3Szrj     this->final_data_size_ += align_address(fde_length + 8, this->addralign());
1144*fae548d3Szrj }
1145*fae548d3Szrj 
1146*fae548d3Szrj // Remove all post-map unwind information for a PLT.
1147*fae548d3Szrj 
1148*fae548d3Szrj void
remove_ehframe_for_plt(Output_data * plt,const unsigned char * cie_data,size_t cie_length)1149*fae548d3Szrj Eh_frame::remove_ehframe_for_plt(Output_data* plt,
1150*fae548d3Szrj 				 const unsigned char* cie_data,
1151*fae548d3Szrj 				 size_t cie_length)
1152*fae548d3Szrj {
1153*fae548d3Szrj   if (!this->mappings_are_done_)
1154*fae548d3Szrj     return;
1155*fae548d3Szrj 
1156*fae548d3Szrj   Cie cie(NULL, 0, 0, elfcpp::DW_EH_PE_pcrel | elfcpp::DW_EH_PE_sdata4, "",
1157*fae548d3Szrj 	  cie_data, cie_length);
1158*fae548d3Szrj   Cie_offsets::iterator find_cie = this->cie_offsets_.find(&cie);
1159*fae548d3Szrj   gold_assert (find_cie != this->cie_offsets_.end());
1160*fae548d3Szrj   Cie* pcie = *find_cie;
1161*fae548d3Szrj 
1162*fae548d3Szrj   while (pcie->fde_count() != 0)
1163*fae548d3Szrj     {
1164*fae548d3Szrj       const Fde* fde = pcie->last_fde();
1165*fae548d3Szrj       if (!fde->post_map(plt))
1166*fae548d3Szrj 	break;
1167*fae548d3Szrj       size_t length = fde->length();
1168*fae548d3Szrj       this->final_data_size_ -= align_address(length + 8, this->addralign());
1169*fae548d3Szrj       pcie->remove_fde();
1170*fae548d3Szrj     }
1171*fae548d3Szrj }
1172*fae548d3Szrj 
1173*fae548d3Szrj // Return the number of FDEs.
1174*fae548d3Szrj 
1175*fae548d3Szrj unsigned int
fde_count() const1176*fae548d3Szrj Eh_frame::fde_count() const
1177*fae548d3Szrj {
1178*fae548d3Szrj   unsigned int ret = 0;
1179*fae548d3Szrj   for (Unmergeable_cie_offsets::const_iterator p =
1180*fae548d3Szrj 	 this->unmergeable_cie_offsets_.begin();
1181*fae548d3Szrj        p != this->unmergeable_cie_offsets_.end();
1182*fae548d3Szrj        ++p)
1183*fae548d3Szrj     ret += (*p)->fde_count();
1184*fae548d3Szrj   for (Cie_offsets::const_iterator p = this->cie_offsets_.begin();
1185*fae548d3Szrj        p != this->cie_offsets_.end();
1186*fae548d3Szrj        ++p)
1187*fae548d3Szrj     ret += (*p)->fde_count();
1188*fae548d3Szrj   return ret;
1189*fae548d3Szrj }
1190*fae548d3Szrj 
1191*fae548d3Szrj // Set the final data size.
1192*fae548d3Szrj 
1193*fae548d3Szrj void
set_final_data_size()1194*fae548d3Szrj Eh_frame::set_final_data_size()
1195*fae548d3Szrj {
1196*fae548d3Szrj   // We can be called more than once if Layout::set_segment_offsets
1197*fae548d3Szrj   // finds a better mapping.  We don't want to add all the mappings
1198*fae548d3Szrj   // again.
1199*fae548d3Szrj   if (this->mappings_are_done_)
1200*fae548d3Szrj     {
1201*fae548d3Szrj       this->set_data_size(this->final_data_size_);
1202*fae548d3Szrj       return;
1203*fae548d3Szrj     }
1204*fae548d3Szrj 
1205*fae548d3Szrj   section_offset_type output_start = 0;
1206*fae548d3Szrj   if (this->is_offset_valid())
1207*fae548d3Szrj     output_start = this->offset() - this->output_section()->offset();
1208*fae548d3Szrj   section_offset_type output_offset = output_start;
1209*fae548d3Szrj 
1210*fae548d3Szrj   for (Unmergeable_cie_offsets::iterator p =
1211*fae548d3Szrj 	 this->unmergeable_cie_offsets_.begin();
1212*fae548d3Szrj        p != this->unmergeable_cie_offsets_.end();
1213*fae548d3Szrj        ++p)
1214*fae548d3Szrj     output_offset = (*p)->set_output_offset(output_offset,
1215*fae548d3Szrj 					    this->addralign(),
1216*fae548d3Szrj 					    this);
1217*fae548d3Szrj 
1218*fae548d3Szrj   for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1219*fae548d3Szrj        p != this->cie_offsets_.end();
1220*fae548d3Szrj        ++p)
1221*fae548d3Szrj     output_offset = (*p)->set_output_offset(output_offset,
1222*fae548d3Szrj 					    this->addralign(),
1223*fae548d3Szrj 					    this);
1224*fae548d3Szrj 
1225*fae548d3Szrj   this->mappings_are_done_ = true;
1226*fae548d3Szrj   this->final_data_size_ = output_offset - output_start;
1227*fae548d3Szrj 
1228*fae548d3Szrj   gold_assert((output_offset & (this->addralign() - 1)) == 0);
1229*fae548d3Szrj   this->set_data_size(this->final_data_size_);
1230*fae548d3Szrj }
1231*fae548d3Szrj 
1232*fae548d3Szrj // Return an output offset for an input offset.
1233*fae548d3Szrj 
1234*fae548d3Szrj bool
do_output_offset(const Relobj * object,unsigned int shndx,section_offset_type offset,section_offset_type * poutput) const1235*fae548d3Szrj Eh_frame::do_output_offset(const Relobj* object, unsigned int shndx,
1236*fae548d3Szrj 			   section_offset_type offset,
1237*fae548d3Szrj 			   section_offset_type* poutput) const
1238*fae548d3Szrj {
1239*fae548d3Szrj   return object->merge_output_offset(shndx, offset, poutput);
1240*fae548d3Szrj }
1241*fae548d3Szrj 
1242*fae548d3Szrj // Write the data to the output file.
1243*fae548d3Szrj 
1244*fae548d3Szrj void
do_write(Output_file * of)1245*fae548d3Szrj Eh_frame::do_write(Output_file* of)
1246*fae548d3Szrj {
1247*fae548d3Szrj   const off_t offset = this->offset();
1248*fae548d3Szrj   const off_t oview_size = this->data_size();
1249*fae548d3Szrj   unsigned char* const oview = of->get_output_view(offset, oview_size);
1250*fae548d3Szrj 
1251*fae548d3Szrj   switch (parameters->size_and_endianness())
1252*fae548d3Szrj     {
1253*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
1254*fae548d3Szrj     case Parameters::TARGET_32_LITTLE:
1255*fae548d3Szrj       this->do_sized_write<32, false>(oview);
1256*fae548d3Szrj       break;
1257*fae548d3Szrj #endif
1258*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
1259*fae548d3Szrj     case Parameters::TARGET_32_BIG:
1260*fae548d3Szrj       this->do_sized_write<32, true>(oview);
1261*fae548d3Szrj       break;
1262*fae548d3Szrj #endif
1263*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
1264*fae548d3Szrj     case Parameters::TARGET_64_LITTLE:
1265*fae548d3Szrj       this->do_sized_write<64, false>(oview);
1266*fae548d3Szrj       break;
1267*fae548d3Szrj #endif
1268*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
1269*fae548d3Szrj     case Parameters::TARGET_64_BIG:
1270*fae548d3Szrj       this->do_sized_write<64, true>(oview);
1271*fae548d3Szrj       break;
1272*fae548d3Szrj #endif
1273*fae548d3Szrj     default:
1274*fae548d3Szrj       gold_unreachable();
1275*fae548d3Szrj     }
1276*fae548d3Szrj 
1277*fae548d3Szrj   of->write_output_view(offset, oview_size, oview);
1278*fae548d3Szrj }
1279*fae548d3Szrj 
1280*fae548d3Szrj // Write the data to the output file--template version.
1281*fae548d3Szrj 
1282*fae548d3Szrj template<int size, bool big_endian>
1283*fae548d3Szrj void
do_sized_write(unsigned char * oview)1284*fae548d3Szrj Eh_frame::do_sized_write(unsigned char* oview)
1285*fae548d3Szrj {
1286*fae548d3Szrj   uint64_t address = this->address();
1287*fae548d3Szrj   unsigned int addralign = this->addralign();
1288*fae548d3Szrj   section_offset_type o = 0;
1289*fae548d3Szrj   const off_t output_offset = this->offset() - this->output_section()->offset();
1290*fae548d3Szrj   Post_fdes post_fdes;
1291*fae548d3Szrj   for (Unmergeable_cie_offsets::iterator p =
1292*fae548d3Szrj 	 this->unmergeable_cie_offsets_.begin();
1293*fae548d3Szrj        p != this->unmergeable_cie_offsets_.end();
1294*fae548d3Szrj        ++p)
1295*fae548d3Szrj     o = (*p)->write<size, big_endian>(oview, output_offset, o, address,
1296*fae548d3Szrj 				      addralign, this->eh_frame_hdr_,
1297*fae548d3Szrj 				      &post_fdes);
1298*fae548d3Szrj   for (Cie_offsets::iterator p = this->cie_offsets_.begin();
1299*fae548d3Szrj        p != this->cie_offsets_.end();
1300*fae548d3Szrj        ++p)
1301*fae548d3Szrj     o = (*p)->write<size, big_endian>(oview, output_offset, o, address,
1302*fae548d3Szrj 				      addralign, this->eh_frame_hdr_,
1303*fae548d3Szrj 				      &post_fdes);
1304*fae548d3Szrj   for (Post_fdes::iterator p = post_fdes.begin();
1305*fae548d3Szrj        p != post_fdes.end();
1306*fae548d3Szrj        ++p)
1307*fae548d3Szrj     o = (*p).fde->write<size, big_endian>(oview, output_offset, o, address,
1308*fae548d3Szrj 					  addralign, (*p).cie_offset,
1309*fae548d3Szrj 					  (*p).fde_encoding,
1310*fae548d3Szrj 					  this->eh_frame_hdr_);
1311*fae548d3Szrj }
1312*fae548d3Szrj 
1313*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
1314*fae548d3Szrj template
1315*fae548d3Szrj Eh_frame::Eh_frame_section_disposition
1316*fae548d3Szrj Eh_frame::add_ehframe_input_section<32, false>(
1317*fae548d3Szrj     Sized_relobj_file<32, false>* object,
1318*fae548d3Szrj     const unsigned char* symbols,
1319*fae548d3Szrj     section_size_type symbols_size,
1320*fae548d3Szrj     const unsigned char* symbol_names,
1321*fae548d3Szrj     section_size_type symbol_names_size,
1322*fae548d3Szrj     unsigned int shndx,
1323*fae548d3Szrj     unsigned int reloc_shndx,
1324*fae548d3Szrj     unsigned int reloc_type);
1325*fae548d3Szrj #endif
1326*fae548d3Szrj 
1327*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
1328*fae548d3Szrj template
1329*fae548d3Szrj Eh_frame::Eh_frame_section_disposition
1330*fae548d3Szrj Eh_frame::add_ehframe_input_section<32, true>(
1331*fae548d3Szrj     Sized_relobj_file<32, true>* object,
1332*fae548d3Szrj     const unsigned char* symbols,
1333*fae548d3Szrj     section_size_type symbols_size,
1334*fae548d3Szrj     const unsigned char* symbol_names,
1335*fae548d3Szrj     section_size_type symbol_names_size,
1336*fae548d3Szrj     unsigned int shndx,
1337*fae548d3Szrj     unsigned int reloc_shndx,
1338*fae548d3Szrj     unsigned int reloc_type);
1339*fae548d3Szrj #endif
1340*fae548d3Szrj 
1341*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
1342*fae548d3Szrj template
1343*fae548d3Szrj Eh_frame::Eh_frame_section_disposition
1344*fae548d3Szrj Eh_frame::add_ehframe_input_section<64, false>(
1345*fae548d3Szrj     Sized_relobj_file<64, false>* object,
1346*fae548d3Szrj     const unsigned char* symbols,
1347*fae548d3Szrj     section_size_type symbols_size,
1348*fae548d3Szrj     const unsigned char* symbol_names,
1349*fae548d3Szrj     section_size_type symbol_names_size,
1350*fae548d3Szrj     unsigned int shndx,
1351*fae548d3Szrj     unsigned int reloc_shndx,
1352*fae548d3Szrj     unsigned int reloc_type);
1353*fae548d3Szrj #endif
1354*fae548d3Szrj 
1355*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
1356*fae548d3Szrj template
1357*fae548d3Szrj Eh_frame::Eh_frame_section_disposition
1358*fae548d3Szrj Eh_frame::add_ehframe_input_section<64, true>(
1359*fae548d3Szrj     Sized_relobj_file<64, true>* object,
1360*fae548d3Szrj     const unsigned char* symbols,
1361*fae548d3Szrj     section_size_type symbols_size,
1362*fae548d3Szrj     const unsigned char* symbol_names,
1363*fae548d3Szrj     section_size_type symbol_names_size,
1364*fae548d3Szrj     unsigned int shndx,
1365*fae548d3Szrj     unsigned int reloc_shndx,
1366*fae548d3Szrj     unsigned int reloc_type);
1367*fae548d3Szrj #endif
1368*fae548d3Szrj 
1369*fae548d3Szrj } // End namespace gold.
1370