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