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