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