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