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