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