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