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