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