* options.h (class General_options): Define --wrap as a special
[platform/upstream/binutils.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright 2006, 2007, 2008 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), got_mod_index_offset_(-1U)
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                    section_size_type view_size);
98
99   // Scan the relocs during a relocatable link.
100   void
101   scan_relocatable_relocs(const General_options& options,
102                           Symbol_table* symtab,
103                           Layout* layout,
104                           Sized_relobj<32, false>* object,
105                           unsigned int data_shndx,
106                           unsigned int sh_type,
107                           const unsigned char* prelocs,
108                           size_t reloc_count,
109                           Output_section* output_section,
110                           bool needs_special_offset_handling,
111                           size_t local_symbol_count,
112                           const unsigned char* plocal_symbols,
113                           Relocatable_relocs*);
114
115   // Relocate a section during a relocatable link.
116   void
117   relocate_for_relocatable(const Relocate_info<32, false>*,
118                            unsigned int sh_type,
119                            const unsigned char* prelocs,
120                            size_t reloc_count,
121                            Output_section* output_section,
122                            off_t offset_in_output_section,
123                            const Relocatable_relocs*,
124                            unsigned char* view,
125                            elfcpp::Elf_types<32>::Elf_Addr view_address,
126                            section_size_type view_size,
127                            unsigned char* reloc_view,
128                            section_size_type reloc_view_size);
129
130   // Return a string used to fill a code section with nops.
131   std::string
132   do_code_fill(section_size_type length) const;
133
134   // Return whether SYM is defined by the ABI.
135   bool
136   do_is_defined_by_abi(Symbol* sym) const
137   { return strcmp(sym->name(), "___tls_get_addr") == 0; }
138
139   // Return the size of the GOT section.
140   section_size_type
141   got_size()
142   {
143     gold_assert(this->got_ != NULL);
144     return this->got_->data_size();
145   }
146
147  private:
148   // The class which scans relocations.
149   struct Scan
150   {
151     inline void
152     local(const General_options& options, Symbol_table* symtab,
153           Layout* layout, Target_i386* target,
154           Sized_relobj<32, false>* object,
155           unsigned int data_shndx,
156           Output_section* output_section,
157           const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
158           const elfcpp::Sym<32, false>& lsym);
159
160     inline void
161     global(const General_options& options, Symbol_table* symtab,
162            Layout* layout, Target_i386* target,
163            Sized_relobj<32, false>* object,
164            unsigned int data_shndx,
165            Output_section* output_section,
166            const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
167            Symbol* gsym);
168
169     static void
170     unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
171
172     static void
173     unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
174                              Symbol*);
175   };
176
177   // The class which implements relocation.
178   class Relocate
179   {
180    public:
181     Relocate()
182       : skip_call_tls_get_addr_(false),
183         local_dynamic_type_(LOCAL_DYNAMIC_NONE)
184     { }
185
186     ~Relocate()
187     {
188       if (this->skip_call_tls_get_addr_)
189         {
190           // FIXME: This needs to specify the location somehow.
191           gold_error(_("missing expected TLS relocation"));
192         }
193     }
194
195     // Return whether the static relocation needs to be applied.
196     inline bool
197     should_apply_static_reloc(const Sized_symbol<32>* gsym,
198                               int ref_flags,
199                               bool is_32bit);
200
201     // Do a relocation.  Return false if the caller should not issue
202     // any warnings about this relocation.
203     inline bool
204     relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
205              const elfcpp::Rel<32, false>&,
206              unsigned int r_type, const Sized_symbol<32>*,
207              const Symbol_value<32>*,
208              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
209              section_size_type);
210
211    private:
212     // Do a TLS relocation.
213     inline void
214     relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
215                  size_t relnum, const elfcpp::Rel<32, false>&,
216                  unsigned int r_type, const Sized_symbol<32>*,
217                  const Symbol_value<32>*,
218                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
219                  section_size_type);
220
221     // Do a TLS General-Dynamic to Initial-Exec transition.
222     inline void
223     tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
224                  Output_segment* tls_segment,
225                  const elfcpp::Rel<32, false>&, unsigned int r_type,
226                  elfcpp::Elf_types<32>::Elf_Addr value,
227                  unsigned char* view,
228                  section_size_type view_size);
229
230     // Do a TLS General-Dynamic to Local-Exec transition.
231     inline void
232     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
233                  Output_segment* tls_segment,
234                  const elfcpp::Rel<32, false>&, unsigned int r_type,
235                  elfcpp::Elf_types<32>::Elf_Addr value,
236                  unsigned char* view,
237                  section_size_type view_size);
238
239     // Do a TLS Local-Dynamic to Local-Exec transition.
240     inline void
241     tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
242                  Output_segment* tls_segment,
243                  const elfcpp::Rel<32, false>&, unsigned int r_type,
244                  elfcpp::Elf_types<32>::Elf_Addr value,
245                  unsigned char* view,
246                  section_size_type view_size);
247
248     // Do a TLS Initial-Exec to Local-Exec transition.
249     static inline void
250     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
251                  Output_segment* tls_segment,
252                  const elfcpp::Rel<32, false>&, unsigned int r_type,
253                  elfcpp::Elf_types<32>::Elf_Addr value,
254                  unsigned char* view,
255                  section_size_type view_size);
256
257     // We need to keep track of which type of local dynamic relocation
258     // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
259     enum Local_dynamic_type
260     {
261       LOCAL_DYNAMIC_NONE,
262       LOCAL_DYNAMIC_SUN,
263       LOCAL_DYNAMIC_GNU
264     };
265
266     // This is set if we should skip the next reloc, which should be a
267     // PLT32 reloc against ___tls_get_addr.
268     bool skip_call_tls_get_addr_;
269     // The type of local dynamic relocation we have seen in the section
270     // being relocated, if any.
271     Local_dynamic_type local_dynamic_type_;
272   };
273
274   // A class which returns the size required for a relocation type,
275   // used while scanning relocs during a relocatable link.
276   class Relocatable_size_for_reloc
277   {
278    public:
279     unsigned int
280     get_size_for_reloc(unsigned int, Relobj*);
281   };
282
283   // Adjust TLS relocation type based on the options and whether this
284   // is a local symbol.
285   static tls::Tls_optimization
286   optimize_tls_reloc(bool is_final, int r_type);
287
288   // Get the GOT section, creating it if necessary.
289   Output_data_got<32, false>*
290   got_section(Symbol_table*, Layout*);
291
292   // Get the GOT PLT section.
293   Output_data_space*
294   got_plt_section() const
295   {
296     gold_assert(this->got_plt_ != NULL);
297     return this->got_plt_;
298   }
299
300   // Create a PLT entry for a global symbol.
301   void
302   make_plt_entry(Symbol_table*, Layout*, Symbol*);
303
304   // Create a GOT entry for the TLS module index.
305   unsigned int
306   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
307                       Sized_relobj<32, false>* object);
308
309   // Get the PLT section.
310   const Output_data_plt_i386*
311   plt_section() const
312   {
313     gold_assert(this->plt_ != NULL);
314     return this->plt_;
315   }
316
317   // Get the dynamic reloc section, creating it if necessary.
318   Reloc_section*
319   rel_dyn_section(Layout*);
320
321   // Return true if the symbol may need a COPY relocation.
322   // References from an executable object to non-function symbols
323   // defined in a dynamic object may need a COPY relocation.
324   bool
325   may_need_copy_reloc(Symbol* gsym)
326   {
327     return (!parameters->options().shared()
328             && gsym->is_from_dynobj()
329             && gsym->type() != elfcpp::STT_FUNC);
330   }
331
332   // Copy a relocation against a global symbol.
333   void
334   copy_reloc(const General_options*, Symbol_table*, Layout*,
335              Sized_relobj<32, false>*, unsigned int,
336              Output_section*, Symbol*, const elfcpp::Rel<32, false>&);
337
338   // Information about this specific target which we pass to the
339   // general Target structure.
340   static const Target::Target_info i386_info;
341
342   // The types of GOT entries needed for this platform.
343   enum Got_type
344   {
345     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
346     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
347     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
348     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
349   };
350
351   // The GOT section.
352   Output_data_got<32, false>* got_;
353   // The PLT section.
354   Output_data_plt_i386* plt_;
355   // The GOT PLT section.
356   Output_data_space* got_plt_;
357   // The dynamic reloc section.
358   Reloc_section* rel_dyn_;
359   // Relocs saved to avoid a COPY reloc.
360   Copy_relocs<32, false>* copy_relocs_;
361   // Space for variables copied with a COPY reloc.
362   Output_data_space* dynbss_;
363   // Offset of the GOT entry for the TLS module index;
364   unsigned int got_mod_index_offset_;
365 };
366
367 const Target::Target_info Target_i386::i386_info =
368 {
369   32,                   // size
370   false,                // is_big_endian
371   elfcpp::EM_386,       // machine_code
372   false,                // has_make_symbol
373   false,                // has_resolve
374   true,                 // has_code_fill
375   true,                 // is_default_stack_executable
376   '\0',                 // wrap_char
377   "/usr/lib/libc.so.1", // dynamic_linker
378   0x08048000,           // default_text_segment_address
379   0x1000,               // abi_pagesize (overridable by -z max-page-size)
380   0x1000                // common_pagesize (overridable by -z common-page-size)
381 };
382
383 // Get the GOT section, creating it if necessary.
384
385 Output_data_got<32, false>*
386 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
387 {
388   if (this->got_ == NULL)
389     {
390       gold_assert(symtab != NULL && layout != NULL);
391
392       this->got_ = new Output_data_got<32, false>();
393
394       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
395                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
396                                       this->got_);
397
398       // The old GNU linker creates a .got.plt section.  We just
399       // create another set of data in the .got section.  Note that we
400       // always create a PLT if we create a GOT, although the PLT
401       // might be empty.
402       this->got_plt_ = new Output_data_space(4);
403       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
404                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
405                                       this->got_plt_);
406
407       // The first three entries are reserved.
408       this->got_plt_->set_current_data_size(3 * 4);
409
410       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
411       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
412                                     this->got_plt_,
413                                     0, 0, elfcpp::STT_OBJECT,
414                                     elfcpp::STB_LOCAL,
415                                     elfcpp::STV_HIDDEN, 0,
416                                     false, false);
417     }
418
419   return this->got_;
420 }
421
422 // Get the dynamic reloc section, creating it if necessary.
423
424 Target_i386::Reloc_section*
425 Target_i386::rel_dyn_section(Layout* layout)
426 {
427   if (this->rel_dyn_ == NULL)
428     {
429       gold_assert(layout != NULL);
430       this->rel_dyn_ = new Reloc_section();
431       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
432                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
433     }
434   return this->rel_dyn_;
435 }
436
437 // A class to handle the PLT data.
438
439 class Output_data_plt_i386 : public Output_section_data
440 {
441  public:
442   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
443
444   Output_data_plt_i386(Layout*, Output_data_space*);
445
446   // Add an entry to the PLT.
447   void
448   add_entry(Symbol* gsym);
449
450   // Return the .rel.plt section data.
451   const Reloc_section*
452   rel_plt() const
453   { return this->rel_; }
454
455  protected:
456   void
457   do_adjust_output_section(Output_section* os);
458
459  private:
460   // The size of an entry in the PLT.
461   static const int plt_entry_size = 16;
462
463   // The first entry in the PLT for an executable.
464   static unsigned char exec_first_plt_entry[plt_entry_size];
465
466   // The first entry in the PLT for a shared object.
467   static unsigned char dyn_first_plt_entry[plt_entry_size];
468
469   // Other entries in the PLT for an executable.
470   static unsigned char exec_plt_entry[plt_entry_size];
471
472   // Other entries in the PLT for a shared object.
473   static unsigned char dyn_plt_entry[plt_entry_size];
474
475   // Set the final size.
476   void
477   set_final_data_size()
478   { this->set_data_size((this->count_ + 1) * plt_entry_size); }
479
480   // Write out the PLT data.
481   void
482   do_write(Output_file*);
483
484   // The reloc section.
485   Reloc_section* rel_;
486   // The .got.plt section.
487   Output_data_space* got_plt_;
488   // The number of PLT entries.
489   unsigned int count_;
490 };
491
492 // Create the PLT section.  The ordinary .got section is an argument,
493 // since we need to refer to the start.  We also create our own .got
494 // section just for PLT entries.
495
496 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
497                                            Output_data_space* got_plt)
498   : Output_section_data(4), got_plt_(got_plt), count_(0)
499 {
500   this->rel_ = new Reloc_section();
501   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
502                                   elfcpp::SHF_ALLOC, this->rel_);
503 }
504
505 void
506 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
507 {
508   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
509   // linker, and so do we.
510   os->set_entsize(4);
511 }
512
513 // Add an entry to the PLT.
514
515 void
516 Output_data_plt_i386::add_entry(Symbol* gsym)
517 {
518   gold_assert(!gsym->has_plt_offset());
519
520   // Note that when setting the PLT offset we skip the initial
521   // reserved PLT entry.
522   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
523
524   ++this->count_;
525
526   section_offset_type got_offset = this->got_plt_->current_data_size();
527
528   // Every PLT entry needs a GOT entry which points back to the PLT
529   // entry (this will be changed by the dynamic linker, normally
530   // lazily when the function is called).
531   this->got_plt_->set_current_data_size(got_offset + 4);
532
533   // Every PLT entry needs a reloc.
534   gsym->set_needs_dynsym_entry();
535   this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
536                          got_offset);
537
538   // Note that we don't need to save the symbol.  The contents of the
539   // PLT are independent of which symbols are used.  The symbols only
540   // appear in the relocations.
541 }
542
543 // The first entry in the PLT for an executable.
544
545 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
546 {
547   0xff, 0x35,   // pushl contents of memory address
548   0, 0, 0, 0,   // replaced with address of .got + 4
549   0xff, 0x25,   // jmp indirect
550   0, 0, 0, 0,   // replaced with address of .got + 8
551   0, 0, 0, 0    // unused
552 };
553
554 // The first entry in the PLT for a shared object.
555
556 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
557 {
558   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
559   0xff, 0xa3, 8, 0, 0, 0,       // jmp *8(%ebx)
560   0, 0, 0, 0                    // unused
561 };
562
563 // Subsequent entries in the PLT for an executable.
564
565 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
566 {
567   0xff, 0x25,   // jmp indirect
568   0, 0, 0, 0,   // replaced with address of symbol in .got
569   0x68,         // pushl immediate
570   0, 0, 0, 0,   // replaced with offset into relocation table
571   0xe9,         // jmp relative
572   0, 0, 0, 0    // replaced with offset to start of .plt
573 };
574
575 // Subsequent entries in the PLT for a shared object.
576
577 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
578 {
579   0xff, 0xa3,   // jmp *offset(%ebx)
580   0, 0, 0, 0,   // replaced with offset of symbol in .got
581   0x68,         // pushl immediate
582   0, 0, 0, 0,   // replaced with offset into relocation table
583   0xe9,         // jmp relative
584   0, 0, 0, 0    // replaced with offset to start of .plt
585 };
586
587 // Write out the PLT.  This uses the hand-coded instructions above,
588 // and adjusts them as needed.  This is all specified by the i386 ELF
589 // Processor Supplement.
590
591 void
592 Output_data_plt_i386::do_write(Output_file* of)
593 {
594   const off_t offset = this->offset();
595   const section_size_type oview_size =
596     convert_to_section_size_type(this->data_size());
597   unsigned char* const oview = of->get_output_view(offset, oview_size);
598
599   const off_t got_file_offset = this->got_plt_->offset();
600   const section_size_type got_size =
601     convert_to_section_size_type(this->got_plt_->data_size());
602   unsigned char* const got_view = of->get_output_view(got_file_offset,
603                                                       got_size);
604
605   unsigned char* pov = oview;
606
607   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
608   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
609
610   if (parameters->options().shared())
611     memcpy(pov, dyn_first_plt_entry, plt_entry_size);
612   else
613     {
614       memcpy(pov, exec_first_plt_entry, plt_entry_size);
615       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
616       elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
617     }
618   pov += plt_entry_size;
619
620   unsigned char* got_pov = got_view;
621
622   memset(got_pov, 0, 12);
623   got_pov += 12;
624
625   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
626
627   unsigned int plt_offset = plt_entry_size;
628   unsigned int plt_rel_offset = 0;
629   unsigned int got_offset = 12;
630   const unsigned int count = this->count_;
631   for (unsigned int i = 0;
632        i < count;
633        ++i,
634          pov += plt_entry_size,
635          got_pov += 4,
636          plt_offset += plt_entry_size,
637          plt_rel_offset += rel_size,
638          got_offset += 4)
639     {
640       // Set and adjust the PLT entry itself.
641
642       if (parameters->options().shared())
643         {
644           memcpy(pov, dyn_plt_entry, plt_entry_size);
645           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
646         }
647       else
648         {
649           memcpy(pov, exec_plt_entry, plt_entry_size);
650           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
651                                                       (got_address
652                                                        + got_offset));
653         }
654
655       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
656       elfcpp::Swap<32, false>::writeval(pov + 12,
657                                         - (plt_offset + plt_entry_size));
658
659       // Set the entry in the GOT.
660       elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
661     }
662
663   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
664   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
665
666   of->write_output_view(offset, oview_size, oview);
667   of->write_output_view(got_file_offset, got_size, got_view);
668 }
669
670 // Create a PLT entry for a global symbol.
671
672 void
673 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
674 {
675   if (gsym->has_plt_offset())
676     return;
677
678   if (this->plt_ == NULL)
679     {
680       // Create the GOT sections first.
681       this->got_section(symtab, layout);
682
683       this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
684       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
685                                       (elfcpp::SHF_ALLOC
686                                        | elfcpp::SHF_EXECINSTR),
687                                       this->plt_);
688     }
689
690   this->plt_->add_entry(gsym);
691 }
692
693 // Create a GOT entry for the TLS module index.
694
695 unsigned int
696 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
697                                  Sized_relobj<32, false>* object)
698 {
699   if (this->got_mod_index_offset_ == -1U)
700     {
701       gold_assert(symtab != NULL && layout != NULL && object != NULL);
702       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
703       Output_data_got<32, false>* got = this->got_section(symtab, layout);
704       unsigned int got_offset = got->add_constant(0);
705       rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
706                          got_offset);
707       got->add_constant(0);
708       this->got_mod_index_offset_ = got_offset;
709     }
710   return this->got_mod_index_offset_;
711 }
712
713 // Handle a relocation against a non-function symbol defined in a
714 // dynamic object.  The traditional way to handle this is to generate
715 // a COPY relocation to copy the variable at runtime from the shared
716 // object into the executable's data segment.  However, this is
717 // undesirable in general, as if the size of the object changes in the
718 // dynamic object, the executable will no longer work correctly.  If
719 // this relocation is in a writable section, then we can create a
720 // dynamic reloc and the dynamic linker will resolve it to the correct
721 // address at runtime.  However, we do not want do that if the
722 // relocation is in a read-only section, as it would prevent the
723 // readonly segment from being shared.  And if we have to eventually
724 // generate a COPY reloc, then any dynamic relocations will be
725 // useless.  So this means that if this is a writable section, we need
726 // to save the relocation until we see whether we have to create a
727 // COPY relocation for this symbol for any other relocation.
728
729 void
730 Target_i386::copy_reloc(const General_options* options,
731                         Symbol_table* symtab,
732                         Layout* layout,
733                         Sized_relobj<32, false>* object,
734                         unsigned int data_shndx,
735                         Output_section* output_section,
736                         Symbol* gsym,
737                         const elfcpp::Rel<32, false>& rel)
738 {
739   Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(gsym);
740
741   if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
742                                                data_shndx, ssym))
743     {
744       // So far we do not need a COPY reloc.  Save this relocation.
745       // If it turns out that we never need a COPY reloc for this
746       // symbol, then we will emit the relocation.
747       if (this->copy_relocs_ == NULL)
748         this->copy_relocs_ = new Copy_relocs<32, false>();
749       this->copy_relocs_->save(ssym, object, data_shndx, output_section, rel);
750     }
751   else
752     {
753       // Allocate space for this symbol in the .bss section.
754
755       elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
756
757       // There is no defined way to determine the required alignment
758       // of the symbol.  We pick the alignment based on the size.  We
759       // set an arbitrary maximum of 256.
760       unsigned int align;
761       for (align = 1; align < 512; align <<= 1)
762         if ((symsize & align) != 0)
763           break;
764
765       if (this->dynbss_ == NULL)
766         {
767           this->dynbss_ = new Output_data_space(align);
768           layout->add_output_section_data(".bss",
769                                           elfcpp::SHT_NOBITS,
770                                           (elfcpp::SHF_ALLOC
771                                            | elfcpp::SHF_WRITE),
772                                           this->dynbss_);
773         }
774
775       Output_data_space* dynbss = this->dynbss_;
776
777       if (align > dynbss->addralign())
778         dynbss->set_space_alignment(align);
779
780       section_size_type dynbss_size =
781         convert_to_section_size_type(dynbss->current_data_size());
782       dynbss_size = align_address(dynbss_size, align);
783       section_size_type offset = dynbss_size;
784       dynbss->set_current_data_size(dynbss_size + symsize);
785
786       symtab->define_with_copy_reloc(ssym, dynbss, offset);
787
788       // Add the COPY reloc.
789       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
790       rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
791     }
792 }
793
794 // Optimize the TLS relocation type based on what we know about the
795 // symbol.  IS_FINAL is true if the final address of this symbol is
796 // known at link time.
797
798 tls::Tls_optimization
799 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
800 {
801   // If we are generating a shared library, then we can't do anything
802   // in the linker.
803   if (parameters->options().shared())
804     return tls::TLSOPT_NONE;
805
806   switch (r_type)
807     {
808     case elfcpp::R_386_TLS_GD:
809     case elfcpp::R_386_TLS_GOTDESC:
810     case elfcpp::R_386_TLS_DESC_CALL:
811       // These are General-Dynamic which permits fully general TLS
812       // access.  Since we know that we are generating an executable,
813       // we can convert this to Initial-Exec.  If we also know that
814       // this is a local symbol, we can further switch to Local-Exec.
815       if (is_final)
816         return tls::TLSOPT_TO_LE;
817       return tls::TLSOPT_TO_IE;
818
819     case elfcpp::R_386_TLS_LDM:
820       // This is Local-Dynamic, which refers to a local symbol in the
821       // dynamic TLS block.  Since we know that we generating an
822       // executable, we can switch to Local-Exec.
823       return tls::TLSOPT_TO_LE;
824
825     case elfcpp::R_386_TLS_LDO_32:
826       // Another type of Local-Dynamic relocation.
827       return tls::TLSOPT_TO_LE;
828
829     case elfcpp::R_386_TLS_IE:
830     case elfcpp::R_386_TLS_GOTIE:
831     case elfcpp::R_386_TLS_IE_32:
832       // These are Initial-Exec relocs which get the thread offset
833       // from the GOT.  If we know that we are linking against the
834       // local symbol, we can switch to Local-Exec, which links the
835       // thread offset into the instruction.
836       if (is_final)
837         return tls::TLSOPT_TO_LE;
838       return tls::TLSOPT_NONE;
839
840     case elfcpp::R_386_TLS_LE:
841     case elfcpp::R_386_TLS_LE_32:
842       // When we already have Local-Exec, there is nothing further we
843       // can do.
844       return tls::TLSOPT_NONE;
845
846     default:
847       gold_unreachable();
848     }
849 }
850
851 // Report an unsupported relocation against a local symbol.
852
853 void
854 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
855                                            unsigned int r_type)
856 {
857   gold_error(_("%s: unsupported reloc %u against local symbol"),
858              object->name().c_str(), r_type);
859 }
860
861 // Scan a relocation for a local symbol.
862
863 inline void
864 Target_i386::Scan::local(const General_options&,
865                          Symbol_table* symtab,
866                          Layout* layout,
867                          Target_i386* target,
868                          Sized_relobj<32, false>* object,
869                          unsigned int data_shndx,
870                          Output_section* output_section,
871                          const elfcpp::Rel<32, false>& reloc,
872                          unsigned int r_type,
873                          const elfcpp::Sym<32, false>& lsym)
874 {
875   switch (r_type)
876     {
877     case elfcpp::R_386_NONE:
878     case elfcpp::R_386_GNU_VTINHERIT:
879     case elfcpp::R_386_GNU_VTENTRY:
880       break;
881
882     case elfcpp::R_386_32:
883       // If building a shared library (or a position-independent
884       // executable), we need to create a dynamic relocation for
885       // this location. The relocation applied at link time will
886       // apply the link-time value, so we flag the location with
887       // an R_386_RELATIVE relocation so the dynamic loader can
888       // relocate it easily.
889       if (parameters->options().output_is_position_independent())
890         {
891           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
892           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
893           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
894                                       output_section, data_shndx,
895                                       reloc.get_r_offset());
896         }
897       break;
898
899     case elfcpp::R_386_16:
900     case elfcpp::R_386_8:
901       // If building a shared library (or a position-independent
902       // executable), we need to create a dynamic relocation for
903       // this location. Because the addend needs to remain in the
904       // data section, we need to be careful not to apply this
905       // relocation statically.
906       if (parameters->options().output_is_position_independent())
907         {
908           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
909           if (lsym.get_st_type() != elfcpp::STT_SECTION)
910             {
911               unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
912               rel_dyn->add_local(object, r_sym, r_type, output_section,
913                                  data_shndx, reloc.get_r_offset());
914             }
915           else
916             {
917               gold_assert(lsym.get_st_value() == 0);
918               rel_dyn->add_local_section(object, lsym.get_st_shndx(),
919                                          r_type, output_section,
920                                          data_shndx, reloc.get_r_offset());
921             }
922         }
923       break;
924
925     case elfcpp::R_386_PC32:
926     case elfcpp::R_386_PC16:
927     case elfcpp::R_386_PC8:
928       break;
929
930     case elfcpp::R_386_PLT32:
931       // Since we know this is a local symbol, we can handle this as a
932       // PC32 reloc.
933       break;
934
935     case elfcpp::R_386_GOTOFF:
936     case elfcpp::R_386_GOTPC:
937       // We need a GOT section.
938       target->got_section(symtab, layout);
939       break;
940
941     case elfcpp::R_386_GOT32:
942       {
943         // The symbol requires a GOT entry.
944         Output_data_got<32, false>* got = target->got_section(symtab, layout);
945         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
946         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
947           {
948             // If we are generating a shared object, we need to add a
949             // dynamic RELATIVE relocation for this symbol's GOT entry.
950             if (parameters->options().output_is_position_independent())
951               {
952                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
953                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
954                 rel_dyn->add_local_relative(
955                     object, r_sym, elfcpp::R_386_RELATIVE, got,
956                     object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
957               }
958           }
959       }
960       break;
961
962       // These are relocations which should only be seen by the
963       // dynamic linker, and should never be seen here.
964     case elfcpp::R_386_COPY:
965     case elfcpp::R_386_GLOB_DAT:
966     case elfcpp::R_386_JUMP_SLOT:
967     case elfcpp::R_386_RELATIVE:
968     case elfcpp::R_386_TLS_TPOFF:
969     case elfcpp::R_386_TLS_DTPMOD32:
970     case elfcpp::R_386_TLS_DTPOFF32:
971     case elfcpp::R_386_TLS_TPOFF32:
972     case elfcpp::R_386_TLS_DESC:
973       gold_error(_("%s: unexpected reloc %u in object file"),
974                  object->name().c_str(), r_type);
975       break;
976
977       // These are initial TLS relocs, which are expected when
978       // linking.
979     case elfcpp::R_386_TLS_GD:            // Global-dynamic
980     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
981     case elfcpp::R_386_TLS_DESC_CALL:
982     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
983     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
984     case elfcpp::R_386_TLS_IE:            // Initial-exec
985     case elfcpp::R_386_TLS_IE_32:
986     case elfcpp::R_386_TLS_GOTIE:
987     case elfcpp::R_386_TLS_LE:            // Local-exec
988     case elfcpp::R_386_TLS_LE_32:
989       {
990         bool output_is_shared = parameters->options().shared();
991         const tls::Tls_optimization optimized_type
992             = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
993         switch (r_type)
994           {
995           case elfcpp::R_386_TLS_GD:          // Global-dynamic
996             if (optimized_type == tls::TLSOPT_NONE)
997               {
998                 // Create a pair of GOT entries for the module index and
999                 // dtv-relative offset.
1000                 Output_data_got<32, false>* got
1001                     = target->got_section(symtab, layout);
1002                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1003                 got->add_local_pair_with_rel(object, r_sym, 
1004                                              lsym.get_st_shndx(),
1005                                              GOT_TYPE_TLS_PAIR,
1006                                              target->rel_dyn_section(layout),
1007                                              elfcpp::R_386_TLS_DTPMOD32, 0);
1008               }
1009             else if (optimized_type != tls::TLSOPT_TO_LE)
1010               unsupported_reloc_local(object, r_type);
1011             break;
1012
1013           case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (from ~oliva)
1014           case elfcpp::R_386_TLS_DESC_CALL:
1015             // FIXME: If not relaxing to LE, we need to generate
1016             // a GOT entry with an R_386_TLS_DESC reloc.
1017             if (optimized_type != tls::TLSOPT_TO_LE)
1018               unsupported_reloc_local(object, r_type);
1019             break;
1020
1021           case elfcpp::R_386_TLS_LDM:         // Local-dynamic
1022             if (optimized_type == tls::TLSOPT_NONE)
1023               {
1024                 // Create a GOT entry for the module index.
1025                 target->got_mod_index_entry(symtab, layout, object);
1026               }
1027             else if (optimized_type != tls::TLSOPT_TO_LE)
1028               unsupported_reloc_local(object, r_type);
1029             break;
1030
1031           case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
1032             break;
1033
1034           case elfcpp::R_386_TLS_IE:          // Initial-exec
1035           case elfcpp::R_386_TLS_IE_32:
1036           case elfcpp::R_386_TLS_GOTIE:
1037             layout->set_has_static_tls();
1038             if (optimized_type == tls::TLSOPT_NONE)
1039               {
1040                 // For the R_386_TLS_IE relocation, we need to create a
1041                 // dynamic relocation when building a shared library.
1042                 if (r_type == elfcpp::R_386_TLS_IE
1043                     && parameters->options().shared())
1044                   {
1045                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1046                     unsigned int r_sym
1047                         = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1048                     rel_dyn->add_local_relative(object, r_sym,
1049                                                 elfcpp::R_386_RELATIVE,
1050                                                 output_section, data_shndx,
1051                                                 reloc.get_r_offset());
1052                   }
1053                 // Create a GOT entry for the tp-relative offset.
1054                 Output_data_got<32, false>* got
1055                     = target->got_section(symtab, layout);
1056                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1057                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1058                                            ? elfcpp::R_386_TLS_TPOFF32
1059                                            : elfcpp::R_386_TLS_TPOFF);
1060                 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
1061                                         target->rel_dyn_section(layout),
1062                                         dyn_r_type);
1063               }
1064             else if (optimized_type != tls::TLSOPT_TO_LE)
1065               unsupported_reloc_local(object, r_type);
1066             break;
1067
1068           case elfcpp::R_386_TLS_LE:          // Local-exec
1069           case elfcpp::R_386_TLS_LE_32:
1070             layout->set_has_static_tls();
1071             if (output_is_shared)
1072               {
1073                 // We need to create a dynamic relocation.
1074                 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1075                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1076                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1077                                            ? elfcpp::R_386_TLS_TPOFF32
1078                                            : elfcpp::R_386_TLS_TPOFF);
1079                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1080                 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1081                                    data_shndx, reloc.get_r_offset());
1082               }
1083             break;
1084
1085           default:
1086             gold_unreachable();
1087           }
1088       }
1089       break;
1090
1091     case elfcpp::R_386_32PLT:
1092     case elfcpp::R_386_TLS_GD_32:
1093     case elfcpp::R_386_TLS_GD_PUSH:
1094     case elfcpp::R_386_TLS_GD_CALL:
1095     case elfcpp::R_386_TLS_GD_POP:
1096     case elfcpp::R_386_TLS_LDM_32:
1097     case elfcpp::R_386_TLS_LDM_PUSH:
1098     case elfcpp::R_386_TLS_LDM_CALL:
1099     case elfcpp::R_386_TLS_LDM_POP:
1100     case elfcpp::R_386_USED_BY_INTEL_200:
1101     default:
1102       unsupported_reloc_local(object, r_type);
1103       break;
1104     }
1105 }
1106
1107 // Report an unsupported relocation against a global symbol.
1108
1109 void
1110 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1111                                             unsigned int r_type,
1112                                             Symbol* gsym)
1113 {
1114   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1115              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1116 }
1117
1118 // Scan a relocation for a global symbol.
1119
1120 inline void
1121 Target_i386::Scan::global(const General_options& options,
1122                           Symbol_table* symtab,
1123                           Layout* layout,
1124                           Target_i386* target,
1125                           Sized_relobj<32, false>* object,
1126                           unsigned int data_shndx,
1127                           Output_section* output_section,
1128                           const elfcpp::Rel<32, false>& reloc,
1129                           unsigned int r_type,
1130                           Symbol* gsym)
1131 {
1132   switch (r_type)
1133     {
1134     case elfcpp::R_386_NONE:
1135     case elfcpp::R_386_GNU_VTINHERIT:
1136     case elfcpp::R_386_GNU_VTENTRY:
1137       break;
1138
1139     case elfcpp::R_386_32:
1140     case elfcpp::R_386_16:
1141     case elfcpp::R_386_8:
1142       {
1143         // Make a PLT entry if necessary.
1144         if (gsym->needs_plt_entry())
1145           {
1146             target->make_plt_entry(symtab, layout, gsym);
1147             // Since this is not a PC-relative relocation, we may be
1148             // taking the address of a function. In that case we need to
1149             // set the entry in the dynamic symbol table to the address of
1150             // the PLT entry.
1151             if (gsym->is_from_dynobj() && !parameters->options().shared())
1152               gsym->set_needs_dynsym_value();
1153           }
1154         // Make a dynamic relocation if necessary.
1155         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1156           {
1157             if (target->may_need_copy_reloc(gsym))
1158               {
1159                 target->copy_reloc(&options, symtab, layout, object,
1160                                    data_shndx, output_section, gsym, reloc);
1161               }
1162             else if (r_type == elfcpp::R_386_32
1163                      && gsym->can_use_relative_reloc(false))
1164               {
1165                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1166                 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1167                                              output_section, object,
1168                                              data_shndx, reloc.get_r_offset());
1169               }
1170             else
1171               {
1172                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1173                 rel_dyn->add_global(gsym, r_type, output_section, object,
1174                                     data_shndx, reloc.get_r_offset());
1175               }
1176           }
1177       }
1178       break;
1179
1180     case elfcpp::R_386_PC32:
1181     case elfcpp::R_386_PC16:
1182     case elfcpp::R_386_PC8:
1183       {
1184         // Make a PLT entry if necessary.
1185         if (gsym->needs_plt_entry())
1186           {
1187             // These relocations are used for function calls only in
1188             // non-PIC code.  For a 32-bit relocation in a shared library,
1189             // we'll need a text relocation anyway, so we can skip the
1190             // PLT entry and let the dynamic linker bind the call directly
1191             // to the target.  For smaller relocations, we should use a
1192             // PLT entry to ensure that the call can reach.
1193             if (!parameters->options().shared()
1194                 || r_type != elfcpp::R_386_PC32)
1195               target->make_plt_entry(symtab, layout, gsym);
1196           }
1197         // Make a dynamic relocation if necessary.
1198         int flags = Symbol::NON_PIC_REF;
1199         if (gsym->type() == elfcpp::STT_FUNC)
1200           flags |= Symbol::FUNCTION_CALL;
1201         if (gsym->needs_dynamic_reloc(flags))
1202           {
1203             if (target->may_need_copy_reloc(gsym))
1204               {
1205                 target->copy_reloc(&options, symtab, layout, object,
1206                                    data_shndx, output_section, gsym, reloc);
1207               }
1208             else
1209               {
1210                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1211                 rel_dyn->add_global(gsym, r_type, output_section, object,
1212                                     data_shndx, reloc.get_r_offset());
1213               }
1214           }
1215       }
1216       break;
1217
1218     case elfcpp::R_386_GOT32:
1219       {
1220         // The symbol requires a GOT entry.
1221         Output_data_got<32, false>* got = target->got_section(symtab, layout);
1222         if (gsym->final_value_is_known())
1223           got->add_global(gsym, GOT_TYPE_STANDARD);
1224         else
1225           {
1226             // If this symbol is not fully resolved, we need to add a
1227             // GOT entry with a dynamic relocation.
1228             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1229             if (gsym->is_from_dynobj()
1230                 || gsym->is_undefined()
1231                 || gsym->is_preemptible())
1232               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1233                                        rel_dyn, elfcpp::R_386_GLOB_DAT);
1234             else
1235               {
1236                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1237                   rel_dyn->add_global_relative(
1238                       gsym, elfcpp::R_386_RELATIVE, got,
1239                       gsym->got_offset(GOT_TYPE_STANDARD));
1240               }
1241           }
1242       }
1243       break;
1244
1245     case elfcpp::R_386_PLT32:
1246       // If the symbol is fully resolved, this is just a PC32 reloc.
1247       // Otherwise we need a PLT entry.
1248       if (gsym->final_value_is_known())
1249         break;
1250       // If building a shared library, we can also skip the PLT entry
1251       // if the symbol is defined in the output file and is protected
1252       // or hidden.
1253       if (gsym->is_defined()
1254           && !gsym->is_from_dynobj()
1255           && !gsym->is_preemptible())
1256         break;
1257       target->make_plt_entry(symtab, layout, gsym);
1258       break;
1259
1260     case elfcpp::R_386_GOTOFF:
1261     case elfcpp::R_386_GOTPC:
1262       // We need a GOT section.
1263       target->got_section(symtab, layout);
1264       break;
1265
1266       // These are relocations which should only be seen by the
1267       // dynamic linker, and should never be seen here.
1268     case elfcpp::R_386_COPY:
1269     case elfcpp::R_386_GLOB_DAT:
1270     case elfcpp::R_386_JUMP_SLOT:
1271     case elfcpp::R_386_RELATIVE:
1272     case elfcpp::R_386_TLS_TPOFF:
1273     case elfcpp::R_386_TLS_DTPMOD32:
1274     case elfcpp::R_386_TLS_DTPOFF32:
1275     case elfcpp::R_386_TLS_TPOFF32:
1276     case elfcpp::R_386_TLS_DESC:
1277       gold_error(_("%s: unexpected reloc %u in object file"),
1278                  object->name().c_str(), r_type);
1279       break;
1280
1281       // These are initial tls relocs, which are expected when
1282       // linking.
1283     case elfcpp::R_386_TLS_GD:            // Global-dynamic
1284     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
1285     case elfcpp::R_386_TLS_DESC_CALL:
1286     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
1287     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
1288     case elfcpp::R_386_TLS_IE:            // Initial-exec
1289     case elfcpp::R_386_TLS_IE_32:
1290     case elfcpp::R_386_TLS_GOTIE:
1291     case elfcpp::R_386_TLS_LE:            // Local-exec
1292     case elfcpp::R_386_TLS_LE_32:
1293       {
1294         const bool is_final = gsym->final_value_is_known();
1295         const tls::Tls_optimization optimized_type
1296             = Target_i386::optimize_tls_reloc(is_final, r_type);
1297         switch (r_type)
1298           {
1299           case elfcpp::R_386_TLS_GD:          // Global-dynamic
1300             if (optimized_type == tls::TLSOPT_NONE)
1301               {
1302                 // Create a pair of GOT entries for the module index and
1303                 // dtv-relative offset.
1304                 Output_data_got<32, false>* got
1305                     = target->got_section(symtab, layout);
1306                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1307                                              target->rel_dyn_section(layout),
1308                                              elfcpp::R_386_TLS_DTPMOD32,
1309                                              elfcpp::R_386_TLS_DTPOFF32);
1310               }
1311             else if (optimized_type == tls::TLSOPT_TO_IE)
1312               {
1313                 // Create a GOT entry for the tp-relative offset.
1314                 Output_data_got<32, false>* got
1315                     = target->got_section(symtab, layout);
1316                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
1317                                          target->rel_dyn_section(layout),
1318                                          elfcpp::R_386_TLS_TPOFF32);
1319               }
1320             else if (optimized_type != tls::TLSOPT_TO_LE)
1321               unsupported_reloc_global(object, r_type, gsym);
1322             break;
1323
1324           case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (~oliva url)
1325           case elfcpp::R_386_TLS_DESC_CALL:
1326             // FIXME: If not relaxing to LE, we need to generate
1327             // a GOT entry with an R_386_TLS_DESC reloc.
1328             if (optimized_type != tls::TLSOPT_TO_LE)
1329               unsupported_reloc_global(object, r_type, gsym);
1330             unsupported_reloc_global(object, r_type, gsym);
1331             break;
1332
1333           case elfcpp::R_386_TLS_LDM:         // Local-dynamic
1334             if (optimized_type == tls::TLSOPT_NONE)
1335               {
1336                 // Create a GOT entry for the module index.
1337                 target->got_mod_index_entry(symtab, layout, object);
1338               }
1339             else if (optimized_type != tls::TLSOPT_TO_LE)
1340               unsupported_reloc_global(object, r_type, gsym);
1341             break;
1342
1343           case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
1344             break;
1345
1346           case elfcpp::R_386_TLS_IE:          // Initial-exec
1347           case elfcpp::R_386_TLS_IE_32:
1348           case elfcpp::R_386_TLS_GOTIE:
1349             layout->set_has_static_tls();
1350             if (optimized_type == tls::TLSOPT_NONE)
1351               {
1352                 // For the R_386_TLS_IE relocation, we need to create a
1353                 // dynamic relocation when building a shared library.
1354                 if (r_type == elfcpp::R_386_TLS_IE
1355                     && parameters->options().shared())
1356                   {
1357                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1358                     rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1359                                                  output_section, object,
1360                                                  data_shndx,
1361                                                  reloc.get_r_offset());
1362                   }
1363                 // Create a GOT entry for the tp-relative offset.
1364                 Output_data_got<32, false>* got
1365                     = target->got_section(symtab, layout);
1366                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1367                                            ? elfcpp::R_386_TLS_TPOFF32
1368                                            : elfcpp::R_386_TLS_TPOFF);
1369                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
1370                                          target->rel_dyn_section(layout),
1371                                          dyn_r_type);
1372               }
1373             else if (optimized_type != tls::TLSOPT_TO_LE)
1374               unsupported_reloc_global(object, r_type, gsym);
1375             break;
1376
1377           case elfcpp::R_386_TLS_LE:          // Local-exec
1378           case elfcpp::R_386_TLS_LE_32:
1379             layout->set_has_static_tls();
1380             if (parameters->options().shared())
1381               {
1382                 // We need to create a dynamic relocation.
1383                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1384                                            ? elfcpp::R_386_TLS_TPOFF32
1385                                            : elfcpp::R_386_TLS_TPOFF);
1386                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1387                 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1388                                     data_shndx, reloc.get_r_offset());
1389               }
1390             break;
1391
1392           default:
1393             gold_unreachable();
1394           }
1395       }
1396       break;
1397
1398     case elfcpp::R_386_32PLT:
1399     case elfcpp::R_386_TLS_GD_32:
1400     case elfcpp::R_386_TLS_GD_PUSH:
1401     case elfcpp::R_386_TLS_GD_CALL:
1402     case elfcpp::R_386_TLS_GD_POP:
1403     case elfcpp::R_386_TLS_LDM_32:
1404     case elfcpp::R_386_TLS_LDM_PUSH:
1405     case elfcpp::R_386_TLS_LDM_CALL:
1406     case elfcpp::R_386_TLS_LDM_POP:
1407     case elfcpp::R_386_USED_BY_INTEL_200:
1408     default:
1409       unsupported_reloc_global(object, r_type, gsym);
1410       break;
1411     }
1412 }
1413
1414 // Scan relocations for a section.
1415
1416 void
1417 Target_i386::scan_relocs(const General_options& options,
1418                          Symbol_table* symtab,
1419                          Layout* layout,
1420                          Sized_relobj<32, false>* object,
1421                          unsigned int data_shndx,
1422                          unsigned int sh_type,
1423                          const unsigned char* prelocs,
1424                          size_t reloc_count,
1425                          Output_section* output_section,
1426                          bool needs_special_offset_handling,
1427                          size_t local_symbol_count,
1428                          const unsigned char* plocal_symbols)
1429 {
1430   if (sh_type == elfcpp::SHT_RELA)
1431     {
1432       gold_error(_("%s: unsupported RELA reloc section"),
1433                  object->name().c_str());
1434       return;
1435     }
1436
1437   gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1438                     Target_i386::Scan>(
1439     options,
1440     symtab,
1441     layout,
1442     this,
1443     object,
1444     data_shndx,
1445     prelocs,
1446     reloc_count,
1447     output_section,
1448     needs_special_offset_handling,
1449     local_symbol_count,
1450     plocal_symbols);
1451 }
1452
1453 // Finalize the sections.
1454
1455 void
1456 Target_i386::do_finalize_sections(Layout* layout)
1457 {
1458   // Fill in some more dynamic tags.
1459   Output_data_dynamic* const odyn = layout->dynamic_data();
1460   if (odyn != NULL)
1461     {
1462       if (this->got_plt_ != NULL)
1463         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1464
1465       if (this->plt_ != NULL)
1466         {
1467           const Output_data* od = this->plt_->rel_plt();
1468           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1469           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1470           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1471         }
1472
1473       if (this->rel_dyn_ != NULL)
1474         {
1475           const Output_data* od = this->rel_dyn_;
1476           odyn->add_section_address(elfcpp::DT_REL, od);
1477           odyn->add_section_size(elfcpp::DT_RELSZ, od);
1478           odyn->add_constant(elfcpp::DT_RELENT,
1479                              elfcpp::Elf_sizes<32>::rel_size);
1480         }
1481
1482       if (!parameters->options().shared())
1483         {
1484           // The value of the DT_DEBUG tag is filled in by the dynamic
1485           // linker at run time, and used by the debugger.
1486           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1487         }
1488     }
1489
1490   // Emit any relocs we saved in an attempt to avoid generating COPY
1491   // relocs.
1492   if (this->copy_relocs_ == NULL)
1493     return;
1494   if (this->copy_relocs_->any_to_emit())
1495     {
1496       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1497       this->copy_relocs_->emit(rel_dyn);
1498     }
1499   delete this->copy_relocs_;
1500   this->copy_relocs_ = NULL;
1501 }
1502
1503 // Return whether a direct absolute static relocation needs to be applied.
1504 // In cases where Scan::local() or Scan::global() has created
1505 // a dynamic relocation other than R_386_RELATIVE, the addend
1506 // of the relocation is carried in the data, and we must not
1507 // apply the static relocation.
1508
1509 inline bool
1510 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1511                                                  int ref_flags,
1512                                                  bool is_32bit)
1513 {
1514   // For local symbols, we will have created a non-RELATIVE dynamic
1515   // relocation only if (a) the output is position independent,
1516   // (b) the relocation is absolute (not pc- or segment-relative), and
1517   // (c) the relocation is not 32 bits wide.
1518   if (gsym == NULL)
1519     return !(parameters->options().output_is_position_independent()
1520              && (ref_flags & Symbol::ABSOLUTE_REF)
1521              && !is_32bit);
1522
1523   // For global symbols, we use the same helper routines used in the
1524   // scan pass.  If we did not create a dynamic relocation, or if we
1525   // created a RELATIVE dynamic relocation, we should apply the static
1526   // relocation.
1527   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1528   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1529                 && gsym->can_use_relative_reloc(ref_flags
1530                                                 & Symbol::FUNCTION_CALL);
1531   return !has_dyn || is_rel;
1532 }
1533
1534 // Perform a relocation.
1535
1536 inline bool
1537 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1538                                 Target_i386* target,
1539                                 size_t relnum,
1540                                 const elfcpp::Rel<32, false>& rel,
1541                                 unsigned int r_type,
1542                                 const Sized_symbol<32>* gsym,
1543                                 const Symbol_value<32>* psymval,
1544                                 unsigned char* view,
1545                                 elfcpp::Elf_types<32>::Elf_Addr address,
1546                                 section_size_type view_size)
1547 {
1548   if (this->skip_call_tls_get_addr_)
1549     {
1550       if (r_type != elfcpp::R_386_PLT32
1551           || gsym == NULL
1552           || strcmp(gsym->name(), "___tls_get_addr") != 0)
1553         gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1554                                _("missing expected TLS relocation"));
1555       else
1556         {
1557           this->skip_call_tls_get_addr_ = false;
1558           return false;
1559         }
1560     }
1561
1562   // Pick the value to use for symbols defined in shared objects.
1563   Symbol_value<32> symval;
1564   bool is_nonpic = (r_type == elfcpp::R_386_PC8
1565                     || r_type == elfcpp::R_386_PC16
1566                     || r_type == elfcpp::R_386_PC32);
1567   if (gsym != NULL
1568       && (gsym->is_from_dynobj()
1569           || (parameters->options().shared()
1570               && (gsym->is_undefined() || gsym->is_preemptible())))
1571       && gsym->has_plt_offset()
1572       && (!is_nonpic || !parameters->options().shared()))
1573     {
1574       symval.set_output_value(target->plt_section()->address()
1575                               + gsym->plt_offset());
1576       psymval = &symval;
1577     }
1578
1579   const Sized_relobj<32, false>* object = relinfo->object;
1580
1581   // Get the GOT offset if needed.
1582   // The GOT pointer points to the end of the GOT section.
1583   // We need to subtract the size of the GOT section to get
1584   // the actual offset to use in the relocation.
1585   bool have_got_offset = false;
1586   unsigned int got_offset = 0;
1587   switch (r_type)
1588     {
1589     case elfcpp::R_386_GOT32:
1590       if (gsym != NULL)
1591         {
1592           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1593           got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1594                         - target->got_size());
1595         }
1596       else
1597         {
1598           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1599           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1600           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1601                         - target->got_size());
1602         }
1603       have_got_offset = true;
1604       break;
1605
1606     default:
1607       break;
1608     }
1609
1610   switch (r_type)
1611     {
1612     case elfcpp::R_386_NONE:
1613     case elfcpp::R_386_GNU_VTINHERIT:
1614     case elfcpp::R_386_GNU_VTENTRY:
1615       break;
1616
1617     case elfcpp::R_386_32:
1618       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
1619         Relocate_functions<32, false>::rel32(view, object, psymval);
1620       break;
1621
1622     case elfcpp::R_386_PC32:
1623       {
1624         int ref_flags = Symbol::NON_PIC_REF;
1625         if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1626           ref_flags |= Symbol::FUNCTION_CALL;
1627         if (should_apply_static_reloc(gsym, ref_flags, true))
1628           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1629       }
1630       break;
1631
1632     case elfcpp::R_386_16:
1633       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1634         Relocate_functions<32, false>::rel16(view, object, psymval);
1635       break;
1636
1637     case elfcpp::R_386_PC16:
1638       {
1639         int ref_flags = Symbol::NON_PIC_REF;
1640         if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1641           ref_flags |= Symbol::FUNCTION_CALL;
1642         if (should_apply_static_reloc(gsym, ref_flags, false))
1643           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1644       }
1645       break;
1646
1647     case elfcpp::R_386_8:
1648       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1649         Relocate_functions<32, false>::rel8(view, object, psymval);
1650       break;
1651
1652     case elfcpp::R_386_PC8:
1653       {
1654         int ref_flags = Symbol::NON_PIC_REF;
1655         if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1656           ref_flags |= Symbol::FUNCTION_CALL;
1657         if (should_apply_static_reloc(gsym, ref_flags, false))
1658           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1659       }
1660       break;
1661
1662     case elfcpp::R_386_PLT32:
1663       gold_assert(gsym == NULL
1664                   || gsym->has_plt_offset()
1665                   || gsym->final_value_is_known()
1666                   || (gsym->is_defined()
1667                       && !gsym->is_from_dynobj()
1668                       && !gsym->is_preemptible()));
1669       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1670       break;
1671
1672     case elfcpp::R_386_GOT32:
1673       gold_assert(have_got_offset);
1674       Relocate_functions<32, false>::rel32(view, got_offset);
1675       break;
1676
1677     case elfcpp::R_386_GOTOFF:
1678       {
1679         elfcpp::Elf_types<32>::Elf_Addr value;
1680         value = (psymval->value(object, 0)
1681                  - target->got_plt_section()->address());
1682         Relocate_functions<32, false>::rel32(view, value);
1683       }
1684       break;
1685
1686     case elfcpp::R_386_GOTPC:
1687       {
1688         elfcpp::Elf_types<32>::Elf_Addr value;
1689         value = target->got_plt_section()->address();
1690         Relocate_functions<32, false>::pcrel32(view, value, address);
1691       }
1692       break;
1693
1694     case elfcpp::R_386_COPY:
1695     case elfcpp::R_386_GLOB_DAT:
1696     case elfcpp::R_386_JUMP_SLOT:
1697     case elfcpp::R_386_RELATIVE:
1698       // These are outstanding tls relocs, which are unexpected when
1699       // linking.
1700     case elfcpp::R_386_TLS_TPOFF:
1701     case elfcpp::R_386_TLS_DTPMOD32:
1702     case elfcpp::R_386_TLS_DTPOFF32:
1703     case elfcpp::R_386_TLS_TPOFF32:
1704     case elfcpp::R_386_TLS_DESC:
1705       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1706                              _("unexpected reloc %u in object file"),
1707                              r_type);
1708       break;
1709
1710       // These are initial tls relocs, which are expected when
1711       // linking.
1712     case elfcpp::R_386_TLS_GD:             // Global-dynamic
1713     case elfcpp::R_386_TLS_GOTDESC:        // Global-dynamic (from ~oliva url)
1714     case elfcpp::R_386_TLS_DESC_CALL:
1715     case elfcpp::R_386_TLS_LDM:            // Local-dynamic
1716     case elfcpp::R_386_TLS_LDO_32:         // Alternate local-dynamic
1717     case elfcpp::R_386_TLS_IE:             // Initial-exec
1718     case elfcpp::R_386_TLS_IE_32:
1719     case elfcpp::R_386_TLS_GOTIE:
1720     case elfcpp::R_386_TLS_LE:             // Local-exec
1721     case elfcpp::R_386_TLS_LE_32:
1722       this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1723                          view, address, view_size);
1724       break;
1725
1726     case elfcpp::R_386_32PLT:
1727     case elfcpp::R_386_TLS_GD_32:
1728     case elfcpp::R_386_TLS_GD_PUSH:
1729     case elfcpp::R_386_TLS_GD_CALL:
1730     case elfcpp::R_386_TLS_GD_POP:
1731     case elfcpp::R_386_TLS_LDM_32:
1732     case elfcpp::R_386_TLS_LDM_PUSH:
1733     case elfcpp::R_386_TLS_LDM_CALL:
1734     case elfcpp::R_386_TLS_LDM_POP:
1735     case elfcpp::R_386_USED_BY_INTEL_200:
1736     default:
1737       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1738                              _("unsupported reloc %u"),
1739                              r_type);
1740       break;
1741     }
1742
1743   return true;
1744 }
1745
1746 // Perform a TLS relocation.
1747
1748 inline void
1749 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1750                                     Target_i386* target,
1751                                     size_t relnum,
1752                                     const elfcpp::Rel<32, false>& rel,
1753                                     unsigned int r_type,
1754                                     const Sized_symbol<32>* gsym,
1755                                     const Symbol_value<32>* psymval,
1756                                     unsigned char* view,
1757                                     elfcpp::Elf_types<32>::Elf_Addr,
1758                                     section_size_type view_size)
1759 {
1760   Output_segment* tls_segment = relinfo->layout->tls_segment();
1761
1762   const Sized_relobj<32, false>* object = relinfo->object;
1763
1764   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1765
1766   const bool is_final =
1767     (gsym == NULL
1768      ? !parameters->options().output_is_position_independent()
1769      : gsym->final_value_is_known());
1770   const tls::Tls_optimization optimized_type
1771       = Target_i386::optimize_tls_reloc(is_final, r_type);
1772   switch (r_type)
1773     {
1774     case elfcpp::R_386_TLS_GD:           // Global-dynamic
1775       if (optimized_type == tls::TLSOPT_TO_LE)
1776         {
1777           gold_assert(tls_segment != NULL);
1778           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1779                              rel, r_type, value, view,
1780                              view_size);
1781           break;
1782         }
1783       else
1784         {
1785           unsigned int got_offset;
1786           if (gsym != NULL)
1787             {
1788               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_PAIR));
1789               got_offset = (gsym->got_offset(GOT_TYPE_TLS_PAIR)
1790                             - target->got_size());
1791             }
1792           else
1793             {
1794               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1795               gold_assert(object->local_has_got_offset(r_sym,
1796                                                        GOT_TYPE_TLS_PAIR));
1797               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_PAIR)
1798                             - target->got_size());
1799             }
1800           if (optimized_type == tls::TLSOPT_TO_IE)
1801             {
1802               gold_assert(tls_segment != NULL);
1803               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1804                                  got_offset, view, view_size);
1805               break;
1806             }
1807           else if (optimized_type == tls::TLSOPT_NONE)
1808             {
1809               // Relocate the field with the offset of the pair of GOT
1810               // entries.
1811               Relocate_functions<32, false>::rel32(view, got_offset);
1812               break;
1813             }
1814         }
1815       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1816                              _("unsupported reloc %u"),
1817                              r_type);
1818       break;
1819
1820     case elfcpp::R_386_TLS_GOTDESC:      // Global-dynamic (from ~oliva url)
1821     case elfcpp::R_386_TLS_DESC_CALL:
1822       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1823                              _("unsupported reloc %u"),
1824                              r_type);
1825       break;
1826
1827     case elfcpp::R_386_TLS_LDM:          // Local-dynamic
1828       if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1829         {
1830           gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1831                                  _("both SUN and GNU model "
1832                                    "TLS relocations"));
1833           break;
1834         }
1835       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1836       if (optimized_type == tls::TLSOPT_TO_LE)
1837         {
1838           gold_assert(tls_segment != NULL);
1839           this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1840                              value, view, view_size);
1841           break;
1842         }
1843       else if (optimized_type == tls::TLSOPT_NONE)
1844         {
1845           // Relocate the field with the offset of the GOT entry for
1846           // the module index.
1847           unsigned int got_offset;
1848           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1849                         - target->got_size());
1850           Relocate_functions<32, false>::rel32(view, got_offset);
1851           break;
1852         }
1853       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1854                              _("unsupported reloc %u"),
1855                              r_type);
1856       break;
1857
1858     case elfcpp::R_386_TLS_LDO_32:       // Alternate local-dynamic
1859       // This reloc can appear in debugging sections, in which case we
1860       // won't see the TLS_LDM reloc.  The local_dynamic_type field
1861       // tells us this.
1862       if (optimized_type == tls::TLSOPT_TO_LE)
1863         {
1864           gold_assert(tls_segment != NULL);
1865           value -= tls_segment->memsz();
1866         }
1867       Relocate_functions<32, false>::rel32(view, value);
1868       break;
1869
1870     case elfcpp::R_386_TLS_IE:           // Initial-exec
1871     case elfcpp::R_386_TLS_GOTIE:
1872     case elfcpp::R_386_TLS_IE_32:
1873       if (optimized_type == tls::TLSOPT_TO_LE)
1874         {
1875           gold_assert(tls_segment != NULL);
1876           Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1877                                               rel, r_type, value, view,
1878                                               view_size);
1879           break;
1880         }
1881       else if (optimized_type == tls::TLSOPT_NONE)
1882         {
1883           // Relocate the field with the offset of the GOT entry for
1884           // the tp-relative offset of the symbol.
1885           unsigned int got_offset;
1886           if (gsym != NULL)
1887             {
1888               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
1889               got_offset = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
1890             }
1891           else
1892             {
1893               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1894               gold_assert(object->local_has_got_offset(r_sym,
1895                                                        GOT_TYPE_TLS_OFFSET));
1896               got_offset = object->local_got_offset(r_sym,
1897                                                     GOT_TYPE_TLS_OFFSET);
1898             }
1899           // For the R_386_TLS_IE relocation, we need to apply the
1900           // absolute address of the GOT entry.
1901           if (r_type == elfcpp::R_386_TLS_IE)
1902             got_offset += target->got_plt_section()->address();
1903           // All GOT offsets are relative to the end of the GOT.
1904           got_offset -= target->got_size();
1905           Relocate_functions<32, false>::rel32(view, got_offset);
1906           break;
1907         }
1908       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1909                              _("unsupported reloc %u"),
1910                              r_type);
1911       break;
1912
1913     case elfcpp::R_386_TLS_LE:           // Local-exec
1914       // If we're creating a shared library, a dynamic relocation will
1915       // have been created for this location, so do not apply it now.
1916       if (!parameters->options().shared())
1917         {
1918           gold_assert(tls_segment != NULL);
1919           value -= tls_segment->memsz();
1920           Relocate_functions<32, false>::rel32(view, value);
1921         }
1922       break;
1923
1924     case elfcpp::R_386_TLS_LE_32:
1925       // If we're creating a shared library, a dynamic relocation will
1926       // have been created for this location, so do not apply it now.
1927       if (!parameters->options().shared())
1928         {
1929           gold_assert(tls_segment != NULL);
1930           value = tls_segment->memsz() - value;
1931           Relocate_functions<32, false>::rel32(view, value);
1932         }
1933       break;
1934     }
1935 }
1936
1937 // Do a relocation in which we convert a TLS General-Dynamic to a
1938 // Local-Exec.
1939
1940 inline void
1941 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1942                                     size_t relnum,
1943                                     Output_segment* tls_segment,
1944                                     const elfcpp::Rel<32, false>& rel,
1945                                     unsigned int,
1946                                     elfcpp::Elf_types<32>::Elf_Addr value,
1947                                     unsigned char* view,
1948                                     section_size_type view_size)
1949 {
1950   // leal foo(,%reg,1),%eax; call ___tls_get_addr
1951   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1952   // leal foo(%reg),%eax; call ___tls_get_addr
1953   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1954
1955   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1956   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1957
1958   unsigned char op1 = view[-1];
1959   unsigned char op2 = view[-2];
1960
1961   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1962                  op2 == 0x8d || op2 == 0x04);
1963   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1964
1965   int roff = 5;
1966
1967   if (op2 == 0x04)
1968     {
1969       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1970       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1971       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1972                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1973       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1974     }
1975   else
1976     {
1977       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1978                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1979       if (rel.get_r_offset() + 9 < view_size
1980           && view[9] == 0x90)
1981         {
1982           // There is a trailing nop.  Use the size byte subl.
1983           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1984           roff = 6;
1985         }
1986       else
1987         {
1988           // Use the five byte subl.
1989           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1990         }
1991     }
1992
1993   value = tls_segment->memsz() - value;
1994   Relocate_functions<32, false>::rel32(view + roff, value);
1995
1996   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1997   // We can skip it.
1998   this->skip_call_tls_get_addr_ = true;
1999 }
2000
2001 // Do a relocation in which we convert a TLS General-Dynamic to an
2002 // Initial-Exec.
2003
2004 inline void
2005 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2006                                     size_t relnum,
2007                                     Output_segment* tls_segment,
2008                                     const elfcpp::Rel<32, false>& rel,
2009                                     unsigned int,
2010                                     elfcpp::Elf_types<32>::Elf_Addr value,
2011                                     unsigned char* view,
2012                                     section_size_type view_size)
2013 {
2014   // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2015   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2016
2017   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2018   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2019
2020   unsigned char op1 = view[-1];
2021   unsigned char op2 = view[-2];
2022
2023   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2024                  op2 == 0x8d || op2 == 0x04);
2025   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2026
2027   int roff = 5;
2028
2029   // FIXME: For now, support only one form.
2030   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2031                  op1 == 0x8d && op2 == 0x04);
2032
2033   if (op2 == 0x04)
2034     {
2035       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2036       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2037       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2038                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2039       memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2040     }
2041   else
2042     {
2043       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2044                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2045       if (rel.get_r_offset() + 9 < view_size
2046           && view[9] == 0x90)
2047         {
2048           // FIXME: This is not the right instruction sequence.
2049           // There is a trailing nop.  Use the size byte subl.
2050           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2051           roff = 6;
2052         }
2053       else
2054         {
2055           // FIXME: This is not the right instruction sequence.
2056           // Use the five byte subl.
2057           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2058         }
2059     }
2060
2061   value = tls_segment->memsz() - value;
2062   Relocate_functions<32, false>::rel32(view + roff, value);
2063
2064   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2065   // We can skip it.
2066   this->skip_call_tls_get_addr_ = true;
2067 }
2068
2069 // Do a relocation in which we convert a TLS Local-Dynamic to a
2070 // Local-Exec.
2071
2072 inline void
2073 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2074                                     size_t relnum,
2075                                     Output_segment*,
2076                                     const elfcpp::Rel<32, false>& rel,
2077                                     unsigned int,
2078                                     elfcpp::Elf_types<32>::Elf_Addr,
2079                                     unsigned char* view,
2080                                     section_size_type view_size)
2081 {
2082   // leal foo(%reg), %eax; call ___tls_get_addr
2083   // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2084
2085   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2086   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2087
2088   // FIXME: Does this test really always pass?
2089   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2090                  view[-2] == 0x8d && view[-1] == 0x83);
2091
2092   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2093
2094   memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2095
2096   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2097   // We can skip it.
2098   this->skip_call_tls_get_addr_ = true;
2099 }
2100
2101 // Do a relocation in which we convert a TLS Initial-Exec to a
2102 // Local-Exec.
2103
2104 inline void
2105 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2106                                     size_t relnum,
2107                                     Output_segment* tls_segment,
2108                                     const elfcpp::Rel<32, false>& rel,
2109                                     unsigned int r_type,
2110                                     elfcpp::Elf_types<32>::Elf_Addr value,
2111                                     unsigned char* view,
2112                                     section_size_type view_size)
2113 {
2114   // We have to actually change the instructions, which means that we
2115   // need to examine the opcodes to figure out which instruction we
2116   // are looking at.
2117   if (r_type == elfcpp::R_386_TLS_IE)
2118     {
2119       // movl %gs:XX,%eax  ==>  movl $YY,%eax
2120       // movl %gs:XX,%reg  ==>  movl $YY,%reg
2121       // addl %gs:XX,%reg  ==>  addl $YY,%reg
2122       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2123       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2124
2125       unsigned char op1 = view[-1];
2126       if (op1 == 0xa1)
2127         {
2128           // movl XX,%eax  ==>  movl $YY,%eax
2129           view[-1] = 0xb8;
2130         }
2131       else
2132         {
2133           tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2134
2135           unsigned char op2 = view[-2];
2136           if (op2 == 0x8b)
2137             {
2138               // movl XX,%reg  ==>  movl $YY,%reg
2139               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2140                              (op1 & 0xc7) == 0x05);
2141               view[-2] = 0xc7;
2142               view[-1] = 0xc0 | ((op1 >> 3) & 7);
2143             }
2144           else if (op2 == 0x03)
2145             {
2146               // addl XX,%reg  ==>  addl $YY,%reg
2147               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2148                              (op1 & 0xc7) == 0x05);
2149               view[-2] = 0x81;
2150               view[-1] = 0xc0 | ((op1 >> 3) & 7);
2151             }
2152           else
2153             tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2154         }
2155     }
2156   else
2157     {
2158       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
2159       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
2160       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
2161       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2162       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2163
2164       unsigned char op1 = view[-1];
2165       unsigned char op2 = view[-2];
2166       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2167                      (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2168       if (op2 == 0x8b)
2169         {
2170           // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
2171           view[-2] = 0xc7;
2172           view[-1] = 0xc0 | ((op1 >> 3) & 7);
2173         }
2174       else if (op2 == 0x2b)
2175         {
2176           // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
2177           view[-2] = 0x81;
2178           view[-1] = 0xe8 | ((op1 >> 3) & 7);
2179         }
2180       else if (op2 == 0x03)
2181         {
2182           // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
2183           view[-2] = 0x81;
2184           view[-1] = 0xc0 | ((op1 >> 3) & 7);
2185         }
2186       else
2187         tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2188     }
2189
2190   value = tls_segment->memsz() - value;
2191   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2192     value = - value;
2193
2194   Relocate_functions<32, false>::rel32(view, value);
2195 }
2196
2197 // Relocate section data.
2198
2199 void
2200 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2201                               unsigned int sh_type,
2202                               const unsigned char* prelocs,
2203                               size_t reloc_count,
2204                               Output_section* output_section,
2205                               bool needs_special_offset_handling,
2206                               unsigned char* view,
2207                               elfcpp::Elf_types<32>::Elf_Addr address,
2208                               section_size_type view_size)
2209 {
2210   gold_assert(sh_type == elfcpp::SHT_REL);
2211
2212   gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2213                          Target_i386::Relocate>(
2214     relinfo,
2215     this,
2216     prelocs,
2217     reloc_count,
2218     output_section,
2219     needs_special_offset_handling,
2220     view,
2221     address,
2222     view_size);
2223 }
2224
2225 // Return the size of a relocation while scanning during a relocatable
2226 // link.
2227
2228 unsigned int
2229 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2230     unsigned int r_type,
2231     Relobj* object)
2232 {
2233   switch (r_type)
2234     {
2235     case elfcpp::R_386_NONE:
2236     case elfcpp::R_386_GNU_VTINHERIT:
2237     case elfcpp::R_386_GNU_VTENTRY:
2238     case elfcpp::R_386_TLS_GD:            // Global-dynamic
2239     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
2240     case elfcpp::R_386_TLS_DESC_CALL:
2241     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
2242     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
2243     case elfcpp::R_386_TLS_IE:            // Initial-exec
2244     case elfcpp::R_386_TLS_IE_32:
2245     case elfcpp::R_386_TLS_GOTIE:
2246     case elfcpp::R_386_TLS_LE:            // Local-exec
2247     case elfcpp::R_386_TLS_LE_32:
2248       return 0;
2249
2250     case elfcpp::R_386_32:
2251     case elfcpp::R_386_PC32:
2252     case elfcpp::R_386_GOT32:
2253     case elfcpp::R_386_PLT32:
2254     case elfcpp::R_386_GOTOFF:
2255     case elfcpp::R_386_GOTPC:
2256      return 4;
2257
2258     case elfcpp::R_386_16:
2259     case elfcpp::R_386_PC16:
2260       return 2;
2261
2262     case elfcpp::R_386_8:
2263     case elfcpp::R_386_PC8:
2264       return 1;
2265
2266       // These are relocations which should only be seen by the
2267       // dynamic linker, and should never be seen here.
2268     case elfcpp::R_386_COPY:
2269     case elfcpp::R_386_GLOB_DAT:
2270     case elfcpp::R_386_JUMP_SLOT:
2271     case elfcpp::R_386_RELATIVE:
2272     case elfcpp::R_386_TLS_TPOFF:
2273     case elfcpp::R_386_TLS_DTPMOD32:
2274     case elfcpp::R_386_TLS_DTPOFF32:
2275     case elfcpp::R_386_TLS_TPOFF32:
2276     case elfcpp::R_386_TLS_DESC:
2277       object->error(_("unexpected reloc %u in object file"), r_type);
2278       return 0;
2279
2280     case elfcpp::R_386_32PLT:
2281     case elfcpp::R_386_TLS_GD_32:
2282     case elfcpp::R_386_TLS_GD_PUSH:
2283     case elfcpp::R_386_TLS_GD_CALL:
2284     case elfcpp::R_386_TLS_GD_POP:
2285     case elfcpp::R_386_TLS_LDM_32:
2286     case elfcpp::R_386_TLS_LDM_PUSH:
2287     case elfcpp::R_386_TLS_LDM_CALL:
2288     case elfcpp::R_386_TLS_LDM_POP:
2289     case elfcpp::R_386_USED_BY_INTEL_200:
2290     default:
2291       object->error(_("unsupported reloc %u in object file"), r_type);
2292       return 0;
2293     }
2294 }
2295
2296 // Scan the relocs during a relocatable link.
2297
2298 void
2299 Target_i386::scan_relocatable_relocs(const General_options& options,
2300                                      Symbol_table* symtab,
2301                                      Layout* layout,
2302                                      Sized_relobj<32, false>* object,
2303                                      unsigned int data_shndx,
2304                                      unsigned int sh_type,
2305                                      const unsigned char* prelocs,
2306                                      size_t reloc_count,
2307                                      Output_section* output_section,
2308                                      bool needs_special_offset_handling,
2309                                      size_t local_symbol_count,
2310                                      const unsigned char* plocal_symbols,
2311                                      Relocatable_relocs* rr)
2312 {
2313   gold_assert(sh_type == elfcpp::SHT_REL);
2314
2315   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2316     Relocatable_size_for_reloc> Scan_relocatable_relocs;
2317
2318   gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2319       Scan_relocatable_relocs>(
2320     options,
2321     symtab,
2322     layout,
2323     object,
2324     data_shndx,
2325     prelocs,
2326     reloc_count,
2327     output_section,
2328     needs_special_offset_handling,
2329     local_symbol_count,
2330     plocal_symbols,
2331     rr);
2332 }
2333
2334 // Relocate a section during a relocatable link.
2335
2336 void
2337 Target_i386::relocate_for_relocatable(
2338     const Relocate_info<32, false>* relinfo,
2339     unsigned int sh_type,
2340     const unsigned char* prelocs,
2341     size_t reloc_count,
2342     Output_section* output_section,
2343     off_t offset_in_output_section,
2344     const Relocatable_relocs* rr,
2345     unsigned char* view,
2346     elfcpp::Elf_types<32>::Elf_Addr view_address,
2347     section_size_type view_size,
2348     unsigned char* reloc_view,
2349     section_size_type reloc_view_size)
2350 {
2351   gold_assert(sh_type == elfcpp::SHT_REL);
2352
2353   gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2354     relinfo,
2355     prelocs,
2356     reloc_count,
2357     output_section,
2358     offset_in_output_section,
2359     rr,
2360     view,
2361     view_address,
2362     view_size,
2363     reloc_view,
2364     reloc_view_size);
2365 }
2366
2367 // Return the value to use for a dynamic which requires special
2368 // treatment.  This is how we support equality comparisons of function
2369 // pointers across shared library boundaries, as described in the
2370 // processor specific ABI supplement.
2371
2372 uint64_t
2373 Target_i386::do_dynsym_value(const Symbol* gsym) const
2374 {
2375   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2376   return this->plt_section()->address() + gsym->plt_offset();
2377 }
2378
2379 // Return a string used to fill a code section with nops to take up
2380 // the specified length.
2381
2382 std::string
2383 Target_i386::do_code_fill(section_size_type length) const
2384 {
2385   if (length >= 16)
2386     {
2387       // Build a jmp instruction to skip over the bytes.
2388       unsigned char jmp[5];
2389       jmp[0] = 0xe9;
2390       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2391       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2392               + std::string(length - 5, '\0'));
2393     }
2394
2395   // Nop sequences of various lengths.
2396   const char nop1[1] = { 0x90 };                   // nop
2397   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
2398   const char nop3[3] = { 0x8d, 0x76, 0x00 };       // leal 0(%esi),%esi
2399   const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00};  // leal 0(%esi,1),%esi
2400   const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26,   // nop
2401                          0x00 };                   // leal 0(%esi,1),%esi
2402   const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00,   // leal 0L(%esi),%esi
2403                          0x00, 0x00 };
2404   const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00,   // leal 0L(%esi,1),%esi
2405                          0x00, 0x00, 0x00 };
2406   const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26,   // nop
2407                          0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2408   const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc,   // movl %esi,%esi
2409                          0x27, 0x00, 0x00, 0x00,   // leal 0L(%edi,1),%edi
2410                          0x00 };
2411   const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2412                            0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2413                            0x00, 0x00 };
2414   const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2415                            0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2416                            0x00, 0x00, 0x00 };
2417   const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2418                            0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2419                            0x00, 0x00, 0x00, 0x00 };
2420   const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2421                            0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2422                            0x27, 0x00, 0x00, 0x00,
2423                            0x00 };
2424   const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2425                            0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2426                            0xbc, 0x27, 0x00, 0x00,
2427                            0x00, 0x00 };
2428   const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2429                            0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2430                            0x90, 0x90, 0x90, 0x90,
2431                            0x90, 0x90, 0x90 };
2432
2433   const char* nops[16] = {
2434     NULL,
2435     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2436     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2437   };
2438
2439   return std::string(nops[length], length);
2440 }
2441
2442 // The selector for i386 object files.
2443
2444 class Target_selector_i386 : public Target_selector
2445 {
2446 public:
2447   Target_selector_i386()
2448     : Target_selector(elfcpp::EM_386, 32, false, "elf32-i386")
2449   { }
2450
2451   Target*
2452   do_instantiate_target()
2453   { return new Target_i386(); }
2454 };
2455
2456 Target_selector_i386 target_selector_i386;
2457
2458 } // End anonymous namespace.