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