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