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