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