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