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