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