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