target.h 41 KB

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