From Craig Silverstein: use tls.h in x86_64.cc.
[platform/upstream/binutils.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright 2006, 2007 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 "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39
40 namespace
41 {
42
43 using namespace gold;
44
45 class Output_data_plt_i386;
46
47 // The i386 target class.
48 // TLS info comes from
49 //   http://people.redhat.com/drepper/tls.pdf
50 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
51
52 class Target_i386 : public Sized_target<32, false>
53 {
54  public:
55   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
56
57   Target_i386()
58     : Sized_target<32, false>(&i386_info),
59       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
60       copy_relocs_(NULL), dynbss_(NULL)
61   { }
62
63   // Scan the relocations to look for symbol adjustments.
64   void
65   scan_relocs(const General_options& options,
66               Symbol_table* symtab,
67               Layout* layout,
68               Sized_relobj<32, false>* object,
69               unsigned int data_shndx,
70               unsigned int sh_type,
71               const unsigned char* prelocs,
72               size_t reloc_count,
73               size_t local_symbol_count,
74               const unsigned char* plocal_symbols,
75               Symbol** global_symbols);
76
77   // Finalize the sections.
78   void
79   do_finalize_sections(Layout*);
80
81   // Return the value to use for a dynamic which requires special
82   // treatment.
83   uint64_t
84   do_dynsym_value(const Symbol*) const;
85
86   // Relocate a section.
87   void
88   relocate_section(const Relocate_info<32, false>*,
89                    unsigned int sh_type,
90                    const unsigned char* prelocs,
91                    size_t reloc_count,
92                    unsigned char* view,
93                    elfcpp::Elf_types<32>::Elf_Addr view_address,
94                    off_t view_size);
95
96   // Return a string used to fill a code section with nops.
97   std::string
98   do_code_fill(off_t length);
99
100  private:
101   // The class which scans relocations.
102   struct Scan
103   {
104     inline void
105     local(const General_options& options, Symbol_table* symtab,
106           Layout* layout, Target_i386* target,
107           Sized_relobj<32, false>* object,
108           unsigned int data_shndx,
109           const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
110           const elfcpp::Sym<32, false>& lsym);
111
112     inline void
113     global(const General_options& options, Symbol_table* symtab,
114            Layout* layout, Target_i386* target,
115            Sized_relobj<32, false>* object,
116            unsigned int data_shndx,
117            const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
118            Symbol* gsym);
119
120     static void
121     unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
122
123     static void
124     unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
125                              Symbol*);
126   };
127
128   // The class which implements relocation.
129   class Relocate
130   {
131    public:
132     Relocate()
133       : skip_call_tls_get_addr_(false),
134         local_dynamic_type_(LOCAL_DYNAMIC_NONE)
135     { }
136
137     ~Relocate()
138     {
139       if (this->skip_call_tls_get_addr_)
140         {
141           // FIXME: This needs to specify the location somehow.
142           fprintf(stderr, _("%s: missing expected TLS relocation\n"),
143                   program_name);
144           gold_exit(false);
145         }
146     }
147
148     // Do a relocation.  Return false if the caller should not issue
149     // any warnings about this relocation.
150     inline bool
151     relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
152              const elfcpp::Rel<32, false>&,
153              unsigned int r_type, const Sized_symbol<32>*,
154              const Symbol_value<32>*,
155              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
156              off_t);
157
158    private:
159     // Do a TLS relocation.
160     inline void
161     relocate_tls(const Relocate_info<32, false>*, size_t relnum,
162                  const elfcpp::Rel<32, false>&,
163                  unsigned int r_type, const Sized_symbol<32>*,
164                  const Symbol_value<32>*,
165                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
166
167     // Do a TLS Initial-Exec to Local-Exec transition.
168     static inline void
169     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
170                  Output_segment* tls_segment,
171                  const elfcpp::Rel<32, false>&, unsigned int r_type,
172                  elfcpp::Elf_types<32>::Elf_Addr value,
173                  unsigned char* view,
174                  off_t view_size);
175
176     // Do a TLS General-Dynamic to Local-Exec transition.
177     inline void
178     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
179                  Output_segment* tls_segment,
180                  const elfcpp::Rel<32, false>&, unsigned int r_type,
181                  elfcpp::Elf_types<32>::Elf_Addr value,
182                  unsigned char* view,
183                  off_t view_size);
184
185     // Do a TLS Local-Dynamic to Local-Exec transition.
186     inline void
187     tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
188                  Output_segment* tls_segment,
189                  const elfcpp::Rel<32, false>&, unsigned int r_type,
190                  elfcpp::Elf_types<32>::Elf_Addr value,
191                  unsigned char* view,
192                  off_t view_size);
193
194     // We need to keep track of which type of local dynamic relocation
195     // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
196     enum Local_dynamic_type
197     {
198       LOCAL_DYNAMIC_NONE,
199       LOCAL_DYNAMIC_SUN,
200       LOCAL_DYNAMIC_GNU
201     };
202
203     // This is set if we should skip the next reloc, which should be a
204     // PLT32 reloc against ___tls_get_addr.
205     bool skip_call_tls_get_addr_;
206     // The type of local dynamic relocation we have seen in the section
207     // being relocated, if any.
208     Local_dynamic_type local_dynamic_type_;
209   };
210
211   // Adjust TLS relocation type based on the options and whether this
212   // is a local symbol.
213   static tls::Tls_optimization
214   optimize_tls_reloc(bool is_final, int r_type);
215
216   // Get the GOT section, creating it if necessary.
217   Output_data_got<32, false>*
218   got_section(Symbol_table*, Layout*);
219
220   // Create a PLT entry for a global symbol.
221   void
222   make_plt_entry(Symbol_table*, Layout*, Symbol*);
223
224   // Get the PLT section.
225   const Output_data_plt_i386*
226   plt_section() const
227   {
228     gold_assert(this->plt_ != NULL);
229     return this->plt_;
230   }
231
232   // Get the dynamic reloc section, creating it if necessary.
233   Reloc_section*
234   rel_dyn_section(Layout*);
235
236   // Copy a relocation against a global symbol.
237   void
238   copy_reloc(const General_options*, Symbol_table*, Layout*,
239              Sized_relobj<32, false>*, unsigned int,
240              Symbol*, const elfcpp::Rel<32, false>&);
241
242   // Information about this specific target which we pass to the
243   // general Target structure.
244   static const Target::Target_info i386_info;
245
246   // The GOT section.
247   Output_data_got<32, false>* got_;
248   // The PLT section.
249   Output_data_plt_i386* plt_;
250   // The GOT PLT section.
251   Output_data_space* got_plt_;
252   // The dynamic reloc section.
253   Reloc_section* rel_dyn_;
254   // Relocs saved to avoid a COPY reloc.
255   Copy_relocs<32, false>* copy_relocs_;
256   // Space for variables copied with a COPY reloc.
257   Output_data_space* dynbss_;
258 };
259
260 const Target::Target_info Target_i386::i386_info =
261 {
262   32,                   // size
263   false,                // is_big_endian
264   elfcpp::EM_386,       // machine_code
265   false,                // has_make_symbol
266   false,                // has_resolve
267   true,                 // has_code_fill
268   "/usr/lib/libc.so.1", // dynamic_linker
269   0x08048000,           // text_segment_address
270   0x1000,               // abi_pagesize
271   0x1000                // common_pagesize
272 };
273
274 // Get the GOT section, creating it if necessary.
275
276 Output_data_got<32, false>*
277 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
278 {
279   if (this->got_ == NULL)
280     {
281       gold_assert(symtab != NULL && layout != NULL);
282
283       this->got_ = new Output_data_got<32, false>();
284
285       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
286                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
287                                       this->got_);
288
289       // The old GNU linker creates a .got.plt section.  We just
290       // create another set of data in the .got section.  Note that we
291       // always create a PLT if we create a GOT, although the PLT
292       // might be empty.
293       this->got_plt_ = new Output_data_space(4);
294       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
295                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
296                                       this->got_plt_);
297
298       // The first three entries are reserved.
299       this->got_plt_->set_space_size(3 * 4);
300
301       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
302       symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
303                                     this->got_plt_,
304                                     0, 0, elfcpp::STT_OBJECT,
305                                     elfcpp::STB_LOCAL,
306                                     elfcpp::STV_HIDDEN, 0,
307                                     false, false);
308     }
309
310   return this->got_;
311 }
312
313 // Get the dynamic reloc section, creating it if necessary.
314
315 Target_i386::Reloc_section*
316 Target_i386::rel_dyn_section(Layout* layout)
317 {
318   if (this->rel_dyn_ == NULL)
319     {
320       gold_assert(layout != NULL);
321       this->rel_dyn_ = new Reloc_section();
322       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
323                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
324     }
325   return this->rel_dyn_;
326 }
327
328 // A class to handle the PLT data.
329
330 class Output_data_plt_i386 : public Output_section_data
331 {
332  public:
333   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
334
335   Output_data_plt_i386(Layout*, Output_data_space*);
336
337   // Add an entry to the PLT.
338   void
339   add_entry(Symbol* gsym);
340
341   // Return the .rel.plt section data.
342   const Reloc_section*
343   rel_plt() const
344   { return this->rel_; }
345
346  protected:
347   void
348   do_adjust_output_section(Output_section* os);
349
350  private:
351   // The size of an entry in the PLT.
352   static const int plt_entry_size = 16;
353
354   // The first entry in the PLT for an executable.
355   static unsigned char exec_first_plt_entry[plt_entry_size];
356
357   // The first entry in the PLT for a shared object.
358   static unsigned char dyn_first_plt_entry[plt_entry_size];
359
360   // Other entries in the PLT for an executable.
361   static unsigned char exec_plt_entry[plt_entry_size];
362
363   // Other entries in the PLT for a shared object.
364   static unsigned char dyn_plt_entry[plt_entry_size];
365
366   // Set the final size.
367   void
368   do_set_address(uint64_t, off_t)
369   { this->set_data_size((this->count_ + 1) * plt_entry_size); }
370
371   // Write out the PLT data.
372   void
373   do_write(Output_file*);
374
375   // The reloc section.
376   Reloc_section* rel_;
377   // The .got.plt section.
378   Output_data_space* got_plt_;
379   // The number of PLT entries.
380   unsigned int count_;
381 };
382
383 // Create the PLT section.  The ordinary .got section is an argument,
384 // since we need to refer to the start.  We also create our own .got
385 // section just for PLT entries.
386
387 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
388                                            Output_data_space* got_plt)
389   : Output_section_data(4), got_plt_(got_plt), count_(0)
390 {
391   this->rel_ = new Reloc_section();
392   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
393                                   elfcpp::SHF_ALLOC, this->rel_);
394 }
395
396 void
397 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
398 {
399   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
400   // linker, and so do we.
401   os->set_entsize(4);
402 }
403
404 // Add an entry to the PLT.
405
406 void
407 Output_data_plt_i386::add_entry(Symbol* gsym)
408 {
409   gold_assert(!gsym->has_plt_offset());
410
411   // Note that when setting the PLT offset we skip the initial
412   // reserved PLT entry.
413   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
414
415   ++this->count_;
416
417   off_t got_offset = this->got_plt_->data_size();
418
419   // Every PLT entry needs a GOT entry which points back to the PLT
420   // entry (this will be changed by the dynamic linker, normally
421   // lazily when the function is called).
422   this->got_plt_->set_space_size(got_offset + 4);
423
424   // Every PLT entry needs a reloc.
425   gsym->set_needs_dynsym_entry();
426   this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
427                          got_offset);
428
429   // Note that we don't need to save the symbol.  The contents of the
430   // PLT are independent of which symbols are used.  The symbols only
431   // appear in the relocations.
432 }
433
434 // The first entry in the PLT for an executable.
435
436 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
437 {
438   0xff, 0x35,   // pushl contents of memory address
439   0, 0, 0, 0,   // replaced with address of .got + 4
440   0xff, 0x25,   // jmp indirect
441   0, 0, 0, 0,   // replaced with address of .got + 8
442   0, 0, 0, 0    // unused
443 };
444
445 // The first entry in the PLT for a shared object.
446
447 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
448 {
449   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
450   0xff, 0xa3, 8, 0, 0, 0,       // jmp *8(%ebx)
451   0, 0, 0, 0                    // unused
452 };
453
454 // Subsequent entries in the PLT for an executable.
455
456 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
457 {
458   0xff, 0x25,   // jmp indirect
459   0, 0, 0, 0,   // replaced with address of symbol in .got
460   0x68,         // pushl immediate
461   0, 0, 0, 0,   // replaced with offset into relocation table
462   0xe9,         // jmp relative
463   0, 0, 0, 0    // replaced with offset to start of .plt
464 };
465
466 // Subsequent entries in the PLT for a shared object.
467
468 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
469 {
470   0xff, 0xa3,   // jmp *offset(%ebx)
471   0, 0, 0, 0,   // replaced with offset of symbol in .got
472   0x68,         // pushl immediate
473   0, 0, 0, 0,   // replaced with offset into relocation table
474   0xe9,         // jmp relative
475   0, 0, 0, 0    // replaced with offset to start of .plt
476 };
477
478 // Write out the PLT.  This uses the hand-coded instructions above,
479 // and adjusts them as needed.  This is all specified by the i386 ELF
480 // Processor Supplement.
481
482 void
483 Output_data_plt_i386::do_write(Output_file* of)
484 {
485   const off_t offset = this->offset();
486   const off_t oview_size = this->data_size();
487   unsigned char* const oview = of->get_output_view(offset, oview_size);
488
489   const off_t got_file_offset = this->got_plt_->offset();
490   const off_t got_size = this->got_plt_->data_size();
491   unsigned char* const got_view = of->get_output_view(got_file_offset,
492                                                       got_size);
493
494   unsigned char* pov = oview;
495
496   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
497   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
498
499   if (parameters->output_is_shared())
500     memcpy(pov, dyn_first_plt_entry, plt_entry_size);
501   else
502     {
503       memcpy(pov, exec_first_plt_entry, plt_entry_size);
504       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
505       elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
506     }
507   pov += plt_entry_size;
508
509   unsigned char* got_pov = got_view;
510
511   memset(got_pov, 0, 12);
512   got_pov += 12;
513
514   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
515
516   unsigned int plt_offset = plt_entry_size;
517   unsigned int plt_rel_offset = 0;
518   unsigned int got_offset = 12;
519   const unsigned int count = this->count_;
520   for (unsigned int i = 0;
521        i < count;
522        ++i,
523          pov += plt_entry_size,
524          got_pov += 4,
525          plt_offset += plt_entry_size,
526          plt_rel_offset += rel_size,
527          got_offset += 4)
528     {
529       // Set and adjust the PLT entry itself.
530
531       if (parameters->output_is_shared())
532         {
533           memcpy(pov, dyn_plt_entry, plt_entry_size);
534           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
535         }
536       else
537         {
538           memcpy(pov, exec_plt_entry, plt_entry_size);
539           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
540                                                       (got_address
541                                                        + got_offset));
542         }
543
544       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
545       elfcpp::Swap<32, false>::writeval(pov + 12,
546                                         - (plt_offset + plt_entry_size));
547
548       // Set the entry in the GOT.
549       elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
550     }
551
552   gold_assert(pov - oview == oview_size);
553   gold_assert(got_pov - got_view == got_size);
554
555   of->write_output_view(offset, oview_size, oview);
556   of->write_output_view(got_file_offset, got_size, got_view);
557 }
558
559 // Create a PLT entry for a global symbol.
560
561 void
562 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
563 {
564   if (gsym->has_plt_offset())
565     return;
566
567   if (this->plt_ == NULL)
568     {
569       // Create the GOT sections first.
570       this->got_section(symtab, layout);
571
572       this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
573       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
574                                       (elfcpp::SHF_ALLOC
575                                        | elfcpp::SHF_EXECINSTR),
576                                       this->plt_);
577     }
578
579   this->plt_->add_entry(gsym);
580 }
581
582 // Handle a relocation against a non-function symbol defined in a
583 // dynamic object.  The traditional way to handle this is to generate
584 // a COPY relocation to copy the variable at runtime from the shared
585 // object into the executable's data segment.  However, this is
586 // undesirable in general, as if the size of the object changes in the
587 // dynamic object, the executable will no longer work correctly.  If
588 // this relocation is in a writable section, then we can create a
589 // dynamic reloc and the dynamic linker will resolve it to the correct
590 // address at runtime.  However, we do not want do that if the
591 // relocation is in a read-only section, as it would prevent the
592 // readonly segment from being shared.  And if we have to eventually
593 // generate a COPY reloc, then any dynamic relocations will be
594 // useless.  So this means that if this is a writable section, we need
595 // to save the relocation until we see whether we have to create a
596 // COPY relocation for this symbol for any other relocation.
597
598 void
599 Target_i386::copy_reloc(const General_options* options,
600                         Symbol_table* symtab,
601                         Layout* layout,
602                         Sized_relobj<32, false>* object,
603                         unsigned int data_shndx, Symbol* gsym,
604                         const elfcpp::Rel<32, false>& rel)
605 {
606   Sized_symbol<32>* ssym;
607   ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
608                                                         SELECT_SIZE(32));
609
610   if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
611                                                data_shndx, ssym))
612     {
613       // So far we do not need a COPY reloc.  Save this relocation.
614       // If it turns out that we never need a COPY reloc for this
615       // symbol, then we will emit the relocation.
616       if (this->copy_relocs_ == NULL)
617         this->copy_relocs_ = new Copy_relocs<32, false>();
618       this->copy_relocs_->save(ssym, object, data_shndx, rel);
619     }
620   else
621     {
622       // Allocate space for this symbol in the .bss section.
623
624       elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
625
626       // There is no defined way to determine the required alignment
627       // of the symbol.  We pick the alignment based on the size.  We
628       // set an arbitrary maximum of 256.
629       unsigned int align;
630       for (align = 1; align < 512; align <<= 1)
631         if ((symsize & align) != 0)
632           break;
633
634       if (this->dynbss_ == NULL)
635         {
636           this->dynbss_ = new Output_data_space(align);
637           layout->add_output_section_data(".bss",
638                                           elfcpp::SHT_NOBITS,
639                                           (elfcpp::SHF_ALLOC
640                                            | elfcpp::SHF_WRITE),
641                                           this->dynbss_);
642         }
643
644       Output_data_space* dynbss = this->dynbss_;
645
646       if (align > dynbss->addralign())
647         dynbss->set_space_alignment(align);
648
649       off_t dynbss_size = dynbss->data_size();
650       dynbss_size = align_address(dynbss_size, align);
651       off_t offset = dynbss_size;
652       dynbss->set_space_size(dynbss_size + symsize);
653
654       // Define the symbol in the .dynbss section.
655       symtab->define_in_output_data(this, ssym->name(), ssym->version(),
656                                     dynbss, offset, symsize, ssym->type(),
657                                     ssym->binding(), ssym->visibility(),
658                                     ssym->nonvis(), false, false);
659
660       // Add the COPY reloc.
661       ssym->set_needs_dynsym_entry();
662       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
663       rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
664     }
665 }
666
667 // Optimize the TLS relocation type based on what we know about the
668 // symbol.  IS_FINAL is true if the final address of this symbol is
669 // known at link time.
670
671 tls::Tls_optimization
672 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
673 {
674   // If we are generating a shared library, then we can't do anything
675   // in the linker.
676   if (parameters->output_is_shared())
677     return tls::TLSOPT_NONE;
678
679   switch (r_type)
680     {
681     case elfcpp::R_386_TLS_GD:
682     case elfcpp::R_386_TLS_GOTDESC:
683     case elfcpp::R_386_TLS_DESC_CALL:
684       // These are General-Dynamic which permits fully general TLS
685       // access.  Since we know that we are generating an executable,
686       // we can convert this to Initial-Exec.  If we also know that
687       // this is a local symbol, we can further switch to Local-Exec.
688       if (is_final)
689         return tls::TLSOPT_TO_LE;
690       return tls::TLSOPT_TO_IE;
691
692     case elfcpp::R_386_TLS_LDM:
693       // This is Local-Dynamic, which refers to a local symbol in the
694       // dynamic TLS block.  Since we know that we generating an
695       // executable, we can switch to Local-Exec.
696       return tls::TLSOPT_TO_LE;
697
698     case elfcpp::R_386_TLS_LDO_32:
699       // Another type of Local-Dynamic relocation.
700       return tls::TLSOPT_TO_LE;
701
702     case elfcpp::R_386_TLS_IE:
703     case elfcpp::R_386_TLS_GOTIE:
704     case elfcpp::R_386_TLS_IE_32:
705       // These are Initial-Exec relocs which get the thread offset
706       // from the GOT.  If we know that we are linking against the
707       // local symbol, we can switch to Local-Exec, which links the
708       // thread offset into the instruction.
709       if (is_final)
710         return tls::TLSOPT_TO_LE;
711       return tls::TLSOPT_NONE;
712
713     case elfcpp::R_386_TLS_LE:
714     case elfcpp::R_386_TLS_LE_32:
715       // When we already have Local-Exec, there is nothing further we
716       // can do.
717       return tls::TLSOPT_NONE;
718
719     default:
720       gold_unreachable();
721     }
722 }
723
724 // Report an unsupported relocation against a local symbol.
725
726 void
727 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
728                                            unsigned int r_type)
729 {
730   fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
731           program_name, object->name().c_str(), r_type);
732 }
733
734 // Scan a relocation for a local symbol.
735
736 inline void
737 Target_i386::Scan::local(const General_options&,
738                          Symbol_table* symtab,
739                          Layout* layout,
740                          Target_i386* target,
741                          Sized_relobj<32, false>* object,
742                          unsigned int,
743                          const elfcpp::Rel<32, false>&,
744                          unsigned int r_type,
745                          const elfcpp::Sym<32, false>&)
746 {
747   switch (r_type)
748     {
749     case elfcpp::R_386_NONE:
750     case elfcpp::R_386_GNU_VTINHERIT:
751     case elfcpp::R_386_GNU_VTENTRY:
752       break;
753
754     case elfcpp::R_386_32:
755     case elfcpp::R_386_16:
756     case elfcpp::R_386_8:
757       // FIXME: If we are generating a shared object we need to copy
758       // this relocation into the object.
759       gold_assert(!parameters->output_is_shared());
760       break;
761
762     case elfcpp::R_386_PC32:
763     case elfcpp::R_386_PC16:
764     case elfcpp::R_386_PC8:
765       break;
766
767     case elfcpp::R_386_GOTOFF:
768     case elfcpp::R_386_GOTPC:
769       // We need a GOT section.
770       target->got_section(symtab, layout);
771       break;
772
773       // These are relocations which should only be seen by the
774       // dynamic linker, and should never be seen here.
775     case elfcpp::R_386_COPY:
776     case elfcpp::R_386_GLOB_DAT:
777     case elfcpp::R_386_JUMP_SLOT:
778     case elfcpp::R_386_RELATIVE:
779     case elfcpp::R_386_TLS_TPOFF:
780     case elfcpp::R_386_TLS_DTPMOD32:
781     case elfcpp::R_386_TLS_DTPOFF32:
782     case elfcpp::R_386_TLS_TPOFF32:
783     case elfcpp::R_386_TLS_DESC:
784       fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
785               program_name, object->name().c_str(), r_type);
786       gold_exit(false);
787       break;
788
789       // These are initial TLS relocs, which are expected when
790       // linking.
791     case elfcpp::R_386_TLS_IE:
792     case elfcpp::R_386_TLS_GOTIE:
793     case elfcpp::R_386_TLS_LE:
794     case elfcpp::R_386_TLS_GD:
795     case elfcpp::R_386_TLS_LDM:
796     case elfcpp::R_386_TLS_LDO_32:
797     case elfcpp::R_386_TLS_IE_32:
798     case elfcpp::R_386_TLS_LE_32:
799     case elfcpp::R_386_TLS_GOTDESC:
800     case elfcpp::R_386_TLS_DESC_CALL:
801       {
802         bool output_is_shared = parameters->output_is_shared();
803         const tls::Tls_optimization optimized_type
804             = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
805         switch (r_type)
806           {
807           case elfcpp::R_386_TLS_LE:
808           case elfcpp::R_386_TLS_LE_32:
809             // FIXME: If generating a shared object, we need to copy
810             // this relocation into the object.
811             gold_assert(!output_is_shared);
812             break;
813
814           case elfcpp::R_386_TLS_IE:
815           case elfcpp::R_386_TLS_IE_32:
816           case elfcpp::R_386_TLS_GOTIE:
817             // FIXME: If not relaxing to LE, we need to generate a
818             // TPOFF or TPOFF32 reloc.
819             if (optimized_type != tls::TLSOPT_TO_LE)
820               unsupported_reloc_local(object, r_type);
821             break;
822
823           case elfcpp::R_386_TLS_LDM:
824             // FIXME: If not relaxing to LE, we need to generate a
825             // DTPMOD32 reloc.
826             if (optimized_type != tls::TLSOPT_TO_LE)
827               unsupported_reloc_local(object, r_type);
828             break;
829
830           case elfcpp::R_386_TLS_LDO_32:
831             break;
832
833           case elfcpp::R_386_TLS_GD:
834           case elfcpp::R_386_TLS_GOTDESC:
835           case elfcpp::R_386_TLS_DESC_CALL:
836             // FIXME: If not relaxing to LE, we need to generate
837             // DTPMOD32 and DTPOFF32 relocs.
838             if (optimized_type != tls::TLSOPT_TO_LE)
839               unsupported_reloc_local(object, r_type);
840             break;
841
842           default:
843             gold_unreachable();
844           }
845       }
846       break;
847
848     case elfcpp::R_386_GOT32:
849     case elfcpp::R_386_PLT32:
850     case elfcpp::R_386_32PLT:
851     case elfcpp::R_386_TLS_GD_32:
852     case elfcpp::R_386_TLS_GD_PUSH:
853     case elfcpp::R_386_TLS_GD_CALL:
854     case elfcpp::R_386_TLS_GD_POP:
855     case elfcpp::R_386_TLS_LDM_32:
856     case elfcpp::R_386_TLS_LDM_PUSH:
857     case elfcpp::R_386_TLS_LDM_CALL:
858     case elfcpp::R_386_TLS_LDM_POP:
859     case elfcpp::R_386_USED_BY_INTEL_200:
860     default:
861       unsupported_reloc_local(object, r_type);
862       break;
863     }
864 }
865
866 // Report an unsupported relocation against a global symbol.
867
868 void
869 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
870                                             unsigned int r_type,
871                                             Symbol* gsym)
872 {
873   fprintf(stderr,
874           _("%s: %s: unsupported reloc %u against global symbol %s\n"),
875           program_name, object->name().c_str(), r_type, gsym->name());
876 }
877
878 // Scan a relocation for a global symbol.
879
880 inline void
881 Target_i386::Scan::global(const General_options& options,
882                           Symbol_table* symtab,
883                           Layout* layout,
884                           Target_i386* target,
885                           Sized_relobj<32, false>* object,
886                           unsigned int data_shndx,
887                           const elfcpp::Rel<32, false>& reloc,
888                           unsigned int r_type,
889                           Symbol* gsym)
890 {
891   switch (r_type)
892     {
893     case elfcpp::R_386_NONE:
894     case elfcpp::R_386_GNU_VTINHERIT:
895     case elfcpp::R_386_GNU_VTENTRY:
896       break;
897
898     case elfcpp::R_386_32:
899     case elfcpp::R_386_PC32:
900     case elfcpp::R_386_16:
901     case elfcpp::R_386_PC16:
902     case elfcpp::R_386_8:
903     case elfcpp::R_386_PC8:
904       // FIXME: If we are generating a shared object we may need to
905       // copy this relocation into the object.  If this symbol is
906       // defined in a shared object, we may need to copy this
907       // relocation in order to avoid a COPY relocation.
908       gold_assert(!parameters->output_is_shared());
909
910       if (gsym->is_from_dynobj())
911         {
912           // This symbol is defined in a dynamic object.  If it is a
913           // function, we make a PLT entry.  Otherwise we need to
914           // either generate a COPY reloc or copy this reloc.
915           if (gsym->type() == elfcpp::STT_FUNC)
916             {
917               target->make_plt_entry(symtab, layout, gsym);
918
919               // If this is not a PC relative reference, then we may
920               // be taking the address of the function.  In that case
921               // we need to set the entry in the dynamic symbol table
922               // to the address of the PLT entry.
923               if (r_type != elfcpp::R_386_PC32
924                   && r_type != elfcpp::R_386_PC16
925                   && r_type != elfcpp::R_386_PC8)
926                 gsym->set_needs_dynsym_value();
927             }
928           else
929             target->copy_reloc(&options, symtab, layout, object, data_shndx,
930                                gsym, reloc);
931         }
932
933       break;
934
935     case elfcpp::R_386_GOT32:
936       {
937         // The symbol requires a GOT entry.
938         Output_data_got<32, false>* got = target->got_section(symtab, layout);
939         if (got->add_global(gsym))
940           {
941             // If this symbol is not fully resolved, we need to add a
942             // dynamic relocation for it.
943             if (!gsym->final_value_is_known())
944               {
945                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
946                 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
947                                     gsym->got_offset());
948               }
949           }
950       }
951       break;
952
953     case elfcpp::R_386_PLT32:
954       // If the symbol is fully resolved, this is just a PC32 reloc.
955       // Otherwise we need a PLT entry.
956       if (gsym->final_value_is_known())
957         break;
958       target->make_plt_entry(symtab, layout, gsym);
959       break;
960
961     case elfcpp::R_386_GOTOFF:
962     case elfcpp::R_386_GOTPC:
963       // We need a GOT section.
964       target->got_section(symtab, layout);
965       break;
966
967       // These are relocations which should only be seen by the
968       // dynamic linker, and should never be seen here.
969     case elfcpp::R_386_COPY:
970     case elfcpp::R_386_GLOB_DAT:
971     case elfcpp::R_386_JUMP_SLOT:
972     case elfcpp::R_386_RELATIVE:
973     case elfcpp::R_386_TLS_TPOFF:
974     case elfcpp::R_386_TLS_DTPMOD32:
975     case elfcpp::R_386_TLS_DTPOFF32:
976     case elfcpp::R_386_TLS_TPOFF32:
977     case elfcpp::R_386_TLS_DESC:
978       fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
979               program_name, object->name().c_str(), r_type);
980       gold_exit(false);
981       break;
982
983       // These are initial tls relocs, which are expected when
984       // linking.
985     case elfcpp::R_386_TLS_IE:
986     case elfcpp::R_386_TLS_GOTIE:
987     case elfcpp::R_386_TLS_LE:
988     case elfcpp::R_386_TLS_GD:
989     case elfcpp::R_386_TLS_LDM:
990     case elfcpp::R_386_TLS_LDO_32:
991     case elfcpp::R_386_TLS_IE_32:
992     case elfcpp::R_386_TLS_LE_32:
993     case elfcpp::R_386_TLS_GOTDESC:
994     case elfcpp::R_386_TLS_DESC_CALL:
995       {
996         const bool is_final = gsym->final_value_is_known();
997         const tls::Tls_optimization optimized_type
998             = Target_i386::optimize_tls_reloc(is_final, r_type);
999         switch (r_type)
1000           {
1001           case elfcpp::R_386_TLS_LE:
1002           case elfcpp::R_386_TLS_LE_32:
1003             // FIXME: If generating a shared object, we need to copy
1004             // this relocation into the object.
1005             gold_assert(!parameters->output_is_shared());
1006             break;
1007
1008           case elfcpp::R_386_TLS_IE:
1009           case elfcpp::R_386_TLS_IE_32:
1010           case elfcpp::R_386_TLS_GOTIE:
1011             // FIXME: If not relaxing to LE, we need to generate a
1012             // TPOFF or TPOFF32 reloc.
1013             if (optimized_type != tls::TLSOPT_TO_LE)
1014               unsupported_reloc_global(object, r_type, gsym);
1015             break;
1016
1017           case elfcpp::R_386_TLS_LDM:
1018             // FIXME: If not relaxing to LE, we need to generate a
1019             // DTPMOD32 reloc.
1020             if (optimized_type != tls::TLSOPT_TO_LE)
1021               unsupported_reloc_global(object, r_type, gsym);
1022             break;
1023
1024           case elfcpp::R_386_TLS_LDO_32:
1025             break;
1026
1027           case elfcpp::R_386_TLS_GD:
1028           case elfcpp::R_386_TLS_GOTDESC:
1029           case elfcpp::R_386_TLS_DESC_CALL:
1030             // FIXME: If not relaxing to LE, we need to generate
1031             // DTPMOD32 and DTPOFF32 relocs.
1032             if (optimized_type != tls::TLSOPT_TO_LE)
1033               unsupported_reloc_global(object, r_type, gsym);
1034             break;
1035
1036           default:
1037             gold_unreachable();
1038           }
1039       }
1040       break;
1041
1042     case elfcpp::R_386_32PLT:
1043     case elfcpp::R_386_TLS_GD_32:
1044     case elfcpp::R_386_TLS_GD_PUSH:
1045     case elfcpp::R_386_TLS_GD_CALL:
1046     case elfcpp::R_386_TLS_GD_POP:
1047     case elfcpp::R_386_TLS_LDM_32:
1048     case elfcpp::R_386_TLS_LDM_PUSH:
1049     case elfcpp::R_386_TLS_LDM_CALL:
1050     case elfcpp::R_386_TLS_LDM_POP:
1051     case elfcpp::R_386_USED_BY_INTEL_200:
1052     default:
1053       unsupported_reloc_global(object, r_type, gsym);
1054       break;
1055     }
1056 }
1057
1058 // Scan relocations for a section.
1059
1060 void
1061 Target_i386::scan_relocs(const General_options& options,
1062                          Symbol_table* symtab,
1063                          Layout* layout,
1064                          Sized_relobj<32, false>* object,
1065                          unsigned int data_shndx,
1066                          unsigned int sh_type,
1067                          const unsigned char* prelocs,
1068                          size_t reloc_count,
1069                          size_t local_symbol_count,
1070                          const unsigned char* plocal_symbols,
1071                          Symbol** global_symbols)
1072 {
1073   if (sh_type == elfcpp::SHT_RELA)
1074     {
1075       fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
1076               program_name, object->name().c_str());
1077       gold_exit(false);
1078     }
1079
1080   gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1081                     Target_i386::Scan>(
1082     options,
1083     symtab,
1084     layout,
1085     this,
1086     object,
1087     data_shndx,
1088     prelocs,
1089     reloc_count,
1090     local_symbol_count,
1091     plocal_symbols,
1092     global_symbols);
1093 }
1094
1095 // Finalize the sections.
1096
1097 void
1098 Target_i386::do_finalize_sections(Layout* layout)
1099 {
1100   // Fill in some more dynamic tags.
1101   Output_data_dynamic* const odyn = layout->dynamic_data();
1102   if (odyn != NULL)
1103     {
1104       if (this->got_plt_ != NULL)
1105         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1106
1107       if (this->plt_ != NULL)
1108         {
1109           const Output_data* od = this->plt_->rel_plt();
1110           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1111           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1112           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1113         }
1114
1115       if (this->rel_dyn_ != NULL)
1116         {
1117           const Output_data* od = this->rel_dyn_;
1118           odyn->add_section_address(elfcpp::DT_REL, od);
1119           odyn->add_section_size(elfcpp::DT_RELSZ, od);
1120           odyn->add_constant(elfcpp::DT_RELENT,
1121                              elfcpp::Elf_sizes<32>::rel_size);
1122         }
1123
1124       if (!parameters->output_is_shared())
1125         {
1126           // The value of the DT_DEBUG tag is filled in by the dynamic
1127           // linker at run time, and used by the debugger.
1128           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1129         }
1130     }
1131
1132   // Emit any relocs we saved in an attempt to avoid generating COPY
1133   // relocs.
1134   if (this->copy_relocs_ == NULL)
1135     return;
1136   if (this->copy_relocs_->any_to_emit())
1137     {
1138       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1139       this->copy_relocs_->emit(rel_dyn);
1140     }
1141   delete this->copy_relocs_;
1142   this->copy_relocs_ = NULL;
1143 }
1144
1145 // Perform a relocation.
1146
1147 inline bool
1148 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1149                                 Target_i386* target,
1150                                 size_t relnum,
1151                                 const elfcpp::Rel<32, false>& rel,
1152                                 unsigned int r_type,
1153                                 const Sized_symbol<32>* gsym,
1154                                 const Symbol_value<32>* psymval,
1155                                 unsigned char* view,
1156                                 elfcpp::Elf_types<32>::Elf_Addr address,
1157                                 off_t view_size)
1158 {
1159   if (this->skip_call_tls_get_addr_)
1160     {
1161       if (r_type != elfcpp::R_386_PLT32
1162           || gsym == NULL
1163           || strcmp(gsym->name(), "___tls_get_addr") != 0)
1164         {
1165           fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1166                   program_name,
1167                   relinfo->location(relnum, rel.get_r_offset()).c_str());
1168           gold_exit(false);
1169         }
1170
1171       this->skip_call_tls_get_addr_ = false;
1172
1173       return false;
1174     }
1175
1176   // Pick the value to use for symbols defined in shared objects.
1177   Symbol_value<32> symval;
1178   if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1179     {
1180       symval.set_output_value(target->plt_section()->address()
1181                               + gsym->plt_offset());
1182       psymval = &symval;
1183     }
1184
1185   const Sized_relobj<32, false>* object = relinfo->object;
1186
1187   switch (r_type)
1188     {
1189     case elfcpp::R_386_NONE:
1190     case elfcpp::R_386_GNU_VTINHERIT:
1191     case elfcpp::R_386_GNU_VTENTRY:
1192       break;
1193
1194     case elfcpp::R_386_32:
1195       Relocate_functions<32, false>::rel32(view, object, psymval);
1196       break;
1197
1198     case elfcpp::R_386_PC32:
1199       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1200       break;
1201
1202     case elfcpp::R_386_16:
1203       Relocate_functions<32, false>::rel16(view, object, psymval);
1204       break;
1205
1206     case elfcpp::R_386_PC16:
1207       Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1208       break;
1209
1210     case elfcpp::R_386_8:
1211       Relocate_functions<32, false>::rel8(view, object, psymval);
1212       break;
1213
1214     case elfcpp::R_386_PC8:
1215       Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1216       break;
1217
1218     case elfcpp::R_386_PLT32:
1219       gold_assert(gsym->has_plt_offset()
1220                   || gsym->final_value_is_known());
1221       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1222       break;
1223
1224     case elfcpp::R_386_GOT32:
1225       // Local GOT offsets not yet supported.
1226       gold_assert(gsym);
1227       gold_assert(gsym->has_got_offset());
1228       Relocate_functions<32, false>::rel32(view, gsym->got_offset());
1229       break;
1230
1231     case elfcpp::R_386_GOTOFF:
1232       {
1233         elfcpp::Elf_types<32>::Elf_Addr value;
1234         value = (psymval->value(object, 0)
1235                  - target->got_section(NULL, NULL)->address());
1236         Relocate_functions<32, false>::rel32(view, value);
1237       }
1238       break;
1239
1240     case elfcpp::R_386_GOTPC:
1241       {
1242         elfcpp::Elf_types<32>::Elf_Addr value;
1243         value = target->got_section(NULL, NULL)->address();
1244         Relocate_functions<32, false>::pcrel32(view, value, address);
1245       }
1246       break;
1247
1248     case elfcpp::R_386_COPY:
1249     case elfcpp::R_386_GLOB_DAT:
1250     case elfcpp::R_386_JUMP_SLOT:
1251     case elfcpp::R_386_RELATIVE:
1252       // These are outstanding tls relocs, which are unexpected when
1253       // linking.
1254     case elfcpp::R_386_TLS_TPOFF:
1255     case elfcpp::R_386_TLS_DTPMOD32:
1256     case elfcpp::R_386_TLS_DTPOFF32:
1257     case elfcpp::R_386_TLS_TPOFF32:
1258     case elfcpp::R_386_TLS_DESC:
1259       fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1260               program_name,
1261               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1262               r_type);
1263       gold_exit(false);
1264       break;
1265
1266       // These are initial tls relocs, which are expected when
1267       // linking.
1268     case elfcpp::R_386_TLS_IE:
1269     case elfcpp::R_386_TLS_GOTIE:
1270     case elfcpp::R_386_TLS_LE:
1271     case elfcpp::R_386_TLS_GD:
1272     case elfcpp::R_386_TLS_LDM:
1273     case elfcpp::R_386_TLS_LDO_32:
1274     case elfcpp::R_386_TLS_IE_32:
1275     case elfcpp::R_386_TLS_LE_32:
1276     case elfcpp::R_386_TLS_GOTDESC:
1277     case elfcpp::R_386_TLS_DESC_CALL:
1278       this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1279                          address, view_size);
1280       break;
1281
1282     case elfcpp::R_386_32PLT:
1283     case elfcpp::R_386_TLS_GD_32:
1284     case elfcpp::R_386_TLS_GD_PUSH:
1285     case elfcpp::R_386_TLS_GD_CALL:
1286     case elfcpp::R_386_TLS_GD_POP:
1287     case elfcpp::R_386_TLS_LDM_32:
1288     case elfcpp::R_386_TLS_LDM_PUSH:
1289     case elfcpp::R_386_TLS_LDM_CALL:
1290     case elfcpp::R_386_TLS_LDM_POP:
1291     case elfcpp::R_386_USED_BY_INTEL_200:
1292     default:
1293       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1294               program_name,
1295               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1296               r_type);
1297       gold_exit(false);
1298       break;
1299     }
1300
1301   return true;
1302 }
1303
1304 // Perform a TLS relocation.
1305
1306 inline void
1307 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1308                                     size_t relnum,
1309                                     const elfcpp::Rel<32, false>& rel,
1310                                     unsigned int r_type,
1311                                     const Sized_symbol<32>* gsym,
1312                                     const Symbol_value<32>* psymval,
1313                                     unsigned char* view,
1314                                     elfcpp::Elf_types<32>::Elf_Addr,
1315                                     off_t view_size)
1316 {
1317   Output_segment* tls_segment = relinfo->layout->tls_segment();
1318   if (tls_segment == NULL)
1319     {
1320       fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1321               program_name,
1322               relinfo->location(relnum, rel.get_r_offset()).c_str());
1323       gold_exit(false);
1324     }
1325
1326   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1327
1328   const bool is_final = (gsym == NULL
1329                          ? !parameters->output_is_shared()
1330                          : gsym->final_value_is_known());
1331   const tls::Tls_optimization optimized_type
1332       = Target_i386::optimize_tls_reloc(is_final, r_type);
1333   switch (r_type)
1334     {
1335     case elfcpp::R_386_TLS_LE_32:
1336       value = tls_segment->vaddr() + tls_segment->memsz() - value;
1337       Relocate_functions<32, false>::rel32(view, value);
1338       break;
1339
1340     case elfcpp::R_386_TLS_LE:
1341       value = value - (tls_segment->vaddr() + tls_segment->memsz());
1342       Relocate_functions<32, false>::rel32(view, value);
1343       break;
1344
1345     case elfcpp::R_386_TLS_IE:
1346     case elfcpp::R_386_TLS_GOTIE:
1347     case elfcpp::R_386_TLS_IE_32:
1348       if (optimized_type == tls::TLSOPT_TO_LE)
1349         {
1350           Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1351                                               rel, r_type, value, view,
1352                                               view_size);
1353           break;
1354         }
1355       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1356               program_name,
1357               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1358               r_type);
1359       gold_exit(false);
1360       break;
1361
1362     case elfcpp::R_386_TLS_GD:
1363       if (optimized_type == tls::TLSOPT_TO_LE)
1364         {
1365           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1366                              rel, r_type, value, view,
1367                              view_size);
1368           break;
1369         }
1370       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1371               program_name,
1372               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1373               r_type);
1374       gold_exit(false);
1375       break;
1376
1377     case elfcpp::R_386_TLS_LDM:
1378       if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1379         {
1380           fprintf(stderr,
1381                   _("%s: %s: both SUN and GNU model TLS relocations\n"),
1382                   program_name,
1383                   relinfo->location(relnum, rel.get_r_offset()).c_str());
1384           gold_exit(false);
1385         }
1386       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1387       if (optimized_type == tls::TLSOPT_TO_LE)
1388         {
1389           this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1390                              value, view, view_size);
1391           break;
1392         }
1393       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1394               program_name,
1395               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1396               r_type);
1397       gold_exit(false);
1398       break;
1399
1400     case elfcpp::R_386_TLS_LDO_32:
1401       // This reloc can appear in debugging sections, in which case we
1402       // won't see the TLS_LDM reloc.  The local_dynamic_type field
1403       // tells us this.
1404       if (optimized_type != tls::TLSOPT_TO_LE
1405           || this->local_dynamic_type_ == LOCAL_DYNAMIC_NONE)
1406         value = value - tls_segment->vaddr();
1407       else if (this->local_dynamic_type_ == LOCAL_DYNAMIC_GNU)
1408         value = value - (tls_segment->vaddr() + tls_segment->memsz());
1409       else
1410         value = tls_segment->vaddr() + tls_segment->memsz() - value;
1411       Relocate_functions<32, false>::rel32(view, value);
1412       break;
1413
1414     case elfcpp::R_386_TLS_GOTDESC:
1415     case elfcpp::R_386_TLS_DESC_CALL:
1416       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1417               program_name,
1418               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1419               r_type);
1420       gold_exit(false);
1421       break;
1422     }
1423 }
1424
1425 // Do a relocation in which we convert a TLS Initial-Exec to a
1426 // Local-Exec.
1427
1428 inline void
1429 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1430                                     size_t relnum,
1431                                     Output_segment* tls_segment,
1432                                     const elfcpp::Rel<32, false>& rel,
1433                                     unsigned int r_type,
1434                                     elfcpp::Elf_types<32>::Elf_Addr value,
1435                                     unsigned char* view,
1436                                     off_t view_size)
1437 {
1438   // We have to actually change the instructions, which means that we
1439   // need to examine the opcodes to figure out which instruction we
1440   // are looking at.
1441   if (r_type == elfcpp::R_386_TLS_IE)
1442     {
1443       // movl %gs:XX,%eax  ==>  movl $YY,%eax
1444       // movl %gs:XX,%reg  ==>  movl $YY,%reg
1445       // addl %gs:XX,%reg  ==>  addl $YY,%reg
1446       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
1447       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1448
1449       unsigned char op1 = view[-1];
1450       if (op1 == 0xa1)
1451         {
1452           // movl XX,%eax  ==>  movl $YY,%eax
1453           view[-1] = 0xb8;
1454         }
1455       else
1456         {
1457           tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1458
1459           unsigned char op2 = view[-2];
1460           if (op2 == 0x8b)
1461             {
1462               // movl XX,%reg  ==>  movl $YY,%reg
1463               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1464                              (op1 & 0xc7) == 0x05);
1465               view[-2] = 0xc7;
1466               view[-1] = 0xc0 | ((op1 >> 3) & 7);
1467             }
1468           else if (op2 == 0x03)
1469             {
1470               // addl XX,%reg  ==>  addl $YY,%reg
1471               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1472                              (op1 & 0xc7) == 0x05);
1473               view[-2] = 0x81;
1474               view[-1] = 0xc0 | ((op1 >> 3) & 7);
1475             }
1476           else
1477             tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1478         }
1479     }
1480   else
1481     {
1482       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
1483       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
1484       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
1485       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1486       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1487
1488       unsigned char op1 = view[-1];
1489       unsigned char op2 = view[-2];
1490       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1491                      (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1492       if (op2 == 0x8b)
1493         {
1494           // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
1495           view[-2] = 0xc7;
1496           view[-1] = 0xc0 | ((op1 >> 3) & 7);
1497         }
1498       else if (op2 == 0x2b)
1499         {
1500           // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
1501           view[-2] = 0x81;
1502           view[-1] = 0xe8 | ((op1 >> 3) & 7);
1503         }
1504       else if (op2 == 0x03)
1505         {
1506           // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
1507           view[-2] = 0x81;
1508           view[-1] = 0xc0 | ((op1 >> 3) & 7);
1509         }
1510       else
1511         tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1512     }
1513
1514   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1515   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1516     value = - value;
1517
1518   Relocate_functions<32, false>::rel32(view, value);
1519 }
1520
1521 // Do a relocation in which we convert a TLS General-Dynamic to a
1522 // Local-Exec.
1523
1524 inline void
1525 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1526                                     size_t relnum,
1527                                     Output_segment* tls_segment,
1528                                     const elfcpp::Rel<32, false>& rel,
1529                                     unsigned int,
1530                                     elfcpp::Elf_types<32>::Elf_Addr value,
1531                                     unsigned char* view,
1532                                     off_t view_size)
1533 {
1534   // leal foo(,%reg,1),%eax; call ___tls_get_addr
1535   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1536   // leal foo(%reg),%eax; call ___tls_get_addr
1537   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1538
1539   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1540   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1541
1542   unsigned char op1 = view[-1];
1543   unsigned char op2 = view[-2];
1544
1545   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1546                  op2 == 0x8d || op2 == 0x04);
1547   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1548
1549   int roff = 5;
1550
1551   if (op2 == 0x04)
1552     {
1553       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1554       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1555       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1556                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1557       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1558     }
1559   else
1560     {
1561       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1562                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1563       if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1564           && view[9] == 0x90)
1565         {
1566           // There is a trailing nop.  Use the size byte subl.
1567           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1568           roff = 6;
1569         }
1570       else
1571         {
1572           // Use the five byte subl.
1573           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1574         }
1575     }
1576
1577   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1578   Relocate_functions<32, false>::rel32(view + roff, value);
1579
1580   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1581   // We can skip it.
1582   this->skip_call_tls_get_addr_ = true;
1583 }
1584
1585 // Do a relocation in which we convert a TLS Local-Dynamic to a
1586 // Local-Exec.
1587
1588 inline void
1589 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
1590                                     size_t relnum,
1591                                     Output_segment*,
1592                                     const elfcpp::Rel<32, false>& rel,
1593                                     unsigned int,
1594                                     elfcpp::Elf_types<32>::Elf_Addr,
1595                                     unsigned char* view,
1596                                     off_t view_size)
1597 {
1598   // leal foo(%reg), %eax; call ___tls_get_addr
1599   // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1600
1601   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1602   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1603
1604   // FIXME: Does this test really always pass?
1605   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1606                  view[-2] == 0x8d && view[-1] == 0x83);
1607
1608   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1609
1610   memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1611
1612   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1613   // We can skip it.
1614   this->skip_call_tls_get_addr_ = true;
1615 }
1616
1617 // Relocate section data.
1618
1619 void
1620 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1621                               unsigned int sh_type,
1622                               const unsigned char* prelocs,
1623                               size_t reloc_count,
1624                               unsigned char* view,
1625                               elfcpp::Elf_types<32>::Elf_Addr address,
1626                               off_t view_size)
1627 {
1628   gold_assert(sh_type == elfcpp::SHT_REL);
1629
1630   gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1631                          Target_i386::Relocate>(
1632     relinfo,
1633     this,
1634     prelocs,
1635     reloc_count,
1636     view,
1637     address,
1638     view_size);
1639 }
1640
1641 // Return the value to use for a dynamic which requires special
1642 // treatment.  This is how we support equality comparisons of function
1643 // pointers across shared library boundaries, as described in the
1644 // processor specific ABI supplement.
1645
1646 uint64_t
1647 Target_i386::do_dynsym_value(const Symbol* gsym) const
1648 {
1649   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1650   return this->plt_section()->address() + gsym->plt_offset();
1651 }
1652
1653 // Return a string used to fill a code section with nops to take up
1654 // the specified length.
1655
1656 std::string
1657 Target_i386::do_code_fill(off_t length)
1658 {
1659   if (length >= 16)
1660     {
1661       // Build a jmp instruction to skip over the bytes.
1662       unsigned char jmp[5];
1663       jmp[0] = 0xe9;
1664       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
1665       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1666               + std::string(length - 5, '\0'));
1667     }
1668
1669   // Nop sequences of various lengths.
1670   const char nop1[1] = { 0x90 };                   // nop
1671   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
1672   const char nop3[3] = { 0x8d, 0x76, 0x00 };       // leal 0(%esi),%esi
1673   const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00};  // leal 0(%esi,1),%esi
1674   const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26,   // nop
1675                          0x00 };                   // leal 0(%esi,1),%esi
1676   const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00,   // leal 0L(%esi),%esi
1677                          0x00, 0x00 };
1678   const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00,   // leal 0L(%esi,1),%esi
1679                          0x00, 0x00, 0x00 };
1680   const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26,   // nop
1681                          0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1682   const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc,   // movl %esi,%esi
1683                          0x27, 0x00, 0x00, 0x00,   // leal 0L(%edi,1),%edi
1684                          0x00 };
1685   const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1686                            0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1687                            0x00, 0x00 };
1688   const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1689                            0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1690                            0x00, 0x00, 0x00 };
1691   const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1692                            0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1693                            0x00, 0x00, 0x00, 0x00 };
1694   const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1695                            0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1696                            0x27, 0x00, 0x00, 0x00,
1697                            0x00 };
1698   const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1699                            0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1700                            0xbc, 0x27, 0x00, 0x00,
1701                            0x00, 0x00 };
1702   const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1703                            0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1704                            0x90, 0x90, 0x90, 0x90,
1705                            0x90, 0x90, 0x90 };
1706
1707   const char* nops[16] = {
1708     NULL,
1709     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1710     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1711   };
1712
1713   return std::string(nops[length], length);
1714 }
1715
1716 // The selector for i386 object files.
1717
1718 class Target_selector_i386 : public Target_selector
1719 {
1720 public:
1721   Target_selector_i386()
1722     : Target_selector(elfcpp::EM_386, 32, false)
1723   { }
1724
1725   Target*
1726   recognize(int machine, int osabi, int abiversion);
1727
1728  private:
1729   Target_i386* target_;
1730 };
1731
1732 // Recognize an i386 object file when we already know that the machine
1733 // number is EM_386.
1734
1735 Target*
1736 Target_selector_i386::recognize(int, int, int)
1737 {
1738   if (this->target_ == NULL)
1739     this->target_ = new Target_i386();
1740   return this->target_;
1741 }
1742
1743 Target_selector_i386 target_selector_i386;
1744
1745 } // End anonymous namespace.