]> Git Repo - binutils.git/blob - gold/i386.cc
From Cary Coutant: Improve i386 shared library TLS support.
[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 <[email protected]>.
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               Output_section* output_section,
74               bool needs_special_offset_handling,
75               size_t local_symbol_count,
76               const unsigned char* plocal_symbols);
77
78   // Finalize the sections.
79   void
80   do_finalize_sections(Layout*);
81
82   // Return the value to use for a dynamic which requires special
83   // treatment.
84   uint64_t
85   do_dynsym_value(const Symbol*) const;
86
87   // Relocate a section.
88   void
89   relocate_section(const Relocate_info<32, false>*,
90                    unsigned int sh_type,
91                    const unsigned char* prelocs,
92                    size_t reloc_count,
93                    Output_section* output_section,
94                    bool needs_special_offset_handling,
95                    unsigned char* view,
96                    elfcpp::Elf_types<32>::Elf_Addr view_address,
97                    off_t view_size);
98
99   // Return a string used to fill a code section with nops.
100   std::string
101   do_code_fill(off_t length);
102
103   // Return whether SYM is defined by the ABI.
104   bool
105   do_is_defined_by_abi(Symbol* sym) const
106   { return strcmp(sym->name(), "___tls_get_addr") == 0; }
107
108   // Return the size of the GOT section.
109   off_t
110   got_size()
111   {
112     gold_assert(this->got_ != NULL);
113     return this->got_->data_size();
114   }
115
116  private:
117   // The class which scans relocations.
118   struct Scan
119   {
120     inline void
121     local(const General_options& options, Symbol_table* symtab,
122           Layout* layout, Target_i386* target,
123           Sized_relobj<32, false>* object,
124           unsigned int data_shndx,
125           Output_section* output_section,
126           const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
127           const elfcpp::Sym<32, false>& lsym);
128
129     inline void
130     global(const General_options& options, Symbol_table* symtab,
131            Layout* layout, Target_i386* target,
132            Sized_relobj<32, false>* object,
133            unsigned int data_shndx,
134            Output_section* output_section,
135            const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
136            Symbol* gsym);
137
138     static void
139     unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
140
141     static void
142     unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
143                              Symbol*);
144   };
145
146   // The class which implements relocation.
147   class Relocate
148   {
149    public:
150     Relocate()
151       : skip_call_tls_get_addr_(false),
152         local_dynamic_type_(LOCAL_DYNAMIC_NONE)
153     { }
154
155     ~Relocate()
156     {
157       if (this->skip_call_tls_get_addr_)
158         {
159           // FIXME: This needs to specify the location somehow.
160           gold_error(_("missing expected TLS relocation"));
161         }
162     }
163
164     // Return whether the static relocation needs to be applied.
165     inline bool
166     should_apply_static_reloc(const Sized_symbol<32>* gsym,
167                               bool is_absolute_ref,
168                               bool is_function_call,
169                               bool is_32bit);
170
171     // Do a relocation.  Return false if the caller should not issue
172     // any warnings about this relocation.
173     inline bool
174     relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
175              const elfcpp::Rel<32, false>&,
176              unsigned int r_type, const Sized_symbol<32>*,
177              const Symbol_value<32>*,
178              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
179              off_t);
180
181    private:
182     // Do a TLS relocation.
183     inline void
184     relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
185                  size_t relnum, const elfcpp::Rel<32, false>&,
186                  unsigned int r_type, const Sized_symbol<32>*,
187                  const Symbol_value<32>*,
188                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
189
190     // Do a TLS General-Dynamic to Initial-Exec transition.
191     inline void
192     tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
193                  Output_segment* tls_segment,
194                  const elfcpp::Rel<32, false>&, unsigned int r_type,
195                  elfcpp::Elf_types<32>::Elf_Addr value,
196                  unsigned char* view,
197                  off_t view_size);
198
199     // Do a TLS General-Dynamic to Local-Exec transition.
200     inline void
201     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
202                  Output_segment* tls_segment,
203                  const elfcpp::Rel<32, false>&, unsigned int r_type,
204                  elfcpp::Elf_types<32>::Elf_Addr value,
205                  unsigned char* view,
206                  off_t view_size);
207
208     // Do a TLS Local-Dynamic to Local-Exec transition.
209     inline void
210     tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
211                  Output_segment* tls_segment,
212                  const elfcpp::Rel<32, false>&, unsigned int r_type,
213                  elfcpp::Elf_types<32>::Elf_Addr value,
214                  unsigned char* view,
215                  off_t view_size);
216
217     // Do a TLS Initial-Exec to Local-Exec transition.
218     static inline void
219     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
220                  Output_segment* tls_segment,
221                  const elfcpp::Rel<32, false>&, unsigned int r_type,
222                  elfcpp::Elf_types<32>::Elf_Addr value,
223                  unsigned char* view,
224                  off_t view_size);
225
226     // We need to keep track of which type of local dynamic relocation
227     // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
228     enum Local_dynamic_type
229     {
230       LOCAL_DYNAMIC_NONE,
231       LOCAL_DYNAMIC_SUN,
232       LOCAL_DYNAMIC_GNU
233     };
234
235     // This is set if we should skip the next reloc, which should be a
236     // PLT32 reloc against ___tls_get_addr.
237     bool skip_call_tls_get_addr_;
238     // The type of local dynamic relocation we have seen in the section
239     // being relocated, if any.
240     Local_dynamic_type local_dynamic_type_;
241   };
242
243   // Adjust TLS relocation type based on the options and whether this
244   // is a local symbol.
245   static tls::Tls_optimization
246   optimize_tls_reloc(bool is_final, int r_type);
247
248   // Get the GOT section, creating it if necessary.
249   Output_data_got<32, false>*
250   got_section(Symbol_table*, Layout*);
251
252   // Get the GOT PLT section.
253   Output_data_space*
254   got_plt_section() const
255   {
256     gold_assert(this->got_plt_ != NULL);
257     return this->got_plt_;
258   }
259
260   // Create a PLT entry for a global symbol.
261   void
262   make_plt_entry(Symbol_table*, Layout*, Symbol*);
263
264   // Get the PLT section.
265   const Output_data_plt_i386*
266   plt_section() const
267   {
268     gold_assert(this->plt_ != NULL);
269     return this->plt_;
270   }
271
272   // Get the dynamic reloc section, creating it if necessary.
273   Reloc_section*
274   rel_dyn_section(Layout*);
275
276   // Return true if the symbol may need a COPY relocation.
277   // References from an executable object to non-function symbols
278   // defined in a dynamic object may need a COPY relocation.
279   bool
280   may_need_copy_reloc(Symbol* gsym)
281   {
282     return (!parameters->output_is_shared()
283             && gsym->is_from_dynobj()
284             && gsym->type() != elfcpp::STT_FUNC);
285   }
286
287   // Copy a relocation against a global symbol.
288   void
289   copy_reloc(const General_options*, Symbol_table*, Layout*,
290              Sized_relobj<32, false>*, unsigned int,
291              Symbol*, const elfcpp::Rel<32, false>&);
292
293   // Information about this specific target which we pass to the
294   // general Target structure.
295   static const Target::Target_info i386_info;
296
297   // The GOT section.
298   Output_data_got<32, false>* got_;
299   // The PLT section.
300   Output_data_plt_i386* plt_;
301   // The GOT PLT section.
302   Output_data_space* got_plt_;
303   // The dynamic reloc section.
304   Reloc_section* rel_dyn_;
305   // Relocs saved to avoid a COPY reloc.
306   Copy_relocs<32, false>* copy_relocs_;
307   // Space for variables copied with a COPY reloc.
308   Output_data_space* dynbss_;
309 };
310
311 const Target::Target_info Target_i386::i386_info =
312 {
313   32,                   // size
314   false,                // is_big_endian
315   elfcpp::EM_386,       // machine_code
316   false,                // has_make_symbol
317   false,                // has_resolve
318   true,                 // has_code_fill
319   true,                 // is_default_stack_executable
320   "/usr/lib/libc.so.1", // dynamic_linker
321   0x08048000,           // default_text_segment_address
322   0x1000,               // abi_pagesize
323   0x1000                // common_pagesize
324 };
325
326 // Get the GOT section, creating it if necessary.
327
328 Output_data_got<32, false>*
329 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
330 {
331   if (this->got_ == NULL)
332     {
333       gold_assert(symtab != NULL && layout != NULL);
334
335       this->got_ = new Output_data_got<32, false>();
336
337       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
338                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
339                                       this->got_);
340
341       // The old GNU linker creates a .got.plt section.  We just
342       // create another set of data in the .got section.  Note that we
343       // always create a PLT if we create a GOT, although the PLT
344       // might be empty.
345       this->got_plt_ = new Output_data_space(4);
346       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
347                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
348                                       this->got_plt_);
349
350       // The first three entries are reserved.
351       this->got_plt_->set_space_size(3 * 4);
352
353       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
354       symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
355                                     this->got_plt_,
356                                     0, 0, elfcpp::STT_OBJECT,
357                                     elfcpp::STB_LOCAL,
358                                     elfcpp::STV_HIDDEN, 0,
359                                     false, false);
360     }
361
362   return this->got_;
363 }
364
365 // Get the dynamic reloc section, creating it if necessary.
366
367 Target_i386::Reloc_section*
368 Target_i386::rel_dyn_section(Layout* layout)
369 {
370   if (this->rel_dyn_ == NULL)
371     {
372       gold_assert(layout != NULL);
373       this->rel_dyn_ = new Reloc_section();
374       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
375                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
376     }
377   return this->rel_dyn_;
378 }
379
380 // A class to handle the PLT data.
381
382 class Output_data_plt_i386 : public Output_section_data
383 {
384  public:
385   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
386
387   Output_data_plt_i386(Layout*, Output_data_space*);
388
389   // Add an entry to the PLT.
390   void
391   add_entry(Symbol* gsym);
392
393   // Return the .rel.plt section data.
394   const Reloc_section*
395   rel_plt() const
396   { return this->rel_; }
397
398  protected:
399   void
400   do_adjust_output_section(Output_section* os);
401
402  private:
403   // The size of an entry in the PLT.
404   static const int plt_entry_size = 16;
405
406   // The first entry in the PLT for an executable.
407   static unsigned char exec_first_plt_entry[plt_entry_size];
408
409   // The first entry in the PLT for a shared object.
410   static unsigned char dyn_first_plt_entry[plt_entry_size];
411
412   // Other entries in the PLT for an executable.
413   static unsigned char exec_plt_entry[plt_entry_size];
414
415   // Other entries in the PLT for a shared object.
416   static unsigned char dyn_plt_entry[plt_entry_size];
417
418   // Set the final size.
419   void
420   do_set_address(uint64_t, off_t)
421   { this->set_data_size((this->count_ + 1) * plt_entry_size); }
422
423   // Write out the PLT data.
424   void
425   do_write(Output_file*);
426
427   // The reloc section.
428   Reloc_section* rel_;
429   // The .got.plt section.
430   Output_data_space* got_plt_;
431   // The number of PLT entries.
432   unsigned int count_;
433 };
434
435 // Create the PLT section.  The ordinary .got section is an argument,
436 // since we need to refer to the start.  We also create our own .got
437 // section just for PLT entries.
438
439 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
440                                            Output_data_space* got_plt)
441   : Output_section_data(4), got_plt_(got_plt), count_(0)
442 {
443   this->rel_ = new Reloc_section();
444   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
445                                   elfcpp::SHF_ALLOC, this->rel_);
446 }
447
448 void
449 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
450 {
451   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
452   // linker, and so do we.
453   os->set_entsize(4);
454 }
455
456 // Add an entry to the PLT.
457
458 void
459 Output_data_plt_i386::add_entry(Symbol* gsym)
460 {
461   gold_assert(!gsym->has_plt_offset());
462
463   // Note that when setting the PLT offset we skip the initial
464   // reserved PLT entry.
465   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
466
467   ++this->count_;
468
469   off_t got_offset = this->got_plt_->data_size();
470
471   // Every PLT entry needs a GOT entry which points back to the PLT
472   // entry (this will be changed by the dynamic linker, normally
473   // lazily when the function is called).
474   this->got_plt_->set_space_size(got_offset + 4);
475
476   // Every PLT entry needs a reloc.
477   gsym->set_needs_dynsym_entry();
478   this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
479                          got_offset);
480
481   // Note that we don't need to save the symbol.  The contents of the
482   // PLT are independent of which symbols are used.  The symbols only
483   // appear in the relocations.
484 }
485
486 // The first entry in the PLT for an executable.
487
488 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
489 {
490   0xff, 0x35,   // pushl contents of memory address
491   0, 0, 0, 0,   // replaced with address of .got + 4
492   0xff, 0x25,   // jmp indirect
493   0, 0, 0, 0,   // replaced with address of .got + 8
494   0, 0, 0, 0    // unused
495 };
496
497 // The first entry in the PLT for a shared object.
498
499 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
500 {
501   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
502   0xff, 0xa3, 8, 0, 0, 0,       // jmp *8(%ebx)
503   0, 0, 0, 0                    // unused
504 };
505
506 // Subsequent entries in the PLT for an executable.
507
508 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
509 {
510   0xff, 0x25,   // jmp indirect
511   0, 0, 0, 0,   // replaced with address of symbol in .got
512   0x68,         // pushl immediate
513   0, 0, 0, 0,   // replaced with offset into relocation table
514   0xe9,         // jmp relative
515   0, 0, 0, 0    // replaced with offset to start of .plt
516 };
517
518 // Subsequent entries in the PLT for a shared object.
519
520 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
521 {
522   0xff, 0xa3,   // jmp *offset(%ebx)
523   0, 0, 0, 0,   // replaced with offset of symbol in .got
524   0x68,         // pushl immediate
525   0, 0, 0, 0,   // replaced with offset into relocation table
526   0xe9,         // jmp relative
527   0, 0, 0, 0    // replaced with offset to start of .plt
528 };
529
530 // Write out the PLT.  This uses the hand-coded instructions above,
531 // and adjusts them as needed.  This is all specified by the i386 ELF
532 // Processor Supplement.
533
534 void
535 Output_data_plt_i386::do_write(Output_file* of)
536 {
537   const off_t offset = this->offset();
538   const off_t oview_size = this->data_size();
539   unsigned char* const oview = of->get_output_view(offset, oview_size);
540
541   const off_t got_file_offset = this->got_plt_->offset();
542   const off_t got_size = this->got_plt_->data_size();
543   unsigned char* const got_view = of->get_output_view(got_file_offset,
544                                                       got_size);
545
546   unsigned char* pov = oview;
547
548   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
549   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
550
551   if (parameters->output_is_shared())
552     memcpy(pov, dyn_first_plt_entry, plt_entry_size);
553   else
554     {
555       memcpy(pov, exec_first_plt_entry, plt_entry_size);
556       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
557       elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
558     }
559   pov += plt_entry_size;
560
561   unsigned char* got_pov = got_view;
562
563   memset(got_pov, 0, 12);
564   got_pov += 12;
565
566   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
567
568   unsigned int plt_offset = plt_entry_size;
569   unsigned int plt_rel_offset = 0;
570   unsigned int got_offset = 12;
571   const unsigned int count = this->count_;
572   for (unsigned int i = 0;
573        i < count;
574        ++i,
575          pov += plt_entry_size,
576          got_pov += 4,
577          plt_offset += plt_entry_size,
578          plt_rel_offset += rel_size,
579          got_offset += 4)
580     {
581       // Set and adjust the PLT entry itself.
582
583       if (parameters->output_is_shared())
584         {
585           memcpy(pov, dyn_plt_entry, plt_entry_size);
586           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
587         }
588       else
589         {
590           memcpy(pov, exec_plt_entry, plt_entry_size);
591           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
592                                                       (got_address
593                                                        + got_offset));
594         }
595
596       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
597       elfcpp::Swap<32, false>::writeval(pov + 12,
598                                         - (plt_offset + plt_entry_size));
599
600       // Set the entry in the GOT.
601       elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
602     }
603
604   gold_assert(pov - oview == oview_size);
605   gold_assert(got_pov - got_view == got_size);
606
607   of->write_output_view(offset, oview_size, oview);
608   of->write_output_view(got_file_offset, got_size, got_view);
609 }
610
611 // Create a PLT entry for a global symbol.
612
613 void
614 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
615 {
616   if (gsym->has_plt_offset())
617     return;
618
619   if (this->plt_ == NULL)
620     {
621       // Create the GOT sections first.
622       this->got_section(symtab, layout);
623
624       this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
625       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
626                                       (elfcpp::SHF_ALLOC
627                                        | elfcpp::SHF_EXECINSTR),
628                                       this->plt_);
629     }
630
631   this->plt_->add_entry(gsym);
632 }
633
634 // Handle a relocation against a non-function symbol defined in a
635 // dynamic object.  The traditional way to handle this is to generate
636 // a COPY relocation to copy the variable at runtime from the shared
637 // object into the executable's data segment.  However, this is
638 // undesirable in general, as if the size of the object changes in the
639 // dynamic object, the executable will no longer work correctly.  If
640 // this relocation is in a writable section, then we can create a
641 // dynamic reloc and the dynamic linker will resolve it to the correct
642 // address at runtime.  However, we do not want do that if the
643 // relocation is in a read-only section, as it would prevent the
644 // readonly segment from being shared.  And if we have to eventually
645 // generate a COPY reloc, then any dynamic relocations will be
646 // useless.  So this means that if this is a writable section, we need
647 // to save the relocation until we see whether we have to create a
648 // COPY relocation for this symbol for any other relocation.
649
650 void
651 Target_i386::copy_reloc(const General_options* options,
652                         Symbol_table* symtab,
653                         Layout* layout,
654                         Sized_relobj<32, false>* object,
655                         unsigned int data_shndx, Symbol* gsym,
656                         const elfcpp::Rel<32, false>& rel)
657 {
658   Sized_symbol<32>* ssym;
659   ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
660                                                         SELECT_SIZE(32));
661
662   if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
663                                                data_shndx, ssym))
664     {
665       // So far we do not need a COPY reloc.  Save this relocation.
666       // If it turns out that we never need a COPY reloc for this
667       // symbol, then we will emit the relocation.
668       if (this->copy_relocs_ == NULL)
669         this->copy_relocs_ = new Copy_relocs<32, false>();
670       this->copy_relocs_->save(ssym, object, data_shndx, rel);
671     }
672   else
673     {
674       // Allocate space for this symbol in the .bss section.
675
676       elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
677
678       // There is no defined way to determine the required alignment
679       // of the symbol.  We pick the alignment based on the size.  We
680       // set an arbitrary maximum of 256.
681       unsigned int align;
682       for (align = 1; align < 512; align <<= 1)
683         if ((symsize & align) != 0)
684           break;
685
686       if (this->dynbss_ == NULL)
687         {
688           this->dynbss_ = new Output_data_space(align);
689           layout->add_output_section_data(".bss",
690                                           elfcpp::SHT_NOBITS,
691                                           (elfcpp::SHF_ALLOC
692                                            | elfcpp::SHF_WRITE),
693                                           this->dynbss_);
694         }
695
696       Output_data_space* dynbss = this->dynbss_;
697
698       if (align > dynbss->addralign())
699         dynbss->set_space_alignment(align);
700
701       off_t dynbss_size = dynbss->data_size();
702       dynbss_size = align_address(dynbss_size, align);
703       off_t offset = dynbss_size;
704       dynbss->set_space_size(dynbss_size + symsize);
705
706       symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
707
708       // Add the COPY reloc.
709       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
710       rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
711     }
712 }
713
714 // Optimize the TLS relocation type based on what we know about the
715 // symbol.  IS_FINAL is true if the final address of this symbol is
716 // known at link time.
717
718 tls::Tls_optimization
719 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
720 {
721   // If we are generating a shared library, then we can't do anything
722   // in the linker.
723   if (parameters->output_is_shared())
724     return tls::TLSOPT_NONE;
725
726   switch (r_type)
727     {
728     case elfcpp::R_386_TLS_GD:
729     case elfcpp::R_386_TLS_GOTDESC:
730     case elfcpp::R_386_TLS_DESC_CALL:
731       // These are General-Dynamic which permits fully general TLS
732       // access.  Since we know that we are generating an executable,
733       // we can convert this to Initial-Exec.  If we also know that
734       // this is a local symbol, we can further switch to Local-Exec.
735       if (is_final)
736         return tls::TLSOPT_TO_LE;
737       return tls::TLSOPT_TO_IE;
738
739     case elfcpp::R_386_TLS_LDM:
740       // This is Local-Dynamic, which refers to a local symbol in the
741       // dynamic TLS block.  Since we know that we generating an
742       // executable, we can switch to Local-Exec.
743       return tls::TLSOPT_TO_LE;
744
745     case elfcpp::R_386_TLS_LDO_32:
746       // Another type of Local-Dynamic relocation.
747       return tls::TLSOPT_TO_LE;
748
749     case elfcpp::R_386_TLS_IE:
750     case elfcpp::R_386_TLS_GOTIE:
751     case elfcpp::R_386_TLS_IE_32:
752       // These are Initial-Exec relocs which get the thread offset
753       // from the GOT.  If we know that we are linking against the
754       // local symbol, we can switch to Local-Exec, which links the
755       // thread offset into the instruction.
756       if (is_final)
757         return tls::TLSOPT_TO_LE;
758       return tls::TLSOPT_NONE;
759
760     case elfcpp::R_386_TLS_LE:
761     case elfcpp::R_386_TLS_LE_32:
762       // When we already have Local-Exec, there is nothing further we
763       // can do.
764       return tls::TLSOPT_NONE;
765
766     default:
767       gold_unreachable();
768     }
769 }
770
771 // Report an unsupported relocation against a local symbol.
772
773 void
774 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
775                                            unsigned int r_type)
776 {
777   gold_error(_("%s: unsupported reloc %u against local symbol"),
778              object->name().c_str(), r_type);
779 }
780
781 // Scan a relocation for a local symbol.
782
783 inline void
784 Target_i386::Scan::local(const General_options&,
785                          Symbol_table* symtab,
786                          Layout* layout,
787                          Target_i386* target,
788                          Sized_relobj<32, false>* object,
789                          unsigned int data_shndx,
790                          Output_section* output_section,
791                          const elfcpp::Rel<32, false>& reloc,
792                          unsigned int r_type,
793                          const elfcpp::Sym<32, false>&)
794 {
795   switch (r_type)
796     {
797     case elfcpp::R_386_NONE:
798     case elfcpp::R_386_GNU_VTINHERIT:
799     case elfcpp::R_386_GNU_VTENTRY:
800       break;
801
802     case elfcpp::R_386_32:
803       // If building a shared library (or a position-independent
804       // executable), we need to create a dynamic relocation for
805       // this location. The relocation applied at link time will
806       // apply the link-time value, so we flag the location with
807       // an R_386_RELATIVE relocation so the dynamic loader can
808       // relocate it easily.
809       if (parameters->output_is_position_independent())
810         {
811           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
812           rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE, data_shndx,
813                              reloc.get_r_offset());
814           if (!output_section->is_section_flag_set(elfcpp::SHF_WRITE))
815             layout->set_have_textrel();
816         }
817       break;
818
819     case elfcpp::R_386_16:
820     case elfcpp::R_386_8:
821       // If building a shared library (or a position-independent
822       // executable), we need to create a dynamic relocation for
823       // this location. Because the addend needs to remain in the
824       // data section, we need to be careful not to apply this
825       // relocation statically.
826       if (parameters->output_is_position_independent())
827         {
828           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
829           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
830           rel_dyn->add_local(object, r_sym, r_type, data_shndx,
831                              reloc.get_r_offset());
832           if (!output_section->is_section_flag_set(elfcpp::SHF_WRITE))
833             layout->set_have_textrel();
834         }
835       break;
836
837     case elfcpp::R_386_PC32:
838     case elfcpp::R_386_PC16:
839     case elfcpp::R_386_PC8:
840       break;
841
842     case elfcpp::R_386_PLT32:
843       // Since we know this is a local symbol, we can handle this as a
844       // PC32 reloc.
845       break;
846
847     case elfcpp::R_386_GOTOFF:
848     case elfcpp::R_386_GOTPC:
849       // We need a GOT section.
850       target->got_section(symtab, layout);
851       break;
852
853     case elfcpp::R_386_GOT32:
854       {
855         // The symbol requires a GOT entry.
856         Output_data_got<32, false>* got = target->got_section(symtab, layout);
857         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
858         if (got->add_local(object, r_sym))
859           {
860             // If we are generating a shared object, we need to add a
861             // dynamic RELATIVE relocation for this symbol.
862             if (parameters->output_is_position_independent())
863               {
864                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
865                 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE,
866                                    data_shndx, reloc.get_r_offset());
867                 if (!output_section->is_section_flag_set(elfcpp::SHF_WRITE))
868                   layout->set_have_textrel();
869               }
870           }
871       }
872       break;
873
874       // These are relocations which should only be seen by the
875       // dynamic linker, and should never be seen here.
876     case elfcpp::R_386_COPY:
877     case elfcpp::R_386_GLOB_DAT:
878     case elfcpp::R_386_JUMP_SLOT:
879     case elfcpp::R_386_RELATIVE:
880     case elfcpp::R_386_TLS_TPOFF:
881     case elfcpp::R_386_TLS_DTPMOD32:
882     case elfcpp::R_386_TLS_DTPOFF32:
883     case elfcpp::R_386_TLS_TPOFF32:
884     case elfcpp::R_386_TLS_DESC:
885       gold_error(_("%s: unexpected reloc %u in object file"),
886                  object->name().c_str(), r_type);
887       break;
888
889       // These are initial TLS relocs, which are expected when
890       // linking.
891     case elfcpp::R_386_TLS_GD:            // Global-dynamic
892     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
893     case elfcpp::R_386_TLS_DESC_CALL:
894     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
895     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
896     case elfcpp::R_386_TLS_IE:            // Initial-exec
897     case elfcpp::R_386_TLS_IE_32:
898     case elfcpp::R_386_TLS_GOTIE:
899     case elfcpp::R_386_TLS_LE:            // Local-exec
900     case elfcpp::R_386_TLS_LE_32:
901       {
902         bool output_is_shared = parameters->output_is_shared();
903         const tls::Tls_optimization optimized_type
904             = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
905         switch (r_type)
906           {
907           case elfcpp::R_386_TLS_GD:          // Global-dynamic
908             if (optimized_type == tls::TLSOPT_NONE)
909               {
910                 // Create a pair of GOT entries for the module index and
911                 // dtv-relative offset.
912                 Output_data_got<32, false>* got
913                     = target->got_section(symtab, layout);
914                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
915                 if (got->add_local_tls(object, r_sym, true))
916                   {
917                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
918                     unsigned int got_off
919                         = object->local_tls_got_offset(r_sym, true);
920                     rel_dyn->add_local(object, r_sym,
921                                        elfcpp::R_386_TLS_DTPMOD32,
922                                        got, got_off);
923                     rel_dyn->add_local(object, r_sym,
924                                        elfcpp::R_386_TLS_DTPOFF32,
925                                        got, got_off + 4);
926                   }
927               }
928             else if (optimized_type != tls::TLSOPT_TO_LE)
929               unsupported_reloc_local(object, r_type);
930             break;
931
932           case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (from ~oliva)
933           case elfcpp::R_386_TLS_DESC_CALL:
934             unsupported_reloc_local(object, r_type);
935             break;
936
937           case elfcpp::R_386_TLS_LDM:         // Local-dynamic
938             if (optimized_type == tls::TLSOPT_NONE)
939               {
940                 // Create a GOT entry for the module index.
941                 Output_data_got<32, false>* got
942                     = target->got_section(symtab, layout);
943                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
944                 if (got->add_local_tls(object, r_sym, false))
945                   {
946                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
947                     unsigned int got_off
948                         = object->local_tls_got_offset(r_sym, false);
949                     rel_dyn->add_local(object, r_sym,
950                                        elfcpp::R_386_TLS_DTPMOD32, got,
951                                        got_off);
952                   }
953               }
954             else if (optimized_type != tls::TLSOPT_TO_LE)
955               unsupported_reloc_local(object, r_type);
956             break;
957
958           case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
959             break;
960
961           case elfcpp::R_386_TLS_IE:          // Initial-exec
962           case elfcpp::R_386_TLS_IE_32:
963           case elfcpp::R_386_TLS_GOTIE:
964             if (optimized_type == tls::TLSOPT_NONE)
965               {
966                 // Create a GOT entry for the tp-relative offset.
967                 Output_data_got<32, false>* got
968                     = target->got_section(symtab, layout);
969                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
970                 if (got->add_local(object, r_sym))
971                   {
972                     unsigned int dyn_r_type
973                         = (r_type == elfcpp::R_386_TLS_IE_32
974                            ? elfcpp::R_386_TLS_TPOFF32
975                            : elfcpp::R_386_TLS_TPOFF);
976                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
977                     unsigned int got_off = object->local_got_offset(r_sym);
978                     rel_dyn->add_local(object, r_sym, dyn_r_type, got,
979                                        got_off);
980                   }
981               }
982             else if (optimized_type != tls::TLSOPT_TO_LE)
983               unsupported_reloc_local(object, r_type);
984             break;
985
986           case elfcpp::R_386_TLS_LE:          // Local-exec
987           case elfcpp::R_386_TLS_LE_32:
988             if (output_is_shared)
989               unsupported_reloc_local(object, r_type);
990             break;
991
992           default:
993             gold_unreachable();
994           }
995       }
996       break;
997
998     case elfcpp::R_386_32PLT:
999     case elfcpp::R_386_TLS_GD_32:
1000     case elfcpp::R_386_TLS_GD_PUSH:
1001     case elfcpp::R_386_TLS_GD_CALL:
1002     case elfcpp::R_386_TLS_GD_POP:
1003     case elfcpp::R_386_TLS_LDM_32:
1004     case elfcpp::R_386_TLS_LDM_PUSH:
1005     case elfcpp::R_386_TLS_LDM_CALL:
1006     case elfcpp::R_386_TLS_LDM_POP:
1007     case elfcpp::R_386_USED_BY_INTEL_200:
1008     default:
1009       unsupported_reloc_local(object, r_type);
1010       break;
1011     }
1012 }
1013
1014 // Report an unsupported relocation against a global symbol.
1015
1016 void
1017 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1018                                             unsigned int r_type,
1019                                             Symbol* gsym)
1020 {
1021   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1022              object->name().c_str(), r_type, gsym->name());
1023 }
1024
1025 // Scan a relocation for a global symbol.
1026
1027 inline void
1028 Target_i386::Scan::global(const General_options& options,
1029                           Symbol_table* symtab,
1030                           Layout* layout,
1031                           Target_i386* target,
1032                           Sized_relobj<32, false>* object,
1033                           unsigned int data_shndx,
1034                           Output_section* output_section,
1035                           const elfcpp::Rel<32, false>& reloc,
1036                           unsigned int r_type,
1037                           Symbol* gsym)
1038 {
1039   switch (r_type)
1040     {
1041     case elfcpp::R_386_NONE:
1042     case elfcpp::R_386_GNU_VTINHERIT:
1043     case elfcpp::R_386_GNU_VTENTRY:
1044       break;
1045
1046     case elfcpp::R_386_32:
1047     case elfcpp::R_386_16:
1048     case elfcpp::R_386_8:
1049       {
1050         // Make a PLT entry if necessary.
1051         if (gsym->needs_plt_entry())
1052           {
1053             target->make_plt_entry(symtab, layout, gsym);
1054             // Since this is not a PC-relative relocation, we may be
1055             // taking the address of a function. In that case we need to
1056             // set the entry in the dynamic symbol table to the address of
1057             // the PLT entry.
1058             if (gsym->is_from_dynobj())
1059               gsym->set_needs_dynsym_value();
1060           }
1061         // Make a dynamic relocation if necessary.
1062         if (gsym->needs_dynamic_reloc(true, false))
1063           {
1064             if (target->may_need_copy_reloc(gsym))
1065               {
1066                 target->copy_reloc(&options, symtab, layout, object,
1067                                    data_shndx, gsym, reloc);
1068               }
1069             else if (r_type == elfcpp::R_386_32
1070                      && gsym->can_use_relative_reloc(false))
1071               {
1072                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1073                 rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE,
1074                                    data_shndx, reloc.get_r_offset());
1075                 if (!output_section->is_section_flag_set(elfcpp::SHF_WRITE))
1076                   layout->set_have_textrel();
1077               }
1078             else
1079               {
1080                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1081                 rel_dyn->add_global(gsym, r_type, object, data_shndx, 
1082                                     reloc.get_r_offset());
1083                 if (!output_section->is_section_flag_set(elfcpp::SHF_WRITE))
1084                   layout->set_have_textrel();
1085               }
1086           }
1087       }
1088       break;
1089
1090     case elfcpp::R_386_PC32:
1091     case elfcpp::R_386_PC16:
1092     case elfcpp::R_386_PC8:
1093       {
1094         // Make a PLT entry if necessary.
1095         if (gsym->needs_plt_entry())
1096           target->make_plt_entry(symtab, layout, gsym);
1097         // Make a dynamic relocation if necessary.
1098         bool is_function_call = (gsym->type() == elfcpp::STT_FUNC);
1099         if (gsym->needs_dynamic_reloc(false, is_function_call))
1100           {
1101             if (target->may_need_copy_reloc(gsym))
1102               {
1103                 target->copy_reloc(&options, symtab, layout, object,
1104                                    data_shndx, gsym, reloc);
1105               }
1106             else
1107               {
1108                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1109                 rel_dyn->add_global(gsym, r_type, object, data_shndx, 
1110                                     reloc.get_r_offset());
1111                 if (!output_section->is_section_flag_set(elfcpp::SHF_WRITE))
1112                   layout->set_have_textrel();
1113               }
1114           }
1115       }
1116       break;
1117
1118     case elfcpp::R_386_GOT32:
1119       {
1120         // The symbol requires a GOT entry.
1121         Output_data_got<32, false>* got = target->got_section(symtab, layout);
1122         if (got->add_global(gsym))
1123           {
1124             // If this symbol is not fully resolved, we need to add a
1125             // dynamic relocation for it.
1126             if (!gsym->final_value_is_known())
1127               {
1128                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1129                 if (gsym->is_from_dynobj()
1130                     || gsym->is_preemptible())
1131                   rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
1132                                       gsym->got_offset());
1133                 else
1134                   {
1135                     rel_dyn->add_local(object, 0, elfcpp::R_386_RELATIVE,
1136                                        got, gsym->got_offset());
1137                     // Make sure we write the link-time value to the GOT.
1138                     gsym->set_needs_value_in_got();
1139                   }
1140               }
1141           }
1142       }
1143       break;
1144
1145     case elfcpp::R_386_PLT32:
1146       // If the symbol is fully resolved, this is just a PC32 reloc.
1147       // Otherwise we need a PLT entry.
1148       if (gsym->final_value_is_known())
1149         break;
1150       // If building a shared library, we can also skip the PLT entry
1151       // if the symbol is defined in the output file and is protected
1152       // or hidden.
1153       if (gsym->is_defined()
1154           && !gsym->is_from_dynobj()
1155           && !gsym->is_preemptible())
1156         break;
1157       target->make_plt_entry(symtab, layout, gsym);
1158       break;
1159
1160     case elfcpp::R_386_GOTOFF:
1161     case elfcpp::R_386_GOTPC:
1162       // We need a GOT section.
1163       target->got_section(symtab, layout);
1164       break;
1165
1166       // These are relocations which should only be seen by the
1167       // dynamic linker, and should never be seen here.
1168     case elfcpp::R_386_COPY:
1169     case elfcpp::R_386_GLOB_DAT:
1170     case elfcpp::R_386_JUMP_SLOT:
1171     case elfcpp::R_386_RELATIVE:
1172     case elfcpp::R_386_TLS_TPOFF:
1173     case elfcpp::R_386_TLS_DTPMOD32:
1174     case elfcpp::R_386_TLS_DTPOFF32:
1175     case elfcpp::R_386_TLS_TPOFF32:
1176     case elfcpp::R_386_TLS_DESC:
1177       gold_error(_("%s: unexpected reloc %u in object file"),
1178                  object->name().c_str(), r_type);
1179       break;
1180
1181       // These are initial tls relocs, which are expected when
1182       // linking.
1183     case elfcpp::R_386_TLS_GD:            // Global-dynamic
1184     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
1185     case elfcpp::R_386_TLS_DESC_CALL:
1186     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
1187     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
1188     case elfcpp::R_386_TLS_IE:            // Initial-exec
1189     case elfcpp::R_386_TLS_IE_32:
1190     case elfcpp::R_386_TLS_GOTIE:
1191     case elfcpp::R_386_TLS_LE:            // Local-exec
1192     case elfcpp::R_386_TLS_LE_32:
1193       {
1194         const bool is_final = gsym->final_value_is_known();
1195         const tls::Tls_optimization optimized_type
1196             = Target_i386::optimize_tls_reloc(is_final, r_type);
1197         switch (r_type)
1198           {
1199           case elfcpp::R_386_TLS_GD:          // Global-dynamic
1200             if (optimized_type == tls::TLSOPT_NONE)
1201               {
1202                 // Create a pair of GOT entries for the module index and
1203                 // dtv-relative offset.
1204                 Output_data_got<32, false>* got
1205                     = target->got_section(symtab, layout);
1206                 if (got->add_global_tls(gsym, true))
1207                   {
1208                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1209                     unsigned int got_off = gsym->tls_got_offset(true);
1210                     rel_dyn->add_global(gsym, elfcpp::R_386_TLS_DTPMOD32,
1211                                         got, got_off);
1212                     rel_dyn->add_global(gsym, elfcpp::R_386_TLS_DTPOFF32,
1213                                         got, got_off + 4);
1214                   }
1215               }
1216             else if (optimized_type == tls::TLSOPT_TO_IE)
1217               {
1218                 // Create a GOT entry for the tp-relative offset.
1219                 Output_data_got<32, false>* got
1220                     = target->got_section(symtab, layout);
1221                 if (got->add_global(gsym))
1222                   {
1223                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1224                     unsigned int got_off = gsym->got_offset();
1225                     rel_dyn->add_global(gsym, elfcpp::R_386_TLS_TPOFF32,
1226                                         got, got_off);
1227                   }
1228               }
1229             else if (optimized_type != tls::TLSOPT_TO_LE)
1230               unsupported_reloc_global(object, r_type, gsym);
1231             break;
1232
1233           case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (~oliva url)
1234           case elfcpp::R_386_TLS_DESC_CALL:
1235             unsupported_reloc_global(object, r_type, gsym);
1236             break;
1237
1238           case elfcpp::R_386_TLS_LDM:         // Local-dynamic
1239             // FIXME: If not relaxing to LE, we need to generate a
1240             // DTPMOD32 reloc.
1241             if (optimized_type == tls::TLSOPT_NONE)
1242               {
1243                 // Create a GOT entry for the module index.
1244                 Output_data_got<32, false>* got
1245                     = target->got_section(symtab, layout);
1246                 if (got->add_global_tls(gsym, false))
1247                   {
1248                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1249                     unsigned int got_off = gsym->tls_got_offset(false);
1250                     rel_dyn->add_global(gsym, elfcpp::R_386_TLS_DTPMOD32,
1251                                         got, got_off);
1252                   }
1253               }
1254             else if (optimized_type != tls::TLSOPT_TO_LE)
1255               unsupported_reloc_global(object, r_type, gsym);
1256             break;
1257
1258           case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
1259             break;
1260
1261           case elfcpp::R_386_TLS_IE:          // Initial-exec
1262           case elfcpp::R_386_TLS_IE_32:
1263           case elfcpp::R_386_TLS_GOTIE:
1264             if (optimized_type == tls::TLSOPT_NONE)
1265               {
1266                 // Create a GOT entry for the tp-relative offset.
1267                 Output_data_got<32, false>* got
1268                     = target->got_section(symtab, layout);
1269                 if (got->add_global(gsym))
1270                   {
1271                     unsigned int dyn_r_type
1272                       = (r_type == elfcpp::R_386_TLS_IE_32
1273                          ? elfcpp::R_386_TLS_TPOFF32
1274                          : elfcpp::R_386_TLS_TPOFF);
1275                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1276                     unsigned int got_off = gsym->got_offset();
1277                     rel_dyn->add_global(gsym, dyn_r_type, got, got_off);
1278                   }
1279               }
1280             else if (optimized_type != tls::TLSOPT_TO_LE)
1281               unsupported_reloc_global(object, r_type, gsym);
1282             break;
1283
1284           case elfcpp::R_386_TLS_LE:          // Local-exec
1285           case elfcpp::R_386_TLS_LE_32:
1286             if (parameters->output_is_shared())
1287               unsupported_reloc_global(object, r_type, gsym);
1288             break;
1289
1290           default:
1291             gold_unreachable();
1292           }
1293       }
1294       break;
1295
1296     case elfcpp::R_386_32PLT:
1297     case elfcpp::R_386_TLS_GD_32:
1298     case elfcpp::R_386_TLS_GD_PUSH:
1299     case elfcpp::R_386_TLS_GD_CALL:
1300     case elfcpp::R_386_TLS_GD_POP:
1301     case elfcpp::R_386_TLS_LDM_32:
1302     case elfcpp::R_386_TLS_LDM_PUSH:
1303     case elfcpp::R_386_TLS_LDM_CALL:
1304     case elfcpp::R_386_TLS_LDM_POP:
1305     case elfcpp::R_386_USED_BY_INTEL_200:
1306     default:
1307       unsupported_reloc_global(object, r_type, gsym);
1308       break;
1309     }
1310 }
1311
1312 // Scan relocations for a section.
1313
1314 void
1315 Target_i386::scan_relocs(const General_options& options,
1316                          Symbol_table* symtab,
1317                          Layout* layout,
1318                          Sized_relobj<32, false>* object,
1319                          unsigned int data_shndx,
1320                          unsigned int sh_type,
1321                          const unsigned char* prelocs,
1322                          size_t reloc_count,
1323                          Output_section* output_section,
1324                          bool needs_special_offset_handling,
1325                          size_t local_symbol_count,
1326                          const unsigned char* plocal_symbols)
1327 {
1328   if (sh_type == elfcpp::SHT_RELA)
1329     {
1330       gold_error(_("%s: unsupported RELA reloc section"),
1331                  object->name().c_str());
1332       return;
1333     }
1334
1335   gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1336                     Target_i386::Scan>(
1337     options,
1338     symtab,
1339     layout,
1340     this,
1341     object,
1342     data_shndx,
1343     prelocs,
1344     reloc_count,
1345     output_section,
1346     needs_special_offset_handling,
1347     local_symbol_count,
1348     plocal_symbols);
1349 }
1350
1351 // Finalize the sections.
1352
1353 void
1354 Target_i386::do_finalize_sections(Layout* layout)
1355 {
1356   // Fill in some more dynamic tags.
1357   Output_data_dynamic* const odyn = layout->dynamic_data();
1358   if (odyn != NULL)
1359     {
1360       if (this->got_plt_ != NULL)
1361         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1362
1363       if (this->plt_ != NULL)
1364         {
1365           const Output_data* od = this->plt_->rel_plt();
1366           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1367           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1368           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1369         }
1370
1371       if (this->rel_dyn_ != NULL)
1372         {
1373           const Output_data* od = this->rel_dyn_;
1374           odyn->add_section_address(elfcpp::DT_REL, od);
1375           odyn->add_section_size(elfcpp::DT_RELSZ, od);
1376           odyn->add_constant(elfcpp::DT_RELENT,
1377                              elfcpp::Elf_sizes<32>::rel_size);
1378         }
1379
1380       if (!parameters->output_is_shared())
1381         {
1382           // The value of the DT_DEBUG tag is filled in by the dynamic
1383           // linker at run time, and used by the debugger.
1384           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1385         }
1386     }
1387
1388   // Emit any relocs we saved in an attempt to avoid generating COPY
1389   // relocs.
1390   if (this->copy_relocs_ == NULL)
1391     return;
1392   if (this->copy_relocs_->any_to_emit())
1393     {
1394       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1395       this->copy_relocs_->emit(rel_dyn);
1396     }
1397   delete this->copy_relocs_;
1398   this->copy_relocs_ = NULL;
1399 }
1400
1401 // Return whether a direct absolute static relocation needs to be applied.
1402 // In cases where Scan::local() or Scan::global() has created
1403 // a dynamic relocation other than R_386_RELATIVE, the addend
1404 // of the relocation is carried in the data, and we must not
1405 // apply the static relocation.
1406
1407 inline bool
1408 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1409                                                  bool is_absolute_ref,
1410                                                  bool is_function_call,
1411                                                  bool is_32bit)
1412 {
1413   // For local symbols, we will have created a non-RELATIVE dynamic
1414   // relocation only if (a) the output is position independent,
1415   // (b) the relocation is absolute (not pc- or segment-relative), and
1416   // (c) the relocation is not 32 bits wide.
1417   if (gsym == NULL)
1418     return !(parameters->output_is_position_independent()
1419              && is_absolute_ref
1420              && !is_32bit);
1421
1422   // For global symbols, we use the same helper routines used in the scan pass.
1423   return !(gsym->needs_dynamic_reloc(is_absolute_ref, is_function_call)
1424            && !gsym->can_use_relative_reloc(is_function_call));
1425 }
1426
1427 // Perform a relocation.
1428
1429 inline bool
1430 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1431                                 Target_i386* target,
1432                                 size_t relnum,
1433                                 const elfcpp::Rel<32, false>& rel,
1434                                 unsigned int r_type,
1435                                 const Sized_symbol<32>* gsym,
1436                                 const Symbol_value<32>* psymval,
1437                                 unsigned char* view,
1438                                 elfcpp::Elf_types<32>::Elf_Addr address,
1439                                 off_t view_size)
1440 {
1441   if (this->skip_call_tls_get_addr_)
1442     {
1443       if (r_type != elfcpp::R_386_PLT32
1444           || gsym == NULL
1445           || strcmp(gsym->name(), "___tls_get_addr") != 0)
1446         gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1447                                _("missing expected TLS relocation"));
1448       else
1449         {
1450           this->skip_call_tls_get_addr_ = false;
1451           return false;
1452         }
1453     }
1454
1455   // Pick the value to use for symbols defined in shared objects.
1456   Symbol_value<32> symval;
1457   if (gsym != NULL
1458       && (gsym->is_from_dynobj()
1459           || (parameters->output_is_shared()
1460               && gsym->is_preemptible()))
1461       && gsym->has_plt_offset())
1462     {
1463       symval.set_output_value(target->plt_section()->address()
1464                               + gsym->plt_offset());
1465       psymval = &symval;
1466     }
1467
1468   const Sized_relobj<32, false>* object = relinfo->object;
1469
1470   // Get the GOT offset if needed.
1471   // The GOT pointer points to the end of the GOT section.
1472   // We need to subtract the size of the GOT section to get
1473   // the actual offset to use in the relocation.
1474   bool have_got_offset = false;
1475   unsigned int got_offset = 0;
1476   switch (r_type)
1477     {
1478     case elfcpp::R_386_GOT32:
1479       if (gsym != NULL)
1480         {
1481           gold_assert(gsym->has_got_offset());
1482           got_offset = gsym->got_offset() - target->got_size();
1483         }
1484       else
1485         {
1486           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1487           gold_assert(object->local_has_got_offset(r_sym));
1488           got_offset = object->local_got_offset(r_sym) - target->got_size();
1489         }
1490       have_got_offset = true;
1491       break;
1492
1493     default:
1494       break;
1495     }
1496
1497   switch (r_type)
1498     {
1499     case elfcpp::R_386_NONE:
1500     case elfcpp::R_386_GNU_VTINHERIT:
1501     case elfcpp::R_386_GNU_VTENTRY:
1502       break;
1503
1504     case elfcpp::R_386_32:
1505       if (should_apply_static_reloc(gsym, true, false, true))
1506         Relocate_functions<32, false>::rel32(view, object, psymval);
1507       break;
1508
1509     case elfcpp::R_386_PC32:
1510       {
1511         bool is_function_call = (gsym != NULL
1512                                  && gsym->type() == elfcpp::STT_FUNC);
1513         if (should_apply_static_reloc(gsym, false, is_function_call, true))
1514           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1515       }
1516       break;
1517
1518     case elfcpp::R_386_16:
1519       if (should_apply_static_reloc(gsym, true, false, false))
1520         Relocate_functions<32, false>::rel16(view, object, psymval);
1521       break;
1522
1523     case elfcpp::R_386_PC16:
1524       {
1525         bool is_function_call = (gsym != NULL
1526                                  && gsym->type() == elfcpp::STT_FUNC);
1527         if (should_apply_static_reloc(gsym, false, is_function_call, false))
1528           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1529       }
1530       break;
1531
1532     case elfcpp::R_386_8:
1533       if (should_apply_static_reloc(gsym, true, false, false))
1534         Relocate_functions<32, false>::rel8(view, object, psymval);
1535       break;
1536
1537     case elfcpp::R_386_PC8:
1538       {
1539         bool is_function_call = (gsym != NULL
1540                                  && gsym->type() == elfcpp::STT_FUNC);
1541         if (should_apply_static_reloc(gsym, false, is_function_call, false))
1542           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1543       }
1544       break;
1545
1546     case elfcpp::R_386_PLT32:
1547       gold_assert(gsym == NULL
1548                   || gsym->has_plt_offset()
1549                   || gsym->final_value_is_known());
1550       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1551       break;
1552
1553     case elfcpp::R_386_GOT32:
1554       gold_assert(have_got_offset);
1555       Relocate_functions<32, false>::rel32(view, got_offset);
1556       break;
1557
1558     case elfcpp::R_386_GOTOFF:
1559       {
1560         elfcpp::Elf_types<32>::Elf_Addr value;
1561         value = (psymval->value(object, 0)
1562                  - target->got_plt_section()->address());
1563         Relocate_functions<32, false>::rel32(view, value);
1564       }
1565       break;
1566
1567     case elfcpp::R_386_GOTPC:
1568       {
1569         elfcpp::Elf_types<32>::Elf_Addr value;
1570         value = target->got_plt_section()->address();
1571         Relocate_functions<32, false>::pcrel32(view, value, address);
1572       }
1573       break;
1574
1575     case elfcpp::R_386_COPY:
1576     case elfcpp::R_386_GLOB_DAT:
1577     case elfcpp::R_386_JUMP_SLOT:
1578     case elfcpp::R_386_RELATIVE:
1579       // These are outstanding tls relocs, which are unexpected when
1580       // linking.
1581     case elfcpp::R_386_TLS_TPOFF:
1582     case elfcpp::R_386_TLS_DTPMOD32:
1583     case elfcpp::R_386_TLS_DTPOFF32:
1584     case elfcpp::R_386_TLS_TPOFF32:
1585     case elfcpp::R_386_TLS_DESC:
1586       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1587                              _("unexpected reloc %u in object file"),
1588                              r_type);
1589       break;
1590
1591       // These are initial tls relocs, which are expected when
1592       // linking.
1593     case elfcpp::R_386_TLS_GD:             // Global-dynamic
1594     case elfcpp::R_386_TLS_GOTDESC:        // Global-dynamic (from ~oliva url)
1595     case elfcpp::R_386_TLS_DESC_CALL:
1596     case elfcpp::R_386_TLS_LDM:            // Local-dynamic
1597     case elfcpp::R_386_TLS_LDO_32:         // Alternate local-dynamic
1598     case elfcpp::R_386_TLS_IE:             // Initial-exec
1599     case elfcpp::R_386_TLS_IE_32:
1600     case elfcpp::R_386_TLS_GOTIE:
1601     case elfcpp::R_386_TLS_LE:             // Local-exec
1602     case elfcpp::R_386_TLS_LE_32:
1603       this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1604                          view, address, view_size);
1605       break;
1606
1607     case elfcpp::R_386_32PLT:
1608     case elfcpp::R_386_TLS_GD_32:
1609     case elfcpp::R_386_TLS_GD_PUSH:
1610     case elfcpp::R_386_TLS_GD_CALL:
1611     case elfcpp::R_386_TLS_GD_POP:
1612     case elfcpp::R_386_TLS_LDM_32:
1613     case elfcpp::R_386_TLS_LDM_PUSH:
1614     case elfcpp::R_386_TLS_LDM_CALL:
1615     case elfcpp::R_386_TLS_LDM_POP:
1616     case elfcpp::R_386_USED_BY_INTEL_200:
1617     default:
1618       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1619                              _("unsupported reloc %u"),
1620                              r_type);
1621       break;
1622     }
1623
1624   return true;
1625 }
1626
1627 // Perform a TLS relocation.
1628
1629 inline void
1630 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1631                                     Target_i386* target,
1632                                     size_t relnum,
1633                                     const elfcpp::Rel<32, false>& rel,
1634                                     unsigned int r_type,
1635                                     const Sized_symbol<32>* gsym,
1636                                     const Symbol_value<32>* psymval,
1637                                     unsigned char* view,
1638                                     elfcpp::Elf_types<32>::Elf_Addr,
1639                                     off_t view_size)
1640 {
1641   Output_segment* tls_segment = relinfo->layout->tls_segment();
1642
1643   const Sized_relobj<32, false>* object = relinfo->object;
1644
1645   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1646
1647   const bool is_final = (gsym == NULL
1648                          ? !parameters->output_is_position_independent()
1649                          : gsym->final_value_is_known());
1650   const tls::Tls_optimization optimized_type
1651       = Target_i386::optimize_tls_reloc(is_final, r_type);
1652   switch (r_type)
1653     {
1654     case elfcpp::R_386_TLS_GD:           // Global-dynamic
1655       if (optimized_type == tls::TLSOPT_TO_LE)
1656         {
1657           gold_assert(tls_segment != NULL);
1658           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1659                              rel, r_type, value, view,
1660                              view_size);
1661           break;
1662         }
1663       else
1664         {
1665           unsigned int got_offset;
1666           if (gsym != NULL)
1667             {
1668               gold_assert(gsym->has_tls_got_offset(true));
1669               got_offset = gsym->tls_got_offset(true) - target->got_size();
1670             }
1671           else
1672             {
1673               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1674               gold_assert(object->local_has_tls_got_offset(r_sym, true));
1675               got_offset = (object->local_tls_got_offset(r_sym, true)
1676                             - target->got_size());
1677             }
1678           if (optimized_type == tls::TLSOPT_TO_IE)
1679             {
1680               gold_assert(tls_segment != NULL);
1681               this->tls_gd_to_ie(relinfo, relnum, tls_segment,
1682                                  rel, r_type, got_offset, view,
1683                                  view_size);
1684               break;
1685             }
1686           else if (optimized_type == tls::TLSOPT_NONE)
1687             {
1688               // Relocate the field with the offset of the pair of GOT
1689               // entries.
1690               Relocate_functions<32, false>::rel32(view, got_offset);
1691               break;
1692             }
1693         }
1694       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1695                              _("unsupported reloc %u"),
1696                              r_type);
1697       break;
1698
1699     case elfcpp::R_386_TLS_GOTDESC:      // Global-dynamic (from ~oliva url)
1700     case elfcpp::R_386_TLS_DESC_CALL:
1701       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1702                              _("unsupported reloc %u"),
1703                              r_type);
1704       break;
1705
1706     case elfcpp::R_386_TLS_LDM:          // Local-dynamic
1707       if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1708         {
1709           gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1710                                  _("both SUN and GNU model "
1711                                    "TLS relocations"));
1712           break;
1713         }
1714       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1715       if (optimized_type == tls::TLSOPT_TO_LE)
1716         {
1717           gold_assert(tls_segment != NULL);
1718           this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1719                              value, view, view_size);
1720           break;
1721         }
1722       else if (optimized_type == tls::TLSOPT_NONE)
1723         {
1724           // Relocate the field with the offset of the GOT entry for
1725           // the module index.
1726           unsigned int got_offset;
1727           if (gsym != NULL)
1728             {
1729               gold_assert(gsym->has_tls_got_offset(false));
1730               got_offset = gsym->tls_got_offset(false) - target->got_size();
1731             }
1732           else
1733             {
1734               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1735               gold_assert(object->local_has_tls_got_offset(r_sym, false));
1736               got_offset = (object->local_tls_got_offset(r_sym, false)
1737                             - target->got_size());
1738             }
1739           Relocate_functions<32, false>::rel32(view, got_offset);
1740           break;
1741         }
1742       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1743                              _("unsupported reloc %u"),
1744                              r_type);
1745       break;
1746
1747     case elfcpp::R_386_TLS_LDO_32:       // Alternate local-dynamic
1748       // This reloc can appear in debugging sections, in which case we
1749       // won't see the TLS_LDM reloc.  The local_dynamic_type field
1750       // tells us this.
1751       gold_assert(tls_segment != NULL);
1752       if (optimized_type != tls::TLSOPT_TO_LE
1753           || this->local_dynamic_type_ == LOCAL_DYNAMIC_NONE)
1754         value = value - tls_segment->vaddr();
1755       else if (this->local_dynamic_type_ == LOCAL_DYNAMIC_GNU)
1756         value = value - (tls_segment->vaddr() + tls_segment->memsz());
1757       else
1758         value = tls_segment->vaddr() + tls_segment->memsz() - value;
1759       Relocate_functions<32, false>::rel32(view, value);
1760       break;
1761
1762     case elfcpp::R_386_TLS_IE:           // Initial-exec
1763     case elfcpp::R_386_TLS_GOTIE:
1764     case elfcpp::R_386_TLS_IE_32:
1765       if (optimized_type == tls::TLSOPT_TO_LE)
1766         {
1767           gold_assert(tls_segment != NULL);
1768           Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1769                                               rel, r_type, value, view,
1770                                               view_size);
1771           break;
1772         }
1773       else if (optimized_type == tls::TLSOPT_NONE)
1774         {
1775           // Relocate the field with the offset of the GOT entry for
1776           // the tp-relative offset of the symbol.
1777           unsigned int got_offset;
1778           if (gsym != NULL)
1779             {
1780               gold_assert(gsym->has_got_offset());
1781               got_offset = gsym->got_offset();
1782             }
1783           else
1784             {
1785               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1786               gold_assert(object->local_has_got_offset(r_sym));
1787               got_offset = object->local_got_offset(r_sym);
1788             }
1789           // For the R_386_TLS_IE relocation, we need to apply the
1790           // absolute address of the GOT entry.
1791           if (r_type == elfcpp::R_386_TLS_IE)
1792             got_offset += target->got_plt_section()->address();
1793           // All GOT offsets are relative to the end of the GOT.
1794           got_offset -= target->got_size();
1795           Relocate_functions<32, false>::rel32(view, got_offset);
1796           break;
1797         }
1798       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1799                              _("unsupported reloc %u"),
1800                              r_type);
1801       break;
1802
1803     case elfcpp::R_386_TLS_LE:           // Local-exec
1804       gold_assert(tls_segment != NULL);
1805       value = value - (tls_segment->vaddr() + tls_segment->memsz());
1806       Relocate_functions<32, false>::rel32(view, value);
1807       break;
1808
1809     case elfcpp::R_386_TLS_LE_32:
1810       gold_assert(tls_segment != NULL);
1811       value = tls_segment->vaddr() + tls_segment->memsz() - value;
1812       Relocate_functions<32, false>::rel32(view, value);
1813       break;
1814     }
1815 }
1816
1817 // Do a relocation in which we convert a TLS General-Dynamic to a
1818 // Local-Exec.
1819
1820 inline void
1821 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1822                                     size_t relnum,
1823                                     Output_segment* tls_segment,
1824                                     const elfcpp::Rel<32, false>& rel,
1825                                     unsigned int,
1826                                     elfcpp::Elf_types<32>::Elf_Addr value,
1827                                     unsigned char* view,
1828                                     off_t view_size)
1829 {
1830   // leal foo(,%reg,1),%eax; call ___tls_get_addr
1831   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1832   // leal foo(%reg),%eax; call ___tls_get_addr
1833   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1834
1835   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1836   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1837
1838   unsigned char op1 = view[-1];
1839   unsigned char op2 = view[-2];
1840
1841   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1842                  op2 == 0x8d || op2 == 0x04);
1843   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1844
1845   int roff = 5;
1846
1847   if (op2 == 0x04)
1848     {
1849       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1850       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1851       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1852                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1853       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1854     }
1855   else
1856     {
1857       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1858                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1859       if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1860           && view[9] == 0x90)
1861         {
1862           // There is a trailing nop.  Use the size byte subl.
1863           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1864           roff = 6;
1865         }
1866       else
1867         {
1868           // Use the five byte subl.
1869           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1870         }
1871     }
1872
1873   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1874   Relocate_functions<32, false>::rel32(view + roff, value);
1875
1876   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1877   // We can skip it.
1878   this->skip_call_tls_get_addr_ = true;
1879 }
1880
1881 // Do a relocation in which we convert a TLS General-Dynamic to a
1882 // Initial-Exec.
1883
1884 inline void
1885 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
1886                                     size_t relnum,
1887                                     Output_segment* tls_segment,
1888                                     const elfcpp::Rel<32, false>& rel,
1889                                     unsigned int,
1890                                     elfcpp::Elf_types<32>::Elf_Addr value,
1891                                     unsigned char* view,
1892                                     off_t view_size)
1893 {
1894   // leal foo(,%ebx,1),%eax; call ___tls_get_addr
1895   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
1896
1897   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1898   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1899
1900   unsigned char op1 = view[-1];
1901   unsigned char op2 = view[-2];
1902
1903   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1904                  op2 == 0x8d || op2 == 0x04);
1905   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1906
1907   int roff = 5;
1908
1909   // FIXME: For now, support only one form.
1910   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1911                  op1 == 0x8d && op2 == 0x04);
1912
1913   if (op2 == 0x04)
1914     {
1915       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1916       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1917       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1918                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1919       memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
1920     }
1921   else
1922     {
1923       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1924                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1925       if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1926           && view[9] == 0x90)
1927         {
1928           // FIXME: This is not the right instruction sequence.
1929           // There is a trailing nop.  Use the size byte subl.
1930           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1931           roff = 6;
1932         }
1933       else
1934         {
1935           // FIXME: This is not the right instruction sequence.
1936           // Use the five byte subl.
1937           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1938         }
1939     }
1940
1941   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1942   Relocate_functions<32, false>::rel32(view + roff, value);
1943
1944   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1945   // We can skip it.
1946   this->skip_call_tls_get_addr_ = true;
1947 }
1948
1949 // Do a relocation in which we convert a TLS Local-Dynamic to a
1950 // Local-Exec.
1951
1952 inline void
1953 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
1954                                     size_t relnum,
1955                                     Output_segment*,
1956                                     const elfcpp::Rel<32, false>& rel,
1957                                     unsigned int,
1958                                     elfcpp::Elf_types<32>::Elf_Addr,
1959                                     unsigned char* view,
1960                                     off_t view_size)
1961 {
1962   // leal foo(%reg), %eax; call ___tls_get_addr
1963   // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1964
1965   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1966   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1967
1968   // FIXME: Does this test really always pass?
1969   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1970                  view[-2] == 0x8d && view[-1] == 0x83);
1971
1972   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1973
1974   memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1975
1976   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1977   // We can skip it.
1978   this->skip_call_tls_get_addr_ = true;
1979 }
1980
1981 // Do a relocation in which we convert a TLS Initial-Exec to a
1982 // Local-Exec.
1983
1984 inline void
1985 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1986                                     size_t relnum,
1987                                     Output_segment* tls_segment,
1988                                     const elfcpp::Rel<32, false>& rel,
1989                                     unsigned int r_type,
1990                                     elfcpp::Elf_types<32>::Elf_Addr value,
1991                                     unsigned char* view,
1992                                     off_t view_size)
1993 {
1994   // We have to actually change the instructions, which means that we
1995   // need to examine the opcodes to figure out which instruction we
1996   // are looking at.
1997   if (r_type == elfcpp::R_386_TLS_IE)
1998     {
1999       // movl %gs:XX,%eax  ==>  movl $YY,%eax
2000       // movl %gs:XX,%reg  ==>  movl $YY,%reg
2001       // addl %gs:XX,%reg  ==>  addl $YY,%reg
2002       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2003       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2004
2005       unsigned char op1 = view[-1];
2006       if (op1 == 0xa1)
2007         {
2008           // movl XX,%eax  ==>  movl $YY,%eax
2009           view[-1] = 0xb8;
2010         }
2011       else
2012         {
2013           tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2014
2015           unsigned char op2 = view[-2];
2016           if (op2 == 0x8b)
2017             {
2018               // movl XX,%reg  ==>  movl $YY,%reg
2019               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2020                              (op1 & 0xc7) == 0x05);
2021               view[-2] = 0xc7;
2022               view[-1] = 0xc0 | ((op1 >> 3) & 7);
2023             }
2024           else if (op2 == 0x03)
2025             {
2026               // addl XX,%reg  ==>  addl $YY,%reg
2027               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2028                              (op1 & 0xc7) == 0x05);
2029               view[-2] = 0x81;
2030               view[-1] = 0xc0 | ((op1 >> 3) & 7);
2031             }
2032           else
2033             tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2034         }
2035     }
2036   else
2037     {
2038       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
2039       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
2040       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
2041       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2042       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2043
2044       unsigned char op1 = view[-1];
2045       unsigned char op2 = view[-2];
2046       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2047                      (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2048       if (op2 == 0x8b)
2049         {
2050           // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
2051           view[-2] = 0xc7;
2052           view[-1] = 0xc0 | ((op1 >> 3) & 7);
2053         }
2054       else if (op2 == 0x2b)
2055         {
2056           // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
2057           view[-2] = 0x81;
2058           view[-1] = 0xe8 | ((op1 >> 3) & 7);
2059         }
2060       else if (op2 == 0x03)
2061         {
2062           // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
2063           view[-2] = 0x81;
2064           view[-1] = 0xc0 | ((op1 >> 3) & 7);
2065         }
2066       else
2067         tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2068     }
2069
2070   value = tls_segment->vaddr() + tls_segment->memsz() - value;
2071   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2072     value = - value;
2073
2074   Relocate_functions<32, false>::rel32(view, value);
2075 }
2076
2077 // Relocate section data.
2078
2079 void
2080 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2081                               unsigned int sh_type,
2082                               const unsigned char* prelocs,
2083                               size_t reloc_count,
2084                               Output_section* output_section,
2085                               bool needs_special_offset_handling,
2086                               unsigned char* view,
2087                               elfcpp::Elf_types<32>::Elf_Addr address,
2088                               off_t view_size)
2089 {
2090   gold_assert(sh_type == elfcpp::SHT_REL);
2091
2092   gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2093                          Target_i386::Relocate>(
2094     relinfo,
2095     this,
2096     prelocs,
2097     reloc_count,
2098     output_section,
2099     needs_special_offset_handling,
2100     view,
2101     address,
2102     view_size);
2103 }
2104
2105 // Return the value to use for a dynamic which requires special
2106 // treatment.  This is how we support equality comparisons of function
2107 // pointers across shared library boundaries, as described in the
2108 // processor specific ABI supplement.
2109
2110 uint64_t
2111 Target_i386::do_dynsym_value(const Symbol* gsym) const
2112 {
2113   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2114   return this->plt_section()->address() + gsym->plt_offset();
2115 }
2116
2117 // Return a string used to fill a code section with nops to take up
2118 // the specified length.
2119
2120 std::string
2121 Target_i386::do_code_fill(off_t length)
2122 {
2123   if (length >= 16)
2124     {
2125       // Build a jmp instruction to skip over the bytes.
2126       unsigned char jmp[5];
2127       jmp[0] = 0xe9;
2128       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2129       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2130               + std::string(length - 5, '\0'));
2131     }
2132
2133   // Nop sequences of various lengths.
2134   const char nop1[1] = { 0x90 };                   // nop
2135   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
2136   const char nop3[3] = { 0x8d, 0x76, 0x00 };       // leal 0(%esi),%esi
2137   const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00};  // leal 0(%esi,1),%esi
2138   const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26,   // nop
2139                          0x00 };                   // leal 0(%esi,1),%esi
2140   const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00,   // leal 0L(%esi),%esi
2141                          0x00, 0x00 };
2142   const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00,   // leal 0L(%esi,1),%esi
2143                          0x00, 0x00, 0x00 };
2144   const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26,   // nop
2145                          0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2146   const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc,   // movl %esi,%esi
2147                          0x27, 0x00, 0x00, 0x00,   // leal 0L(%edi,1),%edi
2148                          0x00 };
2149   const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2150                            0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2151                            0x00, 0x00 };
2152   const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2153                            0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2154                            0x00, 0x00, 0x00 };
2155   const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2156                            0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2157                            0x00, 0x00, 0x00, 0x00 };
2158   const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2159                            0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2160                            0x27, 0x00, 0x00, 0x00,
2161                            0x00 };
2162   const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2163                            0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2164                            0xbc, 0x27, 0x00, 0x00,
2165                            0x00, 0x00 };
2166   const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2167                            0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2168                            0x90, 0x90, 0x90, 0x90,
2169                            0x90, 0x90, 0x90 };
2170
2171   const char* nops[16] = {
2172     NULL,
2173     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2174     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2175   };
2176
2177   return std::string(nops[length], length);
2178 }
2179
2180 // The selector for i386 object files.
2181
2182 class Target_selector_i386 : public Target_selector
2183 {
2184 public:
2185   Target_selector_i386()
2186     : Target_selector(elfcpp::EM_386, 32, false)
2187   { }
2188
2189   Target*
2190   recognize(int machine, int osabi, int abiversion);
2191
2192  private:
2193   Target_i386* target_;
2194 };
2195
2196 // Recognize an i386 object file when we already know that the machine
2197 // number is EM_386.
2198
2199 Target*
2200 Target_selector_i386::recognize(int, int, int)
2201 {
2202   if (this->target_ == NULL)
2203     this->target_ = new Target_i386();
2204   return this->target_;
2205 }
2206
2207 Target_selector_i386 target_selector_i386;
2208
2209 } // End anonymous namespace.
This page took 0.141755 seconds and 4 git commands to generate.