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