Refactor gold to enable support for MIPS-64 relocation format.
[external/binutils.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "dwarf.h"
29 #include "parameters.h"
30 #include "reloc.h"
31 #include "i386.h"
32 #include "object.h"
33 #include "symtab.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "copy-relocs.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "freebsd.h"
42 #include "nacl.h"
43 #include "gc.h"
44
45 namespace
46 {
47
48 using namespace gold;
49
50 // A class to handle the .got.plt section.
51
52 class Output_data_got_plt_i386 : public Output_section_data_build
53 {
54  public:
55   Output_data_got_plt_i386(Layout* layout)
56     : Output_section_data_build(4),
57       layout_(layout)
58   { }
59
60  protected:
61   // Write out the PLT data.
62   void
63   do_write(Output_file*);
64
65   // Write to a map file.
66   void
67   do_print_to_mapfile(Mapfile* mapfile) const
68   { mapfile->print_output_data(this, "** GOT PLT"); }
69
70  private:
71   // A pointer to the Layout class, so that we can find the .dynamic
72   // section when we write out the GOT PLT section.
73   Layout* layout_;
74 };
75
76 // A class to handle the PLT data.
77 // This is an abstract base class that handles most of the linker details
78 // but does not know the actual contents of PLT entries.  The derived
79 // classes below fill in those details.
80
81 class Output_data_plt_i386 : public Output_section_data
82 {
83  public:
84   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
85
86   Output_data_plt_i386(Layout*, uint64_t addralign,
87                        Output_data_got_plt_i386*, Output_data_space*);
88
89   // Add an entry to the PLT.
90   void
91   add_entry(Symbol_table*, Layout*, Symbol* gsym);
92
93   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
94   unsigned int
95   add_local_ifunc_entry(Symbol_table*, Layout*,
96                         Sized_relobj_file<32, false>* relobj,
97                         unsigned int local_sym_index);
98
99   // Return the .rel.plt section data.
100   Reloc_section*
101   rel_plt() const
102   { return this->rel_; }
103
104   // Return where the TLS_DESC relocations should go.
105   Reloc_section*
106   rel_tls_desc(Layout*);
107
108   // Return where the IRELATIVE relocations should go.
109   Reloc_section*
110   rel_irelative(Symbol_table*, Layout*);
111
112   // Return whether we created a section for IRELATIVE relocations.
113   bool
114   has_irelative_section() const
115   { return this->irelative_rel_ != NULL; }
116
117   // Return the number of PLT entries.
118   unsigned int
119   entry_count() const
120   { return this->count_ + this->irelative_count_; }
121
122   // Return the offset of the first non-reserved PLT entry.
123   unsigned int
124   first_plt_entry_offset()
125   { return this->get_plt_entry_size(); }
126
127   // Return the size of a PLT entry.
128   unsigned int
129   get_plt_entry_size() const
130   { return this->do_get_plt_entry_size(); }
131
132   // Return the PLT address to use for a global symbol.
133   uint64_t
134   address_for_global(const Symbol*);
135
136   // Return the PLT address to use for a local symbol.
137   uint64_t
138   address_for_local(const Relobj*, unsigned int symndx);
139
140   // Add .eh_frame information for the PLT.
141   void
142   add_eh_frame(Layout* layout)
143   { this->do_add_eh_frame(layout); }
144
145  protected:
146   // Fill the first PLT entry, given the pointer to the PLT section data
147   // and the runtime address of the GOT.
148   void
149   fill_first_plt_entry(unsigned char* pov,
150                        elfcpp::Elf_types<32>::Elf_Addr got_address)
151   { this->do_fill_first_plt_entry(pov, got_address); }
152
153   // Fill a normal PLT entry, given the pointer to the entry's data in the
154   // section, the runtime address of the GOT, the offset into the GOT of
155   // the corresponding slot, the offset into the relocation section of the
156   // corresponding reloc, and the offset of this entry within the whole
157   // PLT.  Return the offset from this PLT entry's runtime address that
158   // should be used to compute the initial value of the GOT slot.
159   unsigned int
160   fill_plt_entry(unsigned char* pov,
161                  elfcpp::Elf_types<32>::Elf_Addr got_address,
162                  unsigned int got_offset,
163                  unsigned int plt_offset,
164                  unsigned int plt_rel_offset)
165   {
166     return this->do_fill_plt_entry(pov, got_address, got_offset,
167                                    plt_offset, plt_rel_offset);
168   }
169
170   virtual unsigned int
171   do_get_plt_entry_size() const = 0;
172
173   virtual void
174   do_fill_first_plt_entry(unsigned char* pov,
175                           elfcpp::Elf_types<32>::Elf_Addr got_address) = 0;
176
177   virtual unsigned int
178   do_fill_plt_entry(unsigned char* pov,
179                     elfcpp::Elf_types<32>::Elf_Addr got_address,
180                     unsigned int got_offset,
181                     unsigned int plt_offset,
182                     unsigned int plt_rel_offset) = 0;
183
184   virtual void
185   do_add_eh_frame(Layout*) = 0;
186
187   void
188   do_adjust_output_section(Output_section* os);
189
190   // Write to a map file.
191   void
192   do_print_to_mapfile(Mapfile* mapfile) const
193   { mapfile->print_output_data(this, _("** PLT")); }
194
195   // The .eh_frame unwind information for the PLT.
196   // The CIE is common across variants of the PLT format.
197   static const int plt_eh_frame_cie_size = 16;
198   static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
199
200  private:
201   // Set the final size.
202   void
203   set_final_data_size()
204   {
205     this->set_data_size((this->count_ + this->irelative_count_ + 1)
206                         * this->get_plt_entry_size());
207   }
208
209   // Write out the PLT data.
210   void
211   do_write(Output_file*);
212
213   // We keep a list of global STT_GNU_IFUNC symbols, each with its
214   // offset in the GOT.
215   struct Global_ifunc
216   {
217     Symbol* sym;
218     unsigned int got_offset;
219   };
220
221   // We keep a list of local STT_GNU_IFUNC symbols, each with its
222   // offset in the GOT.
223   struct Local_ifunc
224   {
225     Sized_relobj_file<32, false>* object;
226     unsigned int local_sym_index;
227     unsigned int got_offset;
228   };
229
230   // The reloc section.
231   Reloc_section* rel_;
232   // The TLS_DESC relocations, if necessary.  These must follow the
233   // regular PLT relocs.
234   Reloc_section* tls_desc_rel_;
235   // The IRELATIVE relocations, if necessary.  These must follow the
236   // regular relocatoins and the TLS_DESC relocations.
237   Reloc_section* irelative_rel_;
238   // The .got.plt section.
239   Output_data_got_plt_i386* got_plt_;
240   // The part of the .got.plt section used for IRELATIVE relocs.
241   Output_data_space* got_irelative_;
242   // The number of PLT entries.
243   unsigned int count_;
244   // Number of PLT entries with R_386_IRELATIVE relocs.  These follow
245   // the regular PLT entries.
246   unsigned int irelative_count_;
247   // Global STT_GNU_IFUNC symbols.
248   std::vector<Global_ifunc> global_ifuncs_;
249   // Local STT_GNU_IFUNC symbols.
250   std::vector<Local_ifunc> local_ifuncs_;
251 };
252
253 // This is an abstract class for the standard PLT layout.
254 // The derived classes below handle the actual PLT contents
255 // for the executable (non-PIC) and shared-library (PIC) cases.
256 // The unwind information is uniform across those two, so it's here.
257
258 class Output_data_plt_i386_standard : public Output_data_plt_i386
259 {
260  public:
261   Output_data_plt_i386_standard(Layout* layout,
262                                 Output_data_got_plt_i386* got_plt,
263                                 Output_data_space* got_irelative)
264     : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
265   { }
266
267  protected:
268   virtual unsigned int
269   do_get_plt_entry_size() const
270   { return plt_entry_size; }
271
272   virtual void
273   do_add_eh_frame(Layout* layout)
274   {
275     layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
276                                  plt_eh_frame_fde, plt_eh_frame_fde_size);
277   }
278
279   // The size of an entry in the PLT.
280   static const int plt_entry_size = 16;
281
282   // The .eh_frame unwind information for the PLT.
283   static const int plt_eh_frame_fde_size = 32;
284   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
285 };
286
287 // Actually fill the PLT contents for an executable (non-PIC).
288
289 class Output_data_plt_i386_exec : public Output_data_plt_i386_standard
290 {
291 public:
292   Output_data_plt_i386_exec(Layout* layout,
293                             Output_data_got_plt_i386* got_plt,
294                             Output_data_space* got_irelative)
295     : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
296   { }
297
298  protected:
299   virtual void
300   do_fill_first_plt_entry(unsigned char* pov,
301                           elfcpp::Elf_types<32>::Elf_Addr got_address);
302
303   virtual unsigned int
304   do_fill_plt_entry(unsigned char* pov,
305                     elfcpp::Elf_types<32>::Elf_Addr got_address,
306                     unsigned int got_offset,
307                     unsigned int plt_offset,
308                     unsigned int plt_rel_offset);
309
310  private:
311   // The first entry in the PLT for an executable.
312   static const unsigned char first_plt_entry[plt_entry_size];
313
314   // Other entries in the PLT for an executable.
315   static const unsigned char plt_entry[plt_entry_size];
316 };
317
318 // Actually fill the PLT contents for a shared library (PIC).
319
320 class Output_data_plt_i386_dyn : public Output_data_plt_i386_standard
321 {
322  public:
323   Output_data_plt_i386_dyn(Layout* layout,
324                            Output_data_got_plt_i386* got_plt,
325                            Output_data_space* got_irelative)
326     : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
327   { }
328
329  protected:
330   virtual void
331   do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
332
333   virtual unsigned int
334   do_fill_plt_entry(unsigned char* pov,
335                     elfcpp::Elf_types<32>::Elf_Addr,
336                     unsigned int got_offset,
337                     unsigned int plt_offset,
338                     unsigned int plt_rel_offset);
339
340  private:
341   // The first entry in the PLT for a shared object.
342   static const unsigned char first_plt_entry[plt_entry_size];
343
344   // Other entries in the PLT for a shared object.
345   static const unsigned char plt_entry[plt_entry_size];
346 };
347
348 // The i386 target class.
349 // TLS info comes from
350 //   http://people.redhat.com/drepper/tls.pdf
351 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
352
353 class Target_i386 : public Sized_target<32, false>
354 {
355  public:
356   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
357
358   Target_i386(const Target::Target_info* info = &i386_info)
359     : Sized_target<32, false>(info),
360       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
361       got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
362       rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY),
363       got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
364   { }
365
366   // Process the relocations to determine unreferenced sections for
367   // garbage collection.
368   void
369   gc_process_relocs(Symbol_table* symtab,
370                     Layout* layout,
371                     Sized_relobj_file<32, false>* object,
372                     unsigned int data_shndx,
373                     unsigned int sh_type,
374                     const unsigned char* prelocs,
375                     size_t reloc_count,
376                     Output_section* output_section,
377                     bool needs_special_offset_handling,
378                     size_t local_symbol_count,
379                     const unsigned char* plocal_symbols);
380
381   // Scan the relocations to look for symbol adjustments.
382   void
383   scan_relocs(Symbol_table* symtab,
384               Layout* layout,
385               Sized_relobj_file<32, false>* object,
386               unsigned int data_shndx,
387               unsigned int sh_type,
388               const unsigned char* prelocs,
389               size_t reloc_count,
390               Output_section* output_section,
391               bool needs_special_offset_handling,
392               size_t local_symbol_count,
393               const unsigned char* plocal_symbols);
394
395   // Finalize the sections.
396   void
397   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
398
399   // Return the value to use for a dynamic which requires special
400   // treatment.
401   uint64_t
402   do_dynsym_value(const Symbol*) const;
403
404   // Relocate a section.
405   void
406   relocate_section(const Relocate_info<32, false>*,
407                    unsigned int sh_type,
408                    const unsigned char* prelocs,
409                    size_t reloc_count,
410                    Output_section* output_section,
411                    bool needs_special_offset_handling,
412                    unsigned char* view,
413                    elfcpp::Elf_types<32>::Elf_Addr view_address,
414                    section_size_type view_size,
415                    const Reloc_symbol_changes*);
416
417   // Scan the relocs during a relocatable link.
418   void
419   scan_relocatable_relocs(Symbol_table* symtab,
420                           Layout* layout,
421                           Sized_relobj_file<32, false>* object,
422                           unsigned int data_shndx,
423                           unsigned int sh_type,
424                           const unsigned char* prelocs,
425                           size_t reloc_count,
426                           Output_section* output_section,
427                           bool needs_special_offset_handling,
428                           size_t local_symbol_count,
429                           const unsigned char* plocal_symbols,
430                           Relocatable_relocs*);
431
432   // Scan the relocs for --emit-relocs.
433   void
434   emit_relocs_scan(Symbol_table* symtab,
435                    Layout* layout,
436                    Sized_relobj_file<32, false>* object,
437                    unsigned int data_shndx,
438                    unsigned int sh_type,
439                    const unsigned char* prelocs,
440                    size_t reloc_count,
441                    Output_section* output_section,
442                    bool needs_special_offset_handling,
443                    size_t local_symbol_count,
444                    const unsigned char* plocal_syms,
445                    Relocatable_relocs* rr);
446
447   // Emit relocations for a section.
448   void
449   relocate_relocs(const Relocate_info<32, false>*,
450                   unsigned int sh_type,
451                   const unsigned char* prelocs,
452                   size_t reloc_count,
453                   Output_section* output_section,
454                   elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
455                   unsigned char* view,
456                   elfcpp::Elf_types<32>::Elf_Addr view_address,
457                   section_size_type view_size,
458                   unsigned char* reloc_view,
459                   section_size_type reloc_view_size);
460
461   // Return a string used to fill a code section with nops.
462   std::string
463   do_code_fill(section_size_type length) const;
464
465   // Return whether SYM is defined by the ABI.
466   bool
467   do_is_defined_by_abi(const Symbol* sym) const
468   { return strcmp(sym->name(), "___tls_get_addr") == 0; }
469
470   // Return whether a symbol name implies a local label.  The UnixWare
471   // 2.1 cc generates temporary symbols that start with .X, so we
472   // recognize them here.  FIXME: do other SVR4 compilers also use .X?.
473   // If so, we should move the .X recognition into
474   // Target::do_is_local_label_name.
475   bool
476   do_is_local_label_name(const char* name) const
477   {
478     if (name[0] == '.' && name[1] == 'X')
479       return true;
480     return Target::do_is_local_label_name(name);
481   }
482
483   // Return the PLT address to use for a global symbol.
484   uint64_t
485   do_plt_address_for_global(const Symbol* gsym) const
486   { return this->plt_section()->address_for_global(gsym); }
487
488   uint64_t
489   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
490   { return this->plt_section()->address_for_local(relobj, symndx); }
491
492   // We can tell whether we take the address of a function.
493   inline bool
494   do_can_check_for_function_pointers() const
495   { return true; }
496
497   // Return the base for a DW_EH_PE_datarel encoding.
498   uint64_t
499   do_ehframe_datarel_base() const;
500
501   // Return whether SYM is call to a non-split function.
502   bool
503   do_is_call_to_non_split(const Symbol* sym, const unsigned char*) const;
504
505   // Adjust -fsplit-stack code which calls non-split-stack code.
506   void
507   do_calls_non_split(Relobj* object, unsigned int shndx,
508                      section_offset_type fnoffset, section_size_type fnsize,
509                      const unsigned char* prelocs, size_t reloc_count,
510                      unsigned char* view, section_size_type view_size,
511                      std::string* from, std::string* to) const;
512
513   // Return the size of the GOT section.
514   section_size_type
515   got_size() const
516   {
517     gold_assert(this->got_ != NULL);
518     return this->got_->data_size();
519   }
520
521   // Return the number of entries in the GOT.
522   unsigned int
523   got_entry_count() const
524   {
525     if (this->got_ == NULL)
526       return 0;
527     return this->got_size() / 4;
528   }
529
530   // Return the number of entries in the PLT.
531   unsigned int
532   plt_entry_count() const;
533
534   // Return the offset of the first non-reserved PLT entry.
535   unsigned int
536   first_plt_entry_offset() const;
537
538   // Return the size of each PLT entry.
539   unsigned int
540   plt_entry_size() const;
541
542  protected:
543   // Instantiate the plt_ member.
544   // This chooses the right PLT flavor for an executable or a shared object.
545   Output_data_plt_i386*
546   make_data_plt(Layout* layout,
547                 Output_data_got_plt_i386* got_plt,
548                 Output_data_space* got_irelative,
549                 bool dyn)
550   { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); }
551
552   virtual Output_data_plt_i386*
553   do_make_data_plt(Layout* layout,
554                    Output_data_got_plt_i386* got_plt,
555                    Output_data_space* got_irelative,
556                    bool dyn)
557   {
558     if (dyn)
559       return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative);
560     else
561       return new Output_data_plt_i386_exec(layout, got_plt, got_irelative);
562   }
563
564  private:
565   // The class which scans relocations.
566   struct Scan
567   {
568     static inline int
569
570     get_reference_flags(unsigned int r_type);
571
572     inline void
573     local(Symbol_table* symtab, Layout* layout, Target_i386* target,
574           Sized_relobj_file<32, false>* object,
575           unsigned int data_shndx,
576           Output_section* output_section,
577           const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
578           const elfcpp::Sym<32, false>& lsym,
579           bool is_discarded);
580
581     inline void
582     global(Symbol_table* symtab, Layout* layout, Target_i386* target,
583            Sized_relobj_file<32, false>* object,
584            unsigned int data_shndx,
585            Output_section* output_section,
586            const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
587            Symbol* gsym);
588
589     inline bool
590     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
591                                         Target_i386* target,
592                                         Sized_relobj_file<32, false>* object,
593                                         unsigned int data_shndx,
594                                         Output_section* output_section,
595                                         const elfcpp::Rel<32, false>& reloc,
596                                         unsigned int r_type,
597                                         const elfcpp::Sym<32, false>& lsym);
598
599     inline bool
600     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
601                                          Target_i386* target,
602                                          Sized_relobj_file<32, false>* object,
603                                          unsigned int data_shndx,
604                                          Output_section* output_section,
605                                          const elfcpp::Rel<32, false>& reloc,
606                                          unsigned int r_type,
607                                          Symbol* gsym);
608
609     inline bool
610     possible_function_pointer_reloc(unsigned int r_type);
611
612     bool
613     reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
614                               unsigned int r_type);
615
616     static void
617     unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
618
619     static void
620     unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
621                              Symbol*);
622   };
623
624   // The class which implements relocation.
625   class Relocate
626   {
627    public:
628     Relocate()
629       : skip_call_tls_get_addr_(false),
630         local_dynamic_type_(LOCAL_DYNAMIC_NONE)
631     { }
632
633     ~Relocate()
634     {
635       if (this->skip_call_tls_get_addr_)
636         {
637           // FIXME: This needs to specify the location somehow.
638           gold_error(_("missing expected TLS relocation"));
639         }
640     }
641
642     // Return whether the static relocation needs to be applied.
643     inline bool
644     should_apply_static_reloc(const Sized_symbol<32>* gsym,
645                               unsigned int r_type,
646                               bool is_32bit,
647                               Output_section* output_section);
648
649     // Do a relocation.  Return false if the caller should not issue
650     // any warnings about this relocation.
651     inline bool
652     relocate(const Relocate_info<32, false>*, unsigned int,
653              Target_i386*, Output_section*, size_t, const unsigned char*,
654              const Sized_symbol<32>*, const Symbol_value<32>*,
655              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
656              section_size_type);
657
658    private:
659     // Do a TLS relocation.
660     inline void
661     relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
662                  size_t relnum, const elfcpp::Rel<32, false>&,
663                  unsigned int r_type, const Sized_symbol<32>*,
664                  const Symbol_value<32>*,
665                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
666                  section_size_type);
667
668     // Do a TLS General-Dynamic to Initial-Exec transition.
669     inline void
670     tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
671                  Output_segment* tls_segment,
672                  const elfcpp::Rel<32, false>&, unsigned int r_type,
673                  elfcpp::Elf_types<32>::Elf_Addr value,
674                  unsigned char* view,
675                  section_size_type view_size);
676
677     // Do a TLS General-Dynamic to Local-Exec transition.
678     inline void
679     tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
680                  Output_segment* tls_segment,
681                  const elfcpp::Rel<32, false>&, unsigned int r_type,
682                  elfcpp::Elf_types<32>::Elf_Addr value,
683                  unsigned char* view,
684                  section_size_type view_size);
685
686     // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
687     // transition.
688     inline void
689     tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
690                       Output_segment* tls_segment,
691                       const elfcpp::Rel<32, false>&, unsigned int r_type,
692                       elfcpp::Elf_types<32>::Elf_Addr value,
693                       unsigned char* view,
694                       section_size_type view_size);
695
696     // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
697     // transition.
698     inline void
699     tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
700                       Output_segment* tls_segment,
701                       const elfcpp::Rel<32, false>&, unsigned int r_type,
702                       elfcpp::Elf_types<32>::Elf_Addr value,
703                       unsigned char* view,
704                       section_size_type view_size);
705
706     // Do a TLS Local-Dynamic to Local-Exec transition.
707     inline void
708     tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
709                  Output_segment* tls_segment,
710                  const elfcpp::Rel<32, false>&, unsigned int r_type,
711                  elfcpp::Elf_types<32>::Elf_Addr value,
712                  unsigned char* view,
713                  section_size_type view_size);
714
715     // Do a TLS Initial-Exec to Local-Exec transition.
716     static inline void
717     tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
718                  Output_segment* tls_segment,
719                  const elfcpp::Rel<32, false>&, unsigned int r_type,
720                  elfcpp::Elf_types<32>::Elf_Addr value,
721                  unsigned char* view,
722                  section_size_type view_size);
723
724     // We need to keep track of which type of local dynamic relocation
725     // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
726     enum Local_dynamic_type
727     {
728       LOCAL_DYNAMIC_NONE,
729       LOCAL_DYNAMIC_SUN,
730       LOCAL_DYNAMIC_GNU
731     };
732
733     // This is set if we should skip the next reloc, which should be a
734     // PLT32 reloc against ___tls_get_addr.
735     bool skip_call_tls_get_addr_;
736     // The type of local dynamic relocation we have seen in the section
737     // being relocated, if any.
738     Local_dynamic_type local_dynamic_type_;
739   };
740
741   // A class for inquiring about properties of a relocation,
742   // used while scanning relocs during a relocatable link and
743   // garbage collection.
744   class Classify_reloc :
745       public gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
746   {
747    public:
748     // Return the size of the addend of the relocation (only used for SHT_REL).
749     static unsigned int
750     get_size_for_reloc(unsigned int, Relobj*);
751   };
752
753   // Adjust TLS relocation type based on the options and whether this
754   // is a local symbol.
755   static tls::Tls_optimization
756   optimize_tls_reloc(bool is_final, int r_type);
757
758   // Check if relocation against this symbol is a candidate for
759   // conversion from
760   // mov foo@GOT(%reg), %reg
761   // to
762   // lea foo@GOTOFF(%reg), %reg.
763   static bool
764   can_convert_mov_to_lea(const Symbol* gsym)
765   {
766     gold_assert(gsym != NULL);
767     return (gsym->type() != elfcpp::STT_GNU_IFUNC
768             && !gsym->is_undefined ()
769             && !gsym->is_from_dynobj()
770             && !gsym->is_preemptible()
771             && (!parameters->options().shared()
772                 || (gsym->visibility() != elfcpp::STV_DEFAULT
773                     && gsym->visibility() != elfcpp::STV_PROTECTED)
774                 || parameters->options().Bsymbolic())
775             && strcmp(gsym->name(), "_DYNAMIC") != 0);
776   }
777
778   // Get the GOT section, creating it if necessary.
779   Output_data_got<32, false>*
780   got_section(Symbol_table*, Layout*);
781
782   // Get the GOT PLT section.
783   Output_data_got_plt_i386*
784   got_plt_section() const
785   {
786     gold_assert(this->got_plt_ != NULL);
787     return this->got_plt_;
788   }
789
790   // Get the GOT section for TLSDESC entries.
791   Output_data_got<32, false>*
792   got_tlsdesc_section() const
793   {
794     gold_assert(this->got_tlsdesc_ != NULL);
795     return this->got_tlsdesc_;
796   }
797
798   // Create the PLT section.
799   void
800   make_plt_section(Symbol_table* symtab, Layout* layout);
801
802   // Create a PLT entry for a global symbol.
803   void
804   make_plt_entry(Symbol_table*, Layout*, Symbol*);
805
806   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
807   void
808   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
809                              Sized_relobj_file<32, false>* relobj,
810                              unsigned int local_sym_index);
811
812   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
813   void
814   define_tls_base_symbol(Symbol_table*, Layout*);
815
816   // Create a GOT entry for the TLS module index.
817   unsigned int
818   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
819                       Sized_relobj_file<32, false>* object);
820
821   // Get the PLT section.
822   Output_data_plt_i386*
823   plt_section() const
824   {
825     gold_assert(this->plt_ != NULL);
826     return this->plt_;
827   }
828
829   // Get the dynamic reloc section, creating it if necessary.
830   Reloc_section*
831   rel_dyn_section(Layout*);
832
833   // Get the section to use for TLS_DESC relocations.
834   Reloc_section*
835   rel_tls_desc_section(Layout*) const;
836
837   // Get the section to use for IRELATIVE relocations.
838   Reloc_section*
839   rel_irelative_section(Layout*);
840
841   // Add a potential copy relocation.
842   void
843   copy_reloc(Symbol_table* symtab, Layout* layout,
844              Sized_relobj_file<32, false>* object,
845              unsigned int shndx, Output_section* output_section,
846              Symbol* sym, const elfcpp::Rel<32, false>& reloc)
847   {
848     unsigned int r_type = elfcpp::elf_r_type<32>(reloc.get_r_info());
849     this->copy_relocs_.copy_reloc(symtab, layout,
850                                   symtab->get_sized_symbol<32>(sym),
851                                   object, shndx, output_section,
852                                   r_type, reloc.get_r_offset(), 0,
853                                   this->rel_dyn_section(layout));
854   }
855
856   // Information about this specific target which we pass to the
857   // general Target structure.
858   static const Target::Target_info i386_info;
859
860   // The types of GOT entries needed for this platform.
861   // These values are exposed to the ABI in an incremental link.
862   // Do not renumber existing values without changing the version
863   // number of the .gnu_incremental_inputs section.
864   enum Got_type
865   {
866     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
867     GOT_TYPE_TLS_NOFFSET = 1,   // GOT entry for negative TLS offset
868     GOT_TYPE_TLS_OFFSET = 2,    // GOT entry for positive TLS offset
869     GOT_TYPE_TLS_PAIR = 3,      // GOT entry for TLS module/offset pair
870     GOT_TYPE_TLS_DESC = 4       // GOT entry for TLS_DESC pair
871   };
872
873   // The GOT section.
874   Output_data_got<32, false>* got_;
875   // The PLT section.
876   Output_data_plt_i386* plt_;
877   // The GOT PLT section.
878   Output_data_got_plt_i386* got_plt_;
879   // The GOT section for IRELATIVE relocations.
880   Output_data_space* got_irelative_;
881   // The GOT section for TLSDESC relocations.
882   Output_data_got<32, false>* got_tlsdesc_;
883   // The _GLOBAL_OFFSET_TABLE_ symbol.
884   Symbol* global_offset_table_;
885   // The dynamic reloc section.
886   Reloc_section* rel_dyn_;
887   // The section to use for IRELATIVE relocs.
888   Reloc_section* rel_irelative_;
889   // Relocs saved to avoid a COPY reloc.
890   Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
891   // Offset of the GOT entry for the TLS module index.
892   unsigned int got_mod_index_offset_;
893   // True if the _TLS_MODULE_BASE_ symbol has been defined.
894   bool tls_base_symbol_defined_;
895 };
896
897 const Target::Target_info Target_i386::i386_info =
898 {
899   32,                   // size
900   false,                // is_big_endian
901   elfcpp::EM_386,       // machine_code
902   false,                // has_make_symbol
903   false,                // has_resolve
904   true,                 // has_code_fill
905   true,                 // is_default_stack_executable
906   true,                 // can_icf_inline_merge_sections
907   '\0',                 // wrap_char
908   "/usr/lib/libc.so.1", // dynamic_linker
909   0x08048000,           // default_text_segment_address
910   0x1000,               // abi_pagesize (overridable by -z max-page-size)
911   0x1000,               // common_pagesize (overridable by -z common-page-size)
912   false,                // isolate_execinstr
913   0,                    // rosegment_gap
914   elfcpp::SHN_UNDEF,    // small_common_shndx
915   elfcpp::SHN_UNDEF,    // large_common_shndx
916   0,                    // small_common_section_flags
917   0,                    // large_common_section_flags
918   NULL,                 // attributes_section
919   NULL,                 // attributes_vendor
920   "_start",             // entry_symbol_name
921   32,                   // hash_entry_size
922 };
923
924 // Get the GOT section, creating it if necessary.
925
926 Output_data_got<32, false>*
927 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
928 {
929   if (this->got_ == NULL)
930     {
931       gold_assert(symtab != NULL && layout != NULL);
932
933       this->got_ = new Output_data_got<32, false>();
934
935       // When using -z now, we can treat .got.plt as a relro section.
936       // Without -z now, it is modified after program startup by lazy
937       // PLT relocations.
938       bool is_got_plt_relro = parameters->options().now();
939       Output_section_order got_order = (is_got_plt_relro
940                                         ? ORDER_RELRO
941                                         : ORDER_RELRO_LAST);
942       Output_section_order got_plt_order = (is_got_plt_relro
943                                             ? ORDER_RELRO
944                                             : ORDER_NON_RELRO_FIRST);
945
946       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
947                                       (elfcpp::SHF_ALLOC
948                                        | elfcpp::SHF_WRITE),
949                                       this->got_, got_order, true);
950
951       this->got_plt_ = new Output_data_got_plt_i386(layout);
952       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
953                                       (elfcpp::SHF_ALLOC
954                                        | elfcpp::SHF_WRITE),
955                                       this->got_plt_, got_plt_order,
956                                       is_got_plt_relro);
957
958       // The first three entries are reserved.
959       this->got_plt_->set_current_data_size(3 * 4);
960
961       if (!is_got_plt_relro)
962         {
963           // Those bytes can go into the relro segment.
964           layout->increase_relro(3 * 4);
965         }
966
967       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
968       this->global_offset_table_ =
969         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
970                                       Symbol_table::PREDEFINED,
971                                       this->got_plt_,
972                                       0, 0, elfcpp::STT_OBJECT,
973                                       elfcpp::STB_LOCAL,
974                                       elfcpp::STV_HIDDEN, 0,
975                                       false, false);
976
977       // If there are any IRELATIVE relocations, they get GOT entries
978       // in .got.plt after the jump slot relocations.
979       this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
980       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
981                                       (elfcpp::SHF_ALLOC
982                                        | elfcpp::SHF_WRITE),
983                                       this->got_irelative_,
984                                       got_plt_order, is_got_plt_relro);
985
986       // If there are any TLSDESC relocations, they get GOT entries in
987       // .got.plt after the jump slot entries.
988       this->got_tlsdesc_ = new Output_data_got<32, false>();
989       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
990                                       (elfcpp::SHF_ALLOC
991                                        | elfcpp::SHF_WRITE),
992                                       this->got_tlsdesc_,
993                                       got_plt_order, is_got_plt_relro);
994     }
995
996   return this->got_;
997 }
998
999 // Get the dynamic reloc section, creating it if necessary.
1000
1001 Target_i386::Reloc_section*
1002 Target_i386::rel_dyn_section(Layout* layout)
1003 {
1004   if (this->rel_dyn_ == NULL)
1005     {
1006       gold_assert(layout != NULL);
1007       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1008       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1009                                       elfcpp::SHF_ALLOC, this->rel_dyn_,
1010                                       ORDER_DYNAMIC_RELOCS, false);
1011     }
1012   return this->rel_dyn_;
1013 }
1014
1015 // Get the section to use for IRELATIVE relocs, creating it if
1016 // necessary.  These go in .rel.dyn, but only after all other dynamic
1017 // relocations.  They need to follow the other dynamic relocations so
1018 // that they can refer to global variables initialized by those
1019 // relocs.
1020
1021 Target_i386::Reloc_section*
1022 Target_i386::rel_irelative_section(Layout* layout)
1023 {
1024   if (this->rel_irelative_ == NULL)
1025     {
1026       // Make sure we have already create the dynamic reloc section.
1027       this->rel_dyn_section(layout);
1028       this->rel_irelative_ = new Reloc_section(false);
1029       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1030                                       elfcpp::SHF_ALLOC, this->rel_irelative_,
1031                                       ORDER_DYNAMIC_RELOCS, false);
1032       gold_assert(this->rel_dyn_->output_section()
1033                   == this->rel_irelative_->output_section());
1034     }
1035   return this->rel_irelative_;
1036 }
1037
1038 // Write the first three reserved words of the .got.plt section.
1039 // The remainder of the section is written while writing the PLT
1040 // in Output_data_plt_i386::do_write.
1041
1042 void
1043 Output_data_got_plt_i386::do_write(Output_file* of)
1044 {
1045   // The first entry in the GOT is the address of the .dynamic section
1046   // aka the PT_DYNAMIC segment.  The next two entries are reserved.
1047   // We saved space for them when we created the section in
1048   // Target_i386::got_section.
1049   const off_t got_file_offset = this->offset();
1050   gold_assert(this->data_size() >= 12);
1051   unsigned char* const got_view = of->get_output_view(got_file_offset, 12);
1052   Output_section* dynamic = this->layout_->dynamic_section();
1053   uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1054   elfcpp::Swap<32, false>::writeval(got_view, dynamic_addr);
1055   memset(got_view + 4, 0, 8);
1056   of->write_output_view(got_file_offset, 12, got_view);
1057 }
1058
1059 // Create the PLT section.  The ordinary .got section is an argument,
1060 // since we need to refer to the start.  We also create our own .got
1061 // section just for PLT entries.
1062
1063 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
1064                                            uint64_t addralign,
1065                                            Output_data_got_plt_i386* got_plt,
1066                                            Output_data_space* got_irelative)
1067   : Output_section_data(addralign),
1068     tls_desc_rel_(NULL), irelative_rel_(NULL), got_plt_(got_plt),
1069     got_irelative_(got_irelative), count_(0), irelative_count_(0),
1070     global_ifuncs_(), local_ifuncs_()
1071 {
1072   this->rel_ = new Reloc_section(false);
1073   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1074                                   elfcpp::SHF_ALLOC, this->rel_,
1075                                   ORDER_DYNAMIC_PLT_RELOCS, false);
1076 }
1077
1078 void
1079 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
1080 {
1081   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
1082   // linker, and so do we.
1083   os->set_entsize(4);
1084 }
1085
1086 // Add an entry to the PLT.
1087
1088 void
1089 Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
1090                                 Symbol* gsym)
1091 {
1092   gold_assert(!gsym->has_plt_offset());
1093
1094   // Every PLT entry needs a reloc.
1095   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1096       && gsym->can_use_relative_reloc(false))
1097     {
1098       gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size());
1099       ++this->irelative_count_;
1100       section_offset_type got_offset =
1101         this->got_irelative_->current_data_size();
1102       this->got_irelative_->set_current_data_size(got_offset + 4);
1103       Reloc_section* rel = this->rel_irelative(symtab, layout);
1104       rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
1105                                         this->got_irelative_, got_offset);
1106       struct Global_ifunc gi;
1107       gi.sym = gsym;
1108       gi.got_offset = got_offset;
1109       this->global_ifuncs_.push_back(gi);
1110     }
1111   else
1112     {
1113       // When setting the PLT offset we skip the initial reserved PLT
1114       // entry.
1115       gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size());
1116
1117       ++this->count_;
1118
1119       section_offset_type got_offset = this->got_plt_->current_data_size();
1120
1121       // Every PLT entry needs a GOT entry which points back to the
1122       // PLT entry (this will be changed by the dynamic linker,
1123       // normally lazily when the function is called).
1124       this->got_plt_->set_current_data_size(got_offset + 4);
1125
1126       gsym->set_needs_dynsym_entry();
1127       this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
1128                              got_offset);
1129     }
1130
1131   // Note that we don't need to save the symbol.  The contents of the
1132   // PLT are independent of which symbols are used.  The symbols only
1133   // appear in the relocations.
1134 }
1135
1136 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
1137 // the PLT offset.
1138
1139 unsigned int
1140 Output_data_plt_i386::add_local_ifunc_entry(
1141     Symbol_table* symtab,
1142     Layout* layout,
1143     Sized_relobj_file<32, false>* relobj,
1144     unsigned int local_sym_index)
1145 {
1146   unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
1147   ++this->irelative_count_;
1148
1149   section_offset_type got_offset = this->got_irelative_->current_data_size();
1150
1151   // Every PLT entry needs a GOT entry which points back to the PLT
1152   // entry.
1153   this->got_irelative_->set_current_data_size(got_offset + 4);
1154
1155   // Every PLT entry needs a reloc.
1156   Reloc_section* rel = this->rel_irelative(symtab, layout);
1157   rel->add_symbolless_local_addend(relobj, local_sym_index,
1158                                    elfcpp::R_386_IRELATIVE,
1159                                    this->got_irelative_, got_offset);
1160
1161   struct Local_ifunc li;
1162   li.object = relobj;
1163   li.local_sym_index = local_sym_index;
1164   li.got_offset = got_offset;
1165   this->local_ifuncs_.push_back(li);
1166
1167   return plt_offset;
1168 }
1169
1170 // Return where the TLS_DESC relocations should go, creating it if
1171 // necessary. These follow the JUMP_SLOT relocations.
1172
1173 Output_data_plt_i386::Reloc_section*
1174 Output_data_plt_i386::rel_tls_desc(Layout* layout)
1175 {
1176   if (this->tls_desc_rel_ == NULL)
1177     {
1178       this->tls_desc_rel_ = new Reloc_section(false);
1179       layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1180                                       elfcpp::SHF_ALLOC, this->tls_desc_rel_,
1181                                       ORDER_DYNAMIC_PLT_RELOCS, false);
1182       gold_assert(this->tls_desc_rel_->output_section()
1183                   == this->rel_->output_section());
1184     }
1185   return this->tls_desc_rel_;
1186 }
1187
1188 // Return where the IRELATIVE relocations should go in the PLT.  These
1189 // follow the JUMP_SLOT and TLS_DESC relocations.
1190
1191 Output_data_plt_i386::Reloc_section*
1192 Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
1193 {
1194   if (this->irelative_rel_ == NULL)
1195     {
1196       // Make sure we have a place for the TLS_DESC relocations, in
1197       // case we see any later on.
1198       this->rel_tls_desc(layout);
1199       this->irelative_rel_ = new Reloc_section(false);
1200       layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1201                                       elfcpp::SHF_ALLOC, this->irelative_rel_,
1202                                       ORDER_DYNAMIC_PLT_RELOCS, false);
1203       gold_assert(this->irelative_rel_->output_section()
1204                   == this->rel_->output_section());
1205
1206       if (parameters->doing_static_link())
1207         {
1208           // A statically linked executable will only have a .rel.plt
1209           // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
1210           // symbols.  The library will use these symbols to locate
1211           // the IRELATIVE relocs at program startup time.
1212           symtab->define_in_output_data("__rel_iplt_start", NULL,
1213                                         Symbol_table::PREDEFINED,
1214                                         this->irelative_rel_, 0, 0,
1215                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1216                                         elfcpp::STV_HIDDEN, 0, false, true);
1217           symtab->define_in_output_data("__rel_iplt_end", NULL,
1218                                         Symbol_table::PREDEFINED,
1219                                         this->irelative_rel_, 0, 0,
1220                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1221                                         elfcpp::STV_HIDDEN, 0, true, true);
1222         }
1223     }
1224   return this->irelative_rel_;
1225 }
1226
1227 // Return the PLT address to use for a global symbol.
1228
1229 uint64_t
1230 Output_data_plt_i386::address_for_global(const Symbol* gsym)
1231 {
1232   uint64_t offset = 0;
1233   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1234       && gsym->can_use_relative_reloc(false))
1235     offset = (this->count_ + 1) * this->get_plt_entry_size();
1236   return this->address() + offset + gsym->plt_offset();
1237 }
1238
1239 // Return the PLT address to use for a local symbol.  These are always
1240 // IRELATIVE relocs.
1241
1242 uint64_t
1243 Output_data_plt_i386::address_for_local(const Relobj* object,
1244                                         unsigned int r_sym)
1245 {
1246   return (this->address()
1247           + (this->count_ + 1) * this->get_plt_entry_size()
1248           + object->local_plt_offset(r_sym));
1249 }
1250
1251 // The first entry in the PLT for an executable.
1252
1253 const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] =
1254 {
1255   0xff, 0x35,   // pushl contents of memory address
1256   0, 0, 0, 0,   // replaced with address of .got + 4
1257   0xff, 0x25,   // jmp indirect
1258   0, 0, 0, 0,   // replaced with address of .got + 8
1259   0, 0, 0, 0    // unused
1260 };
1261
1262 void
1263 Output_data_plt_i386_exec::do_fill_first_plt_entry(
1264     unsigned char* pov,
1265     elfcpp::Elf_types<32>::Elf_Addr got_address)
1266 {
1267   memcpy(pov, first_plt_entry, plt_entry_size);
1268   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1269   elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1270 }
1271
1272 // The first entry in the PLT for a shared object.
1273
1274 const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] =
1275 {
1276   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
1277   0xff, 0xa3, 8, 0, 0, 0,       // jmp *8(%ebx)
1278   0, 0, 0, 0                    // unused
1279 };
1280
1281 void
1282 Output_data_plt_i386_dyn::do_fill_first_plt_entry(
1283     unsigned char* pov,
1284     elfcpp::Elf_types<32>::Elf_Addr)
1285 {
1286   memcpy(pov, first_plt_entry, plt_entry_size);
1287 }
1288
1289 // Subsequent entries in the PLT for an executable.
1290
1291 const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] =
1292 {
1293   0xff, 0x25,   // jmp indirect
1294   0, 0, 0, 0,   // replaced with address of symbol in .got
1295   0x68,         // pushl immediate
1296   0, 0, 0, 0,   // replaced with offset into relocation table
1297   0xe9,         // jmp relative
1298   0, 0, 0, 0    // replaced with offset to start of .plt
1299 };
1300
1301 unsigned int
1302 Output_data_plt_i386_exec::do_fill_plt_entry(
1303     unsigned char* pov,
1304     elfcpp::Elf_types<32>::Elf_Addr got_address,
1305     unsigned int got_offset,
1306     unsigned int plt_offset,
1307     unsigned int plt_rel_offset)
1308 {
1309   memcpy(pov, plt_entry, plt_entry_size);
1310   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1311                                               got_address + got_offset);
1312   elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1313   elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1314   return 6;
1315 }
1316
1317 // Subsequent entries in the PLT for a shared object.
1318
1319 const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] =
1320 {
1321   0xff, 0xa3,   // jmp *offset(%ebx)
1322   0, 0, 0, 0,   // replaced with offset of symbol in .got
1323   0x68,         // pushl immediate
1324   0, 0, 0, 0,   // replaced with offset into relocation table
1325   0xe9,         // jmp relative
1326   0, 0, 0, 0    // replaced with offset to start of .plt
1327 };
1328
1329 unsigned int
1330 Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov,
1331                                             elfcpp::Elf_types<32>::Elf_Addr,
1332                                             unsigned int got_offset,
1333                                             unsigned int plt_offset,
1334                                             unsigned int plt_rel_offset)
1335 {
1336   memcpy(pov, plt_entry, plt_entry_size);
1337   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1338   elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1339   elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
1340   return 6;
1341 }
1342
1343 // The .eh_frame unwind information for the PLT.
1344
1345 const unsigned char
1346 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1347 {
1348   1,                            // CIE version.
1349   'z',                          // Augmentation: augmentation size included.
1350   'R',                          // Augmentation: FDE encoding included.
1351   '\0',                         // End of augmentation string.
1352   1,                            // Code alignment factor.
1353   0x7c,                         // Data alignment factor.
1354   8,                            // Return address column.
1355   1,                            // Augmentation size.
1356   (elfcpp::DW_EH_PE_pcrel       // FDE encoding.
1357    | elfcpp::DW_EH_PE_sdata4),
1358   elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1359   elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1360   elfcpp::DW_CFA_nop,           // Align to 16 bytes.
1361   elfcpp::DW_CFA_nop
1362 };
1363
1364 const unsigned char
1365 Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1366 {
1367   0, 0, 0, 0,                           // Replaced with offset to .plt.
1368   0, 0, 0, 0,                           // Replaced with size of .plt.
1369   0,                                    // Augmentation size.
1370   elfcpp::DW_CFA_def_cfa_offset, 8,     // DW_CFA_def_cfa_offset: 8.
1371   elfcpp::DW_CFA_advance_loc + 6,       // Advance 6 to __PLT__ + 6.
1372   elfcpp::DW_CFA_def_cfa_offset, 12,    // DW_CFA_def_cfa_offset: 12.
1373   elfcpp::DW_CFA_advance_loc + 10,      // Advance 10 to __PLT__ + 16.
1374   elfcpp::DW_CFA_def_cfa_expression,    // DW_CFA_def_cfa_expression.
1375   11,                                   // Block length.
1376   elfcpp::DW_OP_breg4, 4,               // Push %esp + 4.
1377   elfcpp::DW_OP_breg8, 0,               // Push %eip.
1378   elfcpp::DW_OP_lit15,                  // Push 0xf.
1379   elfcpp::DW_OP_and,                    // & (%eip & 0xf).
1380   elfcpp::DW_OP_lit11,                  // Push 0xb.
1381   elfcpp::DW_OP_ge,                     // >= ((%eip & 0xf) >= 0xb)
1382   elfcpp::DW_OP_lit2,                   // Push 2.
1383   elfcpp::DW_OP_shl,                    // << (((%eip & 0xf) >= 0xb) << 2)
1384   elfcpp::DW_OP_plus,                   // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1385   elfcpp::DW_CFA_nop,                   // Align to 32 bytes.
1386   elfcpp::DW_CFA_nop,
1387   elfcpp::DW_CFA_nop,
1388   elfcpp::DW_CFA_nop
1389 };
1390
1391 // Write out the PLT.  This uses the hand-coded instructions above,
1392 // and adjusts them as needed.  This is all specified by the i386 ELF
1393 // Processor Supplement.
1394
1395 void
1396 Output_data_plt_i386::do_write(Output_file* of)
1397 {
1398   const off_t offset = this->offset();
1399   const section_size_type oview_size =
1400     convert_to_section_size_type(this->data_size());
1401   unsigned char* const oview = of->get_output_view(offset, oview_size);
1402
1403   const off_t got_file_offset = this->got_plt_->offset();
1404   gold_assert(parameters->incremental_update()
1405               || (got_file_offset + this->got_plt_->data_size()
1406                   == this->got_irelative_->offset()));
1407   const section_size_type got_size =
1408     convert_to_section_size_type(this->got_plt_->data_size()
1409                                  + this->got_irelative_->data_size());
1410
1411   unsigned char* const got_view = of->get_output_view(got_file_offset,
1412                                                       got_size);
1413
1414   unsigned char* pov = oview;
1415
1416   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1417   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1418
1419   this->fill_first_plt_entry(pov, got_address);
1420   pov += this->get_plt_entry_size();
1421
1422   // The first three entries in the GOT are reserved, and are written
1423   // by Output_data_got_plt_i386::do_write.
1424   unsigned char* got_pov = got_view + 12;
1425
1426   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1427
1428   unsigned int plt_offset = this->get_plt_entry_size();
1429   unsigned int plt_rel_offset = 0;
1430   unsigned int got_offset = 12;
1431   const unsigned int count = this->count_ + this->irelative_count_;
1432   for (unsigned int i = 0;
1433        i < count;
1434        ++i,
1435          pov += this->get_plt_entry_size(),
1436          got_pov += 4,
1437          plt_offset += this->get_plt_entry_size(),
1438          plt_rel_offset += rel_size,
1439          got_offset += 4)
1440     {
1441       // Set and adjust the PLT entry itself.
1442       unsigned int lazy_offset = this->fill_plt_entry(pov,
1443                                                       got_address,
1444                                                       got_offset,
1445                                                       plt_offset,
1446                                                       plt_rel_offset);
1447
1448       // Set the entry in the GOT.
1449       elfcpp::Swap<32, false>::writeval(got_pov,
1450                                         plt_address + plt_offset + lazy_offset);
1451     }
1452
1453   // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1454   // the GOT to point to the actual symbol value, rather than point to
1455   // the PLT entry.  That will let the dynamic linker call the right
1456   // function when resolving IRELATIVE relocations.
1457   unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
1458   for (std::vector<Global_ifunc>::const_iterator p =
1459          this->global_ifuncs_.begin();
1460        p != this->global_ifuncs_.end();
1461        ++p)
1462     {
1463       const Sized_symbol<32>* ssym =
1464         static_cast<const Sized_symbol<32>*>(p->sym);
1465       elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1466                                         ssym->value());
1467     }
1468
1469   for (std::vector<Local_ifunc>::const_iterator p =
1470          this->local_ifuncs_.begin();
1471        p != this->local_ifuncs_.end();
1472        ++p)
1473     {
1474       const Symbol_value<32>* psymval =
1475         p->object->local_symbol(p->local_sym_index);
1476       elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1477                                         psymval->value(p->object, 0));
1478     }
1479
1480   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1481   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1482
1483   of->write_output_view(offset, oview_size, oview);
1484   of->write_output_view(got_file_offset, got_size, got_view);
1485 }
1486
1487 // Create the PLT section.
1488
1489 void
1490 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1491 {
1492   if (this->plt_ == NULL)
1493     {
1494       // Create the GOT sections first.
1495       this->got_section(symtab, layout);
1496
1497       const bool dyn = parameters->options().output_is_position_independent();
1498       this->plt_ = this->make_data_plt(layout,
1499                                        this->got_plt_,
1500                                        this->got_irelative_,
1501                                        dyn);
1502
1503       // Add unwind information if requested.
1504       if (parameters->options().ld_generated_unwind_info())
1505         this->plt_->add_eh_frame(layout);
1506
1507       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1508                                       (elfcpp::SHF_ALLOC
1509                                        | elfcpp::SHF_EXECINSTR),
1510                                       this->plt_, ORDER_PLT, false);
1511
1512       // Make the sh_info field of .rel.plt point to .plt.
1513       Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1514       rel_plt_os->set_info_section(this->plt_->output_section());
1515     }
1516 }
1517
1518 // Create a PLT entry for a global symbol.
1519
1520 void
1521 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1522 {
1523   if (gsym->has_plt_offset())
1524     return;
1525   if (this->plt_ == NULL)
1526     this->make_plt_section(symtab, layout);
1527   this->plt_->add_entry(symtab, layout, gsym);
1528 }
1529
1530 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1531
1532 void
1533 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1534                                         Sized_relobj_file<32, false>* relobj,
1535                                         unsigned int local_sym_index)
1536 {
1537   if (relobj->local_has_plt_offset(local_sym_index))
1538     return;
1539   if (this->plt_ == NULL)
1540     this->make_plt_section(symtab, layout);
1541   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1542                                                               relobj,
1543                                                               local_sym_index);
1544   relobj->set_local_plt_offset(local_sym_index, plt_offset);
1545 }
1546
1547 // Return the number of entries in the PLT.
1548
1549 unsigned int
1550 Target_i386::plt_entry_count() const
1551 {
1552   if (this->plt_ == NULL)
1553     return 0;
1554   return this->plt_->entry_count();
1555 }
1556
1557 // Return the offset of the first non-reserved PLT entry.
1558
1559 unsigned int
1560 Target_i386::first_plt_entry_offset() const
1561 {
1562   return this->plt_->first_plt_entry_offset();
1563 }
1564
1565 // Return the size of each PLT entry.
1566
1567 unsigned int
1568 Target_i386::plt_entry_size() const
1569 {
1570   return this->plt_->get_plt_entry_size();
1571 }
1572
1573 // Get the section to use for TLS_DESC relocations.
1574
1575 Target_i386::Reloc_section*
1576 Target_i386::rel_tls_desc_section(Layout* layout) const
1577 {
1578   return this->plt_section()->rel_tls_desc(layout);
1579 }
1580
1581 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1582
1583 void
1584 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1585 {
1586   if (this->tls_base_symbol_defined_)
1587     return;
1588
1589   Output_segment* tls_segment = layout->tls_segment();
1590   if (tls_segment != NULL)
1591     {
1592       bool is_exec = parameters->options().output_is_executable();
1593       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1594                                        Symbol_table::PREDEFINED,
1595                                        tls_segment, 0, 0,
1596                                        elfcpp::STT_TLS,
1597                                        elfcpp::STB_LOCAL,
1598                                        elfcpp::STV_HIDDEN, 0,
1599                                        (is_exec
1600                                         ? Symbol::SEGMENT_END
1601                                         : Symbol::SEGMENT_START),
1602                                        true);
1603     }
1604   this->tls_base_symbol_defined_ = true;
1605 }
1606
1607 // Create a GOT entry for the TLS module index.
1608
1609 unsigned int
1610 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1611                                  Sized_relobj_file<32, false>* object)
1612 {
1613   if (this->got_mod_index_offset_ == -1U)
1614     {
1615       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1616       Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1617       Output_data_got<32, false>* got = this->got_section(symtab, layout);
1618       unsigned int got_offset = got->add_constant(0);
1619       rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1620                          got_offset);
1621       got->add_constant(0);
1622       this->got_mod_index_offset_ = got_offset;
1623     }
1624   return this->got_mod_index_offset_;
1625 }
1626
1627 // Optimize the TLS relocation type based on what we know about the
1628 // symbol.  IS_FINAL is true if the final address of this symbol is
1629 // known at link time.
1630
1631 tls::Tls_optimization
1632 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1633 {
1634   // If we are generating a shared library, then we can't do anything
1635   // in the linker.
1636   if (parameters->options().shared())
1637     return tls::TLSOPT_NONE;
1638
1639   switch (r_type)
1640     {
1641     case elfcpp::R_386_TLS_GD:
1642     case elfcpp::R_386_TLS_GOTDESC:
1643     case elfcpp::R_386_TLS_DESC_CALL:
1644       // These are General-Dynamic which permits fully general TLS
1645       // access.  Since we know that we are generating an executable,
1646       // we can convert this to Initial-Exec.  If we also know that
1647       // this is a local symbol, we can further switch to Local-Exec.
1648       if (is_final)
1649         return tls::TLSOPT_TO_LE;
1650       return tls::TLSOPT_TO_IE;
1651
1652     case elfcpp::R_386_TLS_LDM:
1653       // This is Local-Dynamic, which refers to a local symbol in the
1654       // dynamic TLS block.  Since we know that we generating an
1655       // executable, we can switch to Local-Exec.
1656       return tls::TLSOPT_TO_LE;
1657
1658     case elfcpp::R_386_TLS_LDO_32:
1659       // Another type of Local-Dynamic relocation.
1660       return tls::TLSOPT_TO_LE;
1661
1662     case elfcpp::R_386_TLS_IE:
1663     case elfcpp::R_386_TLS_GOTIE:
1664     case elfcpp::R_386_TLS_IE_32:
1665       // These are Initial-Exec relocs which get the thread offset
1666       // from the GOT.  If we know that we are linking against the
1667       // local symbol, we can switch to Local-Exec, which links the
1668       // thread offset into the instruction.
1669       if (is_final)
1670         return tls::TLSOPT_TO_LE;
1671       return tls::TLSOPT_NONE;
1672
1673     case elfcpp::R_386_TLS_LE:
1674     case elfcpp::R_386_TLS_LE_32:
1675       // When we already have Local-Exec, there is nothing further we
1676       // can do.
1677       return tls::TLSOPT_NONE;
1678
1679     default:
1680       gold_unreachable();
1681     }
1682 }
1683
1684 // Get the Reference_flags for a particular relocation.
1685
1686 int
1687 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1688 {
1689   switch (r_type)
1690     {
1691     case elfcpp::R_386_NONE:
1692     case elfcpp::R_386_GNU_VTINHERIT:
1693     case elfcpp::R_386_GNU_VTENTRY:
1694     case elfcpp::R_386_GOTPC:
1695       // No symbol reference.
1696       return 0;
1697
1698     case elfcpp::R_386_32:
1699     case elfcpp::R_386_16:
1700     case elfcpp::R_386_8:
1701       return Symbol::ABSOLUTE_REF;
1702
1703     case elfcpp::R_386_PC32:
1704     case elfcpp::R_386_PC16:
1705     case elfcpp::R_386_PC8:
1706     case elfcpp::R_386_GOTOFF:
1707       return Symbol::RELATIVE_REF;
1708
1709     case elfcpp::R_386_PLT32:
1710       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1711
1712     case elfcpp::R_386_GOT32:
1713     case elfcpp::R_386_GOT32X:
1714       // Absolute in GOT.
1715       return Symbol::ABSOLUTE_REF;
1716
1717     case elfcpp::R_386_TLS_GD:            // Global-dynamic
1718     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
1719     case elfcpp::R_386_TLS_DESC_CALL:
1720     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
1721     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
1722     case elfcpp::R_386_TLS_IE:            // Initial-exec
1723     case elfcpp::R_386_TLS_IE_32:
1724     case elfcpp::R_386_TLS_GOTIE:
1725     case elfcpp::R_386_TLS_LE:            // Local-exec
1726     case elfcpp::R_386_TLS_LE_32:
1727       return Symbol::TLS_REF;
1728
1729     case elfcpp::R_386_COPY:
1730     case elfcpp::R_386_GLOB_DAT:
1731     case elfcpp::R_386_JUMP_SLOT:
1732     case elfcpp::R_386_RELATIVE:
1733     case elfcpp::R_386_IRELATIVE:
1734     case elfcpp::R_386_TLS_TPOFF:
1735     case elfcpp::R_386_TLS_DTPMOD32:
1736     case elfcpp::R_386_TLS_DTPOFF32:
1737     case elfcpp::R_386_TLS_TPOFF32:
1738     case elfcpp::R_386_TLS_DESC:
1739     case elfcpp::R_386_32PLT:
1740     case elfcpp::R_386_TLS_GD_32:
1741     case elfcpp::R_386_TLS_GD_PUSH:
1742     case elfcpp::R_386_TLS_GD_CALL:
1743     case elfcpp::R_386_TLS_GD_POP:
1744     case elfcpp::R_386_TLS_LDM_32:
1745     case elfcpp::R_386_TLS_LDM_PUSH:
1746     case elfcpp::R_386_TLS_LDM_CALL:
1747     case elfcpp::R_386_TLS_LDM_POP:
1748     case elfcpp::R_386_USED_BY_INTEL_200:
1749     default:
1750       // Not expected.  We will give an error later.
1751       return 0;
1752     }
1753 }
1754
1755 // Report an unsupported relocation against a local symbol.
1756
1757 void
1758 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1759                                            unsigned int r_type)
1760 {
1761   gold_error(_("%s: unsupported reloc %u against local symbol"),
1762              object->name().c_str(), r_type);
1763 }
1764
1765 // Return whether we need to make a PLT entry for a relocation of a
1766 // given type against a STT_GNU_IFUNC symbol.
1767
1768 bool
1769 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1770     Sized_relobj_file<32, false>* object,
1771     unsigned int r_type)
1772 {
1773   int flags = Scan::get_reference_flags(r_type);
1774   if (flags & Symbol::TLS_REF)
1775     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1776                object->name().c_str(), r_type);
1777   return flags != 0;
1778 }
1779
1780 // Scan a relocation for a local symbol.
1781
1782 inline void
1783 Target_i386::Scan::local(Symbol_table* symtab,
1784                          Layout* layout,
1785                          Target_i386* target,
1786                          Sized_relobj_file<32, false>* object,
1787                          unsigned int data_shndx,
1788                          Output_section* output_section,
1789                          const elfcpp::Rel<32, false>& reloc,
1790                          unsigned int r_type,
1791                          const elfcpp::Sym<32, false>& lsym,
1792                          bool is_discarded)
1793 {
1794   if (is_discarded)
1795     return;
1796
1797   // A local STT_GNU_IFUNC symbol may require a PLT entry.
1798   if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1799       && this->reloc_needs_plt_for_ifunc(object, r_type))
1800     {
1801       unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1802       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1803     }
1804
1805   switch (r_type)
1806     {
1807     case elfcpp::R_386_NONE:
1808     case elfcpp::R_386_GNU_VTINHERIT:
1809     case elfcpp::R_386_GNU_VTENTRY:
1810       break;
1811
1812     case elfcpp::R_386_32:
1813       // If building a shared library (or a position-independent
1814       // executable), we need to create a dynamic relocation for
1815       // this location. The relocation applied at link time will
1816       // apply the link-time value, so we flag the location with
1817       // an R_386_RELATIVE relocation so the dynamic loader can
1818       // relocate it easily.
1819       if (parameters->options().output_is_position_independent())
1820         {
1821           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1822           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1823           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1824                                       output_section, data_shndx,
1825                                       reloc.get_r_offset());
1826         }
1827       break;
1828
1829     case elfcpp::R_386_16:
1830     case elfcpp::R_386_8:
1831       // If building a shared library (or a position-independent
1832       // executable), we need to create a dynamic relocation for
1833       // this location. Because the addend needs to remain in the
1834       // data section, we need to be careful not to apply this
1835       // relocation statically.
1836       if (parameters->options().output_is_position_independent())
1837         {
1838           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1839           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1840           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1841             rel_dyn->add_local(object, r_sym, r_type, output_section,
1842                                data_shndx, reloc.get_r_offset());
1843           else
1844             {
1845               gold_assert(lsym.get_st_value() == 0);
1846               unsigned int shndx = lsym.get_st_shndx();
1847               bool is_ordinary;
1848               shndx = object->adjust_sym_shndx(r_sym, shndx,
1849                                                &is_ordinary);
1850               if (!is_ordinary)
1851                 object->error(_("section symbol %u has bad shndx %u"),
1852                               r_sym, shndx);
1853               else
1854                 rel_dyn->add_local_section(object, shndx,
1855                                            r_type, output_section,
1856                                            data_shndx, reloc.get_r_offset());
1857             }
1858         }
1859       break;
1860
1861     case elfcpp::R_386_PC32:
1862     case elfcpp::R_386_PC16:
1863     case elfcpp::R_386_PC8:
1864       break;
1865
1866     case elfcpp::R_386_PLT32:
1867       // Since we know this is a local symbol, we can handle this as a
1868       // PC32 reloc.
1869       break;
1870
1871     case elfcpp::R_386_GOTOFF:
1872     case elfcpp::R_386_GOTPC:
1873       // We need a GOT section.
1874       target->got_section(symtab, layout);
1875       break;
1876
1877     case elfcpp::R_386_GOT32:
1878     case elfcpp::R_386_GOT32X:
1879       {
1880         // We need GOT section.
1881         Output_data_got<32, false>* got = target->got_section(symtab, layout);
1882
1883         // If the relocation symbol isn't IFUNC,
1884         // and is local, then we will convert
1885         // mov foo@GOT(%reg), %reg
1886         // to
1887         // lea foo@GOTOFF(%reg), %reg
1888         // in Relocate::relocate.
1889         if (reloc.get_r_offset() >= 2
1890             && lsym.get_st_type() != elfcpp::STT_GNU_IFUNC)
1891           {
1892             section_size_type stype;
1893             const unsigned char* view = object->section_contents(data_shndx,
1894                                                                  &stype, true);
1895             if (view[reloc.get_r_offset() - 2] == 0x8b)
1896               break;
1897           }
1898
1899         // Otherwise, the symbol requires a GOT entry.
1900         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1901
1902         // For a STT_GNU_IFUNC symbol we want the PLT offset.  That
1903         // lets function pointers compare correctly with shared
1904         // libraries.  Otherwise we would need an IRELATIVE reloc.
1905         bool is_new;
1906         if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1907           is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1908         else
1909           is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1910         if (is_new)
1911           {
1912             // If we are generating a shared object, we need to add a
1913             // dynamic RELATIVE relocation for this symbol's GOT entry.
1914             if (parameters->options().output_is_position_independent())
1915               {
1916                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1917                 unsigned int got_offset =
1918                   object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1919                 rel_dyn->add_local_relative(object, r_sym,
1920                                             elfcpp::R_386_RELATIVE,
1921                                             got, got_offset);
1922               }
1923           }
1924       }
1925       break;
1926
1927       // These are relocations which should only be seen by the
1928       // dynamic linker, and should never be seen here.
1929     case elfcpp::R_386_COPY:
1930     case elfcpp::R_386_GLOB_DAT:
1931     case elfcpp::R_386_JUMP_SLOT:
1932     case elfcpp::R_386_RELATIVE:
1933     case elfcpp::R_386_IRELATIVE:
1934     case elfcpp::R_386_TLS_TPOFF:
1935     case elfcpp::R_386_TLS_DTPMOD32:
1936     case elfcpp::R_386_TLS_DTPOFF32:
1937     case elfcpp::R_386_TLS_TPOFF32:
1938     case elfcpp::R_386_TLS_DESC:
1939       gold_error(_("%s: unexpected reloc %u in object file"),
1940                  object->name().c_str(), r_type);
1941       break;
1942
1943       // These are initial TLS relocs, which are expected when
1944       // linking.
1945     case elfcpp::R_386_TLS_GD:            // Global-dynamic
1946     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
1947     case elfcpp::R_386_TLS_DESC_CALL:
1948     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
1949     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
1950     case elfcpp::R_386_TLS_IE:            // Initial-exec
1951     case elfcpp::R_386_TLS_IE_32:
1952     case elfcpp::R_386_TLS_GOTIE:
1953     case elfcpp::R_386_TLS_LE:            // Local-exec
1954     case elfcpp::R_386_TLS_LE_32:
1955       {
1956         bool output_is_shared = parameters->options().shared();
1957         const tls::Tls_optimization optimized_type
1958             = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1959         switch (r_type)
1960           {
1961           case elfcpp::R_386_TLS_GD:          // Global-dynamic
1962             if (optimized_type == tls::TLSOPT_NONE)
1963               {
1964                 // Create a pair of GOT entries for the module index and
1965                 // dtv-relative offset.
1966                 Output_data_got<32, false>* got
1967                     = target->got_section(symtab, layout);
1968                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1969                 unsigned int shndx = lsym.get_st_shndx();
1970                 bool is_ordinary;
1971                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1972                 if (!is_ordinary)
1973                   object->error(_("local symbol %u has bad shndx %u"),
1974                               r_sym, shndx);
1975                 else
1976                   got->add_local_pair_with_rel(object, r_sym, shndx,
1977                                                GOT_TYPE_TLS_PAIR,
1978                                                target->rel_dyn_section(layout),
1979                                                elfcpp::R_386_TLS_DTPMOD32);
1980               }
1981             else if (optimized_type != tls::TLSOPT_TO_LE)
1982               unsupported_reloc_local(object, r_type);
1983             break;
1984
1985           case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (from ~oliva)
1986             target->define_tls_base_symbol(symtab, layout);
1987             if (optimized_type == tls::TLSOPT_NONE)
1988               {
1989                 // Create a double GOT entry with an R_386_TLS_DESC
1990                 // reloc.  The R_386_TLS_DESC reloc is resolved
1991                 // lazily, so the GOT entry needs to be in an area in
1992                 // .got.plt, not .got.  Call got_section to make sure
1993                 // the section has been created.
1994                 target->got_section(symtab, layout);
1995                 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1996                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1997                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1998                   {
1999                     unsigned int got_offset = got->add_constant(0);
2000                     // The local symbol value is stored in the second
2001                     // GOT entry.
2002                     got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
2003                     // That set the GOT offset of the local symbol to
2004                     // point to the second entry, but we want it to
2005                     // point to the first.
2006                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
2007                                                  got_offset);
2008                     Reloc_section* rt = target->rel_tls_desc_section(layout);
2009                     rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
2010                   }
2011               }
2012             else if (optimized_type != tls::TLSOPT_TO_LE)
2013               unsupported_reloc_local(object, r_type);
2014             break;
2015
2016           case elfcpp::R_386_TLS_DESC_CALL:
2017             break;
2018
2019           case elfcpp::R_386_TLS_LDM:         // Local-dynamic
2020             if (optimized_type == tls::TLSOPT_NONE)
2021               {
2022                 // Create a GOT entry for the module index.
2023                 target->got_mod_index_entry(symtab, layout, object);
2024               }
2025             else if (optimized_type != tls::TLSOPT_TO_LE)
2026               unsupported_reloc_local(object, r_type);
2027             break;
2028
2029           case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
2030             break;
2031
2032           case elfcpp::R_386_TLS_IE:          // Initial-exec
2033           case elfcpp::R_386_TLS_IE_32:
2034           case elfcpp::R_386_TLS_GOTIE:
2035             layout->set_has_static_tls();
2036             if (optimized_type == tls::TLSOPT_NONE)
2037               {
2038                 // For the R_386_TLS_IE relocation, we need to create a
2039                 // dynamic relocation when building a shared library.
2040                 if (r_type == elfcpp::R_386_TLS_IE
2041                     && parameters->options().shared())
2042                   {
2043                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2044                     unsigned int r_sym
2045                         = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2046                     rel_dyn->add_local_relative(object, r_sym,
2047                                                 elfcpp::R_386_RELATIVE,
2048                                                 output_section, data_shndx,
2049                                                 reloc.get_r_offset());
2050                   }
2051                 // Create a GOT entry for the tp-relative offset.
2052                 Output_data_got<32, false>* got
2053                     = target->got_section(symtab, layout);
2054                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2055                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2056                                            ? elfcpp::R_386_TLS_TPOFF32
2057                                            : elfcpp::R_386_TLS_TPOFF);
2058                 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2059                                          ? GOT_TYPE_TLS_OFFSET
2060                                          : GOT_TYPE_TLS_NOFFSET);
2061                 got->add_local_with_rel(object, r_sym, got_type,
2062                                         target->rel_dyn_section(layout),
2063                                         dyn_r_type);
2064               }
2065             else if (optimized_type != tls::TLSOPT_TO_LE)
2066               unsupported_reloc_local(object, r_type);
2067             break;
2068
2069           case elfcpp::R_386_TLS_LE:          // Local-exec
2070           case elfcpp::R_386_TLS_LE_32:
2071             layout->set_has_static_tls();
2072             if (output_is_shared)
2073               {
2074                 // We need to create a dynamic relocation.
2075                 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2076                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
2077                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2078                                            ? elfcpp::R_386_TLS_TPOFF32
2079                                            : elfcpp::R_386_TLS_TPOFF);
2080                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2081                 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
2082                                    data_shndx, reloc.get_r_offset());
2083               }
2084             break;
2085
2086           default:
2087             gold_unreachable();
2088           }
2089       }
2090       break;
2091
2092     case elfcpp::R_386_32PLT:
2093     case elfcpp::R_386_TLS_GD_32:
2094     case elfcpp::R_386_TLS_GD_PUSH:
2095     case elfcpp::R_386_TLS_GD_CALL:
2096     case elfcpp::R_386_TLS_GD_POP:
2097     case elfcpp::R_386_TLS_LDM_32:
2098     case elfcpp::R_386_TLS_LDM_PUSH:
2099     case elfcpp::R_386_TLS_LDM_CALL:
2100     case elfcpp::R_386_TLS_LDM_POP:
2101     case elfcpp::R_386_USED_BY_INTEL_200:
2102     default:
2103       unsupported_reloc_local(object, r_type);
2104       break;
2105     }
2106 }
2107
2108 // Report an unsupported relocation against a global symbol.
2109
2110 void
2111 Target_i386::Scan::unsupported_reloc_global(
2112     Sized_relobj_file<32, false>* object,
2113     unsigned int r_type,
2114     Symbol* gsym)
2115 {
2116   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2117              object->name().c_str(), r_type, gsym->demangled_name().c_str());
2118 }
2119
2120 inline bool
2121 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
2122 {
2123   switch (r_type)
2124     {
2125     case elfcpp::R_386_32:
2126     case elfcpp::R_386_16:
2127     case elfcpp::R_386_8:
2128     case elfcpp::R_386_GOTOFF:
2129     case elfcpp::R_386_GOT32:
2130     case elfcpp::R_386_GOT32X:
2131       {
2132         return true;
2133       }
2134     default:
2135       return false;
2136     }
2137   return false;
2138 }
2139
2140 inline bool
2141 Target_i386::Scan::local_reloc_may_be_function_pointer(
2142   Symbol_table* ,
2143   Layout* ,
2144   Target_i386* ,
2145   Sized_relobj_file<32, false>* ,
2146   unsigned int ,
2147   Output_section* ,
2148   const elfcpp::Rel<32, false>& ,
2149   unsigned int r_type,
2150   const elfcpp::Sym<32, false>&)
2151 {
2152   return possible_function_pointer_reloc(r_type);
2153 }
2154
2155 inline bool
2156 Target_i386::Scan::global_reloc_may_be_function_pointer(
2157   Symbol_table* ,
2158   Layout* ,
2159   Target_i386* ,
2160   Sized_relobj_file<32, false>* ,
2161   unsigned int ,
2162   Output_section* ,
2163   const elfcpp::Rel<32, false>& ,
2164   unsigned int r_type,
2165   Symbol*)
2166 {
2167   return possible_function_pointer_reloc(r_type);
2168 }
2169
2170 // Scan a relocation for a global symbol.
2171
2172 inline void
2173 Target_i386::Scan::global(Symbol_table* symtab,
2174                                  Layout* layout,
2175                                  Target_i386* target,
2176                                  Sized_relobj_file<32, false>* object,
2177                                  unsigned int data_shndx,
2178                                  Output_section* output_section,
2179                                  const elfcpp::Rel<32, false>& reloc,
2180                                  unsigned int r_type,
2181                                  Symbol* gsym)
2182 {
2183   // A STT_GNU_IFUNC symbol may require a PLT entry.
2184   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2185       && this->reloc_needs_plt_for_ifunc(object, r_type))
2186     target->make_plt_entry(symtab, layout, gsym);
2187
2188   switch (r_type)
2189     {
2190     case elfcpp::R_386_NONE:
2191     case elfcpp::R_386_GNU_VTINHERIT:
2192     case elfcpp::R_386_GNU_VTENTRY:
2193       break;
2194
2195     case elfcpp::R_386_32:
2196     case elfcpp::R_386_16:
2197     case elfcpp::R_386_8:
2198       {
2199         // Make a PLT entry if necessary.
2200         if (gsym->needs_plt_entry())
2201           {
2202             target->make_plt_entry(symtab, layout, gsym);
2203             // Since this is not a PC-relative relocation, we may be
2204             // taking the address of a function. In that case we need to
2205             // set the entry in the dynamic symbol table to the address of
2206             // the PLT entry.
2207             if (gsym->is_from_dynobj() && !parameters->options().shared())
2208               gsym->set_needs_dynsym_value();
2209           }
2210         // Make a dynamic relocation if necessary.
2211         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2212           {
2213             if (!parameters->options().output_is_position_independent()
2214                 && gsym->may_need_copy_reloc())
2215               {
2216                 target->copy_reloc(symtab, layout, object,
2217                                    data_shndx, output_section, gsym, reloc);
2218               }
2219             else if (r_type == elfcpp::R_386_32
2220                      && gsym->type() == elfcpp::STT_GNU_IFUNC
2221                      && gsym->can_use_relative_reloc(false)
2222                      && !gsym->is_from_dynobj()
2223                      && !gsym->is_undefined()
2224                      && !gsym->is_preemptible())
2225               {
2226                 // Use an IRELATIVE reloc for a locally defined
2227                 // STT_GNU_IFUNC symbol.  This makes a function
2228                 // address in a PIE executable match the address in a
2229                 // shared library that it links against.
2230                 Reloc_section* rel_dyn = target->rel_irelative_section(layout);
2231                 rel_dyn->add_symbolless_global_addend(gsym,
2232                                                       elfcpp::R_386_IRELATIVE,
2233                                                       output_section,
2234                                                       object, data_shndx,
2235                                                       reloc.get_r_offset());
2236               }
2237             else if (r_type == elfcpp::R_386_32
2238                      && gsym->can_use_relative_reloc(false))
2239               {
2240                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2241                 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2242                                              output_section, object,
2243                                              data_shndx, reloc.get_r_offset());
2244               }
2245             else
2246               {
2247                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2248                 rel_dyn->add_global(gsym, r_type, output_section, object,
2249                                     data_shndx, reloc.get_r_offset());
2250               }
2251           }
2252       }
2253       break;
2254
2255     case elfcpp::R_386_PC32:
2256     case elfcpp::R_386_PC16:
2257     case elfcpp::R_386_PC8:
2258       {
2259         // Make a PLT entry if necessary.
2260         if (gsym->needs_plt_entry())
2261           {
2262             // These relocations are used for function calls only in
2263             // non-PIC code.  For a 32-bit relocation in a shared library,
2264             // we'll need a text relocation anyway, so we can skip the
2265             // PLT entry and let the dynamic linker bind the call directly
2266             // to the target.  For smaller relocations, we should use a
2267             // PLT entry to ensure that the call can reach.
2268             if (!parameters->options().shared()
2269                 || r_type != elfcpp::R_386_PC32)
2270               target->make_plt_entry(symtab, layout, gsym);
2271           }
2272         // Make a dynamic relocation if necessary.
2273         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2274           {
2275             if (parameters->options().output_is_executable()
2276                 && gsym->may_need_copy_reloc())
2277               {
2278                 target->copy_reloc(symtab, layout, object,
2279                                    data_shndx, output_section, gsym, reloc);
2280               }
2281             else
2282               {
2283                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2284                 rel_dyn->add_global(gsym, r_type, output_section, object,
2285                                     data_shndx, reloc.get_r_offset());
2286               }
2287           }
2288       }
2289       break;
2290
2291     case elfcpp::R_386_GOT32:
2292     case elfcpp::R_386_GOT32X:
2293       {
2294         // The symbol requires a GOT section.
2295         Output_data_got<32, false>* got = target->got_section(symtab, layout);
2296
2297         // If we convert this from
2298         // mov foo@GOT(%reg), %reg
2299         // to
2300         // lea foo@GOTOFF(%reg), %reg
2301         // in Relocate::relocate, then there is nothing to do here.
2302         if (reloc.get_r_offset() >= 2
2303             && Target_i386::can_convert_mov_to_lea(gsym))
2304           {
2305             section_size_type stype;
2306             const unsigned char* view = object->section_contents(data_shndx,
2307                                                                  &stype, true);
2308             if (view[reloc.get_r_offset() - 2] == 0x8b)
2309               break;
2310           }
2311
2312         if (gsym->final_value_is_known())
2313           {
2314             // For a STT_GNU_IFUNC symbol we want the PLT address.
2315             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2316               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2317             else
2318               got->add_global(gsym, GOT_TYPE_STANDARD);
2319           }
2320         else
2321           {
2322             // If this symbol is not fully resolved, we need to add a
2323             // GOT entry with a dynamic relocation.
2324             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2325
2326             // Use a GLOB_DAT rather than a RELATIVE reloc if:
2327             //
2328             // 1) The symbol may be defined in some other module.
2329             //
2330             // 2) We are building a shared library and this is a
2331             // protected symbol; using GLOB_DAT means that the dynamic
2332             // linker can use the address of the PLT in the main
2333             // executable when appropriate so that function address
2334             // comparisons work.
2335             //
2336             // 3) This is a STT_GNU_IFUNC symbol in position dependent
2337             // code, again so that function address comparisons work.
2338             if (gsym->is_from_dynobj()
2339                 || gsym->is_undefined()
2340                 || gsym->is_preemptible()
2341                 || (gsym->visibility() == elfcpp::STV_PROTECTED
2342                     && parameters->options().shared())
2343                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2344                     && parameters->options().output_is_position_independent()))
2345               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2346                                        rel_dyn, elfcpp::R_386_GLOB_DAT);
2347             else
2348               {
2349                 // For a STT_GNU_IFUNC symbol we want to write the PLT
2350                 // offset into the GOT, so that function pointer
2351                 // comparisons work correctly.
2352                 bool is_new;
2353                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2354                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2355                 else
2356                   {
2357                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2358                     // Tell the dynamic linker to use the PLT address
2359                     // when resolving relocations.
2360                     if (gsym->is_from_dynobj()
2361                         && !parameters->options().shared())
2362                       gsym->set_needs_dynsym_value();
2363                   }
2364                 if (is_new)
2365                   {
2366                     unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2367                     rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2368                                                  got, got_off);
2369                   }
2370               }
2371           }
2372       }
2373       break;
2374
2375     case elfcpp::R_386_PLT32:
2376       // If the symbol is fully resolved, this is just a PC32 reloc.
2377       // Otherwise we need a PLT entry.
2378       if (gsym->final_value_is_known())
2379         break;
2380       // If building a shared library, we can also skip the PLT entry
2381       // if the symbol is defined in the output file and is protected
2382       // or hidden.
2383       if (gsym->is_defined()
2384           && !gsym->is_from_dynobj()
2385           && !gsym->is_preemptible())
2386         break;
2387       target->make_plt_entry(symtab, layout, gsym);
2388       break;
2389
2390     case elfcpp::R_386_GOTOFF:
2391     case elfcpp::R_386_GOTPC:
2392       // We need a GOT section.
2393       target->got_section(symtab, layout);
2394       break;
2395
2396       // These are relocations which should only be seen by the
2397       // dynamic linker, and should never be seen here.
2398     case elfcpp::R_386_COPY:
2399     case elfcpp::R_386_GLOB_DAT:
2400     case elfcpp::R_386_JUMP_SLOT:
2401     case elfcpp::R_386_RELATIVE:
2402     case elfcpp::R_386_IRELATIVE:
2403     case elfcpp::R_386_TLS_TPOFF:
2404     case elfcpp::R_386_TLS_DTPMOD32:
2405     case elfcpp::R_386_TLS_DTPOFF32:
2406     case elfcpp::R_386_TLS_TPOFF32:
2407     case elfcpp::R_386_TLS_DESC:
2408       gold_error(_("%s: unexpected reloc %u in object file"),
2409                  object->name().c_str(), r_type);
2410       break;
2411
2412       // These are initial tls relocs, which are expected when
2413       // linking.
2414     case elfcpp::R_386_TLS_GD:            // Global-dynamic
2415     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
2416     case elfcpp::R_386_TLS_DESC_CALL:
2417     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
2418     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
2419     case elfcpp::R_386_TLS_IE:            // Initial-exec
2420     case elfcpp::R_386_TLS_IE_32:
2421     case elfcpp::R_386_TLS_GOTIE:
2422     case elfcpp::R_386_TLS_LE:            // Local-exec
2423     case elfcpp::R_386_TLS_LE_32:
2424       {
2425         const bool is_final = gsym->final_value_is_known();
2426         const tls::Tls_optimization optimized_type
2427             = Target_i386::optimize_tls_reloc(is_final, r_type);
2428         switch (r_type)
2429           {
2430           case elfcpp::R_386_TLS_GD:          // Global-dynamic
2431             if (optimized_type == tls::TLSOPT_NONE)
2432               {
2433                 // Create a pair of GOT entries for the module index and
2434                 // dtv-relative offset.
2435                 Output_data_got<32, false>* got
2436                     = target->got_section(symtab, layout);
2437                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2438                                              target->rel_dyn_section(layout),
2439                                              elfcpp::R_386_TLS_DTPMOD32,
2440                                              elfcpp::R_386_TLS_DTPOFF32);
2441               }
2442             else if (optimized_type == tls::TLSOPT_TO_IE)
2443               {
2444                 // Create a GOT entry for the tp-relative offset.
2445                 Output_data_got<32, false>* got
2446                     = target->got_section(symtab, layout);
2447                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2448                                          target->rel_dyn_section(layout),
2449                                          elfcpp::R_386_TLS_TPOFF);
2450               }
2451             else if (optimized_type != tls::TLSOPT_TO_LE)
2452               unsupported_reloc_global(object, r_type, gsym);
2453             break;
2454
2455           case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (~oliva url)
2456             target->define_tls_base_symbol(symtab, layout);
2457             if (optimized_type == tls::TLSOPT_NONE)
2458               {
2459                 // Create a double GOT entry with an R_386_TLS_DESC
2460                 // reloc.  The R_386_TLS_DESC reloc is resolved
2461                 // lazily, so the GOT entry needs to be in an area in
2462                 // .got.plt, not .got.  Call got_section to make sure
2463                 // the section has been created.
2464                 target->got_section(symtab, layout);
2465                 Output_data_got<32, false>* got = target->got_tlsdesc_section();
2466                 Reloc_section* rt = target->rel_tls_desc_section(layout);
2467                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2468                                              elfcpp::R_386_TLS_DESC, 0);
2469               }
2470             else if (optimized_type == tls::TLSOPT_TO_IE)
2471               {
2472                 // Create a GOT entry for the tp-relative offset.
2473                 Output_data_got<32, false>* got
2474                     = target->got_section(symtab, layout);
2475                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2476                                          target->rel_dyn_section(layout),
2477                                          elfcpp::R_386_TLS_TPOFF);
2478               }
2479             else if (optimized_type != tls::TLSOPT_TO_LE)
2480               unsupported_reloc_global(object, r_type, gsym);
2481             break;
2482
2483           case elfcpp::R_386_TLS_DESC_CALL:
2484             break;
2485
2486           case elfcpp::R_386_TLS_LDM:         // Local-dynamic
2487             if (optimized_type == tls::TLSOPT_NONE)
2488               {
2489                 // Create a GOT entry for the module index.
2490                 target->got_mod_index_entry(symtab, layout, object);
2491               }
2492             else if (optimized_type != tls::TLSOPT_TO_LE)
2493               unsupported_reloc_global(object, r_type, gsym);
2494             break;
2495
2496           case elfcpp::R_386_TLS_LDO_32:      // Alternate local-dynamic
2497             break;
2498
2499           case elfcpp::R_386_TLS_IE:          // Initial-exec
2500           case elfcpp::R_386_TLS_IE_32:
2501           case elfcpp::R_386_TLS_GOTIE:
2502             layout->set_has_static_tls();
2503             if (optimized_type == tls::TLSOPT_NONE)
2504               {
2505                 // For the R_386_TLS_IE relocation, we need to create a
2506                 // dynamic relocation when building a shared library.
2507                 if (r_type == elfcpp::R_386_TLS_IE
2508                     && parameters->options().shared())
2509                   {
2510                     Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2511                     rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2512                                                  output_section, object,
2513                                                  data_shndx,
2514                                                  reloc.get_r_offset());
2515                   }
2516                 // Create a GOT entry for the tp-relative offset.
2517                 Output_data_got<32, false>* got
2518                     = target->got_section(symtab, layout);
2519                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2520                                            ? elfcpp::R_386_TLS_TPOFF32
2521                                            : elfcpp::R_386_TLS_TPOFF);
2522                 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2523                                          ? GOT_TYPE_TLS_OFFSET
2524                                          : GOT_TYPE_TLS_NOFFSET);
2525                 got->add_global_with_rel(gsym, got_type,
2526                                          target->rel_dyn_section(layout),
2527                                          dyn_r_type);
2528               }
2529             else if (optimized_type != tls::TLSOPT_TO_LE)
2530               unsupported_reloc_global(object, r_type, gsym);
2531             break;
2532
2533           case elfcpp::R_386_TLS_LE:          // Local-exec
2534           case elfcpp::R_386_TLS_LE_32:
2535             layout->set_has_static_tls();
2536             if (parameters->options().shared())
2537               {
2538                 // We need to create a dynamic relocation.
2539                 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2540                                            ? elfcpp::R_386_TLS_TPOFF32
2541                                            : elfcpp::R_386_TLS_TPOFF);
2542                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2543                 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2544                                     data_shndx, reloc.get_r_offset());
2545               }
2546             break;
2547
2548           default:
2549             gold_unreachable();
2550           }
2551       }
2552       break;
2553
2554     case elfcpp::R_386_32PLT:
2555     case elfcpp::R_386_TLS_GD_32:
2556     case elfcpp::R_386_TLS_GD_PUSH:
2557     case elfcpp::R_386_TLS_GD_CALL:
2558     case elfcpp::R_386_TLS_GD_POP:
2559     case elfcpp::R_386_TLS_LDM_32:
2560     case elfcpp::R_386_TLS_LDM_PUSH:
2561     case elfcpp::R_386_TLS_LDM_CALL:
2562     case elfcpp::R_386_TLS_LDM_POP:
2563     case elfcpp::R_386_USED_BY_INTEL_200:
2564     default:
2565       unsupported_reloc_global(object, r_type, gsym);
2566       break;
2567     }
2568 }
2569
2570 // Process relocations for gc.
2571
2572 void
2573 Target_i386::gc_process_relocs(Symbol_table* symtab,
2574                                       Layout* layout,
2575                                       Sized_relobj_file<32, false>* object,
2576                                       unsigned int data_shndx,
2577                                       unsigned int,
2578                                       const unsigned char* prelocs,
2579                                       size_t reloc_count,
2580                                       Output_section* output_section,
2581                                       bool needs_special_offset_handling,
2582                                       size_t local_symbol_count,
2583                                       const unsigned char* plocal_symbols)
2584 {
2585   gold::gc_process_relocs<32, false, Target_i386, Scan, Classify_reloc>(
2586     symtab,
2587     layout,
2588     this,
2589     object,
2590     data_shndx,
2591     prelocs,
2592     reloc_count,
2593     output_section,
2594     needs_special_offset_handling,
2595     local_symbol_count,
2596     plocal_symbols);
2597 }
2598
2599 // Scan relocations for a section.
2600
2601 void
2602 Target_i386::scan_relocs(Symbol_table* symtab,
2603                                 Layout* layout,
2604                                 Sized_relobj_file<32, false>* object,
2605                                 unsigned int data_shndx,
2606                                 unsigned int sh_type,
2607                                 const unsigned char* prelocs,
2608                                 size_t reloc_count,
2609                                 Output_section* output_section,
2610                                 bool needs_special_offset_handling,
2611                                 size_t local_symbol_count,
2612                                 const unsigned char* plocal_symbols)
2613 {
2614   if (sh_type == elfcpp::SHT_RELA)
2615     {
2616       gold_error(_("%s: unsupported RELA reloc section"),
2617                  object->name().c_str());
2618       return;
2619     }
2620
2621   gold::scan_relocs<32, false, Target_i386, Scan, Classify_reloc>(
2622     symtab,
2623     layout,
2624     this,
2625     object,
2626     data_shndx,
2627     prelocs,
2628     reloc_count,
2629     output_section,
2630     needs_special_offset_handling,
2631     local_symbol_count,
2632     plocal_symbols);
2633 }
2634
2635 // Finalize the sections.
2636
2637 void
2638 Target_i386::do_finalize_sections(
2639     Layout* layout,
2640     const Input_objects*,
2641     Symbol_table* symtab)
2642 {
2643   const Reloc_section* rel_plt = (this->plt_ == NULL
2644                                   ? NULL
2645                                   : this->plt_->rel_plt());
2646   layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2647                                   this->rel_dyn_, true, false);
2648
2649   // Emit any relocs we saved in an attempt to avoid generating COPY
2650   // relocs.
2651   if (this->copy_relocs_.any_saved_relocs())
2652     this->copy_relocs_.emit(this->rel_dyn_section(layout));
2653
2654   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2655   // the .got.plt section.
2656   Symbol* sym = this->global_offset_table_;
2657   if (sym != NULL)
2658     {
2659       uint32_t data_size = this->got_plt_->current_data_size();
2660       symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2661     }
2662
2663   if (parameters->doing_static_link()
2664       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2665     {
2666       // If linking statically, make sure that the __rel_iplt symbols
2667       // were defined if necessary, even if we didn't create a PLT.
2668       static const Define_symbol_in_segment syms[] =
2669         {
2670           {
2671             "__rel_iplt_start",         // name
2672             elfcpp::PT_LOAD,            // segment_type
2673             elfcpp::PF_W,               // segment_flags_set
2674             elfcpp::PF(0),              // segment_flags_clear
2675             0,                          // value
2676             0,                          // size
2677             elfcpp::STT_NOTYPE,         // type
2678             elfcpp::STB_GLOBAL,         // binding
2679             elfcpp::STV_HIDDEN,         // visibility
2680             0,                          // nonvis
2681             Symbol::SEGMENT_START,      // offset_from_base
2682             true                        // only_if_ref
2683           },
2684           {
2685             "__rel_iplt_end",           // name
2686             elfcpp::PT_LOAD,            // segment_type
2687             elfcpp::PF_W,               // segment_flags_set
2688             elfcpp::PF(0),              // segment_flags_clear
2689             0,                          // value
2690             0,                          // size
2691             elfcpp::STT_NOTYPE,         // type
2692             elfcpp::STB_GLOBAL,         // binding
2693             elfcpp::STV_HIDDEN,         // visibility
2694             0,                          // nonvis
2695             Symbol::SEGMENT_START,      // offset_from_base
2696             true                        // only_if_ref
2697           }
2698         };
2699
2700       symtab->define_symbols(layout, 2, syms,
2701                              layout->script_options()->saw_sections_clause());
2702     }
2703 }
2704
2705 // Return whether a direct absolute static relocation needs to be applied.
2706 // In cases where Scan::local() or Scan::global() has created
2707 // a dynamic relocation other than R_386_RELATIVE, the addend
2708 // of the relocation is carried in the data, and we must not
2709 // apply the static relocation.
2710
2711 inline bool
2712 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2713                                                  unsigned int r_type,
2714                                                  bool is_32bit,
2715                                                  Output_section* output_section)
2716 {
2717   // If the output section is not allocated, then we didn't call
2718   // scan_relocs, we didn't create a dynamic reloc, and we must apply
2719   // the reloc here.
2720   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2721     return true;
2722
2723   int ref_flags = Scan::get_reference_flags(r_type);
2724
2725   // For local symbols, we will have created a non-RELATIVE dynamic
2726   // relocation only if (a) the output is position independent,
2727   // (b) the relocation is absolute (not pc- or segment-relative), and
2728   // (c) the relocation is not 32 bits wide.
2729   if (gsym == NULL)
2730     return !(parameters->options().output_is_position_independent()
2731              && (ref_flags & Symbol::ABSOLUTE_REF)
2732              && !is_32bit);
2733
2734   // For global symbols, we use the same helper routines used in the
2735   // scan pass.  If we did not create a dynamic relocation, or if we
2736   // created a RELATIVE dynamic relocation, we should apply the static
2737   // relocation.
2738   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2739   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2740                 && gsym->can_use_relative_reloc(ref_flags
2741                                                 & Symbol::FUNCTION_CALL);
2742   return !has_dyn || is_rel;
2743 }
2744
2745 // Perform a relocation.
2746
2747 inline bool
2748 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2749                                 unsigned int,
2750                                 Target_i386* target,
2751                                 Output_section* output_section,
2752                                 size_t relnum,
2753                                 const unsigned char* preloc,
2754                                 const Sized_symbol<32>* gsym,
2755                                 const Symbol_value<32>* psymval,
2756                                 unsigned char* view,
2757                                 elfcpp::Elf_types<32>::Elf_Addr address,
2758                                 section_size_type view_size)
2759 {
2760   const elfcpp::Rel<32, false> rel(preloc);
2761   unsigned int r_type = elfcpp::elf_r_type<32>(rel.get_r_info());
2762
2763   if (this->skip_call_tls_get_addr_)
2764     {
2765       if ((r_type != elfcpp::R_386_PLT32
2766            && r_type != elfcpp::R_386_PC32)
2767           || gsym == NULL
2768           || strcmp(gsym->name(), "___tls_get_addr") != 0)
2769         gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2770                                _("missing expected TLS relocation"));
2771       else
2772         {
2773           this->skip_call_tls_get_addr_ = false;
2774           return false;
2775         }
2776     }
2777
2778   if (view == NULL)
2779     return true;
2780
2781   const Sized_relobj_file<32, false>* object = relinfo->object;
2782
2783   // Pick the value to use for symbols defined in shared objects.
2784   Symbol_value<32> symval;
2785   if (gsym != NULL
2786       && gsym->type() == elfcpp::STT_GNU_IFUNC
2787       && r_type == elfcpp::R_386_32
2788       && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2789       && gsym->can_use_relative_reloc(false)
2790       && !gsym->is_from_dynobj()
2791       && !gsym->is_undefined()
2792       && !gsym->is_preemptible())
2793     {
2794       // In this case we are generating a R_386_IRELATIVE reloc.  We
2795       // want to use the real value of the symbol, not the PLT offset.
2796     }
2797   else if (gsym != NULL
2798            && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2799     {
2800       symval.set_output_value(target->plt_address_for_global(gsym));
2801       psymval = &symval;
2802     }
2803   else if (gsym == NULL && psymval->is_ifunc_symbol())
2804     {
2805       unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2806       if (object->local_has_plt_offset(r_sym))
2807         {
2808           symval.set_output_value(target->plt_address_for_local(object, r_sym));
2809           psymval = &symval;
2810         }
2811     }
2812
2813   bool baseless;
2814
2815   switch (r_type)
2816     {
2817     case elfcpp::R_386_NONE:
2818     case elfcpp::R_386_GNU_VTINHERIT:
2819     case elfcpp::R_386_GNU_VTENTRY:
2820       break;
2821
2822     case elfcpp::R_386_32:
2823       if (should_apply_static_reloc(gsym, r_type, true, output_section))
2824         Relocate_functions<32, false>::rel32(view, object, psymval);
2825       break;
2826
2827     case elfcpp::R_386_PC32:
2828       if (should_apply_static_reloc(gsym, r_type, true, output_section))
2829         Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2830       break;
2831
2832     case elfcpp::R_386_16:
2833       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2834         Relocate_functions<32, false>::rel16(view, object, psymval);
2835       break;
2836
2837     case elfcpp::R_386_PC16:
2838       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2839         Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2840       break;
2841
2842     case elfcpp::R_386_8:
2843       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2844         Relocate_functions<32, false>::rel8(view, object, psymval);
2845       break;
2846
2847     case elfcpp::R_386_PC8:
2848       if (should_apply_static_reloc(gsym, r_type, false, output_section))
2849         Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2850       break;
2851
2852     case elfcpp::R_386_PLT32:
2853       gold_assert(gsym == NULL
2854                   || gsym->has_plt_offset()
2855                   || gsym->final_value_is_known()
2856                   || (gsym->is_defined()
2857                       && !gsym->is_from_dynobj()
2858                       && !gsym->is_preemptible()));
2859       Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2860       break;
2861
2862     case elfcpp::R_386_GOT32:
2863     case elfcpp::R_386_GOT32X:
2864       baseless = (view[-1] & 0xc7) == 0x5;
2865       // R_386_GOT32 and R_386_GOT32X don't work without base register
2866       // when generating a position-independent output file.
2867       if (baseless
2868           && parameters->options().output_is_position_independent())
2869         {
2870           if(gsym)
2871             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2872                                    _("unexpected reloc %u against global symbol %s without base register in object file when generating a position-independent output file"),
2873                                    r_type, gsym->demangled_name().c_str());
2874           else
2875             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2876                                    _("unexpected reloc %u against local symbol without base register in object file when generating a position-independent output file"),
2877                                    r_type);
2878         }
2879
2880       // Convert
2881       // mov foo@GOT(%reg), %reg
2882       // to
2883       // lea foo@GOTOFF(%reg), %reg
2884       // if possible.
2885       if (rel.get_r_offset() >= 2
2886           && view[-2] == 0x8b
2887           && ((gsym == NULL && !psymval->is_ifunc_symbol())
2888               || (gsym != NULL
2889                   && Target_i386::can_convert_mov_to_lea(gsym))))
2890         {
2891           view[-2] = 0x8d;
2892           elfcpp::Elf_types<32>::Elf_Addr value;
2893           value = psymval->value(object, 0);
2894           // Don't subtract the .got.plt section address for baseless
2895           // addressing.
2896           if (!baseless)
2897             value -= target->got_plt_section()->address();
2898           Relocate_functions<32, false>::rel32(view, value);
2899         }
2900       else
2901         {
2902           // The GOT pointer points to the end of the GOT section.
2903           // We need to subtract the size of the GOT section to get
2904           // the actual offset to use in the relocation.
2905           unsigned int got_offset = 0;
2906           if (gsym != NULL)
2907             {
2908               gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2909               got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2910                             - target->got_size());
2911             }
2912           else
2913             {
2914               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2915               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2916               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2917                             - target->got_size());
2918             }
2919           // Add the .got.plt section address for baseless addressing.
2920           if (baseless)
2921             got_offset += target->got_plt_section()->address();
2922           Relocate_functions<32, false>::rel32(view, got_offset);
2923         }
2924       break;
2925
2926     case elfcpp::R_386_GOTOFF:
2927       {
2928         elfcpp::Elf_types<32>::Elf_Addr value;
2929         value = (psymval->value(object, 0)
2930                  - target->got_plt_section()->address());
2931         Relocate_functions<32, false>::rel32(view, value);
2932       }
2933       break;
2934
2935     case elfcpp::R_386_GOTPC:
2936       {
2937         elfcpp::Elf_types<32>::Elf_Addr value;
2938         value = target->got_plt_section()->address();
2939         Relocate_functions<32, false>::pcrel32(view, value, address);
2940       }
2941       break;
2942
2943     case elfcpp::R_386_COPY:
2944     case elfcpp::R_386_GLOB_DAT:
2945     case elfcpp::R_386_JUMP_SLOT:
2946     case elfcpp::R_386_RELATIVE:
2947     case elfcpp::R_386_IRELATIVE:
2948       // These are outstanding tls relocs, which are unexpected when
2949       // linking.
2950     case elfcpp::R_386_TLS_TPOFF:
2951     case elfcpp::R_386_TLS_DTPMOD32:
2952     case elfcpp::R_386_TLS_DTPOFF32:
2953     case elfcpp::R_386_TLS_TPOFF32:
2954     case elfcpp::R_386_TLS_DESC:
2955       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2956                              _("unexpected reloc %u in object file"),
2957                              r_type);
2958       break;
2959
2960       // These are initial tls relocs, which are expected when
2961       // linking.
2962     case elfcpp::R_386_TLS_GD:             // Global-dynamic
2963     case elfcpp::R_386_TLS_GOTDESC:        // Global-dynamic (from ~oliva url)
2964     case elfcpp::R_386_TLS_DESC_CALL:
2965     case elfcpp::R_386_TLS_LDM:            // Local-dynamic
2966     case elfcpp::R_386_TLS_LDO_32:         // Alternate local-dynamic
2967     case elfcpp::R_386_TLS_IE:             // Initial-exec
2968     case elfcpp::R_386_TLS_IE_32:
2969     case elfcpp::R_386_TLS_GOTIE:
2970     case elfcpp::R_386_TLS_LE:             // Local-exec
2971     case elfcpp::R_386_TLS_LE_32:
2972       this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2973                          view, address, view_size);
2974       break;
2975
2976     case elfcpp::R_386_32PLT:
2977     case elfcpp::R_386_TLS_GD_32:
2978     case elfcpp::R_386_TLS_GD_PUSH:
2979     case elfcpp::R_386_TLS_GD_CALL:
2980     case elfcpp::R_386_TLS_GD_POP:
2981     case elfcpp::R_386_TLS_LDM_32:
2982     case elfcpp::R_386_TLS_LDM_PUSH:
2983     case elfcpp::R_386_TLS_LDM_CALL:
2984     case elfcpp::R_386_TLS_LDM_POP:
2985     case elfcpp::R_386_USED_BY_INTEL_200:
2986     default:
2987       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2988                              _("unsupported reloc %u"),
2989                              r_type);
2990       break;
2991     }
2992
2993   return true;
2994 }
2995
2996 // Perform a TLS relocation.
2997
2998 inline void
2999 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
3000                                     Target_i386* target,
3001                                     size_t relnum,
3002                                     const elfcpp::Rel<32, false>& rel,
3003                                     unsigned int r_type,
3004                                     const Sized_symbol<32>* gsym,
3005                                     const Symbol_value<32>* psymval,
3006                                     unsigned char* view,
3007                                     elfcpp::Elf_types<32>::Elf_Addr,
3008                                     section_size_type view_size)
3009 {
3010   Output_segment* tls_segment = relinfo->layout->tls_segment();
3011
3012   const Sized_relobj_file<32, false>* object = relinfo->object;
3013
3014   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
3015
3016   const bool is_final = (gsym == NULL
3017                          ? !parameters->options().shared()
3018                          : gsym->final_value_is_known());
3019   const tls::Tls_optimization optimized_type
3020       = Target_i386::optimize_tls_reloc(is_final, r_type);
3021   switch (r_type)
3022     {
3023     case elfcpp::R_386_TLS_GD:           // Global-dynamic
3024       if (optimized_type == tls::TLSOPT_TO_LE)
3025         {
3026           if (tls_segment == NULL)
3027             {
3028               gold_assert(parameters->errors()->error_count() > 0
3029                           || issue_undefined_symbol_error(gsym));
3030               return;
3031             }
3032           this->tls_gd_to_le(relinfo, relnum, tls_segment,
3033                              rel, r_type, value, view,
3034                              view_size);
3035           break;
3036         }
3037       else
3038         {
3039           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3040                                    ? GOT_TYPE_TLS_NOFFSET
3041                                    : GOT_TYPE_TLS_PAIR);
3042           unsigned int got_offset;
3043           if (gsym != NULL)
3044             {
3045               gold_assert(gsym->has_got_offset(got_type));
3046               got_offset = gsym->got_offset(got_type) - target->got_size();
3047             }
3048           else
3049             {
3050               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3051               gold_assert(object->local_has_got_offset(r_sym, got_type));
3052               got_offset = (object->local_got_offset(r_sym, got_type)
3053                             - target->got_size());
3054             }
3055           if (optimized_type == tls::TLSOPT_TO_IE)
3056             {
3057               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
3058                                  got_offset, view, view_size);
3059               break;
3060             }
3061           else if (optimized_type == tls::TLSOPT_NONE)
3062             {
3063               // Relocate the field with the offset of the pair of GOT
3064               // entries.
3065               Relocate_functions<32, false>::rel32(view, got_offset);
3066               break;
3067             }
3068         }
3069       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3070                              _("unsupported reloc %u"),
3071                              r_type);
3072       break;
3073
3074     case elfcpp::R_386_TLS_GOTDESC:      // Global-dynamic (from ~oliva url)
3075     case elfcpp::R_386_TLS_DESC_CALL:
3076       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
3077       if (optimized_type == tls::TLSOPT_TO_LE)
3078         {
3079           if (tls_segment == NULL)
3080             {
3081               gold_assert(parameters->errors()->error_count() > 0
3082                           || issue_undefined_symbol_error(gsym));
3083               return;
3084             }
3085           this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
3086                                   rel, r_type, value, view,
3087                                   view_size);
3088           break;
3089         }
3090       else
3091         {
3092           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3093                                    ? GOT_TYPE_TLS_NOFFSET
3094                                    : GOT_TYPE_TLS_DESC);
3095           unsigned int got_offset = 0;
3096           if (r_type == elfcpp::R_386_TLS_GOTDESC
3097               && optimized_type == tls::TLSOPT_NONE)
3098             {
3099               // We created GOT entries in the .got.tlsdesc portion of
3100               // the .got.plt section, but the offset stored in the
3101               // symbol is the offset within .got.tlsdesc.
3102               got_offset = (target->got_size()
3103                             + target->got_plt_section()->data_size());
3104             }
3105           if (gsym != NULL)
3106             {
3107               gold_assert(gsym->has_got_offset(got_type));
3108               got_offset += gsym->got_offset(got_type) - target->got_size();
3109             }
3110           else
3111             {
3112               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3113               gold_assert(object->local_has_got_offset(r_sym, got_type));
3114               got_offset += (object->local_got_offset(r_sym, got_type)
3115                              - target->got_size());
3116             }
3117           if (optimized_type == tls::TLSOPT_TO_IE)
3118             {
3119               if (tls_segment == NULL)
3120                 {
3121                   gold_assert(parameters->errors()->error_count() > 0
3122                               || issue_undefined_symbol_error(gsym));
3123                   return;
3124                 }
3125               this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
3126                                       got_offset, view, view_size);
3127               break;
3128             }
3129           else if (optimized_type == tls::TLSOPT_NONE)
3130             {
3131               if (r_type == elfcpp::R_386_TLS_GOTDESC)
3132                 {
3133                   // Relocate the field with the offset of the pair of GOT
3134                   // entries.
3135                   Relocate_functions<32, false>::rel32(view, got_offset);
3136                 }
3137               break;
3138             }
3139         }
3140       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3141                              _("unsupported reloc %u"),
3142                              r_type);
3143       break;
3144
3145     case elfcpp::R_386_TLS_LDM:          // Local-dynamic
3146       if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
3147         {
3148           gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3149                                  _("both SUN and GNU model "
3150                                    "TLS relocations"));
3151           break;
3152         }
3153       this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
3154       if (optimized_type == tls::TLSOPT_TO_LE)
3155         {
3156           if (tls_segment == NULL)
3157             {
3158               gold_assert(parameters->errors()->error_count() > 0
3159                           || issue_undefined_symbol_error(gsym));
3160               return;
3161             }
3162           this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
3163                              value, view, view_size);
3164           break;
3165         }
3166       else if (optimized_type == tls::TLSOPT_NONE)
3167         {
3168           // Relocate the field with the offset of the GOT entry for
3169           // the module index.
3170           unsigned int got_offset;
3171           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
3172                         - target->got_size());
3173           Relocate_functions<32, false>::rel32(view, got_offset);
3174           break;
3175         }
3176       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3177                              _("unsupported reloc %u"),
3178                              r_type);
3179       break;
3180
3181     case elfcpp::R_386_TLS_LDO_32:       // Alternate local-dynamic
3182       if (optimized_type == tls::TLSOPT_TO_LE)
3183         {
3184           // This reloc can appear in debugging sections, in which
3185           // case we must not convert to local-exec.  We decide what
3186           // to do based on whether the section is marked as
3187           // containing executable code.  That is what the GNU linker
3188           // does as well.
3189           elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
3190           if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
3191             {
3192               if (tls_segment == NULL)
3193                 {
3194                   gold_assert(parameters->errors()->error_count() > 0
3195                               || issue_undefined_symbol_error(gsym));
3196                   return;
3197                 }
3198               value -= tls_segment->memsz();
3199             }
3200         }
3201       Relocate_functions<32, false>::rel32(view, value);
3202       break;
3203
3204     case elfcpp::R_386_TLS_IE:           // Initial-exec
3205     case elfcpp::R_386_TLS_GOTIE:
3206     case elfcpp::R_386_TLS_IE_32:
3207       if (optimized_type == tls::TLSOPT_TO_LE)
3208         {
3209           if (tls_segment == NULL)
3210             {
3211               gold_assert(parameters->errors()->error_count() > 0
3212                           || issue_undefined_symbol_error(gsym));
3213               return;
3214             }
3215           Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3216                                               rel, r_type, value, view,
3217                                               view_size);
3218           break;
3219         }
3220       else if (optimized_type == tls::TLSOPT_NONE)
3221         {
3222           // Relocate the field with the offset of the GOT entry for
3223           // the tp-relative offset of the symbol.
3224           unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
3225                                    ? GOT_TYPE_TLS_OFFSET
3226                                    : GOT_TYPE_TLS_NOFFSET);
3227           unsigned int got_offset;
3228           if (gsym != NULL)
3229             {
3230               gold_assert(gsym->has_got_offset(got_type));
3231               got_offset = gsym->got_offset(got_type);
3232             }
3233           else
3234             {
3235               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
3236               gold_assert(object->local_has_got_offset(r_sym, got_type));
3237               got_offset = object->local_got_offset(r_sym, got_type);
3238             }
3239           // For the R_386_TLS_IE relocation, we need to apply the
3240           // absolute address of the GOT entry.
3241           if (r_type == elfcpp::R_386_TLS_IE)
3242             got_offset += target->got_plt_section()->address();
3243           // All GOT offsets are relative to the end of the GOT.
3244           got_offset -= target->got_size();
3245           Relocate_functions<32, false>::rel32(view, got_offset);
3246           break;
3247         }
3248       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
3249                              _("unsupported reloc %u"),
3250                              r_type);
3251       break;
3252
3253     case elfcpp::R_386_TLS_LE:           // Local-exec
3254       // If we're creating a shared library, a dynamic relocation will
3255       // have been created for this location, so do not apply it now.
3256       if (!parameters->options().shared())
3257         {
3258           if (tls_segment == NULL)
3259             {
3260               gold_assert(parameters->errors()->error_count() > 0
3261                           || issue_undefined_symbol_error(gsym));
3262               return;
3263             }
3264           value -= tls_segment->memsz();
3265           Relocate_functions<32, false>::rel32(view, value);
3266         }
3267       break;
3268
3269     case elfcpp::R_386_TLS_LE_32:
3270       // If we're creating a shared library, a dynamic relocation will
3271       // have been created for this location, so do not apply it now.
3272       if (!parameters->options().shared())
3273         {
3274           if (tls_segment == NULL)
3275             {
3276               gold_assert(parameters->errors()->error_count() > 0
3277                           || issue_undefined_symbol_error(gsym));
3278               return;
3279             }
3280           value = tls_segment->memsz() - value;
3281           Relocate_functions<32, false>::rel32(view, value);
3282         }
3283       break;
3284     }
3285 }
3286
3287 // Do a relocation in which we convert a TLS General-Dynamic to a
3288 // Local-Exec.
3289
3290 inline void
3291 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
3292                                     size_t relnum,
3293                                     Output_segment* tls_segment,
3294                                     const elfcpp::Rel<32, false>& rel,
3295                                     unsigned int,
3296                                     elfcpp::Elf_types<32>::Elf_Addr value,
3297                                     unsigned char* view,
3298                                     section_size_type view_size)
3299 {
3300   // leal foo(,%reg,1),%eax; call ___tls_get_addr
3301   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3302   // leal foo(%reg),%eax; call ___tls_get_addr
3303   //  ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
3304
3305   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3306   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3307
3308   unsigned char op1 = view[-1];
3309   unsigned char op2 = view[-2];
3310
3311   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3312                  op2 == 0x8d || op2 == 0x04);
3313   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3314
3315   int roff = 5;
3316
3317   if (op2 == 0x04)
3318     {
3319       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3320       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3321       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3322                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3323       memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3324     }
3325   else
3326     {
3327       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3328                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3329       if (rel.get_r_offset() + 9 < view_size
3330           && view[9] == 0x90)
3331         {
3332           // There is a trailing nop.  Use the size byte subl.
3333           memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3334           roff = 6;
3335         }
3336       else
3337         {
3338           // Use the five byte subl.
3339           memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3340         }
3341     }
3342
3343   value = tls_segment->memsz() - value;
3344   Relocate_functions<32, false>::rel32(view + roff, value);
3345
3346   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3347   // We can skip it.
3348   this->skip_call_tls_get_addr_ = true;
3349 }
3350
3351 // Do a relocation in which we convert a TLS General-Dynamic to an
3352 // Initial-Exec.
3353
3354 inline void
3355 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3356                                     size_t relnum,
3357                                     Output_segment*,
3358                                     const elfcpp::Rel<32, false>& rel,
3359                                     unsigned int,
3360                                     elfcpp::Elf_types<32>::Elf_Addr value,
3361                                     unsigned char* view,
3362                                     section_size_type view_size)
3363 {
3364   // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3365   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3366   // leal foo(%ebx),%eax; call ___tls_get_addr; nop
3367   //  ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3368
3369   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3370   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3371
3372   unsigned char op1 = view[-1];
3373   unsigned char op2 = view[-2];
3374
3375   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3376                  op2 == 0x8d || op2 == 0x04);
3377   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3378
3379   int roff;
3380
3381   if (op2 == 0x04)
3382     {
3383       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3384       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3385       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3386                      ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3387       roff = 5;
3388     }
3389   else
3390     {
3391       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 10);
3392       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3393                      (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3394       tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[9] == 0x90);
3395       roff = 6;
3396     }
3397
3398   memcpy(view + roff - 8, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3399   Relocate_functions<32, false>::rel32(view + roff, value);
3400
3401   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3402   // We can skip it.
3403   this->skip_call_tls_get_addr_ = true;
3404 }
3405
3406 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3407 // General-Dynamic to a Local-Exec.
3408
3409 inline void
3410 Target_i386::Relocate::tls_desc_gd_to_le(
3411     const Relocate_info<32, false>* relinfo,
3412     size_t relnum,
3413     Output_segment* tls_segment,
3414     const elfcpp::Rel<32, false>& rel,
3415     unsigned int r_type,
3416     elfcpp::Elf_types<32>::Elf_Addr value,
3417     unsigned char* view,
3418     section_size_type view_size)
3419 {
3420   if (r_type == elfcpp::R_386_TLS_GOTDESC)
3421     {
3422       // leal foo@TLSDESC(%ebx), %eax
3423       // ==> leal foo@NTPOFF, %eax
3424       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3425       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3426       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3427                      view[-2] == 0x8d && view[-1] == 0x83);
3428       view[-1] = 0x05;
3429       value -= tls_segment->memsz();
3430       Relocate_functions<32, false>::rel32(view, value);
3431     }
3432   else
3433     {
3434       // call *foo@TLSCALL(%eax)
3435       // ==> nop; nop
3436       gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3437       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3438       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3439                      view[0] == 0xff && view[1] == 0x10);
3440       view[0] = 0x66;
3441       view[1] = 0x90;
3442     }
3443 }
3444
3445 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3446 // General-Dynamic to an Initial-Exec.
3447
3448 inline void
3449 Target_i386::Relocate::tls_desc_gd_to_ie(
3450     const Relocate_info<32, false>* relinfo,
3451     size_t relnum,
3452     Output_segment*,
3453     const elfcpp::Rel<32, false>& rel,
3454     unsigned int r_type,
3455     elfcpp::Elf_types<32>::Elf_Addr value,
3456     unsigned char* view,
3457     section_size_type view_size)
3458 {
3459   if (r_type == elfcpp::R_386_TLS_GOTDESC)
3460     {
3461       // leal foo@TLSDESC(%ebx), %eax
3462       // ==> movl foo@GOTNTPOFF(%ebx), %eax
3463       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3464       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3465       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3466                      view[-2] == 0x8d && view[-1] == 0x83);
3467       view[-2] = 0x8b;
3468       Relocate_functions<32, false>::rel32(view, value);
3469     }
3470   else
3471     {
3472       // call *foo@TLSCALL(%eax)
3473       // ==> nop; nop
3474       gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3475       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3476       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3477                      view[0] == 0xff && view[1] == 0x10);
3478       view[0] = 0x66;
3479       view[1] = 0x90;
3480     }
3481 }
3482
3483 // Do a relocation in which we convert a TLS Local-Dynamic to a
3484 // Local-Exec.
3485
3486 inline void
3487 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3488                                     size_t relnum,
3489                                     Output_segment*,
3490                                     const elfcpp::Rel<32, false>& rel,
3491                                     unsigned int,
3492                                     elfcpp::Elf_types<32>::Elf_Addr,
3493                                     unsigned char* view,
3494                                     section_size_type view_size)
3495 {
3496   // leal foo(%reg), %eax; call ___tls_get_addr
3497   // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3498
3499   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3500   tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3501
3502   // FIXME: Does this test really always pass?
3503   tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3504                  view[-2] == 0x8d && view[-1] == 0x83);
3505
3506   tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3507
3508   memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3509
3510   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3511   // We can skip it.
3512   this->skip_call_tls_get_addr_ = true;
3513 }
3514
3515 // Do a relocation in which we convert a TLS Initial-Exec to a
3516 // Local-Exec.
3517
3518 inline void
3519 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3520                                     size_t relnum,
3521                                     Output_segment* tls_segment,
3522                                     const elfcpp::Rel<32, false>& rel,
3523                                     unsigned int r_type,
3524                                     elfcpp::Elf_types<32>::Elf_Addr value,
3525                                     unsigned char* view,
3526                                     section_size_type view_size)
3527 {
3528   // We have to actually change the instructions, which means that we
3529   // need to examine the opcodes to figure out which instruction we
3530   // are looking at.
3531   if (r_type == elfcpp::R_386_TLS_IE)
3532     {
3533       // movl %gs:XX,%eax  ==>  movl $YY,%eax
3534       // movl %gs:XX,%reg  ==>  movl $YY,%reg
3535       // addl %gs:XX,%reg  ==>  addl $YY,%reg
3536       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3537       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3538
3539       unsigned char op1 = view[-1];
3540       if (op1 == 0xa1)
3541         {
3542           // movl XX,%eax  ==>  movl $YY,%eax
3543           view[-1] = 0xb8;
3544         }
3545       else
3546         {
3547           tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3548
3549           unsigned char op2 = view[-2];
3550           if (op2 == 0x8b)
3551             {
3552               // movl XX,%reg  ==>  movl $YY,%reg
3553               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3554                              (op1 & 0xc7) == 0x05);
3555               view[-2] = 0xc7;
3556               view[-1] = 0xc0 | ((op1 >> 3) & 7);
3557             }
3558           else if (op2 == 0x03)
3559             {
3560               // addl XX,%reg  ==>  addl $YY,%reg
3561               tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3562                              (op1 & 0xc7) == 0x05);
3563               view[-2] = 0x81;
3564               view[-1] = 0xc0 | ((op1 >> 3) & 7);
3565             }
3566           else
3567             tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3568         }
3569     }
3570   else
3571     {
3572       // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
3573       // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
3574       // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
3575       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3576       tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3577
3578       unsigned char op1 = view[-1];
3579       unsigned char op2 = view[-2];
3580       tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3581                      (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3582       if (op2 == 0x8b)
3583         {
3584           // movl %gs:XX(%reg1),%reg2  ==>  movl $YY,%reg2
3585           view[-2] = 0xc7;
3586           view[-1] = 0xc0 | ((op1 >> 3) & 7);
3587         }
3588       else if (op2 == 0x2b)
3589         {
3590           // subl %gs:XX(%reg1),%reg2  ==>  subl $YY,%reg2
3591           view[-2] = 0x81;
3592           view[-1] = 0xe8 | ((op1 >> 3) & 7);
3593         }
3594       else if (op2 == 0x03)
3595         {
3596           // addl %gs:XX(%reg1),%reg2  ==>  addl $YY,$reg2
3597           view[-2] = 0x81;
3598           view[-1] = 0xc0 | ((op1 >> 3) & 7);
3599         }
3600       else
3601         tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3602     }
3603
3604   value = tls_segment->memsz() - value;
3605   if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3606     value = - value;
3607
3608   Relocate_functions<32, false>::rel32(view, value);
3609 }
3610
3611 // Relocate section data.
3612
3613 void
3614 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3615                               unsigned int sh_type,
3616                               const unsigned char* prelocs,
3617                               size_t reloc_count,
3618                               Output_section* output_section,
3619                               bool needs_special_offset_handling,
3620                               unsigned char* view,
3621                               elfcpp::Elf_types<32>::Elf_Addr address,
3622                               section_size_type view_size,
3623                               const Reloc_symbol_changes* reloc_symbol_changes)
3624 {
3625   gold_assert(sh_type == elfcpp::SHT_REL);
3626
3627   gold::relocate_section<32, false, Target_i386, Relocate,
3628                          gold::Default_comdat_behavior, Classify_reloc>(
3629     relinfo,
3630     this,
3631     prelocs,
3632     reloc_count,
3633     output_section,
3634     needs_special_offset_handling,
3635     view,
3636     address,
3637     view_size,
3638     reloc_symbol_changes);
3639 }
3640
3641 // Return the size of a relocation while scanning during a relocatable
3642 // link.
3643
3644 unsigned int
3645 Target_i386::Classify_reloc::get_size_for_reloc(
3646     unsigned int r_type,
3647     Relobj* object)
3648 {
3649   switch (r_type)
3650     {
3651     case elfcpp::R_386_NONE:
3652     case elfcpp::R_386_GNU_VTINHERIT:
3653     case elfcpp::R_386_GNU_VTENTRY:
3654     case elfcpp::R_386_TLS_GD:            // Global-dynamic
3655     case elfcpp::R_386_TLS_GOTDESC:       // Global-dynamic (from ~oliva url)
3656     case elfcpp::R_386_TLS_DESC_CALL:
3657     case elfcpp::R_386_TLS_LDM:           // Local-dynamic
3658     case elfcpp::R_386_TLS_LDO_32:        // Alternate local-dynamic
3659     case elfcpp::R_386_TLS_IE:            // Initial-exec
3660     case elfcpp::R_386_TLS_IE_32:
3661     case elfcpp::R_386_TLS_GOTIE:
3662     case elfcpp::R_386_TLS_LE:            // Local-exec
3663     case elfcpp::R_386_TLS_LE_32:
3664       return 0;
3665
3666     case elfcpp::R_386_32:
3667     case elfcpp::R_386_PC32:
3668     case elfcpp::R_386_GOT32:
3669     case elfcpp::R_386_GOT32X:
3670     case elfcpp::R_386_PLT32:
3671     case elfcpp::R_386_GOTOFF:
3672     case elfcpp::R_386_GOTPC:
3673      return 4;
3674
3675     case elfcpp::R_386_16:
3676     case elfcpp::R_386_PC16:
3677       return 2;
3678
3679     case elfcpp::R_386_8:
3680     case elfcpp::R_386_PC8:
3681       return 1;
3682
3683       // These are relocations which should only be seen by the
3684       // dynamic linker, and should never be seen here.
3685     case elfcpp::R_386_COPY:
3686     case elfcpp::R_386_GLOB_DAT:
3687     case elfcpp::R_386_JUMP_SLOT:
3688     case elfcpp::R_386_RELATIVE:
3689     case elfcpp::R_386_IRELATIVE:
3690     case elfcpp::R_386_TLS_TPOFF:
3691     case elfcpp::R_386_TLS_DTPMOD32:
3692     case elfcpp::R_386_TLS_DTPOFF32:
3693     case elfcpp::R_386_TLS_TPOFF32:
3694     case elfcpp::R_386_TLS_DESC:
3695       object->error(_("unexpected reloc %u in object file"), r_type);
3696       return 0;
3697
3698     case elfcpp::R_386_32PLT:
3699     case elfcpp::R_386_TLS_GD_32:
3700     case elfcpp::R_386_TLS_GD_PUSH:
3701     case elfcpp::R_386_TLS_GD_CALL:
3702     case elfcpp::R_386_TLS_GD_POP:
3703     case elfcpp::R_386_TLS_LDM_32:
3704     case elfcpp::R_386_TLS_LDM_PUSH:
3705     case elfcpp::R_386_TLS_LDM_CALL:
3706     case elfcpp::R_386_TLS_LDM_POP:
3707     case elfcpp::R_386_USED_BY_INTEL_200:
3708     default:
3709       object->error(_("unsupported reloc %u in object file"), r_type);
3710       return 0;
3711     }
3712 }
3713
3714 // Scan the relocs during a relocatable link.
3715
3716 void
3717 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3718                                      Layout* layout,
3719                                      Sized_relobj_file<32, false>* object,
3720                                      unsigned int data_shndx,
3721                                      unsigned int sh_type,
3722                                      const unsigned char* prelocs,
3723                                      size_t reloc_count,
3724                                      Output_section* output_section,
3725                                      bool needs_special_offset_handling,
3726                                      size_t local_symbol_count,
3727                                      const unsigned char* plocal_symbols,
3728                                      Relocatable_relocs* rr)
3729 {
3730   typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
3731       Scan_relocatable_relocs;
3732
3733   gold_assert(sh_type == elfcpp::SHT_REL);
3734
3735   gold::scan_relocatable_relocs<32, false, Scan_relocatable_relocs>(
3736     symtab,
3737     layout,
3738     object,
3739     data_shndx,
3740     prelocs,
3741     reloc_count,
3742     output_section,
3743     needs_special_offset_handling,
3744     local_symbol_count,
3745     plocal_symbols,
3746     rr);
3747 }
3748
3749 // Scan the relocs for --emit-relocs.
3750
3751 void
3752 Target_i386::emit_relocs_scan(Symbol_table* symtab,
3753                               Layout* layout,
3754                               Sized_relobj_file<32, false>* object,
3755                               unsigned int data_shndx,
3756                               unsigned int sh_type,
3757                               const unsigned char* prelocs,
3758                               size_t reloc_count,
3759                               Output_section* output_section,
3760                               bool needs_special_offset_handling,
3761                               size_t local_symbol_count,
3762                               const unsigned char* plocal_syms,
3763                               Relocatable_relocs* rr)
3764 {
3765   typedef gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
3766       Classify_reloc;
3767   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
3768       Emit_relocs_strategy;
3769
3770   gold_assert(sh_type == elfcpp::SHT_REL);
3771
3772   gold::scan_relocatable_relocs<32, false, Emit_relocs_strategy>(
3773     symtab,
3774     layout,
3775     object,
3776     data_shndx,
3777     prelocs,
3778     reloc_count,
3779     output_section,
3780     needs_special_offset_handling,
3781     local_symbol_count,
3782     plocal_syms,
3783     rr);
3784 }
3785
3786 // Emit relocations for a section.
3787
3788 void
3789 Target_i386::relocate_relocs(
3790     const Relocate_info<32, false>* relinfo,
3791     unsigned int sh_type,
3792     const unsigned char* prelocs,
3793     size_t reloc_count,
3794     Output_section* output_section,
3795     elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
3796     unsigned char* view,
3797     elfcpp::Elf_types<32>::Elf_Addr view_address,
3798     section_size_type view_size,
3799     unsigned char* reloc_view,
3800     section_size_type reloc_view_size)
3801 {
3802   gold_assert(sh_type == elfcpp::SHT_REL);
3803
3804   gold::relocate_relocs<32, false, Classify_reloc>(
3805     relinfo,
3806     prelocs,
3807     reloc_count,
3808     output_section,
3809     offset_in_output_section,
3810     view,
3811     view_address,
3812     view_size,
3813     reloc_view,
3814     reloc_view_size);
3815 }
3816
3817 // Return the value to use for a dynamic which requires special
3818 // treatment.  This is how we support equality comparisons of function
3819 // pointers across shared library boundaries, as described in the
3820 // processor specific ABI supplement.
3821
3822 uint64_t
3823 Target_i386::do_dynsym_value(const Symbol* gsym) const
3824 {
3825   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3826   return this->plt_address_for_global(gsym);
3827 }
3828
3829 // Return a string used to fill a code section with nops to take up
3830 // the specified length.
3831
3832 std::string
3833 Target_i386::do_code_fill(section_size_type length) const
3834 {
3835   if (length >= 16)
3836     {
3837       // Build a jmp instruction to skip over the bytes.
3838       unsigned char jmp[5];
3839       jmp[0] = 0xe9;
3840       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3841       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3842               + std::string(length - 5, static_cast<char>(0x90)));
3843     }
3844
3845   // Nop sequences of various lengths.
3846   const char nop1[1] = { '\x90' };                   // nop
3847   const char nop2[2] = { '\x66', '\x90' };           // xchg %ax %ax
3848   const char nop3[3] = { '\x8d', '\x76', '\x00' };   // leal 0(%esi),%esi
3849   const char nop4[4] = { '\x8d', '\x74', '\x26',     // leal 0(%esi,1),%esi
3850                          '\x00'};
3851   const char nop5[5] = { '\x90', '\x8d', '\x74',     // nop
3852                          '\x26', '\x00' };           // leal 0(%esi,1),%esi
3853   const char nop6[6] = { '\x8d', '\xb6', '\x00',     // leal 0L(%esi),%esi
3854                          '\x00', '\x00', '\x00' };
3855   const char nop7[7] = { '\x8d', '\xb4', '\x26',     // leal 0L(%esi,1),%esi
3856                          '\x00', '\x00', '\x00',
3857                          '\x00' };
3858   const char nop8[8] = { '\x90', '\x8d', '\xb4',     // nop
3859                          '\x26', '\x00', '\x00',     // leal 0L(%esi,1),%esi
3860                          '\x00', '\x00' };
3861   const char nop9[9] = { '\x89', '\xf6', '\x8d',     // movl %esi,%esi
3862                          '\xbc', '\x27', '\x00',     // leal 0L(%edi,1),%edi
3863                          '\x00', '\x00', '\x00' };
3864   const char nop10[10] = { '\x8d', '\x76', '\x00',   // leal 0(%esi),%esi
3865                            '\x8d', '\xbc', '\x27',   // leal 0L(%edi,1),%edi
3866                            '\x00', '\x00', '\x00',
3867                            '\x00' };
3868   const char nop11[11] = { '\x8d', '\x74', '\x26',   // leal 0(%esi,1),%esi
3869                            '\x00', '\x8d', '\xbc',   // leal 0L(%edi,1),%edi
3870                            '\x27', '\x00', '\x00',
3871                            '\x00', '\x00' };
3872   const char nop12[12] = { '\x8d', '\xb6', '\x00',   // leal 0L(%esi),%esi
3873                            '\x00', '\x00', '\x00',   // leal 0L(%edi),%edi
3874                            '\x8d', '\xbf', '\x00',
3875                            '\x00', '\x00', '\x00' };
3876   const char nop13[13] = { '\x8d', '\xb6', '\x00',   // leal 0L(%esi),%esi
3877                            '\x00', '\x00', '\x00',   // leal 0L(%edi,1),%edi
3878                            '\x8d', '\xbc', '\x27',
3879                            '\x00', '\x00', '\x00',
3880                            '\x00' };
3881   const char nop14[14] = { '\x8d', '\xb4', '\x26',   // leal 0L(%esi,1),%esi
3882                            '\x00', '\x00', '\x00',   // leal 0L(%edi,1),%edi
3883                            '\x00', '\x8d', '\xbc',
3884                            '\x27', '\x00', '\x00',
3885                            '\x00', '\x00' };
3886   const char nop15[15] = { '\xeb', '\x0d', '\x90',   // jmp .+15
3887                            '\x90', '\x90', '\x90',   // nop,nop,nop,...
3888                            '\x90', '\x90', '\x90',
3889                            '\x90', '\x90', '\x90',
3890                            '\x90', '\x90', '\x90' };
3891
3892   const char* nops[16] = {
3893     NULL,
3894     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3895     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3896   };
3897
3898   return std::string(nops[length], length);
3899 }
3900
3901 // Return the value to use for the base of a DW_EH_PE_datarel offset
3902 // in an FDE.  Solaris and SVR4 use DW_EH_PE_datarel because their
3903 // assembler can not write out the difference between two labels in
3904 // different sections, so instead of using a pc-relative value they
3905 // use an offset from the GOT.
3906
3907 uint64_t
3908 Target_i386::do_ehframe_datarel_base() const
3909 {
3910   gold_assert(this->global_offset_table_ != NULL);
3911   Symbol* sym = this->global_offset_table_;
3912   Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3913   return ssym->value();
3914 }
3915
3916 // Return whether SYM should be treated as a call to a non-split
3917 // function.  We don't want that to be true of a call to a
3918 // get_pc_thunk function.
3919
3920 bool
3921 Target_i386::do_is_call_to_non_split(const Symbol* sym,
3922                                      const unsigned char*) const
3923 {
3924   return (sym->type() == elfcpp::STT_FUNC
3925           && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3926 }
3927
3928 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3929 // compiled with -fsplit-stack.  The function calls non-split-stack
3930 // code.  We have to change the function so that it always ensures
3931 // that it has enough stack space to run some random function.
3932
3933 void
3934 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3935                                        section_offset_type fnoffset,
3936                                        section_size_type fnsize,
3937                                        const unsigned char*,
3938                                        size_t,
3939                                        unsigned char* view,
3940                                        section_size_type view_size,
3941                                        std::string* from,
3942                                        std::string* to) const
3943 {
3944   // The function starts with a comparison of the stack pointer and a
3945   // field in the TCB.  This is followed by a jump.
3946
3947   // cmp %gs:NN,%esp
3948   if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3949       && fnsize > 7)
3950     {
3951       // We will call __morestack if the carry flag is set after this
3952       // comparison.  We turn the comparison into an stc instruction
3953       // and some nops.
3954       view[fnoffset] = '\xf9';
3955       this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3956     }
3957   // lea NN(%esp),%ecx
3958   // lea NN(%esp),%edx
3959   else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3960             || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3961            && fnsize > 7)
3962     {
3963       // This is loading an offset from the stack pointer for a
3964       // comparison.  The offset is negative, so we decrease the
3965       // offset by the amount of space we need for the stack.  This
3966       // means we will avoid calling __morestack if there happens to
3967       // be plenty of space on the stack already.
3968       unsigned char* pval = view + fnoffset + 3;
3969       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3970       val -= parameters->options().split_stack_adjust_size();
3971       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3972     }
3973   else
3974     {
3975       if (!object->has_no_split_stack())
3976         object->error(_("failed to match split-stack sequence at "
3977                         "section %u offset %0zx"),
3978                       shndx, static_cast<size_t>(fnoffset));
3979       return;
3980     }
3981
3982   // We have to change the function so that it calls
3983   // __morestack_non_split instead of __morestack.  The former will
3984   // allocate additional stack space.
3985   *from = "__morestack";
3986   *to = "__morestack_non_split";
3987 }
3988
3989 // The selector for i386 object files.  Note this is never instantiated
3990 // directly.  It's only used in Target_selector_i386_nacl, below.
3991
3992 class Target_selector_i386 : public Target_selector_freebsd
3993 {
3994 public:
3995   Target_selector_i386()
3996     : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3997                               "elf32-i386", "elf32-i386-freebsd",
3998                               "elf_i386")
3999   { }
4000
4001   Target*
4002   do_instantiate_target()
4003   { return new Target_i386(); }
4004 };
4005
4006 // NaCl variant.  It uses different PLT contents.
4007
4008 class Output_data_plt_i386_nacl : public Output_data_plt_i386
4009 {
4010  public:
4011   Output_data_plt_i386_nacl(Layout* layout,
4012                             Output_data_got_plt_i386* got_plt,
4013                             Output_data_space* got_irelative)
4014     : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
4015   { }
4016
4017  protected:
4018   virtual unsigned int
4019   do_get_plt_entry_size() const
4020   { return plt_entry_size; }
4021
4022   virtual void
4023   do_add_eh_frame(Layout* layout)
4024   {
4025     layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
4026                                  plt_eh_frame_fde, plt_eh_frame_fde_size);
4027   }
4028
4029   // The size of an entry in the PLT.
4030   static const int plt_entry_size = 64;
4031
4032   // The .eh_frame unwind information for the PLT.
4033   static const int plt_eh_frame_fde_size = 32;
4034   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
4035 };
4036
4037 class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl
4038 {
4039 public:
4040   Output_data_plt_i386_nacl_exec(Layout* layout,
4041                                  Output_data_got_plt_i386* got_plt,
4042                                  Output_data_space* got_irelative)
4043     : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
4044   { }
4045
4046  protected:
4047   virtual void
4048   do_fill_first_plt_entry(unsigned char* pov,
4049                           elfcpp::Elf_types<32>::Elf_Addr got_address);
4050
4051   virtual unsigned int
4052   do_fill_plt_entry(unsigned char* pov,
4053                     elfcpp::Elf_types<32>::Elf_Addr got_address,
4054                     unsigned int got_offset,
4055                     unsigned int plt_offset,
4056                     unsigned int plt_rel_offset);
4057
4058  private:
4059   // The first entry in the PLT for an executable.
4060   static const unsigned char first_plt_entry[plt_entry_size];
4061
4062   // Other entries in the PLT for an executable.
4063   static const unsigned char plt_entry[plt_entry_size];
4064 };
4065
4066 class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl
4067 {
4068  public:
4069   Output_data_plt_i386_nacl_dyn(Layout* layout,
4070                                 Output_data_got_plt_i386* got_plt,
4071                                 Output_data_space* got_irelative)
4072     : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
4073   { }
4074
4075  protected:
4076   virtual void
4077   do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
4078
4079   virtual unsigned int
4080   do_fill_plt_entry(unsigned char* pov,
4081                     elfcpp::Elf_types<32>::Elf_Addr,
4082                     unsigned int got_offset,
4083                     unsigned int plt_offset,
4084                     unsigned int plt_rel_offset);
4085
4086  private:
4087   // The first entry in the PLT for a shared object.
4088   static const unsigned char first_plt_entry[plt_entry_size];
4089
4090   // Other entries in the PLT for a shared object.
4091   static const unsigned char plt_entry[plt_entry_size];
4092 };
4093
4094 class Target_i386_nacl : public Target_i386
4095 {
4096  public:
4097   Target_i386_nacl()
4098     : Target_i386(&i386_nacl_info)
4099   { }
4100
4101  protected:
4102   virtual Output_data_plt_i386*
4103   do_make_data_plt(Layout* layout,
4104                    Output_data_got_plt_i386* got_plt,
4105                    Output_data_space* got_irelative,
4106                    bool dyn)
4107   {
4108     if (dyn)
4109       return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative);
4110     else
4111       return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative);
4112   }
4113
4114   virtual std::string
4115   do_code_fill(section_size_type length) const;
4116
4117  private:
4118   static const Target::Target_info i386_nacl_info;
4119 };
4120
4121 const Target::Target_info Target_i386_nacl::i386_nacl_info =
4122 {
4123   32,                   // size
4124   false,                // is_big_endian
4125   elfcpp::EM_386,       // machine_code
4126   false,                // has_make_symbol
4127   false,                // has_resolve
4128   true,                 // has_code_fill
4129   true,                 // is_default_stack_executable
4130   true,                 // can_icf_inline_merge_sections
4131   '\0',                 // wrap_char
4132   "/lib/ld-nacl-x86-32.so.1", // dynamic_linker
4133   0x20000,              // default_text_segment_address
4134   0x10000,              // abi_pagesize (overridable by -z max-page-size)
4135   0x10000,              // common_pagesize (overridable by -z common-page-size)
4136   true,                 // isolate_execinstr
4137   0x10000000,           // rosegment_gap
4138   elfcpp::SHN_UNDEF,    // small_common_shndx
4139   elfcpp::SHN_UNDEF,    // large_common_shndx
4140   0,                    // small_common_section_flags
4141   0,                    // large_common_section_flags
4142   NULL,                 // attributes_section
4143   NULL,                 // attributes_vendor
4144   "_start",             // entry_symbol_name
4145   32,                   // hash_entry_size
4146 };
4147
4148 #define NACLMASK        0xe0            // 32-byte alignment mask
4149
4150 const unsigned char
4151 Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] =
4152 {
4153   0xff, 0x35,                          // pushl contents of memory address
4154   0, 0, 0, 0,                          // replaced with address of .got + 4
4155   0x8b, 0x0d,                          // movl contents of address, %ecx
4156   0, 0, 0, 0,                          // replaced with address of .got + 8
4157   0x83, 0xe1, NACLMASK,                // andl $NACLMASK, %ecx
4158   0xff, 0xe1,                          // jmp *%ecx
4159   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4160   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4161   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4162   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4163   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4164   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4165   0x90, 0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4166   0x90, 0x90, 0x90, 0x90, 0x90
4167 };
4168
4169 void
4170 Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry(
4171     unsigned char* pov,
4172     elfcpp::Elf_types<32>::Elf_Addr got_address)
4173 {
4174   memcpy(pov, first_plt_entry, plt_entry_size);
4175   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
4176   elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
4177 }
4178
4179 // The first entry in the PLT for a shared object.
4180
4181 const unsigned char
4182 Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] =
4183 {
4184   0xff, 0xb3, 4, 0, 0, 0,       // pushl 4(%ebx)
4185   0x8b, 0x4b, 0x08,             // mov 0x8(%ebx), %ecx
4186   0x83, 0xe1, NACLMASK,         // andl $NACLMASK, %ecx
4187   0xff, 0xe1,                   // jmp *%ecx
4188   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4189   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4190   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4191   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4192   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4193   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4194   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4195   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4196   0x90, 0x90, 0x90, 0x90, 0x90,  // nops
4197   0x90, 0x90, 0x90, 0x90, 0x90   // nops
4198 };
4199
4200 void
4201 Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry(
4202     unsigned char* pov,
4203     elfcpp::Elf_types<32>::Elf_Addr)
4204 {
4205   memcpy(pov, first_plt_entry, plt_entry_size);
4206 }
4207
4208 // Subsequent entries in the PLT for an executable.
4209
4210 const unsigned char
4211 Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] =
4212 {
4213   0x8b, 0x0d,                    // movl contents of address, %ecx */
4214   0, 0, 0, 0,                    // replaced with address of symbol in .got
4215   0x83, 0xe1, NACLMASK,          // andl $NACLMASK, %ecx
4216   0xff, 0xe1,                    // jmp *%ecx
4217
4218   // Pad to the next 32-byte boundary with nop instructions.
4219   0x90,
4220   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4221   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4222
4223   // Lazy GOT entries point here (32-byte aligned).
4224   0x68,                       // pushl immediate
4225   0, 0, 0, 0,                 // replaced with offset into relocation table
4226   0xe9,                       // jmp relative
4227   0, 0, 0, 0,                 // replaced with offset to start of .plt
4228
4229   // Pad to the next 32-byte boundary with nop instructions.
4230   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4231   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4232   0x90, 0x90
4233 };
4234
4235 unsigned int
4236 Output_data_plt_i386_nacl_exec::do_fill_plt_entry(
4237     unsigned char* pov,
4238     elfcpp::Elf_types<32>::Elf_Addr got_address,
4239     unsigned int got_offset,
4240     unsigned int plt_offset,
4241     unsigned int plt_rel_offset)
4242 {
4243   memcpy(pov, plt_entry, plt_entry_size);
4244   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
4245                                               got_address + got_offset);
4246   elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4247   elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4248   return 32;
4249 }
4250
4251 // Subsequent entries in the PLT for a shared object.
4252
4253 const unsigned char
4254 Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] =
4255 {
4256   0x8b, 0x8b,          // movl offset(%ebx), %ecx
4257   0, 0, 0, 0,          // replaced with offset of symbol in .got
4258   0x83, 0xe1, 0xe0,    // andl $NACLMASK, %ecx
4259   0xff, 0xe1,          // jmp *%ecx
4260
4261   // Pad to the next 32-byte boundary with nop instructions.
4262   0x90,
4263   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4264   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4265
4266   // Lazy GOT entries point here (32-byte aligned).
4267   0x68,                // pushl immediate
4268   0, 0, 0, 0,          // replaced with offset into relocation table.
4269   0xe9,                // jmp relative
4270   0, 0, 0, 0,          // replaced with offset to start of .plt.
4271
4272   // Pad to the next 32-byte boundary with nop instructions.
4273   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4274   0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4275   0x90, 0x90
4276 };
4277
4278 unsigned int
4279 Output_data_plt_i386_nacl_dyn::do_fill_plt_entry(
4280     unsigned char* pov,
4281     elfcpp::Elf_types<32>::Elf_Addr,
4282     unsigned int got_offset,
4283     unsigned int plt_offset,
4284     unsigned int plt_rel_offset)
4285 {
4286   memcpy(pov, plt_entry, plt_entry_size);
4287   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
4288   elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
4289   elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
4290   return 32;
4291 }
4292
4293 const unsigned char
4294 Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] =
4295 {
4296   0, 0, 0, 0,                           // Replaced with offset to .plt.
4297   0, 0, 0, 0,                           // Replaced with size of .plt.
4298   0,                                    // Augmentation size.
4299   elfcpp::DW_CFA_def_cfa_offset, 8,     // DW_CFA_def_cfa_offset: 8.
4300   elfcpp::DW_CFA_advance_loc + 6,       // Advance 6 to __PLT__ + 6.
4301   elfcpp::DW_CFA_def_cfa_offset, 12,    // DW_CFA_def_cfa_offset: 12.
4302   elfcpp::DW_CFA_advance_loc + 58,      // Advance 58 to __PLT__ + 64.
4303   elfcpp::DW_CFA_def_cfa_expression,    // DW_CFA_def_cfa_expression.
4304   13,                                   // Block length.
4305   elfcpp::DW_OP_breg4, 4,               // Push %esp + 4.
4306   elfcpp::DW_OP_breg8, 0,               // Push %eip.
4307   elfcpp::DW_OP_const1u, 63,            // Push 0x3f.
4308   elfcpp::DW_OP_and,                    // & (%eip & 0x3f).
4309   elfcpp::DW_OP_const1u, 37,            // Push 0x25.
4310   elfcpp::DW_OP_ge,                     // >= ((%eip & 0x3f) >= 0x25)
4311   elfcpp::DW_OP_lit2,                   // Push 2.
4312   elfcpp::DW_OP_shl,                    // << (((%eip & 0x3f) >= 0x25) << 2)
4313   elfcpp::DW_OP_plus,                   // + ((((%eip&0x3f)>=0x25)<<2)+%esp+4
4314   elfcpp::DW_CFA_nop,                   // Align to 32 bytes.
4315   elfcpp::DW_CFA_nop
4316 };
4317
4318 // Return a string used to fill a code section with nops.
4319 // For NaCl, long NOPs are only valid if they do not cross
4320 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
4321 std::string
4322 Target_i386_nacl::do_code_fill(section_size_type length) const
4323 {
4324   return std::string(length, static_cast<char>(0x90));
4325 }
4326
4327 // The selector for i386-nacl object files.
4328
4329 class Target_selector_i386_nacl
4330   : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl>
4331 {
4332  public:
4333   Target_selector_i386_nacl()
4334     : Target_selector_nacl<Target_selector_i386,
4335                            Target_i386_nacl>("x86-32",
4336                                              "elf32-i386-nacl",
4337                                              "elf_i386_nacl")
4338   { }
4339 };
4340
4341 Target_selector_i386_nacl target_selector_i386;
4342
4343 // IAMCU variant.  It uses EM_IAMCU, not EM_386.
4344
4345 class Target_iamcu : public Target_i386
4346 {
4347  public:
4348   Target_iamcu()
4349     : Target_i386(&iamcu_info)
4350   { }
4351
4352  private:
4353   // Information about this specific target which we pass to the
4354   // general Target structure.
4355   static const Target::Target_info iamcu_info;
4356 };
4357
4358 const Target::Target_info Target_iamcu::iamcu_info =
4359 {
4360   32,                   // size
4361   false,                // is_big_endian
4362   elfcpp::EM_IAMCU,     // machine_code
4363   false,                // has_make_symbol
4364   false,                // has_resolve
4365   true,                 // has_code_fill
4366   true,                 // is_default_stack_executable
4367   true,                 // can_icf_inline_merge_sections
4368   '\0',                 // wrap_char
4369   "/usr/lib/libc.so.1", // dynamic_linker
4370   0x08048000,           // default_text_segment_address
4371   0x1000,               // abi_pagesize (overridable by -z max-page-size)
4372   0x1000,               // common_pagesize (overridable by -z common-page-size)
4373   false,                // isolate_execinstr
4374   0,                    // rosegment_gap
4375   elfcpp::SHN_UNDEF,    // small_common_shndx
4376   elfcpp::SHN_UNDEF,    // large_common_shndx
4377   0,                    // small_common_section_flags
4378   0,                    // large_common_section_flags
4379   NULL,                 // attributes_section
4380   NULL,                 // attributes_vendor
4381   "_start",             // entry_symbol_name
4382   32,                   // hash_entry_size
4383 };
4384
4385 class Target_selector_iamcu : public Target_selector
4386 {
4387 public:
4388   Target_selector_iamcu()
4389     : Target_selector(elfcpp::EM_IAMCU, 32, false, "elf32-iamcu",
4390                       "elf_iamcu")
4391   { }
4392
4393   Target*
4394   do_instantiate_target()
4395   { return new Target_iamcu(); }
4396 };
4397
4398 Target_selector_iamcu target_selector_iamcu;
4399
4400 } // End anonymous namespace.