elfxx-x86.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /* x86 specific support for ELF
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /* Don't generate unused section symbols. */
  17. #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS false
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "bfdlink.h"
  21. #include "libbfd.h"
  22. #include "elf-bfd.h"
  23. #include "hashtab.h"
  24. #include "elf-linker-x86.h"
  25. #include "elf/i386.h"
  26. #include "elf/x86-64.h"
  27. #define X86_64_PCREL_TYPE_P(TYPE) \
  28. ((TYPE) == R_X86_64_PC8 \
  29. || (TYPE) == R_X86_64_PC16 \
  30. || (TYPE) == R_X86_64_PC32 \
  31. || (TYPE) == R_X86_64_PC32_BND \
  32. || (TYPE) == R_X86_64_PC64)
  33. #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
  34. #define X86_PCREL_TYPE_P(IS_X86_64, TYPE) \
  35. ((IS_X86_64) ? X86_64_PCREL_TYPE_P (TYPE) : I386_PCREL_TYPE_P (TYPE))
  36. #define X86_64_SIZE_TYPE_P(TYPE) \
  37. ((TYPE) == R_X86_64_SIZE32 || (TYPE) == R_X86_64_SIZE64)
  38. #define I386_SIZE_TYPE_P(TYPE) ((TYPE) == R_386_SIZE32)
  39. #define X86_SIZE_TYPE_P(IS_X86_64, TYPE) \
  40. ((IS_X86_64) ? X86_64_SIZE_TYPE_P(TYPE) : I386_SIZE_TYPE_P (TYPE))
  41. #define X86_64_GOT_TYPE_P(TYPE) \
  42. ((TYPE) == R_X86_64_GOTPCREL \
  43. || (TYPE) == R_X86_64_GOTPCRELX \
  44. || (TYPE) == R_X86_64_REX_GOTPCRELX \
  45. || (TYPE) == R_X86_64_GOT32 \
  46. || (TYPE) == R_X86_64_GOT64 \
  47. || (TYPE) == R_X86_64_GOTPCREL64 \
  48. || (TYPE) == R_X86_64_GOTPLT64)
  49. #define I386_GOT_TYPE_P(TYPE) \
  50. ((TYPE) == R_386_GOT32 || (TYPE) == R_386_GOT32X)
  51. #define X86_GOT_TYPE_P(IS_X86_64, TYPE) \
  52. ((IS_X86_64) ? X86_64_GOT_TYPE_P (TYPE) : I386_GOT_TYPE_P (TYPE))
  53. #define X86_64_RELATIVE_RELOC_TYPE_P(TYPE) \
  54. (X86_64_PCREL_TYPE_P (TYPE) \
  55. || (TYPE) == R_X86_64_8 \
  56. || (TYPE) == R_X86_64_16 \
  57. || (TYPE) == R_X86_64_32 \
  58. || (TYPE) == R_X86_64_32S \
  59. || (TYPE) == R_X86_64_64)
  60. #define I386_RELATIVE_RELOC_TYPE_P(TYPE) \
  61. ((TYPE) == R_386_32 || (TYPE) == R_386_PC32)
  62. #define X86_RELATIVE_RELOC_TYPE_P(IS_X86_64, TYPE) \
  63. ((IS_X86_64) \
  64. ? X86_64_RELATIVE_RELOC_TYPE_P (TYPE) \
  65. : I386_RELATIVE_RELOC_TYPE_P(TYPE))
  66. #define X86_64_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
  67. (X86_64_SIZE_TYPE_P (TYPE) \
  68. || X86_64_RELATIVE_RELOC_TYPE_P (TYPE))
  69. #define I386_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
  70. (I386_SIZE_TYPE_P (TYPE) \
  71. || I386_RELATIVE_RELOC_TYPE_P (TYPE) \
  72. || (TYPE) == R_386_TLS_IE \
  73. || (TYPE) == R_386_TLS_LE \
  74. || (TYPE) == R_386_TLS_LE_32)
  75. #define X86_NEED_DYNAMIC_RELOC_TYPE_P(IS_X86_64, TYPE) \
  76. ((IS_X86_64) \
  77. ? X86_64_NEED_DYNAMIC_RELOC_TYPE_P (TYPE) \
  78. : I386_NEED_DYNAMIC_RELOC_TYPE_P (TYPE))
  79. #define X86_LOCAL_GOT_RELATIVE_RELOC_P(IS_X86_64, INFO, SYM) \
  80. (bfd_link_pic (INFO) \
  81. && (!(IS_X86_64) || ((SYM) != NULL && (SYM)->st_shndx != SHN_ABS)))
  82. #define PLT_CIE_LENGTH 20
  83. #define PLT_FDE_LENGTH 36
  84. #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
  85. #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
  86. #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
  87. #define X86_64_PCREL_TYPE_P(TYPE) \
  88. ((TYPE) == R_X86_64_PC8 \
  89. || (TYPE) == R_X86_64_PC16 \
  90. || (TYPE) == R_X86_64_PC32 \
  91. || (TYPE) == R_X86_64_PC32_BND \
  92. || (TYPE) == R_X86_64_PC64)
  93. #define ABI_64_P(abfd) \
  94. (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
  95. /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
  96. copying dynamic variables from a shared lib into an app's dynbss
  97. section, and instead use a dynamic relocation to point into the
  98. shared lib. */
  99. #define ELIMINATE_COPY_RELOCS 1
  100. #define elf_x86_hash_table(p, id) \
  101. (is_elf_hash_table ((p)->hash) \
  102. && elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
  103. ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
  104. /* Will references to this symbol always be local in this object? */
  105. #define SYMBOL_REFERENCES_LOCAL_P(INFO, H) \
  106. _bfd_x86_elf_link_symbol_references_local ((INFO), (H))
  107. /* TRUE if an undefined weak symbol should be resolved to 0. Local
  108. undefined weak symbol is always resolved to 0. Reference to an
  109. undefined weak symbol is resolved to 0 in executable if undefined
  110. weak symbol should be resolved to 0 (zero_undefweak > 0). */
  111. #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH) \
  112. ((EH)->elf.root.type == bfd_link_hash_undefweak \
  113. && (SYMBOL_REFERENCES_LOCAL_P ((INFO), &(EH)->elf) \
  114. || (bfd_link_executable (INFO) \
  115. && (EH)->zero_undefweak > 0)))
  116. /* Should copy relocation be generated for a symbol. Don't generate
  117. copy relocation against a protected symbol defined in a shared
  118. object with GNU_PROPERTY_NO_COPY_ON_PROTECTED. */
  119. #define SYMBOL_NO_COPYRELOC(INFO, EH) \
  120. ((EH)->def_protected \
  121. && ((EH)->elf.root.type == bfd_link_hash_defined \
  122. || (EH)->elf.root.type == bfd_link_hash_defweak) \
  123. && elf_has_no_copy_on_protected ((EH)->elf.root.u.def.section->owner) \
  124. && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
  125. && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
  126. /* TRUE if dynamic relocation is needed. If we are creating a shared
  127. library, and this is a reloc against a global symbol, or a non PC
  128. relative reloc against a local symbol, then we need to copy the reloc
  129. into the shared library. However, if we are linking with -Bsymbolic,
  130. we do not need to copy a reloc against a global symbol which is
  131. defined in an object we are including in the link (i.e., DEF_REGULAR
  132. is set).
  133. If PCREL_PLT is true, don't generate dynamic relocation in PIE for
  134. PC-relative relocation against a dynamic function definition in data
  135. section when PLT address can be used.
  136. If on the other hand, we are creating an executable, we may need to
  137. keep relocations for symbols satisfied by a dynamic library if we
  138. manage to avoid copy relocs for the symbol.
  139. We also need to generate dynamic pointer relocation against
  140. STT_GNU_IFUNC symbol in the non-code section. */
  141. #define NEED_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, PCREL_PLT, H, SEC, \
  142. R_TYPE, POINTER_TYPE) \
  143. ((bfd_link_pic (INFO) \
  144. && (! X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
  145. || ((H) != NULL \
  146. && (! (bfd_link_pie (INFO) \
  147. || SYMBOLIC_BIND ((INFO), (H))) \
  148. || (H)->root.type == bfd_link_hash_defweak \
  149. || (!(bfd_link_pie (INFO) \
  150. && (PCREL_PLT) \
  151. && (H)->plt.refcount > 0 \
  152. && ((SEC)->flags & SEC_CODE) == 0 \
  153. && (H)->type == STT_FUNC \
  154. && (H)->def_dynamic) \
  155. && !(H)->def_regular))))) \
  156. || ((H) != NULL \
  157. && (H)->type == STT_GNU_IFUNC \
  158. && (R_TYPE) == POINTER_TYPE \
  159. && ((SEC)->flags & SEC_CODE) == 0) \
  160. || (ELIMINATE_COPY_RELOCS \
  161. && !bfd_link_pic (INFO) \
  162. && (H) != NULL \
  163. && ((H)->root.type == bfd_link_hash_defweak \
  164. || !(H)->def_regular)))
  165. /* TRUE if dynamic relocation should be generated. Don't copy a
  166. pc-relative relocation into the output file if the symbol needs
  167. copy reloc or the symbol is undefined when building executable.
  168. Copy dynamic function pointer relocations. Don't generate dynamic
  169. relocations against resolved undefined weak symbols in PIE, except
  170. when PC32_RELOC is TRUE. Undefined weak symbol is bound locally
  171. when PIC is false. Don't generate dynamic relocations against
  172. non-preemptible absolute symbol. NB: rel_from_abs is set on symbols
  173. defined by linker scripts from "dot" (also SEGMENT_START or ORIGIN)
  174. outside of an output section statement, which will be converted from
  175. absolute to section-relative in set_sym_sections called from
  176. ldexp_finalize_syms after ldemul_finish. */
  177. #define GENERATE_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, EH, R_TYPE, \
  178. SEC, NEED_COPY_RELOC_IN_PIE, \
  179. RESOLVED_TO_ZERO, PC32_RELOC) \
  180. ((bfd_link_pic (INFO) \
  181. && !(bfd_is_abs_section (SEC) \
  182. && ((EH) == NULL \
  183. || (EH)->elf.root.rel_from_abs == 0) \
  184. && ((EH) == NULL \
  185. || SYMBOL_REFERENCES_LOCAL (INFO, &(EH)->elf))) \
  186. && !(NEED_COPY_RELOC_IN_PIE) \
  187. && ((EH) == NULL \
  188. || ((ELF_ST_VISIBILITY ((EH)->elf.other) == STV_DEFAULT \
  189. && (!(RESOLVED_TO_ZERO) || PC32_RELOC)) \
  190. || (EH)->elf.root.type != bfd_link_hash_undefweak)) \
  191. && ((!X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
  192. && !X86_SIZE_TYPE_P (IS_X86_64, R_TYPE)) \
  193. || ! SYMBOL_CALLS_LOCAL ((INFO), \
  194. (struct elf_link_hash_entry *) (EH)))) \
  195. || (ELIMINATE_COPY_RELOCS \
  196. && !bfd_link_pic (INFO) \
  197. && (EH) != NULL \
  198. && (EH)->elf.dynindx != -1 \
  199. && (!(EH)->elf.non_got_ref \
  200. || ((EH)->elf.root.type == bfd_link_hash_undefweak \
  201. && !(RESOLVED_TO_ZERO))) \
  202. && (((EH)->elf.def_dynamic && !(EH)->elf.def_regular) \
  203. || (EH)->elf.root.type == bfd_link_hash_undefined)))
  204. /* TRUE if this input relocation should be copied to output. H->dynindx
  205. may be -1 if this symbol was marked to become local. */
  206. #define COPY_INPUT_RELOC_P(IS_X86_64, INFO, H, R_TYPE) \
  207. ((H) != NULL \
  208. && (H)->dynindx != -1 \
  209. && (X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
  210. || !(bfd_link_executable (INFO) || SYMBOLIC_BIND ((INFO), (H))) \
  211. || !(H)->def_regular))
  212. /* TRUE if this is actually a static link, or it is a -Bsymbolic link
  213. and the symbol is defined locally, or the symbol was forced to be
  214. local because of a version file. */
  215. #define RESOLVED_LOCALLY_P(INFO, H, HTAB) \
  216. (!WILL_CALL_FINISH_DYNAMIC_SYMBOL ((HTAB)->elf.dynamic_sections_created, \
  217. bfd_link_pic (INFO), (H)) \
  218. || (bfd_link_pic (INFO) \
  219. && SYMBOL_REFERENCES_LOCAL_P ((INFO), (H))) \
  220. || (ELF_ST_VISIBILITY ((H)->other) \
  221. && (H)->root.type == bfd_link_hash_undefweak))
  222. /* TRUE if this symbol isn't defined by a shared object. */
  223. #define SYMBOL_DEFINED_NON_SHARED_P(H) \
  224. ((H)->def_regular \
  225. || (H)->root.linker_def \
  226. || (H)->root.ldscript_def \
  227. || ((struct elf_x86_link_hash_entry *) (H))->linker_def \
  228. || ELF_COMMON_DEF_P (H))
  229. /* Return TRUE if the symbol described by a linker hash entry H is
  230. going to be absolute. Similar to bfd_is_abs_symbol, but excluding
  231. all linker-script defined symbols. */
  232. #define ABS_SYMBOL_P(H) \
  233. (bfd_is_abs_symbol (&(H)->root) && !(H)->root.ldscript_def)
  234. /* TRUE if relative relocation should be generated. GOT reference to
  235. global symbol in PIC will lead to dynamic symbol. It becomes a
  236. problem when "time" or "times" is defined as a variable in an
  237. executable, clashing with functions of the same name in libc. If a
  238. symbol isn't undefined weak symbol, don't make it dynamic in PIC and
  239. generate relative relocation. Don't generate relative relocation
  240. against non-preemptible absolute symbol. */
  241. #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
  242. ((H)->dynindx == -1 \
  243. && !(H)->forced_local \
  244. && (H)->root.type != bfd_link_hash_undefweak \
  245. && bfd_link_pic (INFO) \
  246. && !ABS_SYMBOL_P (H))
  247. /* TRUE if this is a pointer reference to a local IFUNC. */
  248. #define POINTER_LOCAL_IFUNC_P(INFO, H) \
  249. ((H)->dynindx == -1 \
  250. || (H)->forced_local \
  251. || bfd_link_executable (INFO))
  252. /* TRUE if this is a PLT reference to a local IFUNC. */
  253. #define PLT_LOCAL_IFUNC_P(INFO, H) \
  254. ((H)->dynindx == -1 \
  255. || ((bfd_link_executable (INFO) \
  256. || ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT) \
  257. && (H)->def_regular \
  258. && (H)->type == STT_GNU_IFUNC))
  259. /* TRUE if TLS IE->LE transition is OK. */
  260. #define TLS_TRANSITION_IE_TO_LE_P(INFO, H, TLS_TYPE) \
  261. (bfd_link_executable (INFO) \
  262. && (H) != NULL \
  263. && (H)->dynindx == -1 \
  264. && (TLS_TYPE & GOT_TLS_IE))
  265. /* Verify that the symbol has an entry in the procedure linkage table. */
  266. #define VERIFY_PLT_ENTRY(INFO, H, PLT, GOTPLT, RELPLT, LOCAL_UNDEFWEAK) \
  267. do \
  268. { \
  269. if (((H)->dynindx == -1 \
  270. && !LOCAL_UNDEFWEAK \
  271. && !(((H)->forced_local || bfd_link_executable (INFO)) \
  272. && (H)->def_regular \
  273. && (H)->type == STT_GNU_IFUNC)) \
  274. || (PLT) == NULL \
  275. || (GOTPLT) == NULL \
  276. || (RELPLT) == NULL) \
  277. abort (); \
  278. } \
  279. while (0);
  280. /* Verify that the symbol supports copy relocation. */
  281. #define VERIFY_COPY_RELOC(H, HTAB) \
  282. do \
  283. { \
  284. if ((H)->dynindx == -1 \
  285. || ((H)->root.type != bfd_link_hash_defined \
  286. && (H)->root.type != bfd_link_hash_defweak) \
  287. || (HTAB)->elf.srelbss == NULL \
  288. || (HTAB)->elf.sreldynrelro == NULL) \
  289. abort (); \
  290. } \
  291. while (0);
  292. /* x86 ELF linker hash entry. */
  293. struct elf_x86_link_hash_entry
  294. {
  295. struct elf_link_hash_entry elf;
  296. unsigned char tls_type;
  297. /* Bit 0: Symbol has no GOT nor PLT relocations.
  298. Bit 1: Symbol has non-GOT/non-PLT relocations in text sections.
  299. zero_undefweak is initialized to 1 and undefined weak symbol
  300. should be resolved to 0 if zero_undefweak > 0. */
  301. unsigned int zero_undefweak : 2;
  302. /* Don't call finish_dynamic_symbol on this symbol. */
  303. unsigned int no_finish_dynamic_symbol : 1;
  304. /* R_*_RELATIVE relocation in GOT for this symbol has been
  305. processed. */
  306. unsigned int got_relative_reloc_done : 1;
  307. /* TRUE if symbol is __tls_get_addr. */
  308. unsigned int tls_get_addr : 1;
  309. /* TRUE if symbol is defined as a protected symbol. */
  310. unsigned int def_protected : 1;
  311. /* 0: Symbol references are unknown.
  312. 1: Symbol references aren't local.
  313. 2: Symbol references are local.
  314. */
  315. unsigned int local_ref : 2;
  316. /* TRUE if symbol is defined by linker. */
  317. unsigned int linker_def : 1;
  318. /* TRUE if symbol is referenced by a non-GOT/non-PLT relocation in a
  319. relocatable object file without indirect external access marker. */
  320. unsigned int non_got_ref_without_indirect_extern_access : 1;
  321. /* TRUE if symbol is referenced by R_386_GOTOFF relocation. This is
  322. only used by i386. */
  323. unsigned int gotoff_ref : 1;
  324. /* TRUE if a weak symbol with a real definition needs a copy reloc.
  325. When there is a weak symbol with a real definition, the processor
  326. independent code will have arranged for us to see the real
  327. definition first. We need to copy the needs_copy bit from the
  328. real definition and check it when allowing copy reloc in PIE. This
  329. is only used by x86-64. */
  330. unsigned int needs_copy : 1;
  331. /* Information about the GOT PLT entry. Filled when there are both
  332. GOT and PLT relocations against the same function. */
  333. union gotplt_union plt_got;
  334. /* Information about the second PLT entry. */
  335. union gotplt_union plt_second;
  336. /* Offset of the GOTPLT entry reserved for the TLS descriptor,
  337. starting at the end of the jump table. */
  338. bfd_vma tlsdesc_got;
  339. };
  340. struct elf_x86_lazy_plt_layout
  341. {
  342. /* The first entry in a lazy procedure linkage table looks like this. */
  343. const bfd_byte *plt0_entry;
  344. unsigned int plt0_entry_size; /* Size of PLT0 entry. */
  345. /* Later entries in a lazy procedure linkage table look like this. */
  346. const bfd_byte *plt_entry;
  347. unsigned int plt_entry_size; /* Size of each PLT entry. */
  348. /* The TLSDESC entry in a lazy procedure linkage table looks like
  349. this. This is for x86-64 only. */
  350. const bfd_byte *plt_tlsdesc_entry;
  351. unsigned int plt_tlsdesc_entry_size; /* Size of TLSDESC entry. */
  352. /* Offsets into the TLSDESC entry that are to be replaced with
  353. GOT+8 and GOT+TDG. These are for x86-64 only. */
  354. unsigned int plt_tlsdesc_got1_offset;
  355. unsigned int plt_tlsdesc_got2_offset;
  356. /* Offset of the end of the PC-relative instructions containing
  357. plt_tlsdesc_got1_offset and plt_tlsdesc_got2_offset. These
  358. are for x86-64 only. */
  359. unsigned int plt_tlsdesc_got1_insn_end;
  360. unsigned int plt_tlsdesc_got2_insn_end;
  361. /* Offsets into plt0_entry that are to be replaced with GOT[1] and
  362. GOT[2]. */
  363. unsigned int plt0_got1_offset;
  364. unsigned int plt0_got2_offset;
  365. /* Offset of the end of the PC-relative instruction containing
  366. plt0_got2_offset. This is for x86-64 only. */
  367. unsigned int plt0_got2_insn_end;
  368. /* Offsets into plt_entry that are to be replaced with... */
  369. unsigned int plt_got_offset; /* ... address of this symbol in .got. */
  370. unsigned int plt_reloc_offset; /* ... offset into relocation table. */
  371. unsigned int plt_plt_offset; /* ... offset to start of .plt. */
  372. /* Length of the PC-relative instruction containing plt_got_offset.
  373. This is used for x86-64 only. */
  374. unsigned int plt_got_insn_size;
  375. /* Offset of the end of the PC-relative jump to plt0_entry. This is
  376. used for x86-64 only. */
  377. unsigned int plt_plt_insn_end;
  378. /* Offset into plt_entry where the initial value of the GOT entry
  379. points. */
  380. unsigned int plt_lazy_offset;
  381. /* The first entry in a PIC lazy procedure linkage table looks like
  382. this. */
  383. const bfd_byte *pic_plt0_entry;
  384. /* Subsequent entries in a PIC lazy procedure linkage table look
  385. like this. */
  386. const bfd_byte *pic_plt_entry;
  387. /* .eh_frame covering the lazy .plt section. */
  388. const bfd_byte *eh_frame_plt;
  389. unsigned int eh_frame_plt_size;
  390. };
  391. struct elf_x86_non_lazy_plt_layout
  392. {
  393. /* Entries in a non-lazy procedure linkage table look like this. */
  394. const bfd_byte *plt_entry;
  395. /* Entries in a PIC non-lazy procedure linkage table look like this.
  396. This is only used for i386 where absolute PLT and PIC PLT are
  397. different. */
  398. const bfd_byte *pic_plt_entry;
  399. unsigned int plt_entry_size; /* Size of each PLT entry. */
  400. /* Offsets into plt_entry that are to be replaced with... */
  401. unsigned int plt_got_offset; /* ... address of this symbol in .got. */
  402. /* Length of the PC-relative instruction containing plt_got_offset.
  403. This is used for x86-64 only. */
  404. unsigned int plt_got_insn_size;
  405. /* .eh_frame covering the non-lazy .plt section. */
  406. const bfd_byte *eh_frame_plt;
  407. unsigned int eh_frame_plt_size;
  408. };
  409. struct elf_x86_plt_layout
  410. {
  411. /* The first entry in a lazy procedure linkage table looks like this. */
  412. const bfd_byte *plt0_entry;
  413. /* Entries in a procedure linkage table look like this. */
  414. const bfd_byte *plt_entry;
  415. unsigned int plt_entry_size; /* Size of each PLT entry. */
  416. /* 1 has PLT0. */
  417. unsigned int has_plt0;
  418. /* Offsets into plt_entry that are to be replaced with... */
  419. unsigned int plt_got_offset; /* ... address of this symbol in .got. */
  420. /* Length of the PC-relative instruction containing plt_got_offset.
  421. This is only used for x86-64. */
  422. unsigned int plt_got_insn_size;
  423. /* Alignment of the .iplt section. */
  424. unsigned int iplt_alignment;
  425. /* .eh_frame covering the .plt section. */
  426. const bfd_byte *eh_frame_plt;
  427. unsigned int eh_frame_plt_size;
  428. };
  429. /* Values in tls_type of x86 ELF linker hash entry. */
  430. #define GOT_UNKNOWN 0
  431. #define GOT_NORMAL 1
  432. #define GOT_TLS_GD 2
  433. #define GOT_TLS_IE 4
  434. #define GOT_TLS_IE_POS 5
  435. #define GOT_TLS_IE_NEG 6
  436. #define GOT_TLS_IE_BOTH 7
  437. #define GOT_TLS_GDESC 8
  438. #define GOT_ABS 9
  439. #define GOT_TLS_GD_BOTH_P(type) \
  440. ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
  441. #define GOT_TLS_GD_P(type) \
  442. ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
  443. #define GOT_TLS_GDESC_P(type) \
  444. ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
  445. #define GOT_TLS_GD_ANY_P(type) \
  446. (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
  447. #define elf_x86_hash_entry(ent) \
  448. ((struct elf_x86_link_hash_entry *)(ent))
  449. /* Information of an input relocation used to compute its contribution
  450. to the DT_RELR section size. */
  451. struct elf_x86_relative_reloc_record
  452. {
  453. /* The original relocation info. */
  454. Elf_Internal_Rela rel;
  455. /* The input or the GOT section where the relocation is applied. */
  456. asection *sec;
  457. /* Local symbol info. NULL for global symbol. */
  458. Elf_Internal_Sym *sym;
  459. union
  460. {
  461. /* Section where the local symbol is defined. */
  462. asection *sym_sec;
  463. /* Global symbol hash. */
  464. struct elf_link_hash_entry *h;
  465. } u;
  466. /* The offset into the output section where the relative relocation
  467. will be applied at run-time. */
  468. bfd_vma offset;
  469. /* The run-time address. */
  470. bfd_vma address;
  471. };
  472. struct elf_x86_relative_reloc_data
  473. {
  474. bfd_size_type count;
  475. bfd_size_type size;
  476. struct elf_x86_relative_reloc_record *data;
  477. };
  478. /* DT_RELR bitmap. */
  479. struct elf_dt_relr_bitmap
  480. {
  481. bfd_size_type count;
  482. bfd_size_type size;
  483. union
  484. {
  485. /* 32-bit bitmap. */
  486. uint32_t *elf32;
  487. /* 64-bit bitmap. */
  488. uint64_t *elf64;
  489. } u;
  490. };
  491. /* x86 ELF linker hash table. */
  492. struct elf_x86_link_hash_table
  493. {
  494. struct elf_link_hash_table elf;
  495. /* Short-cuts to get to dynamic linker sections. */
  496. asection *interp;
  497. asection *plt_eh_frame;
  498. asection *plt_second;
  499. asection *plt_second_eh_frame;
  500. asection *plt_got;
  501. asection *plt_got_eh_frame;
  502. /* Parameters describing PLT generation, lazy or non-lazy. */
  503. struct elf_x86_plt_layout plt;
  504. /* Parameters describing lazy PLT generation. */
  505. const struct elf_x86_lazy_plt_layout *lazy_plt;
  506. /* Parameters describing non-lazy PLT generation. */
  507. const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
  508. union
  509. {
  510. bfd_signed_vma refcount;
  511. bfd_vma offset;
  512. } tls_ld_or_ldm_got;
  513. /* The amount of space used by the jump slots in the GOT. */
  514. bfd_vma sgotplt_jump_table_size;
  515. /* _TLS_MODULE_BASE_ symbol. */
  516. struct bfd_link_hash_entry *tls_module_base;
  517. /* Used by local STT_GNU_IFUNC symbols. */
  518. htab_t loc_hash_table;
  519. void * loc_hash_memory;
  520. /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
  521. bfd_vma next_jump_slot_index;
  522. /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
  523. bfd_vma next_irelative_index;
  524. /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
  525. This is used for i386 only. */
  526. asection *srelplt2;
  527. /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
  528. is only used for i386. */
  529. bfd_vma next_tls_desc_index;
  530. /* DT_RELR bitmap. */
  531. struct elf_dt_relr_bitmap dt_relr_bitmap;
  532. /* Relative relocation data. */
  533. struct elf_x86_relative_reloc_data relative_reloc;
  534. /* Unaligned relative relocation data. */
  535. struct elf_x86_relative_reloc_data unaligned_relative_reloc;
  536. /* Number of relative reloc generation pass. */
  537. unsigned int generate_relative_reloc_pass;
  538. /* Value used to fill the unused bytes of the first PLT entry. This
  539. is only used for i386. */
  540. bfd_byte plt0_pad_byte;
  541. /* TRUE if GOT is referenced. */
  542. unsigned int got_referenced : 1;
  543. /* TRUE if PLT is PC-relative. PLT in PDE and PC-relative PLT in PIE
  544. can be used as function address.
  545. NB: i386 has non-PIC PLT and PIC PLT. Only non-PIC PLT in PDE can
  546. be used as function address. PIC PLT in PIE can't be used as
  547. function address. */
  548. unsigned int pcrel_plt : 1;
  549. bfd_vma (*r_info) (bfd_vma, bfd_vma);
  550. bfd_vma (*r_sym) (bfd_vma);
  551. bool (*is_reloc_section) (const char *);
  552. unsigned int sizeof_reloc;
  553. unsigned int got_entry_size;
  554. unsigned int pointer_r_type;
  555. unsigned int relative_r_type;
  556. int dynamic_interpreter_size;
  557. const char *dynamic_interpreter;
  558. const char *tls_get_addr;
  559. const char *relative_r_name;
  560. void (*elf_append_reloc) (bfd *, asection *, Elf_Internal_Rela *);
  561. void (*elf_write_addend) (bfd *, uint64_t, void *);
  562. void (*elf_write_addend_in_got) (bfd *, uint64_t, void *);
  563. /* Options passed from the linker. */
  564. struct elf_linker_x86_params *params;
  565. };
  566. struct elf_x86_init_table
  567. {
  568. /* The lazy PLT layout. */
  569. const struct elf_x86_lazy_plt_layout *lazy_plt;
  570. /* The non-lazy PLT layout. */
  571. const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
  572. /* The lazy PLT layout for IBT. */
  573. const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
  574. /* The non-lazy PLT layout for IBT. */
  575. const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
  576. bfd_byte plt0_pad_byte;
  577. bfd_vma (*r_info) (bfd_vma, bfd_vma);
  578. bfd_vma (*r_sym) (bfd_vma);
  579. };
  580. struct elf_x86_obj_tdata
  581. {
  582. struct elf_obj_tdata root;
  583. /* tls_type for each local got entry. */
  584. char *local_got_tls_type;
  585. /* GOTPLT entries for TLS descriptors. */
  586. bfd_vma *local_tlsdesc_gotent;
  587. /* R_*_RELATIVE relocation in GOT for this local symbol has been
  588. processed. */
  589. char *relative_reloc_done;
  590. };
  591. enum elf_x86_plt_type
  592. {
  593. plt_non_lazy = 0,
  594. plt_lazy = 1 << 0,
  595. plt_pic = 1 << 1,
  596. plt_second = 1 << 2,
  597. plt_unknown = -1
  598. };
  599. struct elf_x86_plt
  600. {
  601. const char *name;
  602. asection *sec;
  603. bfd_byte *contents;
  604. enum elf_x86_plt_type type;
  605. unsigned int plt_got_offset;
  606. unsigned int plt_entry_size;
  607. unsigned int plt_got_insn_size; /* Only used for x86-64. */
  608. long count;
  609. };
  610. /* Set if a relocation is converted from a GOTPCREL relocation. */
  611. #define R_X86_64_converted_reloc_bit (1 << 7)
  612. #define elf_x86_tdata(abfd) \
  613. ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
  614. #define elf_x86_local_got_tls_type(abfd) \
  615. (elf_x86_tdata (abfd)->local_got_tls_type)
  616. #define elf_x86_local_tlsdesc_gotent(abfd) \
  617. (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
  618. #define elf_x86_relative_reloc_done(abfd) \
  619. (elf_x86_tdata (abfd)->relative_reloc_done)
  620. #define elf_x86_compute_jump_table_size(htab) \
  621. ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
  622. #define is_x86_elf(bfd, htab) \
  623. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  624. && elf_tdata (bfd) != NULL \
  625. && elf_object_id (bfd) == (htab)->elf.hash_table_id)
  626. /* Rename some of the generic section flags to better document how they
  627. are used here. */
  628. #define check_relocs_failed sec_flg0
  629. #define relative_reloc_packed sec_flg1
  630. extern bool _bfd_x86_elf_mkobject
  631. (bfd *);
  632. extern void _bfd_x86_elf_set_tls_module_base
  633. (struct bfd_link_info *);
  634. extern bfd_vma _bfd_x86_elf_dtpoff_base
  635. (struct bfd_link_info *);
  636. extern bool _bfd_x86_elf_readonly_dynrelocs
  637. (struct elf_link_hash_entry *, void *);
  638. extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
  639. (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
  640. bool);
  641. extern hashval_t _bfd_x86_elf_local_htab_hash
  642. (const void *);
  643. extern int _bfd_x86_elf_local_htab_eq
  644. (const void *, const void *);
  645. extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
  646. (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  647. extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
  648. (bfd *);
  649. extern int _bfd_x86_elf_compare_relocs
  650. (const void *, const void *);
  651. extern bool _bfd_x86_elf_link_check_relocs
  652. (bfd *, struct bfd_link_info *);
  653. extern bool _bfd_x86_elf_check_relocs
  654. (bfd *, struct bfd_link_info *, asection *,
  655. const Elf_Internal_Rela *);
  656. extern bool _bfd_x86_elf_link_relax_section
  657. (bfd *, asection *, struct bfd_link_info *, bool *);
  658. extern bool _bfd_elf_x86_size_relative_relocs
  659. (struct bfd_link_info *, bool *);
  660. extern bool _bfd_elf_x86_finish_relative_relocs
  661. (struct bfd_link_info *);
  662. extern void _bfd_elf32_write_addend (bfd *, uint64_t, void *);
  663. extern void _bfd_elf64_write_addend (bfd *, uint64_t, void *);
  664. extern bool _bfd_elf_x86_valid_reloc_p
  665. (asection *, struct bfd_link_info *, struct elf_x86_link_hash_table *,
  666. const Elf_Internal_Rela *, struct elf_link_hash_entry *,
  667. Elf_Internal_Sym *, Elf_Internal_Shdr *, bool *);
  668. extern bool _bfd_x86_elf_size_dynamic_sections
  669. (bfd *, struct bfd_link_info *);
  670. extern struct elf_x86_link_hash_table *_bfd_x86_elf_finish_dynamic_sections
  671. (bfd *, struct bfd_link_info *);
  672. extern bool _bfd_x86_elf_always_size_sections
  673. (bfd *, struct bfd_link_info *);
  674. extern void _bfd_x86_elf_merge_symbol_attribute
  675. (struct elf_link_hash_entry *, unsigned int, bool, bool);
  676. extern void _bfd_x86_elf_copy_indirect_symbol
  677. (struct bfd_link_info *, struct elf_link_hash_entry *,
  678. struct elf_link_hash_entry *);
  679. extern bool _bfd_x86_elf_fixup_symbol
  680. (struct bfd_link_info *, struct elf_link_hash_entry *);
  681. extern bool _bfd_x86_elf_hash_symbol
  682. (struct elf_link_hash_entry *);
  683. extern bool _bfd_x86_elf_adjust_dynamic_symbol
  684. (struct bfd_link_info *, struct elf_link_hash_entry *);
  685. extern void _bfd_x86_elf_hide_symbol
  686. (struct bfd_link_info *, struct elf_link_hash_entry *, bool);
  687. extern bool _bfd_x86_elf_link_symbol_references_local
  688. (struct bfd_link_info *, struct elf_link_hash_entry *);
  689. extern asection * _bfd_x86_elf_gc_mark_hook
  690. (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
  691. struct elf_link_hash_entry *, Elf_Internal_Sym *);
  692. extern long _bfd_x86_elf_get_synthetic_symtab
  693. (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
  694. asymbol **);
  695. extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
  696. (bfd *, unsigned int, bfd_byte *, unsigned int);
  697. extern bool _bfd_x86_elf_merge_gnu_properties
  698. (struct bfd_link_info *, bfd *, bfd *, elf_property *, elf_property *);
  699. extern void _bfd_x86_elf_link_fixup_gnu_properties
  700. (struct bfd_link_info *, elf_property_list **);
  701. extern bfd * _bfd_x86_elf_link_setup_gnu_properties
  702. (struct bfd_link_info *, struct elf_x86_init_table *);
  703. extern void _bfd_x86_elf_link_fixup_ifunc_symbol
  704. (struct bfd_link_info *, struct elf_x86_link_hash_table *,
  705. struct elf_link_hash_entry *, Elf_Internal_Sym *sym);
  706. extern void _bfd_x86_elf_link_report_relative_reloc
  707. (struct bfd_link_info *, asection *, struct elf_link_hash_entry *,
  708. Elf_Internal_Sym *, const char *, const void *);
  709. #define bfd_elf64_mkobject \
  710. _bfd_x86_elf_mkobject
  711. #define bfd_elf32_mkobject \
  712. _bfd_x86_elf_mkobject
  713. #define bfd_elf64_bfd_link_hash_table_create \
  714. _bfd_x86_elf_link_hash_table_create
  715. #define bfd_elf32_bfd_link_hash_table_create \
  716. _bfd_x86_elf_link_hash_table_create
  717. #define bfd_elf64_bfd_link_check_relocs \
  718. _bfd_x86_elf_link_check_relocs
  719. #define bfd_elf32_bfd_link_check_relocs \
  720. _bfd_x86_elf_link_check_relocs
  721. #define bfd_elf32_bfd_relax_section \
  722. _bfd_x86_elf_link_relax_section
  723. #define bfd_elf64_bfd_relax_section \
  724. _bfd_x86_elf_link_relax_section
  725. #define elf_backend_check_relocs \
  726. _bfd_x86_elf_check_relocs
  727. #define elf_backend_size_dynamic_sections \
  728. _bfd_x86_elf_size_dynamic_sections
  729. #define elf_backend_merge_symbol_attribute \
  730. _bfd_x86_elf_merge_symbol_attribute
  731. #define elf_backend_copy_indirect_symbol \
  732. _bfd_x86_elf_copy_indirect_symbol
  733. #define elf_backend_fixup_symbol \
  734. _bfd_x86_elf_fixup_symbol
  735. #define elf_backend_hash_symbol \
  736. _bfd_x86_elf_hash_symbol
  737. #define elf_backend_adjust_dynamic_symbol \
  738. _bfd_x86_elf_adjust_dynamic_symbol
  739. #define elf_backend_gc_mark_hook \
  740. _bfd_x86_elf_gc_mark_hook
  741. #define elf_backend_omit_section_dynsym \
  742. _bfd_elf_omit_section_dynsym_all
  743. #define elf_backend_parse_gnu_properties \
  744. _bfd_x86_elf_parse_gnu_properties
  745. #define elf_backend_merge_gnu_properties \
  746. _bfd_x86_elf_merge_gnu_properties
  747. #define elf_backend_fixup_gnu_properties \
  748. _bfd_x86_elf_link_fixup_gnu_properties
  749. #define elf_backend_size_relative_relocs \
  750. _bfd_elf_x86_size_relative_relocs
  751. #define elf_backend_finish_relative_relocs \
  752. _bfd_elf_x86_finish_relative_relocs
  753. #define ELF_P_ALIGN ELF_MINPAGESIZE
  754. /* Allocate x86 GOT info for local symbols. */
  755. static inline bool
  756. elf_x86_allocate_local_got_info (bfd *abfd, bfd_size_type count)
  757. {
  758. bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
  759. if (local_got_refcounts == NULL)
  760. {
  761. bfd_size_type size = count * (sizeof (bfd_signed_vma)
  762. + sizeof (bfd_vma)
  763. + 2 * sizeof(char));
  764. local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
  765. if (local_got_refcounts == NULL)
  766. return false;
  767. elf_local_got_refcounts (abfd) = local_got_refcounts;
  768. elf_x86_local_tlsdesc_gotent (abfd) =
  769. (bfd_vma *) (local_got_refcounts + count);
  770. elf_x86_local_got_tls_type (abfd) =
  771. (char *) (local_got_refcounts + 2 * count);
  772. elf_x86_relative_reloc_done (abfd) =
  773. ((char *) (local_got_refcounts + 2 * count)) + count;
  774. }
  775. return true;
  776. }