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