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