1*a9fa9459Szrj // i386.cc -- i386 target support 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 
27*a9fa9459Szrj #include "elfcpp.h"
28*a9fa9459Szrj #include "dwarf.h"
29*a9fa9459Szrj #include "parameters.h"
30*a9fa9459Szrj #include "reloc.h"
31*a9fa9459Szrj #include "i386.h"
32*a9fa9459Szrj #include "object.h"
33*a9fa9459Szrj #include "symtab.h"
34*a9fa9459Szrj #include "layout.h"
35*a9fa9459Szrj #include "output.h"
36*a9fa9459Szrj #include "copy-relocs.h"
37*a9fa9459Szrj #include "target.h"
38*a9fa9459Szrj #include "target-reloc.h"
39*a9fa9459Szrj #include "target-select.h"
40*a9fa9459Szrj #include "tls.h"
41*a9fa9459Szrj #include "freebsd.h"
42*a9fa9459Szrj #include "nacl.h"
43*a9fa9459Szrj #include "gc.h"
44*a9fa9459Szrj 
45*a9fa9459Szrj namespace
46*a9fa9459Szrj {
47*a9fa9459Szrj 
48*a9fa9459Szrj using namespace gold;
49*a9fa9459Szrj 
50*a9fa9459Szrj // A class to handle the .got.plt section.
51*a9fa9459Szrj 
52*a9fa9459Szrj class Output_data_got_plt_i386 : public Output_section_data_build
53*a9fa9459Szrj {
54*a9fa9459Szrj  public:
Output_data_got_plt_i386(Layout * layout)55*a9fa9459Szrj   Output_data_got_plt_i386(Layout* layout)
56*a9fa9459Szrj     : Output_section_data_build(4),
57*a9fa9459Szrj       layout_(layout)
58*a9fa9459Szrj   { }
59*a9fa9459Szrj 
60*a9fa9459Szrj  protected:
61*a9fa9459Szrj   // Write out the PLT data.
62*a9fa9459Szrj   void
63*a9fa9459Szrj   do_write(Output_file*);
64*a9fa9459Szrj 
65*a9fa9459Szrj   // Write to a map file.
66*a9fa9459Szrj   void
do_print_to_mapfile(Mapfile * mapfile) const67*a9fa9459Szrj   do_print_to_mapfile(Mapfile* mapfile) const
68*a9fa9459Szrj   { mapfile->print_output_data(this, "** GOT PLT"); }
69*a9fa9459Szrj 
70*a9fa9459Szrj  private:
71*a9fa9459Szrj   // A pointer to the Layout class, so that we can find the .dynamic
72*a9fa9459Szrj   // section when we write out the GOT PLT section.
73*a9fa9459Szrj   Layout* layout_;
74*a9fa9459Szrj };
75*a9fa9459Szrj 
76*a9fa9459Szrj // A class to handle the PLT data.
77*a9fa9459Szrj // This is an abstract base class that handles most of the linker details
78*a9fa9459Szrj // but does not know the actual contents of PLT entries.  The derived
79*a9fa9459Szrj // classes below fill in those details.
80*a9fa9459Szrj 
81*a9fa9459Szrj class Output_data_plt_i386 : public Output_section_data
82*a9fa9459Szrj {
83*a9fa9459Szrj  public:
84*a9fa9459Szrj   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
85*a9fa9459Szrj 
86*a9fa9459Szrj   Output_data_plt_i386(Layout*, uint64_t addralign,
87*a9fa9459Szrj 		       Output_data_got_plt_i386*, Output_data_space*);
88*a9fa9459Szrj 
89*a9fa9459Szrj   // Add an entry to the PLT.
90*a9fa9459Szrj   void
91*a9fa9459Szrj   add_entry(Symbol_table*, Layout*, Symbol* gsym);
92*a9fa9459Szrj 
93*a9fa9459Szrj   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
94*a9fa9459Szrj   unsigned int
95*a9fa9459Szrj   add_local_ifunc_entry(Symbol_table*, Layout*,
96*a9fa9459Szrj 			Sized_relobj_file<32, false>* relobj,
97*a9fa9459Szrj 			unsigned int local_sym_index);
98*a9fa9459Szrj 
99*a9fa9459Szrj   // Return the .rel.plt section data.
100*a9fa9459Szrj   Reloc_section*
rel_plt() const101*a9fa9459Szrj   rel_plt() const
102*a9fa9459Szrj   { return this->rel_; }
103*a9fa9459Szrj 
104*a9fa9459Szrj   // Return where the TLS_DESC relocations should go.
105*a9fa9459Szrj   Reloc_section*
106*a9fa9459Szrj   rel_tls_desc(Layout*);
107*a9fa9459Szrj 
108*a9fa9459Szrj   // Return where the IRELATIVE relocations should go.
109*a9fa9459Szrj   Reloc_section*
110*a9fa9459Szrj   rel_irelative(Symbol_table*, Layout*);
111*a9fa9459Szrj 
112*a9fa9459Szrj   // Return whether we created a section for IRELATIVE relocations.
113*a9fa9459Szrj   bool
has_irelative_section() const114*a9fa9459Szrj   has_irelative_section() const
115*a9fa9459Szrj   { return this->irelative_rel_ != NULL; }
116*a9fa9459Szrj 
117*a9fa9459Szrj   // Return the number of PLT entries.
118*a9fa9459Szrj   unsigned int
entry_count() const119*a9fa9459Szrj   entry_count() const
120*a9fa9459Szrj   { return this->count_ + this->irelative_count_; }
121*a9fa9459Szrj 
122*a9fa9459Szrj   // Return the offset of the first non-reserved PLT entry.
123*a9fa9459Szrj   unsigned int
first_plt_entry_offset()124*a9fa9459Szrj   first_plt_entry_offset()
125*a9fa9459Szrj   { return this->get_plt_entry_size(); }
126*a9fa9459Szrj 
127*a9fa9459Szrj   // Return the size of a PLT entry.
128*a9fa9459Szrj   unsigned int
get_plt_entry_size() const129*a9fa9459Szrj   get_plt_entry_size() const
130*a9fa9459Szrj   { return this->do_get_plt_entry_size(); }
131*a9fa9459Szrj 
132*a9fa9459Szrj   // Return the PLT address to use for a global symbol.
133*a9fa9459Szrj   uint64_t
134*a9fa9459Szrj   address_for_global(const Symbol*);
135*a9fa9459Szrj 
136*a9fa9459Szrj   // Return the PLT address to use for a local symbol.
137*a9fa9459Szrj   uint64_t
138*a9fa9459Szrj   address_for_local(const Relobj*, unsigned int symndx);
139*a9fa9459Szrj 
140*a9fa9459Szrj   // Add .eh_frame information for the PLT.
141*a9fa9459Szrj   void
add_eh_frame(Layout * layout)142*a9fa9459Szrj   add_eh_frame(Layout* layout)
143*a9fa9459Szrj   { this->do_add_eh_frame(layout); }
144*a9fa9459Szrj 
145*a9fa9459Szrj  protected:
146*a9fa9459Szrj   // Fill the first PLT entry, given the pointer to the PLT section data
147*a9fa9459Szrj   // and the runtime address of the GOT.
148*a9fa9459Szrj   void
fill_first_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr got_address)149*a9fa9459Szrj   fill_first_plt_entry(unsigned char* pov,
150*a9fa9459Szrj 		       elfcpp::Elf_types<32>::Elf_Addr got_address)
151*a9fa9459Szrj   { this->do_fill_first_plt_entry(pov, got_address); }
152*a9fa9459Szrj 
153*a9fa9459Szrj   // Fill a normal PLT entry, given the pointer to the entry's data in the
154*a9fa9459Szrj   // section, the runtime address of the GOT, the offset into the GOT of
155*a9fa9459Szrj   // the corresponding slot, the offset into the relocation section of the
156*a9fa9459Szrj   // corresponding reloc, and the offset of this entry within the whole
157*a9fa9459Szrj   // PLT.  Return the offset from this PLT entry's runtime address that
158*a9fa9459Szrj   // should be used to compute the initial value of the GOT slot.
159*a9fa9459Szrj   unsigned int
fill_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr got_address,unsigned int got_offset,unsigned int plt_offset,unsigned int plt_rel_offset)160*a9fa9459Szrj   fill_plt_entry(unsigned char* pov,
161*a9fa9459Szrj 		 elfcpp::Elf_types<32>::Elf_Addr got_address,
162*a9fa9459Szrj 		 unsigned int got_offset,
163*a9fa9459Szrj 		 unsigned int plt_offset,
164*a9fa9459Szrj 		 unsigned int plt_rel_offset)
165*a9fa9459Szrj   {
166*a9fa9459Szrj     return this->do_fill_plt_entry(pov, got_address, got_offset,
167*a9fa9459Szrj 				   plt_offset, plt_rel_offset);
168*a9fa9459Szrj   }
169*a9fa9459Szrj 
170*a9fa9459Szrj   virtual unsigned int
171*a9fa9459Szrj   do_get_plt_entry_size() const = 0;
172*a9fa9459Szrj 
173*a9fa9459Szrj   virtual void
174*a9fa9459Szrj   do_fill_first_plt_entry(unsigned char* pov,
175*a9fa9459Szrj 			  elfcpp::Elf_types<32>::Elf_Addr got_address) = 0;
176*a9fa9459Szrj 
177*a9fa9459Szrj   virtual unsigned int
178*a9fa9459Szrj   do_fill_plt_entry(unsigned char* pov,
179*a9fa9459Szrj 		    elfcpp::Elf_types<32>::Elf_Addr got_address,
180*a9fa9459Szrj 		    unsigned int got_offset,
181*a9fa9459Szrj 		    unsigned int plt_offset,
182*a9fa9459Szrj 		    unsigned int plt_rel_offset) = 0;
183*a9fa9459Szrj 
184*a9fa9459Szrj   virtual void
185*a9fa9459Szrj   do_add_eh_frame(Layout*) = 0;
186*a9fa9459Szrj 
187*a9fa9459Szrj   void
188*a9fa9459Szrj   do_adjust_output_section(Output_section* os);
189*a9fa9459Szrj 
190*a9fa9459Szrj   // Write to a map file.
191*a9fa9459Szrj   void
do_print_to_mapfile(Mapfile * mapfile) const192*a9fa9459Szrj   do_print_to_mapfile(Mapfile* mapfile) const
193*a9fa9459Szrj   { mapfile->print_output_data(this, _("** PLT")); }
194*a9fa9459Szrj 
195*a9fa9459Szrj   // The .eh_frame unwind information for the PLT.
196*a9fa9459Szrj   // The CIE is common across variants of the PLT format.
197*a9fa9459Szrj   static const int plt_eh_frame_cie_size = 16;
198*a9fa9459Szrj   static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
199*a9fa9459Szrj 
200*a9fa9459Szrj  private:
201*a9fa9459Szrj   // Set the final size.
202*a9fa9459Szrj   void
set_final_data_size()203*a9fa9459Szrj   set_final_data_size()
204*a9fa9459Szrj   {
205*a9fa9459Szrj     this->set_data_size((this->count_ + this->irelative_count_ + 1)
206*a9fa9459Szrj 			* this->get_plt_entry_size());
207*a9fa9459Szrj   }
208*a9fa9459Szrj 
209*a9fa9459Szrj   // Write out the PLT data.
210*a9fa9459Szrj   void
211*a9fa9459Szrj   do_write(Output_file*);
212*a9fa9459Szrj 
213*a9fa9459Szrj   // We keep a list of global STT_GNU_IFUNC symbols, each with its
214*a9fa9459Szrj   // offset in the GOT.
215*a9fa9459Szrj   struct Global_ifunc
216*a9fa9459Szrj   {
217*a9fa9459Szrj     Symbol* sym;
218*a9fa9459Szrj     unsigned int got_offset;
219*a9fa9459Szrj   };
220*a9fa9459Szrj 
221*a9fa9459Szrj   // We keep a list of local STT_GNU_IFUNC symbols, each with its
222*a9fa9459Szrj   // offset in the GOT.
223*a9fa9459Szrj   struct Local_ifunc
224*a9fa9459Szrj   {
225*a9fa9459Szrj     Sized_relobj_file<32, false>* object;
226*a9fa9459Szrj     unsigned int local_sym_index;
227*a9fa9459Szrj     unsigned int got_offset;
228*a9fa9459Szrj   };
229*a9fa9459Szrj 
230*a9fa9459Szrj   // The reloc section.
231*a9fa9459Szrj   Reloc_section* rel_;
232*a9fa9459Szrj   // The TLS_DESC relocations, if necessary.  These must follow the
233*a9fa9459Szrj   // regular PLT relocs.
234*a9fa9459Szrj   Reloc_section* tls_desc_rel_;
235*a9fa9459Szrj   // The IRELATIVE relocations, if necessary.  These must follow the
236*a9fa9459Szrj   // regular relocatoins and the TLS_DESC relocations.
237*a9fa9459Szrj   Reloc_section* irelative_rel_;
238*a9fa9459Szrj   // The .got.plt section.
239*a9fa9459Szrj   Output_data_got_plt_i386* got_plt_;
240*a9fa9459Szrj   // The part of the .got.plt section used for IRELATIVE relocs.
241*a9fa9459Szrj   Output_data_space* got_irelative_;
242*a9fa9459Szrj   // The number of PLT entries.
243*a9fa9459Szrj   unsigned int count_;
244*a9fa9459Szrj   // Number of PLT entries with R_386_IRELATIVE relocs.  These follow
245*a9fa9459Szrj   // the regular PLT entries.
246*a9fa9459Szrj   unsigned int irelative_count_;
247*a9fa9459Szrj   // Global STT_GNU_IFUNC symbols.
248*a9fa9459Szrj   std::vector<Global_ifunc> global_ifuncs_;
249*a9fa9459Szrj   // Local STT_GNU_IFUNC symbols.
250*a9fa9459Szrj   std::vector<Local_ifunc> local_ifuncs_;
251*a9fa9459Szrj };
252*a9fa9459Szrj 
253*a9fa9459Szrj // This is an abstract class for the standard PLT layout.
254*a9fa9459Szrj // The derived classes below handle the actual PLT contents
255*a9fa9459Szrj // for the executable (non-PIC) and shared-library (PIC) cases.
256*a9fa9459Szrj // The unwind information is uniform across those two, so it's here.
257*a9fa9459Szrj 
258*a9fa9459Szrj class Output_data_plt_i386_standard : public Output_data_plt_i386
259*a9fa9459Szrj {
260*a9fa9459Szrj  public:
Output_data_plt_i386_standard(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)261*a9fa9459Szrj   Output_data_plt_i386_standard(Layout* layout,
262*a9fa9459Szrj 				Output_data_got_plt_i386* got_plt,
263*a9fa9459Szrj 				Output_data_space* got_irelative)
264*a9fa9459Szrj     : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
265*a9fa9459Szrj   { }
266*a9fa9459Szrj 
267*a9fa9459Szrj  protected:
268*a9fa9459Szrj   virtual unsigned int
do_get_plt_entry_size() const269*a9fa9459Szrj   do_get_plt_entry_size() const
270*a9fa9459Szrj   { return plt_entry_size; }
271*a9fa9459Szrj 
272*a9fa9459Szrj   virtual void
do_add_eh_frame(Layout * layout)273*a9fa9459Szrj   do_add_eh_frame(Layout* layout)
274*a9fa9459Szrj   {
275*a9fa9459Szrj     layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
276*a9fa9459Szrj 				 plt_eh_frame_fde, plt_eh_frame_fde_size);
277*a9fa9459Szrj   }
278*a9fa9459Szrj 
279*a9fa9459Szrj   // The size of an entry in the PLT.
280*a9fa9459Szrj   static const int plt_entry_size = 16;
281*a9fa9459Szrj 
282*a9fa9459Szrj   // The .eh_frame unwind information for the PLT.
283*a9fa9459Szrj   static const int plt_eh_frame_fde_size = 32;
284*a9fa9459Szrj   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
285*a9fa9459Szrj };
286*a9fa9459Szrj 
287*a9fa9459Szrj // Actually fill the PLT contents for an executable (non-PIC).
288*a9fa9459Szrj 
289*a9fa9459Szrj class Output_data_plt_i386_exec : public Output_data_plt_i386_standard
290*a9fa9459Szrj {
291*a9fa9459Szrj public:
Output_data_plt_i386_exec(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)292*a9fa9459Szrj   Output_data_plt_i386_exec(Layout* layout,
293*a9fa9459Szrj 			    Output_data_got_plt_i386* got_plt,
294*a9fa9459Szrj 			    Output_data_space* got_irelative)
295*a9fa9459Szrj     : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
296*a9fa9459Szrj   { }
297*a9fa9459Szrj 
298*a9fa9459Szrj  protected:
299*a9fa9459Szrj   virtual void
300*a9fa9459Szrj   do_fill_first_plt_entry(unsigned char* pov,
301*a9fa9459Szrj 			  elfcpp::Elf_types<32>::Elf_Addr got_address);
302*a9fa9459Szrj 
303*a9fa9459Szrj   virtual unsigned int
304*a9fa9459Szrj   do_fill_plt_entry(unsigned char* pov,
305*a9fa9459Szrj 		    elfcpp::Elf_types<32>::Elf_Addr got_address,
306*a9fa9459Szrj 		    unsigned int got_offset,
307*a9fa9459Szrj 		    unsigned int plt_offset,
308*a9fa9459Szrj 		    unsigned int plt_rel_offset);
309*a9fa9459Szrj 
310*a9fa9459Szrj  private:
311*a9fa9459Szrj   // The first entry in the PLT for an executable.
312*a9fa9459Szrj   static const unsigned char first_plt_entry[plt_entry_size];
313*a9fa9459Szrj 
314*a9fa9459Szrj   // Other entries in the PLT for an executable.
315*a9fa9459Szrj   static const unsigned char plt_entry[plt_entry_size];
316*a9fa9459Szrj };
317*a9fa9459Szrj 
318*a9fa9459Szrj // Actually fill the PLT contents for a shared library (PIC).
319*a9fa9459Szrj 
320*a9fa9459Szrj class Output_data_plt_i386_dyn : public Output_data_plt_i386_standard
321*a9fa9459Szrj {
322*a9fa9459Szrj  public:
Output_data_plt_i386_dyn(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)323*a9fa9459Szrj   Output_data_plt_i386_dyn(Layout* layout,
324*a9fa9459Szrj 			   Output_data_got_plt_i386* got_plt,
325*a9fa9459Szrj 			   Output_data_space* got_irelative)
326*a9fa9459Szrj     : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
327*a9fa9459Szrj   { }
328*a9fa9459Szrj 
329*a9fa9459Szrj  protected:
330*a9fa9459Szrj   virtual void
331*a9fa9459Szrj   do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
332*a9fa9459Szrj 
333*a9fa9459Szrj   virtual unsigned int
334*a9fa9459Szrj   do_fill_plt_entry(unsigned char* pov,
335*a9fa9459Szrj 		    elfcpp::Elf_types<32>::Elf_Addr,
336*a9fa9459Szrj 		    unsigned int got_offset,
337*a9fa9459Szrj 		    unsigned int plt_offset,
338*a9fa9459Szrj 		    unsigned int plt_rel_offset);
339*a9fa9459Szrj 
340*a9fa9459Szrj  private:
341*a9fa9459Szrj   // The first entry in the PLT for a shared object.
342*a9fa9459Szrj   static const unsigned char first_plt_entry[plt_entry_size];
343*a9fa9459Szrj 
344*a9fa9459Szrj   // Other entries in the PLT for a shared object.
345*a9fa9459Szrj   static const unsigned char plt_entry[plt_entry_size];
346*a9fa9459Szrj };
347*a9fa9459Szrj 
348*a9fa9459Szrj // The i386 target class.
349*a9fa9459Szrj // TLS info comes from
350*a9fa9459Szrj //   http://people.redhat.com/drepper/tls.pdf
351*a9fa9459Szrj //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
352*a9fa9459Szrj 
353*a9fa9459Szrj class Target_i386 : public Sized_target<32, false>
354*a9fa9459Szrj {
355*a9fa9459Szrj  public:
356*a9fa9459Szrj   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
357*a9fa9459Szrj 
Target_i386(const Target::Target_info * info=& i386_info)358*a9fa9459Szrj   Target_i386(const Target::Target_info* info = &i386_info)
359*a9fa9459Szrj     : Sized_target<32, false>(info),
360*a9fa9459Szrj       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
361*a9fa9459Szrj       got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
362*a9fa9459Szrj       rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY),
363*a9fa9459Szrj       got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
364*a9fa9459Szrj   { }
365*a9fa9459Szrj 
366*a9fa9459Szrj   // Process the relocations to determine unreferenced sections for
367*a9fa9459Szrj   // garbage collection.
368*a9fa9459Szrj   void
369*a9fa9459Szrj   gc_process_relocs(Symbol_table* symtab,
370*a9fa9459Szrj 		    Layout* layout,
371*a9fa9459Szrj 		    Sized_relobj_file<32, false>* object,
372*a9fa9459Szrj 		    unsigned int data_shndx,
373*a9fa9459Szrj 		    unsigned int sh_type,
374*a9fa9459Szrj 		    const unsigned char* prelocs,
375*a9fa9459Szrj 		    size_t reloc_count,
376*a9fa9459Szrj 		    Output_section* output_section,
377*a9fa9459Szrj 		    bool needs_special_offset_handling,
378*a9fa9459Szrj 		    size_t local_symbol_count,
379*a9fa9459Szrj 		    const unsigned char* plocal_symbols);
380*a9fa9459Szrj 
381*a9fa9459Szrj   // Scan the relocations to look for symbol adjustments.
382*a9fa9459Szrj   void
383*a9fa9459Szrj   scan_relocs(Symbol_table* symtab,
384*a9fa9459Szrj 	      Layout* layout,
385*a9fa9459Szrj 	      Sized_relobj_file<32, false>* object,
386*a9fa9459Szrj 	      unsigned int data_shndx,
387*a9fa9459Szrj 	      unsigned int sh_type,
388*a9fa9459Szrj 	      const unsigned char* prelocs,
389*a9fa9459Szrj 	      size_t reloc_count,
390*a9fa9459Szrj 	      Output_section* output_section,
391*a9fa9459Szrj 	      bool needs_special_offset_handling,
392*a9fa9459Szrj 	      size_t local_symbol_count,
393*a9fa9459Szrj 	      const unsigned char* plocal_symbols);
394*a9fa9459Szrj 
395*a9fa9459Szrj   // Finalize the sections.
396*a9fa9459Szrj   void
397*a9fa9459Szrj   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
398*a9fa9459Szrj 
399*a9fa9459Szrj   // Return the value to use for a dynamic which requires special
400*a9fa9459Szrj   // treatment.
401*a9fa9459Szrj   uint64_t
402*a9fa9459Szrj   do_dynsym_value(const Symbol*) const;
403*a9fa9459Szrj 
404*a9fa9459Szrj   // Relocate a section.
405*a9fa9459Szrj   void
406*a9fa9459Szrj   relocate_section(const Relocate_info<32, false>*,
407*a9fa9459Szrj 		   unsigned int sh_type,
408*a9fa9459Szrj 		   const unsigned char* prelocs,
409*a9fa9459Szrj 		   size_t reloc_count,
410*a9fa9459Szrj 		   Output_section* output_section,
411*a9fa9459Szrj 		   bool needs_special_offset_handling,
412*a9fa9459Szrj 		   unsigned char* view,
413*a9fa9459Szrj 		   elfcpp::Elf_types<32>::Elf_Addr view_address,
414*a9fa9459Szrj 		   section_size_type view_size,
415*a9fa9459Szrj 		   const Reloc_symbol_changes*);
416*a9fa9459Szrj 
417*a9fa9459Szrj   // Scan the relocs during a relocatable link.
418*a9fa9459Szrj   void
419*a9fa9459Szrj   scan_relocatable_relocs(Symbol_table* symtab,
420*a9fa9459Szrj 			  Layout* layout,
421*a9fa9459Szrj 			  Sized_relobj_file<32, false>* object,
422*a9fa9459Szrj 			  unsigned int data_shndx,
423*a9fa9459Szrj 			  unsigned int sh_type,
424*a9fa9459Szrj 			  const unsigned char* prelocs,
425*a9fa9459Szrj 			  size_t reloc_count,
426*a9fa9459Szrj 			  Output_section* output_section,
427*a9fa9459Szrj 			  bool needs_special_offset_handling,
428*a9fa9459Szrj 			  size_t local_symbol_count,
429*a9fa9459Szrj 			  const unsigned char* plocal_symbols,
430*a9fa9459Szrj 			  Relocatable_relocs*);
431*a9fa9459Szrj 
432*a9fa9459Szrj   // Scan the relocs for --emit-relocs.
433*a9fa9459Szrj   void
434*a9fa9459Szrj   emit_relocs_scan(Symbol_table* symtab,
435*a9fa9459Szrj 		   Layout* layout,
436*a9fa9459Szrj 		   Sized_relobj_file<32, false>* object,
437*a9fa9459Szrj 		   unsigned int data_shndx,
438*a9fa9459Szrj 		   unsigned int sh_type,
439*a9fa9459Szrj 		   const unsigned char* prelocs,
440*a9fa9459Szrj 		   size_t reloc_count,
441*a9fa9459Szrj 		   Output_section* output_section,
442*a9fa9459Szrj 		   bool needs_special_offset_handling,
443*a9fa9459Szrj 		   size_t local_symbol_count,
444*a9fa9459Szrj 		   const unsigned char* plocal_syms,
445*a9fa9459Szrj 		   Relocatable_relocs* rr);
446*a9fa9459Szrj 
447*a9fa9459Szrj   // Emit relocations for a section.
448*a9fa9459Szrj   void
449*a9fa9459Szrj   relocate_relocs(const Relocate_info<32, false>*,
450*a9fa9459Szrj 		  unsigned int sh_type,
451*a9fa9459Szrj 		  const unsigned char* prelocs,
452*a9fa9459Szrj 		  size_t reloc_count,
453*a9fa9459Szrj 		  Output_section* output_section,
454*a9fa9459Szrj 		  elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
455*a9fa9459Szrj 		  unsigned char* view,
456*a9fa9459Szrj 		  elfcpp::Elf_types<32>::Elf_Addr view_address,
457*a9fa9459Szrj 		  section_size_type view_size,
458*a9fa9459Szrj 		  unsigned char* reloc_view,
459*a9fa9459Szrj 		  section_size_type reloc_view_size);
460*a9fa9459Szrj 
461*a9fa9459Szrj   // Return a string used to fill a code section with nops.
462*a9fa9459Szrj   std::string
463*a9fa9459Szrj   do_code_fill(section_size_type length) const;
464*a9fa9459Szrj 
465*a9fa9459Szrj   // Return whether SYM is defined by the ABI.
466*a9fa9459Szrj   bool
do_is_defined_by_abi(const Symbol * sym) const467*a9fa9459Szrj   do_is_defined_by_abi(const Symbol* sym) const
468*a9fa9459Szrj   { return strcmp(sym->name(), "___tls_get_addr") == 0; }
469*a9fa9459Szrj 
470*a9fa9459Szrj   // Return whether a symbol name implies a local label.  The UnixWare
471*a9fa9459Szrj   // 2.1 cc generates temporary symbols that start with .X, so we
472*a9fa9459Szrj   // recognize them here.  FIXME: do other SVR4 compilers also use .X?.
473*a9fa9459Szrj   // If so, we should move the .X recognition into
474*a9fa9459Szrj   // Target::do_is_local_label_name.
475*a9fa9459Szrj   bool
do_is_local_label_name(const char * name) const476*a9fa9459Szrj   do_is_local_label_name(const char* name) const
477*a9fa9459Szrj   {
478*a9fa9459Szrj     if (name[0] == '.' && name[1] == 'X')
479*a9fa9459Szrj       return true;
480*a9fa9459Szrj     return Target::do_is_local_label_name(name);
481*a9fa9459Szrj   }
482*a9fa9459Szrj 
483*a9fa9459Szrj   // Return the PLT address to use for a global symbol.
484*a9fa9459Szrj   uint64_t
do_plt_address_for_global(const Symbol * gsym) const485*a9fa9459Szrj   do_plt_address_for_global(const Symbol* gsym) const
486*a9fa9459Szrj   { return this->plt_section()->address_for_global(gsym); }
487*a9fa9459Szrj 
488*a9fa9459Szrj   uint64_t
do_plt_address_for_local(const Relobj * relobj,unsigned int symndx) const489*a9fa9459Szrj   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
490*a9fa9459Szrj   { return this->plt_section()->address_for_local(relobj, symndx); }
491*a9fa9459Szrj 
492*a9fa9459Szrj   // We can tell whether we take the address of a function.
493*a9fa9459Szrj   inline bool
do_can_check_for_function_pointers() const494*a9fa9459Szrj   do_can_check_for_function_pointers() const
495*a9fa9459Szrj   { return true; }
496*a9fa9459Szrj 
497*a9fa9459Szrj   // Return the base for a DW_EH_PE_datarel encoding.
498*a9fa9459Szrj   uint64_t
499*a9fa9459Szrj   do_ehframe_datarel_base() const;
500*a9fa9459Szrj 
501*a9fa9459Szrj   // Return whether SYM is call to a non-split function.
502*a9fa9459Szrj   bool
503*a9fa9459Szrj   do_is_call_to_non_split(const Symbol* sym, const unsigned char*,
504*a9fa9459Szrj 			  const unsigned char*, section_size_type) const;
505*a9fa9459Szrj 
506*a9fa9459Szrj   // Adjust -fsplit-stack code which calls non-split-stack code.
507*a9fa9459Szrj   void
508*a9fa9459Szrj   do_calls_non_split(Relobj* object, unsigned int shndx,
509*a9fa9459Szrj 		     section_offset_type fnoffset, section_size_type fnsize,
510*a9fa9459Szrj 		     const unsigned char* prelocs, size_t reloc_count,
511*a9fa9459Szrj 		     unsigned char* view, section_size_type view_size,
512*a9fa9459Szrj 		     std::string* from, std::string* to) const;
513*a9fa9459Szrj 
514*a9fa9459Szrj   // Return the size of the GOT section.
515*a9fa9459Szrj   section_size_type
got_size() const516*a9fa9459Szrj   got_size() const
517*a9fa9459Szrj   {
518*a9fa9459Szrj     gold_assert(this->got_ != NULL);
519*a9fa9459Szrj     return this->got_->data_size();
520*a9fa9459Szrj   }
521*a9fa9459Szrj 
522*a9fa9459Szrj   // Return the number of entries in the GOT.
523*a9fa9459Szrj   unsigned int
got_entry_count() const524*a9fa9459Szrj   got_entry_count() const
525*a9fa9459Szrj   {
526*a9fa9459Szrj     if (this->got_ == NULL)
527*a9fa9459Szrj       return 0;
528*a9fa9459Szrj     return this->got_size() / 4;
529*a9fa9459Szrj   }
530*a9fa9459Szrj 
531*a9fa9459Szrj   // Return the number of entries in the PLT.
532*a9fa9459Szrj   unsigned int
533*a9fa9459Szrj   plt_entry_count() const;
534*a9fa9459Szrj 
535*a9fa9459Szrj   // Return the offset of the first non-reserved PLT entry.
536*a9fa9459Szrj   unsigned int
537*a9fa9459Szrj   first_plt_entry_offset() const;
538*a9fa9459Szrj 
539*a9fa9459Szrj   // Return the size of each PLT entry.
540*a9fa9459Szrj   unsigned int
541*a9fa9459Szrj   plt_entry_size() const;
542*a9fa9459Szrj 
543*a9fa9459Szrj  protected:
544*a9fa9459Szrj   // Instantiate the plt_ member.
545*a9fa9459Szrj   // This chooses the right PLT flavor for an executable or a shared object.
546*a9fa9459Szrj   Output_data_plt_i386*
make_data_plt(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative,bool dyn)547*a9fa9459Szrj   make_data_plt(Layout* layout,
548*a9fa9459Szrj 		Output_data_got_plt_i386* got_plt,
549*a9fa9459Szrj 		Output_data_space* got_irelative,
550*a9fa9459Szrj 		bool dyn)
551*a9fa9459Szrj   { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); }
552*a9fa9459Szrj 
553*a9fa9459Szrj   virtual Output_data_plt_i386*
do_make_data_plt(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative,bool dyn)554*a9fa9459Szrj   do_make_data_plt(Layout* layout,
555*a9fa9459Szrj 		   Output_data_got_plt_i386* got_plt,
556*a9fa9459Szrj 		   Output_data_space* got_irelative,
557*a9fa9459Szrj 		   bool dyn)
558*a9fa9459Szrj   {
559*a9fa9459Szrj     if (dyn)
560*a9fa9459Szrj       return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative);
561*a9fa9459Szrj     else
562*a9fa9459Szrj       return new Output_data_plt_i386_exec(layout, got_plt, got_irelative);
563*a9fa9459Szrj   }
564*a9fa9459Szrj 
565*a9fa9459Szrj  private:
566*a9fa9459Szrj   // The class which scans relocations.
567*a9fa9459Szrj   struct Scan
568*a9fa9459Szrj   {
569*a9fa9459Szrj     static inline int
570*a9fa9459Szrj 
571*a9fa9459Szrj     get_reference_flags(unsigned int r_type);
572*a9fa9459Szrj 
573*a9fa9459Szrj     inline void
574*a9fa9459Szrj     local(Symbol_table* symtab, Layout* layout, Target_i386* target,
575*a9fa9459Szrj 	  Sized_relobj_file<32, false>* object,
576*a9fa9459Szrj 	  unsigned int data_shndx,
577*a9fa9459Szrj 	  Output_section* output_section,
578*a9fa9459Szrj 	  const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
579*a9fa9459Szrj 	  const elfcpp::Sym<32, false>& lsym,
580*a9fa9459Szrj 	  bool is_discarded);
581*a9fa9459Szrj 
582*a9fa9459Szrj     inline void
583*a9fa9459Szrj     global(Symbol_table* symtab, Layout* layout, Target_i386* target,
584*a9fa9459Szrj 	   Sized_relobj_file<32, false>* object,
585*a9fa9459Szrj 	   unsigned int data_shndx,
586*a9fa9459Szrj 	   Output_section* output_section,
587*a9fa9459Szrj 	   const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
588*a9fa9459Szrj 	   Symbol* gsym);
589*a9fa9459Szrj 
590*a9fa9459Szrj     inline bool
591*a9fa9459Szrj     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
592*a9fa9459Szrj 					Target_i386* target,
593*a9fa9459Szrj 					Sized_relobj_file<32, false>* object,
594*a9fa9459Szrj 					unsigned int data_shndx,
595*a9fa9459Szrj 					Output_section* output_section,
596*a9fa9459Szrj 					const elfcpp::Rel<32, false>& reloc,
597*a9fa9459Szrj 					unsigned int r_type,
598*a9fa9459Szrj 					const elfcpp::Sym<32, false>& lsym);
599*a9fa9459Szrj 
600*a9fa9459Szrj     inline bool
601*a9fa9459Szrj     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
602*a9fa9459Szrj 					 Target_i386* target,
603*a9fa9459Szrj 					 Sized_relobj_file<32, false>* object,
604*a9fa9459Szrj 					 unsigned int data_shndx,
605*a9fa9459Szrj 					 Output_section* output_section,
606*a9fa9459Szrj 					 const elfcpp::Rel<32, false>& reloc,
607*a9fa9459Szrj 					 unsigned int r_type,
608*a9fa9459Szrj 					 Symbol* gsym);
609*a9fa9459Szrj 
610*a9fa9459Szrj     inline bool
611*a9fa9459Szrj     possible_function_pointer_reloc(unsigned int r_type);
612*a9fa9459Szrj 
613*a9fa9459Szrj     bool
614*a9fa9459Szrj     reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
615*a9fa9459Szrj 			      unsigned int r_type);
616*a9fa9459Szrj 
617*a9fa9459Szrj     static void
618*a9fa9459Szrj     unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
619*a9fa9459Szrj 
620*a9fa9459Szrj     static void
621*a9fa9459Szrj     unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
622*a9fa9459Szrj 			     Symbol*);
623*a9fa9459Szrj   };
624*a9fa9459Szrj 
625*a9fa9459Szrj   // The class which implements relocation.
626*a9fa9459Szrj   class Relocate
627*a9fa9459Szrj   {
628*a9fa9459Szrj    public:
Relocate()629*a9fa9459Szrj     Relocate()
630*a9fa9459Szrj       : skip_call_tls_get_addr_(false),
631*a9fa9459Szrj 	local_dynamic_type_(LOCAL_DYNAMIC_NONE)
632*a9fa9459Szrj     { }
633*a9fa9459Szrj 
~Relocate()634*a9fa9459Szrj     ~Relocate()
635*a9fa9459Szrj     {
636*a9fa9459Szrj       if (this->skip_call_tls_get_addr_)
637*a9fa9459Szrj 	{
638*a9fa9459Szrj 	  // FIXME: This needs to specify the location somehow.
639*a9fa9459Szrj 	  gold_error(_("missing expected TLS relocation"));
640*a9fa9459Szrj 	}
641*a9fa9459Szrj     }
642*a9fa9459Szrj 
643*a9fa9459Szrj     // Return whether the static relocation needs to be applied.
644*a9fa9459Szrj     inline bool
645*a9fa9459Szrj     should_apply_static_reloc(const Sized_symbol<32>* gsym,
646*a9fa9459Szrj 			      unsigned int r_type,
647*a9fa9459Szrj 			      bool is_32bit,
648*a9fa9459Szrj 			      Output_section* output_section);
649*a9fa9459Szrj 
650*a9fa9459Szrj     // Do a relocation.  Return false if the caller should not issue
651*a9fa9459Szrj     // any warnings about this relocation.
652*a9fa9459Szrj     inline bool
653*a9fa9459Szrj     relocate(const Relocate_info<32, false>*, unsigned int,
654*a9fa9459Szrj 	     Target_i386*, Output_section*, size_t, const unsigned char*,
655*a9fa9459Szrj 	     const Sized_symbol<32>*, const Symbol_value<32>*,
656*a9fa9459Szrj 	     unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
657*a9fa9459Szrj 	     section_size_type);
658*a9fa9459Szrj 
659*a9fa9459Szrj    private:
660*a9fa9459Szrj     // Do a TLS relocation.
661*a9fa9459Szrj     inline void
662*a9fa9459Szrj     relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
663*a9fa9459Szrj 		 size_t relnum, const elfcpp::Rel<32, false>&,
664*a9fa9459Szrj 		 unsigned int r_type, const Sized_symbol<32>*,
665*a9fa9459Szrj 		 const Symbol_value<32>*,
666*a9fa9459Szrj 		 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
667*a9fa9459Szrj 		 section_size_type);
668*a9fa9459Szrj 
669*a9fa9459Szrj     // Do a TLS General-Dynamic to Initial-Exec transition.
670*a9fa9459Szrj     inline void
671*a9fa9459Szrj     tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
672*a9fa9459Szrj 		 const elfcpp::Rel<32, false>&, unsigned int r_type,
673*a9fa9459Szrj 		 elfcpp::Elf_types<32>::Elf_Addr value,
674*a9fa9459Szrj 		 unsigned char* view,
675*a9fa9459Szrj 		 section_size_type view_size);
676*a9fa9459Szrj 
677*a9fa9459Szrj     // Do a TLS General-Dynamic to Local-Exec transition.
678*a9fa9459Szrj     inline void
679*a9fa9459Szrj     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
680*a9fa9459Szrj 		 Output_segment* tls_segment,
681*a9fa9459Szrj 		 const elfcpp::Rel<32, false>&, unsigned int r_type,
682*a9fa9459Szrj 		 elfcpp::Elf_types<32>::Elf_Addr value,
683*a9fa9459Szrj 		 unsigned char* view,
684*a9fa9459Szrj 		 section_size_type view_size);
685*a9fa9459Szrj 
686*a9fa9459Szrj     // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
687*a9fa9459Szrj     // transition.
688*a9fa9459Szrj     inline void
689*a9fa9459Szrj     tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
690*a9fa9459Szrj 		      const elfcpp::Rel<32, false>&, unsigned int r_type,
691*a9fa9459Szrj 		      elfcpp::Elf_types<32>::Elf_Addr value,
692*a9fa9459Szrj 		      unsigned char* view,
693*a9fa9459Szrj 		      section_size_type view_size);
694*a9fa9459Szrj 
695*a9fa9459Szrj     // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
696*a9fa9459Szrj     // transition.
697*a9fa9459Szrj     inline void
698*a9fa9459Szrj     tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
699*a9fa9459Szrj 		      Output_segment* tls_segment,
700*a9fa9459Szrj 		      const elfcpp::Rel<32, false>&, unsigned int r_type,
701*a9fa9459Szrj 		      elfcpp::Elf_types<32>::Elf_Addr value,
702*a9fa9459Szrj 		      unsigned char* view,
703*a9fa9459Szrj 		      section_size_type view_size);
704*a9fa9459Szrj 
705*a9fa9459Szrj     // Do a TLS Local-Dynamic to Local-Exec transition.
706*a9fa9459Szrj     inline void
707*a9fa9459Szrj     tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
708*a9fa9459Szrj 		 Output_segment* tls_segment,
709*a9fa9459Szrj 		 const elfcpp::Rel<32, false>&, unsigned int r_type,
710*a9fa9459Szrj 		 elfcpp::Elf_types<32>::Elf_Addr value,
711*a9fa9459Szrj 		 unsigned char* view,
712*a9fa9459Szrj 		 section_size_type view_size);
713*a9fa9459Szrj 
714*a9fa9459Szrj     // Do a TLS Initial-Exec to Local-Exec transition.
715*a9fa9459Szrj     static inline void
716*a9fa9459Szrj     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
717*a9fa9459Szrj 		 Output_segment* tls_segment,
718*a9fa9459Szrj 		 const elfcpp::Rel<32, false>&, unsigned int r_type,
719*a9fa9459Szrj 		 elfcpp::Elf_types<32>::Elf_Addr value,
720*a9fa9459Szrj 		 unsigned char* view,
721*a9fa9459Szrj 		 section_size_type view_size);
722*a9fa9459Szrj 
723*a9fa9459Szrj     // We need to keep track of which type of local dynamic relocation
724*a9fa9459Szrj     // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
725*a9fa9459Szrj     enum Local_dynamic_type
726*a9fa9459Szrj     {
727*a9fa9459Szrj       LOCAL_DYNAMIC_NONE,
728*a9fa9459Szrj       LOCAL_DYNAMIC_SUN,
729*a9fa9459Szrj       LOCAL_DYNAMIC_GNU
730*a9fa9459Szrj     };
731*a9fa9459Szrj 
732*a9fa9459Szrj     // This is set if we should skip the next reloc, which should be a
733*a9fa9459Szrj     // PLT32 reloc against ___tls_get_addr.
734*a9fa9459Szrj     bool skip_call_tls_get_addr_;
735*a9fa9459Szrj     // The type of local dynamic relocation we have seen in the section
736*a9fa9459Szrj     // being relocated, if any.
737*a9fa9459Szrj     Local_dynamic_type local_dynamic_type_;
738*a9fa9459Szrj   };
739*a9fa9459Szrj 
740*a9fa9459Szrj   // A class for inquiring about properties of a relocation,
741*a9fa9459Szrj   // used while scanning relocs during a relocatable link and
742*a9fa9459Szrj   // garbage collection.
743*a9fa9459Szrj   class Classify_reloc :
744*a9fa9459Szrj       public gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
745*a9fa9459Szrj   {
746*a9fa9459Szrj    public:
747*a9fa9459Szrj     typedef Reloc_types<elfcpp::SHT_REL, 32, false>::Reloc Reltype;
748*a9fa9459Szrj 
749*a9fa9459Szrj     // Return the explicit addend of the relocation (return 0 for SHT_REL).
750*a9fa9459Szrj     static elfcpp::Elf_types<32>::Elf_Swxword
get_r_addend(const Reltype *)751*a9fa9459Szrj     get_r_addend(const Reltype*)
752*a9fa9459Szrj     { return 0; }
753*a9fa9459Szrj 
754*a9fa9459Szrj     // Return the size of the addend of the relocation (only used for SHT_REL).
755*a9fa9459Szrj     static unsigned int
756*a9fa9459Szrj     get_size_for_reloc(unsigned int, Relobj*);
757*a9fa9459Szrj   };
758*a9fa9459Szrj 
759*a9fa9459Szrj   // Adjust TLS relocation type based on the options and whether this
760*a9fa9459Szrj   // is a local symbol.
761*a9fa9459Szrj   static tls::Tls_optimization
762*a9fa9459Szrj   optimize_tls_reloc(bool is_final, int r_type);
763*a9fa9459Szrj 
764*a9fa9459Szrj   // Check if relocation against this symbol is a candidate for
765*a9fa9459Szrj   // conversion from
766*a9fa9459Szrj   // mov foo@GOT(%reg), %reg
767*a9fa9459Szrj   // to
768*a9fa9459Szrj   // lea foo@GOTOFF(%reg), %reg.
769*a9fa9459Szrj   static bool
can_convert_mov_to_lea(const Symbol * gsym)770*a9fa9459Szrj   can_convert_mov_to_lea(const Symbol* gsym)
771*a9fa9459Szrj   {
772*a9fa9459Szrj     gold_assert(gsym != NULL);
773*a9fa9459Szrj     return (gsym->type() != elfcpp::STT_GNU_IFUNC
774*a9fa9459Szrj 	    && !gsym->is_undefined ()
775*a9fa9459Szrj 	    && !gsym->is_from_dynobj()
776*a9fa9459Szrj 	    && !gsym->is_preemptible()
777*a9fa9459Szrj 	    && (!parameters->options().shared()
778*a9fa9459Szrj 		|| (gsym->visibility() != elfcpp::STV_DEFAULT
779*a9fa9459Szrj 		    && gsym->visibility() != elfcpp::STV_PROTECTED)
780*a9fa9459Szrj 		|| parameters->options().Bsymbolic())
781*a9fa9459Szrj 	    && strcmp(gsym->name(), "_DYNAMIC") != 0);
782*a9fa9459Szrj   }
783*a9fa9459Szrj 
784*a9fa9459Szrj   // Get the GOT section, creating it if necessary.
785*a9fa9459Szrj   Output_data_got<32, false>*
786*a9fa9459Szrj   got_section(Symbol_table*, Layout*);
787*a9fa9459Szrj 
788*a9fa9459Szrj   // Get the GOT PLT section.
789*a9fa9459Szrj   Output_data_got_plt_i386*
got_plt_section() const790*a9fa9459Szrj   got_plt_section() const
791*a9fa9459Szrj   {
792*a9fa9459Szrj     gold_assert(this->got_plt_ != NULL);
793*a9fa9459Szrj     return this->got_plt_;
794*a9fa9459Szrj   }
795*a9fa9459Szrj 
796*a9fa9459Szrj   // Get the GOT section for TLSDESC entries.
797*a9fa9459Szrj   Output_data_got<32, false>*
got_tlsdesc_section() const798*a9fa9459Szrj   got_tlsdesc_section() const
799*a9fa9459Szrj   {
800*a9fa9459Szrj     gold_assert(this->got_tlsdesc_ != NULL);
801*a9fa9459Szrj     return this->got_tlsdesc_;
802*a9fa9459Szrj   }
803*a9fa9459Szrj 
804*a9fa9459Szrj   // Create the PLT section.
805*a9fa9459Szrj   void
806*a9fa9459Szrj   make_plt_section(Symbol_table* symtab, Layout* layout);
807*a9fa9459Szrj 
808*a9fa9459Szrj   // Create a PLT entry for a global symbol.
809*a9fa9459Szrj   void
810*a9fa9459Szrj   make_plt_entry(Symbol_table*, Layout*, Symbol*);
811*a9fa9459Szrj 
812*a9fa9459Szrj   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
813*a9fa9459Szrj   void
814*a9fa9459Szrj   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
815*a9fa9459Szrj 			     Sized_relobj_file<32, false>* relobj,
816*a9fa9459Szrj 			     unsigned int local_sym_index);
817*a9fa9459Szrj 
818*a9fa9459Szrj   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
819*a9fa9459Szrj   void
820*a9fa9459Szrj   define_tls_base_symbol(Symbol_table*, Layout*);
821*a9fa9459Szrj 
822*a9fa9459Szrj   // Create a GOT entry for the TLS module index.
823*a9fa9459Szrj   unsigned int
824*a9fa9459Szrj   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
825*a9fa9459Szrj 		      Sized_relobj_file<32, false>* object);
826*a9fa9459Szrj 
827*a9fa9459Szrj   // Get the PLT section.
828*a9fa9459Szrj   Output_data_plt_i386*
plt_section() const829*a9fa9459Szrj   plt_section() const
830*a9fa9459Szrj   {
831*a9fa9459Szrj     gold_assert(this->plt_ != NULL);
832*a9fa9459Szrj     return this->plt_;
833*a9fa9459Szrj   }
834*a9fa9459Szrj 
835*a9fa9459Szrj   // Get the dynamic reloc section, creating it if necessary.
836*a9fa9459Szrj   Reloc_section*
837*a9fa9459Szrj   rel_dyn_section(Layout*);
838*a9fa9459Szrj 
839*a9fa9459Szrj   // Get the section to use for TLS_DESC relocations.
840*a9fa9459Szrj   Reloc_section*
841*a9fa9459Szrj   rel_tls_desc_section(Layout*) const;
842*a9fa9459Szrj 
843*a9fa9459Szrj   // Get the section to use for IRELATIVE relocations.
844*a9fa9459Szrj   Reloc_section*
845*a9fa9459Szrj   rel_irelative_section(Layout*);
846*a9fa9459Szrj 
847*a9fa9459Szrj   // Add a potential copy relocation.
848*a9fa9459Szrj   void
copy_reloc(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * object,unsigned int shndx,Output_section * output_section,Symbol * sym,const elfcpp::Rel<32,false> & reloc)849*a9fa9459Szrj   copy_reloc(Symbol_table* symtab, Layout* layout,
850*a9fa9459Szrj 	     Sized_relobj_file<32, false>* object,
851*a9fa9459Szrj 	     unsigned int shndx, Output_section* output_section,
852*a9fa9459Szrj 	     Symbol* sym, const elfcpp::Rel<32, false>& reloc)
853*a9fa9459Szrj   {
854*a9fa9459Szrj     unsigned int r_type = elfcpp::elf_r_type<32>(reloc.get_r_info());
855*a9fa9459Szrj     this->copy_relocs_.copy_reloc(symtab, layout,
856*a9fa9459Szrj 				  symtab->get_sized_symbol<32>(sym),
857*a9fa9459Szrj 				  object, shndx, output_section,
858*a9fa9459Szrj 				  r_type, reloc.get_r_offset(), 0,
859*a9fa9459Szrj 				  this->rel_dyn_section(layout));
860*a9fa9459Szrj   }
861*a9fa9459Szrj 
862*a9fa9459Szrj   // Information about this specific target which we pass to the
863*a9fa9459Szrj   // general Target structure.
864*a9fa9459Szrj   static const Target::Target_info i386_info;
865*a9fa9459Szrj 
866*a9fa9459Szrj   // The types of GOT entries needed for this platform.
867*a9fa9459Szrj   // These values are exposed to the ABI in an incremental link.
868*a9fa9459Szrj   // Do not renumber existing values without changing the version
869*a9fa9459Szrj   // number of the .gnu_incremental_inputs section.
870*a9fa9459Szrj   enum Got_type
871*a9fa9459Szrj   {
872*a9fa9459Szrj     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
873*a9fa9459Szrj     GOT_TYPE_TLS_NOFFSET = 1,   // GOT entry for negative TLS offset
874*a9fa9459Szrj     GOT_TYPE_TLS_OFFSET = 2,    // GOT entry for positive TLS offset
875*a9fa9459Szrj     GOT_TYPE_TLS_PAIR = 3,      // GOT entry for TLS module/offset pair
876*a9fa9459Szrj     GOT_TYPE_TLS_DESC = 4       // GOT entry for TLS_DESC pair
877*a9fa9459Szrj   };
878*a9fa9459Szrj 
879*a9fa9459Szrj   // The GOT section.
880*a9fa9459Szrj   Output_data_got<32, false>* got_;
881*a9fa9459Szrj   // The PLT section.
882*a9fa9459Szrj   Output_data_plt_i386* plt_;
883*a9fa9459Szrj   // The GOT PLT section.
884*a9fa9459Szrj   Output_data_got_plt_i386* got_plt_;
885*a9fa9459Szrj   // The GOT section for IRELATIVE relocations.
886*a9fa9459Szrj   Output_data_space* got_irelative_;
887*a9fa9459Szrj   // The GOT section for TLSDESC relocations.
888*a9fa9459Szrj   Output_data_got<32, false>* got_tlsdesc_;
889*a9fa9459Szrj   // The _GLOBAL_OFFSET_TABLE_ symbol.
890*a9fa9459Szrj   Symbol* global_offset_table_;
891*a9fa9459Szrj   // The dynamic reloc section.
892*a9fa9459Szrj   Reloc_section* rel_dyn_;
893*a9fa9459Szrj   // The section to use for IRELATIVE relocs.
894*a9fa9459Szrj   Reloc_section* rel_irelative_;
895*a9fa9459Szrj   // Relocs saved to avoid a COPY reloc.
896*a9fa9459Szrj   Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
897*a9fa9459Szrj   // Offset of the GOT entry for the TLS module index.
898*a9fa9459Szrj   unsigned int got_mod_index_offset_;
899*a9fa9459Szrj   // True if the _TLS_MODULE_BASE_ symbol has been defined.
900*a9fa9459Szrj   bool tls_base_symbol_defined_;
901*a9fa9459Szrj };
902*a9fa9459Szrj 
903*a9fa9459Szrj const Target::Target_info Target_i386::i386_info =
904*a9fa9459Szrj {
905*a9fa9459Szrj   32,			// size
906*a9fa9459Szrj   false,		// is_big_endian
907*a9fa9459Szrj   elfcpp::EM_386,	// machine_code
908*a9fa9459Szrj   false,		// has_make_symbol
909*a9fa9459Szrj   false,		// has_resolve
910*a9fa9459Szrj   true,			// has_code_fill
911*a9fa9459Szrj   true,			// is_default_stack_executable
912*a9fa9459Szrj   true,			// can_icf_inline_merge_sections
913*a9fa9459Szrj   '\0',			// wrap_char
914*a9fa9459Szrj   "/usr/lib/libc.so.1",	// dynamic_linker
915*a9fa9459Szrj   0x08048000,		// default_text_segment_address
916*a9fa9459Szrj   0x1000,		// abi_pagesize (overridable by -z max-page-size)
917*a9fa9459Szrj   0x1000,		// common_pagesize (overridable by -z common-page-size)
918*a9fa9459Szrj   false,                // isolate_execinstr
919*a9fa9459Szrj   0,                    // rosegment_gap
920*a9fa9459Szrj   elfcpp::SHN_UNDEF,	// small_common_shndx
921*a9fa9459Szrj   elfcpp::SHN_UNDEF,	// large_common_shndx
922*a9fa9459Szrj   0,			// small_common_section_flags
923*a9fa9459Szrj   0,			// large_common_section_flags
924*a9fa9459Szrj   NULL,			// attributes_section
925*a9fa9459Szrj   NULL,			// attributes_vendor
926*a9fa9459Szrj   "_start",		// entry_symbol_name
927*a9fa9459Szrj   32,			// hash_entry_size
928*a9fa9459Szrj };
929*a9fa9459Szrj 
930*a9fa9459Szrj // Get the GOT section, creating it if necessary.
931*a9fa9459Szrj 
932*a9fa9459Szrj Output_data_got<32, false>*
got_section(Symbol_table * symtab,Layout * layout)933*a9fa9459Szrj Target_i386::got_section(Symbol_table* symtab, Layout* layout)
934*a9fa9459Szrj {
935*a9fa9459Szrj   if (this->got_ == NULL)
936*a9fa9459Szrj     {
937*a9fa9459Szrj       gold_assert(symtab != NULL && layout != NULL);
938*a9fa9459Szrj 
939*a9fa9459Szrj       this->got_ = new Output_data_got<32, false>();
940*a9fa9459Szrj 
941*a9fa9459Szrj       // When using -z now, we can treat .got.plt as a relro section.
942*a9fa9459Szrj       // Without -z now, it is modified after program startup by lazy
943*a9fa9459Szrj       // PLT relocations.
944*a9fa9459Szrj       bool is_got_plt_relro = parameters->options().now();
945*a9fa9459Szrj       Output_section_order got_order = (is_got_plt_relro
946*a9fa9459Szrj 					? ORDER_RELRO
947*a9fa9459Szrj 					: ORDER_RELRO_LAST);
948*a9fa9459Szrj       Output_section_order got_plt_order = (is_got_plt_relro
949*a9fa9459Szrj 					    ? ORDER_RELRO
950*a9fa9459Szrj 					    : ORDER_NON_RELRO_FIRST);
951*a9fa9459Szrj 
952*a9fa9459Szrj       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
953*a9fa9459Szrj 				      (elfcpp::SHF_ALLOC
954*a9fa9459Szrj 				       | elfcpp::SHF_WRITE),
955*a9fa9459Szrj 				      this->got_, got_order, true);
956*a9fa9459Szrj 
957*a9fa9459Szrj       this->got_plt_ = new Output_data_got_plt_i386(layout);
958*a9fa9459Szrj       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
959*a9fa9459Szrj 				      (elfcpp::SHF_ALLOC
960*a9fa9459Szrj 				       | elfcpp::SHF_WRITE),
961*a9fa9459Szrj 				      this->got_plt_, got_plt_order,
962*a9fa9459Szrj 				      is_got_plt_relro);
963*a9fa9459Szrj 
964*a9fa9459Szrj       // The first three entries are reserved.
965*a9fa9459Szrj       this->got_plt_->set_current_data_size(3 * 4);
966*a9fa9459Szrj 
967*a9fa9459Szrj       if (!is_got_plt_relro)
968*a9fa9459Szrj 	{
969*a9fa9459Szrj 	  // Those bytes can go into the relro segment.
970*a9fa9459Szrj 	  layout->increase_relro(3 * 4);
971*a9fa9459Szrj 	}
972*a9fa9459Szrj 
973*a9fa9459Szrj       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
974*a9fa9459Szrj       this->global_offset_table_ =
975*a9fa9459Szrj 	symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
976*a9fa9459Szrj 				      Symbol_table::PREDEFINED,
977*a9fa9459Szrj 				      this->got_plt_,
978*a9fa9459Szrj 				      0, 0, elfcpp::STT_OBJECT,
979*a9fa9459Szrj 				      elfcpp::STB_LOCAL,
980*a9fa9459Szrj 				      elfcpp::STV_HIDDEN, 0,
981*a9fa9459Szrj 				      false, false);
982*a9fa9459Szrj 
983*a9fa9459Szrj       // If there are any IRELATIVE relocations, they get GOT entries
984*a9fa9459Szrj       // in .got.plt after the jump slot relocations.
985*a9fa9459Szrj       this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
986*a9fa9459Szrj       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
987*a9fa9459Szrj 				      (elfcpp::SHF_ALLOC
988*a9fa9459Szrj 				       | elfcpp::SHF_WRITE),
989*a9fa9459Szrj 				      this->got_irelative_,
990*a9fa9459Szrj 				      got_plt_order, is_got_plt_relro);
991*a9fa9459Szrj 
992*a9fa9459Szrj       // If there are any TLSDESC relocations, they get GOT entries in
993*a9fa9459Szrj       // .got.plt after the jump slot entries.
994*a9fa9459Szrj       this->got_tlsdesc_ = new Output_data_got<32, false>();
995*a9fa9459Szrj       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
996*a9fa9459Szrj 				      (elfcpp::SHF_ALLOC
997*a9fa9459Szrj 				       | elfcpp::SHF_WRITE),
998*a9fa9459Szrj 				      this->got_tlsdesc_,
999*a9fa9459Szrj 				      got_plt_order, is_got_plt_relro);
1000*a9fa9459Szrj     }
1001*a9fa9459Szrj 
1002*a9fa9459Szrj   return this->got_;
1003*a9fa9459Szrj }
1004*a9fa9459Szrj 
1005*a9fa9459Szrj // Get the dynamic reloc section, creating it if necessary.
1006*a9fa9459Szrj 
1007*a9fa9459Szrj Target_i386::Reloc_section*
rel_dyn_section(Layout * layout)1008*a9fa9459Szrj Target_i386::rel_dyn_section(Layout* layout)
1009*a9fa9459Szrj {
1010*a9fa9459Szrj   if (this->rel_dyn_ == NULL)
1011*a9fa9459Szrj     {
1012*a9fa9459Szrj       gold_assert(layout != NULL);
1013*a9fa9459Szrj       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1014*a9fa9459Szrj       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1015*a9fa9459Szrj 				      elfcpp::SHF_ALLOC, this->rel_dyn_,
1016*a9fa9459Szrj 				      ORDER_DYNAMIC_RELOCS, false);
1017*a9fa9459Szrj     }
1018*a9fa9459Szrj   return this->rel_dyn_;
1019*a9fa9459Szrj }
1020*a9fa9459Szrj 
1021*a9fa9459Szrj // Get the section to use for IRELATIVE relocs, creating it if
1022*a9fa9459Szrj // necessary.  These go in .rel.dyn, but only after all other dynamic
1023*a9fa9459Szrj // relocations.  They need to follow the other dynamic relocations so
1024*a9fa9459Szrj // that they can refer to global variables initialized by those
1025*a9fa9459Szrj // relocs.
1026*a9fa9459Szrj 
1027*a9fa9459Szrj Target_i386::Reloc_section*
rel_irelative_section(Layout * layout)1028*a9fa9459Szrj Target_i386::rel_irelative_section(Layout* layout)
1029*a9fa9459Szrj {
1030*a9fa9459Szrj   if (this->rel_irelative_ == NULL)
1031*a9fa9459Szrj     {
1032*a9fa9459Szrj       // Make sure we have already create the dynamic reloc section.
1033*a9fa9459Szrj       this->rel_dyn_section(layout);
1034*a9fa9459Szrj       this->rel_irelative_ = new Reloc_section(false);
1035*a9fa9459Szrj       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1036*a9fa9459Szrj 				      elfcpp::SHF_ALLOC, this->rel_irelative_,
1037*a9fa9459Szrj 				      ORDER_DYNAMIC_RELOCS, false);
1038*a9fa9459Szrj       gold_assert(this->rel_dyn_->output_section()
1039*a9fa9459Szrj 		  == this->rel_irelative_->output_section());
1040*a9fa9459Szrj     }
1041*a9fa9459Szrj   return this->rel_irelative_;
1042*a9fa9459Szrj }
1043*a9fa9459Szrj 
1044*a9fa9459Szrj // Write the first three reserved words of the .got.plt section.
1045*a9fa9459Szrj // The remainder of the section is written while writing the PLT
1046*a9fa9459Szrj // in Output_data_plt_i386::do_write.
1047*a9fa9459Szrj 
1048*a9fa9459Szrj void
do_write(Output_file * of)1049*a9fa9459Szrj Output_data_got_plt_i386::do_write(Output_file* of)
1050*a9fa9459Szrj {
1051*a9fa9459Szrj   // The first entry in the GOT is the address of the .dynamic section
1052*a9fa9459Szrj   // aka the PT_DYNAMIC segment.  The next two entries are reserved.
1053*a9fa9459Szrj   // We saved space for them when we created the section in
1054*a9fa9459Szrj   // Target_i386::got_section.
1055*a9fa9459Szrj   const off_t got_file_offset = this->offset();
1056*a9fa9459Szrj   gold_assert(this->data_size() >= 12);
1057*a9fa9459Szrj   unsigned char* const got_view = of->get_output_view(got_file_offset, 12);
1058*a9fa9459Szrj   Output_section* dynamic = this->layout_->dynamic_section();
1059*a9fa9459Szrj   uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1060*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(got_view, dynamic_addr);
1061*a9fa9459Szrj   memset(got_view + 4, 0, 8);
1062*a9fa9459Szrj   of->write_output_view(got_file_offset, 12, got_view);
1063*a9fa9459Szrj }
1064*a9fa9459Szrj 
1065*a9fa9459Szrj // Create the PLT section.  The ordinary .got section is an argument,
1066*a9fa9459Szrj // since we need to refer to the start.  We also create our own .got
1067*a9fa9459Szrj // section just for PLT entries.
1068*a9fa9459Szrj 
Output_data_plt_i386(Layout * layout,uint64_t addralign,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)1069*a9fa9459Szrj Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
1070*a9fa9459Szrj 					   uint64_t addralign,
1071*a9fa9459Szrj 					   Output_data_got_plt_i386* got_plt,
1072*a9fa9459Szrj 					   Output_data_space* got_irelative)
1073*a9fa9459Szrj   : Output_section_data(addralign),
1074*a9fa9459Szrj     tls_desc_rel_(NULL), irelative_rel_(NULL), got_plt_(got_plt),
1075*a9fa9459Szrj     got_irelative_(got_irelative), count_(0), irelative_count_(0),
1076*a9fa9459Szrj     global_ifuncs_(), local_ifuncs_()
1077*a9fa9459Szrj {
1078*a9fa9459Szrj   this->rel_ = new Reloc_section(false);
1079*a9fa9459Szrj   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1080*a9fa9459Szrj 				  elfcpp::SHF_ALLOC, this->rel_,
1081*a9fa9459Szrj 				  ORDER_DYNAMIC_PLT_RELOCS, false);
1082*a9fa9459Szrj }
1083*a9fa9459Szrj 
1084*a9fa9459Szrj void
do_adjust_output_section(Output_section * os)1085*a9fa9459Szrj Output_data_plt_i386::do_adjust_output_section(Output_section* os)
1086*a9fa9459Szrj {
1087*a9fa9459Szrj   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
1088*a9fa9459Szrj   // linker, and so do we.
1089*a9fa9459Szrj   os->set_entsize(4);
1090*a9fa9459Szrj }
1091*a9fa9459Szrj 
1092*a9fa9459Szrj // Add an entry to the PLT.
1093*a9fa9459Szrj 
1094*a9fa9459Szrj void
add_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)1095*a9fa9459Szrj Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
1096*a9fa9459Szrj 				Symbol* gsym)
1097*a9fa9459Szrj {
1098*a9fa9459Szrj   gold_assert(!gsym->has_plt_offset());
1099*a9fa9459Szrj 
1100*a9fa9459Szrj   // Every PLT entry needs a reloc.
1101*a9fa9459Szrj   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1102*a9fa9459Szrj       && gsym->can_use_relative_reloc(false))
1103*a9fa9459Szrj     {
1104*a9fa9459Szrj       gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size());
1105*a9fa9459Szrj       ++this->irelative_count_;
1106*a9fa9459Szrj       section_offset_type got_offset =
1107*a9fa9459Szrj 	this->got_irelative_->current_data_size();
1108*a9fa9459Szrj       this->got_irelative_->set_current_data_size(got_offset + 4);
1109*a9fa9459Szrj       Reloc_section* rel = this->rel_irelative(symtab, layout);
1110*a9fa9459Szrj       rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
1111*a9fa9459Szrj 					this->got_irelative_, got_offset);
1112*a9fa9459Szrj       struct Global_ifunc gi;
1113*a9fa9459Szrj       gi.sym = gsym;
1114*a9fa9459Szrj       gi.got_offset = got_offset;
1115*a9fa9459Szrj       this->global_ifuncs_.push_back(gi);
1116*a9fa9459Szrj     }
1117*a9fa9459Szrj   else
1118*a9fa9459Szrj     {
1119*a9fa9459Szrj       // When setting the PLT offset we skip the initial reserved PLT
1120*a9fa9459Szrj       // entry.
1121*a9fa9459Szrj       gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size());
1122*a9fa9459Szrj 
1123*a9fa9459Szrj       ++this->count_;
1124*a9fa9459Szrj 
1125*a9fa9459Szrj       section_offset_type got_offset = this->got_plt_->current_data_size();
1126*a9fa9459Szrj 
1127*a9fa9459Szrj       // Every PLT entry needs a GOT entry which points back to the
1128*a9fa9459Szrj       // PLT entry (this will be changed by the dynamic linker,
1129*a9fa9459Szrj       // normally lazily when the function is called).
1130*a9fa9459Szrj       this->got_plt_->set_current_data_size(got_offset + 4);
1131*a9fa9459Szrj 
1132*a9fa9459Szrj       gsym->set_needs_dynsym_entry();
1133*a9fa9459Szrj       this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
1134*a9fa9459Szrj 			     got_offset);
1135*a9fa9459Szrj     }
1136*a9fa9459Szrj 
1137*a9fa9459Szrj   // Note that we don't need to save the symbol.  The contents of the
1138*a9fa9459Szrj   // PLT are independent of which symbols are used.  The symbols only
1139*a9fa9459Szrj   // appear in the relocations.
1140*a9fa9459Szrj }
1141*a9fa9459Szrj 
1142*a9fa9459Szrj // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
1143*a9fa9459Szrj // the PLT offset.
1144*a9fa9459Szrj 
1145*a9fa9459Szrj unsigned int
add_local_ifunc_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * relobj,unsigned int local_sym_index)1146*a9fa9459Szrj Output_data_plt_i386::add_local_ifunc_entry(
1147*a9fa9459Szrj     Symbol_table* symtab,
1148*a9fa9459Szrj     Layout* layout,
1149*a9fa9459Szrj     Sized_relobj_file<32, false>* relobj,
1150*a9fa9459Szrj     unsigned int local_sym_index)
1151*a9fa9459Szrj {
1152*a9fa9459Szrj   unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1153*a9fa9459Szrj   ++this->irelative_count_;
1154*a9fa9459Szrj 
1155*a9fa9459Szrj   section_offset_type got_offset = this->got_irelative_->current_data_size();
1156*a9fa9459Szrj 
1157*a9fa9459Szrj   // Every PLT entry needs a GOT entry which points back to the PLT
1158*a9fa9459Szrj   // entry.
1159*a9fa9459Szrj   this->got_irelative_->set_current_data_size(got_offset + 4);
1160*a9fa9459Szrj 
1161*a9fa9459Szrj   // Every PLT entry needs a reloc.
1162*a9fa9459Szrj   Reloc_section* rel = this->rel_irelative(symtab, layout);
1163*a9fa9459Szrj   rel->add_symbolless_local_addend(relobj, local_sym_index,
1164*a9fa9459Szrj 				   elfcpp::R_386_IRELATIVE,
1165*a9fa9459Szrj 				   this->got_irelative_, got_offset);
1166*a9fa9459Szrj 
1167*a9fa9459Szrj   struct Local_ifunc li;
1168*a9fa9459Szrj   li.object = relobj;
1169*a9fa9459Szrj   li.local_sym_index = local_sym_index;
1170*a9fa9459Szrj   li.got_offset = got_offset;
1171*a9fa9459Szrj   this->local_ifuncs_.push_back(li);
1172*a9fa9459Szrj 
1173*a9fa9459Szrj   return plt_offset;
1174*a9fa9459Szrj }
1175*a9fa9459Szrj 
1176*a9fa9459Szrj // Return where the TLS_DESC relocations should go, creating it if
1177*a9fa9459Szrj // necessary. These follow the JUMP_SLOT relocations.
1178*a9fa9459Szrj 
1179*a9fa9459Szrj Output_data_plt_i386::Reloc_section*
rel_tls_desc(Layout * layout)1180*a9fa9459Szrj Output_data_plt_i386::rel_tls_desc(Layout* layout)
1181*a9fa9459Szrj {
1182*a9fa9459Szrj   if (this->tls_desc_rel_ == NULL)
1183*a9fa9459Szrj     {
1184*a9fa9459Szrj       this->tls_desc_rel_ = new Reloc_section(false);
1185*a9fa9459Szrj       layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1186*a9fa9459Szrj 				      elfcpp::SHF_ALLOC, this->tls_desc_rel_,
1187*a9fa9459Szrj 				      ORDER_DYNAMIC_PLT_RELOCS, false);
1188*a9fa9459Szrj       gold_assert(this->tls_desc_rel_->output_section()
1189*a9fa9459Szrj 		  == this->rel_->output_section());
1190*a9fa9459Szrj     }
1191*a9fa9459Szrj   return this->tls_desc_rel_;
1192*a9fa9459Szrj }
1193*a9fa9459Szrj 
1194*a9fa9459Szrj // Return where the IRELATIVE relocations should go in the PLT.  These
1195*a9fa9459Szrj // follow the JUMP_SLOT and TLS_DESC relocations.
1196*a9fa9459Szrj 
1197*a9fa9459Szrj Output_data_plt_i386::Reloc_section*
rel_irelative(Symbol_table * symtab,Layout * layout)1198*a9fa9459Szrj Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
1199*a9fa9459Szrj {
1200*a9fa9459Szrj   if (this->irelative_rel_ == NULL)
1201*a9fa9459Szrj     {
1202*a9fa9459Szrj       // Make sure we have a place for the TLS_DESC relocations, in
1203*a9fa9459Szrj       // case we see any later on.
1204*a9fa9459Szrj       this->rel_tls_desc(layout);
1205*a9fa9459Szrj       this->irelative_rel_ = new Reloc_section(false);
1206*a9fa9459Szrj       layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1207*a9fa9459Szrj 				      elfcpp::SHF_ALLOC, this->irelative_rel_,
1208*a9fa9459Szrj 				      ORDER_DYNAMIC_PLT_RELOCS, false);
1209*a9fa9459Szrj       gold_assert(this->irelative_rel_->output_section()
1210*a9fa9459Szrj 		  == this->rel_->output_section());
1211*a9fa9459Szrj 
1212*a9fa9459Szrj       if (parameters->doing_static_link())
1213*a9fa9459Szrj 	{
1214*a9fa9459Szrj 	  // A statically linked executable will only have a .rel.plt
1215*a9fa9459Szrj 	  // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
1216*a9fa9459Szrj 	  // symbols.  The library will use these symbols to locate
1217*a9fa9459Szrj 	  // the IRELATIVE relocs at program startup time.
1218*a9fa9459Szrj 	  symtab->define_in_output_data("__rel_iplt_start", NULL,
1219*a9fa9459Szrj 					Symbol_table::PREDEFINED,
1220*a9fa9459Szrj 					this->irelative_rel_, 0, 0,
1221*a9fa9459Szrj 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1222*a9fa9459Szrj 					elfcpp::STV_HIDDEN, 0, false, true);
1223*a9fa9459Szrj 	  symtab->define_in_output_data("__rel_iplt_end", NULL,
1224*a9fa9459Szrj 					Symbol_table::PREDEFINED,
1225*a9fa9459Szrj 					this->irelative_rel_, 0, 0,
1226*a9fa9459Szrj 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1227*a9fa9459Szrj 					elfcpp::STV_HIDDEN, 0, true, true);
1228*a9fa9459Szrj 	}
1229*a9fa9459Szrj     }
1230*a9fa9459Szrj   return this->irelative_rel_;
1231*a9fa9459Szrj }
1232*a9fa9459Szrj 
1233*a9fa9459Szrj // Return the PLT address to use for a global symbol.
1234*a9fa9459Szrj 
1235*a9fa9459Szrj uint64_t
address_for_global(const Symbol * gsym)1236*a9fa9459Szrj Output_data_plt_i386::address_for_global(const Symbol* gsym)
1237*a9fa9459Szrj {
1238*a9fa9459Szrj   uint64_t offset = 0;
1239*a9fa9459Szrj   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1240*a9fa9459Szrj       && gsym->can_use_relative_reloc(false))
1241*a9fa9459Szrj     offset = (this->count_ + 1) * this->get_plt_entry_size();
1242*a9fa9459Szrj   return this->address() + offset + gsym->plt_offset();
1243*a9fa9459Szrj }
1244*a9fa9459Szrj 
1245*a9fa9459Szrj // Return the PLT address to use for a local symbol.  These are always
1246*a9fa9459Szrj // IRELATIVE relocs.
1247*a9fa9459Szrj 
1248*a9fa9459Szrj uint64_t
address_for_local(const Relobj * object,unsigned int r_sym)1249*a9fa9459Szrj Output_data_plt_i386::address_for_local(const Relobj* object,
1250*a9fa9459Szrj 					unsigned int r_sym)
1251*a9fa9459Szrj {
1252*a9fa9459Szrj   return (this->address()
1253*a9fa9459Szrj 	  + (this->count_ + 1) * this->get_plt_entry_size()
1254*a9fa9459Szrj 	  + object->local_plt_offset(r_sym));
1255*a9fa9459Szrj }
1256*a9fa9459Szrj 
1257*a9fa9459Szrj // The first entry in the PLT for an executable.
1258*a9fa9459Szrj 
1259*a9fa9459Szrj const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] =
1260*a9fa9459Szrj {
1261*a9fa9459Szrj   0xff, 0x35,	// pushl contents of memory address
1262*a9fa9459Szrj   0, 0, 0, 0,	// replaced with address of .got + 4
1263*a9fa9459Szrj   0xff, 0x25,	// jmp indirect
1264*a9fa9459Szrj   0, 0, 0, 0,	// replaced with address of .got + 8
1265*a9fa9459Szrj   0, 0, 0, 0	// unused
1266*a9fa9459Szrj };
1267*a9fa9459Szrj 
1268*a9fa9459Szrj void
do_fill_first_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr got_address)1269*a9fa9459Szrj Output_data_plt_i386_exec::do_fill_first_plt_entry(
1270*a9fa9459Szrj     unsigned char* pov,
1271*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr got_address)
1272*a9fa9459Szrj {
1273*a9fa9459Szrj   memcpy(pov, first_plt_entry, plt_entry_size);
1274*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1275*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1276*a9fa9459Szrj }
1277*a9fa9459Szrj 
1278*a9fa9459Szrj // The first entry in the PLT for a shared object.
1279*a9fa9459Szrj 
1280*a9fa9459Szrj const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] =
1281*a9fa9459Szrj {
1282*a9fa9459Szrj   0xff, 0xb3, 4, 0, 0, 0,	// pushl 4(%ebx)
1283*a9fa9459Szrj   0xff, 0xa3, 8, 0, 0, 0,	// jmp *8(%ebx)
1284*a9fa9459Szrj   0, 0, 0, 0			// unused
1285*a9fa9459Szrj };
1286*a9fa9459Szrj 
1287*a9fa9459Szrj void
do_fill_first_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr)1288*a9fa9459Szrj Output_data_plt_i386_dyn::do_fill_first_plt_entry(
1289*a9fa9459Szrj     unsigned char* pov,
1290*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr)
1291*a9fa9459Szrj {
1292*a9fa9459Szrj   memcpy(pov, first_plt_entry, plt_entry_size);
1293*a9fa9459Szrj }
1294*a9fa9459Szrj 
1295*a9fa9459Szrj // Subsequent entries in the PLT for an executable.
1296*a9fa9459Szrj 
1297*a9fa9459Szrj const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] =
1298*a9fa9459Szrj {
1299*a9fa9459Szrj   0xff, 0x25,	// jmp indirect
1300*a9fa9459Szrj   0, 0, 0, 0,	// replaced with address of symbol in .got
1301*a9fa9459Szrj   0x68,		// pushl immediate
1302*a9fa9459Szrj   0, 0, 0, 0,	// replaced with offset into relocation table
1303*a9fa9459Szrj   0xe9,		// jmp relative
1304*a9fa9459Szrj   0, 0, 0, 0	// replaced with offset to start of .plt
1305*a9fa9459Szrj };
1306*a9fa9459Szrj 
1307*a9fa9459Szrj unsigned int
do_fill_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr got_address,unsigned int got_offset,unsigned int plt_offset,unsigned int plt_rel_offset)1308*a9fa9459Szrj Output_data_plt_i386_exec::do_fill_plt_entry(
1309*a9fa9459Szrj     unsigned char* pov,
1310*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr got_address,
1311*a9fa9459Szrj     unsigned int got_offset,
1312*a9fa9459Szrj     unsigned int plt_offset,
1313*a9fa9459Szrj     unsigned int plt_rel_offset)
1314*a9fa9459Szrj {
1315*a9fa9459Szrj   memcpy(pov, plt_entry, plt_entry_size);
1316*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1317*a9fa9459Szrj 					      got_address + got_offset);
1318*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1319*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1320*a9fa9459Szrj   return 6;
1321*a9fa9459Szrj }
1322*a9fa9459Szrj 
1323*a9fa9459Szrj // Subsequent entries in the PLT for a shared object.
1324*a9fa9459Szrj 
1325*a9fa9459Szrj const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] =
1326*a9fa9459Szrj {
1327*a9fa9459Szrj   0xff, 0xa3,	// jmp *offset(%ebx)
1328*a9fa9459Szrj   0, 0, 0, 0,	// replaced with offset of symbol in .got
1329*a9fa9459Szrj   0x68,		// pushl immediate
1330*a9fa9459Szrj   0, 0, 0, 0,	// replaced with offset into relocation table
1331*a9fa9459Szrj   0xe9,		// jmp relative
1332*a9fa9459Szrj   0, 0, 0, 0	// replaced with offset to start of .plt
1333*a9fa9459Szrj };
1334*a9fa9459Szrj 
1335*a9fa9459Szrj unsigned int
do_fill_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr,unsigned int got_offset,unsigned int plt_offset,unsigned int plt_rel_offset)1336*a9fa9459Szrj Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov,
1337*a9fa9459Szrj 					    elfcpp::Elf_types<32>::Elf_Addr,
1338*a9fa9459Szrj 					    unsigned int got_offset,
1339*a9fa9459Szrj 					    unsigned int plt_offset,
1340*a9fa9459Szrj 					    unsigned int plt_rel_offset)
1341*a9fa9459Szrj {
1342*a9fa9459Szrj   memcpy(pov, plt_entry, plt_entry_size);
1343*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1344*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1345*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1346*a9fa9459Szrj   return 6;
1347*a9fa9459Szrj }
1348*a9fa9459Szrj 
1349*a9fa9459Szrj // The .eh_frame unwind information for the PLT.
1350*a9fa9459Szrj 
1351*a9fa9459Szrj const unsigned char
1352*a9fa9459Szrj Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1353*a9fa9459Szrj {
1354*a9fa9459Szrj   1,				// CIE version.
1355*a9fa9459Szrj   'z',				// Augmentation: augmentation size included.
1356*a9fa9459Szrj   'R',				// Augmentation: FDE encoding included.
1357*a9fa9459Szrj   '\0',				// End of augmentation string.
1358*a9fa9459Szrj   1,				// Code alignment factor.
1359*a9fa9459Szrj   0x7c,				// Data alignment factor.
1360*a9fa9459Szrj   8,				// Return address column.
1361*a9fa9459Szrj   1,				// Augmentation size.
1362*a9fa9459Szrj   (elfcpp::DW_EH_PE_pcrel	// FDE encoding.
1363*a9fa9459Szrj    | elfcpp::DW_EH_PE_sdata4),
1364*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa, 4, 4,	// DW_CFA_def_cfa: r4 (esp) ofs 4.
1365*a9fa9459Szrj   elfcpp::DW_CFA_offset + 8, 1,	// DW_CFA_offset: r8 (eip) at cfa-4.
1366*a9fa9459Szrj   elfcpp::DW_CFA_nop,		// Align to 16 bytes.
1367*a9fa9459Szrj   elfcpp::DW_CFA_nop
1368*a9fa9459Szrj };
1369*a9fa9459Szrj 
1370*a9fa9459Szrj const unsigned char
1371*a9fa9459Szrj Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1372*a9fa9459Szrj {
1373*a9fa9459Szrj   0, 0, 0, 0,				// Replaced with offset to .plt.
1374*a9fa9459Szrj   0, 0, 0, 0,				// Replaced with size of .plt.
1375*a9fa9459Szrj   0,					// Augmentation size.
1376*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa_offset, 8,	// DW_CFA_def_cfa_offset: 8.
1377*a9fa9459Szrj   elfcpp::DW_CFA_advance_loc + 6,	// Advance 6 to __PLT__ + 6.
1378*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa_offset, 12,	// DW_CFA_def_cfa_offset: 12.
1379*a9fa9459Szrj   elfcpp::DW_CFA_advance_loc + 10,	// Advance 10 to __PLT__ + 16.
1380*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa_expression,	// DW_CFA_def_cfa_expression.
1381*a9fa9459Szrj   11,					// Block length.
1382*a9fa9459Szrj   elfcpp::DW_OP_breg4, 4,		// Push %esp + 4.
1383*a9fa9459Szrj   elfcpp::DW_OP_breg8, 0,		// Push %eip.
1384*a9fa9459Szrj   elfcpp::DW_OP_lit15,			// Push 0xf.
1385*a9fa9459Szrj   elfcpp::DW_OP_and,			// & (%eip & 0xf).
1386*a9fa9459Szrj   elfcpp::DW_OP_lit11,			// Push 0xb.
1387*a9fa9459Szrj   elfcpp::DW_OP_ge,			// >= ((%eip & 0xf) >= 0xb)
1388*a9fa9459Szrj   elfcpp::DW_OP_lit2,			// Push 2.
1389*a9fa9459Szrj   elfcpp::DW_OP_shl,			// << (((%eip & 0xf) >= 0xb) << 2)
1390*a9fa9459Szrj   elfcpp::DW_OP_plus,			// + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1391*a9fa9459Szrj   elfcpp::DW_CFA_nop,			// Align to 32 bytes.
1392*a9fa9459Szrj   elfcpp::DW_CFA_nop,
1393*a9fa9459Szrj   elfcpp::DW_CFA_nop,
1394*a9fa9459Szrj   elfcpp::DW_CFA_nop
1395*a9fa9459Szrj };
1396*a9fa9459Szrj 
1397*a9fa9459Szrj // Write out the PLT.  This uses the hand-coded instructions above,
1398*a9fa9459Szrj // and adjusts them as needed.  This is all specified by the i386 ELF
1399*a9fa9459Szrj // Processor Supplement.
1400*a9fa9459Szrj 
1401*a9fa9459Szrj void
do_write(Output_file * of)1402*a9fa9459Szrj Output_data_plt_i386::do_write(Output_file* of)
1403*a9fa9459Szrj {
1404*a9fa9459Szrj   const off_t offset = this->offset();
1405*a9fa9459Szrj   const section_size_type oview_size =
1406*a9fa9459Szrj     convert_to_section_size_type(this->data_size());
1407*a9fa9459Szrj   unsigned char* const oview = of->get_output_view(offset, oview_size);
1408*a9fa9459Szrj 
1409*a9fa9459Szrj   const off_t got_file_offset = this->got_plt_->offset();
1410*a9fa9459Szrj   gold_assert(parameters->incremental_update()
1411*a9fa9459Szrj 	      || (got_file_offset + this->got_plt_->data_size()
1412*a9fa9459Szrj 		  == this->got_irelative_->offset()));
1413*a9fa9459Szrj   const section_size_type got_size =
1414*a9fa9459Szrj     convert_to_section_size_type(this->got_plt_->data_size()
1415*a9fa9459Szrj 				 + this->got_irelative_->data_size());
1416*a9fa9459Szrj 
1417*a9fa9459Szrj   unsigned char* const got_view = of->get_output_view(got_file_offset,
1418*a9fa9459Szrj 						      got_size);
1419*a9fa9459Szrj 
1420*a9fa9459Szrj   unsigned char* pov = oview;
1421*a9fa9459Szrj 
1422*a9fa9459Szrj   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1423*a9fa9459Szrj   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1424*a9fa9459Szrj 
1425*a9fa9459Szrj   this->fill_first_plt_entry(pov, got_address);
1426*a9fa9459Szrj   pov += this->get_plt_entry_size();
1427*a9fa9459Szrj 
1428*a9fa9459Szrj   // The first three entries in the GOT are reserved, and are written
1429*a9fa9459Szrj   // by Output_data_got_plt_i386::do_write.
1430*a9fa9459Szrj   unsigned char* got_pov = got_view + 12;
1431*a9fa9459Szrj 
1432*a9fa9459Szrj   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1433*a9fa9459Szrj 
1434*a9fa9459Szrj   unsigned int plt_offset = this->get_plt_entry_size();
1435*a9fa9459Szrj   unsigned int plt_rel_offset = 0;
1436*a9fa9459Szrj   unsigned int got_offset = 12;
1437*a9fa9459Szrj   const unsigned int count = this->count_ + this->irelative_count_;
1438*a9fa9459Szrj   for (unsigned int i = 0;
1439*a9fa9459Szrj        i < count;
1440*a9fa9459Szrj        ++i,
1441*a9fa9459Szrj 	 pov += this->get_plt_entry_size(),
1442*a9fa9459Szrj 	 got_pov += 4,
1443*a9fa9459Szrj 	 plt_offset += this->get_plt_entry_size(),
1444*a9fa9459Szrj 	 plt_rel_offset += rel_size,
1445*a9fa9459Szrj 	 got_offset += 4)
1446*a9fa9459Szrj     {
1447*a9fa9459Szrj       // Set and adjust the PLT entry itself.
1448*a9fa9459Szrj       unsigned int lazy_offset = this->fill_plt_entry(pov,
1449*a9fa9459Szrj 						      got_address,
1450*a9fa9459Szrj 						      got_offset,
1451*a9fa9459Szrj 						      plt_offset,
1452*a9fa9459Szrj 						      plt_rel_offset);
1453*a9fa9459Szrj 
1454*a9fa9459Szrj       // Set the entry in the GOT.
1455*a9fa9459Szrj       elfcpp::Swap<32, false>::writeval(got_pov,
1456*a9fa9459Szrj 					plt_address + plt_offset + lazy_offset);
1457*a9fa9459Szrj     }
1458*a9fa9459Szrj 
1459*a9fa9459Szrj   // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1460*a9fa9459Szrj   // the GOT to point to the actual symbol value, rather than point to
1461*a9fa9459Szrj   // the PLT entry.  That will let the dynamic linker call the right
1462*a9fa9459Szrj   // function when resolving IRELATIVE relocations.
1463*a9fa9459Szrj   unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
1464*a9fa9459Szrj   for (std::vector<Global_ifunc>::const_iterator p =
1465*a9fa9459Szrj 	 this->global_ifuncs_.begin();
1466*a9fa9459Szrj        p != this->global_ifuncs_.end();
1467*a9fa9459Szrj        ++p)
1468*a9fa9459Szrj     {
1469*a9fa9459Szrj       const Sized_symbol<32>* ssym =
1470*a9fa9459Szrj 	static_cast<const Sized_symbol<32>*>(p->sym);
1471*a9fa9459Szrj       elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1472*a9fa9459Szrj 					ssym->value());
1473*a9fa9459Szrj     }
1474*a9fa9459Szrj 
1475*a9fa9459Szrj   for (std::vector<Local_ifunc>::const_iterator p =
1476*a9fa9459Szrj 	 this->local_ifuncs_.begin();
1477*a9fa9459Szrj        p != this->local_ifuncs_.end();
1478*a9fa9459Szrj        ++p)
1479*a9fa9459Szrj     {
1480*a9fa9459Szrj       const Symbol_value<32>* psymval =
1481*a9fa9459Szrj 	p->object->local_symbol(p->local_sym_index);
1482*a9fa9459Szrj       elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1483*a9fa9459Szrj 					psymval->value(p->object, 0));
1484*a9fa9459Szrj     }
1485*a9fa9459Szrj 
1486*a9fa9459Szrj   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1487*a9fa9459Szrj   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1488*a9fa9459Szrj 
1489*a9fa9459Szrj   of->write_output_view(offset, oview_size, oview);
1490*a9fa9459Szrj   of->write_output_view(got_file_offset, got_size, got_view);
1491*a9fa9459Szrj }
1492*a9fa9459Szrj 
1493*a9fa9459Szrj // Create the PLT section.
1494*a9fa9459Szrj 
1495*a9fa9459Szrj void
make_plt_section(Symbol_table * symtab,Layout * layout)1496*a9fa9459Szrj Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1497*a9fa9459Szrj {
1498*a9fa9459Szrj   if (this->plt_ == NULL)
1499*a9fa9459Szrj     {
1500*a9fa9459Szrj       // Create the GOT sections first.
1501*a9fa9459Szrj       this->got_section(symtab, layout);
1502*a9fa9459Szrj 
1503*a9fa9459Szrj       const bool dyn = parameters->options().output_is_position_independent();
1504*a9fa9459Szrj       this->plt_ = this->make_data_plt(layout,
1505*a9fa9459Szrj 				       this->got_plt_,
1506*a9fa9459Szrj 				       this->got_irelative_,
1507*a9fa9459Szrj 				       dyn);
1508*a9fa9459Szrj 
1509*a9fa9459Szrj       // Add unwind information if requested.
1510*a9fa9459Szrj       if (parameters->options().ld_generated_unwind_info())
1511*a9fa9459Szrj 	this->plt_->add_eh_frame(layout);
1512*a9fa9459Szrj 
1513*a9fa9459Szrj       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1514*a9fa9459Szrj 				      (elfcpp::SHF_ALLOC
1515*a9fa9459Szrj 				       | elfcpp::SHF_EXECINSTR),
1516*a9fa9459Szrj 				      this->plt_, ORDER_PLT, false);
1517*a9fa9459Szrj 
1518*a9fa9459Szrj       // Make the sh_info field of .rel.plt point to .plt.
1519*a9fa9459Szrj       Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1520*a9fa9459Szrj       rel_plt_os->set_info_section(this->plt_->output_section());
1521*a9fa9459Szrj     }
1522*a9fa9459Szrj }
1523*a9fa9459Szrj 
1524*a9fa9459Szrj // Create a PLT entry for a global symbol.
1525*a9fa9459Szrj 
1526*a9fa9459Szrj void
make_plt_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)1527*a9fa9459Szrj Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1528*a9fa9459Szrj {
1529*a9fa9459Szrj   if (gsym->has_plt_offset())
1530*a9fa9459Szrj     return;
1531*a9fa9459Szrj   if (this->plt_ == NULL)
1532*a9fa9459Szrj     this->make_plt_section(symtab, layout);
1533*a9fa9459Szrj   this->plt_->add_entry(symtab, layout, gsym);
1534*a9fa9459Szrj }
1535*a9fa9459Szrj 
1536*a9fa9459Szrj // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1537*a9fa9459Szrj 
1538*a9fa9459Szrj void
make_local_ifunc_plt_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * relobj,unsigned int local_sym_index)1539*a9fa9459Szrj Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1540*a9fa9459Szrj 					Sized_relobj_file<32, false>* relobj,
1541*a9fa9459Szrj 					unsigned int local_sym_index)
1542*a9fa9459Szrj {
1543*a9fa9459Szrj   if (relobj->local_has_plt_offset(local_sym_index))
1544*a9fa9459Szrj     return;
1545*a9fa9459Szrj   if (this->plt_ == NULL)
1546*a9fa9459Szrj     this->make_plt_section(symtab, layout);
1547*a9fa9459Szrj   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1548*a9fa9459Szrj 							      relobj,
1549*a9fa9459Szrj 							      local_sym_index);
1550*a9fa9459Szrj   relobj->set_local_plt_offset(local_sym_index, plt_offset);
1551*a9fa9459Szrj }
1552*a9fa9459Szrj 
1553*a9fa9459Szrj // Return the number of entries in the PLT.
1554*a9fa9459Szrj 
1555*a9fa9459Szrj unsigned int
plt_entry_count() const1556*a9fa9459Szrj Target_i386::plt_entry_count() const
1557*a9fa9459Szrj {
1558*a9fa9459Szrj   if (this->plt_ == NULL)
1559*a9fa9459Szrj     return 0;
1560*a9fa9459Szrj   return this->plt_->entry_count();
1561*a9fa9459Szrj }
1562*a9fa9459Szrj 
1563*a9fa9459Szrj // Return the offset of the first non-reserved PLT entry.
1564*a9fa9459Szrj 
1565*a9fa9459Szrj unsigned int
first_plt_entry_offset() const1566*a9fa9459Szrj Target_i386::first_plt_entry_offset() const
1567*a9fa9459Szrj {
1568*a9fa9459Szrj   if (this->plt_ == NULL)
1569*a9fa9459Szrj     return 0;
1570*a9fa9459Szrj   return this->plt_->first_plt_entry_offset();
1571*a9fa9459Szrj }
1572*a9fa9459Szrj 
1573*a9fa9459Szrj // Return the size of each PLT entry.
1574*a9fa9459Szrj 
1575*a9fa9459Szrj unsigned int
plt_entry_size() const1576*a9fa9459Szrj Target_i386::plt_entry_size() const
1577*a9fa9459Szrj {
1578*a9fa9459Szrj   if (this->plt_ == NULL)
1579*a9fa9459Szrj     return 0;
1580*a9fa9459Szrj   return this->plt_->get_plt_entry_size();
1581*a9fa9459Szrj }
1582*a9fa9459Szrj 
1583*a9fa9459Szrj // Get the section to use for TLS_DESC relocations.
1584*a9fa9459Szrj 
1585*a9fa9459Szrj Target_i386::Reloc_section*
rel_tls_desc_section(Layout * layout) const1586*a9fa9459Szrj Target_i386::rel_tls_desc_section(Layout* layout) const
1587*a9fa9459Szrj {
1588*a9fa9459Szrj   return this->plt_section()->rel_tls_desc(layout);
1589*a9fa9459Szrj }
1590*a9fa9459Szrj 
1591*a9fa9459Szrj // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1592*a9fa9459Szrj 
1593*a9fa9459Szrj void
define_tls_base_symbol(Symbol_table * symtab,Layout * layout)1594*a9fa9459Szrj Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1595*a9fa9459Szrj {
1596*a9fa9459Szrj   if (this->tls_base_symbol_defined_)
1597*a9fa9459Szrj     return;
1598*a9fa9459Szrj 
1599*a9fa9459Szrj   Output_segment* tls_segment = layout->tls_segment();
1600*a9fa9459Szrj   if (tls_segment != NULL)
1601*a9fa9459Szrj     {
1602*a9fa9459Szrj       bool is_exec = parameters->options().output_is_executable();
1603*a9fa9459Szrj       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1604*a9fa9459Szrj 				       Symbol_table::PREDEFINED,
1605*a9fa9459Szrj 				       tls_segment, 0, 0,
1606*a9fa9459Szrj 				       elfcpp::STT_TLS,
1607*a9fa9459Szrj 				       elfcpp::STB_LOCAL,
1608*a9fa9459Szrj 				       elfcpp::STV_HIDDEN, 0,
1609*a9fa9459Szrj 				       (is_exec
1610*a9fa9459Szrj 					? Symbol::SEGMENT_END
1611*a9fa9459Szrj 					: Symbol::SEGMENT_START),
1612*a9fa9459Szrj 				       true);
1613*a9fa9459Szrj     }
1614*a9fa9459Szrj   this->tls_base_symbol_defined_ = true;
1615*a9fa9459Szrj }
1616*a9fa9459Szrj 
1617*a9fa9459Szrj // Create a GOT entry for the TLS module index.
1618*a9fa9459Szrj 
1619*a9fa9459Szrj unsigned int
got_mod_index_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * object)1620*a9fa9459Szrj Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1621*a9fa9459Szrj 				 Sized_relobj_file<32, false>* object)
1622*a9fa9459Szrj {
1623*a9fa9459Szrj   if (this->got_mod_index_offset_ == -1U)
1624*a9fa9459Szrj     {
1625*a9fa9459Szrj       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1626*a9fa9459Szrj       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1627*a9fa9459Szrj       Output_data_got<32, false>* got = this->got_section(symtab, layout);
1628*a9fa9459Szrj       unsigned int got_offset = got->add_constant(0);
1629*a9fa9459Szrj       rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1630*a9fa9459Szrj 			 got_offset);
1631*a9fa9459Szrj       got->add_constant(0);
1632*a9fa9459Szrj       this->got_mod_index_offset_ = got_offset;
1633*a9fa9459Szrj     }
1634*a9fa9459Szrj   return this->got_mod_index_offset_;
1635*a9fa9459Szrj }
1636*a9fa9459Szrj 
1637*a9fa9459Szrj // Optimize the TLS relocation type based on what we know about the
1638*a9fa9459Szrj // symbol.  IS_FINAL is true if the final address of this symbol is
1639*a9fa9459Szrj // known at link time.
1640*a9fa9459Szrj 
1641*a9fa9459Szrj tls::Tls_optimization
optimize_tls_reloc(bool is_final,int r_type)1642*a9fa9459Szrj Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1643*a9fa9459Szrj {
1644*a9fa9459Szrj   // If we are generating a shared library, then we can't do anything
1645*a9fa9459Szrj   // in the linker.
1646*a9fa9459Szrj   if (parameters->options().shared())
1647*a9fa9459Szrj     return tls::TLSOPT_NONE;
1648*a9fa9459Szrj 
1649*a9fa9459Szrj   switch (r_type)
1650*a9fa9459Szrj     {
1651*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:
1652*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:
1653*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
1654*a9fa9459Szrj       // These are General-Dynamic which permits fully general TLS
1655*a9fa9459Szrj       // access.  Since we know that we are generating an executable,
1656*a9fa9459Szrj       // we can convert this to Initial-Exec.  If we also know that
1657*a9fa9459Szrj       // this is a local symbol, we can further switch to Local-Exec.
1658*a9fa9459Szrj       if (is_final)
1659*a9fa9459Szrj 	return tls::TLSOPT_TO_LE;
1660*a9fa9459Szrj       return tls::TLSOPT_TO_IE;
1661*a9fa9459Szrj 
1662*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:
1663*a9fa9459Szrj       // This is Local-Dynamic, which refers to a local symbol in the
1664*a9fa9459Szrj       // dynamic TLS block.  Since we know that we generating an
1665*a9fa9459Szrj       // executable, we can switch to Local-Exec.
1666*a9fa9459Szrj       return tls::TLSOPT_TO_LE;
1667*a9fa9459Szrj 
1668*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:
1669*a9fa9459Szrj       // Another type of Local-Dynamic relocation.
1670*a9fa9459Szrj       return tls::TLSOPT_TO_LE;
1671*a9fa9459Szrj 
1672*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:
1673*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
1674*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
1675*a9fa9459Szrj       // These are Initial-Exec relocs which get the thread offset
1676*a9fa9459Szrj       // from the GOT.  If we know that we are linking against the
1677*a9fa9459Szrj       // local symbol, we can switch to Local-Exec, which links the
1678*a9fa9459Szrj       // thread offset into the instruction.
1679*a9fa9459Szrj       if (is_final)
1680*a9fa9459Szrj 	return tls::TLSOPT_TO_LE;
1681*a9fa9459Szrj       return tls::TLSOPT_NONE;
1682*a9fa9459Szrj 
1683*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:
1684*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
1685*a9fa9459Szrj       // When we already have Local-Exec, there is nothing further we
1686*a9fa9459Szrj       // can do.
1687*a9fa9459Szrj       return tls::TLSOPT_NONE;
1688*a9fa9459Szrj 
1689*a9fa9459Szrj     default:
1690*a9fa9459Szrj       gold_unreachable();
1691*a9fa9459Szrj     }
1692*a9fa9459Szrj }
1693*a9fa9459Szrj 
1694*a9fa9459Szrj // Get the Reference_flags for a particular relocation.
1695*a9fa9459Szrj 
1696*a9fa9459Szrj int
get_reference_flags(unsigned int r_type)1697*a9fa9459Szrj Target_i386::Scan::get_reference_flags(unsigned int r_type)
1698*a9fa9459Szrj {
1699*a9fa9459Szrj   switch (r_type)
1700*a9fa9459Szrj     {
1701*a9fa9459Szrj     case elfcpp::R_386_NONE:
1702*a9fa9459Szrj     case elfcpp::R_386_GNU_VTINHERIT:
1703*a9fa9459Szrj     case elfcpp::R_386_GNU_VTENTRY:
1704*a9fa9459Szrj     case elfcpp::R_386_GOTPC:
1705*a9fa9459Szrj       // No symbol reference.
1706*a9fa9459Szrj       return 0;
1707*a9fa9459Szrj 
1708*a9fa9459Szrj     case elfcpp::R_386_32:
1709*a9fa9459Szrj     case elfcpp::R_386_16:
1710*a9fa9459Szrj     case elfcpp::R_386_8:
1711*a9fa9459Szrj       return Symbol::ABSOLUTE_REF;
1712*a9fa9459Szrj 
1713*a9fa9459Szrj     case elfcpp::R_386_PC32:
1714*a9fa9459Szrj     case elfcpp::R_386_PC16:
1715*a9fa9459Szrj     case elfcpp::R_386_PC8:
1716*a9fa9459Szrj     case elfcpp::R_386_GOTOFF:
1717*a9fa9459Szrj       return Symbol::RELATIVE_REF;
1718*a9fa9459Szrj 
1719*a9fa9459Szrj     case elfcpp::R_386_PLT32:
1720*a9fa9459Szrj       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1721*a9fa9459Szrj 
1722*a9fa9459Szrj     case elfcpp::R_386_GOT32:
1723*a9fa9459Szrj     case elfcpp::R_386_GOT32X:
1724*a9fa9459Szrj       // Absolute in GOT.
1725*a9fa9459Szrj       return Symbol::ABSOLUTE_REF;
1726*a9fa9459Szrj 
1727*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:            // Global-dynamic
1728*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
1729*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
1730*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
1731*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
1732*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:            // Initial-exec
1733*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
1734*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
1735*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:            // Local-exec
1736*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
1737*a9fa9459Szrj       return Symbol::TLS_REF;
1738*a9fa9459Szrj 
1739*a9fa9459Szrj     case elfcpp::R_386_COPY:
1740*a9fa9459Szrj     case elfcpp::R_386_GLOB_DAT:
1741*a9fa9459Szrj     case elfcpp::R_386_JUMP_SLOT:
1742*a9fa9459Szrj     case elfcpp::R_386_RELATIVE:
1743*a9fa9459Szrj     case elfcpp::R_386_IRELATIVE:
1744*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF:
1745*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPMOD32:
1746*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPOFF32:
1747*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF32:
1748*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC:
1749*a9fa9459Szrj     case elfcpp::R_386_32PLT:
1750*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_32:
1751*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_PUSH:
1752*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_CALL:
1753*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_POP:
1754*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_32:
1755*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_PUSH:
1756*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_CALL:
1757*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_POP:
1758*a9fa9459Szrj     case elfcpp::R_386_USED_BY_INTEL_200:
1759*a9fa9459Szrj     default:
1760*a9fa9459Szrj       // Not expected.  We will give an error later.
1761*a9fa9459Szrj       return 0;
1762*a9fa9459Szrj     }
1763*a9fa9459Szrj }
1764*a9fa9459Szrj 
1765*a9fa9459Szrj // Report an unsupported relocation against a local symbol.
1766*a9fa9459Szrj 
1767*a9fa9459Szrj void
unsupported_reloc_local(Sized_relobj_file<32,false> * object,unsigned int r_type)1768*a9fa9459Szrj Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1769*a9fa9459Szrj 					   unsigned int r_type)
1770*a9fa9459Szrj {
1771*a9fa9459Szrj   gold_error(_("%s: unsupported reloc %u against local symbol"),
1772*a9fa9459Szrj 	     object->name().c_str(), r_type);
1773*a9fa9459Szrj }
1774*a9fa9459Szrj 
1775*a9fa9459Szrj // Return whether we need to make a PLT entry for a relocation of a
1776*a9fa9459Szrj // given type against a STT_GNU_IFUNC symbol.
1777*a9fa9459Szrj 
1778*a9fa9459Szrj bool
reloc_needs_plt_for_ifunc(Sized_relobj_file<32,false> * object,unsigned int r_type)1779*a9fa9459Szrj Target_i386::Scan::reloc_needs_plt_for_ifunc(
1780*a9fa9459Szrj     Sized_relobj_file<32, false>* object,
1781*a9fa9459Szrj     unsigned int r_type)
1782*a9fa9459Szrj {
1783*a9fa9459Szrj   int flags = Scan::get_reference_flags(r_type);
1784*a9fa9459Szrj   if (flags & Symbol::TLS_REF)
1785*a9fa9459Szrj     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1786*a9fa9459Szrj 	       object->name().c_str(), r_type);
1787*a9fa9459Szrj   return flags != 0;
1788*a9fa9459Szrj }
1789*a9fa9459Szrj 
1790*a9fa9459Szrj // Scan a relocation for a local symbol.
1791*a9fa9459Szrj 
1792*a9fa9459Szrj inline void
local(Symbol_table * symtab,Layout * layout,Target_i386 * target,Sized_relobj_file<32,false> * object,unsigned int data_shndx,Output_section * output_section,const elfcpp::Rel<32,false> & reloc,unsigned int r_type,const elfcpp::Sym<32,false> & lsym,bool is_discarded)1793*a9fa9459Szrj Target_i386::Scan::local(Symbol_table* symtab,
1794*a9fa9459Szrj 			 Layout* layout,
1795*a9fa9459Szrj 			 Target_i386* target,
1796*a9fa9459Szrj 			 Sized_relobj_file<32, false>* object,
1797*a9fa9459Szrj 			 unsigned int data_shndx,
1798*a9fa9459Szrj 			 Output_section* output_section,
1799*a9fa9459Szrj 			 const elfcpp::Rel<32, false>& reloc,
1800*a9fa9459Szrj 			 unsigned int r_type,
1801*a9fa9459Szrj 			 const elfcpp::Sym<32, false>& lsym,
1802*a9fa9459Szrj 			 bool is_discarded)
1803*a9fa9459Szrj {
1804*a9fa9459Szrj   if (is_discarded)
1805*a9fa9459Szrj     return;
1806*a9fa9459Szrj 
1807*a9fa9459Szrj   // A local STT_GNU_IFUNC symbol may require a PLT entry.
1808*a9fa9459Szrj   if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1809*a9fa9459Szrj       && this->reloc_needs_plt_for_ifunc(object, r_type))
1810*a9fa9459Szrj     {
1811*a9fa9459Szrj       unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1812*a9fa9459Szrj       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1813*a9fa9459Szrj     }
1814*a9fa9459Szrj 
1815*a9fa9459Szrj   switch (r_type)
1816*a9fa9459Szrj     {
1817*a9fa9459Szrj     case elfcpp::R_386_NONE:
1818*a9fa9459Szrj     case elfcpp::R_386_GNU_VTINHERIT:
1819*a9fa9459Szrj     case elfcpp::R_386_GNU_VTENTRY:
1820*a9fa9459Szrj       break;
1821*a9fa9459Szrj 
1822*a9fa9459Szrj     case elfcpp::R_386_32:
1823*a9fa9459Szrj       // If building a shared library (or a position-independent
1824*a9fa9459Szrj       // executable), we need to create a dynamic relocation for
1825*a9fa9459Szrj       // this location. The relocation applied at link time will
1826*a9fa9459Szrj       // apply the link-time value, so we flag the location with
1827*a9fa9459Szrj       // an R_386_RELATIVE relocation so the dynamic loader can
1828*a9fa9459Szrj       // relocate it easily.
1829*a9fa9459Szrj       if (parameters->options().output_is_position_independent())
1830*a9fa9459Szrj 	{
1831*a9fa9459Szrj 	  Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1832*a9fa9459Szrj 	  unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1833*a9fa9459Szrj 	  rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1834*a9fa9459Szrj 				      output_section, data_shndx,
1835*a9fa9459Szrj 				      reloc.get_r_offset());
1836*a9fa9459Szrj 	}
1837*a9fa9459Szrj       break;
1838*a9fa9459Szrj 
1839*a9fa9459Szrj     case elfcpp::R_386_16:
1840*a9fa9459Szrj     case elfcpp::R_386_8:
1841*a9fa9459Szrj       // If building a shared library (or a position-independent
1842*a9fa9459Szrj       // executable), we need to create a dynamic relocation for
1843*a9fa9459Szrj       // this location. Because the addend needs to remain in the
1844*a9fa9459Szrj       // data section, we need to be careful not to apply this
1845*a9fa9459Szrj       // relocation statically.
1846*a9fa9459Szrj       if (parameters->options().output_is_position_independent())
1847*a9fa9459Szrj 	{
1848*a9fa9459Szrj 	  Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1849*a9fa9459Szrj 	  unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1850*a9fa9459Szrj 	  if (lsym.get_st_type() != elfcpp::STT_SECTION)
1851*a9fa9459Szrj 	    rel_dyn->add_local(object, r_sym, r_type, output_section,
1852*a9fa9459Szrj 			       data_shndx, reloc.get_r_offset());
1853*a9fa9459Szrj 	  else
1854*a9fa9459Szrj 	    {
1855*a9fa9459Szrj 	      gold_assert(lsym.get_st_value() == 0);
1856*a9fa9459Szrj 	      unsigned int shndx = lsym.get_st_shndx();
1857*a9fa9459Szrj 	      bool is_ordinary;
1858*a9fa9459Szrj 	      shndx = object->adjust_sym_shndx(r_sym, shndx,
1859*a9fa9459Szrj 					       &is_ordinary);
1860*a9fa9459Szrj 	      if (!is_ordinary)
1861*a9fa9459Szrj 		object->error(_("section symbol %u has bad shndx %u"),
1862*a9fa9459Szrj 			      r_sym, shndx);
1863*a9fa9459Szrj 	      else
1864*a9fa9459Szrj 		rel_dyn->add_local_section(object, shndx,
1865*a9fa9459Szrj 					   r_type, output_section,
1866*a9fa9459Szrj 					   data_shndx, reloc.get_r_offset());
1867*a9fa9459Szrj 	    }
1868*a9fa9459Szrj 	}
1869*a9fa9459Szrj       break;
1870*a9fa9459Szrj 
1871*a9fa9459Szrj     case elfcpp::R_386_PC32:
1872*a9fa9459Szrj     case elfcpp::R_386_PC16:
1873*a9fa9459Szrj     case elfcpp::R_386_PC8:
1874*a9fa9459Szrj       break;
1875*a9fa9459Szrj 
1876*a9fa9459Szrj     case elfcpp::R_386_PLT32:
1877*a9fa9459Szrj       // Since we know this is a local symbol, we can handle this as a
1878*a9fa9459Szrj       // PC32 reloc.
1879*a9fa9459Szrj       break;
1880*a9fa9459Szrj 
1881*a9fa9459Szrj     case elfcpp::R_386_GOTOFF:
1882*a9fa9459Szrj     case elfcpp::R_386_GOTPC:
1883*a9fa9459Szrj       // We need a GOT section.
1884*a9fa9459Szrj       target->got_section(symtab, layout);
1885*a9fa9459Szrj       break;
1886*a9fa9459Szrj 
1887*a9fa9459Szrj     case elfcpp::R_386_GOT32:
1888*a9fa9459Szrj     case elfcpp::R_386_GOT32X:
1889*a9fa9459Szrj       {
1890*a9fa9459Szrj 	// We need GOT section.
1891*a9fa9459Szrj 	Output_data_got<32, false>* got = target->got_section(symtab, layout);
1892*a9fa9459Szrj 
1893*a9fa9459Szrj 	// If the relocation symbol isn't IFUNC,
1894*a9fa9459Szrj 	// and is local, then we will convert
1895*a9fa9459Szrj 	// mov foo@GOT(%reg), %reg
1896*a9fa9459Szrj 	// to
1897*a9fa9459Szrj 	// lea foo@GOTOFF(%reg), %reg
1898*a9fa9459Szrj 	// in Relocate::relocate.
1899*a9fa9459Szrj 	if (reloc.get_r_offset() >= 2
1900*a9fa9459Szrj 	    && lsym.get_st_type() != elfcpp::STT_GNU_IFUNC)
1901*a9fa9459Szrj 	  {
1902*a9fa9459Szrj 	    section_size_type stype;
1903*a9fa9459Szrj 	    const unsigned char* view = object->section_contents(data_shndx,
1904*a9fa9459Szrj 								 &stype, true);
1905*a9fa9459Szrj 	    if (view[reloc.get_r_offset() - 2] == 0x8b)
1906*a9fa9459Szrj 	      break;
1907*a9fa9459Szrj 	  }
1908*a9fa9459Szrj 
1909*a9fa9459Szrj 	// Otherwise, the symbol requires a GOT entry.
1910*a9fa9459Szrj 	unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1911*a9fa9459Szrj 
1912*a9fa9459Szrj 	// For a STT_GNU_IFUNC symbol we want the PLT offset.  That
1913*a9fa9459Szrj 	// lets function pointers compare correctly with shared
1914*a9fa9459Szrj 	// libraries.  Otherwise we would need an IRELATIVE reloc.
1915*a9fa9459Szrj 	bool is_new;
1916*a9fa9459Szrj 	if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1917*a9fa9459Szrj 	  is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1918*a9fa9459Szrj 	else
1919*a9fa9459Szrj 	  is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1920*a9fa9459Szrj 	if (is_new)
1921*a9fa9459Szrj 	  {
1922*a9fa9459Szrj 	    // If we are generating a shared object, we need to add a
1923*a9fa9459Szrj 	    // dynamic RELATIVE relocation for this symbol's GOT entry.
1924*a9fa9459Szrj 	    if (parameters->options().output_is_position_independent())
1925*a9fa9459Szrj 	      {
1926*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1927*a9fa9459Szrj 		unsigned int got_offset =
1928*a9fa9459Szrj 		  object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1929*a9fa9459Szrj 		rel_dyn->add_local_relative(object, r_sym,
1930*a9fa9459Szrj 					    elfcpp::R_386_RELATIVE,
1931*a9fa9459Szrj 					    got, got_offset);
1932*a9fa9459Szrj 	      }
1933*a9fa9459Szrj 	  }
1934*a9fa9459Szrj       }
1935*a9fa9459Szrj       break;
1936*a9fa9459Szrj 
1937*a9fa9459Szrj       // These are relocations which should only be seen by the
1938*a9fa9459Szrj       // dynamic linker, and should never be seen here.
1939*a9fa9459Szrj     case elfcpp::R_386_COPY:
1940*a9fa9459Szrj     case elfcpp::R_386_GLOB_DAT:
1941*a9fa9459Szrj     case elfcpp::R_386_JUMP_SLOT:
1942*a9fa9459Szrj     case elfcpp::R_386_RELATIVE:
1943*a9fa9459Szrj     case elfcpp::R_386_IRELATIVE:
1944*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF:
1945*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPMOD32:
1946*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPOFF32:
1947*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF32:
1948*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC:
1949*a9fa9459Szrj       gold_error(_("%s: unexpected reloc %u in object file"),
1950*a9fa9459Szrj 		 object->name().c_str(), r_type);
1951*a9fa9459Szrj       break;
1952*a9fa9459Szrj 
1953*a9fa9459Szrj       // These are initial TLS relocs, which are expected when
1954*a9fa9459Szrj       // linking.
1955*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:            // Global-dynamic
1956*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
1957*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
1958*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
1959*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
1960*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:            // Initial-exec
1961*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
1962*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
1963*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:            // Local-exec
1964*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
1965*a9fa9459Szrj       {
1966*a9fa9459Szrj 	bool output_is_shared = parameters->options().shared();
1967*a9fa9459Szrj 	const tls::Tls_optimization optimized_type
1968*a9fa9459Szrj 	    = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1969*a9fa9459Szrj 	switch (r_type)
1970*a9fa9459Szrj 	  {
1971*a9fa9459Szrj 	  case elfcpp::R_386_TLS_GD:          // Global-dynamic
1972*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
1973*a9fa9459Szrj 	      {
1974*a9fa9459Szrj 		// Create a pair of GOT entries for the module index and
1975*a9fa9459Szrj 		// dtv-relative offset.
1976*a9fa9459Szrj 		Output_data_got<32, false>* got
1977*a9fa9459Szrj 		    = target->got_section(symtab, layout);
1978*a9fa9459Szrj 		unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1979*a9fa9459Szrj 		unsigned int shndx = lsym.get_st_shndx();
1980*a9fa9459Szrj 		bool is_ordinary;
1981*a9fa9459Szrj 		shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1982*a9fa9459Szrj 		if (!is_ordinary)
1983*a9fa9459Szrj 		  object->error(_("local symbol %u has bad shndx %u"),
1984*a9fa9459Szrj 			      r_sym, shndx);
1985*a9fa9459Szrj 		else
1986*a9fa9459Szrj 		  got->add_local_pair_with_rel(object, r_sym, shndx,
1987*a9fa9459Szrj 					       GOT_TYPE_TLS_PAIR,
1988*a9fa9459Szrj 					       target->rel_dyn_section(layout),
1989*a9fa9459Szrj 					       elfcpp::R_386_TLS_DTPMOD32);
1990*a9fa9459Szrj 	      }
1991*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
1992*a9fa9459Szrj 	      unsupported_reloc_local(object, r_type);
1993*a9fa9459Szrj 	    break;
1994*a9fa9459Szrj 
1995*a9fa9459Szrj 	  case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (from ~oliva)
1996*a9fa9459Szrj 	    target->define_tls_base_symbol(symtab, layout);
1997*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
1998*a9fa9459Szrj 	      {
1999*a9fa9459Szrj 		// Create a double GOT entry with an R_386_TLS_DESC
2000*a9fa9459Szrj 		// reloc.  The R_386_TLS_DESC reloc is resolved
2001*a9fa9459Szrj 		// lazily, so the GOT entry needs to be in an area in
2002*a9fa9459Szrj 		// .got.plt, not .got.  Call got_section to make sure
2003*a9fa9459Szrj 		// the section has been created.
2004*a9fa9459Szrj 		target->got_section(symtab, layout);
2005*a9fa9459Szrj 		Output_data_got<32, false>* got = target->got_tlsdesc_section();
2006*a9fa9459Szrj 		unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2007*a9fa9459Szrj 		if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
2008*a9fa9459Szrj 		  {
2009*a9fa9459Szrj 		    unsigned int got_offset = got->add_constant(0);
2010*a9fa9459Szrj 		    // The local symbol value is stored in the second
2011*a9fa9459Szrj 		    // GOT entry.
2012*a9fa9459Szrj 		    got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
2013*a9fa9459Szrj 		    // That set the GOT offset of the local symbol to
2014*a9fa9459Szrj 		    // point to the second entry, but we want it to
2015*a9fa9459Szrj 		    // point to the first.
2016*a9fa9459Szrj 		    object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2017*a9fa9459Szrj 						 got_offset);
2018*a9fa9459Szrj 		    Reloc_section* rt = target->rel_tls_desc_section(layout);
2019*a9fa9459Szrj 		    rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
2020*a9fa9459Szrj 		  }
2021*a9fa9459Szrj 	      }
2022*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2023*a9fa9459Szrj 	      unsupported_reloc_local(object, r_type);
2024*a9fa9459Szrj 	    break;
2025*a9fa9459Szrj 
2026*a9fa9459Szrj 	  case elfcpp::R_386_TLS_DESC_CALL:
2027*a9fa9459Szrj 	    break;
2028*a9fa9459Szrj 
2029*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LDM:         // Local-dynamic
2030*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
2031*a9fa9459Szrj 	      {
2032*a9fa9459Szrj 		// Create a GOT entry for the module index.
2033*a9fa9459Szrj 		target->got_mod_index_entry(symtab, layout, object);
2034*a9fa9459Szrj 	      }
2035*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2036*a9fa9459Szrj 	      unsupported_reloc_local(object, r_type);
2037*a9fa9459Szrj 	    break;
2038*a9fa9459Szrj 
2039*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
2040*a9fa9459Szrj 	    break;
2041*a9fa9459Szrj 
2042*a9fa9459Szrj 	  case elfcpp::R_386_TLS_IE:          // Initial-exec
2043*a9fa9459Szrj 	  case elfcpp::R_386_TLS_IE_32:
2044*a9fa9459Szrj 	  case elfcpp::R_386_TLS_GOTIE:
2045*a9fa9459Szrj 	    layout->set_has_static_tls();
2046*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
2047*a9fa9459Szrj 	      {
2048*a9fa9459Szrj 		// For the R_386_TLS_IE relocation, we need to create a
2049*a9fa9459Szrj 		// dynamic relocation when building a shared library.
2050*a9fa9459Szrj 		if (r_type == elfcpp::R_386_TLS_IE
2051*a9fa9459Szrj 		    && parameters->options().shared())
2052*a9fa9459Szrj 		  {
2053*a9fa9459Szrj 		    Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2054*a9fa9459Szrj 		    unsigned int r_sym
2055*a9fa9459Szrj 			= elfcpp::elf_r_sym<32>(reloc.get_r_info());
2056*a9fa9459Szrj 		    rel_dyn->add_local_relative(object, r_sym,
2057*a9fa9459Szrj 						elfcpp::R_386_RELATIVE,
2058*a9fa9459Szrj 						output_section, data_shndx,
2059*a9fa9459Szrj 						reloc.get_r_offset());
2060*a9fa9459Szrj 		  }
2061*a9fa9459Szrj 		// Create a GOT entry for the tp-relative offset.
2062*a9fa9459Szrj 		Output_data_got<32, false>* got
2063*a9fa9459Szrj 		    = target->got_section(symtab, layout);
2064*a9fa9459Szrj 		unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2065*a9fa9459Szrj 		unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2066*a9fa9459Szrj 					   ? elfcpp::R_386_TLS_TPOFF32
2067*a9fa9459Szrj 					   : elfcpp::R_386_TLS_TPOFF);
2068*a9fa9459Szrj 		unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2069*a9fa9459Szrj 					 ? GOT_TYPE_TLS_OFFSET
2070*a9fa9459Szrj 					 : GOT_TYPE_TLS_NOFFSET);
2071*a9fa9459Szrj 		got->add_local_with_rel(object, r_sym, got_type,
2072*a9fa9459Szrj 					target->rel_dyn_section(layout),
2073*a9fa9459Szrj 					dyn_r_type);
2074*a9fa9459Szrj 	      }
2075*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2076*a9fa9459Szrj 	      unsupported_reloc_local(object, r_type);
2077*a9fa9459Szrj 	    break;
2078*a9fa9459Szrj 
2079*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LE:          // Local-exec
2080*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LE_32:
2081*a9fa9459Szrj 	    layout->set_has_static_tls();
2082*a9fa9459Szrj 	    if (output_is_shared)
2083*a9fa9459Szrj 	      {
2084*a9fa9459Szrj 		// We need to create a dynamic relocation.
2085*a9fa9459Szrj 		gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2086*a9fa9459Szrj 		unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2087*a9fa9459Szrj 		unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2088*a9fa9459Szrj 					   ? elfcpp::R_386_TLS_TPOFF32
2089*a9fa9459Szrj 					   : elfcpp::R_386_TLS_TPOFF);
2090*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2091*a9fa9459Szrj 		rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
2092*a9fa9459Szrj 				   data_shndx, reloc.get_r_offset());
2093*a9fa9459Szrj 	      }
2094*a9fa9459Szrj 	    break;
2095*a9fa9459Szrj 
2096*a9fa9459Szrj 	  default:
2097*a9fa9459Szrj 	    gold_unreachable();
2098*a9fa9459Szrj 	  }
2099*a9fa9459Szrj       }
2100*a9fa9459Szrj       break;
2101*a9fa9459Szrj 
2102*a9fa9459Szrj     case elfcpp::R_386_32PLT:
2103*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_32:
2104*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_PUSH:
2105*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_CALL:
2106*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_POP:
2107*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_32:
2108*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_PUSH:
2109*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_CALL:
2110*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_POP:
2111*a9fa9459Szrj     case elfcpp::R_386_USED_BY_INTEL_200:
2112*a9fa9459Szrj     default:
2113*a9fa9459Szrj       unsupported_reloc_local(object, r_type);
2114*a9fa9459Szrj       break;
2115*a9fa9459Szrj     }
2116*a9fa9459Szrj }
2117*a9fa9459Szrj 
2118*a9fa9459Szrj // Report an unsupported relocation against a global symbol.
2119*a9fa9459Szrj 
2120*a9fa9459Szrj void
unsupported_reloc_global(Sized_relobj_file<32,false> * object,unsigned int r_type,Symbol * gsym)2121*a9fa9459Szrj Target_i386::Scan::unsupported_reloc_global(
2122*a9fa9459Szrj     Sized_relobj_file<32, false>* object,
2123*a9fa9459Szrj     unsigned int r_type,
2124*a9fa9459Szrj     Symbol* gsym)
2125*a9fa9459Szrj {
2126*a9fa9459Szrj   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2127*a9fa9459Szrj 	     object->name().c_str(), r_type, gsym->demangled_name().c_str());
2128*a9fa9459Szrj }
2129*a9fa9459Szrj 
2130*a9fa9459Szrj inline bool
possible_function_pointer_reloc(unsigned int r_type)2131*a9fa9459Szrj Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
2132*a9fa9459Szrj {
2133*a9fa9459Szrj   switch (r_type)
2134*a9fa9459Szrj     {
2135*a9fa9459Szrj     case elfcpp::R_386_32:
2136*a9fa9459Szrj     case elfcpp::R_386_16:
2137*a9fa9459Szrj     case elfcpp::R_386_8:
2138*a9fa9459Szrj     case elfcpp::R_386_GOTOFF:
2139*a9fa9459Szrj     case elfcpp::R_386_GOT32:
2140*a9fa9459Szrj     case elfcpp::R_386_GOT32X:
2141*a9fa9459Szrj       {
2142*a9fa9459Szrj 	return true;
2143*a9fa9459Szrj       }
2144*a9fa9459Szrj     default:
2145*a9fa9459Szrj       return false;
2146*a9fa9459Szrj     }
2147*a9fa9459Szrj   return false;
2148*a9fa9459Szrj }
2149*a9fa9459Szrj 
2150*a9fa9459Szrj inline bool
local_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_i386 *,Sized_relobj_file<32,false> *,unsigned int,Output_section *,const elfcpp::Rel<32,false> &,unsigned int r_type,const elfcpp::Sym<32,false> &)2151*a9fa9459Szrj Target_i386::Scan::local_reloc_may_be_function_pointer(
2152*a9fa9459Szrj   Symbol_table* ,
2153*a9fa9459Szrj   Layout* ,
2154*a9fa9459Szrj   Target_i386* ,
2155*a9fa9459Szrj   Sized_relobj_file<32, false>* ,
2156*a9fa9459Szrj   unsigned int ,
2157*a9fa9459Szrj   Output_section* ,
2158*a9fa9459Szrj   const elfcpp::Rel<32, false>& ,
2159*a9fa9459Szrj   unsigned int r_type,
2160*a9fa9459Szrj   const elfcpp::Sym<32, false>&)
2161*a9fa9459Szrj {
2162*a9fa9459Szrj   return possible_function_pointer_reloc(r_type);
2163*a9fa9459Szrj }
2164*a9fa9459Szrj 
2165*a9fa9459Szrj inline bool
global_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_i386 *,Sized_relobj_file<32,false> *,unsigned int,Output_section *,const elfcpp::Rel<32,false> &,unsigned int r_type,Symbol *)2166*a9fa9459Szrj Target_i386::Scan::global_reloc_may_be_function_pointer(
2167*a9fa9459Szrj   Symbol_table* ,
2168*a9fa9459Szrj   Layout* ,
2169*a9fa9459Szrj   Target_i386* ,
2170*a9fa9459Szrj   Sized_relobj_file<32, false>* ,
2171*a9fa9459Szrj   unsigned int ,
2172*a9fa9459Szrj   Output_section* ,
2173*a9fa9459Szrj   const elfcpp::Rel<32, false>& ,
2174*a9fa9459Szrj   unsigned int r_type,
2175*a9fa9459Szrj   Symbol*)
2176*a9fa9459Szrj {
2177*a9fa9459Szrj   return possible_function_pointer_reloc(r_type);
2178*a9fa9459Szrj }
2179*a9fa9459Szrj 
2180*a9fa9459Szrj // Scan a relocation for a global symbol.
2181*a9fa9459Szrj 
2182*a9fa9459Szrj inline void
global(Symbol_table * symtab,Layout * layout,Target_i386 * target,Sized_relobj_file<32,false> * object,unsigned int data_shndx,Output_section * output_section,const elfcpp::Rel<32,false> & reloc,unsigned int r_type,Symbol * gsym)2183*a9fa9459Szrj Target_i386::Scan::global(Symbol_table* symtab,
2184*a9fa9459Szrj 				 Layout* layout,
2185*a9fa9459Szrj 				 Target_i386* target,
2186*a9fa9459Szrj 				 Sized_relobj_file<32, false>* object,
2187*a9fa9459Szrj 				 unsigned int data_shndx,
2188*a9fa9459Szrj 				 Output_section* output_section,
2189*a9fa9459Szrj 				 const elfcpp::Rel<32, false>& reloc,
2190*a9fa9459Szrj 				 unsigned int r_type,
2191*a9fa9459Szrj 				 Symbol* gsym)
2192*a9fa9459Szrj {
2193*a9fa9459Szrj   // A STT_GNU_IFUNC symbol may require a PLT entry.
2194*a9fa9459Szrj   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2195*a9fa9459Szrj       && this->reloc_needs_plt_for_ifunc(object, r_type))
2196*a9fa9459Szrj     target->make_plt_entry(symtab, layout, gsym);
2197*a9fa9459Szrj 
2198*a9fa9459Szrj   switch (r_type)
2199*a9fa9459Szrj     {
2200*a9fa9459Szrj     case elfcpp::R_386_NONE:
2201*a9fa9459Szrj     case elfcpp::R_386_GNU_VTINHERIT:
2202*a9fa9459Szrj     case elfcpp::R_386_GNU_VTENTRY:
2203*a9fa9459Szrj       break;
2204*a9fa9459Szrj 
2205*a9fa9459Szrj     case elfcpp::R_386_32:
2206*a9fa9459Szrj     case elfcpp::R_386_16:
2207*a9fa9459Szrj     case elfcpp::R_386_8:
2208*a9fa9459Szrj       {
2209*a9fa9459Szrj 	// Make a PLT entry if necessary.
2210*a9fa9459Szrj 	if (gsym->needs_plt_entry())
2211*a9fa9459Szrj 	  {
2212*a9fa9459Szrj 	    target->make_plt_entry(symtab, layout, gsym);
2213*a9fa9459Szrj 	    // Since this is not a PC-relative relocation, we may be
2214*a9fa9459Szrj 	    // taking the address of a function. In that case we need to
2215*a9fa9459Szrj 	    // set the entry in the dynamic symbol table to the address of
2216*a9fa9459Szrj 	    // the PLT entry.
2217*a9fa9459Szrj 	    if (gsym->is_from_dynobj() && !parameters->options().shared())
2218*a9fa9459Szrj 	      gsym->set_needs_dynsym_value();
2219*a9fa9459Szrj 	  }
2220*a9fa9459Szrj 	// Make a dynamic relocation if necessary.
2221*a9fa9459Szrj 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2222*a9fa9459Szrj 	  {
2223*a9fa9459Szrj 	    if (!parameters->options().output_is_position_independent()
2224*a9fa9459Szrj 		&& gsym->may_need_copy_reloc())
2225*a9fa9459Szrj 	      {
2226*a9fa9459Szrj 		target->copy_reloc(symtab, layout, object,
2227*a9fa9459Szrj 				   data_shndx, output_section, gsym, reloc);
2228*a9fa9459Szrj 	      }
2229*a9fa9459Szrj 	    else if (r_type == elfcpp::R_386_32
2230*a9fa9459Szrj 		     && gsym->type() == elfcpp::STT_GNU_IFUNC
2231*a9fa9459Szrj 		     && gsym->can_use_relative_reloc(false)
2232*a9fa9459Szrj 		     && !gsym->is_from_dynobj()
2233*a9fa9459Szrj 		     && !gsym->is_undefined()
2234*a9fa9459Szrj 		     && !gsym->is_preemptible())
2235*a9fa9459Szrj 	      {
2236*a9fa9459Szrj 		// Use an IRELATIVE reloc for a locally defined
2237*a9fa9459Szrj 		// STT_GNU_IFUNC symbol.  This makes a function
2238*a9fa9459Szrj 		// address in a PIE executable match the address in a
2239*a9fa9459Szrj 		// shared library that it links against.
2240*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_irelative_section(layout);
2241*a9fa9459Szrj 		rel_dyn->add_symbolless_global_addend(gsym,
2242*a9fa9459Szrj 						      elfcpp::R_386_IRELATIVE,
2243*a9fa9459Szrj 						      output_section,
2244*a9fa9459Szrj 						      object, data_shndx,
2245*a9fa9459Szrj 						      reloc.get_r_offset());
2246*a9fa9459Szrj 	      }
2247*a9fa9459Szrj 	    else if (r_type == elfcpp::R_386_32
2248*a9fa9459Szrj 		     && gsym->can_use_relative_reloc(false))
2249*a9fa9459Szrj 	      {
2250*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2251*a9fa9459Szrj 		rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2252*a9fa9459Szrj 					     output_section, object,
2253*a9fa9459Szrj 					     data_shndx, reloc.get_r_offset());
2254*a9fa9459Szrj 	      }
2255*a9fa9459Szrj 	    else
2256*a9fa9459Szrj 	      {
2257*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2258*a9fa9459Szrj 		rel_dyn->add_global(gsym, r_type, output_section, object,
2259*a9fa9459Szrj 				    data_shndx, reloc.get_r_offset());
2260*a9fa9459Szrj 	      }
2261*a9fa9459Szrj 	  }
2262*a9fa9459Szrj       }
2263*a9fa9459Szrj       break;
2264*a9fa9459Szrj 
2265*a9fa9459Szrj     case elfcpp::R_386_PC32:
2266*a9fa9459Szrj     case elfcpp::R_386_PC16:
2267*a9fa9459Szrj     case elfcpp::R_386_PC8:
2268*a9fa9459Szrj       {
2269*a9fa9459Szrj 	// Make a PLT entry if necessary.
2270*a9fa9459Szrj 	if (gsym->needs_plt_entry())
2271*a9fa9459Szrj 	  {
2272*a9fa9459Szrj 	    // These relocations are used for function calls only in
2273*a9fa9459Szrj 	    // non-PIC code.  For a 32-bit relocation in a shared library,
2274*a9fa9459Szrj 	    // we'll need a text relocation anyway, so we can skip the
2275*a9fa9459Szrj 	    // PLT entry and let the dynamic linker bind the call directly
2276*a9fa9459Szrj 	    // to the target.  For smaller relocations, we should use a
2277*a9fa9459Szrj 	    // PLT entry to ensure that the call can reach.
2278*a9fa9459Szrj 	    if (!parameters->options().shared()
2279*a9fa9459Szrj 		|| r_type != elfcpp::R_386_PC32)
2280*a9fa9459Szrj 	      target->make_plt_entry(symtab, layout, gsym);
2281*a9fa9459Szrj 	  }
2282*a9fa9459Szrj 	// Make a dynamic relocation if necessary.
2283*a9fa9459Szrj 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2284*a9fa9459Szrj 	  {
2285*a9fa9459Szrj 	    if (parameters->options().output_is_executable()
2286*a9fa9459Szrj 		&& gsym->may_need_copy_reloc())
2287*a9fa9459Szrj 	      {
2288*a9fa9459Szrj 		target->copy_reloc(symtab, layout, object,
2289*a9fa9459Szrj 				   data_shndx, output_section, gsym, reloc);
2290*a9fa9459Szrj 	      }
2291*a9fa9459Szrj 	    else
2292*a9fa9459Szrj 	      {
2293*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2294*a9fa9459Szrj 		rel_dyn->add_global(gsym, r_type, output_section, object,
2295*a9fa9459Szrj 				    data_shndx, reloc.get_r_offset());
2296*a9fa9459Szrj 	      }
2297*a9fa9459Szrj 	  }
2298*a9fa9459Szrj       }
2299*a9fa9459Szrj       break;
2300*a9fa9459Szrj 
2301*a9fa9459Szrj     case elfcpp::R_386_GOT32:
2302*a9fa9459Szrj     case elfcpp::R_386_GOT32X:
2303*a9fa9459Szrj       {
2304*a9fa9459Szrj 	// The symbol requires a GOT section.
2305*a9fa9459Szrj 	Output_data_got<32, false>* got = target->got_section(symtab, layout);
2306*a9fa9459Szrj 
2307*a9fa9459Szrj 	// If we convert this from
2308*a9fa9459Szrj 	// mov foo@GOT(%reg), %reg
2309*a9fa9459Szrj 	// to
2310*a9fa9459Szrj 	// lea foo@GOTOFF(%reg), %reg
2311*a9fa9459Szrj 	// in Relocate::relocate, then there is nothing to do here.
2312*a9fa9459Szrj 	if (reloc.get_r_offset() >= 2
2313*a9fa9459Szrj 	    && Target_i386::can_convert_mov_to_lea(gsym))
2314*a9fa9459Szrj 	  {
2315*a9fa9459Szrj 	    section_size_type stype;
2316*a9fa9459Szrj 	    const unsigned char* view = object->section_contents(data_shndx,
2317*a9fa9459Szrj 								 &stype, true);
2318*a9fa9459Szrj 	    if (view[reloc.get_r_offset() - 2] == 0x8b)
2319*a9fa9459Szrj 	      break;
2320*a9fa9459Szrj 	  }
2321*a9fa9459Szrj 
2322*a9fa9459Szrj 	if (gsym->final_value_is_known())
2323*a9fa9459Szrj 	  {
2324*a9fa9459Szrj 	    // For a STT_GNU_IFUNC symbol we want the PLT address.
2325*a9fa9459Szrj 	    if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2326*a9fa9459Szrj 	      got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2327*a9fa9459Szrj 	    else
2328*a9fa9459Szrj 	      got->add_global(gsym, GOT_TYPE_STANDARD);
2329*a9fa9459Szrj 	  }
2330*a9fa9459Szrj 	else
2331*a9fa9459Szrj 	  {
2332*a9fa9459Szrj 	    // If this symbol is not fully resolved, we need to add a
2333*a9fa9459Szrj 	    // GOT entry with a dynamic relocation.
2334*a9fa9459Szrj 	    Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2335*a9fa9459Szrj 
2336*a9fa9459Szrj 	    // Use a GLOB_DAT rather than a RELATIVE reloc if:
2337*a9fa9459Szrj 	    //
2338*a9fa9459Szrj 	    // 1) The symbol may be defined in some other module.
2339*a9fa9459Szrj 	    //
2340*a9fa9459Szrj 	    // 2) We are building a shared library and this is a
2341*a9fa9459Szrj 	    // protected symbol; using GLOB_DAT means that the dynamic
2342*a9fa9459Szrj 	    // linker can use the address of the PLT in the main
2343*a9fa9459Szrj 	    // executable when appropriate so that function address
2344*a9fa9459Szrj 	    // comparisons work.
2345*a9fa9459Szrj 	    //
2346*a9fa9459Szrj 	    // 3) This is a STT_GNU_IFUNC symbol in position dependent
2347*a9fa9459Szrj 	    // code, again so that function address comparisons work.
2348*a9fa9459Szrj 	    if (gsym->is_from_dynobj()
2349*a9fa9459Szrj 		|| gsym->is_undefined()
2350*a9fa9459Szrj 		|| gsym->is_preemptible()
2351*a9fa9459Szrj 		|| (gsym->visibility() == elfcpp::STV_PROTECTED
2352*a9fa9459Szrj 		    && parameters->options().shared())
2353*a9fa9459Szrj 		|| (gsym->type() == elfcpp::STT_GNU_IFUNC
2354*a9fa9459Szrj 		    && parameters->options().output_is_position_independent()))
2355*a9fa9459Szrj 	      got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2356*a9fa9459Szrj 				       rel_dyn, elfcpp::R_386_GLOB_DAT);
2357*a9fa9459Szrj 	    else
2358*a9fa9459Szrj 	      {
2359*a9fa9459Szrj 		// For a STT_GNU_IFUNC symbol we want to write the PLT
2360*a9fa9459Szrj 		// offset into the GOT, so that function pointer
2361*a9fa9459Szrj 		// comparisons work correctly.
2362*a9fa9459Szrj 		bool is_new;
2363*a9fa9459Szrj 		if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2364*a9fa9459Szrj 		  is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2365*a9fa9459Szrj 		else
2366*a9fa9459Szrj 		  {
2367*a9fa9459Szrj 		    is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2368*a9fa9459Szrj 		    // Tell the dynamic linker to use the PLT address
2369*a9fa9459Szrj 		    // when resolving relocations.
2370*a9fa9459Szrj 		    if (gsym->is_from_dynobj()
2371*a9fa9459Szrj 			&& !parameters->options().shared())
2372*a9fa9459Szrj 		      gsym->set_needs_dynsym_value();
2373*a9fa9459Szrj 		  }
2374*a9fa9459Szrj 		if (is_new)
2375*a9fa9459Szrj 		  {
2376*a9fa9459Szrj 		    unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2377*a9fa9459Szrj 		    rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2378*a9fa9459Szrj 						 got, got_off);
2379*a9fa9459Szrj 		  }
2380*a9fa9459Szrj 	      }
2381*a9fa9459Szrj 	  }
2382*a9fa9459Szrj       }
2383*a9fa9459Szrj       break;
2384*a9fa9459Szrj 
2385*a9fa9459Szrj     case elfcpp::R_386_PLT32:
2386*a9fa9459Szrj       // If the symbol is fully resolved, this is just a PC32 reloc.
2387*a9fa9459Szrj       // Otherwise we need a PLT entry.
2388*a9fa9459Szrj       if (gsym->final_value_is_known())
2389*a9fa9459Szrj 	break;
2390*a9fa9459Szrj       // If building a shared library, we can also skip the PLT entry
2391*a9fa9459Szrj       // if the symbol is defined in the output file and is protected
2392*a9fa9459Szrj       // or hidden.
2393*a9fa9459Szrj       if (gsym->is_defined()
2394*a9fa9459Szrj 	  && !gsym->is_from_dynobj()
2395*a9fa9459Szrj 	  && !gsym->is_preemptible())
2396*a9fa9459Szrj 	break;
2397*a9fa9459Szrj       target->make_plt_entry(symtab, layout, gsym);
2398*a9fa9459Szrj       break;
2399*a9fa9459Szrj 
2400*a9fa9459Szrj     case elfcpp::R_386_GOTOFF:
2401*a9fa9459Szrj       // A GOT-relative reference must resolve locally.
2402*a9fa9459Szrj       if (!gsym->is_defined())
2403*a9fa9459Szrj         gold_error(_("%s: relocation R_386_GOTOFF against undefined symbol %s"
2404*a9fa9459Szrj 		     " cannot be used when making a shared object"),
2405*a9fa9459Szrj 		   object->name().c_str(), gsym->name());
2406*a9fa9459Szrj       else if (gsym->is_from_dynobj())
2407*a9fa9459Szrj         gold_error(_("%s: relocation R_386_GOTOFF against external symbol %s"
2408*a9fa9459Szrj 		     " cannot be used when making a shared object"),
2409*a9fa9459Szrj 		   object->name().c_str(), gsym->name());
2410*a9fa9459Szrj       else if (gsym->is_preemptible())
2411*a9fa9459Szrj         gold_error(_("%s: relocation R_386_GOTOFF against preemptible symbol %s"
2412*a9fa9459Szrj 		     " cannot be used when making a shared object"),
2413*a9fa9459Szrj 		   object->name().c_str(), gsym->name());
2414*a9fa9459Szrj       // We need a GOT section.
2415*a9fa9459Szrj       target->got_section(symtab, layout);
2416*a9fa9459Szrj       break;
2417*a9fa9459Szrj 
2418*a9fa9459Szrj     case elfcpp::R_386_GOTPC:
2419*a9fa9459Szrj       // We need a GOT section.
2420*a9fa9459Szrj       target->got_section(symtab, layout);
2421*a9fa9459Szrj       break;
2422*a9fa9459Szrj 
2423*a9fa9459Szrj       // These are relocations which should only be seen by the
2424*a9fa9459Szrj       // dynamic linker, and should never be seen here.
2425*a9fa9459Szrj     case elfcpp::R_386_COPY:
2426*a9fa9459Szrj     case elfcpp::R_386_GLOB_DAT:
2427*a9fa9459Szrj     case elfcpp::R_386_JUMP_SLOT:
2428*a9fa9459Szrj     case elfcpp::R_386_RELATIVE:
2429*a9fa9459Szrj     case elfcpp::R_386_IRELATIVE:
2430*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF:
2431*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPMOD32:
2432*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPOFF32:
2433*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF32:
2434*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC:
2435*a9fa9459Szrj       gold_error(_("%s: unexpected reloc %u in object file"),
2436*a9fa9459Szrj 		 object->name().c_str(), r_type);
2437*a9fa9459Szrj       break;
2438*a9fa9459Szrj 
2439*a9fa9459Szrj       // These are initial tls relocs, which are expected when
2440*a9fa9459Szrj       // linking.
2441*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:            // Global-dynamic
2442*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
2443*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
2444*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
2445*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
2446*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:            // Initial-exec
2447*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
2448*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
2449*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:            // Local-exec
2450*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
2451*a9fa9459Szrj       {
2452*a9fa9459Szrj 	const bool is_final = gsym->final_value_is_known();
2453*a9fa9459Szrj 	const tls::Tls_optimization optimized_type
2454*a9fa9459Szrj 	    = Target_i386::optimize_tls_reloc(is_final, r_type);
2455*a9fa9459Szrj 	switch (r_type)
2456*a9fa9459Szrj 	  {
2457*a9fa9459Szrj 	  case elfcpp::R_386_TLS_GD:          // Global-dynamic
2458*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
2459*a9fa9459Szrj 	      {
2460*a9fa9459Szrj 		// Create a pair of GOT entries for the module index and
2461*a9fa9459Szrj 		// dtv-relative offset.
2462*a9fa9459Szrj 		Output_data_got<32, false>* got
2463*a9fa9459Szrj 		    = target->got_section(symtab, layout);
2464*a9fa9459Szrj 		got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2465*a9fa9459Szrj 					     target->rel_dyn_section(layout),
2466*a9fa9459Szrj 					     elfcpp::R_386_TLS_DTPMOD32,
2467*a9fa9459Szrj 					     elfcpp::R_386_TLS_DTPOFF32);
2468*a9fa9459Szrj 	      }
2469*a9fa9459Szrj 	    else if (optimized_type == tls::TLSOPT_TO_IE)
2470*a9fa9459Szrj 	      {
2471*a9fa9459Szrj 		// Create a GOT entry for the tp-relative offset.
2472*a9fa9459Szrj 		Output_data_got<32, false>* got
2473*a9fa9459Szrj 		    = target->got_section(symtab, layout);
2474*a9fa9459Szrj 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2475*a9fa9459Szrj 					 target->rel_dyn_section(layout),
2476*a9fa9459Szrj 					 elfcpp::R_386_TLS_TPOFF);
2477*a9fa9459Szrj 	      }
2478*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2479*a9fa9459Szrj 	      unsupported_reloc_global(object, r_type, gsym);
2480*a9fa9459Szrj 	    break;
2481*a9fa9459Szrj 
2482*a9fa9459Szrj 	  case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (~oliva url)
2483*a9fa9459Szrj 	    target->define_tls_base_symbol(symtab, layout);
2484*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
2485*a9fa9459Szrj 	      {
2486*a9fa9459Szrj 		// Create a double GOT entry with an R_386_TLS_DESC
2487*a9fa9459Szrj 		// reloc.  The R_386_TLS_DESC reloc is resolved
2488*a9fa9459Szrj 		// lazily, so the GOT entry needs to be in an area in
2489*a9fa9459Szrj 		// .got.plt, not .got.  Call got_section to make sure
2490*a9fa9459Szrj 		// the section has been created.
2491*a9fa9459Szrj 		target->got_section(symtab, layout);
2492*a9fa9459Szrj 		Output_data_got<32, false>* got = target->got_tlsdesc_section();
2493*a9fa9459Szrj 		Reloc_section* rt = target->rel_tls_desc_section(layout);
2494*a9fa9459Szrj 		got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2495*a9fa9459Szrj 					     elfcpp::R_386_TLS_DESC, 0);
2496*a9fa9459Szrj 	      }
2497*a9fa9459Szrj 	    else if (optimized_type == tls::TLSOPT_TO_IE)
2498*a9fa9459Szrj 	      {
2499*a9fa9459Szrj 		// Create a GOT entry for the tp-relative offset.
2500*a9fa9459Szrj 		Output_data_got<32, false>* got
2501*a9fa9459Szrj 		    = target->got_section(symtab, layout);
2502*a9fa9459Szrj 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2503*a9fa9459Szrj 					 target->rel_dyn_section(layout),
2504*a9fa9459Szrj 					 elfcpp::R_386_TLS_TPOFF);
2505*a9fa9459Szrj 	      }
2506*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2507*a9fa9459Szrj 	      unsupported_reloc_global(object, r_type, gsym);
2508*a9fa9459Szrj 	    break;
2509*a9fa9459Szrj 
2510*a9fa9459Szrj 	  case elfcpp::R_386_TLS_DESC_CALL:
2511*a9fa9459Szrj 	    break;
2512*a9fa9459Szrj 
2513*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LDM:         // Local-dynamic
2514*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
2515*a9fa9459Szrj 	      {
2516*a9fa9459Szrj 		// Create a GOT entry for the module index.
2517*a9fa9459Szrj 		target->got_mod_index_entry(symtab, layout, object);
2518*a9fa9459Szrj 	      }
2519*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2520*a9fa9459Szrj 	      unsupported_reloc_global(object, r_type, gsym);
2521*a9fa9459Szrj 	    break;
2522*a9fa9459Szrj 
2523*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
2524*a9fa9459Szrj 	    break;
2525*a9fa9459Szrj 
2526*a9fa9459Szrj 	  case elfcpp::R_386_TLS_IE:          // Initial-exec
2527*a9fa9459Szrj 	  case elfcpp::R_386_TLS_IE_32:
2528*a9fa9459Szrj 	  case elfcpp::R_386_TLS_GOTIE:
2529*a9fa9459Szrj 	    layout->set_has_static_tls();
2530*a9fa9459Szrj 	    if (optimized_type == tls::TLSOPT_NONE)
2531*a9fa9459Szrj 	      {
2532*a9fa9459Szrj 		// For the R_386_TLS_IE relocation, we need to create a
2533*a9fa9459Szrj 		// dynamic relocation when building a shared library.
2534*a9fa9459Szrj 		if (r_type == elfcpp::R_386_TLS_IE
2535*a9fa9459Szrj 		    && parameters->options().shared())
2536*a9fa9459Szrj 		  {
2537*a9fa9459Szrj 		    Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2538*a9fa9459Szrj 		    rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2539*a9fa9459Szrj 						 output_section, object,
2540*a9fa9459Szrj 						 data_shndx,
2541*a9fa9459Szrj 						 reloc.get_r_offset());
2542*a9fa9459Szrj 		  }
2543*a9fa9459Szrj 		// Create a GOT entry for the tp-relative offset.
2544*a9fa9459Szrj 		Output_data_got<32, false>* got
2545*a9fa9459Szrj 		    = target->got_section(symtab, layout);
2546*a9fa9459Szrj 		unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2547*a9fa9459Szrj 					   ? elfcpp::R_386_TLS_TPOFF32
2548*a9fa9459Szrj 					   : elfcpp::R_386_TLS_TPOFF);
2549*a9fa9459Szrj 		unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2550*a9fa9459Szrj 					 ? GOT_TYPE_TLS_OFFSET
2551*a9fa9459Szrj 					 : GOT_TYPE_TLS_NOFFSET);
2552*a9fa9459Szrj 		got->add_global_with_rel(gsym, got_type,
2553*a9fa9459Szrj 					 target->rel_dyn_section(layout),
2554*a9fa9459Szrj 					 dyn_r_type);
2555*a9fa9459Szrj 	      }
2556*a9fa9459Szrj 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2557*a9fa9459Szrj 	      unsupported_reloc_global(object, r_type, gsym);
2558*a9fa9459Szrj 	    break;
2559*a9fa9459Szrj 
2560*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LE:          // Local-exec
2561*a9fa9459Szrj 	  case elfcpp::R_386_TLS_LE_32:
2562*a9fa9459Szrj 	    layout->set_has_static_tls();
2563*a9fa9459Szrj 	    if (parameters->options().shared())
2564*a9fa9459Szrj 	      {
2565*a9fa9459Szrj 		// We need to create a dynamic relocation.
2566*a9fa9459Szrj 		unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2567*a9fa9459Szrj 					   ? elfcpp::R_386_TLS_TPOFF32
2568*a9fa9459Szrj 					   : elfcpp::R_386_TLS_TPOFF);
2569*a9fa9459Szrj 		Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2570*a9fa9459Szrj 		rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2571*a9fa9459Szrj 				    data_shndx, reloc.get_r_offset());
2572*a9fa9459Szrj 	      }
2573*a9fa9459Szrj 	    break;
2574*a9fa9459Szrj 
2575*a9fa9459Szrj 	  default:
2576*a9fa9459Szrj 	    gold_unreachable();
2577*a9fa9459Szrj 	  }
2578*a9fa9459Szrj       }
2579*a9fa9459Szrj       break;
2580*a9fa9459Szrj 
2581*a9fa9459Szrj     case elfcpp::R_386_32PLT:
2582*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_32:
2583*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_PUSH:
2584*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_CALL:
2585*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_POP:
2586*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_32:
2587*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_PUSH:
2588*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_CALL:
2589*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_POP:
2590*a9fa9459Szrj     case elfcpp::R_386_USED_BY_INTEL_200:
2591*a9fa9459Szrj     default:
2592*a9fa9459Szrj       unsupported_reloc_global(object, r_type, gsym);
2593*a9fa9459Szrj       break;
2594*a9fa9459Szrj     }
2595*a9fa9459Szrj }
2596*a9fa9459Szrj 
2597*a9fa9459Szrj // Process relocations for gc.
2598*a9fa9459Szrj 
2599*a9fa9459Szrj void
gc_process_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * object,unsigned int data_shndx,unsigned int,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)2600*a9fa9459Szrj Target_i386::gc_process_relocs(Symbol_table* symtab,
2601*a9fa9459Szrj 				      Layout* layout,
2602*a9fa9459Szrj 				      Sized_relobj_file<32, false>* object,
2603*a9fa9459Szrj 				      unsigned int data_shndx,
2604*a9fa9459Szrj 				      unsigned int,
2605*a9fa9459Szrj 				      const unsigned char* prelocs,
2606*a9fa9459Szrj 				      size_t reloc_count,
2607*a9fa9459Szrj 				      Output_section* output_section,
2608*a9fa9459Szrj 				      bool needs_special_offset_handling,
2609*a9fa9459Szrj 				      size_t local_symbol_count,
2610*a9fa9459Szrj 				      const unsigned char* plocal_symbols)
2611*a9fa9459Szrj {
2612*a9fa9459Szrj   gold::gc_process_relocs<32, false, Target_i386, Scan, Classify_reloc>(
2613*a9fa9459Szrj     symtab,
2614*a9fa9459Szrj     layout,
2615*a9fa9459Szrj     this,
2616*a9fa9459Szrj     object,
2617*a9fa9459Szrj     data_shndx,
2618*a9fa9459Szrj     prelocs,
2619*a9fa9459Szrj     reloc_count,
2620*a9fa9459Szrj     output_section,
2621*a9fa9459Szrj     needs_special_offset_handling,
2622*a9fa9459Szrj     local_symbol_count,
2623*a9fa9459Szrj     plocal_symbols);
2624*a9fa9459Szrj }
2625*a9fa9459Szrj 
2626*a9fa9459Szrj // Scan relocations for a section.
2627*a9fa9459Szrj 
2628*a9fa9459Szrj void
scan_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)2629*a9fa9459Szrj Target_i386::scan_relocs(Symbol_table* symtab,
2630*a9fa9459Szrj 				Layout* layout,
2631*a9fa9459Szrj 				Sized_relobj_file<32, false>* object,
2632*a9fa9459Szrj 				unsigned int data_shndx,
2633*a9fa9459Szrj 				unsigned int sh_type,
2634*a9fa9459Szrj 				const unsigned char* prelocs,
2635*a9fa9459Szrj 				size_t reloc_count,
2636*a9fa9459Szrj 				Output_section* output_section,
2637*a9fa9459Szrj 				bool needs_special_offset_handling,
2638*a9fa9459Szrj 				size_t local_symbol_count,
2639*a9fa9459Szrj 				const unsigned char* plocal_symbols)
2640*a9fa9459Szrj {
2641*a9fa9459Szrj   if (sh_type == elfcpp::SHT_RELA)
2642*a9fa9459Szrj     {
2643*a9fa9459Szrj       gold_error(_("%s: unsupported RELA reloc section"),
2644*a9fa9459Szrj 		 object->name().c_str());
2645*a9fa9459Szrj       return;
2646*a9fa9459Szrj     }
2647*a9fa9459Szrj 
2648*a9fa9459Szrj   gold::scan_relocs<32, false, Target_i386, Scan, Classify_reloc>(
2649*a9fa9459Szrj     symtab,
2650*a9fa9459Szrj     layout,
2651*a9fa9459Szrj     this,
2652*a9fa9459Szrj     object,
2653*a9fa9459Szrj     data_shndx,
2654*a9fa9459Szrj     prelocs,
2655*a9fa9459Szrj     reloc_count,
2656*a9fa9459Szrj     output_section,
2657*a9fa9459Szrj     needs_special_offset_handling,
2658*a9fa9459Szrj     local_symbol_count,
2659*a9fa9459Szrj     plocal_symbols);
2660*a9fa9459Szrj }
2661*a9fa9459Szrj 
2662*a9fa9459Szrj // Finalize the sections.
2663*a9fa9459Szrj 
2664*a9fa9459Szrj void
do_finalize_sections(Layout * layout,const Input_objects *,Symbol_table * symtab)2665*a9fa9459Szrj Target_i386::do_finalize_sections(
2666*a9fa9459Szrj     Layout* layout,
2667*a9fa9459Szrj     const Input_objects*,
2668*a9fa9459Szrj     Symbol_table* symtab)
2669*a9fa9459Szrj {
2670*a9fa9459Szrj   const Reloc_section* rel_plt = (this->plt_ == NULL
2671*a9fa9459Szrj 				  ? NULL
2672*a9fa9459Szrj 				  : this->plt_->rel_plt());
2673*a9fa9459Szrj   layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2674*a9fa9459Szrj 				  this->rel_dyn_, true, false);
2675*a9fa9459Szrj 
2676*a9fa9459Szrj   // Emit any relocs we saved in an attempt to avoid generating COPY
2677*a9fa9459Szrj   // relocs.
2678*a9fa9459Szrj   if (this->copy_relocs_.any_saved_relocs())
2679*a9fa9459Szrj     this->copy_relocs_.emit(this->rel_dyn_section(layout));
2680*a9fa9459Szrj 
2681*a9fa9459Szrj   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2682*a9fa9459Szrj   // the .got.plt section.
2683*a9fa9459Szrj   Symbol* sym = this->global_offset_table_;
2684*a9fa9459Szrj   if (sym != NULL)
2685*a9fa9459Szrj     {
2686*a9fa9459Szrj       uint32_t data_size = this->got_plt_->current_data_size();
2687*a9fa9459Szrj       symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2688*a9fa9459Szrj     }
2689*a9fa9459Szrj 
2690*a9fa9459Szrj   if (parameters->doing_static_link()
2691*a9fa9459Szrj       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2692*a9fa9459Szrj     {
2693*a9fa9459Szrj       // If linking statically, make sure that the __rel_iplt symbols
2694*a9fa9459Szrj       // were defined if necessary, even if we didn't create a PLT.
2695*a9fa9459Szrj       static const Define_symbol_in_segment syms[] =
2696*a9fa9459Szrj 	{
2697*a9fa9459Szrj 	  {
2698*a9fa9459Szrj 	    "__rel_iplt_start",		// name
2699*a9fa9459Szrj 	    elfcpp::PT_LOAD,		// segment_type
2700*a9fa9459Szrj 	    elfcpp::PF_W,		// segment_flags_set
2701*a9fa9459Szrj 	    elfcpp::PF(0),		// segment_flags_clear
2702*a9fa9459Szrj 	    0,				// value
2703*a9fa9459Szrj 	    0,				// size
2704*a9fa9459Szrj 	    elfcpp::STT_NOTYPE,		// type
2705*a9fa9459Szrj 	    elfcpp::STB_GLOBAL,		// binding
2706*a9fa9459Szrj 	    elfcpp::STV_HIDDEN,		// visibility
2707*a9fa9459Szrj 	    0,				// nonvis
2708*a9fa9459Szrj 	    Symbol::SEGMENT_START,	// offset_from_base
2709*a9fa9459Szrj 	    true			// only_if_ref
2710*a9fa9459Szrj 	  },
2711*a9fa9459Szrj 	  {
2712*a9fa9459Szrj 	    "__rel_iplt_end",		// name
2713*a9fa9459Szrj 	    elfcpp::PT_LOAD,		// segment_type
2714*a9fa9459Szrj 	    elfcpp::PF_W,		// segment_flags_set
2715*a9fa9459Szrj 	    elfcpp::PF(0),		// segment_flags_clear
2716*a9fa9459Szrj 	    0,				// value
2717*a9fa9459Szrj 	    0,				// size
2718*a9fa9459Szrj 	    elfcpp::STT_NOTYPE,		// type
2719*a9fa9459Szrj 	    elfcpp::STB_GLOBAL,		// binding
2720*a9fa9459Szrj 	    elfcpp::STV_HIDDEN,		// visibility
2721*a9fa9459Szrj 	    0,				// nonvis
2722*a9fa9459Szrj 	    Symbol::SEGMENT_START,	// offset_from_base
2723*a9fa9459Szrj 	    true			// only_if_ref
2724*a9fa9459Szrj 	  }
2725*a9fa9459Szrj 	};
2726*a9fa9459Szrj 
2727*a9fa9459Szrj       symtab->define_symbols(layout, 2, syms,
2728*a9fa9459Szrj 			     layout->script_options()->saw_sections_clause());
2729*a9fa9459Szrj     }
2730*a9fa9459Szrj }
2731*a9fa9459Szrj 
2732*a9fa9459Szrj // Return whether a direct absolute static relocation needs to be applied.
2733*a9fa9459Szrj // In cases where Scan::local() or Scan::global() has created
2734*a9fa9459Szrj // a dynamic relocation other than R_386_RELATIVE, the addend
2735*a9fa9459Szrj // of the relocation is carried in the data, and we must not
2736*a9fa9459Szrj // apply the static relocation.
2737*a9fa9459Szrj 
2738*a9fa9459Szrj inline bool
should_apply_static_reloc(const Sized_symbol<32> * gsym,unsigned int r_type,bool is_32bit,Output_section * output_section)2739*a9fa9459Szrj Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2740*a9fa9459Szrj 						 unsigned int r_type,
2741*a9fa9459Szrj 						 bool is_32bit,
2742*a9fa9459Szrj 						 Output_section* output_section)
2743*a9fa9459Szrj {
2744*a9fa9459Szrj   // If the output section is not allocated, then we didn't call
2745*a9fa9459Szrj   // scan_relocs, we didn't create a dynamic reloc, and we must apply
2746*a9fa9459Szrj   // the reloc here.
2747*a9fa9459Szrj   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2748*a9fa9459Szrj     return true;
2749*a9fa9459Szrj 
2750*a9fa9459Szrj   int ref_flags = Scan::get_reference_flags(r_type);
2751*a9fa9459Szrj 
2752*a9fa9459Szrj   // For local symbols, we will have created a non-RELATIVE dynamic
2753*a9fa9459Szrj   // relocation only if (a) the output is position independent,
2754*a9fa9459Szrj   // (b) the relocation is absolute (not pc- or segment-relative), and
2755*a9fa9459Szrj   // (c) the relocation is not 32 bits wide.
2756*a9fa9459Szrj   if (gsym == NULL)
2757*a9fa9459Szrj     return !(parameters->options().output_is_position_independent()
2758*a9fa9459Szrj 	     && (ref_flags & Symbol::ABSOLUTE_REF)
2759*a9fa9459Szrj 	     && !is_32bit);
2760*a9fa9459Szrj 
2761*a9fa9459Szrj   // For global symbols, we use the same helper routines used in the
2762*a9fa9459Szrj   // scan pass.  If we did not create a dynamic relocation, or if we
2763*a9fa9459Szrj   // created a RELATIVE dynamic relocation, we should apply the static
2764*a9fa9459Szrj   // relocation.
2765*a9fa9459Szrj   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2766*a9fa9459Szrj   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2767*a9fa9459Szrj 		&& gsym->can_use_relative_reloc(ref_flags
2768*a9fa9459Szrj 						& Symbol::FUNCTION_CALL);
2769*a9fa9459Szrj   return !has_dyn || is_rel;
2770*a9fa9459Szrj }
2771*a9fa9459Szrj 
2772*a9fa9459Szrj // Perform a relocation.
2773*a9fa9459Szrj 
2774*a9fa9459Szrj inline bool
relocate(const Relocate_info<32,false> * relinfo,unsigned int,Target_i386 * target,Output_section * output_section,size_t relnum,const unsigned char * preloc,const Sized_symbol<32> * gsym,const Symbol_value<32> * psymval,unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr address,section_size_type view_size)2775*a9fa9459Szrj Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2776*a9fa9459Szrj 				unsigned int,
2777*a9fa9459Szrj 				Target_i386* target,
2778*a9fa9459Szrj 				Output_section* output_section,
2779*a9fa9459Szrj 				size_t relnum,
2780*a9fa9459Szrj 				const unsigned char* preloc,
2781*a9fa9459Szrj 				const Sized_symbol<32>* gsym,
2782*a9fa9459Szrj 				const Symbol_value<32>* psymval,
2783*a9fa9459Szrj 				unsigned char* view,
2784*a9fa9459Szrj 				elfcpp::Elf_types<32>::Elf_Addr address,
2785*a9fa9459Szrj 				section_size_type view_size)
2786*a9fa9459Szrj {
2787*a9fa9459Szrj   const elfcpp::Rel<32, false> rel(preloc);
2788*a9fa9459Szrj   unsigned int r_type = elfcpp::elf_r_type<32>(rel.get_r_info());
2789*a9fa9459Szrj 
2790*a9fa9459Szrj   if (this->skip_call_tls_get_addr_)
2791*a9fa9459Szrj     {
2792*a9fa9459Szrj       if ((r_type != elfcpp::R_386_PLT32
2793*a9fa9459Szrj 	   && r_type != elfcpp::R_386_GOT32X
2794*a9fa9459Szrj 	   && r_type != elfcpp::R_386_PC32)
2795*a9fa9459Szrj 	  || gsym == NULL
2796*a9fa9459Szrj 	  || strcmp(gsym->name(), "___tls_get_addr") != 0)
2797*a9fa9459Szrj 	gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2798*a9fa9459Szrj 			       _("missing expected TLS relocation"));
2799*a9fa9459Szrj       else
2800*a9fa9459Szrj 	{
2801*a9fa9459Szrj 	  this->skip_call_tls_get_addr_ = false;
2802*a9fa9459Szrj 	  return false;
2803*a9fa9459Szrj 	}
2804*a9fa9459Szrj     }
2805*a9fa9459Szrj 
2806*a9fa9459Szrj   if (view == NULL)
2807*a9fa9459Szrj     return true;
2808*a9fa9459Szrj 
2809*a9fa9459Szrj   const Sized_relobj_file<32, false>* object = relinfo->object;
2810*a9fa9459Szrj 
2811*a9fa9459Szrj   // Pick the value to use for symbols defined in shared objects.
2812*a9fa9459Szrj   Symbol_value<32> symval;
2813*a9fa9459Szrj   if (gsym != NULL
2814*a9fa9459Szrj       && gsym->type() == elfcpp::STT_GNU_IFUNC
2815*a9fa9459Szrj       && r_type == elfcpp::R_386_32
2816*a9fa9459Szrj       && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2817*a9fa9459Szrj       && gsym->can_use_relative_reloc(false)
2818*a9fa9459Szrj       && !gsym->is_from_dynobj()
2819*a9fa9459Szrj       && !gsym->is_undefined()
2820*a9fa9459Szrj       && !gsym->is_preemptible())
2821*a9fa9459Szrj     {
2822*a9fa9459Szrj       // In this case we are generating a R_386_IRELATIVE reloc.  We
2823*a9fa9459Szrj       // want to use the real value of the symbol, not the PLT offset.
2824*a9fa9459Szrj     }
2825*a9fa9459Szrj   else if (gsym != NULL
2826*a9fa9459Szrj 	   && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2827*a9fa9459Szrj     {
2828*a9fa9459Szrj       symval.set_output_value(target->plt_address_for_global(gsym));
2829*a9fa9459Szrj       psymval = &symval;
2830*a9fa9459Szrj     }
2831*a9fa9459Szrj   else if (gsym == NULL && psymval->is_ifunc_symbol())
2832*a9fa9459Szrj     {
2833*a9fa9459Szrj       unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2834*a9fa9459Szrj       if (object->local_has_plt_offset(r_sym))
2835*a9fa9459Szrj 	{
2836*a9fa9459Szrj 	  symval.set_output_value(target->plt_address_for_local(object, r_sym));
2837*a9fa9459Szrj 	  psymval = &symval;
2838*a9fa9459Szrj 	}
2839*a9fa9459Szrj     }
2840*a9fa9459Szrj 
2841*a9fa9459Szrj   bool baseless;
2842*a9fa9459Szrj 
2843*a9fa9459Szrj   switch (r_type)
2844*a9fa9459Szrj     {
2845*a9fa9459Szrj     case elfcpp::R_386_NONE:
2846*a9fa9459Szrj     case elfcpp::R_386_GNU_VTINHERIT:
2847*a9fa9459Szrj     case elfcpp::R_386_GNU_VTENTRY:
2848*a9fa9459Szrj       break;
2849*a9fa9459Szrj 
2850*a9fa9459Szrj     case elfcpp::R_386_32:
2851*a9fa9459Szrj       if (should_apply_static_reloc(gsym, r_type, true, output_section))
2852*a9fa9459Szrj 	Relocate_functions<32, false>::rel32(view, object, psymval);
2853*a9fa9459Szrj       break;
2854*a9fa9459Szrj 
2855*a9fa9459Szrj     case elfcpp::R_386_PC32:
2856*a9fa9459Szrj       if (should_apply_static_reloc(gsym, r_type, true, output_section))
2857*a9fa9459Szrj 	Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2858*a9fa9459Szrj       break;
2859*a9fa9459Szrj 
2860*a9fa9459Szrj     case elfcpp::R_386_16:
2861*a9fa9459Szrj       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2862*a9fa9459Szrj 	Relocate_functions<32, false>::rel16(view, object, psymval);
2863*a9fa9459Szrj       break;
2864*a9fa9459Szrj 
2865*a9fa9459Szrj     case elfcpp::R_386_PC16:
2866*a9fa9459Szrj       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2867*a9fa9459Szrj 	Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2868*a9fa9459Szrj       break;
2869*a9fa9459Szrj 
2870*a9fa9459Szrj     case elfcpp::R_386_8:
2871*a9fa9459Szrj       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2872*a9fa9459Szrj 	Relocate_functions<32, false>::rel8(view, object, psymval);
2873*a9fa9459Szrj       break;
2874*a9fa9459Szrj 
2875*a9fa9459Szrj     case elfcpp::R_386_PC8:
2876*a9fa9459Szrj       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2877*a9fa9459Szrj 	Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2878*a9fa9459Szrj       break;
2879*a9fa9459Szrj 
2880*a9fa9459Szrj     case elfcpp::R_386_PLT32:
2881*a9fa9459Szrj       gold_assert(gsym == NULL
2882*a9fa9459Szrj 		  || gsym->has_plt_offset()
2883*a9fa9459Szrj 		  || gsym->final_value_is_known()
2884*a9fa9459Szrj 		  || (gsym->is_defined()
2885*a9fa9459Szrj 		      && !gsym->is_from_dynobj()
2886*a9fa9459Szrj 		      && !gsym->is_preemptible()));
2887*a9fa9459Szrj       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2888*a9fa9459Szrj       break;
2889*a9fa9459Szrj 
2890*a9fa9459Szrj     case elfcpp::R_386_GOT32:
2891*a9fa9459Szrj     case elfcpp::R_386_GOT32X:
2892*a9fa9459Szrj       baseless = (view[-1] & 0xc7) == 0x5;
2893*a9fa9459Szrj       // R_386_GOT32 and R_386_GOT32X don't work without base register
2894*a9fa9459Szrj       // when generating a position-independent output file.
2895*a9fa9459Szrj       if (baseless
2896*a9fa9459Szrj 	  && parameters->options().output_is_position_independent())
2897*a9fa9459Szrj 	{
2898*a9fa9459Szrj 	  if(gsym)
2899*a9fa9459Szrj 	    gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2900*a9fa9459Szrj 				   _("unexpected reloc %u against global symbol %s without base register in object file when generating a position-independent output file"),
2901*a9fa9459Szrj 				   r_type, gsym->demangled_name().c_str());
2902*a9fa9459Szrj 	  else
2903*a9fa9459Szrj 	    gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2904*a9fa9459Szrj 				   _("unexpected reloc %u against local symbol without base register in object file when generating a position-independent output file"),
2905*a9fa9459Szrj 				   r_type);
2906*a9fa9459Szrj 	}
2907*a9fa9459Szrj 
2908*a9fa9459Szrj       // Convert
2909*a9fa9459Szrj       // mov foo@GOT(%reg), %reg
2910*a9fa9459Szrj       // to
2911*a9fa9459Szrj       // lea foo@GOTOFF(%reg), %reg
2912*a9fa9459Szrj       // if possible.
2913*a9fa9459Szrj       if (rel.get_r_offset() >= 2
2914*a9fa9459Szrj 	  && view[-2] == 0x8b
2915*a9fa9459Szrj 	  && ((gsym == NULL && !psymval->is_ifunc_symbol())
2916*a9fa9459Szrj 	      || (gsym != NULL
2917*a9fa9459Szrj 		  && Target_i386::can_convert_mov_to_lea(gsym))))
2918*a9fa9459Szrj 	{
2919*a9fa9459Szrj 	  view[-2] = 0x8d;
2920*a9fa9459Szrj 	  elfcpp::Elf_types<32>::Elf_Addr value;
2921*a9fa9459Szrj 	  value = psymval->value(object, 0);
2922*a9fa9459Szrj 	  // Don't subtract the .got.plt section address for baseless
2923*a9fa9459Szrj 	  // addressing.
2924*a9fa9459Szrj 	  if (!baseless)
2925*a9fa9459Szrj 	    value -= target->got_plt_section()->address();
2926*a9fa9459Szrj 	  Relocate_functions<32, false>::rel32(view, value);
2927*a9fa9459Szrj 	}
2928*a9fa9459Szrj       else
2929*a9fa9459Szrj 	{
2930*a9fa9459Szrj 	  // The GOT pointer points to the end of the GOT section.
2931*a9fa9459Szrj 	  // We need to subtract the size of the GOT section to get
2932*a9fa9459Szrj 	  // the actual offset to use in the relocation.
2933*a9fa9459Szrj 	  unsigned int got_offset = 0;
2934*a9fa9459Szrj 	  if (gsym != NULL)
2935*a9fa9459Szrj 	    {
2936*a9fa9459Szrj 	      gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2937*a9fa9459Szrj 	      got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2938*a9fa9459Szrj 			    - target->got_size());
2939*a9fa9459Szrj 	    }
2940*a9fa9459Szrj 	  else
2941*a9fa9459Szrj 	    {
2942*a9fa9459Szrj 	      unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2943*a9fa9459Szrj 	      gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2944*a9fa9459Szrj 	      got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2945*a9fa9459Szrj 			    - target->got_size());
2946*a9fa9459Szrj 	    }
2947*a9fa9459Szrj 	  // Add the .got.plt section address for baseless addressing.
2948*a9fa9459Szrj 	  if (baseless)
2949*a9fa9459Szrj 	    got_offset += target->got_plt_section()->address();
2950*a9fa9459Szrj 	  Relocate_functions<32, false>::rel32(view, got_offset);
2951*a9fa9459Szrj 	}
2952*a9fa9459Szrj       break;
2953*a9fa9459Szrj 
2954*a9fa9459Szrj     case elfcpp::R_386_GOTOFF:
2955*a9fa9459Szrj       {
2956*a9fa9459Szrj 	elfcpp::Elf_types<32>::Elf_Addr value;
2957*a9fa9459Szrj 	value = (psymval->value(object, 0)
2958*a9fa9459Szrj 		 - target->got_plt_section()->address());
2959*a9fa9459Szrj 	Relocate_functions<32, false>::rel32(view, value);
2960*a9fa9459Szrj       }
2961*a9fa9459Szrj       break;
2962*a9fa9459Szrj 
2963*a9fa9459Szrj     case elfcpp::R_386_GOTPC:
2964*a9fa9459Szrj       {
2965*a9fa9459Szrj 	elfcpp::Elf_types<32>::Elf_Addr value;
2966*a9fa9459Szrj 	value = target->got_plt_section()->address();
2967*a9fa9459Szrj 	Relocate_functions<32, false>::pcrel32(view, value, address);
2968*a9fa9459Szrj       }
2969*a9fa9459Szrj       break;
2970*a9fa9459Szrj 
2971*a9fa9459Szrj     case elfcpp::R_386_COPY:
2972*a9fa9459Szrj     case elfcpp::R_386_GLOB_DAT:
2973*a9fa9459Szrj     case elfcpp::R_386_JUMP_SLOT:
2974*a9fa9459Szrj     case elfcpp::R_386_RELATIVE:
2975*a9fa9459Szrj     case elfcpp::R_386_IRELATIVE:
2976*a9fa9459Szrj       // These are outstanding tls relocs, which are unexpected when
2977*a9fa9459Szrj       // linking.
2978*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF:
2979*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPMOD32:
2980*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPOFF32:
2981*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF32:
2982*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC:
2983*a9fa9459Szrj       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2984*a9fa9459Szrj 			     _("unexpected reloc %u in object file"),
2985*a9fa9459Szrj 			     r_type);
2986*a9fa9459Szrj       break;
2987*a9fa9459Szrj 
2988*a9fa9459Szrj       // These are initial tls relocs, which are expected when
2989*a9fa9459Szrj       // linking.
2990*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:             // Global-dynamic
2991*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:        // Global-dynamic (from ~oliva url)
2992*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
2993*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:            // Local-dynamic
2994*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:         // Alternate local-dynamic
2995*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:             // Initial-exec
2996*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
2997*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
2998*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:             // Local-exec
2999*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
3000*a9fa9459Szrj       this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
3001*a9fa9459Szrj 			 view, address, view_size);
3002*a9fa9459Szrj       break;
3003*a9fa9459Szrj 
3004*a9fa9459Szrj     case elfcpp::R_386_32PLT:
3005*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_32:
3006*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_PUSH:
3007*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_CALL:
3008*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_POP:
3009*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_32:
3010*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_PUSH:
3011*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_CALL:
3012*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_POP:
3013*a9fa9459Szrj     case elfcpp::R_386_USED_BY_INTEL_200:
3014*a9fa9459Szrj     default:
3015*a9fa9459Szrj       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3016*a9fa9459Szrj 			     _("unsupported reloc %u"),
3017*a9fa9459Szrj 			     r_type);
3018*a9fa9459Szrj       break;
3019*a9fa9459Szrj     }
3020*a9fa9459Szrj 
3021*a9fa9459Szrj   return true;
3022*a9fa9459Szrj }
3023*a9fa9459Szrj 
3024*a9fa9459Szrj // Perform a TLS relocation.
3025*a9fa9459Szrj 
3026*a9fa9459Szrj inline void
relocate_tls(const Relocate_info<32,false> * relinfo,Target_i386 * target,size_t relnum,const elfcpp::Rel<32,false> & rel,unsigned int r_type,const Sized_symbol<32> * gsym,const Symbol_value<32> * psymval,unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr,section_size_type view_size)3027*a9fa9459Szrj Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
3028*a9fa9459Szrj 				    Target_i386* target,
3029*a9fa9459Szrj 				    size_t relnum,
3030*a9fa9459Szrj 				    const elfcpp::Rel<32, false>& rel,
3031*a9fa9459Szrj 				    unsigned int r_type,
3032*a9fa9459Szrj 				    const Sized_symbol<32>* gsym,
3033*a9fa9459Szrj 				    const Symbol_value<32>* psymval,
3034*a9fa9459Szrj 				    unsigned char* view,
3035*a9fa9459Szrj 				    elfcpp::Elf_types<32>::Elf_Addr,
3036*a9fa9459Szrj 				    section_size_type view_size)
3037*a9fa9459Szrj {
3038*a9fa9459Szrj   Output_segment* tls_segment = relinfo->layout->tls_segment();
3039*a9fa9459Szrj 
3040*a9fa9459Szrj   const Sized_relobj_file<32, false>* object = relinfo->object;
3041*a9fa9459Szrj 
3042*a9fa9459Szrj   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
3043*a9fa9459Szrj 
3044*a9fa9459Szrj   const bool is_final = (gsym == NULL
3045*a9fa9459Szrj 			 ? !parameters->options().shared()
3046*a9fa9459Szrj 			 : gsym->final_value_is_known());
3047*a9fa9459Szrj   const tls::Tls_optimization optimized_type
3048*a9fa9459Szrj       = Target_i386::optimize_tls_reloc(is_final, r_type);
3049*a9fa9459Szrj   switch (r_type)
3050*a9fa9459Szrj     {
3051*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:           // Global-dynamic
3052*a9fa9459Szrj       if (optimized_type == tls::TLSOPT_TO_LE)
3053*a9fa9459Szrj 	{
3054*a9fa9459Szrj 	  if (tls_segment == NULL)
3055*a9fa9459Szrj 	    {
3056*a9fa9459Szrj 	      gold_assert(parameters->errors()->error_count() > 0
3057*a9fa9459Szrj 			  || issue_undefined_symbol_error(gsym));
3058*a9fa9459Szrj 	      return;
3059*a9fa9459Szrj 	    }
3060*a9fa9459Szrj 	  this->tls_gd_to_le(relinfo, relnum, tls_segment,
3061*a9fa9459Szrj 			     rel, r_type, value, view,
3062*a9fa9459Szrj 			     view_size);
3063*a9fa9459Szrj 	  break;
3064*a9fa9459Szrj 	}
3065*a9fa9459Szrj       else
3066*a9fa9459Szrj 	{
3067*a9fa9459Szrj 	  unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3068*a9fa9459Szrj 				   ? GOT_TYPE_TLS_NOFFSET
3069*a9fa9459Szrj 				   : GOT_TYPE_TLS_PAIR);
3070*a9fa9459Szrj 	  unsigned int got_offset;
3071*a9fa9459Szrj 	  if (gsym != NULL)
3072*a9fa9459Szrj 	    {
3073*a9fa9459Szrj 	      gold_assert(gsym->has_got_offset(got_type));
3074*a9fa9459Szrj 	      got_offset = gsym->got_offset(got_type) - target->got_size();
3075*a9fa9459Szrj 	    }
3076*a9fa9459Szrj 	  else
3077*a9fa9459Szrj 	    {
3078*a9fa9459Szrj 	      unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3079*a9fa9459Szrj 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
3080*a9fa9459Szrj 	      got_offset = (object->local_got_offset(r_sym, got_type)
3081*a9fa9459Szrj 			    - target->got_size());
3082*a9fa9459Szrj 	    }
3083*a9fa9459Szrj 	  if (optimized_type == tls::TLSOPT_TO_IE)
3084*a9fa9459Szrj 	    {
3085*a9fa9459Szrj 	      this->tls_gd_to_ie(relinfo, relnum, rel, r_type,
3086*a9fa9459Szrj 				 got_offset, view, view_size);
3087*a9fa9459Szrj 	      break;
3088*a9fa9459Szrj 	    }
3089*a9fa9459Szrj 	  else if (optimized_type == tls::TLSOPT_NONE)
3090*a9fa9459Szrj 	    {
3091*a9fa9459Szrj 	      // Relocate the field with the offset of the pair of GOT
3092*a9fa9459Szrj 	      // entries.
3093*a9fa9459Szrj 	      Relocate_functions<32, false>::rel32(view, got_offset);
3094*a9fa9459Szrj 	      break;
3095*a9fa9459Szrj 	    }
3096*a9fa9459Szrj 	}
3097*a9fa9459Szrj       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3098*a9fa9459Szrj 			     _("unsupported reloc %u"),
3099*a9fa9459Szrj 			     r_type);
3100*a9fa9459Szrj       break;
3101*a9fa9459Szrj 
3102*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:      // Global-dynamic (from ~oliva url)
3103*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
3104*a9fa9459Szrj       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
3105*a9fa9459Szrj       if (optimized_type == tls::TLSOPT_TO_LE)
3106*a9fa9459Szrj 	{
3107*a9fa9459Szrj 	  if (tls_segment == NULL)
3108*a9fa9459Szrj 	    {
3109*a9fa9459Szrj 	      gold_assert(parameters->errors()->error_count() > 0
3110*a9fa9459Szrj 			  || issue_undefined_symbol_error(gsym));
3111*a9fa9459Szrj 	      return;
3112*a9fa9459Szrj 	    }
3113*a9fa9459Szrj 	  this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3114*a9fa9459Szrj 				  rel, r_type, value, view,
3115*a9fa9459Szrj 				  view_size);
3116*a9fa9459Szrj 	  break;
3117*a9fa9459Szrj 	}
3118*a9fa9459Szrj       else
3119*a9fa9459Szrj 	{
3120*a9fa9459Szrj 	  unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3121*a9fa9459Szrj 				   ? GOT_TYPE_TLS_NOFFSET
3122*a9fa9459Szrj 				   : GOT_TYPE_TLS_DESC);
3123*a9fa9459Szrj 	  unsigned int got_offset = 0;
3124*a9fa9459Szrj 	  if (r_type == elfcpp::R_386_TLS_GOTDESC
3125*a9fa9459Szrj 	      && optimized_type == tls::TLSOPT_NONE)
3126*a9fa9459Szrj 	    {
3127*a9fa9459Szrj 	      // We created GOT entries in the .got.tlsdesc portion of
3128*a9fa9459Szrj 	      // the .got.plt section, but the offset stored in the
3129*a9fa9459Szrj 	      // symbol is the offset within .got.tlsdesc.
3130*a9fa9459Szrj 	      got_offset = (target->got_size()
3131*a9fa9459Szrj 			    + target->got_plt_section()->data_size());
3132*a9fa9459Szrj 	    }
3133*a9fa9459Szrj 	  if (gsym != NULL)
3134*a9fa9459Szrj 	    {
3135*a9fa9459Szrj 	      gold_assert(gsym->has_got_offset(got_type));
3136*a9fa9459Szrj 	      got_offset += gsym->got_offset(got_type) - target->got_size();
3137*a9fa9459Szrj 	    }
3138*a9fa9459Szrj 	  else
3139*a9fa9459Szrj 	    {
3140*a9fa9459Szrj 	      unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3141*a9fa9459Szrj 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
3142*a9fa9459Szrj 	      got_offset += (object->local_got_offset(r_sym, got_type)
3143*a9fa9459Szrj 			     - target->got_size());
3144*a9fa9459Szrj 	    }
3145*a9fa9459Szrj 	  if (optimized_type == tls::TLSOPT_TO_IE)
3146*a9fa9459Szrj 	    {
3147*a9fa9459Szrj 	      this->tls_desc_gd_to_ie(relinfo, relnum, rel, r_type,
3148*a9fa9459Szrj 				      got_offset, view, view_size);
3149*a9fa9459Szrj 	      break;
3150*a9fa9459Szrj 	    }
3151*a9fa9459Szrj 	  else if (optimized_type == tls::TLSOPT_NONE)
3152*a9fa9459Szrj 	    {
3153*a9fa9459Szrj 	      if (r_type == elfcpp::R_386_TLS_GOTDESC)
3154*a9fa9459Szrj 		{
3155*a9fa9459Szrj 		  // Relocate the field with the offset of the pair of GOT
3156*a9fa9459Szrj 		  // entries.
3157*a9fa9459Szrj 		  Relocate_functions<32, false>::rel32(view, got_offset);
3158*a9fa9459Szrj 		}
3159*a9fa9459Szrj 	      break;
3160*a9fa9459Szrj 	    }
3161*a9fa9459Szrj 	}
3162*a9fa9459Szrj       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3163*a9fa9459Szrj 			     _("unsupported reloc %u"),
3164*a9fa9459Szrj 			     r_type);
3165*a9fa9459Szrj       break;
3166*a9fa9459Szrj 
3167*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:          // Local-dynamic
3168*a9fa9459Szrj       if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
3169*a9fa9459Szrj 	{
3170*a9fa9459Szrj 	  gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3171*a9fa9459Szrj 				 _("both SUN and GNU model "
3172*a9fa9459Szrj 				   "TLS relocations"));
3173*a9fa9459Szrj 	  break;
3174*a9fa9459Szrj 	}
3175*a9fa9459Szrj       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
3176*a9fa9459Szrj       if (optimized_type == tls::TLSOPT_TO_LE)
3177*a9fa9459Szrj 	{
3178*a9fa9459Szrj 	  if (tls_segment == NULL)
3179*a9fa9459Szrj 	    {
3180*a9fa9459Szrj 	      gold_assert(parameters->errors()->error_count() > 0
3181*a9fa9459Szrj 			  || issue_undefined_symbol_error(gsym));
3182*a9fa9459Szrj 	      return;
3183*a9fa9459Szrj 	    }
3184*a9fa9459Szrj 	  this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
3185*a9fa9459Szrj 			     value, view, view_size);
3186*a9fa9459Szrj 	  break;
3187*a9fa9459Szrj 	}
3188*a9fa9459Szrj       else if (optimized_type == tls::TLSOPT_NONE)
3189*a9fa9459Szrj 	{
3190*a9fa9459Szrj 	  // Relocate the field with the offset of the GOT entry for
3191*a9fa9459Szrj 	  // the module index.
3192*a9fa9459Szrj 	  unsigned int got_offset;
3193*a9fa9459Szrj 	  got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3194*a9fa9459Szrj 			- target->got_size());
3195*a9fa9459Szrj 	  Relocate_functions<32, false>::rel32(view, got_offset);
3196*a9fa9459Szrj 	  break;
3197*a9fa9459Szrj 	}
3198*a9fa9459Szrj       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3199*a9fa9459Szrj 			     _("unsupported reloc %u"),
3200*a9fa9459Szrj 			     r_type);
3201*a9fa9459Szrj       break;
3202*a9fa9459Szrj 
3203*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:       // Alternate local-dynamic
3204*a9fa9459Szrj       if (optimized_type == tls::TLSOPT_TO_LE)
3205*a9fa9459Szrj 	{
3206*a9fa9459Szrj 	  // This reloc can appear in debugging sections, in which
3207*a9fa9459Szrj 	  // case we must not convert to local-exec.  We decide what
3208*a9fa9459Szrj 	  // to do based on whether the section is marked as
3209*a9fa9459Szrj 	  // containing executable code.  That is what the GNU linker
3210*a9fa9459Szrj 	  // does as well.
3211*a9fa9459Szrj 	  elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
3212*a9fa9459Szrj 	  if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
3213*a9fa9459Szrj 	    {
3214*a9fa9459Szrj 	      if (tls_segment == NULL)
3215*a9fa9459Szrj 		{
3216*a9fa9459Szrj 		  gold_assert(parameters->errors()->error_count() > 0
3217*a9fa9459Szrj 			      || issue_undefined_symbol_error(gsym));
3218*a9fa9459Szrj 		  return;
3219*a9fa9459Szrj 		}
3220*a9fa9459Szrj 	      value -= tls_segment->memsz();
3221*a9fa9459Szrj 	    }
3222*a9fa9459Szrj 	}
3223*a9fa9459Szrj       Relocate_functions<32, false>::rel32(view, value);
3224*a9fa9459Szrj       break;
3225*a9fa9459Szrj 
3226*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:           // Initial-exec
3227*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
3228*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
3229*a9fa9459Szrj       if (optimized_type == tls::TLSOPT_TO_LE)
3230*a9fa9459Szrj 	{
3231*a9fa9459Szrj 	  if (tls_segment == NULL)
3232*a9fa9459Szrj 	    {
3233*a9fa9459Szrj 	      gold_assert(parameters->errors()->error_count() > 0
3234*a9fa9459Szrj 			  || issue_undefined_symbol_error(gsym));
3235*a9fa9459Szrj 	      return;
3236*a9fa9459Szrj 	    }
3237*a9fa9459Szrj 	  Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3238*a9fa9459Szrj 					      rel, r_type, value, view,
3239*a9fa9459Szrj 					      view_size);
3240*a9fa9459Szrj 	  break;
3241*a9fa9459Szrj 	}
3242*a9fa9459Szrj       else if (optimized_type == tls::TLSOPT_NONE)
3243*a9fa9459Szrj 	{
3244*a9fa9459Szrj 	  // Relocate the field with the offset of the GOT entry for
3245*a9fa9459Szrj 	  // the tp-relative offset of the symbol.
3246*a9fa9459Szrj 	  unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
3247*a9fa9459Szrj 				   ? GOT_TYPE_TLS_OFFSET
3248*a9fa9459Szrj 				   : GOT_TYPE_TLS_NOFFSET);
3249*a9fa9459Szrj 	  unsigned int got_offset;
3250*a9fa9459Szrj 	  if (gsym != NULL)
3251*a9fa9459Szrj 	    {
3252*a9fa9459Szrj 	      gold_assert(gsym->has_got_offset(got_type));
3253*a9fa9459Szrj 	      got_offset = gsym->got_offset(got_type);
3254*a9fa9459Szrj 	    }
3255*a9fa9459Szrj 	  else
3256*a9fa9459Szrj 	    {
3257*a9fa9459Szrj 	      unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3258*a9fa9459Szrj 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
3259*a9fa9459Szrj 	      got_offset = object->local_got_offset(r_sym, got_type);
3260*a9fa9459Szrj 	    }
3261*a9fa9459Szrj 	  // For the R_386_TLS_IE relocation, we need to apply the
3262*a9fa9459Szrj 	  // absolute address of the GOT entry.
3263*a9fa9459Szrj 	  if (r_type == elfcpp::R_386_TLS_IE)
3264*a9fa9459Szrj 	    got_offset += target->got_plt_section()->address();
3265*a9fa9459Szrj 	  // All GOT offsets are relative to the end of the GOT.
3266*a9fa9459Szrj 	  got_offset -= target->got_size();
3267*a9fa9459Szrj 	  Relocate_functions<32, false>::rel32(view, got_offset);
3268*a9fa9459Szrj 	  break;
3269*a9fa9459Szrj 	}
3270*a9fa9459Szrj       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3271*a9fa9459Szrj 			     _("unsupported reloc %u"),
3272*a9fa9459Szrj 			     r_type);
3273*a9fa9459Szrj       break;
3274*a9fa9459Szrj 
3275*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:           // Local-exec
3276*a9fa9459Szrj       // If we're creating a shared library, a dynamic relocation will
3277*a9fa9459Szrj       // have been created for this location, so do not apply it now.
3278*a9fa9459Szrj       if (!parameters->options().shared())
3279*a9fa9459Szrj 	{
3280*a9fa9459Szrj 	  if (tls_segment == NULL)
3281*a9fa9459Szrj 	    {
3282*a9fa9459Szrj 	      gold_assert(parameters->errors()->error_count() > 0
3283*a9fa9459Szrj 			  || issue_undefined_symbol_error(gsym));
3284*a9fa9459Szrj 	      return;
3285*a9fa9459Szrj 	    }
3286*a9fa9459Szrj 	  value -= tls_segment->memsz();
3287*a9fa9459Szrj 	  Relocate_functions<32, false>::rel32(view, value);
3288*a9fa9459Szrj 	}
3289*a9fa9459Szrj       break;
3290*a9fa9459Szrj 
3291*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
3292*a9fa9459Szrj       // If we're creating a shared library, a dynamic relocation will
3293*a9fa9459Szrj       // have been created for this location, so do not apply it now.
3294*a9fa9459Szrj       if (!parameters->options().shared())
3295*a9fa9459Szrj 	{
3296*a9fa9459Szrj 	  if (tls_segment == NULL)
3297*a9fa9459Szrj 	    {
3298*a9fa9459Szrj 	      gold_assert(parameters->errors()->error_count() > 0
3299*a9fa9459Szrj 			  || issue_undefined_symbol_error(gsym));
3300*a9fa9459Szrj 	      return;
3301*a9fa9459Szrj 	    }
3302*a9fa9459Szrj 	  value = tls_segment->memsz() - value;
3303*a9fa9459Szrj 	  Relocate_functions<32, false>::rel32(view, value);
3304*a9fa9459Szrj 	}
3305*a9fa9459Szrj       break;
3306*a9fa9459Szrj     }
3307*a9fa9459Szrj }
3308*a9fa9459Szrj 
3309*a9fa9459Szrj // Do a relocation in which we convert a TLS General-Dynamic to a
3310*a9fa9459Szrj // Local-Exec.
3311*a9fa9459Szrj 
3312*a9fa9459Szrj inline void
tls_gd_to_le(const Relocate_info<32,false> * relinfo,size_t relnum,Output_segment * tls_segment,const elfcpp::Rel<32,false> & rel,unsigned int,elfcpp::Elf_types<32>::Elf_Addr value,unsigned char * view,section_size_type view_size)3313*a9fa9459Szrj Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
3314*a9fa9459Szrj 				    size_t relnum,
3315*a9fa9459Szrj 				    Output_segment* tls_segment,
3316*a9fa9459Szrj 				    const elfcpp::Rel<32, false>& rel,
3317*a9fa9459Szrj 				    unsigned int,
3318*a9fa9459Szrj 				    elfcpp::Elf_types<32>::Elf_Addr value,
3319*a9fa9459Szrj 				    unsigned char* view,
3320*a9fa9459Szrj 				    section_size_type view_size)
3321*a9fa9459Szrj {
3322*a9fa9459Szrj   // leal foo(,%ebx,1),%eax; call ___tls_get_addr@PLT
3323*a9fa9459Szrj   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3324*a9fa9459Szrj   // leal foo(%ebx),%eax; call ___tls_get_addr@PLT
3325*a9fa9459Szrj   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3326*a9fa9459Szrj   // leal foo(%reg),%eax; call *___tls_get_addr@GOT(%reg)
3327*a9fa9459Szrj   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3328*a9fa9459Szrj 
3329*a9fa9459Szrj   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3330*a9fa9459Szrj   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3331*a9fa9459Szrj 
3332*a9fa9459Szrj   unsigned char op1 = view[-1];
3333*a9fa9459Szrj   unsigned char op2 = view[-2];
3334*a9fa9459Szrj   unsigned char op3 = view[4];
3335*a9fa9459Szrj 
3336*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3337*a9fa9459Szrj 		 op2 == 0x8d || op2 == 0x04);
3338*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3339*a9fa9459Szrj 		 op3 == 0xe8 || op3 == 0xff);
3340*a9fa9459Szrj 
3341*a9fa9459Szrj   int roff = 5;
3342*a9fa9459Szrj 
3343*a9fa9459Szrj   if (op2 == 0x04)
3344*a9fa9459Szrj     {
3345*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3346*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3347*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3348*a9fa9459Szrj 		     ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3349*a9fa9459Szrj       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3350*a9fa9459Szrj     }
3351*a9fa9459Szrj   else
3352*a9fa9459Szrj     {
3353*a9fa9459Szrj       unsigned char reg = op1 & 7;
3354*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3355*a9fa9459Szrj 		     ((op1 & 0xf8) == 0x80
3356*a9fa9459Szrj 		      && reg != 4
3357*a9fa9459Szrj 		      && reg != 0
3358*a9fa9459Szrj 		      && (op3 == 0xe8 || (view[5] & 0x7) == reg)));
3359*a9fa9459Szrj       if (op3 == 0xff
3360*a9fa9459Szrj 	  || (rel.get_r_offset() + 9 < view_size
3361*a9fa9459Szrj 	      && view[9] == 0x90))
3362*a9fa9459Szrj 	{
3363*a9fa9459Szrj 	  // There is an indirect call or a trailing nop.  Use the size
3364*a9fa9459Szrj 	  // byte subl.
3365*a9fa9459Szrj 	  memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3366*a9fa9459Szrj 	  roff = 6;
3367*a9fa9459Szrj 	}
3368*a9fa9459Szrj       else
3369*a9fa9459Szrj 	{
3370*a9fa9459Szrj 	  // Use the five byte subl.
3371*a9fa9459Szrj 	  memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3372*a9fa9459Szrj 	}
3373*a9fa9459Szrj     }
3374*a9fa9459Szrj 
3375*a9fa9459Szrj   value = tls_segment->memsz() - value;
3376*a9fa9459Szrj   Relocate_functions<32, false>::rel32(view + roff, value);
3377*a9fa9459Szrj 
3378*a9fa9459Szrj   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3379*a9fa9459Szrj   // We can skip it.
3380*a9fa9459Szrj   this->skip_call_tls_get_addr_ = true;
3381*a9fa9459Szrj }
3382*a9fa9459Szrj 
3383*a9fa9459Szrj // Do a relocation in which we convert a TLS General-Dynamic to an
3384*a9fa9459Szrj // Initial-Exec.
3385*a9fa9459Szrj 
3386*a9fa9459Szrj inline void
tls_gd_to_ie(const Relocate_info<32,false> * relinfo,size_t relnum,const elfcpp::Rel<32,false> & rel,unsigned int,elfcpp::Elf_types<32>::Elf_Addr value,unsigned char * view,section_size_type view_size)3387*a9fa9459Szrj Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3388*a9fa9459Szrj 				    size_t relnum,
3389*a9fa9459Szrj 				    const elfcpp::Rel<32, false>& rel,
3390*a9fa9459Szrj 				    unsigned int,
3391*a9fa9459Szrj 				    elfcpp::Elf_types<32>::Elf_Addr value,
3392*a9fa9459Szrj 				    unsigned char* view,
3393*a9fa9459Szrj 				    section_size_type view_size)
3394*a9fa9459Szrj {
3395*a9fa9459Szrj   // leal foo(,%ebx,1),%eax; call ___tls_get_addr@PLT
3396*a9fa9459Szrj   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3397*a9fa9459Szrj   // leal foo(%ebx),%eax; call ___tls_get_addr@PLT; nop
3398*a9fa9459Szrj   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3399*a9fa9459Szrj   // leal foo(%reg),%eax; call *___tls_get_addr@GOT(%reg)
3400*a9fa9459Szrj   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%reg),%eax
3401*a9fa9459Szrj 
3402*a9fa9459Szrj   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3403*a9fa9459Szrj   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3404*a9fa9459Szrj 
3405*a9fa9459Szrj   unsigned char op1 = view[-1];
3406*a9fa9459Szrj   unsigned char op2 = view[-2];
3407*a9fa9459Szrj   unsigned char op3 = view[4];
3408*a9fa9459Szrj 
3409*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3410*a9fa9459Szrj 		 op2 == 0x8d || op2 == 0x04);
3411*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3412*a9fa9459Szrj 		 op3 == 0xe8 || op3 == 0xff);
3413*a9fa9459Szrj 
3414*a9fa9459Szrj   int roff;
3415*a9fa9459Szrj 
3416*a9fa9459Szrj   if (op2 == 0x04)
3417*a9fa9459Szrj     {
3418*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3419*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3420*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3421*a9fa9459Szrj 		     ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3422*a9fa9459Szrj       roff = 5;
3423*a9fa9459Szrj     }
3424*a9fa9459Szrj   else
3425*a9fa9459Szrj     {
3426*a9fa9459Szrj       unsigned char reg = op1 & 7;
3427*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 10);
3428*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3429*a9fa9459Szrj 		     ((op1 & 0xf8) == 0x80
3430*a9fa9459Szrj 		      && reg != 4
3431*a9fa9459Szrj 		      && reg != 0
3432*a9fa9459Szrj 		      && ((op3 == 0xe8 && view[9] == 0x90)
3433*a9fa9459Szrj 			   || (view[5] & 0x7) == reg)));
3434*a9fa9459Szrj       roff = 6;
3435*a9fa9459Szrj     }
3436*a9fa9459Szrj 
3437*a9fa9459Szrj   memcpy(view + roff - 8, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3438*a9fa9459Szrj   Relocate_functions<32, false>::rel32(view + roff, value);
3439*a9fa9459Szrj 
3440*a9fa9459Szrj   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3441*a9fa9459Szrj   // We can skip it.
3442*a9fa9459Szrj   this->skip_call_tls_get_addr_ = true;
3443*a9fa9459Szrj }
3444*a9fa9459Szrj 
3445*a9fa9459Szrj // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3446*a9fa9459Szrj // General-Dynamic to a Local-Exec.
3447*a9fa9459Szrj 
3448*a9fa9459Szrj inline void
tls_desc_gd_to_le(const Relocate_info<32,false> * relinfo,size_t relnum,Output_segment * tls_segment,const elfcpp::Rel<32,false> & rel,unsigned int r_type,elfcpp::Elf_types<32>::Elf_Addr value,unsigned char * view,section_size_type view_size)3449*a9fa9459Szrj Target_i386::Relocate::tls_desc_gd_to_le(
3450*a9fa9459Szrj     const Relocate_info<32, false>* relinfo,
3451*a9fa9459Szrj     size_t relnum,
3452*a9fa9459Szrj     Output_segment* tls_segment,
3453*a9fa9459Szrj     const elfcpp::Rel<32, false>& rel,
3454*a9fa9459Szrj     unsigned int r_type,
3455*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr value,
3456*a9fa9459Szrj     unsigned char* view,
3457*a9fa9459Szrj     section_size_type view_size)
3458*a9fa9459Szrj {
3459*a9fa9459Szrj   if (r_type == elfcpp::R_386_TLS_GOTDESC)
3460*a9fa9459Szrj     {
3461*a9fa9459Szrj       // leal foo@TLSDESC(%ebx), %eax
3462*a9fa9459Szrj       // ==> leal foo@NTPOFF, %eax
3463*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3464*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3465*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3466*a9fa9459Szrj 		     view[-2] == 0x8d && view[-1] == 0x83);
3467*a9fa9459Szrj       view[-1] = 0x05;
3468*a9fa9459Szrj       value -= tls_segment->memsz();
3469*a9fa9459Szrj       Relocate_functions<32, false>::rel32(view, value);
3470*a9fa9459Szrj     }
3471*a9fa9459Szrj   else
3472*a9fa9459Szrj     {
3473*a9fa9459Szrj       // call *foo@TLSCALL(%eax)
3474*a9fa9459Szrj       // ==> nop; nop
3475*a9fa9459Szrj       gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3476*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3477*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3478*a9fa9459Szrj 		     view[0] == 0xff && view[1] == 0x10);
3479*a9fa9459Szrj       view[0] = 0x66;
3480*a9fa9459Szrj       view[1] = 0x90;
3481*a9fa9459Szrj     }
3482*a9fa9459Szrj }
3483*a9fa9459Szrj 
3484*a9fa9459Szrj // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3485*a9fa9459Szrj // General-Dynamic to an Initial-Exec.
3486*a9fa9459Szrj 
3487*a9fa9459Szrj inline void
tls_desc_gd_to_ie(const Relocate_info<32,false> * relinfo,size_t relnum,const elfcpp::Rel<32,false> & rel,unsigned int r_type,elfcpp::Elf_types<32>::Elf_Addr value,unsigned char * view,section_size_type view_size)3488*a9fa9459Szrj Target_i386::Relocate::tls_desc_gd_to_ie(
3489*a9fa9459Szrj     const Relocate_info<32, false>* relinfo,
3490*a9fa9459Szrj     size_t relnum,
3491*a9fa9459Szrj     const elfcpp::Rel<32, false>& rel,
3492*a9fa9459Szrj     unsigned int r_type,
3493*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr value,
3494*a9fa9459Szrj     unsigned char* view,
3495*a9fa9459Szrj     section_size_type view_size)
3496*a9fa9459Szrj {
3497*a9fa9459Szrj   if (r_type == elfcpp::R_386_TLS_GOTDESC)
3498*a9fa9459Szrj     {
3499*a9fa9459Szrj       // leal foo@TLSDESC(%ebx), %eax
3500*a9fa9459Szrj       // ==> movl foo@GOTNTPOFF(%ebx), %eax
3501*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3502*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3503*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3504*a9fa9459Szrj 		     view[-2] == 0x8d && view[-1] == 0x83);
3505*a9fa9459Szrj       view[-2] = 0x8b;
3506*a9fa9459Szrj       Relocate_functions<32, false>::rel32(view, value);
3507*a9fa9459Szrj     }
3508*a9fa9459Szrj   else
3509*a9fa9459Szrj     {
3510*a9fa9459Szrj       // call *foo@TLSCALL(%eax)
3511*a9fa9459Szrj       // ==> nop; nop
3512*a9fa9459Szrj       gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3513*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3514*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3515*a9fa9459Szrj 		     view[0] == 0xff && view[1] == 0x10);
3516*a9fa9459Szrj       view[0] = 0x66;
3517*a9fa9459Szrj       view[1] = 0x90;
3518*a9fa9459Szrj     }
3519*a9fa9459Szrj }
3520*a9fa9459Szrj 
3521*a9fa9459Szrj // Do a relocation in which we convert a TLS Local-Dynamic to a
3522*a9fa9459Szrj // Local-Exec.
3523*a9fa9459Szrj 
3524*a9fa9459Szrj inline void
tls_ld_to_le(const Relocate_info<32,false> * relinfo,size_t relnum,Output_segment *,const elfcpp::Rel<32,false> & rel,unsigned int,elfcpp::Elf_types<32>::Elf_Addr,unsigned char * view,section_size_type view_size)3525*a9fa9459Szrj Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3526*a9fa9459Szrj 				    size_t relnum,
3527*a9fa9459Szrj 				    Output_segment*,
3528*a9fa9459Szrj 				    const elfcpp::Rel<32, false>& rel,
3529*a9fa9459Szrj 				    unsigned int,
3530*a9fa9459Szrj 				    elfcpp::Elf_types<32>::Elf_Addr,
3531*a9fa9459Szrj 				    unsigned char* view,
3532*a9fa9459Szrj 				    section_size_type view_size)
3533*a9fa9459Szrj {
3534*a9fa9459Szrj   // leal foo(%ebx), %eax; call ___tls_get_addr@PLT
3535*a9fa9459Szrj   // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3536*a9fa9459Szrj   // leal foo(%reg), %eax; call call *___tls_get_addr@GOT(%reg)
3537*a9fa9459Szrj   // ==> movl %gs:0,%eax; leal (%esi),%esi
3538*a9fa9459Szrj 
3539*a9fa9459Szrj   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3540*a9fa9459Szrj 
3541*a9fa9459Szrj   unsigned char op1 = view[-1];
3542*a9fa9459Szrj   unsigned char op2 = view[-2];
3543*a9fa9459Szrj   unsigned char op3 = view[4];
3544*a9fa9459Szrj 
3545*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3546*a9fa9459Szrj 		 op3 == 0xe8 || op3 == 0xff);
3547*a9fa9459Szrj   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size,
3548*a9fa9459Szrj 		   op3 == 0xe8 ? 9 : 10);
3549*a9fa9459Szrj 
3550*a9fa9459Szrj   // FIXME: Does this test really always pass?
3551*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x8d);
3552*a9fa9459Szrj 
3553*a9fa9459Szrj   unsigned char reg = op1 & 7;
3554*a9fa9459Szrj   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3555*a9fa9459Szrj 		 ((op1 & 0xf8) == 0x80
3556*a9fa9459Szrj 		  && reg != 4
3557*a9fa9459Szrj 		  && reg != 0
3558*a9fa9459Szrj 		  && (op3 == 0xe8 || (view[5] & 0x7) == reg)));
3559*a9fa9459Szrj 
3560*a9fa9459Szrj   if (op3 == 0xe8)
3561*a9fa9459Szrj     memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3562*a9fa9459Szrj   else
3563*a9fa9459Szrj     memcpy(view - 2, "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0\0", 12);
3564*a9fa9459Szrj 
3565*a9fa9459Szrj   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3566*a9fa9459Szrj   // We can skip it.
3567*a9fa9459Szrj   this->skip_call_tls_get_addr_ = true;
3568*a9fa9459Szrj }
3569*a9fa9459Szrj 
3570*a9fa9459Szrj // Do a relocation in which we convert a TLS Initial-Exec to a
3571*a9fa9459Szrj // Local-Exec.
3572*a9fa9459Szrj 
3573*a9fa9459Szrj inline void
tls_ie_to_le(const Relocate_info<32,false> * relinfo,size_t relnum,Output_segment * tls_segment,const elfcpp::Rel<32,false> & rel,unsigned int r_type,elfcpp::Elf_types<32>::Elf_Addr value,unsigned char * view,section_size_type view_size)3574*a9fa9459Szrj Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3575*a9fa9459Szrj 				    size_t relnum,
3576*a9fa9459Szrj 				    Output_segment* tls_segment,
3577*a9fa9459Szrj 				    const elfcpp::Rel<32, false>& rel,
3578*a9fa9459Szrj 				    unsigned int r_type,
3579*a9fa9459Szrj 				    elfcpp::Elf_types<32>::Elf_Addr value,
3580*a9fa9459Szrj 				    unsigned char* view,
3581*a9fa9459Szrj 				    section_size_type view_size)
3582*a9fa9459Szrj {
3583*a9fa9459Szrj   // We have to actually change the instructions, which means that we
3584*a9fa9459Szrj   // need to examine the opcodes to figure out which instruction we
3585*a9fa9459Szrj   // are looking at.
3586*a9fa9459Szrj   if (r_type == elfcpp::R_386_TLS_IE)
3587*a9fa9459Szrj     {
3588*a9fa9459Szrj       // movl %gs:XX,%eax  ==>  movl $YY,%eax
3589*a9fa9459Szrj       // movl %gs:XX,%reg  ==>  movl $YY,%reg
3590*a9fa9459Szrj       // addl %gs:XX,%reg  ==>  addl $YY,%reg
3591*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3592*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3593*a9fa9459Szrj 
3594*a9fa9459Szrj       unsigned char op1 = view[-1];
3595*a9fa9459Szrj       if (op1 == 0xa1)
3596*a9fa9459Szrj 	{
3597*a9fa9459Szrj 	  // movl XX,%eax  ==>  movl $YY,%eax
3598*a9fa9459Szrj 	  view[-1] = 0xb8;
3599*a9fa9459Szrj 	}
3600*a9fa9459Szrj       else
3601*a9fa9459Szrj 	{
3602*a9fa9459Szrj 	  tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3603*a9fa9459Szrj 
3604*a9fa9459Szrj 	  unsigned char op2 = view[-2];
3605*a9fa9459Szrj 	  if (op2 == 0x8b)
3606*a9fa9459Szrj 	    {
3607*a9fa9459Szrj 	      // movl XX,%reg  ==>  movl $YY,%reg
3608*a9fa9459Szrj 	      tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3609*a9fa9459Szrj 			     (op1 & 0xc7) == 0x05);
3610*a9fa9459Szrj 	      view[-2] = 0xc7;
3611*a9fa9459Szrj 	      view[-1] = 0xc0 | ((op1 >> 3) & 7);
3612*a9fa9459Szrj 	    }
3613*a9fa9459Szrj 	  else if (op2 == 0x03)
3614*a9fa9459Szrj 	    {
3615*a9fa9459Szrj 	      // addl XX,%reg  ==>  addl $YY,%reg
3616*a9fa9459Szrj 	      tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3617*a9fa9459Szrj 			     (op1 & 0xc7) == 0x05);
3618*a9fa9459Szrj 	      view[-2] = 0x81;
3619*a9fa9459Szrj 	      view[-1] = 0xc0 | ((op1 >> 3) & 7);
3620*a9fa9459Szrj 	    }
3621*a9fa9459Szrj 	  else
3622*a9fa9459Szrj 	    tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3623*a9fa9459Szrj 	}
3624*a9fa9459Szrj     }
3625*a9fa9459Szrj   else
3626*a9fa9459Szrj     {
3627*a9fa9459Szrj       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
3628*a9fa9459Szrj       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
3629*a9fa9459Szrj       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
3630*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3631*a9fa9459Szrj       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3632*a9fa9459Szrj 
3633*a9fa9459Szrj       unsigned char op1 = view[-1];
3634*a9fa9459Szrj       unsigned char op2 = view[-2];
3635*a9fa9459Szrj       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3636*a9fa9459Szrj 		     (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3637*a9fa9459Szrj       if (op2 == 0x8b)
3638*a9fa9459Szrj 	{
3639*a9fa9459Szrj 	  // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
3640*a9fa9459Szrj 	  view[-2] = 0xc7;
3641*a9fa9459Szrj 	  view[-1] = 0xc0 | ((op1 >> 3) & 7);
3642*a9fa9459Szrj 	}
3643*a9fa9459Szrj       else if (op2 == 0x2b)
3644*a9fa9459Szrj 	{
3645*a9fa9459Szrj 	  // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
3646*a9fa9459Szrj 	  view[-2] = 0x81;
3647*a9fa9459Szrj 	  view[-1] = 0xe8 | ((op1 >> 3) & 7);
3648*a9fa9459Szrj 	}
3649*a9fa9459Szrj       else if (op2 == 0x03)
3650*a9fa9459Szrj 	{
3651*a9fa9459Szrj 	  // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
3652*a9fa9459Szrj 	  view[-2] = 0x81;
3653*a9fa9459Szrj 	  view[-1] = 0xc0 | ((op1 >> 3) & 7);
3654*a9fa9459Szrj 	}
3655*a9fa9459Szrj       else
3656*a9fa9459Szrj 	tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3657*a9fa9459Szrj     }
3658*a9fa9459Szrj 
3659*a9fa9459Szrj   value = tls_segment->memsz() - value;
3660*a9fa9459Szrj   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3661*a9fa9459Szrj     value = - value;
3662*a9fa9459Szrj 
3663*a9fa9459Szrj   Relocate_functions<32, false>::rel32(view, value);
3664*a9fa9459Szrj }
3665*a9fa9459Szrj 
3666*a9fa9459Szrj // Relocate section data.
3667*a9fa9459Szrj 
3668*a9fa9459Szrj void
relocate_section(const Relocate_info<32,false> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)3669*a9fa9459Szrj Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3670*a9fa9459Szrj 			      unsigned int sh_type,
3671*a9fa9459Szrj 			      const unsigned char* prelocs,
3672*a9fa9459Szrj 			      size_t reloc_count,
3673*a9fa9459Szrj 			      Output_section* output_section,
3674*a9fa9459Szrj 			      bool needs_special_offset_handling,
3675*a9fa9459Szrj 			      unsigned char* view,
3676*a9fa9459Szrj 			      elfcpp::Elf_types<32>::Elf_Addr address,
3677*a9fa9459Szrj 			      section_size_type view_size,
3678*a9fa9459Szrj 			      const Reloc_symbol_changes* reloc_symbol_changes)
3679*a9fa9459Szrj {
3680*a9fa9459Szrj   gold_assert(sh_type == elfcpp::SHT_REL);
3681*a9fa9459Szrj 
3682*a9fa9459Szrj   gold::relocate_section<32, false, Target_i386, Relocate,
3683*a9fa9459Szrj 			 gold::Default_comdat_behavior, Classify_reloc>(
3684*a9fa9459Szrj     relinfo,
3685*a9fa9459Szrj     this,
3686*a9fa9459Szrj     prelocs,
3687*a9fa9459Szrj     reloc_count,
3688*a9fa9459Szrj     output_section,
3689*a9fa9459Szrj     needs_special_offset_handling,
3690*a9fa9459Szrj     view,
3691*a9fa9459Szrj     address,
3692*a9fa9459Szrj     view_size,
3693*a9fa9459Szrj     reloc_symbol_changes);
3694*a9fa9459Szrj }
3695*a9fa9459Szrj 
3696*a9fa9459Szrj // Return the size of a relocation while scanning during a relocatable
3697*a9fa9459Szrj // link.
3698*a9fa9459Szrj 
3699*a9fa9459Szrj unsigned int
get_size_for_reloc(unsigned int r_type,Relobj * object)3700*a9fa9459Szrj Target_i386::Classify_reloc::get_size_for_reloc(
3701*a9fa9459Szrj     unsigned int r_type,
3702*a9fa9459Szrj     Relobj* object)
3703*a9fa9459Szrj {
3704*a9fa9459Szrj   switch (r_type)
3705*a9fa9459Szrj     {
3706*a9fa9459Szrj     case elfcpp::R_386_NONE:
3707*a9fa9459Szrj     case elfcpp::R_386_GNU_VTINHERIT:
3708*a9fa9459Szrj     case elfcpp::R_386_GNU_VTENTRY:
3709*a9fa9459Szrj     case elfcpp::R_386_TLS_GD:            // Global-dynamic
3710*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
3711*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC_CALL:
3712*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
3713*a9fa9459Szrj     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
3714*a9fa9459Szrj     case elfcpp::R_386_TLS_IE:            // Initial-exec
3715*a9fa9459Szrj     case elfcpp::R_386_TLS_IE_32:
3716*a9fa9459Szrj     case elfcpp::R_386_TLS_GOTIE:
3717*a9fa9459Szrj     case elfcpp::R_386_TLS_LE:            // Local-exec
3718*a9fa9459Szrj     case elfcpp::R_386_TLS_LE_32:
3719*a9fa9459Szrj       return 0;
3720*a9fa9459Szrj 
3721*a9fa9459Szrj     case elfcpp::R_386_32:
3722*a9fa9459Szrj     case elfcpp::R_386_PC32:
3723*a9fa9459Szrj     case elfcpp::R_386_GOT32:
3724*a9fa9459Szrj     case elfcpp::R_386_GOT32X:
3725*a9fa9459Szrj     case elfcpp::R_386_PLT32:
3726*a9fa9459Szrj     case elfcpp::R_386_GOTOFF:
3727*a9fa9459Szrj     case elfcpp::R_386_GOTPC:
3728*a9fa9459Szrj      return 4;
3729*a9fa9459Szrj 
3730*a9fa9459Szrj     case elfcpp::R_386_16:
3731*a9fa9459Szrj     case elfcpp::R_386_PC16:
3732*a9fa9459Szrj       return 2;
3733*a9fa9459Szrj 
3734*a9fa9459Szrj     case elfcpp::R_386_8:
3735*a9fa9459Szrj     case elfcpp::R_386_PC8:
3736*a9fa9459Szrj       return 1;
3737*a9fa9459Szrj 
3738*a9fa9459Szrj       // These are relocations which should only be seen by the
3739*a9fa9459Szrj       // dynamic linker, and should never be seen here.
3740*a9fa9459Szrj     case elfcpp::R_386_COPY:
3741*a9fa9459Szrj     case elfcpp::R_386_GLOB_DAT:
3742*a9fa9459Szrj     case elfcpp::R_386_JUMP_SLOT:
3743*a9fa9459Szrj     case elfcpp::R_386_RELATIVE:
3744*a9fa9459Szrj     case elfcpp::R_386_IRELATIVE:
3745*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF:
3746*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPMOD32:
3747*a9fa9459Szrj     case elfcpp::R_386_TLS_DTPOFF32:
3748*a9fa9459Szrj     case elfcpp::R_386_TLS_TPOFF32:
3749*a9fa9459Szrj     case elfcpp::R_386_TLS_DESC:
3750*a9fa9459Szrj       object->error(_("unexpected reloc %u in object file"), r_type);
3751*a9fa9459Szrj       return 0;
3752*a9fa9459Szrj 
3753*a9fa9459Szrj     case elfcpp::R_386_32PLT:
3754*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_32:
3755*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_PUSH:
3756*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_CALL:
3757*a9fa9459Szrj     case elfcpp::R_386_TLS_GD_POP:
3758*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_32:
3759*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_PUSH:
3760*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_CALL:
3761*a9fa9459Szrj     case elfcpp::R_386_TLS_LDM_POP:
3762*a9fa9459Szrj     case elfcpp::R_386_USED_BY_INTEL_200:
3763*a9fa9459Szrj     default:
3764*a9fa9459Szrj       object->error(_("unsupported reloc %u in object file"), r_type);
3765*a9fa9459Szrj       return 0;
3766*a9fa9459Szrj     }
3767*a9fa9459Szrj }
3768*a9fa9459Szrj 
3769*a9fa9459Szrj // Scan the relocs during a relocatable link.
3770*a9fa9459Szrj 
3771*a9fa9459Szrj void
scan_relocatable_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols,Relocatable_relocs * rr)3772*a9fa9459Szrj Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3773*a9fa9459Szrj 				     Layout* layout,
3774*a9fa9459Szrj 				     Sized_relobj_file<32, false>* object,
3775*a9fa9459Szrj 				     unsigned int data_shndx,
3776*a9fa9459Szrj 				     unsigned int sh_type,
3777*a9fa9459Szrj 				     const unsigned char* prelocs,
3778*a9fa9459Szrj 				     size_t reloc_count,
3779*a9fa9459Szrj 				     Output_section* output_section,
3780*a9fa9459Szrj 				     bool needs_special_offset_handling,
3781*a9fa9459Szrj 				     size_t local_symbol_count,
3782*a9fa9459Szrj 				     const unsigned char* plocal_symbols,
3783*a9fa9459Szrj 				     Relocatable_relocs* rr)
3784*a9fa9459Szrj {
3785*a9fa9459Szrj   typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
3786*a9fa9459Szrj       Scan_relocatable_relocs;
3787*a9fa9459Szrj 
3788*a9fa9459Szrj   gold_assert(sh_type == elfcpp::SHT_REL);
3789*a9fa9459Szrj 
3790*a9fa9459Szrj   gold::scan_relocatable_relocs<32, false, Scan_relocatable_relocs>(
3791*a9fa9459Szrj     symtab,
3792*a9fa9459Szrj     layout,
3793*a9fa9459Szrj     object,
3794*a9fa9459Szrj     data_shndx,
3795*a9fa9459Szrj     prelocs,
3796*a9fa9459Szrj     reloc_count,
3797*a9fa9459Szrj     output_section,
3798*a9fa9459Szrj     needs_special_offset_handling,
3799*a9fa9459Szrj     local_symbol_count,
3800*a9fa9459Szrj     plocal_symbols,
3801*a9fa9459Szrj     rr);
3802*a9fa9459Szrj }
3803*a9fa9459Szrj 
3804*a9fa9459Szrj // Scan the relocs for --emit-relocs.
3805*a9fa9459Szrj 
3806*a9fa9459Szrj void
emit_relocs_scan(Symbol_table * symtab,Layout * layout,Sized_relobj_file<32,false> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_syms,Relocatable_relocs * rr)3807*a9fa9459Szrj Target_i386::emit_relocs_scan(Symbol_table* symtab,
3808*a9fa9459Szrj 			      Layout* layout,
3809*a9fa9459Szrj 			      Sized_relobj_file<32, false>* object,
3810*a9fa9459Szrj 			      unsigned int data_shndx,
3811*a9fa9459Szrj 			      unsigned int sh_type,
3812*a9fa9459Szrj 			      const unsigned char* prelocs,
3813*a9fa9459Szrj 			      size_t reloc_count,
3814*a9fa9459Szrj 			      Output_section* output_section,
3815*a9fa9459Szrj 			      bool needs_special_offset_handling,
3816*a9fa9459Szrj 			      size_t local_symbol_count,
3817*a9fa9459Szrj 			      const unsigned char* plocal_syms,
3818*a9fa9459Szrj 			      Relocatable_relocs* rr)
3819*a9fa9459Szrj {
3820*a9fa9459Szrj   typedef gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
3821*a9fa9459Szrj       Classify_reloc;
3822*a9fa9459Szrj   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
3823*a9fa9459Szrj       Emit_relocs_strategy;
3824*a9fa9459Szrj 
3825*a9fa9459Szrj   gold_assert(sh_type == elfcpp::SHT_REL);
3826*a9fa9459Szrj 
3827*a9fa9459Szrj   gold::scan_relocatable_relocs<32, false, Emit_relocs_strategy>(
3828*a9fa9459Szrj     symtab,
3829*a9fa9459Szrj     layout,
3830*a9fa9459Szrj     object,
3831*a9fa9459Szrj     data_shndx,
3832*a9fa9459Szrj     prelocs,
3833*a9fa9459Szrj     reloc_count,
3834*a9fa9459Szrj     output_section,
3835*a9fa9459Szrj     needs_special_offset_handling,
3836*a9fa9459Szrj     local_symbol_count,
3837*a9fa9459Szrj     plocal_syms,
3838*a9fa9459Szrj     rr);
3839*a9fa9459Szrj }
3840*a9fa9459Szrj 
3841*a9fa9459Szrj // Emit relocations for a section.
3842*a9fa9459Szrj 
3843*a9fa9459Szrj void
relocate_relocs(const Relocate_info<32,false> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,unsigned char * view,elfcpp::Elf_types<32>::Elf_Addr view_address,section_size_type view_size,unsigned char * reloc_view,section_size_type reloc_view_size)3844*a9fa9459Szrj Target_i386::relocate_relocs(
3845*a9fa9459Szrj     const Relocate_info<32, false>* relinfo,
3846*a9fa9459Szrj     unsigned int sh_type,
3847*a9fa9459Szrj     const unsigned char* prelocs,
3848*a9fa9459Szrj     size_t reloc_count,
3849*a9fa9459Szrj     Output_section* output_section,
3850*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
3851*a9fa9459Szrj     unsigned char* view,
3852*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr view_address,
3853*a9fa9459Szrj     section_size_type view_size,
3854*a9fa9459Szrj     unsigned char* reloc_view,
3855*a9fa9459Szrj     section_size_type reloc_view_size)
3856*a9fa9459Szrj {
3857*a9fa9459Szrj   gold_assert(sh_type == elfcpp::SHT_REL);
3858*a9fa9459Szrj 
3859*a9fa9459Szrj   gold::relocate_relocs<32, false, Classify_reloc>(
3860*a9fa9459Szrj     relinfo,
3861*a9fa9459Szrj     prelocs,
3862*a9fa9459Szrj     reloc_count,
3863*a9fa9459Szrj     output_section,
3864*a9fa9459Szrj     offset_in_output_section,
3865*a9fa9459Szrj     view,
3866*a9fa9459Szrj     view_address,
3867*a9fa9459Szrj     view_size,
3868*a9fa9459Szrj     reloc_view,
3869*a9fa9459Szrj     reloc_view_size);
3870*a9fa9459Szrj }
3871*a9fa9459Szrj 
3872*a9fa9459Szrj // Return the value to use for a dynamic which requires special
3873*a9fa9459Szrj // treatment.  This is how we support equality comparisons of function
3874*a9fa9459Szrj // pointers across shared library boundaries, as described in the
3875*a9fa9459Szrj // processor specific ABI supplement.
3876*a9fa9459Szrj 
3877*a9fa9459Szrj uint64_t
do_dynsym_value(const Symbol * gsym) const3878*a9fa9459Szrj Target_i386::do_dynsym_value(const Symbol* gsym) const
3879*a9fa9459Szrj {
3880*a9fa9459Szrj   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3881*a9fa9459Szrj   return this->plt_address_for_global(gsym);
3882*a9fa9459Szrj }
3883*a9fa9459Szrj 
3884*a9fa9459Szrj // Return a string used to fill a code section with nops to take up
3885*a9fa9459Szrj // the specified length.
3886*a9fa9459Szrj 
3887*a9fa9459Szrj std::string
do_code_fill(section_size_type length) const3888*a9fa9459Szrj Target_i386::do_code_fill(section_size_type length) const
3889*a9fa9459Szrj {
3890*a9fa9459Szrj   if (length >= 16)
3891*a9fa9459Szrj     {
3892*a9fa9459Szrj       // Build a jmp instruction to skip over the bytes.
3893*a9fa9459Szrj       unsigned char jmp[5];
3894*a9fa9459Szrj       jmp[0] = 0xe9;
3895*a9fa9459Szrj       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3896*a9fa9459Szrj       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3897*a9fa9459Szrj 	      + std::string(length - 5, static_cast<char>(0x90)));
3898*a9fa9459Szrj     }
3899*a9fa9459Szrj 
3900*a9fa9459Szrj   // Nop sequences of various lengths.
3901*a9fa9459Szrj   const char nop1[1] = { '\x90' };                   // nop
3902*a9fa9459Szrj   const char nop2[2] = { '\x66', '\x90' };           // xchg %ax %ax
3903*a9fa9459Szrj   const char nop3[3] = { '\x8d', '\x76', '\x00' };   // leal 0(%esi),%esi
3904*a9fa9459Szrj   const char nop4[4] = { '\x8d', '\x74', '\x26',     // leal 0(%esi,1),%esi
3905*a9fa9459Szrj 			 '\x00'};
3906*a9fa9459Szrj   const char nop5[5] = { '\x90', '\x8d', '\x74',     // nop
3907*a9fa9459Szrj 			 '\x26', '\x00' };           // leal 0(%esi,1),%esi
3908*a9fa9459Szrj   const char nop6[6] = { '\x8d', '\xb6', '\x00',     // leal 0L(%esi),%esi
3909*a9fa9459Szrj 			 '\x00', '\x00', '\x00' };
3910*a9fa9459Szrj   const char nop7[7] = { '\x8d', '\xb4', '\x26',     // leal 0L(%esi,1),%esi
3911*a9fa9459Szrj 			 '\x00', '\x00', '\x00',
3912*a9fa9459Szrj 			 '\x00' };
3913*a9fa9459Szrj   const char nop8[8] = { '\x90', '\x8d', '\xb4',     // nop
3914*a9fa9459Szrj 			 '\x26', '\x00', '\x00',     // leal 0L(%esi,1),%esi
3915*a9fa9459Szrj 			 '\x00', '\x00' };
3916*a9fa9459Szrj   const char nop9[9] = { '\x89', '\xf6', '\x8d',     // movl %esi,%esi
3917*a9fa9459Szrj 			 '\xbc', '\x27', '\x00',     // leal 0L(%edi,1),%edi
3918*a9fa9459Szrj 			 '\x00', '\x00', '\x00' };
3919*a9fa9459Szrj   const char nop10[10] = { '\x8d', '\x76', '\x00',   // leal 0(%esi),%esi
3920*a9fa9459Szrj 			   '\x8d', '\xbc', '\x27',   // leal 0L(%edi,1),%edi
3921*a9fa9459Szrj 			   '\x00', '\x00', '\x00',
3922*a9fa9459Szrj 			   '\x00' };
3923*a9fa9459Szrj   const char nop11[11] = { '\x8d', '\x74', '\x26',   // leal 0(%esi,1),%esi
3924*a9fa9459Szrj 			   '\x00', '\x8d', '\xbc',   // leal 0L(%edi,1),%edi
3925*a9fa9459Szrj 			   '\x27', '\x00', '\x00',
3926*a9fa9459Szrj 			   '\x00', '\x00' };
3927*a9fa9459Szrj   const char nop12[12] = { '\x8d', '\xb6', '\x00',   // leal 0L(%esi),%esi
3928*a9fa9459Szrj 			   '\x00', '\x00', '\x00',   // leal 0L(%edi),%edi
3929*a9fa9459Szrj 			   '\x8d', '\xbf', '\x00',
3930*a9fa9459Szrj 			   '\x00', '\x00', '\x00' };
3931*a9fa9459Szrj   const char nop13[13] = { '\x8d', '\xb6', '\x00',   // leal 0L(%esi),%esi
3932*a9fa9459Szrj 			   '\x00', '\x00', '\x00',   // leal 0L(%edi,1),%edi
3933*a9fa9459Szrj 			   '\x8d', '\xbc', '\x27',
3934*a9fa9459Szrj 			   '\x00', '\x00', '\x00',
3935*a9fa9459Szrj 			   '\x00' };
3936*a9fa9459Szrj   const char nop14[14] = { '\x8d', '\xb4', '\x26',   // leal 0L(%esi,1),%esi
3937*a9fa9459Szrj 			   '\x00', '\x00', '\x00',   // leal 0L(%edi,1),%edi
3938*a9fa9459Szrj 			   '\x00', '\x8d', '\xbc',
3939*a9fa9459Szrj 			   '\x27', '\x00', '\x00',
3940*a9fa9459Szrj 			   '\x00', '\x00' };
3941*a9fa9459Szrj   const char nop15[15] = { '\xeb', '\x0d', '\x90',   // jmp .+15
3942*a9fa9459Szrj 			   '\x90', '\x90', '\x90',   // nop,nop,nop,...
3943*a9fa9459Szrj 			   '\x90', '\x90', '\x90',
3944*a9fa9459Szrj 			   '\x90', '\x90', '\x90',
3945*a9fa9459Szrj 			   '\x90', '\x90', '\x90' };
3946*a9fa9459Szrj 
3947*a9fa9459Szrj   const char* nops[16] = {
3948*a9fa9459Szrj     NULL,
3949*a9fa9459Szrj     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3950*a9fa9459Szrj     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3951*a9fa9459Szrj   };
3952*a9fa9459Szrj 
3953*a9fa9459Szrj   return std::string(nops[length], length);
3954*a9fa9459Szrj }
3955*a9fa9459Szrj 
3956*a9fa9459Szrj // Return the value to use for the base of a DW_EH_PE_datarel offset
3957*a9fa9459Szrj // in an FDE.  Solaris and SVR4 use DW_EH_PE_datarel because their
3958*a9fa9459Szrj // assembler can not write out the difference between two labels in
3959*a9fa9459Szrj // different sections, so instead of using a pc-relative value they
3960*a9fa9459Szrj // use an offset from the GOT.
3961*a9fa9459Szrj 
3962*a9fa9459Szrj uint64_t
do_ehframe_datarel_base() const3963*a9fa9459Szrj Target_i386::do_ehframe_datarel_base() const
3964*a9fa9459Szrj {
3965*a9fa9459Szrj   gold_assert(this->global_offset_table_ != NULL);
3966*a9fa9459Szrj   Symbol* sym = this->global_offset_table_;
3967*a9fa9459Szrj   Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3968*a9fa9459Szrj   return ssym->value();
3969*a9fa9459Szrj }
3970*a9fa9459Szrj 
3971*a9fa9459Szrj // Return whether SYM should be treated as a call to a non-split
3972*a9fa9459Szrj // function.  We don't want that to be true of a call to a
3973*a9fa9459Szrj // get_pc_thunk function.
3974*a9fa9459Szrj 
3975*a9fa9459Szrj bool
do_is_call_to_non_split(const Symbol * sym,const unsigned char *,const unsigned char *,section_size_type) const3976*a9fa9459Szrj Target_i386::do_is_call_to_non_split(const Symbol* sym,
3977*a9fa9459Szrj 				     const unsigned char*,
3978*a9fa9459Szrj 				     const unsigned char*,
3979*a9fa9459Szrj 				     section_size_type) const
3980*a9fa9459Szrj {
3981*a9fa9459Szrj   return (sym->type() == elfcpp::STT_FUNC
3982*a9fa9459Szrj 	  && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3983*a9fa9459Szrj }
3984*a9fa9459Szrj 
3985*a9fa9459Szrj // FNOFFSET in section SHNDX in OBJECT is the start of a function
3986*a9fa9459Szrj // compiled with -fsplit-stack.  The function calls non-split-stack
3987*a9fa9459Szrj // code.  We have to change the function so that it always ensures
3988*a9fa9459Szrj // that it has enough stack space to run some random function.
3989*a9fa9459Szrj 
3990*a9fa9459Szrj void
do_calls_non_split(Relobj * object,unsigned int shndx,section_offset_type fnoffset,section_size_type fnsize,const unsigned char *,size_t,unsigned char * view,section_size_type view_size,std::string * from,std::string * to) const3991*a9fa9459Szrj Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3992*a9fa9459Szrj 				       section_offset_type fnoffset,
3993*a9fa9459Szrj 				       section_size_type fnsize,
3994*a9fa9459Szrj 				       const unsigned char*,
3995*a9fa9459Szrj 				       size_t,
3996*a9fa9459Szrj 				       unsigned char* view,
3997*a9fa9459Szrj 				       section_size_type view_size,
3998*a9fa9459Szrj 				       std::string* from,
3999*a9fa9459Szrj 				       std::string* to) const
4000*a9fa9459Szrj {
4001*a9fa9459Szrj   // The function starts with a comparison of the stack pointer and a
4002*a9fa9459Szrj   // field in the TCB.  This is followed by a jump.
4003*a9fa9459Szrj 
4004*a9fa9459Szrj   // cmp %gs:NN,%esp
4005*a9fa9459Szrj   if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
4006*a9fa9459Szrj       && fnsize > 7)
4007*a9fa9459Szrj     {
4008*a9fa9459Szrj       // We will call __morestack if the carry flag is set after this
4009*a9fa9459Szrj       // comparison.  We turn the comparison into an stc instruction
4010*a9fa9459Szrj       // and some nops.
4011*a9fa9459Szrj       view[fnoffset] = '\xf9';
4012*a9fa9459Szrj       this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
4013*a9fa9459Szrj     }
4014*a9fa9459Szrj   // lea NN(%esp),%ecx
4015*a9fa9459Szrj   // lea NN(%esp),%edx
4016*a9fa9459Szrj   else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
4017*a9fa9459Szrj 	    || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
4018*a9fa9459Szrj 	   && fnsize > 7)
4019*a9fa9459Szrj     {
4020*a9fa9459Szrj       // This is loading an offset from the stack pointer for a
4021*a9fa9459Szrj       // comparison.  The offset is negative, so we decrease the
4022*a9fa9459Szrj       // offset by the amount of space we need for the stack.  This
4023*a9fa9459Szrj       // means we will avoid calling __morestack if there happens to
4024*a9fa9459Szrj       // be plenty of space on the stack already.
4025*a9fa9459Szrj       unsigned char* pval = view + fnoffset + 3;
4026*a9fa9459Szrj       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
4027*a9fa9459Szrj       val -= parameters->options().split_stack_adjust_size();
4028*a9fa9459Szrj       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
4029*a9fa9459Szrj     }
4030*a9fa9459Szrj   else
4031*a9fa9459Szrj     {
4032*a9fa9459Szrj       if (!object->has_no_split_stack())
4033*a9fa9459Szrj 	object->error(_("failed to match split-stack sequence at "
4034*a9fa9459Szrj 			"section %u offset %0zx"),
4035*a9fa9459Szrj 		      shndx, static_cast<size_t>(fnoffset));
4036*a9fa9459Szrj       return;
4037*a9fa9459Szrj     }
4038*a9fa9459Szrj 
4039*a9fa9459Szrj   // We have to change the function so that it calls
4040*a9fa9459Szrj   // __morestack_non_split instead of __morestack.  The former will
4041*a9fa9459Szrj   // allocate additional stack space.
4042*a9fa9459Szrj   *from = "__morestack";
4043*a9fa9459Szrj   *to = "__morestack_non_split";
4044*a9fa9459Szrj }
4045*a9fa9459Szrj 
4046*a9fa9459Szrj // The selector for i386 object files.  Note this is never instantiated
4047*a9fa9459Szrj // directly.  It's only used in Target_selector_i386_nacl, below.
4048*a9fa9459Szrj 
4049*a9fa9459Szrj class Target_selector_i386 : public Target_selector_freebsd
4050*a9fa9459Szrj {
4051*a9fa9459Szrj public:
Target_selector_i386()4052*a9fa9459Szrj   Target_selector_i386()
4053*a9fa9459Szrj     : Target_selector_freebsd(elfcpp::EM_386, 32, false,
4054*a9fa9459Szrj 			      "elf32-i386", "elf32-i386-freebsd",
4055*a9fa9459Szrj 			      "elf_i386")
4056*a9fa9459Szrj   { }
4057*a9fa9459Szrj 
4058*a9fa9459Szrj   Target*
do_instantiate_target()4059*a9fa9459Szrj   do_instantiate_target()
4060*a9fa9459Szrj   { return new Target_i386(); }
4061*a9fa9459Szrj };
4062*a9fa9459Szrj 
4063*a9fa9459Szrj // NaCl variant.  It uses different PLT contents.
4064*a9fa9459Szrj 
4065*a9fa9459Szrj class Output_data_plt_i386_nacl : public Output_data_plt_i386
4066*a9fa9459Szrj {
4067*a9fa9459Szrj  public:
Output_data_plt_i386_nacl(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)4068*a9fa9459Szrj   Output_data_plt_i386_nacl(Layout* layout,
4069*a9fa9459Szrj 			    Output_data_got_plt_i386* got_plt,
4070*a9fa9459Szrj 			    Output_data_space* got_irelative)
4071*a9fa9459Szrj     : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
4072*a9fa9459Szrj   { }
4073*a9fa9459Szrj 
4074*a9fa9459Szrj  protected:
4075*a9fa9459Szrj   virtual unsigned int
do_get_plt_entry_size() const4076*a9fa9459Szrj   do_get_plt_entry_size() const
4077*a9fa9459Szrj   { return plt_entry_size; }
4078*a9fa9459Szrj 
4079*a9fa9459Szrj   virtual void
do_add_eh_frame(Layout * layout)4080*a9fa9459Szrj   do_add_eh_frame(Layout* layout)
4081*a9fa9459Szrj   {
4082*a9fa9459Szrj     layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
4083*a9fa9459Szrj 				 plt_eh_frame_fde, plt_eh_frame_fde_size);
4084*a9fa9459Szrj   }
4085*a9fa9459Szrj 
4086*a9fa9459Szrj   // The size of an entry in the PLT.
4087*a9fa9459Szrj   static const int plt_entry_size = 64;
4088*a9fa9459Szrj 
4089*a9fa9459Szrj   // The .eh_frame unwind information for the PLT.
4090*a9fa9459Szrj   static const int plt_eh_frame_fde_size = 32;
4091*a9fa9459Szrj   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4092*a9fa9459Szrj };
4093*a9fa9459Szrj 
4094*a9fa9459Szrj class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl
4095*a9fa9459Szrj {
4096*a9fa9459Szrj public:
Output_data_plt_i386_nacl_exec(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)4097*a9fa9459Szrj   Output_data_plt_i386_nacl_exec(Layout* layout,
4098*a9fa9459Szrj 				 Output_data_got_plt_i386* got_plt,
4099*a9fa9459Szrj 				 Output_data_space* got_irelative)
4100*a9fa9459Szrj     : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
4101*a9fa9459Szrj   { }
4102*a9fa9459Szrj 
4103*a9fa9459Szrj  protected:
4104*a9fa9459Szrj   virtual void
4105*a9fa9459Szrj   do_fill_first_plt_entry(unsigned char* pov,
4106*a9fa9459Szrj 			  elfcpp::Elf_types<32>::Elf_Addr got_address);
4107*a9fa9459Szrj 
4108*a9fa9459Szrj   virtual unsigned int
4109*a9fa9459Szrj   do_fill_plt_entry(unsigned char* pov,
4110*a9fa9459Szrj 		    elfcpp::Elf_types<32>::Elf_Addr got_address,
4111*a9fa9459Szrj 		    unsigned int got_offset,
4112*a9fa9459Szrj 		    unsigned int plt_offset,
4113*a9fa9459Szrj 		    unsigned int plt_rel_offset);
4114*a9fa9459Szrj 
4115*a9fa9459Szrj  private:
4116*a9fa9459Szrj   // The first entry in the PLT for an executable.
4117*a9fa9459Szrj   static const unsigned char first_plt_entry[plt_entry_size];
4118*a9fa9459Szrj 
4119*a9fa9459Szrj   // Other entries in the PLT for an executable.
4120*a9fa9459Szrj   static const unsigned char plt_entry[plt_entry_size];
4121*a9fa9459Szrj };
4122*a9fa9459Szrj 
4123*a9fa9459Szrj class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl
4124*a9fa9459Szrj {
4125*a9fa9459Szrj  public:
Output_data_plt_i386_nacl_dyn(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative)4126*a9fa9459Szrj   Output_data_plt_i386_nacl_dyn(Layout* layout,
4127*a9fa9459Szrj 				Output_data_got_plt_i386* got_plt,
4128*a9fa9459Szrj 				Output_data_space* got_irelative)
4129*a9fa9459Szrj     : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
4130*a9fa9459Szrj   { }
4131*a9fa9459Szrj 
4132*a9fa9459Szrj  protected:
4133*a9fa9459Szrj   virtual void
4134*a9fa9459Szrj   do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
4135*a9fa9459Szrj 
4136*a9fa9459Szrj   virtual unsigned int
4137*a9fa9459Szrj   do_fill_plt_entry(unsigned char* pov,
4138*a9fa9459Szrj 		    elfcpp::Elf_types<32>::Elf_Addr,
4139*a9fa9459Szrj 		    unsigned int got_offset,
4140*a9fa9459Szrj 		    unsigned int plt_offset,
4141*a9fa9459Szrj 		    unsigned int plt_rel_offset);
4142*a9fa9459Szrj 
4143*a9fa9459Szrj  private:
4144*a9fa9459Szrj   // The first entry in the PLT for a shared object.
4145*a9fa9459Szrj   static const unsigned char first_plt_entry[plt_entry_size];
4146*a9fa9459Szrj 
4147*a9fa9459Szrj   // Other entries in the PLT for a shared object.
4148*a9fa9459Szrj   static const unsigned char plt_entry[plt_entry_size];
4149*a9fa9459Szrj };
4150*a9fa9459Szrj 
4151*a9fa9459Szrj class Target_i386_nacl : public Target_i386
4152*a9fa9459Szrj {
4153*a9fa9459Szrj  public:
Target_i386_nacl()4154*a9fa9459Szrj   Target_i386_nacl()
4155*a9fa9459Szrj     : Target_i386(&i386_nacl_info)
4156*a9fa9459Szrj   { }
4157*a9fa9459Szrj 
4158*a9fa9459Szrj  protected:
4159*a9fa9459Szrj   virtual Output_data_plt_i386*
do_make_data_plt(Layout * layout,Output_data_got_plt_i386 * got_plt,Output_data_space * got_irelative,bool dyn)4160*a9fa9459Szrj   do_make_data_plt(Layout* layout,
4161*a9fa9459Szrj 		   Output_data_got_plt_i386* got_plt,
4162*a9fa9459Szrj 		   Output_data_space* got_irelative,
4163*a9fa9459Szrj 		   bool dyn)
4164*a9fa9459Szrj   {
4165*a9fa9459Szrj     if (dyn)
4166*a9fa9459Szrj       return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative);
4167*a9fa9459Szrj     else
4168*a9fa9459Szrj       return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative);
4169*a9fa9459Szrj   }
4170*a9fa9459Szrj 
4171*a9fa9459Szrj   virtual std::string
4172*a9fa9459Szrj   do_code_fill(section_size_type length) const;
4173*a9fa9459Szrj 
4174*a9fa9459Szrj  private:
4175*a9fa9459Szrj   static const Target::Target_info i386_nacl_info;
4176*a9fa9459Szrj };
4177*a9fa9459Szrj 
4178*a9fa9459Szrj const Target::Target_info Target_i386_nacl::i386_nacl_info =
4179*a9fa9459Szrj {
4180*a9fa9459Szrj   32,			// size
4181*a9fa9459Szrj   false,		// is_big_endian
4182*a9fa9459Szrj   elfcpp::EM_386,	// machine_code
4183*a9fa9459Szrj   false,		// has_make_symbol
4184*a9fa9459Szrj   false,		// has_resolve
4185*a9fa9459Szrj   true,			// has_code_fill
4186*a9fa9459Szrj   true,			// is_default_stack_executable
4187*a9fa9459Szrj   true,			// can_icf_inline_merge_sections
4188*a9fa9459Szrj   '\0',			// wrap_char
4189*a9fa9459Szrj   "/lib/ld-nacl-x86-32.so.1", // dynamic_linker
4190*a9fa9459Szrj   0x20000,		// default_text_segment_address
4191*a9fa9459Szrj   0x10000,		// abi_pagesize (overridable by -z max-page-size)
4192*a9fa9459Szrj   0x10000,		// common_pagesize (overridable by -z common-page-size)
4193*a9fa9459Szrj   true,                 // isolate_execinstr
4194*a9fa9459Szrj   0x10000000,           // rosegment_gap
4195*a9fa9459Szrj   elfcpp::SHN_UNDEF,	// small_common_shndx
4196*a9fa9459Szrj   elfcpp::SHN_UNDEF,	// large_common_shndx
4197*a9fa9459Szrj   0,			// small_common_section_flags
4198*a9fa9459Szrj   0,			// large_common_section_flags
4199*a9fa9459Szrj   NULL,			// attributes_section
4200*a9fa9459Szrj   NULL,			// attributes_vendor
4201*a9fa9459Szrj   "_start",		// entry_symbol_name
4202*a9fa9459Szrj   32,			// hash_entry_size
4203*a9fa9459Szrj };
4204*a9fa9459Szrj 
4205*a9fa9459Szrj #define	NACLMASK	0xe0            // 32-byte alignment mask
4206*a9fa9459Szrj 
4207*a9fa9459Szrj const unsigned char
4208*a9fa9459Szrj Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] =
4209*a9fa9459Szrj {
4210*a9fa9459Szrj   0xff, 0x35,                          // pushl contents of memory address
4211*a9fa9459Szrj   0, 0, 0, 0,                          // replaced with address of .got + 4
4212*a9fa9459Szrj   0x8b, 0x0d,                          // movl contents of address, %ecx
4213*a9fa9459Szrj   0, 0, 0, 0,                          // replaced with address of .got + 8
4214*a9fa9459Szrj   0x83, 0xe1, NACLMASK,                // andl $NACLMASK, %ecx
4215*a9fa9459Szrj   0xff, 0xe1,                          // jmp *%ecx
4216*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4217*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4218*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4219*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4220*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4221*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4222*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4223*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90
4224*a9fa9459Szrj };
4225*a9fa9459Szrj 
4226*a9fa9459Szrj void
do_fill_first_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr got_address)4227*a9fa9459Szrj Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry(
4228*a9fa9459Szrj     unsigned char* pov,
4229*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr got_address)
4230*a9fa9459Szrj {
4231*a9fa9459Szrj   memcpy(pov, first_plt_entry, plt_entry_size);
4232*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
4233*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
4234*a9fa9459Szrj }
4235*a9fa9459Szrj 
4236*a9fa9459Szrj // The first entry in the PLT for a shared object.
4237*a9fa9459Szrj 
4238*a9fa9459Szrj const unsigned char
4239*a9fa9459Szrj Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] =
4240*a9fa9459Szrj {
4241*a9fa9459Szrj   0xff, 0xb3, 4, 0, 0, 0,	// pushl 4(%ebx)
4242*a9fa9459Szrj   0x8b, 0x4b, 0x08,		// mov 0x8(%ebx), %ecx
4243*a9fa9459Szrj   0x83, 0xe1, NACLMASK,         // andl $NACLMASK, %ecx
4244*a9fa9459Szrj   0xff, 0xe1,                   // jmp *%ecx
4245*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4246*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4247*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4248*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4249*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4250*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4251*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4252*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4253*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4254*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90   // nops
4255*a9fa9459Szrj };
4256*a9fa9459Szrj 
4257*a9fa9459Szrj void
do_fill_first_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr)4258*a9fa9459Szrj Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry(
4259*a9fa9459Szrj     unsigned char* pov,
4260*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr)
4261*a9fa9459Szrj {
4262*a9fa9459Szrj   memcpy(pov, first_plt_entry, plt_entry_size);
4263*a9fa9459Szrj }
4264*a9fa9459Szrj 
4265*a9fa9459Szrj // Subsequent entries in the PLT for an executable.
4266*a9fa9459Szrj 
4267*a9fa9459Szrj const unsigned char
4268*a9fa9459Szrj Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] =
4269*a9fa9459Szrj {
4270*a9fa9459Szrj   0x8b, 0x0d,                    // movl contents of address, %ecx */
4271*a9fa9459Szrj   0, 0, 0, 0,                    // replaced with address of symbol in .got
4272*a9fa9459Szrj   0x83, 0xe1, NACLMASK,          // andl $NACLMASK, %ecx
4273*a9fa9459Szrj   0xff, 0xe1,                    // jmp *%ecx
4274*a9fa9459Szrj 
4275*a9fa9459Szrj   // Pad to the next 32-byte boundary with nop instructions.
4276*a9fa9459Szrj   0x90,
4277*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4278*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4279*a9fa9459Szrj 
4280*a9fa9459Szrj   // Lazy GOT entries point here (32-byte aligned).
4281*a9fa9459Szrj   0x68,                       // pushl immediate
4282*a9fa9459Szrj   0, 0, 0, 0,                 // replaced with offset into relocation table
4283*a9fa9459Szrj   0xe9,                       // jmp relative
4284*a9fa9459Szrj   0, 0, 0, 0,                 // replaced with offset to start of .plt
4285*a9fa9459Szrj 
4286*a9fa9459Szrj   // Pad to the next 32-byte boundary with nop instructions.
4287*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4288*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4289*a9fa9459Szrj   0x90, 0x90
4290*a9fa9459Szrj };
4291*a9fa9459Szrj 
4292*a9fa9459Szrj unsigned int
do_fill_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr got_address,unsigned int got_offset,unsigned int plt_offset,unsigned int plt_rel_offset)4293*a9fa9459Szrj Output_data_plt_i386_nacl_exec::do_fill_plt_entry(
4294*a9fa9459Szrj     unsigned char* pov,
4295*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr got_address,
4296*a9fa9459Szrj     unsigned int got_offset,
4297*a9fa9459Szrj     unsigned int plt_offset,
4298*a9fa9459Szrj     unsigned int plt_rel_offset)
4299*a9fa9459Szrj {
4300*a9fa9459Szrj   memcpy(pov, plt_entry, plt_entry_size);
4301*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4302*a9fa9459Szrj 					      got_address + got_offset);
4303*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4304*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4305*a9fa9459Szrj   return 32;
4306*a9fa9459Szrj }
4307*a9fa9459Szrj 
4308*a9fa9459Szrj // Subsequent entries in the PLT for a shared object.
4309*a9fa9459Szrj 
4310*a9fa9459Szrj const unsigned char
4311*a9fa9459Szrj Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] =
4312*a9fa9459Szrj {
4313*a9fa9459Szrj   0x8b, 0x8b,          // movl offset(%ebx), %ecx
4314*a9fa9459Szrj   0, 0, 0, 0,          // replaced with offset of symbol in .got
4315*a9fa9459Szrj   0x83, 0xe1, 0xe0,    // andl $NACLMASK, %ecx
4316*a9fa9459Szrj   0xff, 0xe1,          // jmp *%ecx
4317*a9fa9459Szrj 
4318*a9fa9459Szrj   // Pad to the next 32-byte boundary with nop instructions.
4319*a9fa9459Szrj   0x90,
4320*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4321*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4322*a9fa9459Szrj 
4323*a9fa9459Szrj   // Lazy GOT entries point here (32-byte aligned).
4324*a9fa9459Szrj   0x68,                // pushl immediate
4325*a9fa9459Szrj   0, 0, 0, 0,          // replaced with offset into relocation table.
4326*a9fa9459Szrj   0xe9,                // jmp relative
4327*a9fa9459Szrj   0, 0, 0, 0,          // replaced with offset to start of .plt.
4328*a9fa9459Szrj 
4329*a9fa9459Szrj   // Pad to the next 32-byte boundary with nop instructions.
4330*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4331*a9fa9459Szrj   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4332*a9fa9459Szrj   0x90, 0x90
4333*a9fa9459Szrj };
4334*a9fa9459Szrj 
4335*a9fa9459Szrj unsigned int
do_fill_plt_entry(unsigned char * pov,elfcpp::Elf_types<32>::Elf_Addr,unsigned int got_offset,unsigned int plt_offset,unsigned int plt_rel_offset)4336*a9fa9459Szrj Output_data_plt_i386_nacl_dyn::do_fill_plt_entry(
4337*a9fa9459Szrj     unsigned char* pov,
4338*a9fa9459Szrj     elfcpp::Elf_types<32>::Elf_Addr,
4339*a9fa9459Szrj     unsigned int got_offset,
4340*a9fa9459Szrj     unsigned int plt_offset,
4341*a9fa9459Szrj     unsigned int plt_rel_offset)
4342*a9fa9459Szrj {
4343*a9fa9459Szrj   memcpy(pov, plt_entry, plt_entry_size);
4344*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
4345*a9fa9459Szrj   elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4346*a9fa9459Szrj   elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4347*a9fa9459Szrj   return 32;
4348*a9fa9459Szrj }
4349*a9fa9459Szrj 
4350*a9fa9459Szrj const unsigned char
4351*a9fa9459Szrj Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4352*a9fa9459Szrj {
4353*a9fa9459Szrj   0, 0, 0, 0,				// Replaced with offset to .plt.
4354*a9fa9459Szrj   0, 0, 0, 0,				// Replaced with size of .plt.
4355*a9fa9459Szrj   0,					// Augmentation size.
4356*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa_offset, 8,	// DW_CFA_def_cfa_offset: 8.
4357*a9fa9459Szrj   elfcpp::DW_CFA_advance_loc + 6,	// Advance 6 to __PLT__ + 6.
4358*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa_offset, 12,	// DW_CFA_def_cfa_offset: 12.
4359*a9fa9459Szrj   elfcpp::DW_CFA_advance_loc + 58,	// Advance 58 to __PLT__ + 64.
4360*a9fa9459Szrj   elfcpp::DW_CFA_def_cfa_expression,	// DW_CFA_def_cfa_expression.
4361*a9fa9459Szrj   13,					// Block length.
4362*a9fa9459Szrj   elfcpp::DW_OP_breg4, 4,		// Push %esp + 4.
4363*a9fa9459Szrj   elfcpp::DW_OP_breg8, 0,		// Push %eip.
4364*a9fa9459Szrj   elfcpp::DW_OP_const1u, 63,            // Push 0x3f.
4365*a9fa9459Szrj   elfcpp::DW_OP_and,			// & (%eip & 0x3f).
4366*a9fa9459Szrj   elfcpp::DW_OP_const1u, 37,            // Push 0x25.
4367*a9fa9459Szrj   elfcpp::DW_OP_ge,			// >= ((%eip & 0x3f) >= 0x25)
4368*a9fa9459Szrj   elfcpp::DW_OP_lit2,			// Push 2.
4369*a9fa9459Szrj   elfcpp::DW_OP_shl,			// << (((%eip & 0x3f) >= 0x25) << 2)
4370*a9fa9459Szrj   elfcpp::DW_OP_plus,			// + ((((%eip&0x3f)>=0x25)<<2)+%esp+4
4371*a9fa9459Szrj   elfcpp::DW_CFA_nop,			// Align to 32 bytes.
4372*a9fa9459Szrj   elfcpp::DW_CFA_nop
4373*a9fa9459Szrj };
4374*a9fa9459Szrj 
4375*a9fa9459Szrj // Return a string used to fill a code section with nops.
4376*a9fa9459Szrj // For NaCl, long NOPs are only valid if they do not cross
4377*a9fa9459Szrj // bundle alignment boundaries, so keep it simple with one-byte NOPs.
4378*a9fa9459Szrj std::string
do_code_fill(section_size_type length) const4379*a9fa9459Szrj Target_i386_nacl::do_code_fill(section_size_type length) const
4380*a9fa9459Szrj {
4381*a9fa9459Szrj   return std::string(length, static_cast<char>(0x90));
4382*a9fa9459Szrj }
4383*a9fa9459Szrj 
4384*a9fa9459Szrj // The selector for i386-nacl object files.
4385*a9fa9459Szrj 
4386*a9fa9459Szrj class Target_selector_i386_nacl
4387*a9fa9459Szrj   : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl>
4388*a9fa9459Szrj {
4389*a9fa9459Szrj  public:
Target_selector_i386_nacl()4390*a9fa9459Szrj   Target_selector_i386_nacl()
4391*a9fa9459Szrj     : Target_selector_nacl<Target_selector_i386,
4392*a9fa9459Szrj 			   Target_i386_nacl>("x86-32",
4393*a9fa9459Szrj 					     "elf32-i386-nacl",
4394*a9fa9459Szrj 					     "elf_i386_nacl")
4395*a9fa9459Szrj   { }
4396*a9fa9459Szrj };
4397*a9fa9459Szrj 
4398*a9fa9459Szrj Target_selector_i386_nacl target_selector_i386;
4399*a9fa9459Szrj 
4400*a9fa9459Szrj // IAMCU variant.  It uses EM_IAMCU, not EM_386.
4401*a9fa9459Szrj 
4402*a9fa9459Szrj class Target_iamcu : public Target_i386
4403*a9fa9459Szrj {
4404*a9fa9459Szrj  public:
Target_iamcu()4405*a9fa9459Szrj   Target_iamcu()
4406*a9fa9459Szrj     : Target_i386(&iamcu_info)
4407*a9fa9459Szrj   { }
4408*a9fa9459Szrj 
4409*a9fa9459Szrj  private:
4410*a9fa9459Szrj   // Information about this specific target which we pass to the
4411*a9fa9459Szrj   // general Target structure.
4412*a9fa9459Szrj   static const Target::Target_info iamcu_info;
4413*a9fa9459Szrj };
4414*a9fa9459Szrj 
4415*a9fa9459Szrj const Target::Target_info Target_iamcu::iamcu_info =
4416*a9fa9459Szrj {
4417*a9fa9459Szrj   32,			// size
4418*a9fa9459Szrj   false,		// is_big_endian
4419*a9fa9459Szrj   elfcpp::EM_IAMCU,	// machine_code
4420*a9fa9459Szrj   false,		// has_make_symbol
4421*a9fa9459Szrj   false,		// has_resolve
4422*a9fa9459Szrj   true,			// has_code_fill
4423*a9fa9459Szrj   true,			// is_default_stack_executable
4424*a9fa9459Szrj   true,			// can_icf_inline_merge_sections
4425*a9fa9459Szrj   '\0',			// wrap_char
4426*a9fa9459Szrj   "/usr/lib/libc.so.1",	// dynamic_linker
4427*a9fa9459Szrj   0x08048000,		// default_text_segment_address
4428*a9fa9459Szrj   0x1000,		// abi_pagesize (overridable by -z max-page-size)
4429*a9fa9459Szrj   0x1000,		// common_pagesize (overridable by -z common-page-size)
4430*a9fa9459Szrj   false,                // isolate_execinstr
4431*a9fa9459Szrj   0,                    // rosegment_gap
4432*a9fa9459Szrj   elfcpp::SHN_UNDEF,	// small_common_shndx
4433*a9fa9459Szrj   elfcpp::SHN_UNDEF,	// large_common_shndx
4434*a9fa9459Szrj   0,			// small_common_section_flags
4435*a9fa9459Szrj   0,			// large_common_section_flags
4436*a9fa9459Szrj   NULL,			// attributes_section
4437*a9fa9459Szrj   NULL,			// attributes_vendor
4438*a9fa9459Szrj   "_start",		// entry_symbol_name
4439*a9fa9459Szrj   32,			// hash_entry_size
4440*a9fa9459Szrj };
4441*a9fa9459Szrj 
4442*a9fa9459Szrj class Target_selector_iamcu : public Target_selector
4443*a9fa9459Szrj {
4444*a9fa9459Szrj public:
Target_selector_iamcu()4445*a9fa9459Szrj   Target_selector_iamcu()
4446*a9fa9459Szrj     : Target_selector(elfcpp::EM_IAMCU, 32, false, "elf32-iamcu",
4447*a9fa9459Szrj 		      "elf_iamcu")
4448*a9fa9459Szrj   { }
4449*a9fa9459Szrj 
4450*a9fa9459Szrj   Target*
do_instantiate_target()4451*a9fa9459Szrj   do_instantiate_target()
4452*a9fa9459Szrj   { return new Target_iamcu(); }
4453*a9fa9459Szrj };
4454*a9fa9459Szrj 
4455*a9fa9459Szrj Target_selector_iamcu target_selector_iamcu;
4456*a9fa9459Szrj 
4457*a9fa9459Szrj } // End anonymous namespace.
4458