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