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