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