[GOLD] Relocate::relocate() params
[external/binutils.git] / gold / tilegx.cc
1 // tilegx.cc -- tilegx target support for gold.
2
3 // Copyright (C) 2012-2015 Free Software Foundation, Inc.
4 // Written by Jiong Wang (jiwang@tilera.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 "tilegx.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 "gc.h"
42 #include "icf.h"
43
44 // the first got entry reserved
45 const int32_t TILEGX_GOT_RESERVE_COUNT = 1;
46
47 // the first two .got.plt entry reserved
48 const int32_t TILEGX_GOTPLT_RESERVE_COUNT = 2;
49
50 // 1. for both 64/32 bit mode, the instruction bundle is always 64bit.
51 // 2. thus .plt section should always be aligned to 64 bit.
52 const int32_t TILEGX_INST_BUNDLE_SIZE = 64;
53
54 namespace
55 {
56
57 using namespace gold;
58
59 // A class to handle the PLT data.
60 // This is an abstract base class that handles most of the linker details
61 // but does not know the actual contents of PLT entries.  The derived
62 // classes below fill in those details.
63
64 template<int size, bool big_endian>
65 class Output_data_plt_tilegx : public Output_section_data
66 {
67  public:
68   typedef Output_data_reloc<elfcpp::SHT_RELA, true,size, big_endian>
69     Reloc_section;
70
71   Output_data_plt_tilegx(Layout* layout, uint64_t addralign,
72                          Output_data_got<size, big_endian>* got,
73                          Output_data_space* got_plt,
74                          Output_data_space* got_irelative)
75     : Output_section_data(addralign), layout_(layout),
76       irelative_rel_(NULL), got_(got), got_plt_(got_plt),
77       got_irelative_(got_irelative), count_(0),
78       irelative_count_(0), free_list_()
79   { this->init(layout); }
80
81   Output_data_plt_tilegx(Layout* layout, uint64_t plt_entry_size,
82                          Output_data_got<size, big_endian>* got,
83                          Output_data_space* got_plt,
84                          Output_data_space* got_irelative,
85                          unsigned int plt_count)
86     : Output_section_data((plt_count + 1) * plt_entry_size,
87                           TILEGX_INST_BUNDLE_SIZE, false),
88       layout_(layout), irelative_rel_(NULL), got_(got),
89       got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
90       irelative_count_(0), free_list_()
91   {
92     this->init(layout);
93
94     // Initialize the free list and reserve the first entry.
95     this->free_list_.init((plt_count + 1) * plt_entry_size, false);
96     this->free_list_.remove(0, plt_entry_size);
97   }
98
99   // Initialize the PLT section.
100   void
101   init(Layout* layout);
102
103   // Add an entry to the PLT.
104   void
105   add_entry(Symbol_table*, Layout*, Symbol* gsym);
106
107   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
108   unsigned int
109   add_local_ifunc_entry(Symbol_table*, Layout*,
110     Sized_relobj_file<size, big_endian>*, unsigned int);
111
112   // Add the relocation for a PLT entry.
113   void
114   add_relocation(Symbol_table*, Layout*, Symbol*, unsigned int);
115
116   // Return the .rela.plt section data.
117   Reloc_section*
118   rela_plt()
119   { return this->rel_; }
120
121   // Return where the IRELATIVE relocations should go in the PLT
122   // relocations.
123   Reloc_section*
124   rela_irelative(Symbol_table*, Layout*);
125
126   // Return whether we created a section for IRELATIVE relocations.
127   bool
128   has_irelative_section() const
129   { return this->irelative_rel_ != NULL; }
130
131   // Return the number of PLT entries.
132   unsigned int
133   entry_count() const
134   { return this->count_ + this->irelative_count_; }
135
136   // Return the offset of the first non-reserved PLT entry.
137   unsigned int
138   first_plt_entry_offset()
139   { return this->get_plt_entry_size(); }
140
141   // Return the size of a PLT entry.
142   unsigned int
143   get_plt_entry_size() const
144   { return plt_entry_size; }
145
146   // Reserve a slot in the PLT for an existing symbol in an incremental update.
147   void
148   reserve_slot(unsigned int plt_index)
149   {
150     this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
151                             (plt_index + 2) * this->get_plt_entry_size());
152   }
153
154   // Return the PLT address to use for a global symbol.
155   uint64_t
156   address_for_global(const Symbol*);
157
158   // Return the PLT address to use for a local symbol.
159   uint64_t
160   address_for_local(const Relobj*, unsigned int symndx);
161
162  protected:
163   // Fill in the first PLT entry.
164   void
165   fill_first_plt_entry(unsigned char*);
166
167   // Fill in a normal PLT entry.  Returns the offset into the entry that
168   // should be the initial GOT slot value.
169   void
170   fill_plt_entry(unsigned char*,
171                  typename elfcpp::Elf_types<size>::Elf_Addr,
172                  unsigned int,
173                  typename elfcpp::Elf_types<size>::Elf_Addr,
174                  unsigned int, unsigned int);
175
176   void
177   do_adjust_output_section(Output_section* os);
178
179   // Write to a map file.
180   void
181   do_print_to_mapfile(Mapfile* mapfile) const
182   { mapfile->print_output_data(this, _("** PLT")); }
183
184  private:
185   // Set the final size.
186   void
187   set_final_data_size();
188
189   // Write out the PLT data.
190   void
191   do_write(Output_file*);
192
193   // A pointer to the Layout class, so that we can find the .dynamic
194   // section when we write out the GOT PLT section.
195   Layout* layout_;
196   // The reloc section.
197   Reloc_section* rel_;
198   // The IRELATIVE relocs, if necessary.  These must follow the
199   // regular PLT relocations.
200   Reloc_section* irelative_rel_;
201   // The .got section.
202   Output_data_got<size, big_endian>* got_;
203   // The .got.plt section.
204   Output_data_space* got_plt_;
205   // The part of the .got.plt section used for IRELATIVE relocs.
206   Output_data_space* got_irelative_;
207   // The number of PLT entries.
208   unsigned int count_;
209   // Number of PLT entries with R_TILEGX_IRELATIVE relocs.  These
210   // follow the regular PLT entries.
211   unsigned int irelative_count_;
212   // List of available regions within the section, for incremental
213   // update links.
214   Free_list free_list_;
215   // The size of an entry in the PLT.
216   static const int plt_entry_size = 40;
217   // The first entry in the PLT.
218   static const unsigned char first_plt_entry[plt_entry_size];
219   // Other entries in the PLT for an executable.
220   static const unsigned char plt_entry[plt_entry_size];
221 };
222
223 // The tilegx target class.
224 // See the ABI at
225 //   http://www.tilera.com/scm
226 // TLS info comes from
227 //   http://people.redhat.com/drepper/tls.pdf
228
229 template<int size, bool big_endian>
230 class Target_tilegx : public Sized_target<size, big_endian>
231 {
232  public:
233   // TileGX use RELA
234   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
235     Reloc_section;
236
237   Target_tilegx(const Target::Target_info* info = &tilegx_info)
238     : Sized_target<size, big_endian>(info),
239       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
240       global_offset_table_(NULL), tilegx_dynamic_(NULL), rela_dyn_(NULL),
241       rela_irelative_(NULL), copy_relocs_(elfcpp::R_TILEGX_COPY),
242       got_mod_index_offset_(-1U),
243       tls_get_addr_sym_defined_(false)
244   { }
245
246   // Scan the relocations to look for symbol adjustments.
247   void
248   gc_process_relocs(Symbol_table* symtab,
249                     Layout* layout,
250                     Sized_relobj_file<size, big_endian>* object,
251                     unsigned int data_shndx,
252                     unsigned int sh_type,
253                     const unsigned char* prelocs,
254                     size_t reloc_count,
255                     Output_section* output_section,
256                     bool needs_special_offset_handling,
257                     size_t local_symbol_count,
258                     const unsigned char* plocal_symbols);
259
260   // Scan the relocations to look for symbol adjustments.
261   void
262   scan_relocs(Symbol_table* symtab,
263               Layout* layout,
264               Sized_relobj_file<size, big_endian>* object,
265               unsigned int data_shndx,
266               unsigned int sh_type,
267               const unsigned char* prelocs,
268               size_t reloc_count,
269               Output_section* output_section,
270               bool needs_special_offset_handling,
271               size_t local_symbol_count,
272               const unsigned char* plocal_symbols);
273
274   // Finalize the sections.
275   void
276   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
277
278   // Return the value to use for a dynamic which requires special
279   // treatment.
280   uint64_t
281   do_dynsym_value(const Symbol*) const;
282
283   // Relocate a section.
284   void
285   relocate_section(const Relocate_info<size, big_endian>*,
286                    unsigned int sh_type,
287                    const unsigned char* prelocs,
288                    size_t reloc_count,
289                    Output_section* output_section,
290                    bool needs_special_offset_handling,
291                    unsigned char* view,
292                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
293                    section_size_type view_size,
294                    const Reloc_symbol_changes*);
295
296   // Scan the relocs during a relocatable link.
297   void
298   scan_relocatable_relocs(Symbol_table* symtab,
299                           Layout* layout,
300                           Sized_relobj_file<size, big_endian>* object,
301                           unsigned int data_shndx,
302                           unsigned int sh_type,
303                           const unsigned char* prelocs,
304                           size_t reloc_count,
305                           Output_section* output_section,
306                           bool needs_special_offset_handling,
307                           size_t local_symbol_count,
308                           const unsigned char* plocal_symbols,
309                           Relocatable_relocs*);
310
311   // Relocate a section during a relocatable link.
312   void
313   relocate_relocs(
314       const Relocate_info<size, big_endian>*,
315       unsigned int sh_type,
316       const unsigned char* prelocs,
317       size_t reloc_count,
318       Output_section* output_section,
319       typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
320       unsigned char* view,
321       typename elfcpp::Elf_types<size>::Elf_Addr view_address,
322       section_size_type view_size,
323       unsigned char* reloc_view,
324       section_size_type reloc_view_size);
325
326   // Return whether SYM is defined by the ABI.
327   bool
328   do_is_defined_by_abi(const Symbol* sym) const
329   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
330
331   // define tilegx specific symbols
332   virtual void
333   do_define_standard_symbols(Symbol_table*, Layout*);
334
335   // Return the PLT section.
336   uint64_t
337   do_plt_address_for_global(const Symbol* gsym) const
338   { return this->plt_section()->address_for_global(gsym); }
339
340   uint64_t
341   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
342   { return this->plt_section()->address_for_local(relobj, symndx); }
343
344   // This function should be defined in targets that can use relocation
345   // types to determine (implemented in local_reloc_may_be_function_pointer
346   // and global_reloc_may_be_function_pointer)
347   // if a function's pointer is taken.  ICF uses this in safe mode to only
348   // fold those functions whose pointer is defintely not taken.  For tilegx
349   // pie binaries, safe ICF cannot be done by looking at relocation types.
350   bool
351   do_can_check_for_function_pointers() const
352   { return true; }
353
354   // Return the base for a DW_EH_PE_datarel encoding.
355   uint64_t
356   do_ehframe_datarel_base() const;
357
358   // Return whether there is a GOT section.
359   bool
360   has_got_section() const
361   { return this->got_ != NULL; }
362
363   // Return the size of the GOT section.
364   section_size_type
365   got_size() const
366   {
367     gold_assert(this->got_ != NULL);
368     return this->got_->data_size();
369   }
370
371   // Return the number of entries in the GOT.
372   unsigned int
373   got_entry_count() const
374   {
375     if (this->got_ == NULL)
376       return 0;
377     return this->got_size() / (size / 8);
378   }
379
380   // Return the number of entries in the PLT.
381   unsigned int
382   plt_entry_count() const;
383
384   // Return the offset of the first non-reserved PLT entry.
385   unsigned int
386   first_plt_entry_offset() const;
387
388   // Return the size of each PLT entry.
389   unsigned int
390   plt_entry_size() const;
391
392   // Create the GOT section for an incremental update.
393   Output_data_got_base*
394   init_got_plt_for_update(Symbol_table* symtab,
395                           Layout* layout,
396                           unsigned int got_count,
397                           unsigned int plt_count);
398
399   // Reserve a GOT entry for a local symbol, and regenerate any
400   // necessary dynamic relocations.
401   void
402   reserve_local_got_entry(unsigned int got_index,
403                           Sized_relobj<size, big_endian>* obj,
404                           unsigned int r_sym,
405                           unsigned int got_type);
406
407   // Reserve a GOT entry for a global symbol, and regenerate any
408   // necessary dynamic relocations.
409   void
410   reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
411                            unsigned int got_type);
412
413   // Register an existing PLT entry for a global symbol.
414   void
415   register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
416                             Symbol* gsym);
417
418   // Force a COPY relocation for a given symbol.
419   void
420   emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
421
422   // Apply an incremental relocation.
423   void
424   apply_relocation(const Relocate_info<size, big_endian>* relinfo,
425                    typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
426                    unsigned int r_type,
427                    typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
428                    const Symbol* gsym,
429                    unsigned char* view,
430                    typename elfcpp::Elf_types<size>::Elf_Addr address,
431                    section_size_type view_size);
432
433  private:
434   // The class which scans relocations.
435   class Scan
436   {
437   public:
438     Scan()
439       : issued_non_pic_error_(false)
440     { }
441
442     static inline int
443     get_reference_flags(unsigned int r_type);
444
445     inline void
446     local(Symbol_table* symtab, Layout* layout, Target_tilegx* target,
447           Sized_relobj_file<size, big_endian>* object,
448           unsigned int data_shndx,
449           Output_section* output_section,
450           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
451           const elfcpp::Sym<size, big_endian>& lsym,
452           bool is_discarded);
453
454     inline void
455     global(Symbol_table* symtab, Layout* layout, Target_tilegx* target,
456            Sized_relobj_file<size, big_endian>* object,
457            unsigned int data_shndx,
458            Output_section* output_section,
459            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
460            Symbol* gsym);
461
462     inline bool
463     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
464                             Target_tilegx* target,
465                             Sized_relobj_file<size, big_endian>* object,
466                             unsigned int data_shndx,
467                             Output_section* output_section,
468                             const elfcpp::Rela<size, big_endian>& reloc,
469                             unsigned int r_type,
470                             const elfcpp::Sym<size, big_endian>& lsym);
471
472     inline bool
473     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
474                             Target_tilegx* target,
475                             Sized_relobj_file<size, big_endian>* object,
476                             unsigned int data_shndx,
477                             Output_section* output_section,
478                             const elfcpp::Rela<size, big_endian>& reloc,
479                             unsigned int r_type,
480                             Symbol* gsym);
481
482   private:
483     static void
484     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
485                             unsigned int r_type);
486
487     static void
488     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
489                              unsigned int r_type, Symbol*);
490
491     void
492     check_non_pic(Relobj*, unsigned int r_type);
493
494     inline bool
495     possible_function_pointer_reloc(unsigned int r_type);
496
497     bool
498     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
499                               unsigned int r_type);
500
501     // Whether we have issued an error about a non-PIC compilation.
502     bool issued_non_pic_error_;
503   };
504
505   // The class which implements relocation.
506   class Relocate
507   {
508    public:
509     Relocate()
510     { }
511
512     ~Relocate()
513     {
514     }
515
516     // Do a relocation.  Return false if the caller should not issue
517     // any warnings about this relocation.
518     inline bool
519     relocate(const Relocate_info<size, big_endian>*, unsigned int,
520              Target_tilegx*, Output_section*, size_t, const unsigned char*,
521              const Sized_symbol<size>*, const Symbol_value<size>*,
522              unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
523              section_size_type);
524   };
525
526   // A class which returns the size required for a relocation type,
527   // used while scanning relocs during a relocatable link.
528   class Relocatable_size_for_reloc
529   {
530    public:
531     unsigned int
532     get_size_for_reloc(unsigned int, Relobj*);
533   };
534
535   // Adjust TLS relocation type based on the options and whether this
536   // is a local symbol.
537   static tls::Tls_optimization
538   optimize_tls_reloc(bool is_final, int r_type);
539
540   // Get the GOT section, creating it if necessary.
541   Output_data_got<size, big_endian>*
542   got_section(Symbol_table*, Layout*);
543
544   // Get the GOT PLT section.
545   Output_data_space*
546   got_plt_section() const
547   {
548     gold_assert(this->got_plt_ != NULL);
549     return this->got_plt_;
550   }
551
552   // Create the PLT section.
553   void
554   make_plt_section(Symbol_table* symtab, Layout* layout);
555
556   // Create a PLT entry for a global symbol.
557   void
558   make_plt_entry(Symbol_table*, Layout*, Symbol*);
559
560   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
561   void
562   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
563                              Sized_relobj_file<size, big_endian>* relobj,
564                              unsigned int local_sym_index);
565
566   // Create a GOT entry for the TLS module index.
567   unsigned int
568   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
569                       Sized_relobj_file<size, big_endian>* object);
570
571   // Get the PLT section.
572   Output_data_plt_tilegx<size, big_endian>*
573   plt_section() const
574   {
575     gold_assert(this->plt_ != NULL);
576     return this->plt_;
577   }
578
579   // Get the dynamic reloc section, creating it if necessary.
580   Reloc_section*
581   rela_dyn_section(Layout*);
582
583   // Get the section to use for IRELATIVE relocations.
584   Reloc_section*
585   rela_irelative_section(Layout*);
586
587   // Add a potential copy relocation.
588   void
589   copy_reloc(Symbol_table* symtab, Layout* layout,
590              Sized_relobj_file<size, big_endian>* object,
591              unsigned int shndx, Output_section* output_section,
592              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
593   {
594     unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
595     this->copy_relocs_.copy_reloc(symtab, layout,
596                                   symtab->get_sized_symbol<size>(sym),
597                                   object, shndx, output_section,
598                                   r_type, reloc.get_r_offset(),
599                                   reloc.get_r_addend(),
600                                   this->rela_dyn_section(layout));
601   }
602
603   // Information about this specific target which we pass to the
604   // general Target structure.
605   static const Target::Target_info tilegx_info;
606
607   // The types of GOT entries needed for this platform.
608   // These values are exposed to the ABI in an incremental link.
609   // Do not renumber existing values without changing the version
610   // number of the .gnu_incremental_inputs section.
611   enum Got_type
612   {
613     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
614     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
615     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
616     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
617   };
618
619   // This type is used as the argument to the target specific
620   // relocation routines.  The only target specific reloc is
621   // R_X86_64_TLSDESC against a local symbol.
622   struct Tlsdesc_info
623   {
624     Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
625                  unsigned int a_r_sym)
626       : object(a_object), r_sym(a_r_sym)
627     { }
628
629     // The object in which the local symbol is defined.
630     Sized_relobj_file<size, big_endian>* object;
631     // The local symbol index in the object.
632     unsigned int r_sym;
633   };
634
635   // The GOT section.
636   Output_data_got<size, big_endian>* got_;
637   // The PLT section.
638   Output_data_plt_tilegx<size, big_endian>* plt_;
639   // The GOT PLT section.
640   Output_data_space* got_plt_;
641   // The GOT section for IRELATIVE relocations.
642   Output_data_space* got_irelative_;
643   // The _GLOBAL_OFFSET_TABLE_ symbol.
644   Symbol* global_offset_table_;
645   // The _TILEGX_DYNAMIC_ symbol.
646   Symbol* tilegx_dynamic_;
647   // The dynamic reloc section.
648   Reloc_section* rela_dyn_;
649   // The section to use for IRELATIVE relocs.
650   Reloc_section* rela_irelative_;
651   // Relocs saved to avoid a COPY reloc.
652   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
653   // Offset of the GOT entry for the TLS module index.
654   unsigned int got_mod_index_offset_;
655   // True if the _tls_get_addr symbol has been defined.
656   bool tls_get_addr_sym_defined_;
657 };
658
659 template<>
660 const Target::Target_info Target_tilegx<64, false>::tilegx_info =
661 {
662   64,                   // size
663   false,                // is_big_endian
664   elfcpp::EM_TILEGX,    // machine_code
665   false,                // has_make_symbol
666   false,                // has_resolve
667   false,                // has_code_fill
668   true,                 // is_default_stack_executable
669   false,                // can_icf_inline_merge_sections
670   '\0',                 // wrap_char
671   "/lib/ld.so.1",       // program interpreter
672   0x10000,              // default_text_segment_address
673   0x10000,              // abi_pagesize (overridable by -z max-page-size)
674   0x10000,              // common_pagesize (overridable by -z common-page-size)
675   false,                // isolate_execinstr
676   0,                    // rosegment_gap
677   elfcpp::SHN_UNDEF,    // small_common_shndx
678   elfcpp::SHN_UNDEF,    // large_common_shndx
679   0,                    // small_common_section_flags
680   0,                    // large_common_section_flags
681   NULL,                 // attributes_section
682   NULL,                 // attributes_vendor
683   "_start",             // entry_symbol_name
684   32,                   // hash_entry_size
685 };
686
687 template<>
688 const Target::Target_info Target_tilegx<32, false>::tilegx_info =
689 {
690   32,                   // size
691   false,                // is_big_endian
692   elfcpp::EM_TILEGX,    // machine_code
693   false,                // has_make_symbol
694   false,                // has_resolve
695   false,                // has_code_fill
696   true,                 // is_default_stack_executable
697   false,                // can_icf_inline_merge_sections
698   '\0',                 // wrap_char
699   "/lib32/ld.so.1",     // program interpreter
700   0x10000,              // default_text_segment_address
701   0x10000,              // abi_pagesize (overridable by -z max-page-size)
702   0x10000,              // common_pagesize (overridable by -z common-page-size)
703   false,                // isolate_execinstr
704   0,                    // rosegment_gap
705   elfcpp::SHN_UNDEF,    // small_common_shndx
706   elfcpp::SHN_UNDEF,    // large_common_shndx
707   0,                    // small_common_section_flags
708   0,                    // large_common_section_flags
709   NULL,                 // attributes_section
710   NULL,                 // attributes_vendor
711   "_start",             // entry_symbol_name
712   32,                   // hash_entry_size
713 };
714
715 template<>
716 const Target::Target_info Target_tilegx<64, true>::tilegx_info =
717 {
718   64,                   // size
719   true,                 // is_big_endian
720   elfcpp::EM_TILEGX,    // machine_code
721   false,                // has_make_symbol
722   false,                // has_resolve
723   false,                // has_code_fill
724   true,                 // is_default_stack_executable
725   false,                // can_icf_inline_merge_sections
726   '\0',                 // wrap_char
727   "/lib/ld.so.1",       // program interpreter
728   0x10000,              // default_text_segment_address
729   0x10000,              // abi_pagesize (overridable by -z max-page-size)
730   0x10000,              // common_pagesize (overridable by -z common-page-size)
731   false,                // isolate_execinstr
732   0,                    // rosegment_gap
733   elfcpp::SHN_UNDEF,    // small_common_shndx
734   elfcpp::SHN_UNDEF,    // large_common_shndx
735   0,                    // small_common_section_flags
736   0,                    // large_common_section_flags
737   NULL,                 // attributes_section
738   NULL,                 // attributes_vendor
739   "_start",             // entry_symbol_name
740   32,                   // hash_entry_size
741 };
742
743 template<>
744 const Target::Target_info Target_tilegx<32, true>::tilegx_info =
745 {
746   32,                   // size
747   true,                 // is_big_endian
748   elfcpp::EM_TILEGX,    // machine_code
749   false,                // has_make_symbol
750   false,                // has_resolve
751   false,                // has_code_fill
752   true,                 // is_default_stack_executable
753   false,                // can_icf_inline_merge_sections
754   '\0',                 // wrap_char
755   "/lib32/ld.so.1",     // program interpreter
756   0x10000,              // default_text_segment_address
757   0x10000,              // abi_pagesize (overridable by -z max-page-size)
758   0x10000,              // common_pagesize (overridable by -z common-page-size)
759   false,                // isolate_execinstr
760   0,                    // rosegment_gap
761   elfcpp::SHN_UNDEF,    // small_common_shndx
762   elfcpp::SHN_UNDEF,    // large_common_shndx
763   0,                    // small_common_section_flags
764   0,                    // large_common_section_flags
765   NULL,                 // attributes_section
766   NULL,                  // attributes_vendor
767   "_start",             // entry_symbol_name
768   32,                   // hash_entry_size
769 };
770
771 // tilegx relocation handlers
772 template<int size, bool big_endian>
773 class Tilegx_relocate_functions
774 {
775 public:
776   // overflow check will be supported later
777   typedef enum
778   {
779     STATUS_OKAY,        // No error during relocation.
780     STATUS_OVERFLOW,    // Relocation overflow.
781     STATUS_BAD_RELOC    // Relocation cannot be applied.
782   } Status;
783
784   struct Tilegx_howto
785   {
786     // right shift operand by this number of bits.
787     unsigned char srshift;
788
789     // the offset to apply relocation.
790     unsigned char doffset;
791
792     // set to 1 for pc-relative relocation.
793     unsigned char is_pcrel;
794
795     // size in bits, or 0 if this table entry should be ignored.
796     unsigned char bsize;
797
798     // whether we need to check overflow.
799     unsigned char overflow;
800   };
801
802   static const Tilegx_howto howto[elfcpp::R_TILEGX_NUM];
803
804 private:
805
806   // Do a simple rela relocation
807   template<int valsize>
808   static inline void
809   rela(unsigned char* view,
810        const Sized_relobj_file<size, big_endian>* object,
811        const Symbol_value<size>* psymval,
812        typename elfcpp::Swap<size, big_endian>::Valtype addend,
813        elfcpp::Elf_Xword srshift, elfcpp::Elf_Xword doffset,
814        elfcpp::Elf_Xword bitmask)
815   {
816     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
817     Valtype* wv = reinterpret_cast<Valtype*>(view);
818     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
819     Valtype reloc = 0;
820     if (size == 32)
821       reloc = Bits<32>::sign_extend(psymval->value(object, addend)) >> srshift;
822     else
823       reloc = psymval->value(object, addend) >> srshift;
824
825     elfcpp::Elf_Xword dst_mask = bitmask << doffset;
826
827     val &= ~dst_mask;
828     reloc &= bitmask;
829
830     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | (reloc<<doffset));
831   }
832
833   // Do a simple rela relocation
834   template<int valsize>
835   static inline void
836   rela_ua(unsigned char* view,
837           const Sized_relobj_file<size, big_endian>* object,
838           const Symbol_value<size>* psymval,
839           typename elfcpp::Swap<size, big_endian>::Valtype addend,
840           elfcpp::Elf_Xword srshift, elfcpp::Elf_Xword doffset,
841           elfcpp::Elf_Xword bitmask)
842   {
843     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
844       Valtype;
845     unsigned char* wv = view;
846     Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
847     Valtype reloc = 0;
848     if (size == 32)
849       reloc = Bits<32>::sign_extend(psymval->value(object, addend)) >> srshift;
850     else
851       reloc = psymval->value(object, addend) >> srshift;
852
853     elfcpp::Elf_Xword dst_mask = bitmask << doffset;
854
855     val &= ~dst_mask;
856     reloc &= bitmask;
857
858     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv,
859       val | (reloc<<doffset));
860   }
861
862   template<int valsize>
863   static inline void
864   rela(unsigned char* view,
865        const Sized_relobj_file<size, big_endian>* object,
866        const Symbol_value<size>* psymval,
867        typename elfcpp::Swap<size, big_endian>::Valtype addend,
868        elfcpp::Elf_Xword srshift, elfcpp::Elf_Xword doffset1,
869        elfcpp::Elf_Xword bitmask1, elfcpp::Elf_Xword doffset2,
870        elfcpp::Elf_Xword bitmask2)
871   {
872     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
873     Valtype* wv = reinterpret_cast<Valtype*>(view);
874     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
875     Valtype reloc = 0;
876     if (size == 32)
877       reloc = Bits<32>::sign_extend(psymval->value(object, addend)) >> srshift;
878     else
879       reloc = psymval->value(object, addend) >> srshift;
880
881     elfcpp::Elf_Xword dst_mask = (bitmask1 << doffset1)
882                                   | (bitmask2 << doffset2);
883     val &= ~dst_mask;
884     reloc = ((reloc & bitmask1) << doffset1)
885              | ((reloc & bitmask2) << doffset2);
886
887     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
888
889   }
890
891   // Do a simple PC relative relocation with a Symbol_value with the
892   // addend in the relocation.
893   template<int valsize>
894   static inline void
895   pcrela(unsigned char* view,
896          const Sized_relobj_file<size, big_endian>* object,
897          const Symbol_value<size>* psymval,
898          typename elfcpp::Swap<size, big_endian>::Valtype addend,
899          typename elfcpp::Elf_types<size>::Elf_Addr address,
900          elfcpp::Elf_Xword srshift, elfcpp::Elf_Xword doffset,
901          elfcpp::Elf_Xword bitmask)
902
903   {
904     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
905     Valtype* wv = reinterpret_cast<Valtype*>(view);
906     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
907     Valtype reloc = 0;
908     if (size == 32)
909       reloc = Bits<32>::sign_extend(psymval->value(object, addend) - address)
910                >> srshift;
911     else
912       reloc = (psymval->value(object, addend) - address) >> srshift;
913
914     elfcpp::Elf_Xword dst_mask = bitmask << doffset;
915     val &= ~dst_mask;
916     reloc &= bitmask;
917
918     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | (reloc<<doffset));
919   }
920
921   template<int valsize>
922   static inline void
923   pcrela_ua(unsigned char* view,
924            const Sized_relobj_file<size, big_endian>* object,
925            const Symbol_value<size>* psymval,
926            typename elfcpp::Swap<size, big_endian>::Valtype addend,
927            typename elfcpp::Elf_types<size>::Elf_Addr address,
928            elfcpp::Elf_Xword srshift, elfcpp::Elf_Xword doffset,
929            elfcpp::Elf_Xword bitmask)
930
931   {
932     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
933       Valtype;
934     unsigned char* wv = view;
935     Valtype reloc = 0;
936     if (size == 32)
937       reloc = Bits<32>::sign_extend(psymval->value(object, addend) - address)
938                >> srshift;
939     else
940       reloc = (psymval->value(object, addend) - address) >> srshift;
941
942     reloc &= bitmask;
943
944     elfcpp::Swap<valsize, big_endian>::writeval(wv, reloc << doffset);
945   }
946
947   template<int valsize>
948   static inline void
949   pcrela(unsigned char* view,
950          const Sized_relobj_file<size, big_endian>* object,
951          const Symbol_value<size>* psymval,
952          typename elfcpp::Swap<size, big_endian>::Valtype addend,
953          typename elfcpp::Elf_types<size>::Elf_Addr address,
954          elfcpp::Elf_Xword srshift, elfcpp::Elf_Xword doffset1,
955          elfcpp::Elf_Xword bitmask1, elfcpp::Elf_Xword doffset2,
956          elfcpp::Elf_Xword bitmask2)
957
958   {
959     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
960     Valtype* wv = reinterpret_cast<Valtype*>(view);
961     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
962     Valtype reloc = 0;
963     if (size == 32)
964       reloc = Bits<32>::sign_extend(psymval->value(object, addend) - address)
965                >> srshift;
966     else
967       reloc = (psymval->value(object, addend) - address) >> srshift;
968
969     elfcpp::Elf_Xword dst_mask = (bitmask1 << doffset1)
970                                   | (bitmask2 << doffset2);
971     val &= ~dst_mask;
972     reloc = ((reloc & bitmask1) << doffset1)
973              | ((reloc & bitmask2) << doffset2);
974
975     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
976   }
977
978   typedef Tilegx_relocate_functions<size, big_endian> This;
979   typedef Relocate_functions<size, big_endian> Base;
980
981 public:
982
983   static inline void
984   abs64(unsigned char* view,
985         const Sized_relobj_file<size, big_endian>* object,
986         const Symbol_value<size>* psymval,
987         typename elfcpp::Elf_types<size>::Elf_Addr addend)
988   {
989     This::template rela_ua<64>(view, object, psymval, addend, 0, 0,
990                                0xffffffffffffffffllu);
991   }
992
993   static inline void
994   abs32(unsigned char* view,
995         const Sized_relobj_file<size, big_endian>* object,
996         const Symbol_value<size>* psymval,
997         typename elfcpp::Elf_types<size>::Elf_Addr addend)
998   {
999     This::template rela_ua<32>(view, object, psymval, addend, 0, 0,
1000                                0xffffffff);
1001   }
1002
1003   static inline void
1004   abs16(unsigned char* view,
1005         const Sized_relobj_file<size, big_endian>* object,
1006         const Symbol_value<size>* psymval,
1007         typename elfcpp::Elf_types<size>::Elf_Addr addend)
1008   {
1009     This::template rela_ua<16>(view, object, psymval, addend, 0, 0,
1010                                0xffff);
1011   }
1012
1013   static inline void
1014   pc_abs64(unsigned char* view,
1015         const Sized_relobj_file<size, big_endian>* object,
1016         const Symbol_value<size>* psymval,
1017         typename elfcpp::Elf_types<size>::Elf_Addr addend,
1018             typename elfcpp::Elf_types<size>::Elf_Addr address)
1019   {
1020     This::template pcrela_ua<64>(view, object, psymval, addend, address, 0, 0,
1021                                0xffffffffffffffffllu);
1022   }
1023
1024   static inline void
1025   pc_abs32(unsigned char* view,
1026         const Sized_relobj_file<size, big_endian>* object,
1027         const Symbol_value<size>* psymval,
1028         typename elfcpp::Elf_types<size>::Elf_Addr addend,
1029             typename elfcpp::Elf_types<size>::Elf_Addr address)
1030   {
1031     This::template pcrela_ua<32>(view, object, psymval, addend, address, 0, 0,
1032                                  0xffffffff);
1033   }
1034
1035   static inline void
1036   pc_abs16(unsigned char* view,
1037         const Sized_relobj_file<size, big_endian>* object,
1038         const Symbol_value<size>* psymval,
1039         typename elfcpp::Elf_types<size>::Elf_Addr addend,
1040             typename elfcpp::Elf_types<size>::Elf_Addr address)
1041   {
1042     This::template pcrela_ua<16>(view, object, psymval, addend, address, 0, 0,
1043                                  0xffff);
1044   }
1045
1046   static inline void
1047   imm_x_general(unsigned char* view,
1048                 const Sized_relobj_file<size, big_endian>* object,
1049                 const Symbol_value<size>* psymval,
1050                 typename elfcpp::Elf_types<size>::Elf_Addr addend,
1051                 Tilegx_howto &r_howto)
1052   {
1053     This::template rela<64>(view, object, psymval, addend,
1054                             (elfcpp::Elf_Xword)(r_howto.srshift),
1055                             (elfcpp::Elf_Xword)(r_howto.doffset),
1056                             (elfcpp::Elf_Xword)((1 << r_howto.bsize) - 1));
1057   }
1058
1059   static inline void
1060   imm_x_pcrel_general(unsigned char* view,
1061                       const Sized_relobj_file<size, big_endian>* object,
1062                       const Symbol_value<size>* psymval,
1063                       typename elfcpp::Elf_types<size>::Elf_Addr addend,
1064                       typename elfcpp::Elf_types<size>::Elf_Addr address,
1065                       Tilegx_howto &r_howto)
1066   {
1067     This::template pcrela<64>(view, object, psymval, addend, address,
1068                               (elfcpp::Elf_Xword)(r_howto.srshift),
1069                               (elfcpp::Elf_Xword)(r_howto.doffset),
1070                               (elfcpp::Elf_Xword)((1 << r_howto.bsize) - 1));
1071   }
1072
1073   static inline void
1074   imm_x_two_part_general(unsigned char* view,
1075                          const Sized_relobj_file<size, big_endian>* object,
1076                          const Symbol_value<size>* psymval,
1077                          typename elfcpp::Elf_types<size>::Elf_Addr addend,
1078                          typename elfcpp::Elf_types<size>::Elf_Addr address,
1079                          unsigned int r_type)
1080   {
1081
1082     elfcpp::Elf_Xword doffset1 = 0llu;
1083     elfcpp::Elf_Xword doffset2 = 0llu;
1084     elfcpp::Elf_Xword dmask1   = 0llu;
1085     elfcpp::Elf_Xword dmask2   = 0llu;
1086     elfcpp::Elf_Xword rshift   = 0llu;
1087     unsigned int pc_rel        = 0;
1088
1089     switch (r_type)
1090       {
1091       case elfcpp::R_TILEGX_BROFF_X1:
1092         doffset1 = 31llu;
1093         doffset2 = 37llu;
1094         dmask1   = 0x3fllu;
1095         dmask2   = 0x1ffc0llu;
1096         rshift   = 3llu;
1097         pc_rel   = 1;
1098         break;
1099       case elfcpp::R_TILEGX_DEST_IMM8_X1:
1100         doffset1 = 31llu;
1101         doffset2 = 43llu;
1102         dmask1   = 0x3fllu;
1103         dmask2   = 0xc0llu;
1104         rshift   = 0llu;
1105         break;
1106       }
1107
1108     if (pc_rel)
1109       This::template pcrela<64>(view, object, psymval, addend, address,
1110                                 rshift, doffset1, dmask1, doffset2, dmask2);
1111     else
1112       This::template rela<64>(view, object, psymval, addend, rshift,
1113                               doffset1, dmask1, doffset2, dmask2);
1114
1115   }
1116
1117   static inline void
1118   tls_relax(unsigned char* view, unsigned int r_type,
1119             tls::Tls_optimization opt_t)
1120   {
1121
1122     const uint64_t TILEGX_X_MOVE_R0_R0 = 0x283bf8005107f000llu;
1123     const uint64_t TILEGX_Y_MOVE_R0_R0 = 0xae05f800540bf000llu;
1124     const uint64_t TILEGX_X_LD         = 0x286ae80000000000llu;
1125     const uint64_t TILEGX_X_LD4S       = 0x286a980000000000llu;
1126     const uint64_t TILEGX_X1_FULL_MASK = 0x3fffffff80000000llu;
1127     const uint64_t TILEGX_X0_RRR_MASK  = 0x000000007ffc0000llu;
1128     const uint64_t TILEGX_X1_RRR_MASK  = 0x3ffe000000000000llu;
1129     const uint64_t TILEGX_Y0_RRR_MASK  = 0x00000000780c0000llu;
1130     const uint64_t TILEGX_Y1_RRR_MASK  = 0x3c06000000000000llu;
1131     const uint64_t TILEGX_X0_RRR_SRCB_MASK = 0x000000007ffff000llu;
1132     const uint64_t TILEGX_X1_RRR_SRCB_MASK = 0x3ffff80000000000llu;
1133     const uint64_t TILEGX_Y0_RRR_SRCB_MASK = 0x00000000780ff000llu;
1134     const uint64_t TILEGX_Y1_RRR_SRCB_MASK = 0x3c07f80000000000llu;
1135     const uint64_t TILEGX_X_ADD_R0_R0_TP   = 0x2807a800500f5000llu;
1136     const uint64_t TILEGX_Y_ADD_R0_R0_TP   = 0x9a13a8002c275000llu;
1137     const uint64_t TILEGX_X_ADDX_R0_R0_TP  = 0x2805a800500b5000llu;
1138     const uint64_t TILEGX_Y_ADDX_R0_R0_TP  = 0x9a01a8002c035000llu;
1139
1140     const uint64_t R_TILEGX_IMM8_X0_TLS_ADD_MASK =
1141       (TILEGX_X0_RRR_MASK | (0x3Fllu << 12));
1142
1143     const uint64_t R_TILEGX_IMM8_X1_TLS_ADD_MASK =
1144       (TILEGX_X1_RRR_MASK | (0x3Fllu << 43));
1145
1146     const uint64_t R_TILEGX_IMM8_Y0_TLS_ADD_MASK =
1147       (TILEGX_Y0_RRR_MASK | (0x3Fllu << 12));
1148
1149     const uint64_t R_TILEGX_IMM8_Y1_TLS_ADD_MASK =
1150       (TILEGX_Y1_RRR_MASK | (0x3Fllu << 43));
1151
1152     const uint64_t R_TILEGX_IMM8_X0_TLS_ADD_LE_MASK =
1153       (TILEGX_X0_RRR_SRCB_MASK | (0x3Fllu << 6));
1154
1155     const uint64_t R_TILEGX_IMM8_X1_TLS_ADD_LE_MASK =
1156       (TILEGX_X1_RRR_SRCB_MASK | (0x3Fllu << 37));
1157
1158     const uint64_t R_TILEGX_IMM8_Y0_TLS_ADD_LE_MASK =
1159       (TILEGX_Y0_RRR_SRCB_MASK | (0x3Fllu << 6));
1160
1161     const uint64_t R_TILEGX_IMM8_Y1_TLS_ADD_LE_MASK =
1162       (TILEGX_Y1_RRR_SRCB_MASK | (0x3Fllu << 37));
1163
1164     typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1165     Valtype* wv = reinterpret_cast<Valtype*>(view);
1166     Valtype val = elfcpp::Swap<64, big_endian>::readval(wv);
1167     Valtype reloc = 0;
1168
1169     switch (r_type)
1170     {
1171       case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
1172         if (opt_t == tls::TLSOPT_NONE) {
1173           // GD/IE: 1. copy dest operand into the second source operand
1174           //        2. change the opcode to "add"
1175           reloc = (val & 0x3Fllu) << 12;  // featch the dest reg
1176           reloc |= ((size == 32
1177                      ? TILEGX_X_ADDX_R0_R0_TP
1178                      : TILEGX_X_ADD_R0_R0_TP)
1179                     & TILEGX_X0_RRR_MASK);  // change opcode
1180           val &= ~R_TILEGX_IMM8_X0_TLS_ADD_MASK;
1181         } else if (opt_t == tls::TLSOPT_TO_LE) {
1182           // LE: 1. copy dest operand into the first source operand
1183           //     2. change the opcode to "move"
1184           reloc = (val & 0x3Fllu) << 6;
1185           reloc |= (TILEGX_X_MOVE_R0_R0 & TILEGX_X0_RRR_SRCB_MASK);
1186           val &= ~R_TILEGX_IMM8_X0_TLS_ADD_LE_MASK;
1187         } else
1188           gold_unreachable();
1189         break;
1190       case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
1191         if (opt_t == tls::TLSOPT_NONE) {
1192           reloc = (val & (0x3Fllu << 31)) << 12;
1193           reloc |= ((size == 32
1194                      ? TILEGX_X_ADDX_R0_R0_TP
1195                      : TILEGX_X_ADD_R0_R0_TP)
1196                     & TILEGX_X1_RRR_MASK);
1197           val &= ~R_TILEGX_IMM8_X1_TLS_ADD_MASK;
1198         } else if (opt_t == tls::TLSOPT_TO_LE) {
1199           reloc = (val & (0x3Fllu << 31)) << 6;
1200           reloc |= (TILEGX_X_MOVE_R0_R0 & TILEGX_X1_RRR_SRCB_MASK);
1201           val &= ~R_TILEGX_IMM8_X1_TLS_ADD_LE_MASK;
1202         } else
1203           gold_unreachable();
1204         break;
1205       case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
1206         if (opt_t == tls::TLSOPT_NONE) {
1207           reloc = (val & 0x3Fllu) << 12;
1208           reloc |= ((size == 32
1209                      ? TILEGX_Y_ADDX_R0_R0_TP
1210                      : TILEGX_Y_ADD_R0_R0_TP)
1211                     & TILEGX_Y0_RRR_MASK);
1212           val &= ~R_TILEGX_IMM8_Y0_TLS_ADD_MASK;
1213         } else if (opt_t == tls::TLSOPT_TO_LE) {
1214           reloc = (val & 0x3Fllu) << 6;
1215           reloc |= (TILEGX_Y_MOVE_R0_R0 & TILEGX_Y0_RRR_SRCB_MASK);
1216           val &= ~R_TILEGX_IMM8_Y0_TLS_ADD_LE_MASK;
1217         } else
1218           gold_unreachable();
1219         break;
1220       case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
1221         if (opt_t == tls::TLSOPT_NONE) {
1222           reloc = (val & (0x3Fllu << 31)) << 12;
1223           reloc |= ((size == 32
1224                      ? TILEGX_Y_ADDX_R0_R0_TP
1225                      : TILEGX_Y_ADD_R0_R0_TP)
1226                     & TILEGX_Y1_RRR_MASK);
1227           val &= ~R_TILEGX_IMM8_Y1_TLS_ADD_MASK;
1228         } else if (opt_t == tls::TLSOPT_TO_LE) {
1229           reloc = (val & (0x3Fllu << 31)) << 6;
1230           reloc |= (TILEGX_Y_MOVE_R0_R0 & TILEGX_Y1_RRR_SRCB_MASK);
1231           val &= ~R_TILEGX_IMM8_Y1_TLS_ADD_LE_MASK;
1232         } else
1233           gold_unreachable();
1234         break;
1235       case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
1236         if (opt_t == tls::TLSOPT_NONE) {
1237           // GD see comments for optimize_tls_reloc
1238           reloc = TILEGX_X_MOVE_R0_R0 & TILEGX_X0_RRR_SRCB_MASK;
1239           val &= ~TILEGX_X0_RRR_SRCB_MASK;
1240         } else if (opt_t == tls::TLSOPT_TO_IE
1241                    || opt_t == tls::TLSOPT_TO_LE) {
1242           // IE/LE
1243           reloc = (size == 32
1244                    ? TILEGX_X_ADDX_R0_R0_TP
1245                    : TILEGX_X_ADD_R0_R0_TP)
1246                    & TILEGX_X0_RRR_SRCB_MASK;
1247           val &= ~TILEGX_X0_RRR_SRCB_MASK;
1248         }
1249         break;
1250       case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
1251         if (opt_t == tls::TLSOPT_NONE) {
1252           reloc = TILEGX_X_MOVE_R0_R0 & TILEGX_X1_RRR_SRCB_MASK;
1253           val &= ~TILEGX_X1_RRR_SRCB_MASK;
1254         } else if (opt_t == tls::TLSOPT_TO_IE
1255                    || opt_t == tls::TLSOPT_TO_LE) {
1256           reloc = (size == 32
1257                    ? TILEGX_X_ADDX_R0_R0_TP
1258                    : TILEGX_X_ADD_R0_R0_TP)
1259                    & TILEGX_X1_RRR_SRCB_MASK;
1260           val &= ~TILEGX_X1_RRR_SRCB_MASK;
1261         }
1262         break;
1263       case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
1264         if (opt_t == tls::TLSOPT_NONE) {
1265           reloc = TILEGX_Y_MOVE_R0_R0 & TILEGX_Y0_RRR_SRCB_MASK;
1266           val &= ~TILEGX_Y0_RRR_SRCB_MASK;
1267         } else if (opt_t == tls::TLSOPT_TO_IE
1268                    || opt_t == tls::TLSOPT_TO_LE) {
1269           reloc = (size == 32
1270                    ? TILEGX_Y_ADDX_R0_R0_TP
1271                    : TILEGX_Y_ADD_R0_R0_TP)
1272                    & TILEGX_Y0_RRR_SRCB_MASK;
1273           val &= ~TILEGX_Y0_RRR_SRCB_MASK;
1274         }
1275         break;
1276       case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
1277         if (opt_t == tls::TLSOPT_NONE) {
1278           reloc = TILEGX_Y_MOVE_R0_R0 & TILEGX_Y1_RRR_SRCB_MASK;
1279           val &= ~TILEGX_Y1_RRR_SRCB_MASK;
1280         } else if (opt_t == tls::TLSOPT_TO_IE
1281                    || opt_t == tls::TLSOPT_TO_LE) {
1282           reloc = (size == 32
1283                    ? TILEGX_Y_ADDX_R0_R0_TP
1284                    : TILEGX_Y_ADD_R0_R0_TP)
1285                    & TILEGX_Y1_RRR_SRCB_MASK;
1286           val &= ~TILEGX_Y1_RRR_SRCB_MASK;
1287         }
1288         break;
1289       case elfcpp::R_TILEGX_TLS_IE_LOAD:
1290         if (opt_t == tls::TLSOPT_NONE) {
1291           // IE
1292           reloc = (size == 32
1293                    ? TILEGX_X_LD4S
1294                    : TILEGX_X_LD)
1295                    & TILEGX_X1_RRR_SRCB_MASK;
1296           val &= ~TILEGX_X1_RRR_SRCB_MASK;
1297         } else if (opt_t == tls::TLSOPT_TO_LE) {
1298           // LE
1299           reloc = TILEGX_X_MOVE_R0_R0 & TILEGX_X1_RRR_SRCB_MASK;
1300           val &= ~TILEGX_X1_RRR_SRCB_MASK;
1301         } else
1302           gold_unreachable();
1303         break;
1304       case elfcpp::R_TILEGX_TLS_GD_CALL:
1305         if (opt_t == tls::TLSOPT_TO_IE) {
1306           // ld/ld4s r0, r0
1307           reloc = (size == 32
1308                   ? TILEGX_X_LD4S
1309                   : TILEGX_X_LD) & TILEGX_X1_FULL_MASK;
1310           val &= ~TILEGX_X1_FULL_MASK;
1311         } else if (opt_t == tls::TLSOPT_TO_LE) {
1312           // move r0, r0
1313           reloc = TILEGX_X_MOVE_R0_R0 & TILEGX_X1_FULL_MASK;
1314           val &= ~TILEGX_X1_FULL_MASK;
1315         } else
1316           // should be handled in ::relocate
1317           gold_unreachable();
1318         break;
1319       default:
1320         gold_unreachable();
1321         break;
1322     }
1323     elfcpp::Swap<64, big_endian>::writeval(wv, val | reloc);
1324   }
1325 };
1326
1327 template<>
1328 const Tilegx_relocate_functions<64, false>::Tilegx_howto
1329 Tilegx_relocate_functions<64, false>::howto[elfcpp::R_TILEGX_NUM] =
1330 {
1331   {  0,  0, 0,  0, 0}, // R_TILEGX_NONE
1332   {  0,  0, 0, 64, 0}, // R_TILEGX_64
1333   {  0,  0, 0, 32, 0}, // R_TILEGX_32
1334   {  0,  0, 0, 16, 0}, // R_TILEGX_16
1335   {  0,  0, 0,  8, 0}, // R_TILEGX_8
1336   {  0,  0, 1, 64, 0}, // R_TILEGX_64_PCREL
1337   {  0,  0, 1, 32, 0}, // R_TILEGX_32_PCREL
1338   {  0,  0, 1, 16, 0}, // R_TILEGX_16_PCREL
1339   {  0,  0, 1,  8, 0}, // R_TILEGX_8_PCREL
1340   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0
1341   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1
1342   { 32,  0, 0,  0, 0}, // R_TILEGX_HW2
1343   { 48,  0, 0,  0, 0}, // R_TILEGX_HW3
1344   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0_LAST
1345   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1_LAST
1346   { 32,  0, 0,  0, 0}, // R_TILEGX_HW2_LAST
1347   {  0,  0, 0,  0, 0}, // R_TILEGX_COPY
1348   {  0,  0, 0,  8, 0}, // R_TILEGX_GLOB_DAT
1349   {  0,  0, 0,  0, 0}, // R_TILEGX_JMP_SLOT
1350   {  0,  0, 0,  0, 0}, // R_TILEGX_RELATIVE
1351   {  3,  1, 1,  0, 0}, // R_TILEGX_BROFF_X1
1352   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1
1353   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1_PLT
1354   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X0
1355   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y0
1356   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X1
1357   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y1
1358   {  0,  1, 0,  8, 0}, // R_TILEGX_DEST_IMM8_X1
1359   {  0,  1, 0,  8, 0}, // R_TILEGX_MT_IMM14_X1
1360   {  0,  1, 0,  8, 0}, // R_TILEGX_MF_IMM14_X1
1361   {  0,  1, 0,  8, 0}, // R_TILEGX_MMSTART_X0
1362   {  0,  1, 0,  8, 0}, // R_TILEGX_MMEND_X0
1363   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X0
1364   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X1
1365   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y0
1366   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y1
1367   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0
1368   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0
1369   { 16, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW1
1370   { 16, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW1
1371   { 32, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW2
1372   { 32, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW2
1373   { 48, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW3
1374   { 48, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW3
1375   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST
1376   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST
1377   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST
1378   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST
1379   { 32, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST
1380   { 32, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST
1381   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PCREL
1382   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PCREL
1383   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PCREL
1384   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PCREL
1385   { 32, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PCREL
1386   { 32, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PCREL
1387   { 48, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW3_PCREL
1388   { 48, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW3_PCREL
1389   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PCREL
1390   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PCREL
1391   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PCREL
1392   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PCREL
1393   { 32, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PCREL
1394   { 32, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PCREL
1395   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_GOT
1396   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_GOT
1397   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PLT_PCREL
1398   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PLT_PCREL
1399   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PLT_PCREL
1400   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PLT_PCREL
1401   { 32, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PLT_PCREL
1402   { 32, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PLT_PCREL
1403   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_GOT
1404   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_GOT
1405   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_GOT
1406   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_GOT
1407   { 32, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_GOT
1408   { 32, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_GOT
1409   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_GD
1410   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_GD
1411   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_LE
1412   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_LE
1413   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE
1414   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE
1415   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE
1416   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE
1417   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD
1418   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD
1419   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD
1420   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD
1421   {  0,  0, 0,  0, 0}, // R_TILEGX_IRELATIVE
1422   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1423   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_IE
1424   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_IE
1425   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL
1426   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL
1427   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL
1428   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL
1429   { 32, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL
1430   { 32, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL
1431   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE
1432   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE
1433   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE
1434   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE
1435   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1436   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1437   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD64
1438   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF64
1439   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF64
1440   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD32
1441   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF32
1442   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF32
1443   {  3, 31, 1, 27, 0}, // R_TILEGX_TLS_GD_CALL
1444   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_GD_ADD
1445   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_GD_ADD
1446   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_GD_ADD
1447   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_GD_ADD
1448   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_IE_LOAD
1449   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_ADD
1450   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_ADD
1451   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_ADD
1452   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_ADD
1453   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTINHERIT
1454   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTENTRY
1455 };
1456
1457 template<>
1458 const Tilegx_relocate_functions<32, false>::Tilegx_howto
1459 Tilegx_relocate_functions<32, false>::howto[elfcpp::R_TILEGX_NUM] =
1460 {
1461   {  0,  0, 0,  0, 0}, // R_TILEGX_NONE
1462   {  0,  0, 0, 64, 0}, // R_TILEGX_64
1463   {  0,  0, 0, 32, 0}, // R_TILEGX_32
1464   {  0,  0, 0, 16, 0}, // R_TILEGX_16
1465   {  0,  0, 0,  8, 0}, // R_TILEGX_8
1466   {  0,  0, 1, 64, 0}, // R_TILEGX_64_PCREL
1467   {  0,  0, 1, 32, 0}, // R_TILEGX_32_PCREL
1468   {  0,  0, 1, 16, 0}, // R_TILEGX_16_PCREL
1469   {  0,  0, 1,  8, 0}, // R_TILEGX_8_PCREL
1470   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0
1471   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1
1472   { 31,  0, 0,  0, 0}, // R_TILEGX_HW2
1473   { 31,  0, 0,  0, 0}, // R_TILEGX_HW3
1474   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0_LAST
1475   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1_LAST
1476   { 31,  0, 0,  0, 0}, // R_TILEGX_HW2_LAST
1477   {  0,  0, 0,  0, 0}, // R_TILEGX_COPY
1478   {  0,  0, 0,  8, 0}, // R_TILEGX_GLOB_DAT
1479   {  0,  0, 0,  0, 0}, // R_TILEGX_JMP_SLOT
1480   {  0,  0, 0,  0, 0}, // R_TILEGX_RELATIVE
1481   {  3,  1, 1,  0, 0}, // R_TILEGX_BROFF_X1
1482   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1
1483   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1_PLT
1484   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X0
1485   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y0
1486   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X1
1487   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y1
1488   {  0,  1, 0,  8, 0}, // R_TILEGX_DEST_IMM8_X1
1489   {  0,  1, 0,  8, 0}, // R_TILEGX_MT_IMM14_X1
1490   {  0,  1, 0,  8, 0}, // R_TILEGX_MF_IMM14_X1
1491   {  0,  1, 0,  8, 0}, // R_TILEGX_MMSTART_X0
1492   {  0,  1, 0,  8, 0}, // R_TILEGX_MMEND_X0
1493   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X0
1494   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X1
1495   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y0
1496   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y1
1497   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0
1498   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0
1499   { 16, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW1
1500   { 16, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW1
1501   { 31, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW2
1502   { 31, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW2
1503   { 31, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW3
1504   { 31, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW3
1505   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST
1506   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST
1507   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST
1508   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST
1509   { 31, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST
1510   { 31, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST
1511   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PCREL
1512   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PCREL
1513   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PCREL
1514   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PCREL
1515   { 31, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PCREL
1516   { 31, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PCREL
1517   { 31, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW3_PCREL
1518   { 31, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW3_PCREL
1519   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PCREL
1520   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PCREL
1521   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PCREL
1522   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PCREL
1523   { 31, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PCREL
1524   { 31, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PCREL
1525   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_GOT
1526   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_GOT
1527   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PLT_PCREL
1528   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PLT_PCREL
1529   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PLT_PCREL
1530   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PLT_PCREL
1531   { 31, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PLT_PCREL
1532   { 31, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PLT_PCREL
1533   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_GOT
1534   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_GOT
1535   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_GOT
1536   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_GOT
1537   { 31, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_GOT
1538   { 31, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_GOT
1539   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_GD
1540   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_GD
1541   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_LE
1542   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_LE
1543   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE
1544   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE
1545   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE
1546   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE
1547   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD
1548   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD
1549   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD
1550   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD
1551   {  0,  0, 0,  0, 0}, // R_TILEGX_IRELATIVE
1552   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1553   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_IE
1554   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_IE
1555   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL
1556   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL
1557   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL
1558   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL
1559   { 31, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL
1560   { 31, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL
1561   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE
1562   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE
1563   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE
1564   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE
1565   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1566   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1567   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD64
1568   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF64
1569   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF64
1570   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD32
1571   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF32
1572   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF32
1573   {  3, 31, 1, 27, 0}, // R_TILEGX_TLS_GD_CALL
1574   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_GD_ADD
1575   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_GD_ADD
1576   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_GD_ADD
1577   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_GD_ADD
1578   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_IE_LOAD
1579   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_ADD
1580   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_ADD
1581   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_ADD
1582   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_ADD
1583   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTINHERIT
1584   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTENTRY
1585 };
1586
1587 template<>
1588 const Tilegx_relocate_functions<64, true>::Tilegx_howto
1589 Tilegx_relocate_functions<64, true>::howto[elfcpp::R_TILEGX_NUM] =
1590 {
1591   {  0,  0, 0,  0, 0}, // R_TILEGX_NONE
1592   {  0,  0, 0, 64, 0}, // R_TILEGX_64
1593   {  0,  0, 0, 32, 0}, // R_TILEGX_32
1594   {  0,  0, 0, 16, 0}, // R_TILEGX_16
1595   {  0,  0, 0,  8, 0}, // R_TILEGX_8
1596   {  0,  0, 1, 64, 0}, // R_TILEGX_64_PCREL
1597   {  0,  0, 1, 32, 0}, // R_TILEGX_32_PCREL
1598   {  0,  0, 1, 16, 0}, // R_TILEGX_16_PCREL
1599   {  0,  0, 1,  8, 0}, // R_TILEGX_8_PCREL
1600   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0
1601   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1
1602   { 32,  0, 0,  0, 0}, // R_TILEGX_HW2
1603   { 48,  0, 0,  0, 0}, // R_TILEGX_HW3
1604   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0_LAST
1605   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1_LAST
1606   { 32,  0, 0,  0, 0}, // R_TILEGX_HW2_LAST
1607   {  0,  0, 0,  0, 0}, // R_TILEGX_COPY
1608   {  0,  0, 0,  8, 0}, // R_TILEGX_GLOB_DAT
1609   {  0,  0, 0,  0, 0}, // R_TILEGX_JMP_SLOT
1610   {  0,  0, 0,  0, 0}, // R_TILEGX_RELATIVE
1611   {  3,  1, 1,  0, 0}, // R_TILEGX_BROFF_X1
1612   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1
1613   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1_PLT
1614   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X0
1615   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y0
1616   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X1
1617   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y1
1618   {  0,  1, 0,  8, 0}, // R_TILEGX_DEST_IMM8_X1
1619   {  0,  1, 0,  8, 0}, // R_TILEGX_MT_IMM14_X1
1620   {  0,  1, 0,  8, 0}, // R_TILEGX_MF_IMM14_X1
1621   {  0,  1, 0,  8, 0}, // R_TILEGX_MMSTART_X0
1622   {  0,  1, 0,  8, 0}, // R_TILEGX_MMEND_X0
1623   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X0
1624   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X1
1625   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y0
1626   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y1
1627   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0
1628   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0
1629   { 16, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW1
1630   { 16, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW1
1631   { 32, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW2
1632   { 32, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW2
1633   { 48, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW3
1634   { 48, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW3
1635   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST
1636   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST
1637   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST
1638   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST
1639   { 32, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST
1640   { 32, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST
1641   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PCREL
1642   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PCREL
1643   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PCREL
1644   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PCREL
1645   { 32, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PCREL
1646   { 32, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PCREL
1647   { 48, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW3_PCREL
1648   { 48, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW3_PCREL
1649   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PCREL
1650   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PCREL
1651   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PCREL
1652   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PCREL
1653   { 32, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PCREL
1654   { 32, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PCREL
1655   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_GOT
1656   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_GOT
1657   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PLT_PCREL
1658   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PLT_PCREL
1659   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PLT_PCREL
1660   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PLT_PCREL
1661   { 32, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PLT_PCREL
1662   { 32, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PLT_PCREL
1663   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_GOT
1664   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_GOT
1665   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_GOT
1666   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_GOT
1667   { 32, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_GOT
1668   { 32, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_GOT
1669   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_GD
1670   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_GD
1671   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_LE
1672   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_LE
1673   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE
1674   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE
1675   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE
1676   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE
1677   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD
1678   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD
1679   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD
1680   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD
1681   {  0,  0, 0,  0, 0}, // R_TILEGX_IRELATIVE
1682   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1683   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_IE
1684   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_IE
1685   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL
1686   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL
1687   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL
1688   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL
1689   { 32, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL
1690   { 32, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL
1691   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE
1692   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE
1693   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE
1694   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE
1695   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1696   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1697   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD64
1698   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF64
1699   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF64
1700   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD32
1701   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF32
1702   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF32
1703   {  3, 31, 1, 27, 0}, // R_TILEGX_TLS_GD_CALL
1704   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_GD_ADD
1705   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_GD_ADD
1706   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_GD_ADD
1707   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_GD_ADD
1708   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_IE_LOAD
1709   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_ADD
1710   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_ADD
1711   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_ADD
1712   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_ADD
1713   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTINHERIT
1714   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTENTRY
1715 };
1716
1717 template<>
1718 const Tilegx_relocate_functions<32, true>::Tilegx_howto
1719 Tilegx_relocate_functions<32, true>::howto[elfcpp::R_TILEGX_NUM] =
1720 {
1721   {  0,  0, 0,  0, 0}, // R_TILEGX_NONE
1722   {  0,  0, 0, 64, 0}, // R_TILEGX_64
1723   {  0,  0, 0, 32, 0}, // R_TILEGX_32
1724   {  0,  0, 0, 16, 0}, // R_TILEGX_16
1725   {  0,  0, 0,  8, 0}, // R_TILEGX_8
1726   {  0,  0, 1, 64, 0}, // R_TILEGX_64_PCREL
1727   {  0,  0, 1, 32, 0}, // R_TILEGX_32_PCREL
1728   {  0,  0, 1, 16, 0}, // R_TILEGX_16_PCREL
1729   {  0,  0, 1,  8, 0}, // R_TILEGX_8_PCREL
1730   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0
1731   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1
1732   { 31,  0, 0,  0, 0}, // R_TILEGX_HW2
1733   { 31,  0, 0,  0, 0}, // R_TILEGX_HW3
1734   {  0,  0, 0,  0, 0}, // R_TILEGX_HW0_LAST
1735   { 16,  0, 0,  0, 0}, // R_TILEGX_HW1_LAST
1736   { 31,  0, 0,  0, 0}, // R_TILEGX_HW2_LAST
1737   {  0,  0, 0,  0, 0}, // R_TILEGX_COPY
1738   {  0,  0, 0,  8, 0}, // R_TILEGX_GLOB_DAT
1739   {  0,  0, 0,  0, 0}, // R_TILEGX_JMP_SLOT
1740   {  0,  0, 0,  0, 0}, // R_TILEGX_RELATIVE
1741   {  3,  1, 1,  0, 0}, // R_TILEGX_BROFF_X1
1742   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1
1743   {  3, 31, 1, 27, 0}, // R_TILEGX_JUMPOFF_X1_PLT
1744   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X0
1745   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y0
1746   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_X1
1747   {  0,  1, 0,  8, 0}, // R_TILEGX_IMM8_Y1
1748   {  0,  1, 0,  8, 0}, // R_TILEGX_DEST_IMM8_X1
1749   {  0,  1, 0,  8, 0}, // R_TILEGX_MT_IMM14_X1
1750   {  0,  1, 0,  8, 0}, // R_TILEGX_MF_IMM14_X1
1751   {  0,  1, 0,  8, 0}, // R_TILEGX_MMSTART_X0
1752   {  0,  1, 0,  8, 0}, // R_TILEGX_MMEND_X0
1753   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X0
1754   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_X1
1755   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y0
1756   {  0,  1, 0,  8, 0}, // R_TILEGX_SHAMT_Y1
1757   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0
1758   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0
1759   { 16, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW1
1760   { 16, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW1
1761   { 31, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW2
1762   { 31, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW2
1763   { 31, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW3
1764   { 31, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW3
1765   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST
1766   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST
1767   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST
1768   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST
1769   { 31, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST
1770   { 31, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST
1771   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PCREL
1772   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PCREL
1773   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PCREL
1774   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PCREL
1775   { 31, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PCREL
1776   { 31, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PCREL
1777   { 31, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW3_PCREL
1778   { 31, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW3_PCREL
1779   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PCREL
1780   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PCREL
1781   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PCREL
1782   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PCREL
1783   { 31, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PCREL
1784   { 31, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PCREL
1785   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_GOT
1786   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_GOT
1787   {  0, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW0_PLT_PCREL
1788   {  0, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW0_PLT_PCREL
1789   { 16, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW1_PLT_PCREL
1790   { 16, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW1_PLT_PCREL
1791   { 31, 12, 1, 16, 0}, // R_TILEGX_IMM16_X0_HW2_PLT_PCREL
1792   { 31, 43, 1, 16, 0}, // R_TILEGX_IMM16_X1_HW2_PLT_PCREL
1793   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_GOT
1794   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_GOT
1795   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_GOT
1796   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_GOT
1797   { 31, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_GOT
1798   { 31, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_GOT
1799   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_GD
1800   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_GD
1801   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_LE
1802   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_LE
1803   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE
1804   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE
1805   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE
1806   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE
1807   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD
1808   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD
1809   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD
1810   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD
1811   {  0,  0, 0,  0, 0}, // R_TILEGX_IRELATIVE
1812   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1813   {  0, 12, 0, 16, 0}, // R_TILEGX_IMM16_X0_HW0_TLS_IE
1814   {  0, 43, 0, 16, 0}, // R_TILEGX_IMM16_X1_HW0_TLS_IE
1815   {  0, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL
1816   {  0, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL
1817   { 16, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL
1818   { 16, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL
1819   { 31, 12, 1, 16, 1}, // R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL
1820   { 31, 43, 1, 16, 1}, // R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL
1821   {  0, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE
1822   {  0, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE
1823   { 16, 12, 0, 16, 1}, // R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE
1824   { 16, 43, 0, 16, 1}, // R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE
1825   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1826   {  0,  0, 0,  0, 0}, // R_TILEGX_INVALID
1827   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD64
1828   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF64
1829   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF64
1830   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPMOD32
1831   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_DTPOFF32
1832   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_TPOFF32
1833   {  3, 31, 1, 27, 0}, // R_TILEGX_TLS_GD_CALL
1834   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_GD_ADD
1835   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_GD_ADD
1836   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_GD_ADD
1837   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_GD_ADD
1838   {  0,  0, 0,  0, 0}, // R_TILEGX_TLS_IE_LOAD
1839   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X0_TLS_ADD
1840   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_X1_TLS_ADD
1841   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y0_TLS_ADD
1842   {  0,  0, 0,  0, 0}, // R_TILEGX_IMM8_Y1_TLS_ADD
1843   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTINHERIT
1844   {  0,  0, 0,  0, 0}, // R_TILEGX_GNU_VTENTRY
1845 };
1846
1847 // Get the GOT section, creating it if necessary.
1848
1849 template<int size, bool big_endian>
1850 Output_data_got<size, big_endian>*
1851 Target_tilegx<size, big_endian>::got_section(Symbol_table* symtab,
1852                                              Layout* layout)
1853 {
1854   if (this->got_ == NULL)
1855     {
1856       gold_assert(symtab != NULL && layout != NULL);
1857
1858       // When using -z now, we can treat .got.plt as a relro section.
1859       // Without -z now, it is modified after program startup by lazy
1860       // PLT relocations.
1861       bool is_got_plt_relro = parameters->options().now();
1862       Output_section_order got_order = (is_got_plt_relro
1863                                         ? ORDER_RELRO
1864                                         : ORDER_RELRO_LAST);
1865       Output_section_order got_plt_order = (is_got_plt_relro
1866                                             ? ORDER_RELRO
1867                                             : ORDER_NON_RELRO_FIRST);
1868
1869       this->got_ = new Output_data_got<size, big_endian>();
1870
1871       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1872                                       (elfcpp::SHF_ALLOC
1873                                        | elfcpp::SHF_WRITE),
1874                                       this->got_, got_order, true);
1875
1876       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1877       this->global_offset_table_ =
1878         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1879                                       Symbol_table::PREDEFINED,
1880                                       this->got_,
1881                                       0, 0, elfcpp::STT_OBJECT,
1882                                       elfcpp::STB_LOCAL,
1883                                       elfcpp::STV_HIDDEN, 0,
1884                                       false, false);
1885
1886       if (parameters->options().shared()) {
1887         // we need to keep the address of .dynamic section in the
1888         // first got entry for .so
1889         this->tilegx_dynamic_ =
1890           symtab->define_in_output_data("_TILEGX_DYNAMIC_", NULL,
1891                                         Symbol_table::PREDEFINED,
1892                                         layout->dynamic_section(),
1893                                         0, 0, elfcpp::STT_OBJECT,
1894                                         elfcpp::STB_LOCAL,
1895                                         elfcpp::STV_HIDDEN, 0,
1896                                         false, false);
1897
1898         this->got_->add_global(this->tilegx_dynamic_, GOT_TYPE_STANDARD);
1899       } else
1900         // for executable, just set the first entry to zero.
1901         this->got_->set_current_data_size(size / 8);
1902
1903       this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
1904       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1905                                       (elfcpp::SHF_ALLOC
1906                                        | elfcpp::SHF_WRITE),
1907                                       this->got_plt_, got_plt_order,
1908                                       is_got_plt_relro);
1909
1910       // The first two entries are reserved.
1911       this->got_plt_->set_current_data_size
1912              (TILEGX_GOTPLT_RESERVE_COUNT * (size / 8));
1913
1914       if (!is_got_plt_relro)
1915         {
1916           // Those bytes can go into the relro segment.
1917           layout->increase_relro(size / 8);
1918         }
1919
1920
1921       // If there are any IRELATIVE relocations, they get GOT entries
1922       // in .got.plt after the jump slot entries.
1923       this->got_irelative_
1924          = new Output_data_space(size / 8, "** GOT IRELATIVE PLT");
1925       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1926                                       (elfcpp::SHF_ALLOC
1927                                        | elfcpp::SHF_WRITE),
1928                                       this->got_irelative_,
1929                                       got_plt_order, is_got_plt_relro);
1930     }
1931
1932   return this->got_;
1933 }
1934
1935 // Get the dynamic reloc section, creating it if necessary.
1936
1937 template<int size, bool big_endian>
1938 typename Target_tilegx<size, big_endian>::Reloc_section*
1939 Target_tilegx<size, big_endian>::rela_dyn_section(Layout* layout)
1940 {
1941   if (this->rela_dyn_ == NULL)
1942     {
1943       gold_assert(layout != NULL);
1944       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1945       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1946                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
1947                                       ORDER_DYNAMIC_RELOCS, false);
1948     }
1949   return this->rela_dyn_;
1950 }
1951
1952 // Get the section to use for IRELATIVE relocs, creating it if
1953 // necessary.  These go in .rela.dyn, but only after all other dynamic
1954 // relocations.  They need to follow the other dynamic relocations so
1955 // that they can refer to global variables initialized by those
1956 // relocs.
1957
1958 template<int size, bool big_endian>
1959 typename Target_tilegx<size, big_endian>::Reloc_section*
1960 Target_tilegx<size, big_endian>::rela_irelative_section(Layout* layout)
1961 {
1962   if (this->rela_irelative_ == NULL)
1963     {
1964       // Make sure we have already created the dynamic reloc section.
1965       this->rela_dyn_section(layout);
1966       this->rela_irelative_ = new Reloc_section(false);
1967       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1968                                       elfcpp::SHF_ALLOC, this->rela_irelative_,
1969                                       ORDER_DYNAMIC_RELOCS, false);
1970       gold_assert(this->rela_dyn_->output_section()
1971                   == this->rela_irelative_->output_section());
1972     }
1973   return this->rela_irelative_;
1974 }
1975
1976 // Initialize the PLT section.
1977
1978 template<int size, bool big_endian>
1979 void
1980 Output_data_plt_tilegx<size, big_endian>::init(Layout* layout)
1981 {
1982   this->rel_ = new Reloc_section(false);
1983   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1984                                   elfcpp::SHF_ALLOC, this->rel_,
1985                                   ORDER_DYNAMIC_PLT_RELOCS, false);
1986 }
1987
1988 template<int size, bool big_endian>
1989 void
1990 Output_data_plt_tilegx<size, big_endian>::do_adjust_output_section(
1991   Output_section* os)
1992 {
1993   os->set_entsize(this->get_plt_entry_size());
1994 }
1995
1996 // Add an entry to the PLT.
1997
1998 template<int size, bool big_endian>
1999 void
2000 Output_data_plt_tilegx<size, big_endian>::add_entry(Symbol_table* symtab,
2001   Layout* layout, Symbol* gsym)
2002 {
2003   gold_assert(!gsym->has_plt_offset());
2004
2005   unsigned int plt_index;
2006   off_t plt_offset;
2007   section_offset_type got_offset;
2008
2009   unsigned int* pcount;
2010   unsigned int reserved;
2011   Output_data_space* got;
2012   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2013       && gsym->can_use_relative_reloc(false))
2014     {
2015       pcount = &this->irelative_count_;
2016       reserved = 0;
2017       got = this->got_irelative_;
2018     }
2019   else
2020     {
2021       pcount = &this->count_;
2022       reserved = TILEGX_GOTPLT_RESERVE_COUNT;
2023       got = this->got_plt_;
2024     }
2025
2026   if (!this->is_data_size_valid())
2027     {
2028       plt_index = *pcount;
2029
2030       // TILEGX .plt section layout
2031       //
2032       //  ----
2033       //   plt_header
2034       //  ----
2035       //   plt stub
2036       //  ----
2037       //   ...
2038       //  ----
2039       //
2040       // TILEGX .got.plt section layout
2041       //
2042       //  ----
2043       //  reserv1
2044       //  ----
2045       //  reserv2
2046       //  ----
2047       //   entries for normal function
2048       //  ----
2049       //   ...
2050       //  ----
2051       //   entries for ifunc
2052       //  ----
2053       //   ...
2054       //  ----
2055       if (got == this->got_irelative_)
2056         plt_offset = plt_index * this->get_plt_entry_size();
2057       else
2058         plt_offset = (plt_index + 1) * this->get_plt_entry_size();
2059
2060       ++*pcount;
2061
2062       got_offset = (plt_index + reserved) * (size / 8);
2063       gold_assert(got_offset == got->current_data_size());
2064
2065       // Every PLT entry needs a GOT entry which points back to the PLT
2066       // entry (this will be changed by the dynamic linker, normally
2067       // lazily when the function is called).
2068       got->set_current_data_size(got_offset + size / 8);
2069     }
2070   else
2071     {
2072       // FIXME: This is probably not correct for IRELATIVE relocs.
2073
2074       // For incremental updates, find an available slot.
2075       plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
2076                                              this->get_plt_entry_size(), 0);
2077       if (plt_offset == -1)
2078         gold_fallback(_("out of patch space (PLT);"
2079                         " relink with --incremental-full"));
2080
2081       // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
2082       // can be calculated from the PLT index, adjusting for the three
2083       // reserved entries at the beginning of the GOT.
2084       plt_index = plt_offset / this->get_plt_entry_size() - 1;
2085       got_offset = (plt_index + reserved) * (size / 8);
2086     }
2087
2088   gsym->set_plt_offset(plt_offset);
2089
2090   // Every PLT entry needs a reloc.
2091   this->add_relocation(symtab, layout, gsym, got_offset);
2092
2093   // Note that we don't need to save the symbol.  The contents of the
2094   // PLT are independent of which symbols are used.  The symbols only
2095   // appear in the relocations.
2096 }
2097
2098 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
2099 // the PLT offset.
2100
2101 template<int size, bool big_endian>
2102 unsigned int
2103 Output_data_plt_tilegx<size, big_endian>::add_local_ifunc_entry(
2104     Symbol_table* symtab,
2105     Layout* layout,
2106     Sized_relobj_file<size, big_endian>* relobj,
2107     unsigned int local_sym_index)
2108 {
2109   unsigned int plt_offset =
2110     this->irelative_count_ * this->get_plt_entry_size();
2111   ++this->irelative_count_;
2112
2113   section_offset_type got_offset = this->got_irelative_->current_data_size();
2114
2115   // Every PLT entry needs a GOT entry which points back to the PLT
2116   // entry.
2117   this->got_irelative_->set_current_data_size(got_offset + size / 8);
2118
2119   // Every PLT entry needs a reloc.
2120   Reloc_section* rela = this->rela_irelative(symtab, layout);
2121   rela->add_symbolless_local_addend(relobj, local_sym_index,
2122                                     elfcpp::R_TILEGX_IRELATIVE,
2123                                     this->got_irelative_, got_offset, 0);
2124
2125   return plt_offset;
2126 }
2127
2128 // Add the relocation for a PLT entry.
2129
2130 template<int size, bool big_endian>
2131 void
2132 Output_data_plt_tilegx<size, big_endian>::add_relocation(Symbol_table* symtab,
2133                                              Layout* layout,
2134                                              Symbol* gsym,
2135                                              unsigned int got_offset)
2136 {
2137   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2138       && gsym->can_use_relative_reloc(false))
2139     {
2140       Reloc_section* rela = this->rela_irelative(symtab, layout);
2141       rela->add_symbolless_global_addend(gsym, elfcpp::R_TILEGX_IRELATIVE,
2142                                          this->got_irelative_, got_offset, 0);
2143     }
2144   else
2145     {
2146       gsym->set_needs_dynsym_entry();
2147       this->rel_->add_global(gsym, elfcpp::R_TILEGX_JMP_SLOT, this->got_plt_,
2148                              got_offset, 0);
2149     }
2150 }
2151
2152 // Return where the IRELATIVE relocations should go in the PLT.  These
2153 // follow the JUMP_SLOT and the TLSDESC relocations.
2154
2155 template<int size, bool big_endian>
2156 typename Output_data_plt_tilegx<size, big_endian>::Reloc_section*
2157 Output_data_plt_tilegx<size, big_endian>::rela_irelative(Symbol_table* symtab,
2158                                                          Layout* layout)
2159 {
2160   if (this->irelative_rel_ == NULL)
2161     {
2162       // case we see any later on.
2163       this->irelative_rel_ = new Reloc_section(false);
2164       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
2165                                       elfcpp::SHF_ALLOC, this->irelative_rel_,
2166                                       ORDER_DYNAMIC_PLT_RELOCS, false);
2167       gold_assert(this->irelative_rel_->output_section()
2168                   == this->rel_->output_section());
2169
2170       if (parameters->doing_static_link())
2171         {
2172           // A statically linked executable will only have a .rela.plt
2173           // section to hold R_TILEGX_IRELATIVE relocs for
2174           // STT_GNU_IFUNC symbols.  The library will use these
2175           // symbols to locate the IRELATIVE relocs at program startup
2176           // time.
2177           symtab->define_in_output_data("__rela_iplt_start", NULL,
2178                                         Symbol_table::PREDEFINED,
2179                                         this->irelative_rel_, 0, 0,
2180                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2181                                         elfcpp::STV_HIDDEN, 0, false, true);
2182           symtab->define_in_output_data("__rela_iplt_end", NULL,
2183                                         Symbol_table::PREDEFINED,
2184                                         this->irelative_rel_, 0, 0,
2185                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2186                                         elfcpp::STV_HIDDEN, 0, true, true);
2187         }
2188     }
2189   return this->irelative_rel_;
2190 }
2191
2192 // Return the PLT address to use for a global symbol.
2193
2194 template<int size, bool big_endian>
2195 uint64_t
2196 Output_data_plt_tilegx<size, big_endian>::address_for_global(
2197   const Symbol* gsym)
2198 {
2199   uint64_t offset = 0;
2200   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2201       && gsym->can_use_relative_reloc(false))
2202     offset = (this->count_ + 1) * this->get_plt_entry_size();
2203   return this->address() + offset + gsym->plt_offset();
2204 }
2205
2206 // Return the PLT address to use for a local symbol.  These are always
2207 // IRELATIVE relocs.
2208
2209 template<int size, bool big_endian>
2210 uint64_t
2211 Output_data_plt_tilegx<size, big_endian>::address_for_local(
2212     const Relobj* object,
2213     unsigned int r_sym)
2214 {
2215   return (this->address()
2216           + (this->count_ + 1) * this->get_plt_entry_size()
2217           + object->local_plt_offset(r_sym));
2218 }
2219
2220 // Set the final size.
2221 template<int size, bool big_endian>
2222 void
2223 Output_data_plt_tilegx<size, big_endian>::set_final_data_size()
2224 {
2225   unsigned int count = this->count_ + this->irelative_count_;
2226   this->set_data_size((count + 1) * this->get_plt_entry_size());
2227 }
2228
2229 // The first entry in the PLT for an executable.
2230 template<>
2231 const unsigned char
2232 Output_data_plt_tilegx<64, false>::first_plt_entry[plt_entry_size] =
2233 {
2234   0x00, 0x30, 0x48, 0x51,
2235   0x6e, 0x43, 0xa0, 0x18, // { ld_add r28, r27, 8 }
2236   0x00, 0x30, 0xbc, 0x35,
2237   0x00, 0x40, 0xde, 0x9e, // { ld r27, r27 }
2238   0xff, 0xaf, 0x30, 0x40,
2239   0x60, 0x73, 0x6a, 0x28, // { info 10 ; jr r27 }
2240   // padding
2241   0x00, 0x00, 0x00, 0x00,
2242   0x00, 0x00, 0x00, 0x00,
2243   0x00, 0x00, 0x00, 0x00,
2244   0x00, 0x00, 0x00, 0x00
2245 };
2246
2247 template<>
2248 const unsigned char
2249 Output_data_plt_tilegx<32, false>::first_plt_entry[plt_entry_size] =
2250 {
2251   0x00, 0x30, 0x48, 0x51,
2252   0x6e, 0x23, 0x58, 0x18, // { ld4s_add r28, r27, 4 }
2253   0x00, 0x30, 0xbc, 0x35,
2254   0x00, 0x40, 0xde, 0x9c, // { ld4s r27, r27 }
2255   0xff, 0xaf, 0x30, 0x40,
2256   0x60, 0x73, 0x6a, 0x28, // { info 10 ; jr r27 }
2257   // padding
2258   0x00, 0x00, 0x00, 0x00,
2259   0x00, 0x00, 0x00, 0x00,
2260   0x00, 0x00, 0x00, 0x00,
2261   0x00, 0x00, 0x00, 0x00
2262 };
2263
2264 template<>
2265 const unsigned char
2266 Output_data_plt_tilegx<64, true>::first_plt_entry[plt_entry_size] =
2267 {
2268   0x00, 0x30, 0x48, 0x51,
2269   0x6e, 0x43, 0xa0, 0x18, // { ld_add r28, r27, 8 }
2270   0x00, 0x30, 0xbc, 0x35,
2271   0x00, 0x40, 0xde, 0x9e, // { ld r27, r27 }
2272   0xff, 0xaf, 0x30, 0x40,
2273   0x60, 0x73, 0x6a, 0x28, // { info 10 ; jr r27 }
2274   // padding
2275   0x00, 0x00, 0x00, 0x00,
2276   0x00, 0x00, 0x00, 0x00,
2277   0x00, 0x00, 0x00, 0x00,
2278   0x00, 0x00, 0x00, 0x00
2279 };
2280
2281 template<>
2282 const unsigned char
2283 Output_data_plt_tilegx<32, true>::first_plt_entry[plt_entry_size] =
2284 {
2285   0x00, 0x30, 0x48, 0x51,
2286   0x6e, 0x23, 0x58, 0x18, // { ld4s_add r28, r27, 4 }
2287   0x00, 0x30, 0xbc, 0x35,
2288   0x00, 0x40, 0xde, 0x9c, // { ld4s r27, r27 }
2289   0xff, 0xaf, 0x30, 0x40,
2290   0x60, 0x73, 0x6a, 0x28, // { info 10 ; jr r27 }
2291   // padding
2292   0x00, 0x00, 0x00, 0x00,
2293   0x00, 0x00, 0x00, 0x00,
2294   0x00, 0x00, 0x00, 0x00,
2295   0x00, 0x00, 0x00, 0x00
2296 };
2297
2298 template<int size, bool big_endian>
2299 void
2300 Output_data_plt_tilegx<size, big_endian>::fill_first_plt_entry(
2301   unsigned char* pov)
2302 {
2303   memcpy(pov, first_plt_entry, plt_entry_size);
2304 }
2305
2306 // Subsequent entries in the PLT for an executable.
2307
2308 template<>
2309 const unsigned char
2310 Output_data_plt_tilegx<64, false>::plt_entry[plt_entry_size] =
2311 {
2312   0xdc, 0x0f, 0x00, 0x10,
2313   0x0d, 0xf0, 0x6a, 0x28, // { moveli r28, 0 ; lnk r26 }
2314   0xdb, 0x0f, 0x00, 0x10,
2315   0x8e, 0x03, 0x00, 0x38, // { moveli r27, 0 ; shl16insli r28, r28, 0 }
2316   0x9c, 0xc6, 0x0d, 0xd0,
2317   0x6d, 0x03, 0x00, 0x38, // { add r28, r26, r28 ; shl16insli r27, r27, 0 }
2318   0x9b, 0xb6, 0xc5, 0xad,
2319   0xff, 0x57, 0xe0, 0x8e, // { add r27, r26, r27 ; info 10 ; ld r28, r28 }
2320   0xdd, 0x0f, 0x00, 0x70,
2321   0x80, 0x73, 0x6a, 0x28, // { shl16insli r29, zero, 0 ; jr r28 }
2322
2323 };
2324
2325 template<>
2326 const unsigned char
2327 Output_data_plt_tilegx<32, false>::plt_entry[plt_entry_size] =
2328 {
2329   0xdc, 0x0f, 0x00, 0x10,
2330   0x0d, 0xf0, 0x6a, 0x28, // { moveli r28, 0 ; lnk r26 }
2331   0xdb, 0x0f, 0x00, 0x10,
2332   0x8e, 0x03, 0x00, 0x38, // { moveli r27, 0 ; shl16insli r28, r28, 0 }
2333   0x9c, 0xc6, 0x0d, 0xd0,
2334   0x6d, 0x03, 0x00, 0x38, // { add r28, r26, r28 ; shl16insli r27, r27, 0 }
2335   0x9b, 0xb6, 0xc5, 0xad,
2336   0xff, 0x57, 0xe0, 0x8c, // { add r27, r26, r27 ; info 10 ; ld4s r28, r28 }
2337   0xdd, 0x0f, 0x00, 0x70,
2338   0x80, 0x73, 0x6a, 0x28, // { shl16insli r29, zero, 0 ; jr r28 }
2339 };
2340
2341 template<>
2342 const unsigned char
2343 Output_data_plt_tilegx<64, true>::plt_entry[plt_entry_size] =
2344 {
2345   0xdc, 0x0f, 0x00, 0x10,
2346   0x0d, 0xf0, 0x6a, 0x28, // { moveli r28, 0 ; lnk r26 }
2347   0xdb, 0x0f, 0x00, 0x10,
2348   0x8e, 0x03, 0x00, 0x38, // { moveli r27, 0 ; shl16insli r28, r28, 0 }
2349   0x9c, 0xc6, 0x0d, 0xd0,
2350   0x6d, 0x03, 0x00, 0x38, // { add r28, r26, r28 ; shl16insli r27, r27, 0 }
2351   0x9b, 0xb6, 0xc5, 0xad,
2352   0xff, 0x57, 0xe0, 0x8e, // { add r27, r26, r27 ; info 10 ; ld r28, r28 }
2353   0xdd, 0x0f, 0x00, 0x70,
2354   0x80, 0x73, 0x6a, 0x28, // { shl16insli r29, zero, 0 ; jr r28 }
2355
2356 };
2357
2358 template<>
2359 const unsigned char
2360 Output_data_plt_tilegx<32, true>::plt_entry[plt_entry_size] =
2361 {
2362   0xdc, 0x0f, 0x00, 0x10,
2363   0x0d, 0xf0, 0x6a, 0x28, // { moveli r28, 0 ; lnk r26 }
2364   0xdb, 0x0f, 0x00, 0x10,
2365   0x8e, 0x03, 0x00, 0x38, // { moveli r27, 0 ; shl16insli r28, r28, 0 }
2366   0x9c, 0xc6, 0x0d, 0xd0,
2367   0x6d, 0x03, 0x00, 0x38, // { add r28, r26, r28 ; shl16insli r27, r27, 0 }
2368   0x9b, 0xb6, 0xc5, 0xad,
2369   0xff, 0x57, 0xe0, 0x8c, // { add r27, r26, r27 ; info 10 ; ld4s r28, r28 }
2370   0xdd, 0x0f, 0x00, 0x70,
2371   0x80, 0x73, 0x6a, 0x28, // { shl16insli r29, zero, 0 ; jr r28 }
2372 };
2373
2374 template<int size, bool big_endian>
2375 void
2376 Output_data_plt_tilegx<size, big_endian>::fill_plt_entry(
2377                  unsigned char* pov,
2378                  typename elfcpp::Elf_types<size>::Elf_Addr gotplt_base,
2379                  unsigned int got_offset,
2380                  typename elfcpp::Elf_types<size>::Elf_Addr plt_base,
2381                  unsigned int plt_offset, unsigned int plt_index)
2382 {
2383
2384   const uint32_t TILEGX_IMM16_MASK = 0xFFFF;
2385   const uint32_t TILEGX_X0_IMM16_BITOFF = 12;
2386   const uint32_t TILEGX_X1_IMM16_BITOFF = 43;
2387
2388   typedef typename elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::Valtype
2389     Valtype;
2390   memcpy(pov, plt_entry, plt_entry_size);
2391
2392   // first bundle in plt stub - x0
2393   Valtype* wv = reinterpret_cast<Valtype*>(pov);
2394   Valtype val = elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::readval(wv);
2395   Valtype reloc =
2396     ((gotplt_base + got_offset) - (plt_base + plt_offset + 8)) >> 16;
2397   elfcpp::Elf_Xword dst_mask =
2398     (elfcpp::Elf_Xword)(TILEGX_IMM16_MASK) << TILEGX_X0_IMM16_BITOFF;
2399   val &= ~dst_mask;
2400   reloc &= TILEGX_IMM16_MASK;
2401   elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::writeval(wv,
2402     val | (reloc<<TILEGX_X0_IMM16_BITOFF));
2403
2404   // second bundle in plt stub - x1
2405   wv = reinterpret_cast<Valtype*>(pov + 8);
2406   val = elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::readval(wv);
2407   reloc = (gotplt_base + got_offset) - (plt_base + plt_offset + 8);
2408   dst_mask = (elfcpp::Elf_Xword)(TILEGX_IMM16_MASK) << TILEGX_X1_IMM16_BITOFF;
2409   val &= ~dst_mask;
2410   reloc &= TILEGX_IMM16_MASK;
2411   elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::writeval(wv,
2412     val | (reloc<<TILEGX_X1_IMM16_BITOFF));
2413
2414   // second bundle in plt stub - x0
2415   wv = reinterpret_cast<Valtype*>(pov + 8);
2416   val = elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::readval(wv);
2417   reloc = (gotplt_base - (plt_base + plt_offset + 8)) >> 16;
2418   dst_mask = (elfcpp::Elf_Xword)(TILEGX_IMM16_MASK) << TILEGX_X0_IMM16_BITOFF;
2419   val &= ~dst_mask;
2420   reloc &= TILEGX_IMM16_MASK;
2421   elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::writeval(wv,
2422     val | (reloc<<TILEGX_X0_IMM16_BITOFF));
2423
2424   // third bundle in plt stub - x1
2425   wv = reinterpret_cast<Valtype*>(pov + 16);
2426   val = elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::readval(wv);
2427   reloc = gotplt_base - (plt_base + plt_offset + 8);
2428   dst_mask = (elfcpp::Elf_Xword)(TILEGX_IMM16_MASK) << TILEGX_X1_IMM16_BITOFF;
2429   val &= ~dst_mask;
2430   reloc &= TILEGX_IMM16_MASK;
2431   elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::writeval(wv,
2432     val | (reloc<<TILEGX_X1_IMM16_BITOFF));
2433
2434   // fifth bundle in plt stub - carry plt_index x0
2435   wv = reinterpret_cast<Valtype*>(pov + 32);
2436   val = elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::readval(wv);
2437   dst_mask = (elfcpp::Elf_Xword)(TILEGX_IMM16_MASK) << TILEGX_X0_IMM16_BITOFF;
2438   val &= ~dst_mask;
2439   plt_index &= TILEGX_IMM16_MASK;
2440   elfcpp::Swap<TILEGX_INST_BUNDLE_SIZE, big_endian>::writeval(wv,
2441     val | (plt_index<<TILEGX_X0_IMM16_BITOFF));
2442
2443 }
2444
2445 // Write out the PLT.  This uses the hand-coded instructions above.
2446
2447 template<int size, bool big_endian>
2448 void
2449 Output_data_plt_tilegx<size, big_endian>::do_write(Output_file* of)
2450 {
2451   const off_t offset = this->offset();
2452   const section_size_type oview_size =
2453     convert_to_section_size_type(this->data_size());
2454   unsigned char* const oview = of->get_output_view(offset, oview_size);
2455
2456   const off_t got_file_offset = this->got_plt_->offset();
2457   gold_assert(parameters->incremental_update()
2458               || (got_file_offset + this->got_plt_->data_size()
2459                   == this->got_irelative_->offset()));
2460   const section_size_type got_size =
2461     convert_to_section_size_type(this->got_plt_->data_size()
2462                                  + this->got_irelative_->data_size());
2463   unsigned char* const got_view = of->get_output_view(got_file_offset,
2464                                                       got_size);
2465
2466   unsigned char* pov = oview;
2467
2468   // The base address of the .plt section.
2469   typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
2470   typename elfcpp::Elf_types<size>::Elf_Addr got_address =
2471     this->got_plt_->address();
2472
2473   this->fill_first_plt_entry(pov);
2474   pov += this->get_plt_entry_size();
2475
2476   unsigned char* got_pov = got_view;
2477
2478   // first entry of .got.plt are set to -1
2479   // second entry of .got.plt are set to 0
2480   memset(got_pov, 0xff, size / 8);
2481   got_pov += size / 8;
2482   memset(got_pov, 0x0, size / 8);
2483   got_pov += size / 8;
2484
2485   unsigned int plt_offset = this->get_plt_entry_size();
2486   const unsigned int count = this->count_ + this->irelative_count_;
2487   unsigned int got_offset = (size / 8) * TILEGX_GOTPLT_RESERVE_COUNT;
2488   for (unsigned int plt_index = 0;
2489        plt_index < count;
2490        ++plt_index,
2491          pov += this->get_plt_entry_size(),
2492          got_pov += size / 8,
2493          plt_offset += this->get_plt_entry_size(),
2494          got_offset += size / 8)
2495     {
2496       // Set and adjust the PLT entry itself.
2497       this->fill_plt_entry(pov, got_address, got_offset,
2498                            plt_address, plt_offset, plt_index);
2499
2500       // Initialize entry in .got.plt to plt start address
2501       elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
2502     }
2503
2504   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
2505   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
2506
2507   of->write_output_view(offset, oview_size, oview);
2508   of->write_output_view(got_file_offset, got_size, got_view);
2509 }
2510
2511 // Create the PLT section.
2512
2513 template<int size, bool big_endian>
2514 void
2515 Target_tilegx<size, big_endian>::make_plt_section(Symbol_table* symtab,
2516                                                   Layout* layout)
2517 {
2518   if (this->plt_ == NULL)
2519     {
2520       // Create the GOT sections first.
2521       this->got_section(symtab, layout);
2522
2523       // Ensure that .rela.dyn always appears before .rela.plt,
2524       // becuase on TILE-Gx, .rela.dyn needs to include .rela.plt
2525       // in it's range.
2526       this->rela_dyn_section(layout);
2527
2528       this->plt_ = new Output_data_plt_tilegx<size, big_endian>(layout,
2529         TILEGX_INST_BUNDLE_SIZE, this->got_, this->got_plt_,
2530         this->got_irelative_);
2531
2532       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2533                                       (elfcpp::SHF_ALLOC
2534                                        | elfcpp::SHF_EXECINSTR),
2535                                       this->plt_, ORDER_NON_RELRO_FIRST,
2536                                       false);
2537
2538       // Make the sh_info field of .rela.plt point to .plt.
2539       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2540       rela_plt_os->set_info_section(this->plt_->output_section());
2541     }
2542 }
2543
2544 // Create a PLT entry for a global symbol.
2545
2546 template<int size, bool big_endian>
2547 void
2548 Target_tilegx<size, big_endian>::make_plt_entry(Symbol_table* symtab,
2549                                                 Layout* layout, Symbol* gsym)
2550 {
2551   if (gsym->has_plt_offset())
2552     return;
2553
2554   if (this->plt_ == NULL)
2555     this->make_plt_section(symtab, layout);
2556
2557   this->plt_->add_entry(symtab, layout, gsym);
2558 }
2559
2560 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2561
2562 template<int size, bool big_endian>
2563 void
2564 Target_tilegx<size, big_endian>::make_local_ifunc_plt_entry(
2565     Symbol_table* symtab, Layout* layout,
2566     Sized_relobj_file<size, big_endian>* relobj,
2567     unsigned int local_sym_index)
2568 {
2569   if (relobj->local_has_plt_offset(local_sym_index))
2570     return;
2571   if (this->plt_ == NULL)
2572     this->make_plt_section(symtab, layout);
2573   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
2574                                                               relobj,
2575                                                               local_sym_index);
2576   relobj->set_local_plt_offset(local_sym_index, plt_offset);
2577 }
2578
2579 // Return the number of entries in the PLT.
2580
2581 template<int size, bool big_endian>
2582 unsigned int
2583 Target_tilegx<size, big_endian>::plt_entry_count() const
2584 {
2585   if (this->plt_ == NULL)
2586     return 0;
2587   return this->plt_->entry_count();
2588 }
2589
2590 // Return the offset of the first non-reserved PLT entry.
2591
2592 template<int size, bool big_endian>
2593 unsigned int
2594 Target_tilegx<size, big_endian>::first_plt_entry_offset() const
2595 {
2596   return this->plt_->first_plt_entry_offset();
2597 }
2598
2599 // Return the size of each PLT entry.
2600
2601 template<int size, bool big_endian>
2602 unsigned int
2603 Target_tilegx<size, big_endian>::plt_entry_size() const
2604 {
2605   return this->plt_->get_plt_entry_size();
2606 }
2607
2608 // Create the GOT and PLT sections for an incremental update.
2609
2610 template<int size, bool big_endian>
2611 Output_data_got_base*
2612 Target_tilegx<size, big_endian>::init_got_plt_for_update(Symbol_table* symtab,
2613                                        Layout* layout,
2614                                        unsigned int got_count,
2615                                        unsigned int plt_count)
2616 {
2617   gold_assert(this->got_ == NULL);
2618
2619   this->got_ =
2620     new Output_data_got<size, big_endian>((got_count
2621                                            + TILEGX_GOT_RESERVE_COUNT)
2622                                           * (size / 8));
2623   layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2624                                   (elfcpp::SHF_ALLOC
2625                                    | elfcpp::SHF_WRITE),
2626                                   this->got_, ORDER_RELRO_LAST,
2627                                   true);
2628
2629   // Define _GLOBAL_OFFSET_TABLE_ at the start of the GOT.
2630   this->global_offset_table_ =
2631     symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2632                                   Symbol_table::PREDEFINED,
2633                                   this->got_,
2634                                   0, 0, elfcpp::STT_OBJECT,
2635                                   elfcpp::STB_LOCAL,
2636                                   elfcpp::STV_HIDDEN, 0,
2637                                   false, false);
2638
2639   if (parameters->options().shared()) {
2640     this->tilegx_dynamic_ =
2641             symtab->define_in_output_data("_TILEGX_DYNAMIC_", NULL,
2642                             Symbol_table::PREDEFINED,
2643                             layout->dynamic_section(),
2644                             0, 0, elfcpp::STT_OBJECT,
2645                             elfcpp::STB_LOCAL,
2646                             elfcpp::STV_HIDDEN, 0,
2647                             false, false);
2648
2649     this->got_->add_global(this->tilegx_dynamic_, GOT_TYPE_STANDARD);
2650   } else
2651     this->got_->set_current_data_size(size / 8);
2652
2653   // Add the two reserved entries.
2654   this->got_plt_
2655      = new Output_data_space((plt_count + TILEGX_GOTPLT_RESERVE_COUNT)
2656                               * (size / 8), size / 8, "** GOT PLT");
2657   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2658                                   (elfcpp::SHF_ALLOC
2659                                    | elfcpp::SHF_WRITE),
2660                                   this->got_plt_, ORDER_NON_RELRO_FIRST,
2661                                   false);
2662
2663   // If there are any IRELATIVE relocations, they get GOT entries in
2664   // .got.plt after the jump slot.
2665   this->got_irelative_
2666      = new Output_data_space(0, size / 8, "** GOT IRELATIVE PLT");
2667   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2668                                   elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2669                                   this->got_irelative_,
2670                                   ORDER_NON_RELRO_FIRST, false);
2671
2672   // Create the PLT section.
2673   this->plt_ = new Output_data_plt_tilegx<size, big_endian>(layout,
2674     this->plt_entry_size(), this->got_, this->got_plt_, this->got_irelative_,
2675     plt_count);
2676
2677   layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2678                                   elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2679                                   this->plt_, ORDER_PLT, false);
2680
2681   // Make the sh_info field of .rela.plt point to .plt.
2682   Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2683   rela_plt_os->set_info_section(this->plt_->output_section());
2684
2685   // Create the rela_dyn section.
2686   this->rela_dyn_section(layout);
2687
2688   return this->got_;
2689 }
2690
2691 // Reserve a GOT entry for a local symbol, and regenerate any
2692 // necessary dynamic relocations.
2693
2694 template<int size, bool big_endian>
2695 void
2696 Target_tilegx<size, big_endian>::reserve_local_got_entry(
2697     unsigned int got_index,
2698     Sized_relobj<size, big_endian>* obj,
2699     unsigned int r_sym,
2700     unsigned int got_type)
2701 {
2702   unsigned int got_offset = (got_index + TILEGX_GOT_RESERVE_COUNT)
2703                             * (size / 8);
2704   Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2705
2706   this->got_->reserve_local(got_index, obj, r_sym, got_type);
2707   switch (got_type)
2708     {
2709     case GOT_TYPE_STANDARD:
2710       if (parameters->options().output_is_position_independent())
2711         rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_TILEGX_RELATIVE,
2712                                      this->got_, got_offset, 0, false);
2713       break;
2714     case GOT_TYPE_TLS_OFFSET:
2715       rela_dyn->add_local(obj, r_sym,
2716                           size == 32 ? elfcpp::R_TILEGX_TLS_DTPOFF32
2717                                        : elfcpp::R_TILEGX_TLS_DTPOFF64,
2718                           this->got_, got_offset, 0);
2719       break;
2720     case GOT_TYPE_TLS_PAIR:
2721       this->got_->reserve_slot(got_index + 1);
2722       rela_dyn->add_local(obj, r_sym,
2723                           size == 32 ? elfcpp::R_TILEGX_TLS_DTPMOD32
2724                                        : elfcpp::R_TILEGX_TLS_DTPMOD64,
2725                           this->got_, got_offset, 0);
2726       break;
2727     case GOT_TYPE_TLS_DESC:
2728       gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2729       break;
2730     default:
2731       gold_unreachable();
2732     }
2733 }
2734
2735 // Reserve a GOT entry for a global symbol, and regenerate any
2736 // necessary dynamic relocations.
2737
2738 template<int size, bool big_endian>
2739 void
2740 Target_tilegx<size, big_endian>::reserve_global_got_entry(
2741   unsigned int got_index, Symbol* gsym, unsigned int got_type)
2742 {
2743   unsigned int got_offset = (got_index + TILEGX_GOT_RESERVE_COUNT)
2744                             * (size / 8);
2745   Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
2746
2747   this->got_->reserve_global(got_index, gsym, got_type);
2748   switch (got_type)
2749     {
2750     case GOT_TYPE_STANDARD:
2751       if (!gsym->final_value_is_known())
2752         {
2753           if (gsym->is_from_dynobj()
2754               || gsym->is_undefined()
2755               || gsym->is_preemptible()
2756               || gsym->type() == elfcpp::STT_GNU_IFUNC)
2757             rela_dyn->add_global(gsym, elfcpp::R_TILEGX_GLOB_DAT,
2758                                  this->got_, got_offset, 0);
2759           else
2760             rela_dyn->add_global_relative(gsym, elfcpp::R_TILEGX_RELATIVE,
2761                                           this->got_, got_offset, 0, false);
2762         }
2763       break;
2764     case GOT_TYPE_TLS_OFFSET:
2765       rela_dyn->add_global_relative(gsym,
2766                                     size == 32 ? elfcpp::R_TILEGX_TLS_TPOFF32
2767                                                : elfcpp::R_TILEGX_TLS_TPOFF64,
2768                                     this->got_, got_offset, 0, false);
2769       break;
2770     case GOT_TYPE_TLS_PAIR:
2771       this->got_->reserve_slot(got_index + 1);
2772       rela_dyn->add_global_relative(gsym,
2773                                     size == 32 ? elfcpp::R_TILEGX_TLS_DTPMOD32
2774                                                : elfcpp::R_TILEGX_TLS_DTPMOD64,
2775                                     this->got_, got_offset, 0, false);
2776       rela_dyn->add_global_relative(gsym,
2777                                     size == 32 ? elfcpp::R_TILEGX_TLS_DTPOFF32
2778                                                : elfcpp::R_TILEGX_TLS_DTPOFF64,
2779                                     this->got_, got_offset + size / 8,
2780                                     0, false);
2781       break;
2782     case GOT_TYPE_TLS_DESC:
2783       gold_fatal(_("TLS_DESC not yet supported for TILEGX"));
2784       break;
2785     default:
2786       gold_unreachable();
2787     }
2788 }
2789
2790 // Register an existing PLT entry for a global symbol.
2791
2792 template<int size, bool big_endian>
2793 void
2794 Target_tilegx<size, big_endian>::register_global_plt_entry(
2795   Symbol_table* symtab, Layout* layout, unsigned int plt_index, Symbol* gsym)
2796 {
2797   gold_assert(this->plt_ != NULL);
2798   gold_assert(!gsym->has_plt_offset());
2799
2800   this->plt_->reserve_slot(plt_index);
2801
2802   gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
2803
2804   unsigned int got_offset = (plt_index + 2) * (size / 8);
2805   this->plt_->add_relocation(symtab, layout, gsym, got_offset);
2806 }
2807
2808 // Force a COPY relocation for a given symbol.
2809
2810 template<int size, bool big_endian>
2811 void
2812 Target_tilegx<size, big_endian>::emit_copy_reloc(
2813     Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
2814 {
2815   this->copy_relocs_.emit_copy_reloc(symtab,
2816                                      symtab->get_sized_symbol<size>(sym),
2817                                      os,
2818                                      offset,
2819                                      this->rela_dyn_section(NULL));
2820 }
2821
2822 // Create a GOT entry for the TLS module index.
2823
2824 template<int size, bool big_endian>
2825 unsigned int
2826 Target_tilegx<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
2827                                   Layout* layout,
2828                                   Sized_relobj_file<size, big_endian>* object)
2829 {
2830   if (this->got_mod_index_offset_ == -1U)
2831     {
2832       gold_assert(symtab != NULL && layout != NULL && object != NULL);
2833       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2834       Output_data_got<size, big_endian>* got
2835          = this->got_section(symtab, layout);
2836       unsigned int got_offset = got->add_constant(0);
2837       rela_dyn->add_local(object, 0,
2838                           size == 32 ? elfcpp::R_TILEGX_TLS_DTPMOD32
2839                                        : elfcpp::R_TILEGX_TLS_DTPMOD64, got,
2840                           got_offset, 0);
2841       got->add_constant(0);
2842       this->got_mod_index_offset_ = got_offset;
2843     }
2844   return this->got_mod_index_offset_;
2845 }
2846
2847 // Optimize the TLS relocation type based on what we know about the
2848 // symbol.  IS_FINAL is true if the final address of this symbol is
2849 // known at link time.
2850 //
2851 // the transformation rules is described below:
2852 //
2853 //   compiler GD reference
2854 //    |
2855 //    V
2856 //     moveli      tmp, hw1_last_tls_gd(x)     X0/X1
2857 //     shl16insli  r0,  tmp, hw0_tls_gd(x)     X0/X1
2858 //     addi        r0, got, tls_add(x)         Y0/Y1/X0/X1
2859 //     jal         tls_gd_call(x)              X1
2860 //     addi        adr, r0,  tls_gd_add(x)     Y0/Y1/X0/X1
2861 //
2862 //     linker tranformation of GD insn sequence
2863 //      |
2864 //      V
2865 //      ==> GD:
2866 //       moveli      tmp, hw1_last_tls_gd(x)     X0/X1
2867 //       shl16insli  r0,  tmp, hw0_tls_gd(x)     X0/X1
2868 //       add         r0,  got, r0                Y0/Y1/X0/X1
2869 //       jal         plt(__tls_get_addr)         X1
2870 //       move        adr, r0                     Y0/Y1/X0/X1
2871 //      ==> IE:
2872 //       moveli      tmp, hw1_last_tls_ie(x)     X0/X1
2873 //       shl16insli  r0,  tmp, hw0_tls_ie(x)     X0/X1
2874 //       add         r0,  got, r0                Y0/Y1/X0/X1
2875 //       ld          r0,  r0                     X1
2876 //       add         adr, r0, tp                 Y0/Y1/X0/X1
2877 //      ==> LE:
2878 //       moveli      tmp, hw1_last_tls_le(x)     X0/X1
2879 //       shl16insli  r0,  tmp, hw0_tls_le(x)     X0/X1
2880 //       move        r0,  r0                     Y0/Y1/X0/X1
2881 //       move        r0,  r0                     Y0/Y1/X0/X1
2882 //       add         adr, r0, tp                 Y0/Y1/X0/X1
2883 //
2884 //
2885 //   compiler IE reference
2886 //    |
2887 //    V
2888 //     moveli      tmp, hw1_last_tls_ie(x)     X0/X1
2889 //     shl16insli  tmp, tmp, hw0_tls_ie(x)     X0/X1
2890 //     addi        tmp, got, tls_add(x)        Y0/Y1/X0/X1
2891 //     ld_tls      tmp, tmp, tls_ie_load(x)    X1
2892 //     add         adr, tmp, tp                Y0/Y1/X0/X1
2893 //
2894 //     linker transformation for IE insn sequence
2895 //      |
2896 //      V
2897 //      ==> IE:
2898 //       moveli      tmp, hw1_last_tls_ie(x)     X0/X1
2899 //       shl16insli  tmp, tmp, hw0_tls_ie(x)     X0/X1
2900 //       add         tmp, got, tmp               Y0/Y1/X0/X1
2901 //       ld          tmp, tmp                    X1
2902 //       add         adr, tmp, tp                Y0/Y1/X0/X1
2903 //      ==> LE:
2904 //       moveli      tmp, hw1_last_tls_le(x)     X0/X1
2905 //       shl16insli  tmp, tmp, hw0_tls_le(x)     X0/X1
2906 //       move        tmp, tmp                    Y0/Y1/X0/X1
2907 //       move        tmp, tmp                    Y0/Y1/X0/X1
2908 //
2909 //
2910 //   compiler LE reference
2911 //    |
2912 //    V
2913 //     moveli        tmp, hw1_last_tls_le(x)     X0/X1
2914 //     shl16insli    tmp, tmp, hw0_tls_le(x)     X0/X1
2915 //     add           adr, tmp, tp                Y0/Y1/X0/X1
2916
2917 template<int size, bool big_endian>
2918 tls::Tls_optimization
2919 Target_tilegx<size, big_endian>::optimize_tls_reloc(bool is_final, int r_type)
2920 {
2921   // If we are generating a shared library, then we can't do anything
2922   // in the linker.
2923   if (parameters->options().shared())
2924     return tls::TLSOPT_NONE;
2925
2926   switch (r_type)
2927     {
2928     // unique GD relocations
2929     case elfcpp::R_TILEGX_TLS_GD_CALL:
2930     case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
2931     case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
2932     case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
2933     case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
2934     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
2935     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
2936     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
2937     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
2938     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
2939     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
2940       // These are General-Dynamic which permits fully general TLS
2941       // access.  Since we know that we are generating an executable,
2942       // we can convert this to Initial-Exec.  If we also know that
2943       // this is a local symbol, we can further switch to Local-Exec.
2944       if (is_final)
2945         return tls::TLSOPT_TO_LE;
2946       return tls::TLSOPT_TO_IE;
2947
2948     // unique IE relocations
2949     case elfcpp::R_TILEGX_TLS_IE_LOAD:
2950     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
2951     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
2952     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
2953     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
2954     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
2955     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
2956       // These are Initial-Exec relocs which get the thread offset
2957       // from the GOT.  If we know that we are linking against the
2958       // local symbol, we can switch to Local-Exec, which links the
2959       // thread offset into the instruction.
2960       if (is_final)
2961         return tls::TLSOPT_TO_LE;
2962       return tls::TLSOPT_NONE;
2963
2964     // could be created for both GD and IE
2965     // but they are expanded into the same
2966     // instruction in GD and IE.
2967     case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
2968     case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
2969     case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
2970     case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
2971       if (is_final)
2972         return tls::TLSOPT_TO_LE;
2973       return tls::TLSOPT_NONE;
2974
2975     // unique LE relocations
2976     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
2977     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
2978     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
2979     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
2980     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
2981     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
2982       // When we already have Local-Exec, there is nothing further we
2983       // can do.
2984       return tls::TLSOPT_NONE;
2985
2986     default:
2987       gold_unreachable();
2988     }
2989 }
2990
2991 // Get the Reference_flags for a particular relocation.
2992
2993 template<int size, bool big_endian>
2994 int
2995 Target_tilegx<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2996 {
2997   switch (r_type)
2998     {
2999     case elfcpp::R_TILEGX_NONE:
3000     case elfcpp::R_TILEGX_GNU_VTINHERIT:
3001     case elfcpp::R_TILEGX_GNU_VTENTRY:
3002       // No symbol reference.
3003       return 0;
3004
3005     case elfcpp::R_TILEGX_64:
3006     case elfcpp::R_TILEGX_32:
3007     case elfcpp::R_TILEGX_16:
3008     case elfcpp::R_TILEGX_8:
3009       return Symbol::ABSOLUTE_REF;
3010
3011     case elfcpp::R_TILEGX_BROFF_X1:
3012     case elfcpp::R_TILEGX_64_PCREL:
3013     case elfcpp::R_TILEGX_32_PCREL:
3014     case elfcpp::R_TILEGX_16_PCREL:
3015     case elfcpp::R_TILEGX_8_PCREL:
3016     case elfcpp::R_TILEGX_IMM16_X0_HW0_PCREL:
3017     case elfcpp::R_TILEGX_IMM16_X1_HW0_PCREL:
3018     case elfcpp::R_TILEGX_IMM16_X0_HW1_PCREL:
3019     case elfcpp::R_TILEGX_IMM16_X1_HW1_PCREL:
3020     case elfcpp::R_TILEGX_IMM16_X0_HW2_PCREL:
3021     case elfcpp::R_TILEGX_IMM16_X1_HW2_PCREL:
3022     case elfcpp::R_TILEGX_IMM16_X0_HW3_PCREL:
3023     case elfcpp::R_TILEGX_IMM16_X1_HW3_PCREL:
3024     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
3025     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
3026     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
3027     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
3028     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
3029     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
3030       return Symbol::RELATIVE_REF;
3031
3032     case elfcpp::R_TILEGX_JUMPOFF_X1:
3033     case elfcpp::R_TILEGX_JUMPOFF_X1_PLT:
3034     case elfcpp::R_TILEGX_IMM16_X0_HW0_PLT_PCREL:
3035     case elfcpp::R_TILEGX_IMM16_X1_HW0_PLT_PCREL:
3036     case elfcpp::R_TILEGX_IMM16_X0_HW1_PLT_PCREL:
3037     case elfcpp::R_TILEGX_IMM16_X1_HW1_PLT_PCREL:
3038     case elfcpp::R_TILEGX_IMM16_X0_HW2_PLT_PCREL:
3039     case elfcpp::R_TILEGX_IMM16_X1_HW2_PLT_PCREL:
3040     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL:
3041     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL:
3042     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL:
3043     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL:
3044     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL:
3045     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL:
3046       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
3047
3048     case elfcpp::R_TILEGX_IMM16_X0_HW0:
3049     case elfcpp::R_TILEGX_IMM16_X1_HW0:
3050     case elfcpp::R_TILEGX_IMM16_X0_HW1:
3051     case elfcpp::R_TILEGX_IMM16_X1_HW1:
3052     case elfcpp::R_TILEGX_IMM16_X0_HW2:
3053     case elfcpp::R_TILEGX_IMM16_X1_HW2:
3054     case elfcpp::R_TILEGX_IMM16_X0_HW3:
3055     case elfcpp::R_TILEGX_IMM16_X1_HW3:
3056     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST:
3057     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST:
3058     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST:
3059     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST:
3060     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST:
3061     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST:
3062       return Symbol::ABSOLUTE_REF;
3063
3064     case elfcpp::R_TILEGX_IMM16_X0_HW0_GOT:
3065     case elfcpp::R_TILEGX_IMM16_X1_HW0_GOT:
3066     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_GOT:
3067     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_GOT:
3068     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_GOT:
3069     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_GOT:
3070       // Absolute in GOT.
3071       return Symbol::ABSOLUTE_REF;
3072
3073     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
3074     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
3075     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
3076     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
3077     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
3078     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
3079     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
3080     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
3081     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3082     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3083     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3084     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3085     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
3086     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
3087     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
3088     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
3089     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
3090     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
3091     case elfcpp::R_TILEGX_TLS_DTPOFF64:
3092     case elfcpp::R_TILEGX_TLS_DTPMOD32:
3093     case elfcpp::R_TILEGX_TLS_DTPOFF32:
3094     case elfcpp::R_TILEGX_TLS_TPOFF32:
3095     case elfcpp::R_TILEGX_TLS_GD_CALL:
3096     case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
3097     case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
3098     case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
3099     case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
3100     case elfcpp::R_TILEGX_TLS_IE_LOAD:
3101     case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
3102     case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
3103     case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
3104     case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
3105       return Symbol::TLS_REF;
3106
3107     case elfcpp::R_TILEGX_COPY:
3108     case elfcpp::R_TILEGX_GLOB_DAT:
3109     case elfcpp::R_TILEGX_JMP_SLOT:
3110     case elfcpp::R_TILEGX_RELATIVE:
3111     case elfcpp::R_TILEGX_TLS_TPOFF64:
3112     case elfcpp::R_TILEGX_TLS_DTPMOD64:
3113     default:
3114       // Not expected.  We will give an error later.
3115       return 0;
3116     }
3117 }
3118
3119 // Report an unsupported relocation against a local symbol.
3120
3121 template<int size, bool big_endian>
3122 void
3123 Target_tilegx<size, big_endian>::Scan::unsupported_reloc_local(
3124      Sized_relobj_file<size, big_endian>* object,
3125      unsigned int r_type)
3126 {
3127   gold_error(_("%s: unsupported reloc %u against local symbol"),
3128              object->name().c_str(), r_type);
3129 }
3130
3131 // We are about to emit a dynamic relocation of type R_TYPE.  If the
3132 // dynamic linker does not support it, issue an error.
3133 template<int size, bool big_endian>
3134 void
3135 Target_tilegx<size, big_endian>::Scan::check_non_pic(Relobj* object,
3136                                                      unsigned int r_type)
3137 {
3138   switch (r_type)
3139     {
3140       // These are the relocation types supported by glibc for tilegx
3141       // which should always work.
3142     case elfcpp::R_TILEGX_RELATIVE:
3143     case elfcpp::R_TILEGX_GLOB_DAT:
3144     case elfcpp::R_TILEGX_JMP_SLOT:
3145     case elfcpp::R_TILEGX_TLS_DTPMOD64:
3146     case elfcpp::R_TILEGX_TLS_DTPOFF64:
3147     case elfcpp::R_TILEGX_TLS_TPOFF64:
3148     case elfcpp::R_TILEGX_8:
3149     case elfcpp::R_TILEGX_16:
3150     case elfcpp::R_TILEGX_32:
3151     case elfcpp::R_TILEGX_64:
3152     case elfcpp::R_TILEGX_COPY:
3153     case elfcpp::R_TILEGX_IMM16_X0_HW0:
3154     case elfcpp::R_TILEGX_IMM16_X1_HW0:
3155     case elfcpp::R_TILEGX_IMM16_X0_HW1:
3156     case elfcpp::R_TILEGX_IMM16_X1_HW1:
3157     case elfcpp::R_TILEGX_IMM16_X0_HW2:
3158     case elfcpp::R_TILEGX_IMM16_X1_HW2:
3159     case elfcpp::R_TILEGX_IMM16_X0_HW3:
3160     case elfcpp::R_TILEGX_IMM16_X1_HW3:
3161     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST:
3162     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST:
3163     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST:
3164     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST:
3165     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST:
3166     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST:
3167     case elfcpp::R_TILEGX_BROFF_X1:
3168     case elfcpp::R_TILEGX_JUMPOFF_X1:
3169     case elfcpp::R_TILEGX_IMM16_X0_HW0_PCREL:
3170     case elfcpp::R_TILEGX_IMM16_X1_HW0_PCREL:
3171     case elfcpp::R_TILEGX_IMM16_X0_HW1_PCREL:
3172     case elfcpp::R_TILEGX_IMM16_X1_HW1_PCREL:
3173     case elfcpp::R_TILEGX_IMM16_X0_HW2_PCREL:
3174     case elfcpp::R_TILEGX_IMM16_X1_HW2_PCREL:
3175     case elfcpp::R_TILEGX_IMM16_X0_HW3_PCREL:
3176     case elfcpp::R_TILEGX_IMM16_X1_HW3_PCREL:
3177     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
3178     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
3179     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
3180     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
3181     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
3182     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
3183       return;
3184
3185     default:
3186       // This prevents us from issuing more than one error per reloc
3187       // section.  But we can still wind up issuing more than one
3188       // error per object file.
3189       if (this->issued_non_pic_error_)
3190         return;
3191       gold_assert(parameters->options().output_is_position_independent());
3192       object->error(_("requires unsupported dynamic reloc %u; "
3193                       "recompile with -fPIC"),
3194                     r_type);
3195       this->issued_non_pic_error_ = true;
3196       return;
3197
3198     case elfcpp::R_TILEGX_NONE:
3199       gold_unreachable();
3200     }
3201 }
3202
3203 // Return whether we need to make a PLT entry for a relocation of the
3204 // given type against a STT_GNU_IFUNC symbol.
3205
3206 template<int size, bool big_endian>
3207 bool
3208 Target_tilegx<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
3209      Sized_relobj_file<size, big_endian>* object, unsigned int r_type)
3210 {
3211   int flags = Scan::get_reference_flags(r_type);
3212   if (flags & Symbol::TLS_REF)
3213     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3214                object->name().c_str(), r_type);
3215   return flags != 0;
3216 }
3217
3218 // Scan a relocation for a local symbol.
3219
3220 template<int size, bool big_endian>
3221 inline void
3222 Target_tilegx<size, big_endian>::Scan::local(Symbol_table* symtab,
3223                                  Layout* layout,
3224                                  Target_tilegx<size, big_endian>* target,
3225                                  Sized_relobj_file<size, big_endian>* object,
3226                                  unsigned int data_shndx,
3227                                  Output_section* output_section,
3228                                  const elfcpp::Rela<size, big_endian>& reloc,
3229                                  unsigned int r_type,
3230                                  const elfcpp::Sym<size, big_endian>& lsym,
3231                                  bool is_discarded)
3232 {
3233   if (is_discarded)
3234     return;
3235
3236   // A local STT_GNU_IFUNC symbol may require a PLT entry.
3237   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3238   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3239     {
3240       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3241       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
3242     }
3243
3244   switch (r_type)
3245     {
3246     case elfcpp::R_TILEGX_NONE:
3247     case elfcpp::R_TILEGX_GNU_VTINHERIT:
3248     case elfcpp::R_TILEGX_GNU_VTENTRY:
3249       break;
3250
3251     // If building a shared library (or a position-independent
3252     // executable), because the runtime address needs plus
3253     // the module base address, so generate a R_TILEGX_RELATIVE.
3254     case elfcpp::R_TILEGX_32:
3255     case elfcpp::R_TILEGX_64:
3256       if (parameters->options().output_is_position_independent())
3257         {
3258           unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3259           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3260           rela_dyn->add_local_relative(object, r_sym,
3261                                        elfcpp::R_TILEGX_RELATIVE,
3262                                        output_section, data_shndx,
3263                                        reloc.get_r_offset(),
3264                                        reloc.get_r_addend(), is_ifunc);
3265         }
3266       break;
3267
3268     // If building a shared library (or a position-independent
3269     // executable), we need to create a dynamic relocation for this
3270     // location.
3271     case elfcpp::R_TILEGX_8:
3272     case elfcpp::R_TILEGX_16:
3273     case elfcpp::R_TILEGX_IMM16_X0_HW0:
3274     case elfcpp::R_TILEGX_IMM16_X1_HW0:
3275     case elfcpp::R_TILEGX_IMM16_X0_HW1:
3276     case elfcpp::R_TILEGX_IMM16_X1_HW1:
3277     case elfcpp::R_TILEGX_IMM16_X0_HW2:
3278     case elfcpp::R_TILEGX_IMM16_X1_HW2:
3279     case elfcpp::R_TILEGX_IMM16_X0_HW3:
3280     case elfcpp::R_TILEGX_IMM16_X1_HW3:
3281     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST:
3282     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST:
3283     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST:
3284     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST:
3285     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST:
3286     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST:
3287       if (parameters->options().output_is_position_independent())
3288         {
3289           this->check_non_pic(object, r_type);
3290
3291           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3292           unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3293           if (lsym.get_st_type() != elfcpp::STT_SECTION)
3294             rela_dyn->add_local(object, r_sym, r_type, output_section,
3295                                 data_shndx, reloc.get_r_offset(),
3296                                 reloc.get_r_addend());
3297           else
3298             {
3299               gold_assert(lsym.get_st_value() == 0);
3300               rela_dyn->add_symbolless_local_addend(object, r_sym, r_type,
3301                                                     output_section,
3302                                                     data_shndx,
3303                                                     reloc.get_r_offset(),
3304                                                     reloc.get_r_addend());
3305
3306             }
3307         }
3308       break;
3309
3310     // R_TILEGX_JUMPOFF_X1_PLT against local symbol
3311     // may happen for ifunc case.
3312     case elfcpp::R_TILEGX_JUMPOFF_X1_PLT:
3313     case elfcpp::R_TILEGX_JUMPOFF_X1:
3314     case elfcpp::R_TILEGX_64_PCREL:
3315     case elfcpp::R_TILEGX_32_PCREL:
3316     case elfcpp::R_TILEGX_16_PCREL:
3317     case elfcpp::R_TILEGX_8_PCREL:
3318     case elfcpp::R_TILEGX_BROFF_X1:
3319     case elfcpp::R_TILEGX_IMM16_X0_HW0_PCREL:
3320     case elfcpp::R_TILEGX_IMM16_X1_HW0_PCREL:
3321     case elfcpp::R_TILEGX_IMM16_X0_HW1_PCREL:
3322     case elfcpp::R_TILEGX_IMM16_X1_HW1_PCREL:
3323     case elfcpp::R_TILEGX_IMM16_X0_HW2_PCREL:
3324     case elfcpp::R_TILEGX_IMM16_X1_HW2_PCREL:
3325     case elfcpp::R_TILEGX_IMM16_X0_HW3_PCREL:
3326     case elfcpp::R_TILEGX_IMM16_X1_HW3_PCREL:
3327     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
3328     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
3329     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
3330     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
3331     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
3332     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
3333     case elfcpp::R_TILEGX_IMM16_X0_HW0_PLT_PCREL:
3334     case elfcpp::R_TILEGX_IMM16_X1_HW0_PLT_PCREL:
3335     case elfcpp::R_TILEGX_IMM16_X0_HW1_PLT_PCREL:
3336     case elfcpp::R_TILEGX_IMM16_X1_HW1_PLT_PCREL:
3337     case elfcpp::R_TILEGX_IMM16_X0_HW2_PLT_PCREL:
3338     case elfcpp::R_TILEGX_IMM16_X1_HW2_PLT_PCREL:
3339     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL:
3340     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL:
3341     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL:
3342     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL:
3343     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL:
3344     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL:
3345       break;
3346
3347     case elfcpp::R_TILEGX_IMM16_X0_HW0_GOT:
3348     case elfcpp::R_TILEGX_IMM16_X1_HW0_GOT:
3349     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_GOT:
3350     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_GOT:
3351     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_GOT:
3352     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_GOT:
3353       {
3354         // The symbol requires a GOT entry.
3355         Output_data_got<size, big_endian>* got
3356            = target->got_section(symtab, layout);
3357         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3358
3359         // For a STT_GNU_IFUNC symbol we want the PLT offset.  That
3360         // lets function pointers compare correctly with shared
3361         // libraries.  Otherwise we would need an IRELATIVE reloc.
3362         bool is_new;
3363         if (is_ifunc)
3364           is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3365         else
3366           is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3367         if (is_new)
3368           {
3369             // tilegx dynamic linker will not update local got entry,
3370             // so, if we are generating a shared object, we need to add a
3371             // dynamic relocation for this symbol's GOT entry to inform
3372             // dynamic linker plus the load base explictly.
3373             if (parameters->options().output_is_position_independent())
3374               {
3375                unsigned int got_offset
3376                   = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3377
3378                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3379                 rela_dyn->add_local_relative(object, r_sym,
3380                                              r_type,
3381                                              got, got_offset, 0, is_ifunc);
3382               }
3383           }
3384       }
3385       break;
3386
3387     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
3388     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
3389     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
3390     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
3391     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
3392     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
3393     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
3394     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
3395     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3396     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3397     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3398     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3399     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
3400     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
3401     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
3402     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
3403     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
3404     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
3405     case elfcpp::R_TILEGX_TLS_GD_CALL:
3406     case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
3407     case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
3408     case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
3409     case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
3410     case elfcpp::R_TILEGX_TLS_IE_LOAD:
3411     case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
3412     case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
3413     case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
3414     case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
3415       {
3416          bool output_is_shared = parameters->options().shared();
3417          const tls::Tls_optimization opt_t =
3418           Target_tilegx<size, big_endian>::optimize_tls_reloc(
3419             !output_is_shared, r_type);
3420
3421          switch (r_type)
3422            {
3423              case elfcpp::R_TILEGX_TLS_GD_CALL:
3424                // FIXME: predefine __tls_get_addr
3425                //
3426                // R_TILEGX_TLS_GD_CALL implicitly reference __tls_get_addr,
3427                // while all other target, x86/arm/mips/powerpc/sparc
3428                // generate tls relocation against __tls_get_addr explictly,
3429                // so for TILEGX, we need the following hack.
3430                if (opt_t == tls::TLSOPT_NONE) {
3431                  if (!target->tls_get_addr_sym_defined_) {
3432                    Symbol* sym = NULL;
3433                    options::parse_set(NULL, "__tls_get_addr",
3434                                      (gold::options::String_set*)
3435                                      &parameters->options().undefined());
3436                    symtab->add_undefined_symbols_from_command_line(layout);
3437                    target->tls_get_addr_sym_defined_ = true;
3438                    sym = symtab->lookup("__tls_get_addr");
3439                    sym->set_in_reg();
3440                  }
3441                  target->make_plt_entry(symtab, layout,
3442                                         symtab->lookup("__tls_get_addr"));
3443                }
3444                break;
3445
3446              // only make effect when applying relocation
3447              case elfcpp::R_TILEGX_TLS_IE_LOAD:
3448              case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
3449              case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
3450              case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
3451              case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
3452              case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
3453              case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
3454              case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
3455              case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
3456                break;
3457
3458              // GD: requires two GOT entry for module index and offset
3459              // IE: requires one GOT entry for tp-relative offset
3460              // LE: shouldn't happen for global symbol
3461              case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
3462              case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
3463              case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3464              case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3465              case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3466              case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3467                {
3468                  if (opt_t == tls::TLSOPT_NONE) {
3469                    Output_data_got<size, big_endian> *got
3470                       = target->got_section(symtab, layout);
3471                    unsigned int r_sym
3472                       = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3473                    unsigned int shndx = lsym.get_st_shndx();
3474                    bool is_ordinary;
3475                    shndx = object->adjust_sym_shndx(r_sym, shndx,
3476                                                     &is_ordinary);
3477                    if (!is_ordinary)
3478                      object->error(_("local symbol %u has bad shndx %u"),
3479                                    r_sym, shndx);
3480                    else
3481                      got->add_local_pair_with_rel(object, r_sym, shndx,
3482                                            GOT_TYPE_TLS_PAIR,
3483                                            target->rela_dyn_section(layout),
3484                                            size == 32
3485                                            ? elfcpp::R_TILEGX_TLS_DTPMOD32
3486                                            : elfcpp::R_TILEGX_TLS_DTPMOD64);
3487                   } else if (opt_t == tls::TLSOPT_TO_IE) {
3488                     Output_data_got<size, big_endian>* got
3489                        = target->got_section(symtab, layout);
3490                     Reloc_section* rela_dyn
3491                        = target->rela_dyn_section(layout);
3492                     unsigned int r_sym
3493                        = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3494                     unsigned int off = got->add_constant(0);
3495                     object->set_local_got_offset(r_sym,
3496                                                  GOT_TYPE_TLS_OFFSET,off);
3497                     rela_dyn->add_symbolless_local_addend(object, r_sym,
3498                                             size == 32
3499                                             ? elfcpp::R_TILEGX_TLS_TPOFF32
3500                                             : elfcpp::R_TILEGX_TLS_TPOFF64,
3501                                             got, off, 0);
3502                   } else if (opt_t != tls::TLSOPT_TO_LE)
3503                     // only TO_LE is allowed for local symbol
3504                     unsupported_reloc_local(object, r_type);
3505                }
3506                break;
3507
3508              // IE
3509              case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
3510              case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
3511              case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
3512              case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
3513              case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
3514              case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
3515                {
3516                  layout->set_has_static_tls();
3517                  if (opt_t == tls::TLSOPT_NONE) {
3518                    Output_data_got<size, big_endian>* got
3519                       = target->got_section(symtab, layout);
3520                    Reloc_section* rela_dyn
3521                       = target->rela_dyn_section(layout);
3522                    unsigned int r_sym
3523                       = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3524                    unsigned int off = got->add_constant(0);
3525                    object->set_local_got_offset(r_sym,
3526                                                 GOT_TYPE_TLS_OFFSET, off);
3527                    rela_dyn->add_symbolless_local_addend(object, r_sym,
3528                                             size == 32
3529                                             ? elfcpp::R_TILEGX_TLS_TPOFF32
3530                                             : elfcpp::R_TILEGX_TLS_TPOFF64,
3531                                             got, off, 0);
3532                  } else if (opt_t != tls::TLSOPT_TO_LE)
3533                    unsupported_reloc_local(object, r_type);
3534                }
3535                break;
3536
3537              // LE
3538              case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
3539              case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
3540              case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
3541              case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
3542              case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
3543              case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
3544                layout->set_has_static_tls();
3545                if (parameters->options().shared()) {
3546                  // defer to dynamic linker
3547                  gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
3548                  unsigned int r_sym
3549                     = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3550                  Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3551                  rela_dyn->add_symbolless_local_addend(object, r_sym, r_type,
3552                                                   output_section, data_shndx,
3553                                                   reloc.get_r_offset(), 0);
3554                }
3555                break;
3556
3557              default:
3558                gold_unreachable();
3559            }
3560       }
3561       break;
3562
3563     case elfcpp::R_TILEGX_COPY:
3564     case elfcpp::R_TILEGX_GLOB_DAT:
3565     case elfcpp::R_TILEGX_JMP_SLOT:
3566     case elfcpp::R_TILEGX_RELATIVE:
3567       // These are outstanding tls relocs, which are unexpected when linking
3568     case elfcpp::R_TILEGX_TLS_TPOFF32:
3569     case elfcpp::R_TILEGX_TLS_TPOFF64:
3570     case elfcpp::R_TILEGX_TLS_DTPMOD32:
3571     case elfcpp::R_TILEGX_TLS_DTPMOD64:
3572     case elfcpp::R_TILEGX_TLS_DTPOFF32:
3573     case elfcpp::R_TILEGX_TLS_DTPOFF64:
3574       gold_error(_("%s: unexpected reloc %u in object file"),
3575                  object->name().c_str(), r_type);
3576       break;
3577
3578     default:
3579       gold_error(_("%s: unsupported reloc %u against local symbol"),
3580                  object->name().c_str(), r_type);
3581       break;
3582     }
3583 }
3584
3585
3586 // Report an unsupported relocation against a global symbol.
3587
3588 template<int size, bool big_endian>
3589 void
3590 Target_tilegx<size, big_endian>::Scan::unsupported_reloc_global(
3591     Sized_relobj_file<size, big_endian>* object,
3592     unsigned int r_type,
3593     Symbol* gsym)
3594 {
3595   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3596              object->name().c_str(), r_type, gsym->demangled_name().c_str());
3597 }
3598
3599 // Returns true if this relocation type could be that of a function pointer.
3600 template<int size, bool big_endian>
3601 inline bool
3602 Target_tilegx<size, big_endian>::Scan::possible_function_pointer_reloc(
3603   unsigned int r_type)
3604 {
3605   switch (r_type)
3606     {
3607       case elfcpp::R_TILEGX_IMM16_X0_HW0:
3608       case elfcpp::R_TILEGX_IMM16_X1_HW0:
3609       case elfcpp::R_TILEGX_IMM16_X0_HW1:
3610       case elfcpp::R_TILEGX_IMM16_X1_HW1:
3611       case elfcpp::R_TILEGX_IMM16_X0_HW2:
3612       case elfcpp::R_TILEGX_IMM16_X1_HW2:
3613       case elfcpp::R_TILEGX_IMM16_X0_HW3:
3614       case elfcpp::R_TILEGX_IMM16_X1_HW3:
3615       case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST:
3616       case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST:
3617       case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST:
3618       case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST:
3619       case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST:
3620       case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST:
3621       case elfcpp::R_TILEGX_IMM16_X0_HW0_PCREL:
3622       case elfcpp::R_TILEGX_IMM16_X1_HW0_PCREL:
3623       case elfcpp::R_TILEGX_IMM16_X0_HW1_PCREL:
3624       case elfcpp::R_TILEGX_IMM16_X1_HW1_PCREL:
3625       case elfcpp::R_TILEGX_IMM16_X0_HW2_PCREL:
3626       case elfcpp::R_TILEGX_IMM16_X1_HW2_PCREL:
3627       case elfcpp::R_TILEGX_IMM16_X0_HW3_PCREL:
3628       case elfcpp::R_TILEGX_IMM16_X1_HW3_PCREL:
3629       case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
3630       case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
3631       case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
3632       case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
3633       case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
3634       case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
3635       case elfcpp::R_TILEGX_IMM16_X0_HW0_GOT:
3636       case elfcpp::R_TILEGX_IMM16_X1_HW0_GOT:
3637       case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_GOT:
3638       case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_GOT:
3639       case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_GOT:
3640       case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_GOT:
3641       {
3642         return true;
3643       }
3644     }
3645   return false;
3646 }
3647
3648 // For safe ICF, scan a relocation for a local symbol to check if it
3649 // corresponds to a function pointer being taken.  In that case mark
3650 // the function whose pointer was taken as not foldable.
3651
3652 template<int size, bool big_endian>
3653 inline bool
3654 Target_tilegx<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
3655   Symbol_table* ,
3656   Layout* ,
3657   Target_tilegx<size, big_endian>* ,
3658   Sized_relobj_file<size, big_endian>* ,
3659   unsigned int ,
3660   Output_section* ,
3661   const elfcpp::Rela<size, big_endian>& ,
3662   unsigned int r_type,
3663   const elfcpp::Sym<size, big_endian>&)
3664 {
3665   return possible_function_pointer_reloc(r_type);
3666 }
3667
3668 // For safe ICF, scan a relocation for a global symbol to check if it
3669 // corresponds to a function pointer being taken.  In that case mark
3670 // the function whose pointer was taken as not foldable.
3671
3672 template<int size, bool big_endian>
3673 inline bool
3674 Target_tilegx<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
3675   Symbol_table*,
3676   Layout* ,
3677   Target_tilegx<size, big_endian>* ,
3678   Sized_relobj_file<size, big_endian>* ,
3679   unsigned int ,
3680   Output_section* ,
3681   const elfcpp::Rela<size, big_endian>& ,
3682   unsigned int r_type,
3683   Symbol* gsym)
3684 {
3685   // GOT is not a function.
3686   if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
3687     return false;
3688
3689   // When building a shared library, do not fold symbols whose visibility
3690   // is hidden, internal or protected.
3691   return ((parameters->options().shared()
3692            && (gsym->visibility() == elfcpp::STV_INTERNAL
3693                || gsym->visibility() == elfcpp::STV_PROTECTED
3694                || gsym->visibility() == elfcpp::STV_HIDDEN))
3695           || possible_function_pointer_reloc(r_type));
3696 }
3697
3698 // Scan a relocation for a global symbol.
3699
3700 template<int size, bool big_endian>
3701 inline void
3702 Target_tilegx<size, big_endian>::Scan::global(Symbol_table* symtab,
3703                             Layout* layout,
3704                             Target_tilegx<size, big_endian>* target,
3705                             Sized_relobj_file<size, big_endian>* object,
3706                             unsigned int data_shndx,
3707                             Output_section* output_section,
3708                             const elfcpp::Rela<size, big_endian>& reloc,
3709                             unsigned int r_type,
3710                             Symbol* gsym)
3711 {
3712   // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
3713   // section.  We check here to avoid creating a dynamic reloc against
3714   // _GLOBAL_OFFSET_TABLE_.
3715   if (!target->has_got_section()
3716       && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
3717     target->got_section(symtab, layout);
3718
3719   // A STT_GNU_IFUNC symbol may require a PLT entry.
3720   if (gsym->type() == elfcpp::STT_GNU_IFUNC
3721       && this->reloc_needs_plt_for_ifunc(object, r_type))
3722     target->make_plt_entry(symtab, layout, gsym);
3723
3724   switch (r_type)
3725     {
3726     case elfcpp::R_TILEGX_NONE:
3727     case elfcpp::R_TILEGX_GNU_VTINHERIT:
3728     case elfcpp::R_TILEGX_GNU_VTENTRY:
3729       break;
3730
3731     case elfcpp::R_TILEGX_DEST_IMM8_X1:
3732     case elfcpp::R_TILEGX_IMM16_X0_HW0:
3733     case elfcpp::R_TILEGX_IMM16_X1_HW0:
3734     case elfcpp::R_TILEGX_IMM16_X0_HW1:
3735     case elfcpp::R_TILEGX_IMM16_X1_HW1:
3736     case elfcpp::R_TILEGX_IMM16_X0_HW2:
3737     case elfcpp::R_TILEGX_IMM16_X1_HW2:
3738     case elfcpp::R_TILEGX_IMM16_X0_HW3:
3739     case elfcpp::R_TILEGX_IMM16_X1_HW3:
3740     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST:
3741     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST:
3742     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST:
3743     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST:
3744     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST:
3745     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST:
3746     case elfcpp::R_TILEGX_64:
3747     case elfcpp::R_TILEGX_32:
3748     case elfcpp::R_TILEGX_16:
3749     case elfcpp::R_TILEGX_8:
3750       {
3751         // Make a PLT entry if necessary.
3752         if (gsym->needs_plt_entry())
3753           {
3754             target->make_plt_entry(symtab, layout, gsym);
3755             // Since this is not a PC-relative relocation, we may be
3756             // taking the address of a function. In that case we need to
3757             // set the entry in the dynamic symbol table to the address of
3758             // the PLT entry.
3759             if (gsym->is_from_dynobj() && !parameters->options().shared())
3760               gsym->set_needs_dynsym_value();
3761           }
3762         // Make a dynamic relocation if necessary.
3763         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3764           {
3765             if (!parameters->options().output_is_position_independent()
3766                 && gsym->may_need_copy_reloc())
3767               {
3768                 target->copy_reloc(symtab, layout, object,
3769                                    data_shndx, output_section, gsym, reloc);
3770               }
3771             else if (((size == 64 && r_type == elfcpp::R_TILEGX_64)
3772                       || (size == 32 && r_type == elfcpp::R_TILEGX_32))
3773                      && gsym->type() == elfcpp::STT_GNU_IFUNC
3774                      && gsym->can_use_relative_reloc(false)
3775                      && !gsym->is_from_dynobj()
3776                      && !gsym->is_undefined()
3777                      && !gsym->is_preemptible())
3778               {
3779                 // Use an IRELATIVE reloc for a locally defined
3780                 // STT_GNU_IFUNC symbol.  This makes a function
3781                 // address in a PIE executable match the address in a
3782                 // shared library that it links against.
3783                 Reloc_section* rela_dyn =
3784                   target->rela_irelative_section(layout);
3785                 unsigned int r_type = elfcpp::R_TILEGX_IRELATIVE;
3786                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
3787                                                    output_section, object,
3788                                                    data_shndx,
3789                                                    reloc.get_r_offset(),
3790                                                    reloc.get_r_addend());
3791               } else if ((r_type == elfcpp::R_TILEGX_64
3792                           || r_type == elfcpp::R_TILEGX_32)
3793                          && gsym->can_use_relative_reloc(false))
3794               {
3795                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3796                 rela_dyn->add_global_relative(gsym, elfcpp::R_TILEGX_RELATIVE,
3797                                               output_section, object,
3798                                               data_shndx,
3799                                               reloc.get_r_offset(),
3800                                               reloc.get_r_addend(), false);
3801               }
3802             else
3803               {
3804                 this->check_non_pic(object, r_type);
3805                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3806                 rela_dyn->add_global(gsym, r_type, output_section, object,
3807                                      data_shndx, reloc.get_r_offset(),
3808                                      reloc.get_r_addend());
3809               }
3810           }
3811       }
3812       break;
3813
3814     case elfcpp::R_TILEGX_BROFF_X1:
3815     case elfcpp::R_TILEGX_IMM16_X0_HW0_PCREL:
3816     case elfcpp::R_TILEGX_IMM16_X1_HW0_PCREL:
3817     case elfcpp::R_TILEGX_IMM16_X0_HW1_PCREL:
3818     case elfcpp::R_TILEGX_IMM16_X1_HW1_PCREL:
3819     case elfcpp::R_TILEGX_IMM16_X0_HW2_PCREL:
3820     case elfcpp::R_TILEGX_IMM16_X1_HW2_PCREL:
3821     case elfcpp::R_TILEGX_IMM16_X0_HW3_PCREL:
3822     case elfcpp::R_TILEGX_IMM16_X1_HW3_PCREL:
3823     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
3824     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
3825     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
3826     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
3827     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
3828     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
3829     case elfcpp::R_TILEGX_64_PCREL:
3830     case elfcpp::R_TILEGX_32_PCREL:
3831     case elfcpp::R_TILEGX_16_PCREL:
3832     case elfcpp::R_TILEGX_8_PCREL:
3833       {
3834         // Make a PLT entry if necessary.
3835         if (gsym->needs_plt_entry())
3836           target->make_plt_entry(symtab, layout, gsym);
3837         // Make a dynamic relocation if necessary.
3838         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
3839           {
3840             if (parameters->options().output_is_executable()
3841                 && gsym->may_need_copy_reloc())
3842               {
3843                 target->copy_reloc(symtab, layout, object,
3844                                    data_shndx, output_section, gsym, reloc);
3845               }
3846             else
3847               {
3848                 this->check_non_pic(object, r_type);
3849                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3850                 rela_dyn->add_global(gsym, r_type, output_section, object,
3851                                      data_shndx, reloc.get_r_offset(),
3852                                      reloc.get_r_addend());
3853               }
3854           }
3855       }
3856       break;
3857
3858     case elfcpp::R_TILEGX_IMM16_X0_HW0_GOT:
3859     case elfcpp::R_TILEGX_IMM16_X1_HW0_GOT:
3860     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_GOT:
3861     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_GOT:
3862     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_GOT:
3863     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_GOT:
3864       {
3865         // The symbol requires a GOT entry.
3866         Output_data_got<size, big_endian>* got
3867            = target->got_section(symtab, layout);
3868         if (gsym->final_value_is_known())
3869           {
3870             // For a STT_GNU_IFUNC symbol we want the PLT address.
3871             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3872               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3873             else
3874               got->add_global(gsym, GOT_TYPE_STANDARD);
3875           }
3876         else
3877           {
3878             // If this symbol is not fully resolved, we need to add a
3879             // dynamic relocation for it.
3880             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3881
3882             // Use a GLOB_DAT rather than a RELATIVE reloc if:
3883             //
3884             // 1) The symbol may be defined in some other module.
3885             //
3886             // 2) We are building a shared library and this is a
3887             // protected symbol; using GLOB_DAT means that the dynamic
3888             // linker can use the address of the PLT in the main
3889             // executable when appropriate so that function address
3890             // comparisons work.
3891             //
3892             // 3) This is a STT_GNU_IFUNC symbol in position dependent
3893             // code, again so that function address comparisons work.
3894             if (gsym->is_from_dynobj()
3895                 || gsym->is_undefined()
3896                 || gsym->is_preemptible()
3897                 || (gsym->visibility() == elfcpp::STV_PROTECTED
3898                     && parameters->options().shared())
3899                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3900                     && parameters->options().output_is_position_independent()))
3901               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3902                                        elfcpp::R_TILEGX_GLOB_DAT);
3903             else
3904               {
3905                 // For a STT_GNU_IFUNC symbol we want to write the PLT
3906                 // offset into the GOT, so that function pointer
3907                 // comparisons work correctly.
3908                 bool is_new;
3909                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3910                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3911                 else
3912                   {
3913                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3914                     // Tell the dynamic linker to use the PLT address
3915                     // when resolving relocations.
3916                     if (gsym->is_from_dynobj()
3917                         && !parameters->options().shared())
3918                       gsym->set_needs_dynsym_value();
3919                   }
3920                 if (is_new)
3921                   {
3922                     unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3923                     rela_dyn->add_global_relative(gsym,
3924                                                   r_type,
3925                                                   got, got_off, 0, false);
3926                   }
3927               }
3928           }
3929       }
3930       break;
3931
3932     // a minor difference here for R_TILEGX_JUMPOFF_X1
3933     // between bfd linker and gold linker for gold, when
3934     // R_TILEGX_JUMPOFF_X1 against global symbol, we
3935     // turn it into JUMPOFF_X1_PLT, otherwise the distance
3936     // to the symbol function may overflow at runtime.
3937     case elfcpp::R_TILEGX_JUMPOFF_X1:
3938
3939     case elfcpp::R_TILEGX_JUMPOFF_X1_PLT:
3940     case elfcpp::R_TILEGX_IMM16_X0_HW0_PLT_PCREL:
3941     case elfcpp::R_TILEGX_IMM16_X1_HW0_PLT_PCREL:
3942     case elfcpp::R_TILEGX_IMM16_X0_HW1_PLT_PCREL:
3943     case elfcpp::R_TILEGX_IMM16_X1_HW1_PLT_PCREL:
3944     case elfcpp::R_TILEGX_IMM16_X0_HW2_PLT_PCREL:
3945     case elfcpp::R_TILEGX_IMM16_X1_HW2_PLT_PCREL:
3946     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL:
3947     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL:
3948     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL:
3949     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL:
3950     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL:
3951     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL:
3952       // If the symbol is fully resolved, this is just a PC32 reloc.
3953       // Otherwise we need a PLT entry.
3954       if (gsym->final_value_is_known())
3955         break;
3956       // If building a shared library, we can also skip the PLT entry
3957       // if the symbol is defined in the output file and is protected
3958       // or hidden.
3959       if (gsym->is_defined()
3960           && !gsym->is_from_dynobj()
3961           && !gsym->is_preemptible())
3962         break;
3963       target->make_plt_entry(symtab, layout, gsym);
3964       break;
3965
3966
3967     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
3968     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
3969     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
3970     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
3971     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
3972     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
3973     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
3974     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
3975     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
3976     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
3977     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
3978     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
3979     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
3980     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
3981     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
3982     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
3983     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
3984     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
3985     case elfcpp::R_TILEGX_TLS_GD_CALL:
3986     case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
3987     case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
3988     case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
3989     case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
3990     case elfcpp::R_TILEGX_TLS_IE_LOAD:
3991     case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
3992     case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
3993     case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
3994     case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
3995       {
3996          const bool is_final = gsym->final_value_is_known();
3997          const tls::Tls_optimization opt_t =
3998           Target_tilegx<size, big_endian>::optimize_tls_reloc(is_final,
3999                                                               r_type);
4000
4001          switch (r_type)
4002            {
4003               // only expand to plt against __tls_get_addr in GD model
4004               case elfcpp::R_TILEGX_TLS_GD_CALL:
4005                 if (opt_t == tls::TLSOPT_NONE) {
4006                   // FIXME:  it's better '__tls_get_addr' referenced explictly
4007                   if (!target->tls_get_addr_sym_defined_) {
4008                     Symbol* sym = NULL;
4009                     options::parse_set(NULL, "__tls_get_addr",
4010                                        (gold::options::String_set*)
4011                                        &parameters->options().undefined());
4012                     symtab->add_undefined_symbols_from_command_line(layout);
4013                     target->tls_get_addr_sym_defined_ = true;
4014                     sym = symtab->lookup("__tls_get_addr");
4015                     sym->set_in_reg();
4016                   }
4017                   target->make_plt_entry(symtab, layout,
4018                                          symtab->lookup("__tls_get_addr"));
4019                 }
4020                 break;
4021
4022               // only make effect when applying relocation
4023               case elfcpp::R_TILEGX_TLS_IE_LOAD:
4024               case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
4025               case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
4026               case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
4027               case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
4028               case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
4029               case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
4030               case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
4031               case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
4032                 break;
4033
4034               // GD: requires two GOT entry for module index and offset
4035               // IE: requires one GOT entry for tp-relative offset
4036               // LE: shouldn't happen for global symbol
4037               case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
4038               case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
4039               case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
4040               case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
4041               case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
4042               case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
4043                 {
4044                   if (opt_t == tls::TLSOPT_NONE) {
4045                       Output_data_got<size, big_endian>* got
4046                         = target->got_section(symtab, layout);
4047                       got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
4048                                              target->rela_dyn_section(layout),
4049                                              size == 32
4050                                            ? elfcpp::R_TILEGX_TLS_DTPMOD32
4051                                            : elfcpp::R_TILEGX_TLS_DTPMOD64,
4052                                              size == 32
4053                                            ? elfcpp::R_TILEGX_TLS_DTPOFF32
4054                                            : elfcpp::R_TILEGX_TLS_DTPOFF64);
4055                   } else if (opt_t == tls::TLSOPT_TO_IE) {
4056                     // Create a GOT entry for the tp-relative offset.
4057                     Output_data_got<size, big_endian>* got
4058                        = target->got_section(symtab, layout);
4059                     got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4060                                            target->rela_dyn_section(layout),
4061                                            size == 32
4062                                            ? elfcpp::R_TILEGX_TLS_TPOFF32
4063                                            : elfcpp::R_TILEGX_TLS_TPOFF64);
4064                   } else if (opt_t != tls::TLSOPT_TO_LE)
4065                     // exteranl symbol should not be optimized to TO_LE
4066                     unsupported_reloc_global(object, r_type, gsym);
4067                 }
4068                 break;
4069
4070               // IE
4071               case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
4072               case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
4073               case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
4074               case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
4075               case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
4076               case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
4077                 {
4078                     layout->set_has_static_tls();
4079                   if (opt_t == tls::TLSOPT_NONE) {
4080                     // Create a GOT entry for the tp-relative offset.
4081                     Output_data_got<size, big_endian>* got
4082                        = target->got_section(symtab, layout);
4083                     got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
4084                                            target->rela_dyn_section(layout),
4085                                            size == 32
4086                                            ? elfcpp::R_TILEGX_TLS_TPOFF32
4087                                            : elfcpp::R_TILEGX_TLS_TPOFF64);
4088                   } else if (opt_t != tls::TLSOPT_TO_LE)
4089                     unsupported_reloc_global(object, r_type, gsym);
4090                 }
4091                 break;
4092
4093               // LE
4094               case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
4095               case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
4096               case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
4097               case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
4098               case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
4099               case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
4100                   layout->set_has_static_tls();
4101                 if (parameters->options().shared()) {
4102                   // defer to dynamic linker
4103                   Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4104                   rela_dyn->add_symbolless_global_addend(gsym, r_type,
4105                                                       output_section, object,
4106                                                       data_shndx,
4107                                                       reloc.get_r_offset(), 0);
4108                   }
4109                 break;
4110
4111               default:
4112                 gold_unreachable();
4113            }
4114       }
4115       break;
4116
4117     // below are outstanding relocs
4118     // should not existed in static linking stage
4119     case elfcpp::R_TILEGX_COPY:
4120     case elfcpp::R_TILEGX_GLOB_DAT:
4121     case elfcpp::R_TILEGX_JMP_SLOT:
4122     case elfcpp::R_TILEGX_RELATIVE:
4123     case elfcpp::R_TILEGX_TLS_TPOFF32:
4124     case elfcpp::R_TILEGX_TLS_TPOFF64:
4125     case elfcpp::R_TILEGX_TLS_DTPMOD32:
4126     case elfcpp::R_TILEGX_TLS_DTPMOD64:
4127     case elfcpp::R_TILEGX_TLS_DTPOFF32:
4128     case elfcpp::R_TILEGX_TLS_DTPOFF64:
4129       gold_error(_("%s: unexpected reloc %u in object file"),
4130                  object->name().c_str(), r_type);
4131       break;
4132
4133     default:
4134       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4135                  object->name().c_str(), r_type,
4136                  gsym->demangled_name().c_str());
4137       break;
4138     }
4139 }
4140
4141 template<int size, bool big_endian>
4142 void
4143 Target_tilegx<size, big_endian>::gc_process_relocs(Symbol_table* symtab,
4144                                   Layout* layout,
4145                                   Sized_relobj_file<size, big_endian>* object,
4146                                   unsigned int data_shndx,
4147                                   unsigned int sh_type,
4148                                   const unsigned char* prelocs,
4149                                   size_t reloc_count,
4150                                   Output_section* output_section,
4151                                   bool needs_special_offset_handling,
4152                                   size_t local_symbol_count,
4153                                   const unsigned char* plocal_symbols)
4154 {
4155   typedef Target_tilegx<size, big_endian> Tilegx;
4156   typedef typename Target_tilegx<size, big_endian>::Scan Scan;
4157
4158   if (sh_type == elfcpp::SHT_REL)
4159     {
4160       return;
4161     }
4162
4163    gold::gc_process_relocs<size, big_endian,
4164                            Tilegx, elfcpp::SHT_RELA, Scan,
4165       typename Target_tilegx<size, big_endian>::Relocatable_size_for_reloc>(
4166           symtab,
4167           layout,
4168           this,
4169           object,
4170           data_shndx,
4171           prelocs,
4172           reloc_count,
4173           output_section,
4174           needs_special_offset_handling,
4175           local_symbol_count,
4176           plocal_symbols);
4177 }
4178 // Scan relocations for a section.
4179
4180 template<int size, bool big_endian>
4181 void
4182 Target_tilegx<size, big_endian>::scan_relocs(Symbol_table* symtab,
4183                                  Layout* layout,
4184                                  Sized_relobj_file<size, big_endian>* object,
4185                                  unsigned int data_shndx,
4186                                  unsigned int sh_type,
4187                                  const unsigned char* prelocs,
4188                                  size_t reloc_count,
4189                                  Output_section* output_section,
4190                                  bool needs_special_offset_handling,
4191                                  size_t local_symbol_count,
4192                                  const unsigned char* plocal_symbols)
4193 {
4194   typedef Target_tilegx<size, big_endian> Tilegx;
4195   typedef typename Target_tilegx<size, big_endian>::Scan Scan;
4196
4197   if (sh_type == elfcpp::SHT_REL)
4198     {
4199       gold_error(_("%s: unsupported REL reloc section"),
4200                  object->name().c_str());
4201       return;
4202     }
4203
4204   gold::scan_relocs<size, big_endian, Tilegx, elfcpp::SHT_RELA, Scan>(
4205     symtab,
4206     layout,
4207     this,
4208     object,
4209     data_shndx,
4210     prelocs,
4211     reloc_count,
4212     output_section,
4213     needs_special_offset_handling,
4214     local_symbol_count,
4215     plocal_symbols);
4216 }
4217
4218 template<int size, bool big_endian>
4219 void
4220 Target_tilegx<size, big_endian>::do_define_standard_symbols(
4221     Symbol_table* symtab,
4222     Layout* layout)
4223 {
4224   Output_section* feedback_section = layout->find_output_section(".feedback");
4225
4226   if (feedback_section != NULL)
4227     {
4228       symtab->define_in_output_data("__feedback_section_end",
4229                     NULL,
4230                     Symbol_table::PREDEFINED,
4231                     feedback_section,
4232                     0,
4233                     0,
4234                     elfcpp::STT_NOTYPE,
4235                     elfcpp::STB_GLOBAL,
4236                     elfcpp::STV_HIDDEN,
4237                     0,
4238                     true, // offset_is_from_end
4239                     false);
4240     }
4241 }
4242
4243 // Finalize the sections.
4244
4245 template<int size, bool big_endian>
4246 void
4247 Target_tilegx<size, big_endian>::do_finalize_sections(
4248     Layout* layout,
4249     const Input_objects*,
4250     Symbol_table* symtab)
4251 {
4252   const Reloc_section* rel_plt = (this->plt_ == NULL
4253                                   ? NULL
4254                                   : this->plt_->rela_plt());
4255   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
4256                                   this->rela_dyn_, true, true);
4257
4258   // Emit any relocs we saved in an attempt to avoid generating COPY
4259   // relocs.
4260   if (this->copy_relocs_.any_saved_relocs())
4261     this->copy_relocs_.emit(this->rela_dyn_section(layout));
4262
4263   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4264   // the .got section.
4265   Symbol* sym = this->global_offset_table_;
4266   if (sym != NULL)
4267     {
4268       uint64_t data_size = this->got_->current_data_size();
4269       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
4270
4271       // If the .got section is more than 0x8000 bytes, we add
4272       // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
4273       // bit relocations have a greater chance of working.
4274       if (data_size >= 0x8000)
4275         symtab->get_sized_symbol<size>(sym)->set_value(
4276           symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
4277     }
4278
4279   if (parameters->doing_static_link()
4280       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
4281     {
4282       // If linking statically, make sure that the __rela_iplt symbols
4283       // were defined if necessary, even if we didn't create a PLT.
4284       static const Define_symbol_in_segment syms[] =
4285         {
4286           {
4287             "__rela_iplt_start",        // name
4288             elfcpp::PT_LOAD,            // segment_type
4289             elfcpp::PF_W,               // segment_flags_set
4290             elfcpp::PF(0),              // segment_flags_clear
4291             0,                          // value
4292             0,                          // size
4293             elfcpp::STT_NOTYPE,         // type
4294             elfcpp::STB_GLOBAL,         // binding
4295             elfcpp::STV_HIDDEN,         // visibility
4296             0,                          // nonvis
4297             Symbol::SEGMENT_START,      // offset_from_base
4298             true                        // only_if_ref
4299           },
4300           {
4301             "__rela_iplt_end",          // name
4302             elfcpp::PT_LOAD,            // segment_type
4303             elfcpp::PF_W,               // segment_flags_set
4304             elfcpp::PF(0),              // segment_flags_clear
4305             0,                          // value
4306             0,                          // size
4307             elfcpp::STT_NOTYPE,         // type
4308             elfcpp::STB_GLOBAL,         // binding
4309             elfcpp::STV_HIDDEN,         // visibility
4310             0,                          // nonvis
4311             Symbol::SEGMENT_START,      // offset_from_base
4312             true                        // only_if_ref
4313           }
4314         };
4315
4316       symtab->define_symbols(layout, 2, syms,
4317                              layout->script_options()->saw_sections_clause());
4318     }
4319 }
4320
4321 // Perform a relocation.
4322
4323 template<int size, bool big_endian>
4324 inline bool
4325 Target_tilegx<size, big_endian>::Relocate::relocate(
4326     const Relocate_info<size, big_endian>* relinfo,
4327     unsigned int,
4328     Target_tilegx<size, big_endian>* target,
4329     Output_section*,
4330     size_t relnum,
4331     const unsigned char* preloc,
4332     const Sized_symbol<size>* gsym,
4333     const Symbol_value<size>* psymval,
4334     unsigned char* view,
4335     typename elfcpp::Elf_types<size>::Elf_Addr address,
4336     section_size_type)
4337 {
4338   if (view == NULL)
4339     return true;
4340
4341   typedef Tilegx_relocate_functions<size, big_endian> TilegxReloc;
4342   typename TilegxReloc::Tilegx_howto r_howto;
4343
4344   const elfcpp::Rela<size, big_endian> rela(preloc);
4345   unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4346   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
4347
4348   // Pick the value to use for symbols defined in the PLT.
4349   Symbol_value<size> symval;
4350   if (gsym != NULL
4351       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4352     {
4353       symval.set_output_value(target->plt_address_for_global(gsym));
4354       psymval = &symval;
4355     }
4356   else if (gsym == NULL && psymval->is_ifunc_symbol())
4357     {
4358       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4359       if (object->local_has_plt_offset(r_sym))
4360         {
4361           symval.set_output_value(target->plt_address_for_local(object, r_sym));
4362           psymval = &symval;
4363         }
4364     }
4365
4366   elfcpp::Elf_Xword addend = rela.get_r_addend();
4367
4368   // Get the GOT offset if needed.
4369   // For tilegx, the GOT pointer points to the start of the GOT section.
4370   bool have_got_offset = false;
4371   int got_offset = 0;
4372   int got_base = target->got_ != NULL
4373                  ? target->got_->current_data_size() >= 0x8000 ? 0x8000 : 0
4374                  : 0;
4375   unsigned int got_type = GOT_TYPE_STANDARD;
4376   bool always_apply_relocation = false;
4377   switch (r_type)
4378     {
4379     case elfcpp::R_TILEGX_IMM16_X0_HW0_GOT:
4380     case elfcpp::R_TILEGX_IMM16_X1_HW0_GOT:
4381     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_GOT:
4382     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_GOT:
4383     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_GOT:
4384     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_GOT:
4385       if (gsym != NULL)
4386         {
4387           gold_assert(gsym->has_got_offset(got_type));
4388           got_offset = gsym->got_offset(got_type) - got_base;
4389         }
4390       else
4391         {
4392           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4393           gold_assert(object->local_has_got_offset(r_sym, got_type));
4394           got_offset =
4395             object->local_got_offset(r_sym, got_type) - got_base;
4396         }
4397       have_got_offset = true;
4398       break;
4399
4400     default:
4401       break;
4402     }
4403
4404   r_howto = TilegxReloc::howto[r_type];
4405   switch (r_type)
4406     {
4407     case elfcpp::R_TILEGX_NONE:
4408     case elfcpp::R_TILEGX_GNU_VTINHERIT:
4409     case elfcpp::R_TILEGX_GNU_VTENTRY:
4410       break;
4411
4412     case elfcpp::R_TILEGX_IMM16_X0_HW0_GOT:
4413     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_GOT:
4414     case elfcpp::R_TILEGX_IMM16_X1_HW0_GOT:
4415     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_GOT:
4416     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_GOT:
4417     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_GOT:
4418       gold_assert(have_got_offset);
4419       symval.set_output_value(got_offset);
4420       psymval = &symval;
4421       always_apply_relocation = true;
4422       addend = 0;
4423
4424     // when under PIC mode, these relocations are deferred to rtld
4425     case elfcpp::R_TILEGX_IMM16_X0_HW0:
4426     case elfcpp::R_TILEGX_IMM16_X1_HW0:
4427     case elfcpp::R_TILEGX_IMM16_X0_HW1:
4428     case elfcpp::R_TILEGX_IMM16_X1_HW1:
4429     case elfcpp::R_TILEGX_IMM16_X0_HW2:
4430     case elfcpp::R_TILEGX_IMM16_X1_HW2:
4431     case elfcpp::R_TILEGX_IMM16_X0_HW3:
4432     case elfcpp::R_TILEGX_IMM16_X1_HW3:
4433     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST:
4434     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST:
4435     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST:
4436     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST:
4437     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST:
4438     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST:
4439       if (always_apply_relocation
4440           || !parameters->options().output_is_position_independent())
4441         TilegxReloc::imm_x_general(view, object, psymval, addend, r_howto);
4442       break;
4443
4444     case elfcpp::R_TILEGX_JUMPOFF_X1:
4445     case elfcpp::R_TILEGX_JUMPOFF_X1_PLT:
4446       gold_assert(gsym == NULL
4447                   || gsym->has_plt_offset()
4448                   || gsym->final_value_is_known()
4449                   || (gsym->is_defined()
4450                       && !gsym->is_from_dynobj()
4451                       && !gsym->is_preemptible()));
4452       TilegxReloc::imm_x_pcrel_general(view, object, psymval, addend,
4453                                        address, r_howto);
4454       break;
4455
4456
4457     case elfcpp::R_TILEGX_IMM16_X0_HW0_PLT_PCREL:
4458     case elfcpp::R_TILEGX_IMM16_X0_HW0_PCREL:
4459     case elfcpp::R_TILEGX_IMM16_X1_HW0_PLT_PCREL:
4460     case elfcpp::R_TILEGX_IMM16_X1_HW0_PCREL:
4461     case elfcpp::R_TILEGX_IMM16_X0_HW1_PLT_PCREL:
4462     case elfcpp::R_TILEGX_IMM16_X0_HW1_PCREL:
4463     case elfcpp::R_TILEGX_IMM16_X1_HW1_PLT_PCREL:
4464     case elfcpp::R_TILEGX_IMM16_X1_HW1_PCREL:
4465     case elfcpp::R_TILEGX_IMM16_X0_HW2_PLT_PCREL:
4466     case elfcpp::R_TILEGX_IMM16_X0_HW2_PCREL:
4467     case elfcpp::R_TILEGX_IMM16_X1_HW2_PLT_PCREL:
4468     case elfcpp::R_TILEGX_IMM16_X1_HW2_PCREL:
4469     case elfcpp::R_TILEGX_IMM16_X0_HW3_PCREL:
4470     case elfcpp::R_TILEGX_IMM16_X1_HW3_PCREL:
4471     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL:
4472     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_PCREL:
4473     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL:
4474     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_PCREL:
4475     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL:
4476     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_PCREL:
4477     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL:
4478     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_PCREL:
4479     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL:
4480     case elfcpp::R_TILEGX_IMM16_X0_HW2_LAST_PCREL:
4481     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL:
4482     case elfcpp::R_TILEGX_IMM16_X1_HW2_LAST_PCREL:
4483       TilegxReloc::imm_x_pcrel_general(view, object, psymval, addend,
4484                                        address, r_howto);
4485       break;
4486
4487     case elfcpp::R_TILEGX_BROFF_X1:
4488     case elfcpp::R_TILEGX_DEST_IMM8_X1:
4489       TilegxReloc::imm_x_two_part_general(view, object, psymval,
4490                                           addend, address, r_type);
4491       break;
4492
4493
4494     // below are general relocation types, which can be
4495     // handled by target-independent handlers
4496     case elfcpp::R_TILEGX_64:
4497       TilegxReloc::abs64(view, object, psymval, addend);
4498       break;
4499
4500     case elfcpp::R_TILEGX_64_PCREL:
4501       TilegxReloc::pc_abs64(view, object, psymval, addend, address);
4502       break;
4503
4504     case elfcpp::R_TILEGX_32:
4505       TilegxReloc::abs32(view, object, psymval, addend);
4506       break;
4507
4508     case elfcpp::R_TILEGX_32_PCREL:
4509       TilegxReloc::pc_abs32(view, object, psymval, addend, address);
4510       break;
4511
4512     case elfcpp::R_TILEGX_16:
4513       TilegxReloc::abs16(view, object, psymval, addend);
4514       break;
4515
4516     case elfcpp::R_TILEGX_16_PCREL:
4517       TilegxReloc::pc_abs16(view, object, psymval, addend, address);
4518       break;
4519
4520     case elfcpp::R_TILEGX_8:
4521       Relocate_functions<size, big_endian>::rela8(view, object,
4522                                                   psymval, addend);
4523       break;
4524
4525     case elfcpp::R_TILEGX_8_PCREL:
4526       Relocate_functions<size, big_endian>::pcrela8(view, object,
4527                                                     psymval, addend, address);
4528       break;
4529
4530     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
4531     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
4532     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
4533     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
4534     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
4535     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
4536     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
4537     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
4538     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
4539     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
4540     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
4541     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
4542     case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
4543     case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
4544     case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
4545     case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
4546     case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
4547     case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
4548     case elfcpp::R_TILEGX_TLS_GD_CALL:
4549     case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
4550     case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
4551     case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
4552     case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
4553     case elfcpp::R_TILEGX_TLS_IE_LOAD:
4554     case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
4555     case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
4556     case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
4557     case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
4558       {
4559         const bool is_final = (gsym == NULL
4560                                ? !parameters->options().shared()
4561                                : gsym->final_value_is_known());
4562         tls::Tls_optimization opt_t =
4563           Target_tilegx<size, big_endian>::optimize_tls_reloc(is_final,
4564                                                               r_type);
4565
4566         switch (r_type)
4567           {
4568
4569             case elfcpp::R_TILEGX_TLS_GD_CALL:
4570               {
4571                 if (opt_t == tls::TLSOPT_NONE) {
4572                   Symbol *tls_sym = relinfo->symtab->lookup("__tls_get_addr");
4573                   symval.set_output_value(
4574                     target->plt_address_for_global(tls_sym));
4575                   psymval = &symval;
4576                   TilegxReloc::imm_x_pcrel_general(view, object, psymval,
4577                                                    addend, address, r_howto);
4578                 }
4579                 else if (opt_t == tls::TLSOPT_TO_IE
4580                          || opt_t == tls::TLSOPT_TO_LE)
4581                   TilegxReloc::tls_relax(view, r_type, opt_t);
4582               }
4583               break;
4584
4585             // XX_TLS_GD is the same as normal X_GOT relocation
4586             // except allocating a got entry pair,
4587             case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_GD:
4588             case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_GD:
4589             case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
4590             case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
4591             case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
4592             case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
4593               if (opt_t == tls::TLSOPT_NONE) {
4594                 got_type = GOT_TYPE_TLS_PAIR;
4595                 have_got_offset = true;
4596               } else if (opt_t == tls::TLSOPT_TO_IE) {
4597                 got_type = GOT_TYPE_TLS_OFFSET;
4598                 have_got_offset = true;
4599               }
4600               goto do_update_value;
4601             // XX_TLS_IE is the same as normal X_GOT relocation
4602             // except allocating one additional runtime relocation
4603             case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_IE:
4604             case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_IE:
4605             case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
4606             case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
4607             case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
4608             case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
4609               if (opt_t == tls::TLSOPT_NONE) {
4610                 got_type = GOT_TYPE_TLS_OFFSET;
4611                 have_got_offset = true;
4612               }
4613             do_update_value:
4614               if (have_got_offset) {
4615                 if (gsym != NULL) {
4616                   gold_assert(gsym->has_got_offset(got_type));
4617                   got_offset = gsym->got_offset(got_type) - got_base;
4618                 } else {
4619                   unsigned int r_sym
4620                      = elfcpp::elf_r_sym<size>(rela.get_r_info());
4621                   gold_assert(object->local_has_got_offset(r_sym, got_type));
4622                   got_offset =
4623                     object->local_got_offset(r_sym, got_type) - got_base;
4624                 }
4625               }
4626
4627               if (opt_t == tls::TLSOPT_NONE
4628                   || opt_t == tls::TLSOPT_TO_IE) {
4629                 // for both GD/IE, these relocations
4630                 // actually calculate got offset, so
4631                 // there behavior are the same
4632                 gold_assert(have_got_offset);
4633                 symval.set_output_value(got_offset);
4634                 psymval = &symval;
4635                 addend = 0;
4636                 TilegxReloc::imm_x_general(view, object, psymval,
4637                                            addend, r_howto);
4638                 break;
4639               } // else if (opt_t == tls::TLSOPT_TO_LE)
4640                 //   both GD/IE are turned into LE, which
4641                 //   is absolute relocation.
4642                 //
4643                 //  |  go through
4644                 //  |
4645                 //  V
4646             // LE
4647             //
4648             // tp
4649             // |
4650             // V
4651             //  t_var1 | t_var2 | t_var3 | ...
4652             //  --------------------------------------------------
4653             //
4654             //  so offset to tp should be negative, we get offset
4655             //  from the following formular for LE
4656             //
4657             //    t_var1_off = t_var1_sym_value - tls_section_start
4658             //
4659             case elfcpp::R_TILEGX_IMM16_X0_HW0_TLS_LE:
4660             case elfcpp::R_TILEGX_IMM16_X1_HW0_TLS_LE:
4661             case elfcpp::R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE:
4662             case elfcpp::R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE:
4663             case elfcpp::R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE:
4664             case elfcpp::R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE:
4665               {
4666                 Output_segment *tls_segment = relinfo->layout->tls_segment();
4667                 if (tls_segment == NULL) {
4668                   gold_assert(parameters->errors()->error_count() > 0
4669                               || issue_undefined_symbol_error(gsym));
4670                   return false;
4671                 }
4672
4673                 typename elfcpp::Elf_types<size>::Elf_Addr value
4674                   = psymval->value(relinfo->object, 0);
4675                 symval.set_output_value(value);
4676                 psymval = &symval;
4677                 TilegxReloc::imm_x_general(view, object, psymval,
4678                                            addend, r_howto);
4679               }
4680               break;
4681
4682             // tls relaxation
4683             case elfcpp::R_TILEGX_TLS_IE_LOAD:
4684             case elfcpp::R_TILEGX_IMM8_X0_TLS_ADD:
4685             case elfcpp::R_TILEGX_IMM8_X1_TLS_ADD:
4686             case elfcpp::R_TILEGX_IMM8_Y0_TLS_ADD:
4687             case elfcpp::R_TILEGX_IMM8_Y1_TLS_ADD:
4688             case elfcpp::R_TILEGX_IMM8_X0_TLS_GD_ADD:
4689             case elfcpp::R_TILEGX_IMM8_X1_TLS_GD_ADD:
4690             case elfcpp::R_TILEGX_IMM8_Y0_TLS_GD_ADD:
4691             case elfcpp::R_TILEGX_IMM8_Y1_TLS_GD_ADD:
4692               TilegxReloc::tls_relax(view, r_type, opt_t);
4693               break;
4694
4695             default:
4696               gold_unreachable();
4697           }
4698       }
4699       break;
4700
4701     // below are outstanding relocs
4702     // should not existed in static linking stage
4703     case elfcpp::R_TILEGX_COPY:
4704     case elfcpp::R_TILEGX_GLOB_DAT:
4705     case elfcpp::R_TILEGX_JMP_SLOT:
4706     case elfcpp::R_TILEGX_RELATIVE:
4707     case elfcpp::R_TILEGX_TLS_TPOFF32:
4708     case elfcpp::R_TILEGX_TLS_TPOFF64:
4709     case elfcpp::R_TILEGX_TLS_DTPMOD32:
4710     case elfcpp::R_TILEGX_TLS_DTPMOD64:
4711     case elfcpp::R_TILEGX_TLS_DTPOFF32:
4712     case elfcpp::R_TILEGX_TLS_DTPOFF64:
4713       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4714                              _("unexpected reloc %u in object file"),
4715                              r_type);
4716       break;
4717
4718     default:
4719       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4720                              _("unsupported reloc %u"),
4721                              r_type);
4722       break;
4723     }
4724
4725   return true;
4726 }
4727
4728 // Relocate section data.
4729
4730 template<int size, bool big_endian>
4731 void
4732 Target_tilegx<size, big_endian>::relocate_section(
4733     const Relocate_info<size, big_endian>* relinfo,
4734     unsigned int sh_type,
4735     const unsigned char* prelocs,
4736     size_t reloc_count,
4737     Output_section* output_section,
4738     bool needs_special_offset_handling,
4739     unsigned char* view,
4740     typename elfcpp::Elf_types<size>::Elf_Addr address,
4741     section_size_type view_size,
4742     const Reloc_symbol_changes* reloc_symbol_changes)
4743 {
4744   typedef Target_tilegx<size, big_endian> Tilegx;
4745   typedef typename Target_tilegx<size, big_endian>::Relocate Tilegx_relocate;
4746
4747   gold_assert(sh_type == elfcpp::SHT_RELA);
4748
4749   gold::relocate_section<size, big_endian, Tilegx, elfcpp::SHT_RELA,
4750                          Tilegx_relocate, gold::Default_comdat_behavior>(
4751     relinfo,
4752     this,
4753     prelocs,
4754     reloc_count,
4755     output_section,
4756     needs_special_offset_handling,
4757     view,
4758     address,
4759     view_size,
4760     reloc_symbol_changes);
4761 }
4762
4763 // Apply an incremental relocation.  Incremental relocations always refer
4764 // to global symbols.
4765
4766 template<int size, bool big_endian>
4767 void
4768 Target_tilegx<size, big_endian>::apply_relocation(
4769     const Relocate_info<size, big_endian>* relinfo,
4770     typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
4771     unsigned int r_type,
4772     typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
4773     const Symbol* gsym,
4774     unsigned char* view,
4775     typename elfcpp::Elf_types<size>::Elf_Addr address,
4776     section_size_type view_size)
4777 {
4778   gold::apply_relocation<size, big_endian, Target_tilegx<size, big_endian>,
4779                          typename Target_tilegx<size, big_endian>::Relocate>(
4780     relinfo,
4781     this,
4782     r_offset,
4783     r_type,
4784     r_addend,
4785     gsym,
4786     view,
4787     address,
4788     view_size);
4789 }
4790
4791 // Return the size of a relocation while scanning during a relocatable
4792 // link.
4793
4794 template<int size, bool big_endian>
4795 unsigned int
4796 Target_tilegx<size,big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4797   unsigned int, Relobj*)
4798 {
4799   // We are always SHT_RELA, so we should never get here.
4800   gold_unreachable();
4801   return 0;
4802 }
4803
4804 // Scan the relocs during a relocatable link.
4805
4806 template<int size, bool big_endian>
4807 void
4808 Target_tilegx<size, big_endian>::scan_relocatable_relocs(
4809     Symbol_table* symtab,
4810     Layout* layout,
4811     Sized_relobj_file<size, big_endian>* object,
4812     unsigned int data_shndx,
4813     unsigned int sh_type,
4814     const unsigned char* prelocs,
4815     size_t reloc_count,
4816     Output_section* output_section,
4817     bool needs_special_offset_handling,
4818     size_t local_symbol_count,
4819     const unsigned char* plocal_symbols,
4820     Relocatable_relocs* rr)
4821 {
4822   gold_assert(sh_type == elfcpp::SHT_RELA);
4823
4824   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
4825     Relocatable_size_for_reloc> Scan_relocatable_relocs;
4826
4827   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
4828       Scan_relocatable_relocs>(
4829     symtab,
4830     layout,
4831     object,
4832     data_shndx,
4833     prelocs,
4834     reloc_count,
4835     output_section,
4836     needs_special_offset_handling,
4837     local_symbol_count,
4838     plocal_symbols,
4839     rr);
4840 }
4841
4842 // Relocate a section during a relocatable link.
4843
4844 template<int size, bool big_endian>
4845 void
4846 Target_tilegx<size, big_endian>::relocate_relocs(
4847     const Relocate_info<size, big_endian>* relinfo,
4848     unsigned int sh_type,
4849     const unsigned char* prelocs,
4850     size_t reloc_count,
4851     Output_section* output_section,
4852     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4853     unsigned char* view,
4854     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4855     section_size_type view_size,
4856     unsigned char* reloc_view,
4857     section_size_type reloc_view_size)
4858 {
4859   gold_assert(sh_type == elfcpp::SHT_RELA);
4860
4861   gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
4862     relinfo,
4863     prelocs,
4864     reloc_count,
4865     output_section,
4866     offset_in_output_section,
4867     view,
4868     view_address,
4869     view_size,
4870     reloc_view,
4871     reloc_view_size);
4872 }
4873
4874 // Return the value to use for a dynamic which requires special
4875 // treatment.  This is how we support equality comparisons of function
4876 // pointers across shared library boundaries, as described in the
4877 // processor specific ABI supplement.
4878
4879 template<int size, bool big_endian>
4880 uint64_t
4881 Target_tilegx<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4882 {
4883   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4884   return this->plt_address_for_global(gsym);
4885 }
4886
4887 // Return the value to use for the base of a DW_EH_PE_datarel offset
4888 // in an FDE.  Solaris and SVR4 use DW_EH_PE_datarel because their
4889 // assembler can not write out the difference between two labels in
4890 // different sections, so instead of using a pc-relative value they
4891 // use an offset from the GOT.
4892
4893 template<int size, bool big_endian>
4894 uint64_t
4895 Target_tilegx<size, big_endian>::do_ehframe_datarel_base() const
4896 {
4897   gold_assert(this->global_offset_table_ != NULL);
4898   Symbol* sym = this->global_offset_table_;
4899   Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
4900   return ssym->value();
4901 }
4902
4903 // The selector for tilegx object files.
4904
4905 template<int size, bool big_endian>
4906 class Target_selector_tilegx : public Target_selector
4907 {
4908 public:
4909   Target_selector_tilegx()
4910     : Target_selector(elfcpp::EM_TILEGX, size, big_endian,
4911                       (size == 64
4912                        ? (big_endian ? "elf64-tilegx-be" : "elf64-tilegx-le")
4913                           : (big_endian ? "elf32-tilegx-be"
4914                                            : "elf32-tilegx-le")),
4915                       (size == 64
4916                        ? (big_endian ? "elf64tilegx_be" : "elf64tilegx")
4917                           : (big_endian ? "elf32tilegx_be" : "elf32tilegx")))
4918   { }
4919
4920   Target*
4921   do_instantiate_target()
4922   { return new Target_tilegx<size, big_endian>(); }
4923
4924 };
4925
4926 Target_selector_tilegx<64, false> target_selector_tilegx64_le;
4927 Target_selector_tilegx<32, false> target_selector_tilegx32_le;
4928 Target_selector_tilegx<64, true> target_selector_tilegx64_be;
4929 Target_selector_tilegx<32, true> target_selector_tilegx32_be;
4930 } // End anonymous namespace.