cpuelf.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Includes ELF-related code specific to x86_64
  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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cpuelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. implementation
  21. uses
  22. globtype,cutils,cclasses,
  23. verbose,elfbase,
  24. systems,aasmbase,ogbase,ogelf,assemble;
  25. type
  26. TElfExeOutputx86_64=class(TElfExeOutput)
  27. private
  28. procedure MaybeWriteGOTEntry(relocval:aint;objsym:TObjSymbol);
  29. procedure MaybeWriteTLSIEGotEntry(relocval:aint;objsym:TObjSymbol);
  30. protected
  31. procedure WriteFirstPLTEntry;override;
  32. procedure WritePLTEntry(exesym:TExeSymbol);override;
  33. procedure WriteIndirectPLTEntry(exesym:TExeSymbol);override;
  34. procedure GOTRelocPass1(objsec:TObjSection;var idx:longint);override;
  35. procedure DoRelocationFixup(objsec:TObjSection);override;
  36. end;
  37. const
  38. { Relocation types }
  39. R_X86_64_NONE = 0;
  40. R_X86_64_64 = 1; { Direct 64 bit }
  41. R_X86_64_PC32 = 2; { PC relative 32 bit signed }
  42. R_X86_64_GOT32 = 3; { 32 bit GOT entry }
  43. R_X86_64_PLT32 = 4; { 32 bit PLT address }
  44. R_X86_64_COPY = 5; { Copy symbol at runtime }
  45. R_X86_64_GLOB_DAT = 6; { Create GOT entry }
  46. R_X86_64_JUMP_SLOT = 7; { Create PLT entry }
  47. R_X86_64_RELATIVE = 8; { Adjust by program base }
  48. R_X86_64_GOTPCREL = 9; { 32 bit signed PC relative offset to GOT }
  49. R_X86_64_32 = 10; { Direct 32 bit zero extended }
  50. R_X86_64_32S = 11; { Direct 32 bit sign extended }
  51. R_X86_64_16 = 12; { Direct 16 bit zero extended }
  52. R_X86_64_PC16 = 13; { 16 bit sign extended PC relative }
  53. R_X86_64_8 = 14; { Direct 8 bit sign extended }
  54. R_X86_64_PC8 = 15; { 8 bit sign extended PC relative }
  55. R_X86_64_DTPMOD64 = 16; { ID of module containing symbol }
  56. R_X86_64_DTPOFF64 = 17; { Offset in module's TLS block }
  57. R_X86_64_TPOFF64 = 18; { Offset in initial TLS block }
  58. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  59. R_X86_64_TLSGD = 19;
  60. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  61. R_X86_64_TLSLD = 20;
  62. R_X86_64_DTPOFF32 = 21; { Offset in TLS block }
  63. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  64. R_X86_64_GOTTPOFF = 22;
  65. R_X86_64_TPOFF32 = 23; { Offset in initial TLS block }
  66. R_X86_64_PC64 = 24; { PC relative 64-bit signed }
  67. R_X86_64_GOTOFF64 = 25; { 64-bit offset from GOT base }
  68. R_X86_64_GOTPC32 = 26; { PC-relative offset GOT }
  69. R_X86_64_GOT64 = 27; { 64-bit GOT entry offset }
  70. R_X86_64_GOTPCREL64 = 28; { 64-bit PC relative offset to GOT entry }
  71. R_X86_64_GOTPC64 = 29; { 64-bit PC relative offset to GOT }
  72. R_X86_64_GOTPLT64 = 30; { Like GOT64, indicates that PLT entry needed }
  73. R_X86_64_PLTOFF64 = 31; { 64-bit GOT relative offset to PLT entry }
  74. R_X86_64_SIZE32 = 32;
  75. R_X86_64_SIZE64 = 33;
  76. R_X86_64_GOTPC32_TLSDESC = 34;
  77. R_X86_64_TLSDESC_CALL = 35;
  78. R_X86_64_TLSDESC = 36;
  79. R_X86_64_IRELATIVE = 37;
  80. R_X86_64_GNU_VTINHERIT = 250; { GNU extension to record C++ vtable hierarchy }
  81. R_X86_64_GNU_VTENTRY = 251; { GNU extension to record C++ vtable member usage }
  82. type
  83. TRelocProp=record
  84. name: PChar;
  85. size: byte;
  86. end;
  87. const
  88. relocprops: array[0..37] of TRelocProp=(
  89. (name: 'R_X86_64_NONE'; size:0),
  90. (name: 'R_X86_64_64'; size:8),
  91. (name: 'R_X86_64_PC32'; size:4),
  92. (name: 'R_X86_64_GOT32'; size:4),
  93. (name: 'R_X86_64_PLT32'; size:4),
  94. (name: 'R_X86_64_COPY'; size:0),
  95. (name: 'R_X86_64_GLOB_DAT'; size:8),
  96. (name: 'R_X86_64_JUMP_SLOT';size:8),
  97. (name: 'R_X86_64_RELATIVE'; size:8),
  98. (name: 'R_X86_64_GOTPCREL'; size:4),
  99. (name: 'R_X86_64_32'; size:4),
  100. (name: 'R_X86_64_32S'; size:4),
  101. (name: 'R_X86_64_16'; size:2),
  102. (name: 'R_X86_64_PC16'; size:2),
  103. (name: 'R_X86_64_8'; size:1),
  104. (name: 'R_X86_64_PC8'; size:1),
  105. (name: 'R_X86_64_DTPMOD64'; size:8),
  106. (name: 'R_X86_64_DTPOFF64'; size:8),
  107. (name: 'R_X86_64_TPOFF64'; size:8),
  108. (name: 'R_X86_64_TLSGD'; size:4),
  109. (name: 'R_X86_64_TLSLD'; size:4),
  110. (name: 'R_X86_64_DTPOFF32'; size:4),
  111. (name: 'R_X86_64_GOTTPOFF'; size:4),
  112. (name: 'R_X86_64_TPOFF32'; size:4),
  113. (name: 'R_X86_64_PC64'; size:8),
  114. (name: 'R_X86_64_GOTOFF64'; size:8),
  115. (name: 'R_X86_64_GOTPC32'; size:4),
  116. (name: 'R_X86_64_GOT64'; size:8),
  117. (name: 'R_X86_64_GOTPCREL64'; size:8),
  118. (name: 'R_X86_64_GOTPC64'; size:8),
  119. (name: 'R_X86_64_GOTPLT64'; size:8),
  120. (name: 'R_X86_64_PLTOFF64'; size:8),
  121. (name: 'R_X86_64_SIZE32'; size:4),
  122. (name: 'R_X86_64_SIZE64'; size:8),
  123. (name: 'R_X86_64_GOTPC32_TLSDESC'; size:4),
  124. (name: 'R_X86_64_TLSDESC_CALL'; size:0),
  125. (name: 'R_X86_64_TLSDESC'; size:8),
  126. (name: 'R_X86_64_IRELATIVE'; size:8)
  127. );
  128. {****************************************************************************
  129. ELF Target methods
  130. ****************************************************************************}
  131. function elf_x86_64_encodereloc(objrel:TObjRelocation):byte;
  132. begin
  133. case objrel.typ of
  134. RELOC_NONE :
  135. result:=R_X86_64_NONE;
  136. { Note: 8 and 16-bit relocations are known to be non-conformant with
  137. AMD64 ABI, so they aren't handled. }
  138. RELOC_RELATIVE :
  139. if objrel.size=8 then
  140. result:=R_X86_64_PC64
  141. else if objrel.size=4 then
  142. result:=R_X86_64_PC32
  143. else
  144. InternalError(2012061900);
  145. RELOC_ABSOLUTE :
  146. if objrel.size=8 then
  147. result:=R_X86_64_64
  148. else if objrel.size=4 then
  149. result:=R_X86_64_32
  150. else
  151. InternalError(2012061901);
  152. RELOC_ABSOLUTE32 :
  153. result:=R_X86_64_32S;
  154. RELOC_GOTPCREL :
  155. result:=R_X86_64_GOTPCREL;
  156. RELOC_PLT32 :
  157. result:=R_X86_64_PLT32;
  158. else
  159. result:=0;
  160. InternalError(2012082302);
  161. end;
  162. end;
  163. procedure elf_x86_64_loadreloc(objrel:TObjRelocation);
  164. begin
  165. end;
  166. function elf_x86_64_relocname(reltyp:byte):string;
  167. begin
  168. if reltyp<=high(relocprops) then
  169. result:=relocprops[reltyp].name
  170. else
  171. result:='unknown ('+tostr(reltyp)+')';
  172. end;
  173. {****************************************************************************
  174. TELFExeOutputx86_64
  175. ****************************************************************************}
  176. procedure TElfExeOutputx86_64.GOTRelocPass1(objsec:TObjSection;var idx:longint);
  177. var
  178. objsym:TObjSymbol;
  179. objreloc:TObjRelocation;
  180. reltyp:byte;
  181. externsym,fromtext:boolean;
  182. begin
  183. objreloc:=TObjRelocation(objsec.ObjRelocations[idx]);
  184. if (ObjReloc.flags and rf_raw)=0 then
  185. reltyp:=ElfTarget.encodereloc(ObjReloc)
  186. else
  187. reltyp:=ObjReloc.ftype;
  188. case reltyp of
  189. R_X86_64_PLT32,
  190. R_X86_64_PLTOFF64,
  191. R_X86_64_GOTPLT64:
  192. begin
  193. if assigned(ObjReloc.symbol) and assigned(ObjReloc.symbol.exesymbol) then
  194. begin
  195. objsym:=ObjReloc.symbol.exesymbol.ObjSymbol;
  196. objsym.refs:=objsym.refs or symref_plt;
  197. end;
  198. end;
  199. end;
  200. case reltyp of
  201. R_X86_64_GOTTPOFF:
  202. begin
  203. { TLS IE to locally defined symbol, convert into LE so GOT entry isn't needed
  204. (Is TLS IE allowed in shared libs at all? Yes it is, when lib is accessing
  205. a threadvar in main program or in other *statically* loaded lib; TLS IE access to
  206. own threadvars may render library not loadable dynamically) }
  207. (*
  208. if not (IsSharedLibrary or (sym.dynindex>0)) then
  209. begin
  210. if not IsValidIEtoLE(objsec,ObjReloc) then
  211. Comment(v_error,'Cannot transform TLS IE to LE');
  212. TranslateIEtoLE(objsec,ObjReloc);
  213. ObjReloc.ftype:=R_X86_64_TPOFF32;
  214. exit;
  215. end;
  216. *)
  217. AllocGOTSlot(objreloc.symbol);
  218. end;
  219. R_X86_64_GOT32,
  220. R_X86_64_GOT64,
  221. R_X86_64_GOTPCREL,
  222. R_X86_64_GOTPCREL64:
  223. begin
  224. if AllocGOTSlot(objreloc.symbol) then
  225. if IsSharedLibrary and (objreloc.symbol.exesymbol.dynindex=0) then
  226. Inc(relative_reloc_count);
  227. end;
  228. //R_X86_64_TLSGD,
  229. //R_X86_64_TLSLD: { TODO: allocate two GOT slots }
  230. R_X86_64_TPOFF32,
  231. { R_X86_64_32S cannot be used in DSOs at all }
  232. R_X86_64_32S:
  233. if IsSharedLibrary then
  234. ReportNonDSOReloc(reltyp,objsec,objreloc);
  235. { R_X86_64_32 is processed by rtld, but binutils accept it in data sections only.
  236. Relocating against local symbols is tricky: changing into RELATIVE is not possible,
  237. so it is changed into relocation against section symbol. This requires adding
  238. the appropriate section symbol to dynamic symtable. BFD also has some obscure logic
  239. behind, e.g. it uses .text section for symbols from .data section.
  240. For now, leave this situation unhandled, as 32-bit relocations aren't commonly
  241. used in 64-bit code. }
  242. R_X86_64_32:
  243. if IsSharedLibrary then
  244. begin
  245. if (oso_executable in objsec.SecOptions) or
  246. not (oso_write in objsec.SecOptions) then
  247. ReportNonDSOReloc(reltyp,objsec,objreloc)
  248. else
  249. InternalError(2012092601);
  250. end;
  251. R_X86_64_64:
  252. begin
  253. fromtext:=(oso_executable in objsec.SecOptions) or
  254. not (oso_write in objsec.SecOptions);
  255. externsym:=assigned(objreloc.symbol) and
  256. assigned(objreloc.symbol.exesymbol) and
  257. (objreloc.symbol.exesymbol.dynindex<>0);
  258. if IsSharedLibrary then
  259. begin
  260. if fromtext then
  261. hastextrelocs:=True;
  262. dynrelocsec.alloc(dynrelocsec.shentsize);
  263. objreloc.flags:=objreloc.flags or rf_dynamic;
  264. if (not externsym) then
  265. Inc(relative_reloc_count);
  266. end
  267. else if externsym then
  268. // TODO: R_X86_64_32 and R_X86_64_32S here?
  269. begin
  270. objsym:=objreloc.symbol.ExeSymbol.ObjSymbol;
  271. { If symbol has non-GOT references from readonly sections, then it needs a
  272. copy reloc, which eliminates any dynamic relocations to this symbol from
  273. writable sections as well. OTOH if it is referenced *only* from writable
  274. sections, then it's better not to generate a copy reloc and keep dynamic
  275. relocations. The following code is based on assumption that all readonly
  276. sections are processed before writable ones (which is true for current
  277. segment mapping).
  278. For arbitrary segment mapping, this will probably require a separate pass. }
  279. if fromtext then
  280. objsym.refs:=objsym.refs or symref_from_text
  281. else if (objsym.refs and symref_from_text)=0 then
  282. begin
  283. dynrelocsec.alloc(dynrelocsec.shentsize);
  284. objreloc.flags:=objreloc.flags or rf_dynamic;
  285. end;
  286. end;
  287. end;
  288. end;
  289. end;
  290. procedure TElfExeOutputx86_64.MaybeWriteGOTEntry(relocval:aint;objsym:TObjSymbol);
  291. var
  292. gotoff,tmp:aword;
  293. begin
  294. gotoff:=objsym.exesymbol.gotoffset;
  295. if gotoff=0 then
  296. InternalError(2012060902);
  297. { the GOT slot itself, and a dynamic relocation for it }
  298. { TODO: only data symbols must get here }
  299. if gotoff=gotobjsec.Data.size+sizeof(pint) then
  300. begin
  301. gotobjsec.write(relocval,sizeof(pint));
  302. tmp:=gotobjsec.mempos+gotoff-sizeof(pint);
  303. if (objsym.exesymbol.dynindex>0) then
  304. dynreloclist.Add(TObjRelocation.CreateRaw(tmp,objsym,R_X86_64_GLOB_DAT))
  305. else
  306. if IsSharedLibrary then
  307. WriteDynRelocEntry(tmp,R_X86_64_RELATIVE,0,relocval);
  308. end;
  309. end;
  310. procedure TElfExeOutputx86_64.MaybeWriteTLSIEGotEntry(relocval:aint;objsym:TObjSymbol);
  311. var
  312. gotoff,tmp: aword;
  313. objrel: TObjRelocation;
  314. begin
  315. gotoff:=objsym.exesymbol.gotoffset;
  316. if gotoff=0 then
  317. InternalError(2012060903);
  318. if gotoff=gotobjsec.Data.size+sizeof(pint) then
  319. begin
  320. tmp:=gotobjsec.mempos+gotoff-sizeof(pint);
  321. if (objsym.exesymbol.dynindex>0) then
  322. begin
  323. gotobjsec.writezeros(sizeof(pint));
  324. dynreloclist.Add(TObjRelocation.CreateRaw(tmp,objsym,R_X86_64_TPOFF64));
  325. end
  326. else
  327. begin
  328. gotobjsec.write(relocval,sizeof(pint));
  329. if IsSharedLibrary then
  330. begin
  331. objrel:=TObjRelocation.CreateRaw(tmp,nil,R_X86_64_TPOFF64);
  332. objrel.orgsize:=relocval;
  333. dynreloclist.Add(objrel);
  334. end;
  335. end;
  336. end;
  337. end;
  338. procedure TElfExeOutputx86_64.DoRelocationFixup(objsec:TObjSection);
  339. var
  340. i,zero:longint;
  341. objreloc: TObjRelocation;
  342. address,
  343. relocval : aint;
  344. relocsec : TObjSection;
  345. data: TDynamicArray;
  346. reltyp,relsize: byte;
  347. PC: aword;
  348. begin
  349. data:=objsec.data;
  350. for i:=0 to objsec.ObjRelocations.Count-1 do
  351. begin
  352. objreloc:=TObjRelocation(objsec.ObjRelocations[i]);
  353. case objreloc.typ of
  354. RELOC_NONE:
  355. continue;
  356. RELOC_ZERO:
  357. begin
  358. data.Seek(objreloc.dataoffset);
  359. zero:=0;
  360. data.Write(zero,4);
  361. continue;
  362. end;
  363. else
  364. ;
  365. end;
  366. if (objreloc.flags and rf_raw)=0 then
  367. reltyp:=ElfTarget.encodereloc(objreloc)
  368. else
  369. reltyp:=objreloc.ftype;
  370. if reltyp<=high(relocprops) then
  371. relsize:=relocprops[reltyp].size
  372. else
  373. InternalError(2012092103);
  374. if ElfTarget.relocs_use_addend then
  375. address:=objreloc.orgsize
  376. else
  377. begin
  378. data.Seek(objreloc.dataoffset);
  379. data.Read(address,relsize);
  380. end;
  381. if assigned(objreloc.symbol) then
  382. begin
  383. relocsec:=objreloc.symbol.objsection;
  384. relocval:=objreloc.symbol.address;
  385. end
  386. else if assigned(objreloc.objsection) then
  387. begin
  388. relocsec:=objreloc.objsection;
  389. relocval:=objreloc.objsection.mempos
  390. end
  391. else
  392. internalerror(2012060702);
  393. { Only debug sections are allowed to have relocs pointing to unused sections }
  394. if assigned(relocsec) and not (relocsec.used and assigned(relocsec.exesection)) and
  395. not (oso_debug in objsec.secoptions) then
  396. begin
  397. writeln(objsec.fullname,' references ',relocsec.fullname);
  398. internalerror(2012060703);
  399. end;
  400. PC:=objsec.mempos+objreloc.dataoffset;
  401. { TODO: if relocsec=nil, relocations must be copied to .rela.dyn section }
  402. if (relocsec=nil) or (relocsec.used) then
  403. case reltyp of
  404. R_X86_64_PC32,
  405. R_X86_64_PC64:
  406. begin
  407. // TODO: ld rejects PC32 relocations to dynamic symbols, they must use @PLT
  408. address:=address+relocval-PC;
  409. end;
  410. R_X86_64_PLT32:
  411. begin
  412. { If target is in current object, treat as RELOC_RELATIVE }
  413. address:=address+relocval-PC;
  414. end;
  415. //R_X86_64_DTPOFF64 is possible in data??
  416. R_X86_64_DTPOFF32:
  417. begin
  418. { In executable it behaves as TPOFF32 (i.e. generates negative offset),
  419. but data expressions like ".long foo@dtpoff" resolve to positive offset }
  420. if IsSharedLibrary or not (oso_executable in objsec.SecOptions) then
  421. address:=address+relocval-tlsseg.MemPos
  422. else
  423. address:=address+relocval-(tlsseg.MemPos+tlsseg.MemSize);
  424. end;
  425. R_X86_64_TPOFF32,
  426. R_X86_64_TPOFF64:
  427. address:=address+relocval-(tlsseg.MemPos+tlsseg.MemSize);
  428. R_X86_64_GOTTPOFF:
  429. begin
  430. if IsSharedLibrary then
  431. relocval:=relocval-tlsseg.MemPos
  432. else
  433. relocval:=relocval-(tlsseg.MemPos+tlsseg.MemSize);
  434. MaybeWriteTLSIEGotEntry(relocval,objreloc.symbol);
  435. { resolves to PC-relative offset to GOT slot }
  436. relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
  437. address:=address+relocval-PC;
  438. end;
  439. R_X86_64_GOTPCREL,
  440. R_X86_64_GOTPCREL64:
  441. begin
  442. MaybeWriteGOTEntry(relocval,objreloc.symbol);
  443. { resolves to PC-relative offset to GOT slot }
  444. relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
  445. address:=address+relocval-PC;
  446. end;
  447. R_X86_64_32S,
  448. R_X86_64_32:
  449. inc(address,relocval);
  450. R_X86_64_64:
  451. begin
  452. inc(address,relocval);
  453. if (objreloc.flags and rf_dynamic)<>0 then
  454. begin
  455. if (objreloc.symbol=nil) or
  456. (objreloc.symbol.exesymbol=nil) or
  457. (objreloc.symbol.exesymbol.dynindex=0) then
  458. WriteDynRelocEntry(PC,R_X86_64_RELATIVE,0,address)
  459. else
  460. dynreloclist.add(TObjRelocation.CreateRaw(PC,objreloc.symbol,R_X86_64_64));
  461. end;
  462. end;
  463. R_X86_64_GOTPC32,
  464. R_X86_64_GOTPC64:
  465. begin
  466. address:=address+gotsymbol.address-PC;
  467. end;
  468. R_X86_64_GOT32,
  469. R_X86_64_GOT64:
  470. begin
  471. MaybeWriteGOTEntry(relocval,objreloc.symbol);
  472. relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint)-gotsymbol.address;
  473. address:=address+relocval;
  474. end;
  475. R_X86_64_GOTOFF64,
  476. R_X86_64_PLTOFF64:
  477. begin
  478. address:=address+relocval-gotsymbol.address;
  479. end;
  480. else
  481. begin
  482. writeln(objreloc.typ);
  483. internalerror(200604014);
  484. end;
  485. end
  486. else { not relocsec.Used }
  487. address:=0; { Relocation in debug section points to unused section, which is eliminated by linker }
  488. case relsize of
  489. 8: ;
  490. 4:
  491. begin
  492. case reltyp of
  493. R_X86_64_32:
  494. if qword(address)>qword($FFFFFFFF) then
  495. ReportRelocOverflow(reltyp,objsec,objreloc);
  496. else
  497. if (address>high(longint)) or (address<low(longint)) then
  498. ReportRelocOverflow(reltyp,objsec,objreloc);
  499. end;
  500. end;
  501. else
  502. InternalError(2012101102);
  503. end;
  504. data.Seek(objreloc.dataoffset);
  505. data.Write(address,relsize);
  506. end;
  507. end;
  508. procedure TElfExeOutputx86_64.WriteFirstPLTEntry;
  509. begin
  510. pltobjsec.writeBytes(#$FF#$35); // push got+8(%rip)
  511. pltobjsec.writeReloc_internal(gotpltobjsec,sizeof(pint),4,RELOC_RELATIVE);
  512. pltobjsec.writeBytes(#$FF#$25); // jmp *got+16(%rip)
  513. pltobjsec.writeReloc_internal(gotpltobjsec,2*sizeof(pint),4,RELOC_RELATIVE);
  514. pltobjsec.writeBytes(#$0F#$1F#$40#$00); // nopl 0(%rax)
  515. end;
  516. procedure TElfExeOutputx86_64.WritePLTEntry(exesym:TExeSymbol);
  517. var
  518. got_offset: aword;
  519. tmp: pint;
  520. begin
  521. pltobjsec.writeBytes(#$FF#$25); // jmp *got+x(%rip)
  522. pltobjsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size,4,RELOC_RELATIVE);
  523. pltobjsec.writeBytes(#$68); // push $index
  524. tmp:=pltrelocsec.size div pltrelocsec.shentsize;
  525. pltobjsec.write(tmp,4);
  526. pltobjsec.writeBytes(#$E9); // jmp .plt
  527. tmp:=-(4+pltobjsec.Size);
  528. pltobjsec.write(tmp,4);
  529. { write a .got.plt slot pointing back to the 'push' instruction }
  530. gotpltobjsec.writeReloc_internal(pltobjsec,pltobjsec.size-(16-6),sizeof(pint),RELOC_ABSOLUTE);
  531. { write a .rela.plt entry (Elf64_rela record) }
  532. pltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
  533. got_offset:=(qword(exesym.dynindex) shl 32) or R_X86_64_JUMP_SLOT;
  534. pltrelocsec.write(got_offset,sizeof(pint));
  535. if ElfTarget.relocs_use_addend then
  536. pltrelocsec.writezeros(sizeof(pint));
  537. end;
  538. procedure TElfExeOutputx86_64.WriteIndirectPLTEntry(exesym:TExeSymbol);
  539. var
  540. tmp: pint;
  541. objsym:TObjSymbol;
  542. targetsym:TObjSymbol;
  543. begin
  544. targetsym:=exesym.ObjSymbol;
  545. objsym:=internalobjdata.CreateSymbol(exesym.name);
  546. objsym.typ:=AT_FUNCTION;
  547. objsym.bind:=exesym.ObjSymbol.bind; { AB_EXTERNAL or AB_WEAK_EXTERNAL }
  548. objsym.offset:=pltobjsec.size;
  549. objsym.objsection:=pltobjsec;
  550. exesym.ObjSymbol:=objsym;
  551. pltobjsec.writeBytes(#$FF#$25); // jmp *got+x(%rip)
  552. pltobjsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size,4,RELOC_RELATIVE);
  553. { TODO: Are these entries relevant when linking dynamic?
  554. (for static linking, they don't matter) }
  555. pltobjsec.writeBytes(#$68); // push $index
  556. tmp:=pltrelocsec.size div pltrelocsec.shentsize;
  557. pltobjsec.write(tmp,4);
  558. pltobjsec.writeBytes(#$E9); // jmp .plt
  559. tmp:=-(4+pltobjsec.Size);
  560. pltobjsec.write(tmp,4);
  561. { write a .got.plt slot pointing back to the 'push' instruction }
  562. gotpltobjsec.writeReloc_internal(pltobjsec,pltobjsec.size-(16-6),sizeof(pint),RELOC_ABSOLUTE);
  563. { write a .rela.iplt entry (Elf64_rela record) }
  564. ipltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
  565. tmp:=R_X86_64_IRELATIVE;
  566. ipltrelocsec.write(tmp,sizeof(pint));
  567. if ElfTarget.relocs_use_addend then
  568. ipltrelocsec.writeReloc_internal(targetsym.objsection,targetsym.offset,sizeof(pint),RELOC_ABSOLUTE);
  569. end;
  570. {*****************************************************************************
  571. Initialize
  572. *****************************************************************************}
  573. const
  574. elf_target_x86_64: TElfTarget =
  575. (
  576. max_page_size: $200000;
  577. exe_image_base: $400000;
  578. machine_code: EM_X86_64;
  579. relocs_use_addend: true;
  580. dyn_reloc_codes: (
  581. R_X86_64_RELATIVE,
  582. R_X86_64_GLOB_DAT,
  583. R_X86_64_JUMP_SLOT,
  584. R_X86_64_COPY,
  585. R_X86_64_IRELATIVE
  586. );
  587. relocname: @elf_x86_64_relocName;
  588. encodereloc: @elf_x86_64_encodeReloc;
  589. loadreloc: @elf_x86_64_loadReloc;
  590. loadsection: nil;
  591. encodeflags: nil;
  592. );
  593. as_x86_64_elf64_info : tasminfo =
  594. (
  595. id : as_x86_64_elf64;
  596. idtxt : 'ELF';
  597. asmbin : '';
  598. asmcmd : '';
  599. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  600. system_x86_64_openbsd,system_x86_64_netbsd,
  601. system_x86_64_dragonfly,system_x86_64_solaris,
  602. system_x86_64_aros,system_x86_64_android,
  603. system_x86_64_haiku];
  604. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  605. labelprefix : '.L';
  606. comment : '';
  607. dollarsign: '$';
  608. );
  609. initialization
  610. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  611. ElfTarget:=elf_target_x86_64;
  612. ElfExeOutputClass:=TElfExeOutputx86_64;
  613. end.