From Craig Silverstein: support local GOT relocs for i386.
[platform/upstream/binutils.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "i386.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39
40 namespace
41 {
42
43 using namespace gold;
44
45 class Output_data_plt_i386;
46
47 // The i386 target class.
48 // TLS info comes from
49 //   http://people.redhat.com/drepper/tls.pdf
50 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
51
52 class Target_i386 : public Sized_target<32, false>
53 {
54  public:
55   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
56
57   Target_i386()
58     : Sized_target<32, false>(&i386_info),
59       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
60       copy_relocs_(NULL), dynbss_(NULL)
61   { }
62
63   // Scan the relocations to look for symbol adjustments.
64   void
65   scan_relocs(const General_options& options,
66               Symbol_table* symtab,
67               Layout* layout,
68               Sized_relobj<32, false>* object,
69               unsigned int data_shndx,
70               unsigned int sh_type,
71               const unsigned char* prelocs,
72               size_t reloc_count,
73               size_t local_symbol_count,
74               const unsigned char* plocal_symbols,
75               Symbol** global_symbols);
76
77   // Finalize the sections.
78   void
79   do_finalize_sections(Layout*);
80
81   // Return the value to use for a dynamic which requires special
82   // treatment.
83   uint64_t
84   do_dynsym_value(const Symbol*) const;
85
86   // Relocate a section.
87   void
88   relocate_section(const Relocate_info<32, false>*,
89                    unsigned int sh_type,
90                    const unsigned char* prelocs,
91                    size_t reloc_count,
92                    unsigned char* view,
93                    elfcpp::Elf_types<32>::Elf_Addr view_address,
94                    off_t view_size);
95
96   // Return a string used to fill a code section with nops.
97   std::string
98   do_code_fill(off_t length);
99
100  private:
101   // The class which scans relocations.
102   struct Scan
103   {
104     inline void
105     local(const General_options& options, Symbol_table* symtab,
106           Layout* layout, Target_i386* target,
107           Sized_relobj<32, false>* object,
108           unsigned int data_shndx,
109           const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
110           const elfcpp::Sym<32, false>& lsym);
111
112     inline void
113     global(const General_options& options, Symbol_table* symtab,
114            Layout* layout, Target_i386* target,
115            Sized_relobj<32, false>* object,
116            unsigned int data_shndx,
117            const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
118            Symbol* gsym);
119
120     static void
121     unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
122
123     static void
124     unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
125                              Symbol*);
126   };
127
128   // The class which implements relocation.
129   class Relocate
130   {
131    public:
132     Relocate()
133       : skip_call_tls_get_addr_(false),
134         local_dynamic_type_(LOCAL_DYNAMIC_NONE)
135     { }
136
137     ~Relocate()
138     {
139       if (this->skip_call_tls_get_addr_)
140         {
141           // FIXME: This needs to specify the location somehow.
142           gold_error(_("missing expected TLS relocation"));
143         }
144     }
145
146     // Do a relocation.  Return false if the caller should not issue
147     // any warnings about this relocation.
148     inline bool
149     relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
150              const elfcpp::Rel<32, false>&,
151              unsigned int r_type, const Sized_symbol<32>*,
152              const Symbol_value<32>*,
153              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
154              off_t);
155
156    private:
157     // Do a TLS relocation.
158     inline void
159     relocate_tls(const Relocate_info<32, false>*, size_t relnum,
160                  const elfcpp::Rel<32, false>&,
161                  unsigned int r_type, const Sized_symbol<32>*,
162                  const Symbol_value<32>*,
163                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
164
165     // Do a TLS Initial-Exec to Local-Exec transition.
166     static inline void
167     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
168                  Output_segment* tls_segment,
169                  const elfcpp::Rel<32, false>&, unsigned int r_type,
170                  elfcpp::Elf_types<32>::Elf_Addr value,
171                  unsigned char* view,
172                  off_t view_size);
173
174     // Do a TLS General-Dynamic to Local-Exec transition.
175     inline void
176     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
177                  Output_segment* tls_segment,
178                  const elfcpp::Rel<32, false>&, unsigned int r_type,
179                  elfcpp::Elf_types<32>::Elf_Addr value,
180                  unsigned char* view,
181                  off_t view_size);
182
183     // Do a TLS Local-Dynamic to Local-Exec transition.
184     inline void
185     tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
186                  Output_segment* tls_segment,
187                  const elfcpp::Rel<32, false>&, unsigned int r_type,
188                  elfcpp::Elf_types<32>::Elf_Addr value,
189                  unsigned char* view,
190                  off_t view_size);
191
192     // We need to keep track of which type of local dynamic relocation
193     // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
194     enum Local_dynamic_type
195     {
196       LOCAL_DYNAMIC_NONE,
197       LOCAL_DYNAMIC_SUN,
198       LOCAL_DYNAMIC_GNU
199     };
200
201     // This is set if we should skip the next reloc, which should be a
202     // PLT32 reloc against ___tls_get_addr.
203     bool skip_call_tls_get_addr_;
204     // The type of local dynamic relocation we have seen in the section
205     // being relocated, if any.
206     Local_dynamic_type local_dynamic_type_;
207   };
208
209   // Adjust TLS relocation type based on the options and whether this
210   // is a local symbol.
211   static tls::Tls_optimization
212   optimize_tls_reloc(bool is_final, int r_type);
213
214   // Get the GOT section, creating it if necessary.
215   Output_data_got<32, false>*
216   got_section(Symbol_table*, Layout*);
217
218   // Create a PLT entry for a global symbol.
219   void
220   make_plt_entry(Symbol_table*, Layout*, Symbol*);
221
222   // Get the PLT section.
223   const Output_data_plt_i386*
224   plt_section() const
225   {
226     gold_assert(this->plt_ != NULL);
227     return this->plt_;
228   }
229
230   // Get the dynamic reloc section, creating it if necessary.
231   Reloc_section*
232   rel_dyn_section(Layout*);
233
234   // Copy a relocation against a global symbol.
235   void
236   copy_reloc(const General_options*, Symbol_table*, Layout*,
237              Sized_relobj<32, false>*, unsigned int,
238              Symbol*, const elfcpp::Rel<32, false>&);
239
240   // Information about this specific target which we pass to the
241   // general Target structure.
242   static const Target::Target_info i386_info;
243
244   // The GOT section.
245   Output_data_got<32, false>* got_;
246   // The PLT section.
247   Output_data_plt_i386* plt_;
248   // The GOT PLT section.
249   Output_data_space* got_plt_;
250   // The dynamic reloc section.
251   Reloc_section* rel_dyn_;
252   // Relocs saved to avoid a COPY reloc.
253   Copy_relocs<32, false>* copy_relocs_;
254   // Space for variables copied with a COPY reloc.
255   Output_data_space* dynbss_;
256 };
257
258 const Target::Target_info Target_i386::i386_info =
259 {
260   32,                   // size
261   false,                // is_big_endian
262   elfcpp::EM_386,       // machine_code
263   false,                // has_make_symbol
264   false,                // has_resolve
265   true,                 // has_code_fill
266   "/usr/lib/libc.so.1", // dynamic_linker
267   0x08048000,           // text_segment_address
268   0x1000,               // abi_pagesize
269   0x1000                // common_pagesize
270 };
271
272 // Get the GOT section, creating it if necessary.
273
274 Output_data_got<32, false>*
275 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
276 {
277   if (this->got_ == NULL)
278     {
279       gold_assert(symtab != NULL && layout != NULL);
280
281       this->got_ = new Output_data_got<32, false>();
282
283       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
284                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
285                                       this->got_);
286
287       // The old GNU linker creates a .got.plt section.  We just
288       // create another set of data in the .got section.  Note that we
289       // always create a PLT if we create a GOT, although the PLT
290       // might be empty.
291       this->got_plt_ = new Output_data_space(4);
292       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
293                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
294                                       this->got_plt_);
295
296       // The first three entries are reserved.
297       this->got_plt_->set_space_size(3 * 4);
298
299       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
300       symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
301                                     this->got_plt_,
302                                     0, 0, elfcpp::STT_OBJECT,
303                                     elfcpp::STB_LOCAL,
304                                     elfcpp::STV_HIDDEN, 0,
305                                     false, false);
306     }
307
308   return this->got_;
309 }
310
311 // Get the dynamic reloc section, creating it if necessary.
312
313 Target_i386::Reloc_section*
314 Target_i386::rel_dyn_section(Layout* layout)
315 {
316   if (this->rel_dyn_ == NULL)
317     {
318       gold_assert(layout != NULL);
319       this->rel_dyn_ = new Reloc_section();
320       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
321                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
322     }
323   return this->rel_dyn_;
324 }
325
326 // A class to handle the PLT data.
327
328 class Output_data_plt_i386 : public Output_section_data
329 {
330  public:
331   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
332
333   Output_data_plt_i386(Layout*, Output_data_space*);
334
335   // Add an entry to the PLT.
336   void
337   add_entry(Symbol* gsym);
338
339   // Return the .rel.plt section data.
340   const Reloc_section*
341   rel_plt() const
342   { return this->rel_; }
343
344  protected:
345   void
346   do_adjust_output_section(Output_section* os);
347
348  private:
349   // The size of an entry in the PLT.
350   static const int plt_entry_size = 16;
351
352   // The first entry in the PLT for an executable.
353   static unsigned char exec_first_plt_entry[plt_entry_size];
354
355   // The first entry in the PLT for a shared object.
356   static unsigned char dyn_first_plt_entry[plt_entry_size];
357
358   // Other entries in the PLT for an executable.
359   static unsigned char exec_plt_entry[plt_entry_size];
360
361   // Other entries in the PLT for a shared object.
362   static unsigned char dyn_plt_entry[plt_entry_size];
363
364   // Set the final size.
365   void
366   do_set_address(uint64_t, off_t)
367   { this->set_data_size((this->count_ + 1) * plt_entry_size); }
368
369   // Write out the PLT data.
370   void
371   do_write(Output_file*);
372
373   // The reloc section.
374   Reloc_section* rel_;
375   // The .got.plt section.
376   Output_data_space* got_plt_;
377   // The number of PLT entries.
378   unsigned int count_;
379 };
380
381 // Create the PLT section.  The ordinary .got section is an argument,
382 // since we need to refer to the start.  We also create our own .got
383 // section just for PLT entries.
384
385 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
386                                            Output_data_space* got_plt)
387   : Output_section_data(4), got_plt_(got_plt), count_(0)
388 {
389   this->rel_ = new Reloc_section();
390   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
391                                   elfcpp::SHF_ALLOC, this->rel_);
392 }
393
394 void
395 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
396 {
397   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
398   // linker, and so do we.
399   os->set_entsize(4);
400 }
401
402 // Add an entry to the PLT.
403
404 void
405 Output_data_plt_i386::add_entry(Symbol* gsym)
406 {
407   gold_assert(!gsym->has_plt_offset());
408
409   // Note that when setting the PLT offset we skip the initial
410   // reserved PLT entry.
411   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
412
413   ++this->count_;
414
415   off_t got_offset = this->got_plt_->data_size();
416
417   // Every PLT entry needs a GOT entry which points back to the PLT
418   // entry (this will be changed by the dynamic linker, normally
419   // lazily when the function is called).
420   this->got_plt_->set_space_size(got_offset + 4);
421
422   // Every PLT entry needs a reloc.
423   gsym->set_needs_dynsym_entry();
424   this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
425                          got_offset);
426
427   // Note that we don't need to save the symbol.  The contents of the
428   // PLT are independent of which symbols are used.  The symbols only
429   // appear in the relocations.
430 }
431
432 // The first entry in the PLT for an executable.
433
434 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
435 {
436   0xff, 0x35,   // pushl contents of memory address
437   0, 0, 0, 0,   // replaced with address of .got + 4
438   0xff, 0x25,   // jmp indirect
439   0, 0, 0, 0,   // replaced with address of .got + 8
440   0, 0, 0, 0    // unused
441 };
442
443 // The first entry in the PLT for a shared object.
444
445 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
446 {
447   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
448   0xff, 0xa3, 8, 0, 0, 0,       // jmp *8(%ebx)
449   0, 0, 0, 0                    // unused
450 };
451
452 // Subsequent entries in the PLT for an executable.
453
454 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
455 {
456   0xff, 0x25,   // jmp indirect
457   0, 0, 0, 0,   // replaced with address of symbol in .got
458   0x68,         // pushl immediate
459   0, 0, 0, 0,   // replaced with offset into relocation table
460   0xe9,         // jmp relative
461   0, 0, 0, 0    // replaced with offset to start of .plt
462 };
463
464 // Subsequent entries in the PLT for a shared object.
465
466 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
467 {
468   0xff, 0xa3,   // jmp *offset(%ebx)
469   0, 0, 0, 0,   // replaced with offset of symbol in .got
470   0x68,         // pushl immediate
471   0, 0, 0, 0,   // replaced with offset into relocation table
472   0xe9,         // jmp relative
473   0, 0, 0, 0    // replaced with offset to start of .plt
474 };
475
476 // Write out the PLT.  This uses the hand-coded instructions above,
477 // and adjusts them as needed.  This is all specified by the i386 ELF
478 // Processor Supplement.
479
480 void
481 Output_data_plt_i386::do_write(Output_file* of)
482 {
483   const off_t offset = this->offset();
484   const off_t oview_size = this->data_size();
485   unsigned char* const oview = of->get_output_view(offset, oview_size);
486
487   const off_t got_file_offset = this->got_plt_->offset();
488   const off_t got_size = this->got_plt_->data_size();
489   unsigned char* const got_view = of->get_output_view(got_file_offset,
490                                                       got_size);
491
492   unsigned char* pov = oview;
493
494   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
495   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
496
497   if (parameters->output_is_shared())
498     memcpy(pov, dyn_first_plt_entry, plt_entry_size);
499   else
500     {
501       memcpy(pov, exec_first_plt_entry, plt_entry_size);
502       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
503       elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
504     }
505   pov += plt_entry_size;
506
507   unsigned char* got_pov = got_view;
508
509   memset(got_pov, 0, 12);
510   got_pov += 12;
511
512   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
513
514   unsigned int plt_offset = plt_entry_size;
515   unsigned int plt_rel_offset = 0;
516   unsigned int got_offset = 12;
517   const unsigned int count = this->count_;
518   for (unsigned int i = 0;
519        i < count;
520        ++i,
521          pov += plt_entry_size,
522          got_pov += 4,
523          plt_offset += plt_entry_size,
524          plt_rel_offset += rel_size,
525          got_offset += 4)
526     {
527       // Set and adjust the PLT entry itself.
528
529       if (parameters->output_is_shared())
530         {
531           memcpy(pov, dyn_plt_entry, plt_entry_size);
532           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
533         }
534       else
535         {
536           memcpy(pov, exec_plt_entry, plt_entry_size);
537           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
538                                                       (got_address
539                                                        + got_offset));
540         }
541
542       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
543       elfcpp::Swap<32, false>::writeval(pov + 12,
544                                         - (plt_offset + plt_entry_size));
545
546       // Set the entry in the GOT.
547       elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
548     }
549
550   gold_assert(pov - oview == oview_size);
551   gold_assert(got_pov - got_view == got_size);
552
553   of->write_output_view(offset, oview_size, oview);
554   of->write_output_view(got_file_offset, got_size, got_view);
555 }
556
557 // Create a PLT entry for a global symbol.
558
559 void
560 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
561 {
562   if (gsym->has_plt_offset())
563     return;
564
565   if (this->plt_ == NULL)
566     {
567       // Create the GOT sections first.
568       this->got_section(symtab, layout);
569
570       this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
571       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
572                                       (elfcpp::SHF_ALLOC
573                                        | elfcpp::SHF_EXECINSTR),
574                                       this->plt_);
575     }
576
577   this->plt_->add_entry(gsym);
578 }
579
580 // Handle a relocation against a non-function symbol defined in a
581 // dynamic object.  The traditional way to handle this is to generate
582 // a COPY relocation to copy the variable at runtime from the shared
583 // object into the executable's data segment.  However, this is
584 // undesirable in general, as if the size of the object changes in the
585 // dynamic object, the executable will no longer work correctly.  If
586 // this relocation is in a writable section, then we can create a
587 // dynamic reloc and the dynamic linker will resolve it to the correct
588 // address at runtime.  However, we do not want do that if the
589 // relocation is in a read-only section, as it would prevent the
590 // readonly segment from being shared.  And if we have to eventually
591 // generate a COPY reloc, then any dynamic relocations will be
592 // useless.  So this means that if this is a writable section, we need
593 // to save the relocation until we see whether we have to create a
594 // COPY relocation for this symbol for any other relocation.
595
596 void
597 Target_i386::copy_reloc(const General_options* options,
598                         Symbol_table* symtab,
599                         Layout* layout,
600                         Sized_relobj<32, false>* object,
601                         unsigned int data_shndx, Symbol* gsym,
602                         const elfcpp::Rel<32, false>& rel)
603 {
604   Sized_symbol<32>* ssym;
605   ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
606                                                         SELECT_SIZE(32));
607
608   if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
609                                                data_shndx, ssym))
610     {
611       // So far we do not need a COPY reloc.  Save this relocation.
612       // If it turns out that we never need a COPY reloc for this
613       // symbol, then we will emit the relocation.
614       if (this->copy_relocs_ == NULL)
615         this->copy_relocs_ = new Copy_relocs<32, false>();
616       this->copy_relocs_->save(ssym, object, data_shndx, rel);
617     }
618   else
619     {
620       // Allocate space for this symbol in the .bss section.
621
622       elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
623
624       // There is no defined way to determine the required alignment
625       // of the symbol.  We pick the alignment based on the size.  We
626       // set an arbitrary maximum of 256.
627       unsigned int align;
628       for (align = 1; align < 512; align <<= 1)
629         if ((symsize & align) != 0)
630           break;
631
632       if (this->dynbss_ == NULL)
633         {
634           this->dynbss_ = new Output_data_space(align);
635           layout->add_output_section_data(".bss",
636                                           elfcpp::SHT_NOBITS,
637                                           (elfcpp::SHF_ALLOC
638                                            | elfcpp::SHF_WRITE),
639                                           this->dynbss_);
640         }
641
642       Output_data_space* dynbss = this->dynbss_;
643
644       if (align > dynbss->addralign())
645         dynbss->set_space_alignment(align);
646
647       off_t dynbss_size = dynbss->data_size();
648       dynbss_size = align_address(dynbss_size, align);
649       off_t offset = dynbss_size;
650       dynbss->set_space_size(dynbss_size + symsize);
651
652       // Define the symbol in the .dynbss section.
653       symtab->define_in_output_data(this, ssym->name(), ssym->version(),
654                                     dynbss, offset, symsize, ssym->type(),
655                                     ssym->binding(), ssym->visibility(),
656                                     ssym->nonvis(), false, false);
657
658       // Add the COPY reloc.
659       ssym->set_needs_dynsym_entry();
660       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
661       rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
662     }
663 }
664
665 // Optimize the TLS relocation type based on what we know about the
666 // symbol.  IS_FINAL is true if the final address of this symbol is
667 // known at link time.
668
669 tls::Tls_optimization
670 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
671 {
672   // If we are generating a shared library, then we can't do anything
673   // in the linker.
674   if (parameters->output_is_shared())
675     return tls::TLSOPT_NONE;
676
677   switch (r_type)
678     {
679     case elfcpp::R_386_TLS_GD:
680     case elfcpp::R_386_TLS_GOTDESC:
681     case elfcpp::R_386_TLS_DESC_CALL:
682       // These are General-Dynamic which permits fully general TLS
683       // access.  Since we know that we are generating an executable,
684       // we can convert this to Initial-Exec.  If we also know that
685       // this is a local symbol, we can further switch to Local-Exec.
686       if (is_final)
687         return tls::TLSOPT_TO_LE;
688       return tls::TLSOPT_TO_IE;
689
690     case elfcpp::R_386_TLS_LDM:
691       // This is Local-Dynamic, which refers to a local symbol in the
692       // dynamic TLS block.  Since we know that we generating an
693       // executable, we can switch to Local-Exec.
694       return tls::TLSOPT_TO_LE;
695
696     case elfcpp::R_386_TLS_LDO_32:
697       // Another type of Local-Dynamic relocation.
698       return tls::TLSOPT_TO_LE;
699
700     case elfcpp::R_386_TLS_IE:
701     case elfcpp::R_386_TLS_GOTIE:
702     case elfcpp::R_386_TLS_IE_32:
703       // These are Initial-Exec relocs which get the thread offset
704       // from the GOT.  If we know that we are linking against the
705       // local symbol, we can switch to Local-Exec, which links the
706       // thread offset into the instruction.
707       if (is_final)
708         return tls::TLSOPT_TO_LE;
709       return tls::TLSOPT_NONE;
710
711     case elfcpp::R_386_TLS_LE:
712     case elfcpp::R_386_TLS_LE_32:
713       // When we already have Local-Exec, there is nothing further we
714       // can do.
715       return tls::TLSOPT_NONE;
716
717     default:
718       gold_unreachable();
719     }
720 }
721
722 // Report an unsupported relocation against a local symbol.
723
724 void
725 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
726                                            unsigned int r_type)
727 {
728   gold_error(_("%s: unsupported reloc %u against local symbol"),
729              object->name().c_str(), r_type);
730 }
731
732 // Scan a relocation for a local symbol.
733
734 inline void
735 Target_i386::Scan::local(const General_options&,
736                          Symbol_table* symtab,
737                          Layout* layout,
738                          Target_i386* target,
739                          Sized_relobj<32, false>* object,
740                          unsigned int data_shndx,
741                          const elfcpp::Rel<32, false>& reloc,
742                          unsigned int r_type,
743                          const elfcpp::Sym<32, false>&)
744 {
745   switch (r_type)
746     {
747     case elfcpp::R_386_NONE:
748     case elfcpp::R_386_GNU_VTINHERIT:
749     case elfcpp::R_386_GNU_VTENTRY:
750       break;
751
752     case elfcpp::R_386_32:
753     case elfcpp::R_386_16:
754     case elfcpp::R_386_8:
755       // FIXME: If we are generating a shared object we need to copy
756       // this relocation into the object.
757       gold_assert(!parameters->output_is_shared());
758       break;
759
760     case elfcpp::R_386_PC32:
761     case elfcpp::R_386_PC16:
762     case elfcpp::R_386_PC8:
763       break;
764
765     case elfcpp::R_386_GOTOFF:
766     case elfcpp::R_386_GOTPC:
767       // We need a GOT section.
768       target->got_section(symtab, layout);
769       break;
770
771     case elfcpp::R_386_GOT32:
772       {
773         // The symbol requires a GOT entry.
774         Output_data_got<32, false>* got = target->got_section(symtab, layout);
775         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
776         if (got->add_local(object, r_sym))
777           {
778             // If we are generating a shared object, we need to add a
779             // dynamic RELATIVE relocation for this symbol.
780             if (parameters->output_is_shared())
781               {
782                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
783                 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE,
784                                    data_shndx, reloc.get_r_offset());
785               }
786           }
787       }
788       break;
789
790       // These are relocations which should only be seen by the
791       // dynamic linker, and should never be seen here.
792     case elfcpp::R_386_COPY:
793     case elfcpp::R_386_GLOB_DAT:
794     case elfcpp::R_386_JUMP_SLOT:
795     case elfcpp::R_386_RELATIVE:
796     case elfcpp::R_386_TLS_TPOFF:
797     case elfcpp::R_386_TLS_DTPMOD32:
798     case elfcpp::R_386_TLS_DTPOFF32:
799     case elfcpp::R_386_TLS_TPOFF32:
800     case elfcpp::R_386_TLS_DESC:
801       gold_error(_("%s: unexpected reloc %u in object file"),
802                  object->name().c_str(), r_type);
803       break;
804
805       // These are initial TLS relocs, which are expected when
806       // linking.
807     case elfcpp::R_386_TLS_IE:
808     case elfcpp::R_386_TLS_GOTIE:
809     case elfcpp::R_386_TLS_LE:
810     case elfcpp::R_386_TLS_GD:
811     case elfcpp::R_386_TLS_LDM:
812     case elfcpp::R_386_TLS_LDO_32:
813     case elfcpp::R_386_TLS_IE_32:
814     case elfcpp::R_386_TLS_LE_32:
815     case elfcpp::R_386_TLS_GOTDESC:
816     case elfcpp::R_386_TLS_DESC_CALL:
817       {
818         bool output_is_shared = parameters->output_is_shared();
819         const tls::Tls_optimization optimized_type
820             = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
821         switch (r_type)
822           {
823           case elfcpp::R_386_TLS_LE:
824           case elfcpp::R_386_TLS_LE_32:
825             // FIXME: If generating a shared object, we need to copy
826             // this relocation into the object.
827             gold_assert(!output_is_shared);
828             break;
829
830           case elfcpp::R_386_TLS_IE:
831           case elfcpp::R_386_TLS_IE_32:
832           case elfcpp::R_386_TLS_GOTIE:
833             // FIXME: If not relaxing to LE, we need to generate a
834             // TPOFF or TPOFF32 reloc.
835             if (optimized_type != tls::TLSOPT_TO_LE)
836               unsupported_reloc_local(object, r_type);
837             break;
838
839           case elfcpp::R_386_TLS_LDM:
840             // FIXME: If not relaxing to LE, we need to generate a
841             // DTPMOD32 reloc.
842             if (optimized_type != tls::TLSOPT_TO_LE)
843               unsupported_reloc_local(object, r_type);
844             break;
845
846           case elfcpp::R_386_TLS_LDO_32:
847             break;
848
849           case elfcpp::R_386_TLS_GD:
850           case elfcpp::R_386_TLS_GOTDESC:
851           case elfcpp::R_386_TLS_DESC_CALL:
852             // FIXME: If not relaxing to LE, we need to generate
853             // DTPMOD32 and DTPOFF32 relocs.
854             if (optimized_type != tls::TLSOPT_TO_LE)
855               unsupported_reloc_local(object, r_type);
856             break;
857
858           default:
859             gold_unreachable();
860           }
861       }
862       break;
863
864     case elfcpp::R_386_PLT32:
865     case elfcpp::R_386_32PLT:
866     case elfcpp::R_386_TLS_GD_32:
867     case elfcpp::R_386_TLS_GD_PUSH:
868     case elfcpp::R_386_TLS_GD_CALL:
869     case elfcpp::R_386_TLS_GD_POP:
870     case elfcpp::R_386_TLS_LDM_32:
871     case elfcpp::R_386_TLS_LDM_PUSH:
872     case elfcpp::R_386_TLS_LDM_CALL:
873     case elfcpp::R_386_TLS_LDM_POP:
874     case elfcpp::R_386_USED_BY_INTEL_200:
875     default:
876       unsupported_reloc_local(object, r_type);
877       break;
878     }
879 }
880
881 // Report an unsupported relocation against a global symbol.
882
883 void
884 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
885                                             unsigned int r_type,
886                                             Symbol* gsym)
887 {
888   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
889              object->name().c_str(), r_type, gsym->name());
890 }
891
892 // Scan a relocation for a global symbol.
893
894 inline void
895 Target_i386::Scan::global(const General_options& options,
896                           Symbol_table* symtab,
897                           Layout* layout,
898                           Target_i386* target,
899                           Sized_relobj<32, false>* object,
900                           unsigned int data_shndx,
901                           const elfcpp::Rel<32, false>& reloc,
902                           unsigned int r_type,
903                           Symbol* gsym)
904 {
905   switch (r_type)
906     {
907     case elfcpp::R_386_NONE:
908     case elfcpp::R_386_GNU_VTINHERIT:
909     case elfcpp::R_386_GNU_VTENTRY:
910       break;
911
912     case elfcpp::R_386_32:
913     case elfcpp::R_386_PC32:
914     case elfcpp::R_386_16:
915     case elfcpp::R_386_PC16:
916     case elfcpp::R_386_8:
917     case elfcpp::R_386_PC8:
918       // FIXME: If we are generating a shared object we may need to
919       // copy this relocation into the object.  If this symbol is
920       // defined in a shared object, we may need to copy this
921       // relocation in order to avoid a COPY relocation.
922       gold_assert(!parameters->output_is_shared());
923
924       if (gsym->is_from_dynobj())
925         {
926           // This symbol is defined in a dynamic object.  If it is a
927           // function, we make a PLT entry.  Otherwise we need to
928           // either generate a COPY reloc or copy this reloc.
929           if (gsym->type() == elfcpp::STT_FUNC)
930             {
931               target->make_plt_entry(symtab, layout, gsym);
932
933               // If this is not a PC relative reference, then we may
934               // be taking the address of the function.  In that case
935               // we need to set the entry in the dynamic symbol table
936               // to the address of the PLT entry.
937               if (r_type != elfcpp::R_386_PC32
938                   && r_type != elfcpp::R_386_PC16
939                   && r_type != elfcpp::R_386_PC8)
940                 gsym->set_needs_dynsym_value();
941             }
942           else
943             target->copy_reloc(&options, symtab, layout, object, data_shndx,
944                                gsym, reloc);
945         }
946
947       break;
948
949     case elfcpp::R_386_GOT32:
950       {
951         // The symbol requires a GOT entry.
952         Output_data_got<32, false>* got = target->got_section(symtab, layout);
953         if (got->add_global(gsym))
954           {
955             // If this symbol is not fully resolved, we need to add a
956             // dynamic relocation for it.
957             if (!gsym->final_value_is_known())
958               {
959                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
960                 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
961                                     gsym->got_offset());
962               }
963           }
964       }
965       break;
966
967     case elfcpp::R_386_PLT32:
968       // If the symbol is fully resolved, this is just a PC32 reloc.
969       // Otherwise we need a PLT entry.
970       if (gsym->final_value_is_known())
971         break;
972       target->make_plt_entry(symtab, layout, gsym);
973       break;
974
975     case elfcpp::R_386_GOTOFF:
976     case elfcpp::R_386_GOTPC:
977       // We need a GOT section.
978       target->got_section(symtab, layout);
979       break;
980
981       // These are relocations which should only be seen by the
982       // dynamic linker, and should never be seen here.
983     case elfcpp::R_386_COPY:
984     case elfcpp::R_386_GLOB_DAT:
985     case elfcpp::R_386_JUMP_SLOT:
986     case elfcpp::R_386_RELATIVE:
987     case elfcpp::R_386_TLS_TPOFF:
988     case elfcpp::R_386_TLS_DTPMOD32:
989     case elfcpp::R_386_TLS_DTPOFF32:
990     case elfcpp::R_386_TLS_TPOFF32:
991     case elfcpp::R_386_TLS_DESC:
992       gold_error(_("%s: unexpected reloc %u in object file"),
993                  object->name().c_str(), r_type);
994       break;
995
996       // These are initial tls relocs, which are expected when
997       // linking.
998     case elfcpp::R_386_TLS_IE:
999     case elfcpp::R_386_TLS_GOTIE:
1000     case elfcpp::R_386_TLS_LE:
1001     case elfcpp::R_386_TLS_GD:
1002     case elfcpp::R_386_TLS_LDM:
1003     case elfcpp::R_386_TLS_LDO_32:
1004     case elfcpp::R_386_TLS_IE_32:
1005     case elfcpp::R_386_TLS_LE_32:
1006     case elfcpp::R_386_TLS_GOTDESC:
1007     case elfcpp::R_386_TLS_DESC_CALL:
1008       {
1009         const bool is_final = gsym->final_value_is_known();
1010         const tls::Tls_optimization optimized_type
1011             = Target_i386::optimize_tls_reloc(is_final, r_type);
1012         switch (r_type)
1013           {
1014           case elfcpp::R_386_TLS_LE:
1015           case elfcpp::R_386_TLS_LE_32:
1016             // FIXME: If generating a shared object, we need to copy
1017             // this relocation into the object.
1018             gold_assert(!parameters->output_is_shared());
1019             break;
1020
1021           case elfcpp::R_386_TLS_IE:
1022           case elfcpp::R_386_TLS_IE_32:
1023           case elfcpp::R_386_TLS_GOTIE:
1024             // FIXME: If not relaxing to LE, we need to generate a
1025             // TPOFF or TPOFF32 reloc.
1026             if (optimized_type != tls::TLSOPT_TO_LE)
1027               unsupported_reloc_global(object, r_type, gsym);
1028             break;
1029
1030           case elfcpp::R_386_TLS_LDM:
1031             // FIXME: If not relaxing to LE, we need to generate a
1032             // DTPMOD32 reloc.
1033             if (optimized_type != tls::TLSOPT_TO_LE)
1034               unsupported_reloc_global(object, r_type, gsym);
1035             break;
1036
1037           case elfcpp::R_386_TLS_LDO_32:
1038             break;
1039
1040           case elfcpp::R_386_TLS_GD:
1041           case elfcpp::R_386_TLS_GOTDESC:
1042           case elfcpp::R_386_TLS_DESC_CALL:
1043             // FIXME: If not relaxing to LE, we need to generate
1044             // DTPMOD32 and DTPOFF32 relocs.
1045             if (optimized_type != tls::TLSOPT_TO_LE)
1046               unsupported_reloc_global(object, r_type, gsym);
1047             break;
1048
1049           default:
1050             gold_unreachable();
1051           }
1052       }
1053       break;
1054
1055     case elfcpp::R_386_32PLT:
1056     case elfcpp::R_386_TLS_GD_32:
1057     case elfcpp::R_386_TLS_GD_PUSH:
1058     case elfcpp::R_386_TLS_GD_CALL:
1059     case elfcpp::R_386_TLS_GD_POP:
1060     case elfcpp::R_386_TLS_LDM_32:
1061     case elfcpp::R_386_TLS_LDM_PUSH:
1062     case elfcpp::R_386_TLS_LDM_CALL:
1063     case elfcpp::R_386_TLS_LDM_POP:
1064     case elfcpp::R_386_USED_BY_INTEL_200:
1065     default:
1066       unsupported_reloc_global(object, r_type, gsym);
1067       break;
1068     }
1069 }
1070
1071 // Scan relocations for a section.
1072
1073 void
1074 Target_i386::scan_relocs(const General_options& options,
1075                          Symbol_table* symtab,
1076                          Layout* layout,
1077                          Sized_relobj<32, false>* object,
1078                          unsigned int data_shndx,
1079                          unsigned int sh_type,
1080                          const unsigned char* prelocs,
1081                          size_t reloc_count,
1082                          size_t local_symbol_count,
1083                          const unsigned char* plocal_symbols,
1084                          Symbol** global_symbols)
1085 {
1086   if (sh_type == elfcpp::SHT_RELA)
1087     {
1088       gold_error(_("%s: unsupported RELA reloc section"),
1089                  object->name().c_str());
1090       return;
1091     }
1092
1093   gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1094                     Target_i386::Scan>(
1095     options,
1096     symtab,
1097     layout,
1098     this,
1099     object,
1100     data_shndx,
1101     prelocs,
1102     reloc_count,
1103     local_symbol_count,
1104     plocal_symbols,
1105     global_symbols);
1106 }
1107
1108 // Finalize the sections.
1109
1110 void
1111 Target_i386::do_finalize_sections(Layout* layout)
1112 {
1113   // Fill in some more dynamic tags.
1114   Output_data_dynamic* const odyn = layout->dynamic_data();
1115   if (odyn != NULL)
1116     {
1117       if (this->got_plt_ != NULL)
1118         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1119
1120       if (this->plt_ != NULL)
1121         {
1122           const Output_data* od = this->plt_->rel_plt();
1123           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1124           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1125           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1126         }
1127
1128       if (this->rel_dyn_ != NULL)
1129         {
1130           const Output_data* od = this->rel_dyn_;
1131           odyn->add_section_address(elfcpp::DT_REL, od);
1132           odyn->add_section_size(elfcpp::DT_RELSZ, od);
1133           odyn->add_constant(elfcpp::DT_RELENT,
1134                              elfcpp::Elf_sizes<32>::rel_size);
1135         }
1136
1137       if (!parameters->output_is_shared())
1138         {
1139           // The value of the DT_DEBUG tag is filled in by the dynamic
1140           // linker at run time, and used by the debugger.
1141           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1142         }
1143     }
1144
1145   // Emit any relocs we saved in an attempt to avoid generating COPY
1146   // relocs.
1147   if (this->copy_relocs_ == NULL)
1148     return;
1149   if (this->copy_relocs_->any_to_emit())
1150     {
1151       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1152       this->copy_relocs_->emit(rel_dyn);
1153     }
1154   delete this->copy_relocs_;
1155   this->copy_relocs_ = NULL;
1156 }
1157
1158 // Perform a relocation.
1159
1160 inline bool
1161 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1162                                 Target_i386* target,
1163                                 size_t relnum,
1164                                 const elfcpp::Rel<32, false>& rel,
1165                                 unsigned int r_type,
1166                                 const Sized_symbol<32>* gsym,
1167                                 const Symbol_value<32>* psymval,
1168                                 unsigned char* view,
1169                                 elfcpp::Elf_types<32>::Elf_Addr address,
1170                                 off_t view_size)
1171 {
1172   if (this->skip_call_tls_get_addr_)
1173     {
1174       if (r_type != elfcpp::R_386_PLT32
1175           || gsym == NULL
1176           || strcmp(gsym->name(), "___tls_get_addr") != 0)
1177         gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1178                                _("missing expected TLS relocation"));
1179       else
1180         {
1181           this->skip_call_tls_get_addr_ = false;
1182           return false;
1183         }
1184     }
1185
1186   // Pick the value to use for symbols defined in shared objects.
1187   Symbol_value<32> symval;
1188   if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1189     {
1190       symval.set_output_value(target->plt_section()->address()
1191                               + gsym->plt_offset());
1192       psymval = &symval;
1193     }
1194
1195   const Sized_relobj<32, false>* object = relinfo->object;
1196
1197   // Get the GOT offset if needed.
1198   bool have_got_offset = false;
1199   unsigned int got_offset = 0;
1200   switch (r_type)
1201     {
1202     case elfcpp::R_386_GOT32:
1203       if (gsym != NULL)
1204         {
1205           gold_assert(gsym->has_got_offset());
1206           got_offset = gsym->got_offset();
1207         }
1208       else
1209         {
1210           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1211           got_offset = object->local_got_offset(r_sym);
1212         }
1213       have_got_offset = true;
1214       break;
1215
1216     default:
1217       break;
1218     }
1219
1220   switch (r_type)
1221     {
1222     case elfcpp::R_386_NONE:
1223     case elfcpp::R_386_GNU_VTINHERIT:
1224     case elfcpp::R_386_GNU_VTENTRY:
1225       break;
1226
1227     case elfcpp::R_386_32:
1228       Relocate_functions<32, false>::rel32(view, object, psymval);
1229       break;
1230
1231     case elfcpp::R_386_PC32:
1232       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1233       break;
1234
1235     case elfcpp::R_386_16:
1236       Relocate_functions<32, false>::rel16(view, object, psymval);
1237       break;
1238
1239     case elfcpp::R_386_PC16:
1240       Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1241       break;
1242
1243     case elfcpp::R_386_8:
1244       Relocate_functions<32, false>::rel8(view, object, psymval);
1245       break;
1246
1247     case elfcpp::R_386_PC8:
1248       Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1249       break;
1250
1251     case elfcpp::R_386_PLT32:
1252       gold_assert(gsym->has_plt_offset()
1253                   || gsym->final_value_is_known());
1254       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1255       break;
1256
1257     case elfcpp::R_386_GOT32:
1258       gold_assert(have_got_offset);
1259       Relocate_functions<32, false>::rel32(view, got_offset);
1260       break;
1261
1262     case elfcpp::R_386_GOTOFF:
1263       {
1264         elfcpp::Elf_types<32>::Elf_Addr value;
1265         value = (psymval->value(object, 0)
1266                  - target->got_section(NULL, NULL)->address());
1267         Relocate_functions<32, false>::rel32(view, value);
1268       }
1269       break;
1270
1271     case elfcpp::R_386_GOTPC:
1272       {
1273         elfcpp::Elf_types<32>::Elf_Addr value;
1274         value = target->got_section(NULL, NULL)->address();
1275         Relocate_functions<32, false>::pcrel32(view, value, address);
1276       }
1277       break;
1278
1279     case elfcpp::R_386_COPY:
1280     case elfcpp::R_386_GLOB_DAT:
1281     case elfcpp::R_386_JUMP_SLOT:
1282     case elfcpp::R_386_RELATIVE:
1283       // These are outstanding tls relocs, which are unexpected when
1284       // linking.
1285     case elfcpp::R_386_TLS_TPOFF:
1286     case elfcpp::R_386_TLS_DTPMOD32:
1287     case elfcpp::R_386_TLS_DTPOFF32:
1288     case elfcpp::R_386_TLS_TPOFF32:
1289     case elfcpp::R_386_TLS_DESC:
1290       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1291                              _("unexpected reloc %u in object file"),
1292                              r_type);
1293       break;
1294
1295       // These are initial tls relocs, which are expected when
1296       // linking.
1297     case elfcpp::R_386_TLS_IE:
1298     case elfcpp::R_386_TLS_GOTIE:
1299     case elfcpp::R_386_TLS_LE:
1300     case elfcpp::R_386_TLS_GD:
1301     case elfcpp::R_386_TLS_LDM:
1302     case elfcpp::R_386_TLS_LDO_32:
1303     case elfcpp::R_386_TLS_IE_32:
1304     case elfcpp::R_386_TLS_LE_32:
1305     case elfcpp::R_386_TLS_GOTDESC:
1306     case elfcpp::R_386_TLS_DESC_CALL:
1307       this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1308                          address, view_size);
1309       break;
1310
1311     case elfcpp::R_386_32PLT:
1312     case elfcpp::R_386_TLS_GD_32:
1313     case elfcpp::R_386_TLS_GD_PUSH:
1314     case elfcpp::R_386_TLS_GD_CALL:
1315     case elfcpp::R_386_TLS_GD_POP:
1316     case elfcpp::R_386_TLS_LDM_32:
1317     case elfcpp::R_386_TLS_LDM_PUSH:
1318     case elfcpp::R_386_TLS_LDM_CALL:
1319     case elfcpp::R_386_TLS_LDM_POP:
1320     case elfcpp::R_386_USED_BY_INTEL_200:
1321     default:
1322       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1323                              _("unsupported reloc %u"),
1324                              r_type);
1325       break;
1326     }
1327
1328   return true;
1329 }
1330
1331 // Perform a TLS relocation.
1332
1333 inline void
1334 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1335                                     size_t relnum,
1336                                     const elfcpp::Rel<32, false>& rel,
1337                                     unsigned int r_type,
1338                                     const Sized_symbol<32>* gsym,
1339                                     const Symbol_value<32>* psymval,
1340                                     unsigned char* view,
1341                                     elfcpp::Elf_types<32>::Elf_Addr,
1342                                     off_t view_size)
1343 {
1344   Output_segment* tls_segment = relinfo->layout->tls_segment();
1345   if (tls_segment == NULL)
1346     {
1347       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1348                              _("TLS reloc but no TLS segment"));
1349       return;
1350     }
1351
1352   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1353
1354   const bool is_final = (gsym == NULL
1355                          ? !parameters->output_is_shared()
1356                          : gsym->final_value_is_known());
1357   const tls::Tls_optimization optimized_type
1358       = Target_i386::optimize_tls_reloc(is_final, r_type);
1359   switch (r_type)
1360     {
1361     case elfcpp::R_386_TLS_LE_32:
1362       value = tls_segment->vaddr() + tls_segment->memsz() - value;
1363       Relocate_functions<32, false>::rel32(view, value);
1364       break;
1365
1366     case elfcpp::R_386_TLS_LE:
1367       value = value - (tls_segment->vaddr() + tls_segment->memsz());
1368       Relocate_functions<32, false>::rel32(view, value);
1369       break;
1370
1371     case elfcpp::R_386_TLS_IE:
1372     case elfcpp::R_386_TLS_GOTIE:
1373     case elfcpp::R_386_TLS_IE_32:
1374       if (optimized_type == tls::TLSOPT_TO_LE)
1375         {
1376           Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1377                                               rel, r_type, value, view,
1378                                               view_size);
1379           break;
1380         }
1381       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1382                              _("unsupported reloc %u"),
1383                              r_type);
1384       break;
1385
1386     case elfcpp::R_386_TLS_GD:
1387       if (optimized_type == tls::TLSOPT_TO_LE)
1388         {
1389           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1390                              rel, r_type, value, view,
1391                              view_size);
1392           break;
1393         }
1394       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1395                              _("unsupported reloc %u"),
1396                              r_type);
1397       break;
1398
1399     case elfcpp::R_386_TLS_LDM:
1400       if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1401         {
1402           gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1403                                  _("both SUN and GNU model "
1404                                    "TLS relocations"));
1405           break;
1406         }
1407       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1408       if (optimized_type == tls::TLSOPT_TO_LE)
1409         {
1410           this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1411                              value, view, view_size);
1412           break;
1413         }
1414       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1415                              _("unsupported reloc %u"),
1416                              r_type);
1417       break;
1418
1419     case elfcpp::R_386_TLS_LDO_32:
1420       // This reloc can appear in debugging sections, in which case we
1421       // won't see the TLS_LDM reloc.  The local_dynamic_type field
1422       // tells us this.
1423       if (optimized_type != tls::TLSOPT_TO_LE
1424           || this->local_dynamic_type_ == LOCAL_DYNAMIC_NONE)
1425         value = value - tls_segment->vaddr();
1426       else if (this->local_dynamic_type_ == LOCAL_DYNAMIC_GNU)
1427         value = value - (tls_segment->vaddr() + tls_segment->memsz());
1428       else
1429         value = tls_segment->vaddr() + tls_segment->memsz() - value;
1430       Relocate_functions<32, false>::rel32(view, value);
1431       break;
1432
1433     case elfcpp::R_386_TLS_GOTDESC:
1434     case elfcpp::R_386_TLS_DESC_CALL:
1435       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1436                              _("unsupported reloc %u"),
1437                              r_type);
1438       break;
1439     }
1440 }
1441
1442 // Do a relocation in which we convert a TLS Initial-Exec to a
1443 // Local-Exec.
1444
1445 inline void
1446 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1447                                     size_t relnum,
1448                                     Output_segment* tls_segment,
1449                                     const elfcpp::Rel<32, false>& rel,
1450                                     unsigned int r_type,
1451                                     elfcpp::Elf_types<32>::Elf_Addr value,
1452                                     unsigned char* view,
1453                                     off_t view_size)
1454 {
1455   // We have to actually change the instructions, which means that we
1456   // need to examine the opcodes to figure out which instruction we
1457   // are looking at.
1458   if (r_type == elfcpp::R_386_TLS_IE)
1459     {
1460       // movl %gs:XX,%eax  ==>  movl $YY,%eax
1461       // movl %gs:XX,%reg  ==>  movl $YY,%reg
1462       // addl %gs:XX,%reg  ==>  addl $YY,%reg
1463       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
1464       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1465
1466       unsigned char op1 = view[-1];
1467       if (op1 == 0xa1)
1468         {
1469           // movl XX,%eax  ==>  movl $YY,%eax
1470           view[-1] = 0xb8;
1471         }
1472       else
1473         {
1474           tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1475
1476           unsigned char op2 = view[-2];
1477           if (op2 == 0x8b)
1478             {
1479               // movl XX,%reg  ==>  movl $YY,%reg
1480               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1481                              (op1 & 0xc7) == 0x05);
1482               view[-2] = 0xc7;
1483               view[-1] = 0xc0 | ((op1 >> 3) & 7);
1484             }
1485           else if (op2 == 0x03)
1486             {
1487               // addl XX,%reg  ==>  addl $YY,%reg
1488               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1489                              (op1 & 0xc7) == 0x05);
1490               view[-2] = 0x81;
1491               view[-1] = 0xc0 | ((op1 >> 3) & 7);
1492             }
1493           else
1494             tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1495         }
1496     }
1497   else
1498     {
1499       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
1500       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
1501       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
1502       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1503       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1504
1505       unsigned char op1 = view[-1];
1506       unsigned char op2 = view[-2];
1507       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1508                      (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1509       if (op2 == 0x8b)
1510         {
1511           // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
1512           view[-2] = 0xc7;
1513           view[-1] = 0xc0 | ((op1 >> 3) & 7);
1514         }
1515       else if (op2 == 0x2b)
1516         {
1517           // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
1518           view[-2] = 0x81;
1519           view[-1] = 0xe8 | ((op1 >> 3) & 7);
1520         }
1521       else if (op2 == 0x03)
1522         {
1523           // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
1524           view[-2] = 0x81;
1525           view[-1] = 0xc0 | ((op1 >> 3) & 7);
1526         }
1527       else
1528         tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1529     }
1530
1531   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1532   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1533     value = - value;
1534
1535   Relocate_functions<32, false>::rel32(view, value);
1536 }
1537
1538 // Do a relocation in which we convert a TLS General-Dynamic to a
1539 // Local-Exec.
1540
1541 inline void
1542 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1543                                     size_t relnum,
1544                                     Output_segment* tls_segment,
1545                                     const elfcpp::Rel<32, false>& rel,
1546                                     unsigned int,
1547                                     elfcpp::Elf_types<32>::Elf_Addr value,
1548                                     unsigned char* view,
1549                                     off_t view_size)
1550 {
1551   // leal foo(,%reg,1),%eax; call ___tls_get_addr
1552   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1553   // leal foo(%reg),%eax; call ___tls_get_addr
1554   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1555
1556   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1557   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1558
1559   unsigned char op1 = view[-1];
1560   unsigned char op2 = view[-2];
1561
1562   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1563                  op2 == 0x8d || op2 == 0x04);
1564   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1565
1566   int roff = 5;
1567
1568   if (op2 == 0x04)
1569     {
1570       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1571       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1572       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1573                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1574       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1575     }
1576   else
1577     {
1578       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1579                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1580       if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1581           && view[9] == 0x90)
1582         {
1583           // There is a trailing nop.  Use the size byte subl.
1584           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1585           roff = 6;
1586         }
1587       else
1588         {
1589           // Use the five byte subl.
1590           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1591         }
1592     }
1593
1594   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1595   Relocate_functions<32, false>::rel32(view + roff, value);
1596
1597   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1598   // We can skip it.
1599   this->skip_call_tls_get_addr_ = true;
1600 }
1601
1602 // Do a relocation in which we convert a TLS Local-Dynamic to a
1603 // Local-Exec.
1604
1605 inline void
1606 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
1607                                     size_t relnum,
1608                                     Output_segment*,
1609                                     const elfcpp::Rel<32, false>& rel,
1610                                     unsigned int,
1611                                     elfcpp::Elf_types<32>::Elf_Addr,
1612                                     unsigned char* view,
1613                                     off_t view_size)
1614 {
1615   // leal foo(%reg), %eax; call ___tls_get_addr
1616   // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1617
1618   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1619   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1620
1621   // FIXME: Does this test really always pass?
1622   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1623                  view[-2] == 0x8d && view[-1] == 0x83);
1624
1625   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1626
1627   memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1628
1629   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1630   // We can skip it.
1631   this->skip_call_tls_get_addr_ = true;
1632 }
1633
1634 // Relocate section data.
1635
1636 void
1637 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1638                               unsigned int sh_type,
1639                               const unsigned char* prelocs,
1640                               size_t reloc_count,
1641                               unsigned char* view,
1642                               elfcpp::Elf_types<32>::Elf_Addr address,
1643                               off_t view_size)
1644 {
1645   gold_assert(sh_type == elfcpp::SHT_REL);
1646
1647   gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1648                          Target_i386::Relocate>(
1649     relinfo,
1650     this,
1651     prelocs,
1652     reloc_count,
1653     view,
1654     address,
1655     view_size);
1656 }
1657
1658 // Return the value to use for a dynamic which requires special
1659 // treatment.  This is how we support equality comparisons of function
1660 // pointers across shared library boundaries, as described in the
1661 // processor specific ABI supplement.
1662
1663 uint64_t
1664 Target_i386::do_dynsym_value(const Symbol* gsym) const
1665 {
1666   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1667   return this->plt_section()->address() + gsym->plt_offset();
1668 }
1669
1670 // Return a string used to fill a code section with nops to take up
1671 // the specified length.
1672
1673 std::string
1674 Target_i386::do_code_fill(off_t length)
1675 {
1676   if (length >= 16)
1677     {
1678       // Build a jmp instruction to skip over the bytes.
1679       unsigned char jmp[5];
1680       jmp[0] = 0xe9;
1681       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
1682       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1683               + std::string(length - 5, '\0'));
1684     }
1685
1686   // Nop sequences of various lengths.
1687   const char nop1[1] = { 0x90 };                   // nop
1688   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
1689   const char nop3[3] = { 0x8d, 0x76, 0x00 };       // leal 0(%esi),%esi
1690   const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00};  // leal 0(%esi,1),%esi
1691   const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26,   // nop
1692                          0x00 };                   // leal 0(%esi,1),%esi
1693   const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00,   // leal 0L(%esi),%esi
1694                          0x00, 0x00 };
1695   const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00,   // leal 0L(%esi,1),%esi
1696                          0x00, 0x00, 0x00 };
1697   const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26,   // nop
1698                          0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1699   const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc,   // movl %esi,%esi
1700                          0x27, 0x00, 0x00, 0x00,   // leal 0L(%edi,1),%edi
1701                          0x00 };
1702   const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1703                            0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1704                            0x00, 0x00 };
1705   const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1706                            0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1707                            0x00, 0x00, 0x00 };
1708   const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1709                            0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1710                            0x00, 0x00, 0x00, 0x00 };
1711   const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1712                            0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1713                            0x27, 0x00, 0x00, 0x00,
1714                            0x00 };
1715   const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1716                            0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1717                            0xbc, 0x27, 0x00, 0x00,
1718                            0x00, 0x00 };
1719   const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1720                            0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1721                            0x90, 0x90, 0x90, 0x90,
1722                            0x90, 0x90, 0x90 };
1723
1724   const char* nops[16] = {
1725     NULL,
1726     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1727     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1728   };
1729
1730   return std::string(nops[length], length);
1731 }
1732
1733 // The selector for i386 object files.
1734
1735 class Target_selector_i386 : public Target_selector
1736 {
1737 public:
1738   Target_selector_i386()
1739     : Target_selector(elfcpp::EM_386, 32, false)
1740   { }
1741
1742   Target*
1743   recognize(int machine, int osabi, int abiversion);
1744
1745  private:
1746   Target_i386* target_;
1747 };
1748
1749 // Recognize an i386 object file when we already know that the machine
1750 // number is EM_386.
1751
1752 Target*
1753 Target_selector_i386::recognize(int, int, int)
1754 {
1755   if (this->target_ == NULL)
1756     this->target_ = new Target_i386();
1757   return this->target_;
1758 }
1759
1760 Target_selector_i386 target_selector_i386;
1761
1762 } // End anonymous namespace.