Added a testsuite. More support for COPY relocations.
[platform/upstream/binutils.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 #include "gold.h"
4
5 #include <cstring>
6
7 #include "elfcpp.h"
8 #include "reloc.h"
9 #include "i386.h"
10 #include "object.h"
11 #include "symtab.h"
12 #include "layout.h"
13 #include "output.h"
14 #include "target.h"
15 #include "target-reloc.h"
16 #include "target-select.h"
17
18 namespace
19 {
20
21 using namespace gold;
22
23 class Output_data_plt_i386;
24
25 // The i386 target class.
26
27 class Target_i386 : public Sized_target<32, false>
28 {
29  public:
30   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
31
32   Target_i386()
33     : Sized_target<32, false>(&i386_info),
34       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
35       copy_relocs_(NULL), dynbss_(NULL)
36   { }
37
38   // Scan the relocations to look for symbol adjustments.
39   void
40   scan_relocs(const General_options& options,
41               Symbol_table* symtab,
42               Layout* layout,
43               Sized_relobj<32, false>* object,
44               unsigned int data_shndx,
45               unsigned int sh_type,
46               const unsigned char* prelocs,
47               size_t reloc_count,
48               size_t local_symbol_count,
49               const unsigned char* plocal_symbols,
50               Symbol** global_symbols);
51
52   // Finalize the sections.
53   void
54   do_finalize_sections(Layout*);
55
56   // Relocate a section.
57   void
58   relocate_section(const Relocate_info<32, false>*,
59                    unsigned int sh_type,
60                    const unsigned char* prelocs,
61                    size_t reloc_count,
62                    unsigned char* view,
63                    elfcpp::Elf_types<32>::Elf_Addr view_address,
64                    off_t view_size);
65
66  private:
67   // The class which scans relocations.
68   struct Scan
69   {
70     inline void
71     local(const General_options& options, Symbol_table* symtab,
72           Layout* layout, Target_i386* target,
73           Sized_relobj<32, false>* object,
74           unsigned int data_shndx,
75           const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
76           const elfcpp::Sym<32, false>& lsym);
77
78     inline void
79     global(const General_options& options, Symbol_table* symtab,
80            Layout* layout, Target_i386* target,
81            Sized_relobj<32, false>* object,
82            unsigned int data_shndx,
83            const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
84            Symbol* gsym);
85   };
86
87   // The class which implements relocation.
88   class Relocate
89   {
90    public:
91     Relocate()
92       : skip_call_tls_get_addr_(false)
93     { }
94
95     ~Relocate()
96     {
97       if (this->skip_call_tls_get_addr_)
98         {
99           // FIXME: This needs to specify the location somehow.
100           fprintf(stderr, _("%s: missing expected TLS relocation\n"),
101                   program_name);
102           gold_exit(false);
103         }
104     }
105
106     // Do a relocation.  Return false if the caller should not issue
107     // any warnings about this relocation.
108     inline bool
109     relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
110              const elfcpp::Rel<32, false>&,
111              unsigned int r_type, const Sized_symbol<32>*,
112              elfcpp::Elf_types<32>::Elf_Addr,
113              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
114              off_t);
115
116    private:
117     // Do a TLS relocation.
118     inline void
119     relocate_tls(const Relocate_info<32, false>*, size_t relnum,
120                  const elfcpp::Rel<32, false>&,
121                  unsigned int r_type, const Sized_symbol<32>*,
122                  elfcpp::Elf_types<32>::Elf_Addr,
123                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
124
125     // Do a TLS Initial-Exec to Local-Exec transition.
126     static inline void
127     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
128                  Output_segment* tls_segment,
129                  const elfcpp::Rel<32, false>&, unsigned int r_type,
130                  elfcpp::Elf_types<32>::Elf_Addr value,
131                  unsigned char* view,
132                  off_t view_size);
133
134     // Do a TLS Global-Dynamic to Local-Exec transition.
135     inline void
136     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
137                  Output_segment* tls_segment,
138                  const elfcpp::Rel<32, false>&, unsigned int r_type,
139                  elfcpp::Elf_types<32>::Elf_Addr value,
140                  unsigned char* view,
141                  off_t view_size);
142
143     // Check the range for a TLS relocation.
144     static inline void
145     check_range(const Relocate_info<32, false>*, size_t relnum,
146                 const elfcpp::Rel<32, false>&, off_t, off_t);
147
148     // Check the validity of a TLS relocation.  This is like assert.
149     static inline void
150     check_tls(const Relocate_info<32, false>*, size_t relnum,
151               const elfcpp::Rel<32, false>&, bool);
152
153     // This is set if we should skip the next reloc, which should be a
154     // PLT32 reloc against ___tls_get_addr.
155     bool skip_call_tls_get_addr_;
156   };
157
158   // Adjust TLS relocation type based on the options and whether this
159   // is a local symbol.
160   static unsigned int
161   optimize_tls_reloc(const General_options*, bool is_final, int r_type);
162
163   // Get the GOT section, creating it if necessary.
164   Output_data_got<32, false>*
165   got_section(const General_options*, Symbol_table*, Layout*);
166
167   // Create a PLT entry for a global symbol.
168   void
169   make_plt_entry(const General_options* options, Symbol_table*,
170                  Layout*, Symbol*);
171
172   // Get the PLT section.
173   Output_data_plt_i386*
174   plt_section() const
175   {
176     gold_assert(this->plt_ != NULL);
177     return this->plt_;
178   }
179
180   // Get the dynamic reloc section, creating it if necessary.
181   Reloc_section*
182   rel_dyn_section(Layout*);
183
184   // Copy a relocation against a global symbol.
185   void
186   copy_reloc(const General_options*, Symbol_table*, Layout*,
187              Sized_relobj<32, false>*, unsigned int,
188              Symbol*, const elfcpp::Rel<32, false>&);
189
190   // Information about this specific target which we pass to the
191   // general Target structure.
192   static const Target::Target_info i386_info;
193
194   // The GOT section.
195   Output_data_got<32, false>* got_;
196   // The PLT section.
197   Output_data_plt_i386* plt_;
198   // The GOT PLT section.
199   Output_data_space* got_plt_;
200   // The dynamic reloc section.
201   Reloc_section* rel_dyn_;
202   // Relocs saved to avoid a COPY reloc.
203   Copy_relocs<32, false>* copy_relocs_;
204   // Space for variables copied with a COPY reloc.
205   Output_data_space* dynbss_;
206 };
207
208 const Target::Target_info Target_i386::i386_info =
209 {
210   32,                   // size
211   false,                // is_big_endian
212   elfcpp::EM_386,       // machine_code
213   false,                // has_make_symbol
214   false,                // has_resolve
215   "/usr/lib/libc.so.1", // dynamic_linker
216   0x08048000,           // text_segment_address
217   0x1000,               // abi_pagesize
218   0x1000                // common_pagesize
219 };
220
221 // Get the GOT section, creating it if necessary.
222
223 Output_data_got<32, false>*
224 Target_i386::got_section(const General_options* options, Symbol_table* symtab,
225                          Layout* layout)
226 {
227   if (this->got_ == NULL)
228     {
229       gold_assert(options != NULL && symtab != NULL && layout != NULL);
230
231       this->got_ = new Output_data_got<32, false>(options);
232
233       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
234                                       elfcpp::SHF_ALLOC, this->got_);
235
236       // The old GNU linker creates a .got.plt section.  We just
237       // create another set of data in the .got section.  Note that we
238       // always create a PLT if we create a GOT, although the PLT
239       // might be empty.
240       this->got_plt_ = new Output_data_space(4);
241       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
242                                       elfcpp::SHF_ALLOC, this->got_plt_);
243
244       // The first three entries are reserved.
245       this->got_plt_->set_space_size(3 * 4);
246
247       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
248       symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_",
249                                     this->got_plt_,
250                                     0, 0, elfcpp::STT_OBJECT,
251                                     elfcpp::STB_GLOBAL,
252                                     elfcpp::STV_HIDDEN, 0,
253                                     false, false);
254     }
255
256   return this->got_;
257 }
258
259 // Get the dynamic reloc section, creating it if necessary.
260
261 Target_i386::Reloc_section*
262 Target_i386::rel_dyn_section(Layout* layout)
263 {
264   if (this->rel_dyn_ == NULL)
265     {
266       gold_assert(layout != NULL);
267       this->rel_dyn_ = new Reloc_section();
268       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
269                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
270     }
271   return this->rel_dyn_;
272 }
273
274 // A class to handle the PLT data.
275
276 class Output_data_plt_i386 : public Output_section_data
277 {
278  public:
279   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
280
281   Output_data_plt_i386(Layout*, Output_data_space*, bool is_shared);
282
283   // Add an entry to the PLT.
284   void
285   add_entry(Symbol* gsym);
286
287  private:
288   // The size of an entry in the PLT.
289   static const int plt_entry_size = 16;
290
291   // The first entry in the PLT for an executable.
292   static unsigned char exec_first_plt_entry[plt_entry_size];
293
294   // The first entry in the PLT for a shared object.
295   static unsigned char dyn_first_plt_entry[plt_entry_size];
296
297   // Other entries in the PLT for an executable.
298   static unsigned char exec_plt_entry[plt_entry_size];
299
300   // Other entries in the PLT for a shared object.
301   static unsigned char dyn_plt_entry[plt_entry_size];
302
303   // Set the final size.
304   void
305   do_set_address(uint64_t, off_t)
306   { this->set_data_size((this->count_ + 1) * plt_entry_size); }
307
308   // Write out the PLT data.
309   void
310   do_write(Output_file*);
311
312   // The reloc section.
313   Reloc_section* rel_;
314   // The .got.plt section.
315   Output_data_space* got_plt_;
316   // The number of PLT entries.
317   unsigned int count_;
318   // Whether we are generated a shared object.
319   bool is_shared_;
320 };
321
322 // Create the PLT section.  The ordinary .got section is an argument,
323 // since we need to refer to the start.  We also create our own .got
324 // section just for PLT entries.
325
326 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
327                                            Output_data_space* got_plt,
328                                            bool is_shared)
329   : Output_section_data(4), got_plt_(got_plt), is_shared_(is_shared)
330 {
331   this->rel_ = new Reloc_section();
332   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
333                                   elfcpp::SHF_ALLOC, this->rel_);
334 }
335
336 // Add an entry to the PLT.
337
338 void
339 Output_data_plt_i386::add_entry(Symbol* gsym)
340 {
341   gold_assert(!gsym->has_plt_offset());
342
343   // Note that when setting the PLT offset we skip the initial
344   // reserved PLT entry.
345   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
346
347   ++this->count_;
348
349   off_t got_offset = this->got_plt_->data_size();
350
351   // Every PLT entry needs a GOT entry which points back to the PLT
352   // entry (this will be changed by the dynamic linker, normally
353   // lazily when the function is called).
354   this->got_plt_->set_space_size(got_offset + 4);
355
356   // Every PLT entry needs a reloc.
357   this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
358                          got_offset);
359
360   // Note that we don't need to save the symbol.  The contents of the
361   // PLT are independent of which symbols are used.  The symbols only
362   // appear in the relocations.
363 }
364
365 // The first entry in the PLT for an executable.
366
367 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
368 {
369   0xff, 0x35,   // pushl contents of memory address
370   0, 0, 0, 0,   // replaced with address of .got + 4
371   0xff, 0x25,   // jmp indirect
372   0, 0, 0, 0,   // replaced with address of .got + 8
373   0, 0, 0, 0    // unused
374 };
375
376 // The first entry in the PLT for a shared object.
377
378 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
379 {
380   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
381   0xff, 0xa3, 8, 0, 0, 0,       // jmp *8(%ebx)
382   0, 0, 0, 0                    // unused
383 };
384
385 // Subsequent entries in the PLT for an executable.
386
387 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
388 {
389   0xff, 0x25,   // jmp indirect
390   0, 0, 0, 0,   // replaced with address of symbol in .got
391   0x68,         // pushl immediate
392   0, 0, 0, 0,   // replaced with offset into relocation table
393   0xe9,         // jmp relative
394   0, 0, 0, 0    // replaced with offset to start of .plt
395 };
396
397 // Subsequent entries in the PLT for a shared object.
398
399 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
400 {
401   0xff, 0xa3,   // jmp *offset(%ebx)
402   0, 0, 0, 0,   // replaced with offset of symbol in .got
403   0x68,         // pushl immediate
404   0, 0, 0, 0,   // replaced with offset into relocation table
405   0xe9,         // jmp relative
406   0, 0, 0, 0    // replaced with offset to start of .plt
407 };
408
409 // Write out the PLT.  This uses the hand-coded instructions above,
410 // and adjusts them as needed.  This is all specified by the i386 ELF
411 // Processor Supplement.
412
413 void
414 Output_data_plt_i386::do_write(Output_file* of)
415 {
416   const off_t offset = this->offset();
417   const off_t oview_size = this->data_size();
418   unsigned char* const oview = of->get_output_view(offset, oview_size);
419
420   const off_t got_file_offset = this->got_plt_->offset();
421   const off_t got_size = this->got_plt_->data_size();
422   unsigned char* const got_view = of->get_output_view(got_file_offset,
423                                                       got_size);
424
425   unsigned char* pov = oview;
426
427   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
428   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
429
430   if (this->is_shared_)
431     memcpy(pov, dyn_first_plt_entry, plt_entry_size);
432   else
433     {
434       memcpy(pov, exec_first_plt_entry, plt_entry_size);
435       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
436       elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
437     }
438   pov += plt_entry_size;
439
440   unsigned char* got_pov = got_view;
441
442   memset(got_pov, 0, 12);
443   got_pov += 12;
444
445   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
446
447   unsigned int plt_offset = plt_entry_size;
448   unsigned int plt_rel_offset = 0;
449   unsigned int got_offset = 12;
450   const unsigned int count = this->count_;
451   for (unsigned int i = 0;
452        i < count;
453        ++i,
454          pov += plt_entry_size,
455          got_pov += 4,
456          plt_offset += plt_entry_size,
457          plt_rel_offset += rel_size,
458          got_offset += 4)
459     {
460       // Set and adjust the PLT entry itself.
461
462       if (this->is_shared_)
463         {
464           memcpy(pov, dyn_plt_entry, plt_entry_size);
465           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
466         }
467       else
468         {
469           memcpy(pov, exec_plt_entry, plt_entry_size);
470           elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
471                                                       (got_address
472                                                        + got_offset));
473         }
474
475       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
476       elfcpp::Swap<32, false>::writeval(pov + 12,
477                                         - (plt_offset + plt_entry_size));
478
479       // Set the entry in the GOT.
480       elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
481     }
482
483   gold_assert(pov - oview == oview_size);
484   gold_assert(got_pov - got_view == got_size);
485
486   of->write_output_view(offset, oview_size, oview);
487   of->write_output_view(got_file_offset, got_size, got_view);
488 }
489
490 // Create a PLT entry for a global symbol.
491
492 void
493 Target_i386::make_plt_entry(const General_options* options,
494                             Symbol_table* symtab, Layout* layout, Symbol* gsym)
495 {
496   if (gsym->has_plt_offset())
497     return;
498
499   if (this->plt_ == NULL)
500     {
501       // Create the GOT sections first.
502       this->got_section(options, symtab, layout);
503
504       this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
505                                             options->is_shared());
506     }
507
508   this->plt_->add_entry(gsym);
509 }
510
511 // Handle a relocation against a non-function symbol defined in a
512 // dynamic object.  The traditional way to handle this is to generate
513 // a COPY relocation to copy the variable at runtime from the shared
514 // object into the executable's data segment.  However, this is
515 // undesirable in general, as if the size of the object changes in the
516 // dynamic object, the executable will no longer work correctly.  If
517 // this relocation is in a writable section, then we can create a
518 // dynamic reloc and the dynamic linker will resolve it to the correct
519 // address at runtime.  However, we do not want do that if the
520 // relocation is in a read-only section, as it would prevent the
521 // readonly segment from being shared.  And if we have to eventually
522 // generate a COPY reloc, then any dynamic relocations will be
523 // useless.  So this means that if this is a writable section, we need
524 // to save the relocation until we see whether we have to create a
525 // COPY relocation for this symbol for any other relocation.
526
527 void
528 Target_i386::copy_reloc(const General_options* options,
529                         Symbol_table* symtab,
530                         Layout* layout,
531                         Sized_relobj<32, false>* object,
532                         unsigned int data_shndx, Symbol* gsym,
533                         const elfcpp::Rel<32, false>& rel)
534 {
535   Sized_symbol<32>* ssym;
536   ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
537                                                         SELECT_SIZE(32));
538
539   if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
540                                                data_shndx, ssym))
541     {
542       // So far we do not need a COPY reloc.  Save this relocation.
543       // If it turns out that we never need a COPY reloc for this
544       // symbol, then we will emit the relocation.
545       if (this->copy_relocs_ == NULL)
546         this->copy_relocs_ = new Copy_relocs<32, false>();
547       this->copy_relocs_->save(ssym, object, data_shndx, rel);
548     }
549   else
550     {
551       // Allocate space for this symbol in the .bss section.
552
553       elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
554
555       // There is no defined way to determine the required alignment
556       // of the symbol.  We pick the alignment based on the size.  We
557       // set an arbitrary maximum of 256.
558       unsigned int align;
559       for (align = 1; align < 512; align <<= 1)
560         if ((symsize & align) != 0)
561           break;
562
563       if (this->dynbss_ == NULL)
564         {
565           this->dynbss_ = new Output_data_space(align);
566           layout->add_output_section_data(".bss",
567                                           elfcpp::SHT_NOBITS,
568                                           (elfcpp::SHF_ALLOC
569                                            | elfcpp::SHF_WRITE),
570                                           this->dynbss_);
571         }
572
573       Output_data_space* dynbss = this->dynbss_;
574
575       if (align > dynbss->addralign())
576         dynbss->set_space_alignment(align);
577
578       off_t dynbss_size = dynbss->data_size();
579       dynbss_size = align_address(dynbss_size, align);
580       off_t offset = dynbss_size;
581       dynbss->set_space_size(dynbss_size + symsize);
582
583       // Define the symbol in the .dynbss section.
584       symtab->define_in_output_data(this, ssym->name(), dynbss, offset,
585                                     symsize, ssym->type(), ssym->binding(),
586                                     ssym->visibility(), ssym->nonvis(),
587                                     false, false);
588
589       // Add the COPY reloc.
590       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
591       rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
592     }
593 }
594
595 // Optimize the TLS relocation type based on what we know about the
596 // symbol.  IS_FINAL is true if the final address of this symbol is
597 // known at link time.
598
599 unsigned int
600 Target_i386::optimize_tls_reloc(const General_options* options,
601                                 bool is_final,
602                                 int r_type)
603 {
604   // If we are generating a shared library, then we can't do anything
605   // in the linker.
606   if (options->is_shared())
607     return r_type;
608
609   switch (r_type)
610     {
611     case elfcpp::R_386_TLS_GD:
612     case elfcpp::R_386_TLS_GOTDESC:
613     case elfcpp::R_386_TLS_DESC_CALL:
614       // These are Global-Dynamic which permits fully general TLS
615       // access.  Since we know that we are generating an executable,
616       // we can convert this to Initial-Exec.  If we also know that
617       // this is a local symbol, we can further switch to Local-Exec.
618       if (is_final)
619         return elfcpp::R_386_TLS_LE_32;
620       return elfcpp::R_386_TLS_IE_32;
621
622     case elfcpp::R_386_TLS_LDM:
623       // This is Local-Dynamic, which refers to a local symbol in the
624       // dynamic TLS block.  Since we know that we generating an
625       // executable, we can switch to Local-Exec.
626       return elfcpp::R_386_TLS_LE_32;
627
628     case elfcpp::R_386_TLS_LDO_32:
629       // Another type of Local-Dynamic relocation.
630       return elfcpp::R_386_TLS_LE;
631
632     case elfcpp::R_386_TLS_IE:
633     case elfcpp::R_386_TLS_GOTIE:
634     case elfcpp::R_386_TLS_IE_32:
635       // These are Initial-Exec relocs which get the thread offset
636       // from the GOT.  If we know that we are linking against the
637       // local symbol, we can switch to Local-Exec, which links the
638       // thread offset into the instruction.
639       if (is_final)
640         return elfcpp::R_386_TLS_LE_32;
641       return r_type;
642         
643     case elfcpp::R_386_TLS_LE:
644     case elfcpp::R_386_TLS_LE_32:
645       // When we already have Local-Exec, there is nothing further we
646       // can do.
647       return r_type;
648
649     default:
650       gold_unreachable();
651     }
652 }
653
654 // Scan a relocation for a local symbol.
655
656 inline void
657 Target_i386::Scan::local(const General_options& options,
658                          Symbol_table* symtab,
659                          Layout* layout,
660                          Target_i386* target,
661                          Sized_relobj<32, false>* object,
662                          unsigned int,
663                          const elfcpp::Rel<32, false>&,
664                          unsigned int r_type,
665                          const elfcpp::Sym<32, false>&)
666 {
667   switch (r_type)
668     {
669     case elfcpp::R_386_NONE:
670     case elfcpp::R_386_GNU_VTINHERIT:
671     case elfcpp::R_386_GNU_VTENTRY:
672       break;
673
674     case elfcpp::R_386_32:
675     case elfcpp::R_386_16:
676     case elfcpp::R_386_8:
677       // FIXME: If we are generating a shared object we need to copy
678       // this relocation into the object.
679       gold_assert(!options.is_shared());
680       break;
681
682     case elfcpp::R_386_PC32:
683     case elfcpp::R_386_PC16:
684     case elfcpp::R_386_PC8:
685       break;
686
687     case elfcpp::R_386_GOTOFF:
688     case elfcpp::R_386_GOTPC:
689       // We need a GOT section.
690       target->got_section(&options, symtab, layout);
691       break;
692
693     case elfcpp::R_386_COPY:
694     case elfcpp::R_386_GLOB_DAT:
695     case elfcpp::R_386_JUMP_SLOT:
696     case elfcpp::R_386_RELATIVE:
697     case elfcpp::R_386_TLS_TPOFF:
698     case elfcpp::R_386_TLS_DTPMOD32:
699     case elfcpp::R_386_TLS_DTPOFF32:
700     case elfcpp::R_386_TLS_TPOFF32:
701     case elfcpp::R_386_TLS_DESC:
702       fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
703               program_name, object->name().c_str(), r_type);
704       gold_exit(false);
705       break;
706
707     case elfcpp::R_386_TLS_IE:
708     case elfcpp::R_386_TLS_GOTIE:
709     case elfcpp::R_386_TLS_LE:
710     case elfcpp::R_386_TLS_GD:
711     case elfcpp::R_386_TLS_LDM:
712     case elfcpp::R_386_TLS_LDO_32:
713     case elfcpp::R_386_TLS_IE_32:
714     case elfcpp::R_386_TLS_LE_32:
715     case elfcpp::R_386_TLS_GOTDESC:
716     case elfcpp::R_386_TLS_DESC_CALL:
717       r_type = Target_i386::optimize_tls_reloc(&options,
718                                                !options.is_shared(),
719                                                r_type);
720       switch (r_type)
721         {
722         case elfcpp::R_386_TLS_LE:
723         case elfcpp::R_386_TLS_LE_32:
724           // FIXME: If generating a shared object, we need to copy
725           // this relocation into the object.
726           gold_assert(!options.is_shared());
727           break;
728
729         case elfcpp::R_386_TLS_IE:
730         case elfcpp::R_386_TLS_GOTIE:
731         case elfcpp::R_386_TLS_GD:
732         case elfcpp::R_386_TLS_LDM:
733         case elfcpp::R_386_TLS_LDO_32:
734         case elfcpp::R_386_TLS_IE_32:
735         case elfcpp::R_386_TLS_GOTDESC:
736         case elfcpp::R_386_TLS_DESC_CALL:
737           fprintf(stderr,
738                   _("%s: %s: unsupported reloc %u against local symbol\n"),
739                   program_name, object->name().c_str(), r_type);
740           break;
741         }
742       break;
743
744     case elfcpp::R_386_GOT32:
745     case elfcpp::R_386_PLT32:
746     case elfcpp::R_386_32PLT:
747     case elfcpp::R_386_TLS_GD_32:
748     case elfcpp::R_386_TLS_GD_PUSH:
749     case elfcpp::R_386_TLS_GD_CALL:
750     case elfcpp::R_386_TLS_GD_POP:
751     case elfcpp::R_386_TLS_LDM_32:
752     case elfcpp::R_386_TLS_LDM_PUSH:
753     case elfcpp::R_386_TLS_LDM_CALL:
754     case elfcpp::R_386_TLS_LDM_POP:
755     case elfcpp::R_386_USED_BY_INTEL_200:
756     default:
757       fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
758               program_name, object->name().c_str(), r_type);
759       break;
760     }
761 }
762
763 // Scan a relocation for a global symbol.
764
765 inline void
766 Target_i386::Scan::global(const General_options& options,
767                           Symbol_table* symtab,
768                           Layout* layout,
769                           Target_i386* target,
770                           Sized_relobj<32, false>* object,
771                           unsigned int data_shndx,
772                           const elfcpp::Rel<32, false>& reloc,
773                           unsigned int r_type,
774                           Symbol* gsym)
775 {
776   switch (r_type)
777     {
778     case elfcpp::R_386_NONE:
779     case elfcpp::R_386_GNU_VTINHERIT:
780     case elfcpp::R_386_GNU_VTENTRY: 
781       break;
782
783     case elfcpp::R_386_32:
784     case elfcpp::R_386_PC32:
785     case elfcpp::R_386_16:
786     case elfcpp::R_386_PC16:
787     case elfcpp::R_386_8:
788     case elfcpp::R_386_PC8:
789       // FIXME: If we are generating a shared object we may need to
790       // copy this relocation into the object.  If this symbol is
791       // defined in a shared object, we may need to copy this
792       // relocation in order to avoid a COPY relocation.
793       gold_assert(!options.is_shared());
794
795       if (gsym->is_defined_in_dynobj())
796         {
797           // This symbol is defined in a dynamic object.  If it is a
798           // function, we make a PLT entry.  Otherwise we need to
799           // either generate a COPY reloc or copy this reloc.
800           if (gsym->type() == elfcpp::STT_FUNC)
801             target->make_plt_entry(&options, symtab, layout, gsym);
802           else
803             target->copy_reloc(&options, symtab, layout, object, data_shndx,
804                                gsym, reloc);
805         }
806
807       break;
808
809     case elfcpp::R_386_GOT32:
810       // The symbol requires a GOT entry.
811       if (target->got_section(&options, symtab, layout)->add_global(gsym))
812         {
813           // If this symbol is not fully resolved, we need to add a
814           // dynamic relocation for it.
815           if (!gsym->final_value_is_known(&options))
816             gold_unreachable();
817         }
818       break;
819
820     case elfcpp::R_386_PLT32:
821       // If the symbol is fully resolved, this is just a PC32 reloc.
822       // Otherwise we need a PLT entry.
823       if (gsym->final_value_is_known(&options))
824         break;
825       target->make_plt_entry(&options, symtab, layout, gsym);
826       break;
827
828     case elfcpp::R_386_GOTOFF:
829     case elfcpp::R_386_GOTPC:
830       // We need a GOT section.
831       target->got_section(&options, symtab, layout);
832       break;
833
834     case elfcpp::R_386_COPY:
835     case elfcpp::R_386_GLOB_DAT:
836     case elfcpp::R_386_JUMP_SLOT:
837     case elfcpp::R_386_RELATIVE:
838     case elfcpp::R_386_TLS_TPOFF:
839     case elfcpp::R_386_TLS_DTPMOD32:
840     case elfcpp::R_386_TLS_DTPOFF32:
841     case elfcpp::R_386_TLS_TPOFF32:
842     case elfcpp::R_386_TLS_DESC:
843       fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
844               program_name, object->name().c_str(), r_type);
845       gold_exit(false);
846       break;
847
848     case elfcpp::R_386_TLS_IE:
849     case elfcpp::R_386_TLS_GOTIE:
850     case elfcpp::R_386_TLS_LE:
851     case elfcpp::R_386_TLS_GD:
852     case elfcpp::R_386_TLS_LDM:
853     case elfcpp::R_386_TLS_LDO_32:
854     case elfcpp::R_386_TLS_IE_32:
855     case elfcpp::R_386_TLS_LE_32:
856     case elfcpp::R_386_TLS_GOTDESC:
857     case elfcpp::R_386_TLS_DESC_CALL:
858       {
859         const bool is_final = gsym->final_value_is_known(&options);
860         r_type = Target_i386::optimize_tls_reloc(&options, is_final, r_type);
861         switch (r_type)
862           {
863           case elfcpp::R_386_TLS_LE:
864           case elfcpp::R_386_TLS_LE_32:
865             // FIXME: If generating a shared object, we need to copy
866             // this relocation into the object.
867             gold_assert(!options.is_shared());
868             break;
869
870           case elfcpp::R_386_TLS_IE:
871           case elfcpp::R_386_TLS_GOTIE:
872           case elfcpp::R_386_TLS_GD:
873           case elfcpp::R_386_TLS_LDM:
874           case elfcpp::R_386_TLS_LDO_32:
875           case elfcpp::R_386_TLS_IE_32:
876           case elfcpp::R_386_TLS_GOTDESC:
877           case elfcpp::R_386_TLS_DESC_CALL:
878             fprintf(stderr,
879                     _("%s: %s: unsupported reloc %u "
880                       "against global symbol %s\n"),
881                     program_name, object->name().c_str(), r_type,
882                     gsym->name());
883             break;
884           }
885       }
886       break;
887
888     case elfcpp::R_386_32PLT:
889     case elfcpp::R_386_TLS_GD_32:
890     case elfcpp::R_386_TLS_GD_PUSH:
891     case elfcpp::R_386_TLS_GD_CALL:
892     case elfcpp::R_386_TLS_GD_POP:
893     case elfcpp::R_386_TLS_LDM_32:
894     case elfcpp::R_386_TLS_LDM_PUSH:
895     case elfcpp::R_386_TLS_LDM_CALL:
896     case elfcpp::R_386_TLS_LDM_POP:
897     case elfcpp::R_386_USED_BY_INTEL_200:
898     default:
899       fprintf(stderr,
900               _("%s: %s: unsupported reloc %u against global symbol %s\n"),
901               program_name, object->name().c_str(), r_type, gsym->name());
902       break;
903     }
904 }
905
906 // Scan relocations for a section.
907
908 void
909 Target_i386::scan_relocs(const General_options& options,
910                          Symbol_table* symtab,
911                          Layout* layout,
912                          Sized_relobj<32, false>* object,
913                          unsigned int data_shndx,
914                          unsigned int sh_type,
915                          const unsigned char* prelocs,
916                          size_t reloc_count,
917                          size_t local_symbol_count,
918                          const unsigned char* plocal_symbols,
919                          Symbol** global_symbols)
920 {
921   if (sh_type == elfcpp::SHT_RELA)
922     {
923       fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
924               program_name, object->name().c_str());
925       gold_exit(false);
926     }
927
928   gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
929                     Target_i386::Scan>(
930     options,
931     symtab,
932     layout,
933     this,
934     object,
935     data_shndx,
936     prelocs,
937     reloc_count,
938     local_symbol_count,
939     plocal_symbols,
940     global_symbols);
941 }
942
943 // Finalize the sections.  This is where we emit any relocs we saved
944 // in an attempt to avoid generating extra COPY relocs.
945
946 void
947 Target_i386::do_finalize_sections(Layout* layout)
948 {
949   if (this->copy_relocs_ == NULL)
950     return;
951   if (this->copy_relocs_->any_to_emit())
952     {
953       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
954       this->copy_relocs_->emit(rel_dyn);
955     }
956   delete this->copy_relocs_;
957   this->copy_relocs_ = NULL;
958 }
959
960 // Perform a relocation.
961
962 inline bool
963 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
964                                 Target_i386* target,
965                                 size_t relnum,
966                                 const elfcpp::Rel<32, false>& rel,
967                                 unsigned int r_type,
968                                 const Sized_symbol<32>* gsym,
969                                 elfcpp::Elf_types<32>::Elf_Addr value,
970                                 unsigned char* view,
971                                 elfcpp::Elf_types<32>::Elf_Addr address,
972                                 off_t view_size)
973 {
974   if (this->skip_call_tls_get_addr_)
975     {
976       if (r_type != elfcpp::R_386_PLT32
977           || gsym == NULL
978           || strcmp(gsym->name(), "___tls_get_addr") != 0)
979         {
980           fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
981                   program_name,
982                   relinfo->location(relnum, rel.get_r_offset()).c_str());
983           gold_exit(false);
984         }
985
986       this->skip_call_tls_get_addr_ = false;
987
988       return false;
989     }
990
991   // Pick the value to use for symbols defined in shared objects.
992   if (gsym != NULL && gsym->is_defined_in_dynobj())
993     {
994       if (gsym->has_plt_offset())
995         address = target->plt_section()->address() + gsym->plt_offset();
996       else
997         gold_unreachable();
998     }
999
1000   switch (r_type)
1001     {
1002     case elfcpp::R_386_NONE:
1003     case elfcpp::R_386_GNU_VTINHERIT:
1004     case elfcpp::R_386_GNU_VTENTRY:
1005       break;
1006
1007     case elfcpp::R_386_32:
1008       Relocate_functions<32, false>::rel32(view, value);
1009       break;
1010
1011     case elfcpp::R_386_PC32:
1012       Relocate_functions<32, false>::pcrel32(view, value, address);
1013       break;
1014
1015     case elfcpp::R_386_16:
1016       Relocate_functions<32, false>::rel16(view, value);
1017       break;
1018
1019     case elfcpp::R_386_PC16:
1020       Relocate_functions<32, false>::pcrel16(view, value, address);
1021       break;
1022
1023     case elfcpp::R_386_8:
1024       Relocate_functions<32, false>::rel8(view, value);
1025       break;
1026
1027     case elfcpp::R_386_PC8:
1028       Relocate_functions<32, false>::pcrel8(view, value, address);
1029       break;
1030
1031     case elfcpp::R_386_PLT32:
1032       gold_assert(gsym->has_plt_offset()
1033                   || gsym->final_value_is_known(relinfo->options));
1034       Relocate_functions<32, false>::pcrel32(view, value, address);
1035       break;
1036
1037     case elfcpp::R_386_GOT32:
1038       // Local GOT offsets not yet supported.
1039       gold_assert(gsym);
1040       gold_assert(gsym->has_got_offset());
1041       value = gsym->got_offset();
1042       Relocate_functions<32, false>::rel32(view, value);
1043       break;
1044
1045     case elfcpp::R_386_GOTOFF:
1046       value -= target->got_section(NULL, NULL, NULL)->address();
1047       Relocate_functions<32, false>::rel32(view, value);
1048       break;
1049
1050     case elfcpp::R_386_GOTPC:
1051       value = target->got_section(NULL, NULL, NULL)->address();
1052       Relocate_functions<32, false>::pcrel32(view, value, address);
1053       break;
1054
1055     case elfcpp::R_386_COPY:
1056     case elfcpp::R_386_GLOB_DAT:
1057     case elfcpp::R_386_JUMP_SLOT:
1058     case elfcpp::R_386_RELATIVE:
1059     case elfcpp::R_386_TLS_TPOFF:
1060     case elfcpp::R_386_TLS_DTPMOD32:
1061     case elfcpp::R_386_TLS_DTPOFF32:
1062     case elfcpp::R_386_TLS_TPOFF32:
1063     case elfcpp::R_386_TLS_DESC:
1064       fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1065               program_name,
1066               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1067               r_type);
1068       gold_exit(false);
1069       break;
1070
1071     case elfcpp::R_386_TLS_IE:
1072     case elfcpp::R_386_TLS_GOTIE:
1073     case elfcpp::R_386_TLS_LE:
1074     case elfcpp::R_386_TLS_GD:
1075     case elfcpp::R_386_TLS_LDM:
1076     case elfcpp::R_386_TLS_LDO_32:
1077     case elfcpp::R_386_TLS_IE_32:
1078     case elfcpp::R_386_TLS_LE_32:
1079     case elfcpp::R_386_TLS_GOTDESC:
1080     case elfcpp::R_386_TLS_DESC_CALL:
1081       this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view,
1082                          address, view_size);
1083       break;
1084
1085     case elfcpp::R_386_32PLT:
1086     case elfcpp::R_386_TLS_GD_32:
1087     case elfcpp::R_386_TLS_GD_PUSH:
1088     case elfcpp::R_386_TLS_GD_CALL:
1089     case elfcpp::R_386_TLS_GD_POP:
1090     case elfcpp::R_386_TLS_LDM_32:
1091     case elfcpp::R_386_TLS_LDM_PUSH:
1092     case elfcpp::R_386_TLS_LDM_CALL:
1093     case elfcpp::R_386_TLS_LDM_POP:
1094     case elfcpp::R_386_USED_BY_INTEL_200:
1095     default:
1096       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1097               program_name,
1098               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1099               r_type);
1100       // gold_exit(false);
1101       break;
1102     }
1103
1104   return true;
1105 }
1106
1107 // Perform a TLS relocation.
1108
1109 inline void
1110 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1111                                     size_t relnum,
1112                                     const elfcpp::Rel<32, false>& rel,
1113                                     unsigned int r_type,
1114                                     const Sized_symbol<32>* gsym,
1115                                     elfcpp::Elf_types<32>::Elf_Addr value,
1116                                     unsigned char* view,
1117                                     elfcpp::Elf_types<32>::Elf_Addr,
1118                                     off_t view_size)
1119 {
1120   Output_segment* tls_segment = relinfo->layout->tls_segment();
1121   if (tls_segment == NULL)
1122     {
1123       fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1124               program_name,
1125               relinfo->location(relnum, rel.get_r_offset()).c_str());
1126       gold_exit(false);
1127     }
1128
1129   const bool is_final = (gsym == NULL
1130                          ? !relinfo->options->is_shared()
1131                          : gsym->final_value_is_known(relinfo->options));
1132   const unsigned int opt_r_type =
1133     Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
1134   switch (r_type)
1135     {
1136     case elfcpp::R_386_TLS_LE_32:
1137       value = tls_segment->vaddr() + tls_segment->memsz() - value;
1138       Relocate_functions<32, false>::rel32(view, value);
1139       break;
1140
1141     case elfcpp::R_386_TLS_LE:
1142       value = value - (tls_segment->vaddr() + tls_segment->memsz());
1143       Relocate_functions<32, false>::rel32(view, value);
1144       break;
1145
1146     case elfcpp::R_386_TLS_IE:
1147     case elfcpp::R_386_TLS_GOTIE:
1148     case elfcpp::R_386_TLS_IE_32:
1149       if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1150         {
1151           Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1152                                               rel, r_type, value, view,
1153                                               view_size);
1154           break;
1155         }
1156       fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1157               program_name,
1158               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1159               r_type);
1160       // gold_exit(false);
1161       break;
1162
1163     case elfcpp::R_386_TLS_GD:
1164       if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1165         {
1166           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1167                              rel, r_type, value, view,
1168                              view_size);
1169           break;
1170         }
1171       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1172               program_name,
1173               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1174               r_type);
1175       // gold_exit(false);
1176       break;
1177
1178     case elfcpp::R_386_TLS_LDM:
1179     case elfcpp::R_386_TLS_LDO_32:
1180     case elfcpp::R_386_TLS_GOTDESC:
1181     case elfcpp::R_386_TLS_DESC_CALL:
1182       fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1183               program_name,
1184               relinfo->location(relnum, rel.get_r_offset()).c_str(),
1185               r_type);
1186       // gold_exit(false);
1187       break;
1188     }
1189 }
1190
1191 // Do a relocation in which we convert a TLS Initial-Exec to a
1192 // Local-Exec.
1193
1194 inline void
1195 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1196                                     size_t relnum,
1197                                     Output_segment* tls_segment,
1198                                     const elfcpp::Rel<32, false>& rel,
1199                                     unsigned int r_type,
1200                                     elfcpp::Elf_types<32>::Elf_Addr value,
1201                                     unsigned char* view,
1202                                     off_t view_size)
1203 {
1204   // We have to actually change the instructions, which means that we
1205   // need to examine the opcodes to figure out which instruction we
1206   // are looking at.
1207   if (r_type == elfcpp::R_386_TLS_IE)
1208     {
1209       // movl %gs:XX,%eax  ==>  movl $YY,%eax
1210       // movl %gs:XX,%reg  ==>  movl $YY,%reg
1211       // addl %gs:XX,%reg  ==>  addl $YY,%reg
1212       Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1213       Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1214
1215       unsigned char op1 = view[-1];
1216       if (op1 == 0xa1)
1217         {
1218           // movl XX,%eax  ==>  movl $YY,%eax
1219           view[-1] = 0xb8;
1220         }
1221       else
1222         {
1223           Target_i386::Relocate::check_range(relinfo, relnum, rel,
1224                                              view_size, -2);
1225
1226           unsigned char op2 = view[-2];
1227           if (op2 == 0x8b)
1228             {
1229               // movl XX,%reg  ==>  movl $YY,%reg
1230               Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1231                                                (op1 & 0xc7) == 0x05);
1232               view[-2] = 0xc7;
1233               view[-1] = 0xc0 | ((op1 >> 3) & 7);
1234             }
1235           else if (op2 == 0x03)
1236             {
1237               // addl XX,%reg  ==>  addl $YY,%reg
1238               Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1239                                                (op1 & 0xc7) == 0x05);
1240               view[-2] = 0x81;
1241               view[-1] = 0xc0 | ((op1 >> 3) & 7);
1242             }
1243           else
1244             Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1245         }
1246     }
1247   else
1248     {
1249       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
1250       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
1251       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
1252       Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1253       Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1254
1255       unsigned char op1 = view[-1];
1256       unsigned char op2 = view[-2];
1257       Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1258                                        (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1259       if (op2 == 0x8b)
1260         {
1261           // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
1262           view[-2] = 0xc7;
1263           view[-1] = 0xc0 | ((op1 >> 3) & 7);
1264         }
1265       else if (op2 == 0x2b)
1266         {
1267           // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
1268           view[-2] = 0x81;
1269           view[-1] = 0xe8 | ((op1 >> 3) & 7);
1270         }
1271       else if (op2 == 0x03)
1272         {
1273           // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
1274           view[-2] = 0x81;
1275           view[-1] = 0xc0 | ((op1 >> 3) & 7);
1276         }
1277       else
1278         Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1279     }
1280
1281   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1282   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1283     value = - value;
1284
1285   Relocate_functions<32, false>::rel32(view, value);
1286 }
1287
1288 // Do a relocation in which we convert a TLS Global-Dynamic to a
1289 // Local-Exec.
1290
1291 inline void
1292 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1293                                     size_t relnum,
1294                                     Output_segment* tls_segment,
1295                                     const elfcpp::Rel<32, false>& rel,
1296                                     unsigned int,
1297                                     elfcpp::Elf_types<32>::Elf_Addr value,
1298                                     unsigned char* view,
1299                                     off_t view_size)
1300 {
1301   // leal foo(,%reg,1),%eax; call ___tls_get_addr
1302   //  ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1303   // leal foo(%reg),%eax; call ___tls_get_addr
1304   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1305
1306   Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1307   Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1308
1309   unsigned char op1 = view[-1];
1310   unsigned char op2 = view[-2];
1311
1312   Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1313                                    op2 == 0x8d || op2 == 0x04);
1314   Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1315                                    view[4] == 0xe8);
1316
1317   int roff = 5;
1318
1319   if (op2 == 0x04)
1320     {
1321       Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1322       Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1323                                        view[-3] == 0x8d);
1324       Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1325                                        ((op1 & 0xc7) == 0x05
1326                                         && op1 != (4 << 3)));
1327       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1328     }
1329   else
1330     {
1331       Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1332                                        (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1333       if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1334         {
1335           // There is a trailing nop.  Use the size byte subl.
1336           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1337           roff = 6;
1338         }
1339       else
1340         {
1341           // Use the five byte subl.
1342           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1343         }
1344     }
1345
1346   value = tls_segment->vaddr() + tls_segment->memsz() - value;
1347   Relocate_functions<32, false>::rel32(view + roff, value);
1348
1349   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1350   // We can skip it.
1351   this->skip_call_tls_get_addr_ = true;
1352 }
1353
1354 // Check the range for a TLS relocation.
1355
1356 inline void
1357 Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1358                                    size_t relnum,
1359                                    const elfcpp::Rel<32, false>& rel,
1360                                    off_t view_size, off_t off)
1361 {
1362   off_t offset = rel.get_r_offset() + off;
1363   if (offset < 0 || offset > view_size)
1364     {
1365       fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1366               program_name,
1367               relinfo->location(relnum, rel.get_r_offset()).c_str());
1368       gold_exit(false);
1369     }
1370 }
1371
1372 // Check the validity of a TLS relocation.  This is like assert.
1373
1374 inline void
1375 Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1376                                  size_t relnum,
1377                                  const elfcpp::Rel<32, false>& rel,
1378                                  bool valid)
1379 {
1380   if (!valid)
1381     {
1382       fprintf(stderr,
1383               _("%s: %s: TLS relocation against invalid instruction\n"),
1384               program_name,
1385               relinfo->location(relnum, rel.get_r_offset()).c_str());
1386       gold_exit(false);
1387     }
1388 }
1389
1390 // Relocate section data.
1391
1392 void
1393 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1394                               unsigned int sh_type,
1395                               const unsigned char* prelocs,
1396                               size_t reloc_count,
1397                               unsigned char* view,
1398                               elfcpp::Elf_types<32>::Elf_Addr address,
1399                               off_t view_size)
1400 {
1401   gold_assert(sh_type == elfcpp::SHT_REL);
1402
1403   gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1404                          Target_i386::Relocate>(
1405     relinfo,
1406     this,
1407     prelocs,
1408     reloc_count,
1409     view,
1410     address,
1411     view_size);
1412 }
1413
1414 // The selector for i386 object files.
1415
1416 class Target_selector_i386 : public Target_selector
1417 {
1418 public:
1419   Target_selector_i386()
1420     : Target_selector(elfcpp::EM_386, 32, false)
1421   { }
1422
1423   Target*
1424   recognize(int machine, int osabi, int abiversion);
1425
1426  private:
1427   Target_i386* target_;
1428 };
1429
1430 // Recognize an i386 object file when we already know that the machine
1431 // number is EM_386.
1432
1433 Target*
1434 Target_selector_i386::recognize(int, int, int)
1435 {
1436   if (this->target_ == NULL)
1437     this->target_ = new Target_i386();
1438   return this->target_;
1439 }
1440
1441 Target_selector_i386 target_selector_i386;
1442
1443 } // End anonymous namespace.