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