cpuelf.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. end;
  364. if (objreloc.flags and rf_raw)=0 then
  365. reltyp:=ElfTarget.encodereloc(objreloc)
  366. else
  367. reltyp:=objreloc.ftype;
  368. if reltyp<=high(relocprops) then
  369. relsize:=relocprops[reltyp].size
  370. else
  371. InternalError(2012092103);
  372. if ElfTarget.relocs_use_addend then
  373. address:=objreloc.orgsize
  374. else
  375. begin
  376. data.Seek(objreloc.dataoffset);
  377. data.Read(address,relsize);
  378. end;
  379. if assigned(objreloc.symbol) then
  380. begin
  381. relocsec:=objreloc.symbol.objsection;
  382. relocval:=objreloc.symbol.address;
  383. end
  384. else if assigned(objreloc.objsection) then
  385. begin
  386. relocsec:=objreloc.objsection;
  387. relocval:=objreloc.objsection.mempos
  388. end
  389. else
  390. internalerror(2012060702);
  391. { Only debug sections are allowed to have relocs pointing to unused sections }
  392. if assigned(relocsec) and not (relocsec.used and assigned(relocsec.exesection)) and
  393. not (oso_debug in objsec.secoptions) then
  394. begin
  395. writeln(objsec.fullname,' references ',relocsec.fullname);
  396. internalerror(2012060703);
  397. end;
  398. PC:=objsec.mempos+objreloc.dataoffset;
  399. { TODO: if relocsec=nil, relocations must be copied to .rela.dyn section }
  400. if (relocsec=nil) or (relocsec.used) then
  401. case reltyp of
  402. R_X86_64_PC32,
  403. R_X86_64_PC64:
  404. begin
  405. // TODO: ld rejects PC32 relocations to dynamic symbols, they must use @PLT
  406. address:=address+relocval-PC;
  407. end;
  408. R_X86_64_PLT32:
  409. begin
  410. { If target is in current object, treat as RELOC_RELATIVE }
  411. address:=address+relocval-PC;
  412. end;
  413. //R_X86_64_DTPOFF64 is possible in data??
  414. R_X86_64_DTPOFF32:
  415. begin
  416. { In executable it behaves as TPOFF32 (i.e. generates negative offset),
  417. but data expressions like ".long foo@dtpoff" resolve to positive offset }
  418. if IsSharedLibrary or not (oso_executable in objsec.SecOptions) then
  419. address:=address+relocval-tlsseg.MemPos
  420. else
  421. address:=address+relocval-(tlsseg.MemPos+tlsseg.MemSize);
  422. end;
  423. R_X86_64_TPOFF32,
  424. R_X86_64_TPOFF64:
  425. address:=address+relocval-(tlsseg.MemPos+tlsseg.MemSize);
  426. R_X86_64_GOTTPOFF:
  427. begin
  428. if IsSharedLibrary then
  429. relocval:=relocval-tlsseg.MemPos
  430. else
  431. relocval:=relocval-(tlsseg.MemPos+tlsseg.MemSize);
  432. MaybeWriteTLSIEGotEntry(relocval,objreloc.symbol);
  433. { resolves to PC-relative offset to GOT slot }
  434. relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
  435. address:=address+relocval-PC;
  436. end;
  437. R_X86_64_GOTPCREL,
  438. R_X86_64_GOTPCREL64:
  439. begin
  440. MaybeWriteGOTEntry(relocval,objreloc.symbol);
  441. { resolves to PC-relative offset to GOT slot }
  442. relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint);
  443. address:=address+relocval-PC;
  444. end;
  445. R_X86_64_32S,
  446. R_X86_64_32:
  447. inc(address,relocval);
  448. R_X86_64_64:
  449. begin
  450. inc(address,relocval);
  451. if (objreloc.flags and rf_dynamic)<>0 then
  452. begin
  453. if (objreloc.symbol=nil) or
  454. (objreloc.symbol.exesymbol=nil) or
  455. (objreloc.symbol.exesymbol.dynindex=0) then
  456. WriteDynRelocEntry(PC,R_X86_64_RELATIVE,0,address)
  457. else
  458. dynreloclist.add(TObjRelocation.CreateRaw(PC,objreloc.symbol,R_X86_64_64));
  459. end;
  460. end;
  461. R_X86_64_GOTPC32,
  462. R_X86_64_GOTPC64:
  463. begin
  464. address:=address+gotsymbol.address-PC;
  465. end;
  466. R_X86_64_GOT32,
  467. R_X86_64_GOT64:
  468. begin
  469. MaybeWriteGOTEntry(relocval,objreloc.symbol);
  470. relocval:=gotobjsec.mempos+objreloc.symbol.exesymbol.gotoffset-sizeof(pint)-gotsymbol.address;
  471. address:=address+relocval;
  472. end;
  473. R_X86_64_GOTOFF64,
  474. R_X86_64_PLTOFF64:
  475. begin
  476. address:=address+relocval-gotsymbol.address;
  477. end;
  478. else
  479. begin
  480. writeln(objreloc.typ);
  481. internalerror(200604014);
  482. end;
  483. end
  484. else { not relocsec.Used }
  485. address:=0; { Relocation in debug section points to unused section, which is eliminated by linker }
  486. case relsize of
  487. 8: ;
  488. 4:
  489. begin
  490. case reltyp of
  491. R_X86_64_32:
  492. if qword(address)>qword($FFFFFFFF) then
  493. ReportRelocOverflow(reltyp,objsec,objreloc);
  494. else
  495. if (address>high(longint)) or (address<low(longint)) then
  496. ReportRelocOverflow(reltyp,objsec,objreloc);
  497. end;
  498. end;
  499. else
  500. InternalError(2012101102);
  501. end;
  502. data.Seek(objreloc.dataoffset);
  503. data.Write(address,relsize);
  504. end;
  505. end;
  506. procedure TElfExeOutputx86_64.WriteFirstPLTEntry;
  507. begin
  508. pltobjsec.writeBytes(#$FF#$35); // push got+8(%rip)
  509. pltobjsec.writeReloc_internal(gotpltobjsec,sizeof(pint),4,RELOC_RELATIVE);
  510. pltobjsec.writeBytes(#$FF#$25); // jmp *got+16(%rip)
  511. pltobjsec.writeReloc_internal(gotpltobjsec,2*sizeof(pint),4,RELOC_RELATIVE);
  512. pltobjsec.writeBytes(#$0F#$1F#$40#$00); // nopl 0(%rax)
  513. end;
  514. procedure TElfExeOutputx86_64.WritePLTEntry(exesym:TExeSymbol);
  515. var
  516. got_offset: aword;
  517. tmp: pint;
  518. begin
  519. pltobjsec.writeBytes(#$FF#$25); // jmp *got+x(%rip)
  520. pltobjsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size,4,RELOC_RELATIVE);
  521. pltobjsec.writeBytes(#$68); // push $index
  522. tmp:=pltrelocsec.size div pltrelocsec.shentsize;
  523. pltobjsec.write(tmp,4);
  524. pltobjsec.writeBytes(#$E9); // jmp .plt
  525. tmp:=-(4+pltobjsec.Size);
  526. pltobjsec.write(tmp,4);
  527. { write a .got.plt slot pointing back to the 'push' instruction }
  528. gotpltobjsec.writeReloc_internal(pltobjsec,pltobjsec.size-(16-6),sizeof(pint),RELOC_ABSOLUTE);
  529. { write a .rela.plt entry (Elf64_rela record) }
  530. pltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
  531. got_offset:=(qword(exesym.dynindex) shl 32) or R_X86_64_JUMP_SLOT;
  532. pltrelocsec.write(got_offset,sizeof(pint));
  533. if ElfTarget.relocs_use_addend then
  534. pltrelocsec.writezeros(sizeof(pint));
  535. end;
  536. procedure TElfExeOutputx86_64.WriteIndirectPLTEntry(exesym:TExeSymbol);
  537. var
  538. tmp: pint;
  539. objsym:TObjSymbol;
  540. targetsym:TObjSymbol;
  541. begin
  542. targetsym:=exesym.ObjSymbol;
  543. objsym:=internalobjdata.CreateSymbol(exesym.name);
  544. objsym.typ:=AT_FUNCTION;
  545. objsym.bind:=exesym.ObjSymbol.bind; { AB_EXTERNAL or AB_WEAK_EXTERNAL }
  546. objsym.offset:=pltobjsec.size;
  547. objsym.objsection:=pltobjsec;
  548. exesym.ObjSymbol:=objsym;
  549. pltobjsec.writeBytes(#$FF#$25); // jmp *got+x(%rip)
  550. pltobjsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size,4,RELOC_RELATIVE);
  551. { TODO: Are these entries relevant when linking dynamic?
  552. (for static linking, they don't matter) }
  553. pltobjsec.writeBytes(#$68); // push $index
  554. tmp:=pltrelocsec.size div pltrelocsec.shentsize;
  555. pltobjsec.write(tmp,4);
  556. pltobjsec.writeBytes(#$E9); // jmp .plt
  557. tmp:=-(4+pltobjsec.Size);
  558. pltobjsec.write(tmp,4);
  559. { write a .got.plt slot pointing back to the 'push' instruction }
  560. gotpltobjsec.writeReloc_internal(pltobjsec,pltobjsec.size-(16-6),sizeof(pint),RELOC_ABSOLUTE);
  561. { write a .rela.iplt entry (Elf64_rela record) }
  562. ipltrelocsec.writeReloc_internal(gotpltobjsec,gotpltobjsec.size-sizeof(pint),sizeof(pint),RELOC_ABSOLUTE);
  563. tmp:=R_X86_64_IRELATIVE;
  564. ipltrelocsec.write(tmp,sizeof(pint));
  565. if ElfTarget.relocs_use_addend then
  566. ipltrelocsec.writeReloc_internal(targetsym.objsection,targetsym.offset,sizeof(pint),RELOC_ABSOLUTE);
  567. end;
  568. {*****************************************************************************
  569. Initialize
  570. *****************************************************************************}
  571. const
  572. elf_target_x86_64: TElfTarget =
  573. (
  574. max_page_size: $200000;
  575. exe_image_base: $400000;
  576. machine_code: EM_X86_64;
  577. relocs_use_addend: true;
  578. dyn_reloc_codes: (
  579. R_X86_64_RELATIVE,
  580. R_X86_64_GLOB_DAT,
  581. R_X86_64_JUMP_SLOT,
  582. R_X86_64_COPY,
  583. R_X86_64_IRELATIVE
  584. );
  585. relocname: @elf_x86_64_relocName;
  586. encodereloc: @elf_x86_64_encodeReloc;
  587. loadreloc: @elf_x86_64_loadReloc;
  588. loadsection: nil;
  589. encodeflags: nil;
  590. );
  591. as_x86_64_elf64_info : tasminfo =
  592. (
  593. id : as_x86_64_elf64;
  594. idtxt : 'ELF';
  595. asmbin : '';
  596. asmcmd : '';
  597. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  598. system_x86_64_openbsd,system_x86_64_netbsd,
  599. system_x86_64_dragonfly,system_x86_64_solaris,
  600. system_x86_64_aros,system_x86_64_android,
  601. system_x86_64_haiku];
  602. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  603. labelprefix : '.L';
  604. comment : '';
  605. dollarsign: '$';
  606. );
  607. initialization
  608. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  609. ElfTarget:=elf_target_x86_64;
  610. ElfExeOutputClass:=TElfExeOutputx86_64;
  611. end.