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