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