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