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