target.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. // target.h -- target support for gold -*- C++ -*-
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. // The abstract class Target is the interface for target specific
  18. // support. It defines abstract methods which each target must
  19. // implement. Typically there will be one target per processor, but
  20. // in some cases it may be necessary to have subclasses.
  21. // For speed and consistency we want to use inline functions to handle
  22. // relocation processing. So besides implementations of the abstract
  23. // methods, each target is expected to define a template
  24. // specialization of the relocation functions.
  25. #ifndef GOLD_TARGET_H
  26. #define GOLD_TARGET_H
  27. #include "elfcpp.h"
  28. #include "options.h"
  29. #include "parameters.h"
  30. #include "stringpool.h"
  31. #include "debug.h"
  32. namespace gold
  33. {
  34. class Object;
  35. class Relobj;
  36. template<int size, bool big_endian>
  37. class Sized_relobj;
  38. template<int size, bool big_endian>
  39. class Sized_relobj_file;
  40. class Relocatable_relocs;
  41. template<int size, bool big_endian>
  42. struct Relocate_info;
  43. class Reloc_symbol_changes;
  44. class Symbol;
  45. template<int size>
  46. class Sized_symbol;
  47. class Symbol_table;
  48. class Output_data;
  49. class Output_data_got_base;
  50. class Output_section;
  51. class Input_objects;
  52. class Task;
  53. struct Symbol_location;
  54. class Versions;
  55. // The abstract class for target specific handling.
  56. class Target
  57. {
  58. public:
  59. virtual ~Target()
  60. { }
  61. // Return the bit size that this target implements. This should
  62. // return 32 or 64.
  63. int
  64. get_size() const
  65. { return this->pti_->size; }
  66. // Return whether this target is big-endian.
  67. bool
  68. is_big_endian() const
  69. { return this->pti_->is_big_endian; }
  70. // Machine code to store in e_machine field of ELF header.
  71. elfcpp::EM
  72. machine_code() const
  73. { return this->pti_->machine_code; }
  74. // Processor specific flags to store in e_flags field of ELF header.
  75. elfcpp::Elf_Word
  76. processor_specific_flags() const
  77. { return this->processor_specific_flags_; }
  78. // Whether processor specific flags are set at least once.
  79. bool
  80. are_processor_specific_flags_set() const
  81. { return this->are_processor_specific_flags_set_; }
  82. // Whether this target has a specific make_symbol function.
  83. bool
  84. has_make_symbol() const
  85. { return this->pti_->has_make_symbol; }
  86. // Whether this target has a specific resolve function.
  87. bool
  88. has_resolve() const
  89. { return this->pti_->has_resolve; }
  90. // Whether this target has a specific code fill function.
  91. bool
  92. has_code_fill() const
  93. { return this->pti_->has_code_fill; }
  94. // Return the default name of the dynamic linker.
  95. const char*
  96. dynamic_linker() const
  97. { return this->pti_->dynamic_linker; }
  98. // Return the default address to use for the text segment.
  99. uint64_t
  100. default_text_segment_address() const
  101. { return this->pti_->default_text_segment_address; }
  102. // Return the ABI specified page size.
  103. uint64_t
  104. abi_pagesize() const
  105. {
  106. if (parameters->options().max_page_size() > 0)
  107. return parameters->options().max_page_size();
  108. else
  109. return this->pti_->abi_pagesize;
  110. }
  111. // Return the common page size used on actual systems.
  112. uint64_t
  113. common_pagesize() const
  114. {
  115. if (parameters->options().common_page_size() > 0)
  116. return std::min(parameters->options().common_page_size(),
  117. this->abi_pagesize());
  118. else
  119. return std::min(this->pti_->common_pagesize,
  120. this->abi_pagesize());
  121. }
  122. // Return whether PF_X segments must contain nothing but the contents of
  123. // SHF_EXECINSTR sections (no non-executable data, no headers).
  124. bool
  125. isolate_execinstr() const
  126. { return this->pti_->isolate_execinstr; }
  127. uint64_t
  128. rosegment_gap() const
  129. { return this->pti_->rosegment_gap; }
  130. // If we see some object files with .note.GNU-stack sections, and
  131. // some objects files without them, this returns whether we should
  132. // consider the object files without them to imply that the stack
  133. // should be executable.
  134. bool
  135. is_default_stack_executable() const
  136. { return this->pti_->is_default_stack_executable; }
  137. // Return a character which may appear as a prefix for a wrap
  138. // symbol. If this character appears, we strip it when checking for
  139. // wrapping and add it back when forming the final symbol name.
  140. // This should be '\0' if not special prefix is required, which is
  141. // the normal case.
  142. char
  143. wrap_char() const
  144. { return this->pti_->wrap_char; }
  145. // Return the special section index which indicates a small common
  146. // symbol. This will return SHN_UNDEF if there are no small common
  147. // symbols.
  148. elfcpp::Elf_Half
  149. small_common_shndx() const
  150. { return this->pti_->small_common_shndx; }
  151. // Return values to add to the section flags for the section holding
  152. // small common symbols.
  153. elfcpp::Elf_Xword
  154. small_common_section_flags() const
  155. {
  156. gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
  157. return this->pti_->small_common_section_flags;
  158. }
  159. // Return the special section index which indicates a large common
  160. // symbol. This will return SHN_UNDEF if there are no large common
  161. // symbols.
  162. elfcpp::Elf_Half
  163. large_common_shndx() const
  164. { return this->pti_->large_common_shndx; }
  165. // Return values to add to the section flags for the section holding
  166. // large common symbols.
  167. elfcpp::Elf_Xword
  168. large_common_section_flags() const
  169. {
  170. gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
  171. return this->pti_->large_common_section_flags;
  172. }
  173. // This hook is called when an output section is created.
  174. void
  175. new_output_section(Output_section* os) const
  176. { this->do_new_output_section(os); }
  177. // This is called to tell the target to complete any sections it is
  178. // handling. After this all sections must have their final size.
  179. void
  180. finalize_sections(Layout* layout, const Input_objects* input_objects,
  181. Symbol_table* symtab)
  182. { return this->do_finalize_sections(layout, input_objects, symtab); }
  183. // Return the value to use for a global symbol which needs a special
  184. // value in the dynamic symbol table. This will only be called if
  185. // the backend first calls symbol->set_needs_dynsym_value().
  186. uint64_t
  187. dynsym_value(const Symbol* sym) const
  188. { return this->do_dynsym_value(sym); }
  189. // Return a string to use to fill out a code section. This is
  190. // basically one or more NOPS which must fill out the specified
  191. // length in bytes.
  192. std::string
  193. code_fill(section_size_type length) const
  194. { return this->do_code_fill(length); }
  195. // Return whether SYM is known to be defined by the ABI. This is
  196. // used to avoid inappropriate warnings about undefined symbols.
  197. bool
  198. is_defined_by_abi(const Symbol* sym) const
  199. { return this->do_is_defined_by_abi(sym); }
  200. // Adjust the output file header before it is written out. VIEW
  201. // points to the header in external form. LEN is the length.
  202. void
  203. adjust_elf_header(unsigned char* view, int len)
  204. { return this->do_adjust_elf_header(view, len); }
  205. // Return address and size to plug into eh_frame FDEs associated with a PLT.
  206. void
  207. plt_fde_location(const Output_data* plt, unsigned char* oview,
  208. uint64_t* address, off_t* len) const
  209. { return this->do_plt_fde_location(plt, oview, address, len); }
  210. // Return whether NAME is a local label name. This is used to implement the
  211. // --discard-locals options.
  212. bool
  213. is_local_label_name(const char* name) const
  214. { return this->do_is_local_label_name(name); }
  215. // Get the symbol index to use for a target specific reloc.
  216. unsigned int
  217. reloc_symbol_index(void* arg, unsigned int type) const
  218. { return this->do_reloc_symbol_index(arg, type); }
  219. // Get the addend to use for a target specific reloc.
  220. uint64_t
  221. reloc_addend(void* arg, unsigned int type, uint64_t addend) const
  222. { return this->do_reloc_addend(arg, type, addend); }
  223. // Return the PLT address to use for a global symbol.
  224. uint64_t
  225. plt_address_for_global(const Symbol* sym) const
  226. { return this->do_plt_address_for_global(sym); }
  227. // Return the PLT address to use for a local symbol.
  228. uint64_t
  229. plt_address_for_local(const Relobj* object, unsigned int symndx) const
  230. { return this->do_plt_address_for_local(object, symndx); }
  231. // Return the offset to use for the GOT_INDX'th got entry which is
  232. // for a local tls symbol specified by OBJECT, SYMNDX.
  233. int64_t
  234. tls_offset_for_local(const Relobj* object,
  235. unsigned int symndx,
  236. unsigned int got_indx) const
  237. { return do_tls_offset_for_local(object, symndx, got_indx); }
  238. // Return the offset to use for the GOT_INDX'th got entry which is
  239. // for global tls symbol GSYM.
  240. int64_t
  241. tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const
  242. { return do_tls_offset_for_global(gsym, got_indx); }
  243. // For targets that use function descriptors, if LOC is the location
  244. // of a function, modify it to point at the function entry location.
  245. void
  246. function_location(Symbol_location* loc) const
  247. { return do_function_location(loc); }
  248. // Return whether this target can use relocation types to determine
  249. // if a function's address is taken.
  250. bool
  251. can_check_for_function_pointers() const
  252. { return this->do_can_check_for_function_pointers(); }
  253. // Return whether a relocation to a merged section can be processed
  254. // to retrieve the contents.
  255. bool
  256. can_icf_inline_merge_sections () const
  257. { return this->pti_->can_icf_inline_merge_sections; }
  258. // Whether a section called SECTION_NAME may have function pointers to
  259. // sections not eligible for safe ICF folding.
  260. virtual bool
  261. section_may_have_icf_unsafe_pointers(const char* section_name) const
  262. { return this->do_section_may_have_icf_unsafe_pointers(section_name); }
  263. // Return the base to use for the PC value in an FDE when it is
  264. // encoded using DW_EH_PE_datarel. This does not appear to be
  265. // documented anywhere, but it is target specific. Any use of
  266. // DW_EH_PE_datarel in gcc requires defining a special macro
  267. // (ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX) to output the value.
  268. uint64_t
  269. ehframe_datarel_base() const
  270. { return this->do_ehframe_datarel_base(); }
  271. // Return true if a reference to SYM from a reloc of type R_TYPE
  272. // means that the current function may call an object compiled
  273. // without -fsplit-stack. SYM is known to be defined in an object
  274. // compiled without -fsplit-stack.
  275. bool
  276. is_call_to_non_split(const Symbol* sym, unsigned int r_type) const
  277. { return this->do_is_call_to_non_split(sym, r_type); }
  278. // A function starts at OFFSET in section SHNDX in OBJECT. That
  279. // function was compiled with -fsplit-stack, but it refers to a
  280. // function which was compiled without -fsplit-stack. VIEW is a
  281. // modifiable view of the section; VIEW_SIZE is the size of the
  282. // view. The target has to adjust the function so that it allocates
  283. // enough stack.
  284. void
  285. calls_non_split(Relobj* object, unsigned int shndx,
  286. section_offset_type fnoffset, section_size_type fnsize,
  287. unsigned char* view, section_size_type view_size,
  288. std::string* from, std::string* to) const
  289. {
  290. this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
  291. from, to);
  292. }
  293. // Make an ELF object.
  294. template<int size, bool big_endian>
  295. Object*
  296. make_elf_object(const std::string& name, Input_file* input_file,
  297. off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
  298. { return this->do_make_elf_object(name, input_file, offset, ehdr); }
  299. // Make an output section.
  300. Output_section*
  301. make_output_section(const char* name, elfcpp::Elf_Word type,
  302. elfcpp::Elf_Xword flags)
  303. { return this->do_make_output_section(name, type, flags); }
  304. // Return true if target wants to perform relaxation.
  305. bool
  306. may_relax() const
  307. {
  308. // Run the dummy relaxation pass twice if relaxation debugging is enabled.
  309. if (is_debugging_enabled(DEBUG_RELAXATION))
  310. return true;
  311. return this->do_may_relax();
  312. }
  313. // Perform a relaxation pass. Return true if layout may be changed.
  314. bool
  315. relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
  316. Layout* layout, const Task* task)
  317. {
  318. // Run the dummy relaxation pass twice if relaxation debugging is enabled.
  319. if (is_debugging_enabled(DEBUG_RELAXATION))
  320. return pass < 2;
  321. return this->do_relax(pass, input_objects, symtab, layout, task);
  322. }
  323. // Return the target-specific name of attributes section. This is
  324. // NULL if a target does not use attributes section or if it uses
  325. // the default section name ".gnu.attributes".
  326. const char*
  327. attributes_section() const
  328. { return this->pti_->attributes_section; }
  329. // Return the vendor name of vendor attributes.
  330. const char*
  331. attributes_vendor() const
  332. { return this->pti_->attributes_vendor; }
  333. // Whether a section called NAME is an attribute section.
  334. bool
  335. is_attributes_section(const char* name) const
  336. {
  337. return ((this->pti_->attributes_section != NULL
  338. && strcmp(name, this->pti_->attributes_section) == 0)
  339. || strcmp(name, ".gnu.attributes") == 0);
  340. }
  341. // Return a bit mask of argument types for attribute with TAG.
  342. int
  343. attribute_arg_type(int tag) const
  344. { return this->do_attribute_arg_type(tag); }
  345. // Return the attribute tag of the position NUM in the list of fixed
  346. // attributes. Normally there is no reordering and
  347. // attributes_order(NUM) == NUM.
  348. int
  349. attributes_order(int num) const
  350. { return this->do_attributes_order(num); }
  351. // When a target is selected as the default target, we call this method,
  352. // which may be used for expensive, target-specific initialization.
  353. void
  354. select_as_default_target()
  355. { this->do_select_as_default_target(); }
  356. // Return the value to store in the EI_OSABI field in the ELF
  357. // header.
  358. elfcpp::ELFOSABI
  359. osabi() const
  360. { return this->osabi_; }
  361. // Set the value to store in the EI_OSABI field in the ELF header.
  362. void
  363. set_osabi(elfcpp::ELFOSABI osabi)
  364. { this->osabi_ = osabi; }
  365. // Define target-specific standard symbols.
  366. void
  367. define_standard_symbols(Symbol_table* symtab, Layout* layout)
  368. { this->do_define_standard_symbols(symtab, layout); }
  369. // Return the output section name to use given an input section
  370. // name, or NULL if no target specific name mapping is required.
  371. // Set *PLEN to the length of the name if returning non-NULL.
  372. const char*
  373. output_section_name(const Relobj* relobj,
  374. const char* name,
  375. size_t* plen) const
  376. { return this->do_output_section_name(relobj, name, plen); }
  377. // Add any special sections for this symbol to the gc work list.
  378. void
  379. gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const
  380. { this->do_gc_mark_symbol(symtab, sym); }
  381. // Return the name of the entry point symbol.
  382. const char*
  383. entry_symbol_name() const
  384. { return this->pti_->entry_symbol_name; }
  385. // Whether the target has a custom set_dynsym_indexes method.
  386. bool
  387. has_custom_set_dynsym_indexes() const
  388. { return this->do_has_custom_set_dynsym_indexes(); }
  389. // Custom set_dynsym_indexes method for a target.
  390. unsigned int
  391. set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
  392. std::vector<Symbol*>* syms, Stringpool* dynpool,
  393. Versions* versions, Symbol_table* symtab) const
  394. {
  395. return this->do_set_dynsym_indexes(dyn_symbols, index, syms, dynpool,
  396. versions, symtab);
  397. }
  398. // Get the custom dynamic tag value.
  399. unsigned int
  400. dynamic_tag_custom_value(elfcpp::DT tag) const
  401. { return this->do_dynamic_tag_custom_value(tag); }
  402. // Adjust the value written to the dynamic symbol table.
  403. void
  404. adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
  405. { this->do_adjust_dyn_symbol(sym, view); }
  406. // Return whether to include the section in the link.
  407. bool
  408. should_include_section(elfcpp::Elf_Word sh_type) const
  409. { return this->do_should_include_section(sh_type); }
  410. protected:
  411. // This struct holds the constant information for a child class. We
  412. // use a struct to avoid the overhead of virtual function calls for
  413. // simple information.
  414. struct Target_info
  415. {
  416. // Address size (32 or 64).
  417. int size;
  418. // Whether the target is big endian.
  419. bool is_big_endian;
  420. // The code to store in the e_machine field of the ELF header.
  421. elfcpp::EM machine_code;
  422. // Whether this target has a specific make_symbol function.
  423. bool has_make_symbol;
  424. // Whether this target has a specific resolve function.
  425. bool has_resolve;
  426. // Whether this target has a specific code fill function.
  427. bool has_code_fill;
  428. // Whether an object file with no .note.GNU-stack sections implies
  429. // that the stack should be executable.
  430. bool is_default_stack_executable;
  431. // Whether a relocation to a merged section can be processed to
  432. // retrieve the contents.
  433. bool can_icf_inline_merge_sections;
  434. // Prefix character to strip when checking for wrapping.
  435. char wrap_char;
  436. // The default dynamic linker name.
  437. const char* dynamic_linker;
  438. // The default text segment address.
  439. uint64_t default_text_segment_address;
  440. // The ABI specified page size.
  441. uint64_t abi_pagesize;
  442. // The common page size used by actual implementations.
  443. uint64_t common_pagesize;
  444. // Whether PF_X segments must contain nothing but the contents of
  445. // SHF_EXECINSTR sections (no non-executable data, no headers).
  446. bool isolate_execinstr;
  447. // If nonzero, distance from the text segment to the read-only segment.
  448. uint64_t rosegment_gap;
  449. // The special section index for small common symbols; SHN_UNDEF
  450. // if none.
  451. elfcpp::Elf_Half small_common_shndx;
  452. // The special section index for large common symbols; SHN_UNDEF
  453. // if none.
  454. elfcpp::Elf_Half large_common_shndx;
  455. // Section flags for small common section.
  456. elfcpp::Elf_Xword small_common_section_flags;
  457. // Section flags for large common section.
  458. elfcpp::Elf_Xword large_common_section_flags;
  459. // Name of attributes section if it is not ".gnu.attributes".
  460. const char* attributes_section;
  461. // Vendor name of vendor attributes.
  462. const char* attributes_vendor;
  463. // Name of the main entry point to the program.
  464. const char* entry_symbol_name;
  465. };
  466. Target(const Target_info* pti)
  467. : pti_(pti), processor_specific_flags_(0),
  468. are_processor_specific_flags_set_(false), osabi_(elfcpp::ELFOSABI_NONE)
  469. { }
  470. // Virtual function which may be implemented by the child class.
  471. virtual void
  472. do_new_output_section(Output_section*) const
  473. { }
  474. // Virtual function which may be implemented by the child class.
  475. virtual void
  476. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
  477. { }
  478. // Virtual function which may be implemented by the child class.
  479. virtual uint64_t
  480. do_dynsym_value(const Symbol*) const
  481. { gold_unreachable(); }
  482. // Virtual function which must be implemented by the child class if
  483. // needed.
  484. virtual std::string
  485. do_code_fill(section_size_type) const
  486. { gold_unreachable(); }
  487. // Virtual function which may be implemented by the child class.
  488. virtual bool
  489. do_is_defined_by_abi(const Symbol*) const
  490. { return false; }
  491. // Adjust the output file header before it is written out. VIEW
  492. // points to the header in external form. LEN is the length, and
  493. // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
  494. // By default, we set the EI_OSABI field if requested (in
  495. // Sized_target).
  496. virtual void
  497. do_adjust_elf_header(unsigned char*, int) = 0;
  498. // Return address and size to plug into eh_frame FDEs associated with a PLT.
  499. virtual void
  500. do_plt_fde_location(const Output_data* plt, unsigned char* oview,
  501. uint64_t* address, off_t* len) const;
  502. // Virtual function which may be overridden by the child class.
  503. virtual bool
  504. do_is_local_label_name(const char*) const;
  505. // Virtual function that must be overridden by a target which uses
  506. // target specific relocations.
  507. virtual unsigned int
  508. do_reloc_symbol_index(void*, unsigned int) const
  509. { gold_unreachable(); }
  510. // Virtual function that must be overridden by a target which uses
  511. // target specific relocations.
  512. virtual uint64_t
  513. do_reloc_addend(void*, unsigned int, uint64_t) const
  514. { gold_unreachable(); }
  515. // Virtual functions that must be overridden by a target that uses
  516. // STT_GNU_IFUNC symbols.
  517. virtual uint64_t
  518. do_plt_address_for_global(const Symbol*) const
  519. { gold_unreachable(); }
  520. virtual uint64_t
  521. do_plt_address_for_local(const Relobj*, unsigned int) const
  522. { gold_unreachable(); }
  523. virtual int64_t
  524. do_tls_offset_for_local(const Relobj*, unsigned int, unsigned int) const
  525. { gold_unreachable(); }
  526. virtual int64_t
  527. do_tls_offset_for_global(Symbol*, unsigned int) const
  528. { gold_unreachable(); }
  529. virtual void
  530. do_function_location(Symbol_location*) const = 0;
  531. // Virtual function which may be overriden by the child class.
  532. virtual bool
  533. do_can_check_for_function_pointers() const
  534. { return false; }
  535. // Virtual function which may be overridden by the child class. We
  536. // recognize some default sections for which we don't care whether
  537. // they have function pointers.
  538. virtual bool
  539. do_section_may_have_icf_unsafe_pointers(const char* section_name) const
  540. {
  541. // We recognize sections for normal vtables, construction vtables and
  542. // EH frames.
  543. return (!is_prefix_of(".rodata._ZTV", section_name)
  544. && !is_prefix_of(".data.rel.ro._ZTV", section_name)
  545. && !is_prefix_of(".rodata._ZTC", section_name)
  546. && !is_prefix_of(".data.rel.ro._ZTC", section_name)
  547. && !is_prefix_of(".eh_frame", section_name));
  548. }
  549. virtual uint64_t
  550. do_ehframe_datarel_base() const
  551. { gold_unreachable(); }
  552. // Virtual function which may be overridden by the child class. The
  553. // default implementation is that any function not defined by the
  554. // ABI is a call to a non-split function.
  555. virtual bool
  556. do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
  557. // Virtual function which may be overridden by the child class.
  558. virtual void
  559. do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
  560. section_size_type, unsigned char*, section_size_type,
  561. std::string*, std::string*) const;
  562. // make_elf_object hooks. There are four versions of these for
  563. // different address sizes and endianness.
  564. // Set processor specific flags.
  565. void
  566. set_processor_specific_flags(elfcpp::Elf_Word flags)
  567. {
  568. this->processor_specific_flags_ = flags;
  569. this->are_processor_specific_flags_set_ = true;
  570. }
  571. #ifdef HAVE_TARGET_32_LITTLE
  572. // Virtual functions which may be overridden by the child class.
  573. virtual Object*
  574. do_make_elf_object(const std::string&, Input_file*, off_t,
  575. const elfcpp::Ehdr<32, false>&);
  576. #endif
  577. #ifdef HAVE_TARGET_32_BIG
  578. // Virtual functions which may be overridden by the child class.
  579. virtual Object*
  580. do_make_elf_object(const std::string&, Input_file*, off_t,
  581. const elfcpp::Ehdr<32, true>&);
  582. #endif
  583. #ifdef HAVE_TARGET_64_LITTLE
  584. // Virtual functions which may be overridden by the child class.
  585. virtual Object*
  586. do_make_elf_object(const std::string&, Input_file*, off_t,
  587. const elfcpp::Ehdr<64, false>& ehdr);
  588. #endif
  589. #ifdef HAVE_TARGET_64_BIG
  590. // Virtual functions which may be overridden by the child class.
  591. virtual Object*
  592. do_make_elf_object(const std::string& name, Input_file* input_file,
  593. off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
  594. #endif
  595. // Virtual functions which may be overridden by the child class.
  596. virtual Output_section*
  597. do_make_output_section(const char* name, elfcpp::Elf_Word type,
  598. elfcpp::Elf_Xword flags);
  599. // Virtual function which may be overridden by the child class.
  600. virtual bool
  601. do_may_relax() const
  602. { return parameters->options().relax(); }
  603. // Virtual function which may be overridden by the child class.
  604. virtual bool
  605. do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*)
  606. { return false; }
  607. // A function for targets to call. Return whether BYTES/LEN matches
  608. // VIEW/VIEW_SIZE at OFFSET.
  609. bool
  610. match_view(const unsigned char* view, section_size_type view_size,
  611. section_offset_type offset, const char* bytes, size_t len) const;
  612. // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
  613. // for LEN bytes.
  614. void
  615. set_view_to_nop(unsigned char* view, section_size_type view_size,
  616. section_offset_type offset, size_t len) const;
  617. // This must be overridden by the child class if it has target-specific
  618. // attributes subsection in the attribute section.
  619. virtual int
  620. do_attribute_arg_type(int) const
  621. { gold_unreachable(); }
  622. // This may be overridden by the child class.
  623. virtual int
  624. do_attributes_order(int num) const
  625. { return num; }
  626. // This may be overridden by the child class.
  627. virtual void
  628. do_select_as_default_target()
  629. { }
  630. // This may be overridden by the child class.
  631. virtual void
  632. do_define_standard_symbols(Symbol_table*, Layout*)
  633. { }
  634. // This may be overridden by the child class.
  635. virtual const char*
  636. do_output_section_name(const Relobj*, const char*, size_t*) const
  637. { return NULL; }
  638. // This may be overridden by the child class.
  639. virtual void
  640. do_gc_mark_symbol(Symbol_table*, Symbol*) const
  641. { }
  642. // This may be overridden by the child class.
  643. virtual bool
  644. do_has_custom_set_dynsym_indexes() const
  645. { return false; }
  646. // This may be overridden by the child class.
  647. virtual unsigned int
  648. do_set_dynsym_indexes(std::vector<Symbol*>*, unsigned int,
  649. std::vector<Symbol*>*, Stringpool*, Versions*,
  650. Symbol_table*) const
  651. { gold_unreachable(); }
  652. // This may be overridden by the child class.
  653. virtual unsigned int
  654. do_dynamic_tag_custom_value(elfcpp::DT) const
  655. { gold_unreachable(); }
  656. // This may be overridden by the child class.
  657. virtual void
  658. do_adjust_dyn_symbol(const Symbol*, unsigned char*) const
  659. { }
  660. // This may be overridden by the child class.
  661. virtual bool
  662. do_should_include_section(elfcpp::Elf_Word) const
  663. { return true; }
  664. private:
  665. // The implementations of the four do_make_elf_object virtual functions are
  666. // almost identical except for their sizes and endianness. We use a template.
  667. // for their implementations.
  668. template<int size, bool big_endian>
  669. inline Object*
  670. do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
  671. const elfcpp::Ehdr<size, big_endian>&);
  672. Target(const Target&);
  673. Target& operator=(const Target&);
  674. // The target information.
  675. const Target_info* pti_;
  676. // Processor-specific flags.
  677. elfcpp::Elf_Word processor_specific_flags_;
  678. // Whether the processor-specific flags are set at least once.
  679. bool are_processor_specific_flags_set_;
  680. // If not ELFOSABI_NONE, the value to put in the EI_OSABI field of
  681. // the ELF header. This is handled at this level because it is
  682. // OS-specific rather than processor-specific.
  683. elfcpp::ELFOSABI osabi_;
  684. };
  685. // The abstract class for a specific size and endianness of target.
  686. // Each actual target implementation class should derive from an
  687. // instantiation of Sized_target.
  688. template<int size, bool big_endian>
  689. class Sized_target : public Target
  690. {
  691. public:
  692. // Make a new symbol table entry for the target. This should be
  693. // overridden by a target which needs additional information in the
  694. // symbol table. This will only be called if has_make_symbol()
  695. // returns true.
  696. virtual Sized_symbol<size>*
  697. make_symbol() const
  698. { gold_unreachable(); }
  699. // Resolve a symbol for the target. This should be overridden by a
  700. // target which needs to take special action. TO is the
  701. // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
  702. // VERSION is the version of SYM. This will only be called if
  703. // has_resolve() returns true.
  704. virtual void
  705. resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
  706. const char*)
  707. { gold_unreachable(); }
  708. // Process the relocs for a section, and record information of the
  709. // mapping from source to destination sections. This mapping is later
  710. // used to determine unreferenced garbage sections. This procedure is
  711. // only called during garbage collection.
  712. virtual void
  713. gc_process_relocs(Symbol_table* symtab,
  714. Layout* layout,
  715. Sized_relobj_file<size, big_endian>* object,
  716. unsigned int data_shndx,
  717. unsigned int sh_type,
  718. const unsigned char* prelocs,
  719. size_t reloc_count,
  720. Output_section* output_section,
  721. bool needs_special_offset_handling,
  722. size_t local_symbol_count,
  723. const unsigned char* plocal_symbols) = 0;
  724. // Scan the relocs for a section, and record any information
  725. // required for the symbol. SYMTAB is the symbol table. OBJECT is
  726. // the object in which the section appears. DATA_SHNDX is the
  727. // section index that these relocs apply to. SH_TYPE is the type of
  728. // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
  729. // the relocation data. RELOC_COUNT is the number of relocs.
  730. // LOCAL_SYMBOL_COUNT is the number of local symbols.
  731. // OUTPUT_SECTION is the output section.
  732. // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
  733. // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
  734. // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
  735. // pointers to the global symbol table from OBJECT.
  736. virtual void
  737. scan_relocs(Symbol_table* symtab,
  738. Layout* layout,
  739. Sized_relobj_file<size, big_endian>* object,
  740. unsigned int data_shndx,
  741. unsigned int sh_type,
  742. const unsigned char* prelocs,
  743. size_t reloc_count,
  744. Output_section* output_section,
  745. bool needs_special_offset_handling,
  746. size_t local_symbol_count,
  747. const unsigned char* plocal_symbols) = 0;
  748. // Relocate section data. SH_TYPE is the type of the relocation
  749. // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
  750. // information. RELOC_COUNT is the number of relocs.
  751. // OUTPUT_SECTION is the output section.
  752. // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
  753. // to correspond to the output section. VIEW is a view into the
  754. // output file holding the section contents, VIEW_ADDRESS is the
  755. // virtual address of the view, and VIEW_SIZE is the size of the
  756. // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
  757. // parameters refer to the complete output section data, not just
  758. // the input section data.
  759. virtual void
  760. relocate_section(const Relocate_info<size, big_endian>*,
  761. unsigned int sh_type,
  762. const unsigned char* prelocs,
  763. size_t reloc_count,
  764. Output_section* output_section,
  765. bool needs_special_offset_handling,
  766. unsigned char* view,
  767. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  768. section_size_type view_size,
  769. const Reloc_symbol_changes*) = 0;
  770. // Scan the relocs during a relocatable link. The parameters are
  771. // like scan_relocs, with an additional Relocatable_relocs
  772. // parameter, used to record the disposition of the relocs.
  773. virtual void
  774. scan_relocatable_relocs(Symbol_table* symtab,
  775. Layout* layout,
  776. Sized_relobj_file<size, big_endian>* object,
  777. unsigned int data_shndx,
  778. unsigned int sh_type,
  779. const unsigned char* prelocs,
  780. size_t reloc_count,
  781. Output_section* output_section,
  782. bool needs_special_offset_handling,
  783. size_t local_symbol_count,
  784. const unsigned char* plocal_symbols,
  785. Relocatable_relocs*) = 0;
  786. // Emit relocations for a section during a relocatable link, and for
  787. // --emit-relocs. The parameters are like relocate_section, with
  788. // additional parameters for the view of the output reloc section.
  789. virtual void
  790. relocate_relocs(const Relocate_info<size, big_endian>*,
  791. unsigned int sh_type,
  792. const unsigned char* prelocs,
  793. size_t reloc_count,
  794. Output_section* output_section,
  795. typename elfcpp::Elf_types<size>::Elf_Off
  796. offset_in_output_section,
  797. const Relocatable_relocs*,
  798. unsigned char* view,
  799. typename elfcpp::Elf_types<size>::Elf_Addr view_address,
  800. section_size_type view_size,
  801. unsigned char* reloc_view,
  802. section_size_type reloc_view_size) = 0;
  803. // Perform target-specific processing in a relocatable link. This is
  804. // only used if we use the relocation strategy RELOC_SPECIAL.
  805. // RELINFO points to a Relocation_info structure. SH_TYPE is the relocation
  806. // section type. PRELOC_IN points to the original relocation. RELNUM is
  807. // the index number of the relocation in the relocation section.
  808. // OUTPUT_SECTION is the output section to which the relocation is applied.
  809. // OFFSET_IN_OUTPUT_SECTION is the offset of the relocation input section
  810. // within the output section. VIEW points to the output view of the
  811. // output section. VIEW_ADDRESS is output address of the view. VIEW_SIZE
  812. // is the size of the output view and PRELOC_OUT points to the new
  813. // relocation in the output object.
  814. //
  815. // A target only needs to override this if the generic code in
  816. // target-reloc.h cannot handle some relocation types.
  817. virtual void
  818. relocate_special_relocatable(const Relocate_info<size, big_endian>*
  819. /*relinfo */,
  820. unsigned int /* sh_type */,
  821. const unsigned char* /* preloc_in */,
  822. size_t /* relnum */,
  823. Output_section* /* output_section */,
  824. typename elfcpp::Elf_types<size>::Elf_Off
  825. /* offset_in_output_section */,
  826. unsigned char* /* view */,
  827. typename elfcpp::Elf_types<size>::Elf_Addr
  828. /* view_address */,
  829. section_size_type /* view_size */,
  830. unsigned char* /* preloc_out*/)
  831. { gold_unreachable(); }
  832. // Return the number of entries in the GOT. This is only used for
  833. // laying out the incremental link info sections. A target needs
  834. // to implement this to support incremental linking.
  835. virtual unsigned int
  836. got_entry_count() const
  837. { gold_unreachable(); }
  838. // Return the number of entries in the PLT. This is only used for
  839. // laying out the incremental link info sections. A target needs
  840. // to implement this to support incremental linking.
  841. virtual unsigned int
  842. plt_entry_count() const
  843. { gold_unreachable(); }
  844. // Return the offset of the first non-reserved PLT entry. This is
  845. // only used for laying out the incremental link info sections.
  846. // A target needs to implement this to support incremental linking.
  847. virtual unsigned int
  848. first_plt_entry_offset() const
  849. { gold_unreachable(); }
  850. // Return the size of each PLT entry. This is only used for
  851. // laying out the incremental link info sections. A target needs
  852. // to implement this to support incremental linking.
  853. virtual unsigned int
  854. plt_entry_size() const
  855. { gold_unreachable(); }
  856. // Create the GOT and PLT sections for an incremental update.
  857. // A target needs to implement this to support incremental linking.
  858. virtual Output_data_got_base*
  859. init_got_plt_for_update(Symbol_table*,
  860. Layout*,
  861. unsigned int /* got_count */,
  862. unsigned int /* plt_count */)
  863. { gold_unreachable(); }
  864. // Reserve a GOT entry for a local symbol, and regenerate any
  865. // necessary dynamic relocations.
  866. virtual void
  867. reserve_local_got_entry(unsigned int /* got_index */,
  868. Sized_relobj<size, big_endian>* /* obj */,
  869. unsigned int /* r_sym */,
  870. unsigned int /* got_type */)
  871. { gold_unreachable(); }
  872. // Reserve a GOT entry for a global symbol, and regenerate any
  873. // necessary dynamic relocations.
  874. virtual void
  875. reserve_global_got_entry(unsigned int /* got_index */, Symbol* /* gsym */,
  876. unsigned int /* got_type */)
  877. { gold_unreachable(); }
  878. // Register an existing PLT entry for a global symbol.
  879. // A target needs to implement this to support incremental linking.
  880. virtual void
  881. register_global_plt_entry(Symbol_table*, Layout*,
  882. unsigned int /* plt_index */,
  883. Symbol*)
  884. { gold_unreachable(); }
  885. // Force a COPY relocation for a given symbol.
  886. // A target needs to implement this to support incremental linking.
  887. virtual void
  888. emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t)
  889. { gold_unreachable(); }
  890. // Apply an incremental relocation.
  891. virtual void
  892. apply_relocation(const Relocate_info<size, big_endian>* /* relinfo */,
  893. typename elfcpp::Elf_types<size>::Elf_Addr /* r_offset */,
  894. unsigned int /* r_type */,
  895. typename elfcpp::Elf_types<size>::Elf_Swxword /* r_addend */,
  896. const Symbol* /* gsym */,
  897. unsigned char* /* view */,
  898. typename elfcpp::Elf_types<size>::Elf_Addr /* address */,
  899. section_size_type /* view_size */)
  900. { gold_unreachable(); }
  901. // Handle target specific gc actions when adding a gc reference from
  902. // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
  903. // and DST_OFF.
  904. void
  905. gc_add_reference(Symbol_table* symtab,
  906. Relobj* src_obj,
  907. unsigned int src_shndx,
  908. Relobj* dst_obj,
  909. unsigned int dst_shndx,
  910. typename elfcpp::Elf_types<size>::Elf_Addr dst_off) const
  911. {
  912. this->do_gc_add_reference(symtab, src_obj, src_shndx,
  913. dst_obj, dst_shndx, dst_off);
  914. }
  915. protected:
  916. Sized_target(const Target::Target_info* pti)
  917. : Target(pti)
  918. {
  919. gold_assert(pti->size == size);
  920. gold_assert(pti->is_big_endian ? big_endian : !big_endian);
  921. }
  922. // Set the EI_OSABI field if requested.
  923. virtual void
  924. do_adjust_elf_header(unsigned char*, int);
  925. // Handle target specific gc actions when adding a gc reference.
  926. virtual void
  927. do_gc_add_reference(Symbol_table*, Relobj*, unsigned int,
  928. Relobj*, unsigned int,
  929. typename elfcpp::Elf_types<size>::Elf_Addr) const
  930. { }
  931. virtual void
  932. do_function_location(Symbol_location*) const
  933. { }
  934. };
  935. } // End namespace gold.
  936. #endif // !defined(GOLD_TARGET_H)