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