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