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