ogelf.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains the binary elf writer
  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 ogelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,globtype,
  23. { target }
  24. systems,
  25. { assembler }
  26. cpuinfo,cpubase,aasmbase,aasmtai,assemble,
  27. { output }
  28. ogbase;
  29. type
  30. TElfObjSection = class(TObjSection)
  31. public
  32. secshidx : longint; { index for the section in symtab }
  33. shstridx,
  34. shtype,
  35. shflags,
  36. shlink,
  37. shinfo,
  38. shentsize : longint;
  39. { relocation }
  40. relocsect : TElfObjSection;
  41. constructor create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  42. constructor create_ext(const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  43. destructor destroy;override;
  44. end;
  45. TElfObjData = class(TObjData)
  46. public
  47. symtabsect,
  48. strtabsect,
  49. shstrtabsect,
  50. gotpcsect,
  51. gotoffsect,
  52. goTSect,
  53. plTSect,
  54. symsect : TElfObjSection;
  55. syms : Tdynamicarray;
  56. constructor create(const n:string);override;
  57. destructor destroy;override;
  58. function sectionname(atype:TAsmSectiontype;const aname:string):string;override;
  59. function sectiontype2align(atype:TAsmSectiontype):shortint;override;
  60. procedure CreateDebugSections;override;
  61. procedure writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);override;
  62. procedure writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);override;
  63. end;
  64. TElfObjectOutput = class(tObjOutput)
  65. private
  66. elf32data : TElfObjData;
  67. symidx,
  68. localsyms : longint;
  69. procedure createrelocsection(s:TElfObjSection);
  70. procedure createshstrtab;
  71. procedure createsymtab;
  72. procedure writesectionheader(s:TElfObjSection);
  73. procedure writesectiondata(s:TElfObjSection);
  74. procedure write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  75. procedure section_write_symbol(p:TObject;arg:pointer);
  76. procedure section_write_sh_string(p:TObject;arg:pointer);
  77. procedure section_count_sections(p:TObject;arg:pointer);
  78. procedure section_create_relocsec(p:TObject;arg:pointer);
  79. procedure section_set_datapos(p:TObject;arg:pointer);
  80. procedure section_relocsec_set_datapos(p:TObject;arg:pointer);
  81. procedure section_write_data(p:TObject;arg:pointer);
  82. procedure section_write_sechdr(p:TObject;arg:pointer);
  83. procedure section_write_relocsec(p:TObject;arg:pointer);
  84. protected
  85. function writedata(data:TObjData):boolean;override;
  86. public
  87. constructor Create(smart:boolean);override;
  88. end;
  89. TElfAssembler = class(tinternalassembler)
  90. constructor create(smart:boolean);override;
  91. end;
  92. implementation
  93. uses
  94. strings,
  95. verbose,
  96. cutils,globals,fmodule;
  97. const
  98. symbolresize = 200*18;
  99. const
  100. R_386_32 = 1; { ordinary absolute relocation }
  101. R_386_PC32 = 2; { PC-relative relocation }
  102. R_386_GOT32 = 3; { an offset into GOT }
  103. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  104. R_386_GOTOFF = 9; { an offset from GOT base }
  105. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  106. R_SPARC_32 = 3;
  107. R_SPARC_WDISP30 = 7;
  108. R_SPARC_HI22 = 9;
  109. R_SPARC_LO10 = 12;
  110. { AMD64 relocations }
  111. R_X86_64_NONE = 0;
  112. { Direct 64 bit }
  113. R_X86_64_64 = 1;
  114. { PC relative 32 bit signed }
  115. R_X86_64_PC32 = 2;
  116. { 32 bit GOT entry }
  117. R_X86_64_GOT32 = 3;
  118. { 32 bit PLT address }
  119. R_X86_64_PLT32 = 4;
  120. { Copy symbol at runtime }
  121. R_X86_64_COPY = 5;
  122. { Create GOT entry }
  123. R_X86_64_GLOB_DAT = 6;
  124. { Create PLT entry }
  125. R_X86_64_JUMP_SLOT = 7;
  126. { Adjust by program base }
  127. R_X86_64_RELATIVE = 8;
  128. { 32 bit signed PC relative offset to GOT }
  129. R_X86_64_GOTPCREL = 9;
  130. { Direct 32 bit zero extended }
  131. R_X86_64_32 = 10;
  132. { Direct 32 bit sign extended }
  133. R_X86_64_32S = 11;
  134. { Direct 16 bit zero extended }
  135. R_X86_64_16 = 12;
  136. { 16 bit sign extended pc relative }
  137. R_X86_64_PC16 = 13;
  138. { Direct 8 bit sign extended }
  139. R_X86_64_8 = 14;
  140. { 8 bit sign extended pc relative }
  141. R_X86_64_PC8 = 15;
  142. { ID of module containing symbol }
  143. R_X86_64_DTPMOD64 = 16;
  144. { Offset in module's TLS block }
  145. R_X86_64_DTPOFF64 = 17;
  146. { Offset in initial TLS block }
  147. R_X86_64_TPOFF64 = 18;
  148. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  149. R_X86_64_TLSGD = 19;
  150. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  151. R_X86_64_TLSLD = 20;
  152. { Offset in TLS block }
  153. R_X86_64_DTPOFF32 = 21;
  154. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  155. R_X86_64_GOTTPOFF = 22;
  156. { Offset in initial TLS block }
  157. R_X86_64_TPOFF32 = 23;
  158. R_X86_64_NUM = 24;
  159. SHN_UNDEF = 0;
  160. SHN_ABS = $fff1;
  161. SHN_COMMON = $fff2;
  162. SHT_NULL = 0;
  163. SHT_PROGBITS = 1;
  164. SHT_SYMTAB = 2;
  165. SHT_STRTAB = 3;
  166. SHT_RELA = 4;
  167. SHT_HASH = 5;
  168. SHT_DYNAMIC = 6;
  169. SHT_NOTE = 7;
  170. SHT_NOBITS = 8;
  171. SHT_REL = 9;
  172. SHT_SHLIB = 10;
  173. SHT_DYNSYM = 11;
  174. SHF_WRITE = 1;
  175. SHF_ALLOC = 2;
  176. SHF_EXECINSTR = 4;
  177. STB_LOCAL = 0;
  178. STB_GLOBAL = 1;
  179. STB_WEAK = 2;
  180. STT_NOTYPE = 0;
  181. STT_OBJECT = 1;
  182. STT_FUNC = 2;
  183. STT_SECTION = 3;
  184. STT_FILE = 4;
  185. type
  186. { Structures which are written directly to the output file }
  187. TElf32header=packed record
  188. magic0123 : longint;
  189. file_class : byte;
  190. data_encoding : byte;
  191. file_version : byte;
  192. padding : array[$07..$0f] of byte;
  193. e_type : word;
  194. e_machine : word;
  195. e_version : longint;
  196. e_entry : longint; { entrypoint }
  197. e_phoff : longint; { program header offset }
  198. e_shoff : longint; { sections header offset }
  199. e_flags : longint;
  200. e_ehsize : word; { elf header size in bytes }
  201. e_phentsize : word; { size of an entry in the program header array }
  202. e_phnum : word; { 0..e_phnum-1 of entrys }
  203. e_shentsize : word; { size of an entry in sections header array }
  204. e_shnum : word; { 0..e_shnum-1 of entrys }
  205. e_shstrndx : word; { index of string section header }
  206. end;
  207. TElf32sechdr=packed record
  208. sh_name : longint;
  209. sh_type : longint;
  210. sh_flags : longint;
  211. sh_addr : longint;
  212. sh_offset : longint;
  213. sh_size : longint;
  214. sh_link : longint;
  215. sh_info : longint;
  216. sh_addralign : longint;
  217. sh_entsize : longint;
  218. end;
  219. TElf32reloc=packed record
  220. address : longint;
  221. info : longint; { bit 0-7: type, 8-31: symbol }
  222. end;
  223. TElf32symbol=packed record
  224. st_name : longint;
  225. st_value : longint;
  226. st_size : longint;
  227. st_info : byte; { bit 0-3: type, 4-7: bind }
  228. st_other : byte;
  229. st_shndx : word;
  230. end;
  231. telf64header=packed record
  232. magic0123 : longint;
  233. file_class : byte;
  234. data_encoding : byte;
  235. file_version : byte;
  236. padding : array[$07..$0f] of byte;
  237. e_type : word;
  238. e_machine : word;
  239. e_version : longint;
  240. e_entry : qword; { entrypoint }
  241. e_phoff : qword; { program header offset }
  242. e_shoff : qword; { sections header offset }
  243. e_flags : longint;
  244. e_ehsize : word; { elf header size in bytes }
  245. e_phentsize : word; { size of an entry in the program header array }
  246. e_phnum : word; { 0..e_phnum-1 of entrys }
  247. e_shentsize : word; { size of an entry in sections header array }
  248. e_shnum : word; { 0..e_shnum-1 of entrys }
  249. e_shstrndx : word; { index of string section header }
  250. end;
  251. telf64sechdr=packed record
  252. sh_name : longint;
  253. sh_type : longint;
  254. sh_flags : qword;
  255. sh_addr : qword;
  256. sh_offset : qword;
  257. sh_size : qword;
  258. sh_link : longint;
  259. sh_info : longint;
  260. sh_addralign : qword;
  261. sh_entsize : qword;
  262. end;
  263. telf64reloc=packed record
  264. address : qword;
  265. info : qword; { bit 0-7: type, 8-31: symbol }
  266. addend : qword;
  267. end;
  268. telf64symbol=packed record
  269. st_name : longint;
  270. st_info : byte; { bit 0-3: type, 4-7: bind }
  271. st_other : byte;
  272. st_shndx : word;
  273. st_value : qword;
  274. st_size : qword;
  275. end;
  276. telf64stab=packed record
  277. strpos : longint;
  278. ntype : byte;
  279. nother : byte;
  280. ndesc : word;
  281. nvalue : longint;
  282. end;
  283. {$ifdef cpu64bit}
  284. telfheader = telf64header;
  285. telfreloc = telf64reloc;
  286. telfsymbol = telf64symbol;
  287. telfsechdr = telf64sechdr;
  288. {$else cpu64bit}
  289. telfheader = telf32header;
  290. telfreloc = telf32reloc;
  291. telfsymbol = telf32symbol;
  292. telfsechdr = telf32sechdr;
  293. {$endif cpu64bit}
  294. function MayBeSwapHeader(h : telf32header) : telf32header;
  295. begin
  296. result:=h;
  297. if source_info.endian<>target_info.endian then
  298. with h do
  299. begin
  300. result.e_type:=swapword(e_type);
  301. result.e_machine:=swapword(e_machine);
  302. result.e_version:=swaplong(e_version);
  303. result.e_entry:=swaplong(e_entry);
  304. result.e_phoff:=swaplong(e_phoff);
  305. result.e_shoff:=swaplong(e_shoff);
  306. result.e_flags:=swaplong(e_flags);
  307. result.e_ehsize:=swapword(e_ehsize);
  308. result.e_phentsize:=swapword(e_phentsize);
  309. result.e_phnum:=swapword(e_phnum);
  310. result.e_shentsize:=swapword(e_shentsize);
  311. result.e_shnum:=swapword(e_shnum);
  312. result.e_shstrndx:=swapword(e_shstrndx);
  313. end;
  314. end;
  315. function MayBeSwapHeader(h : telf64header) : telf64header;
  316. begin
  317. result:=h;
  318. if source_info.endian<>target_info.endian then
  319. with h do
  320. begin
  321. result.e_type:=swapword(e_type);
  322. result.e_machine:=swapword(e_machine);
  323. result.e_version:=swaplong(e_version);
  324. result.e_entry:=swapqword(e_entry);
  325. result.e_phoff:=swapqword(e_phoff);
  326. result.e_shoff:=swapqword(e_shoff);
  327. result.e_flags:=swaplong(e_flags);
  328. result.e_ehsize:=swapword(e_ehsize);
  329. result.e_phentsize:=swapword(e_phentsize);
  330. result.e_phnum:=swapword(e_phnum);
  331. result.e_shentsize:=swapword(e_shentsize);
  332. result.e_shnum:=swapword(e_shnum);
  333. result.e_shstrndx:=swapword(e_shstrndx);
  334. end;
  335. end;
  336. function MaybeSwapSecHeader(h : telf32sechdr) : telf32sechdr;
  337. begin
  338. result:=h;
  339. if source_info.endian<>target_info.endian then
  340. with h do
  341. begin
  342. result.sh_name:=SwapLong(sh_name);
  343. result.sh_type:=SwapLong(sh_type);
  344. result.sh_flags:=SwapLong(sh_flags);
  345. result.sh_addr:=SwapLong(sh_addr);
  346. result.sh_offset:=SwapLong(sh_offset);
  347. result.sh_size:=SwapLong(sh_size);
  348. result.sh_link:=SwapLong(sh_link);
  349. result.sh_info:=SwapLong(sh_info);
  350. result.sh_addralign:=SwapLong(sh_addralign);
  351. result.sh_entsize:=SwapLong(sh_entsize);
  352. end;
  353. end;
  354. function MaybeSwapSecHeader(h : telf64sechdr) : telf64sechdr;
  355. begin
  356. result:=h;
  357. if source_info.endian<>target_info.endian then
  358. with h do
  359. begin
  360. result.sh_name:=SwapLong(sh_name);
  361. result.sh_type:=SwapLong(sh_type);
  362. result.sh_flags:=SwapQWord(sh_flags);
  363. result.sh_addr:=SwapQWord(sh_addr);
  364. result.sh_offset:=SwapQWord(sh_offset);
  365. result.sh_size:=SwapQWord(sh_size);
  366. result.sh_link:=SwapLong(sh_link);
  367. result.sh_info:=SwapLong(sh_info);
  368. result.sh_addralign:=SwapQWord(sh_addralign);
  369. result.sh_entsize:=SwapQWord(sh_entsize);
  370. end;
  371. end;
  372. function MaybeSwapElfSymbol(h : telf32symbol) : telf32symbol;
  373. begin
  374. result:=h;
  375. if source_info.endian<>target_info.endian then
  376. with h do
  377. begin
  378. result.st_name:=SwapLong(st_name);
  379. result.st_value:=SwapLong(st_value);
  380. result.st_size:=SwapLong(st_size);
  381. result.st_shndx:=SwapWord(st_shndx);
  382. end;
  383. end;
  384. function MaybeSwapElfSymbol(h : telf64symbol) : telf64symbol;
  385. begin
  386. result:=h;
  387. if source_info.endian<>target_info.endian then
  388. with h do
  389. begin
  390. result.st_name:=SwapLong(st_name);
  391. result.st_value:=SwapQWord(st_value);
  392. result.st_size:=SwapQWord(st_size);
  393. result.st_shndx:=SwapWord(st_shndx);
  394. end;
  395. end;
  396. function MaybeSwapElfReloc(h : telf32reloc) : telf32reloc;
  397. begin
  398. result:=h;
  399. if source_info.endian<>target_info.endian then
  400. with h do
  401. begin
  402. result.address:=SwapLong(address);
  403. result.info:=SwapLong(info);
  404. end;
  405. end;
  406. function MaybeSwapElfReloc(h : telf64reloc) : telf64reloc;
  407. begin
  408. result:=h;
  409. if source_info.endian<>target_info.endian then
  410. with h do
  411. begin
  412. result.address:=SwapQWord(address);
  413. result.info:=SwapQWord(info);
  414. end;
  415. end;
  416. {****************************************************************************
  417. Helpers
  418. ****************************************************************************}
  419. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  420. begin
  421. { Section Type }
  422. AshType:=SHT_PROGBITS;
  423. if oso_strings in aoptions then
  424. AshType:=SHT_STRTAB
  425. else if not(oso_data in aoptions) then
  426. AshType:=SHT_NOBITS;
  427. { Section Flags }
  428. Ashflags:=0;
  429. if oso_load in aoptions then
  430. Ashflags:=Ashflags or SHF_ALLOC;
  431. if oso_executable in aoptions then
  432. Ashflags:=Ashflags or SHF_EXECINSTR;
  433. if oso_write in aoptions then
  434. Ashflags:=Ashflags or SHF_WRITE;
  435. end;
  436. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  437. begin
  438. aoptions:=[];
  439. { Section Type }
  440. if AshType<>SHT_NOBITS then
  441. include(aoptions,oso_data);
  442. if AshType=SHT_STRTAB then
  443. include(aoptions,oso_strings);
  444. { Section Flags }
  445. if Ashflags and SHF_ALLOC<>0 then
  446. include(aoptions,oso_load)
  447. else
  448. include(aoptions,oso_noload);
  449. if Ashflags and SHF_WRITE<>0 then
  450. include(aoptions,oso_write)
  451. else
  452. include(aoptions,oso_readonly);
  453. end;
  454. {****************************************************************************
  455. TSection
  456. ****************************************************************************}
  457. constructor TElfObjSection.create(const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  458. begin
  459. inherited create(Aname,Aalign,aoptions);
  460. secshidx:=0;
  461. shstridx:=0;
  462. encodesechdrflags(aoptions,shtype,shflags);
  463. shlink:=0;
  464. shinfo:=0;
  465. if name='.stab' then
  466. shentsize:=sizeof(TObjStabEntry);
  467. relocsect:=nil;
  468. end;
  469. constructor TElfObjSection.create_ext(const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  470. var
  471. aoptions : TObjSectionOptions;
  472. begin
  473. decodesechdrflags(Ashtype,Ashflags,aoptions);
  474. inherited create(Aname,Aalign,aoptions);
  475. secshidx:=0;
  476. shstridx:=0;
  477. shtype:=AshType;
  478. shflags:=AshFlags;
  479. shlink:=Ashlink;
  480. shinfo:=Ashinfo;
  481. shentsize:=Aentsize;
  482. relocsect:=nil;
  483. end;
  484. destructor TElfObjSection.destroy;
  485. begin
  486. if assigned(relocsect) then
  487. relocsect.free;
  488. inherited destroy;
  489. end;
  490. {****************************************************************************
  491. TElfObjData
  492. ****************************************************************************}
  493. constructor TElfObjData.create(const n:string);
  494. begin
  495. inherited create(n);
  496. CObjSection:=TElfObjSection;
  497. { reset }
  498. Syms:=TDynamicArray.Create(symbolresize);
  499. { default sections }
  500. symtabsect:=TElfObjSection.create_ext('.symtab',2,0,0,0,4,sizeof(telfsymbol));
  501. strtabsect:=TElfObjSection.create_ext('.strtab',3,0,0,0,1,0);
  502. shstrtabsect:=TElfObjSection.create_ext('.shstrtab',3,0,0,0,1,0);
  503. { insert the empty and filename as first in strtab }
  504. strtabsect.writestr(#0);
  505. strtabsect.writestr(SplitFileName(current_module.mainsource^)+#0);
  506. { we need at least the following sections }
  507. createsection(sec_code,'');
  508. createsection(sec_data,'');
  509. createsection(sec_bss,'');
  510. if tf_section_threadvars in target_info.flags then
  511. createsection(sec_threadvar,'');
  512. if (tf_needs_dwarf_cfi in target_info.flags) and
  513. (af_supports_dwarf in target_asm.flags) then
  514. createsection(sec_debug_frame,'');
  515. end;
  516. destructor TElfObjData.destroy;
  517. begin
  518. Syms.Free;
  519. symtabsect.free;
  520. strtabsect.free;
  521. shstrtabsect.free;
  522. inherited destroy;
  523. end;
  524. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string):string;
  525. const
  526. secnames : array[TAsmSectiontype] of string[13] = ('',
  527. {$ifdef userodata}
  528. '.text','.data','.rodata','.bss','.threadvar',
  529. {$else userodata}
  530. '.text','.data','.data','.bss','.threadvar',
  531. {$endif userodata}
  532. '.text', { darwin stubs }
  533. '.stab','.stabstr',
  534. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  535. '.eh_frame',
  536. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  537. 'fpc',
  538. ''
  539. );
  540. begin
  541. if (use_smartlink_section and
  542. (aname<>'')) or (atype=sec_fpc) then
  543. result:=secnames[atype]+'.'+aname
  544. else
  545. result:=secnames[atype];
  546. end;
  547. function TElfObjData.sectiontype2align(atype:TAsmSectiontype):shortint;
  548. begin
  549. if atype=sec_stabstr then
  550. result:=1
  551. else
  552. result:=sizeof(aint);
  553. end;
  554. procedure TElfObjData.CreateDebugSections;
  555. begin
  556. stabssec:=createsection(sec_stab,'');
  557. stabstrsec:=createsection(sec_stabstr,'');
  558. end;
  559. procedure TElfObjData.writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);
  560. var
  561. symaddr : longint;
  562. begin
  563. if CurrObjSec=nil then
  564. internalerror(200403292);
  565. {$ifdef userodata}
  566. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  567. internalerror(200408252);
  568. {$endif userodata}
  569. if assigned(p) then
  570. begin
  571. { real address of the symbol }
  572. symaddr:=p.address;
  573. { Local ObjSymbols can be resolved already or need a section reloc }
  574. if (p.bind=AB_LOCAL) then
  575. begin
  576. { For a relative relocation in the same section the
  577. value can be calculated }
  578. if (p.objsection=CurrObjSec) and
  579. (relative=RELOC_RELATIVE) then
  580. inc(data,symaddr-len-CurrObjSec.Size)
  581. else
  582. begin
  583. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,relative);
  584. inc(data,symaddr);
  585. end;
  586. end
  587. else
  588. begin
  589. CurrObjSec.addsymreloc(CurrObjSec.Size,p,relative);
  590. {$ifndef x86_64}
  591. if relative=RELOC_RELATIVE then
  592. dec(data,len);
  593. {$endif x86_64}
  594. end;
  595. end;
  596. CurrObjSec.write(data,len);
  597. end;
  598. procedure TElfObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  599. var
  600. stab : TObjStabEntry;
  601. begin
  602. if not assigned(StabsSec) then
  603. internalerror(200602271);
  604. fillchar(stab,sizeof(TObjStabEntry),0);
  605. if assigned(p) and (p[0]<>#0) then
  606. begin
  607. stab.strpos:=stabstrsec.Size;
  608. stabstrsec.write(p^,strlen(p)+1);
  609. end;
  610. stab.ntype:=nidx;
  611. stab.ndesc:=ndesc;
  612. stab.nother:=nother;
  613. stab.nvalue:=offset;
  614. stabssec.write(stab,sizeof(stab));
  615. if assigned(ps) then
  616. stabssec.addsymreloc(stabssec.Size-4,ps,RELOC_ABSOLUTE);
  617. end;
  618. {****************************************************************************
  619. TElfObjectOutput
  620. ****************************************************************************}
  621. constructor TElfObjectOutput.create(smart:boolean);
  622. begin
  623. inherited Create(smart);
  624. CObjData:=TElfObjData;
  625. end;
  626. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection);
  627. var
  628. {$ifdef ver2_0_0}
  629. relnative,
  630. {$endif ver2_0_0}
  631. rel : telfreloc;
  632. r : TObjRelocation;
  633. relsym,reltyp : longint;
  634. begin
  635. with elf32data do
  636. begin
  637. {$ifdef userodata}
  638. { rodata can't have relocations }
  639. if s.sectype=sec_rodata then
  640. begin
  641. if assigned(s.relocations.first) then
  642. internalerror(200408251);
  643. exit;
  644. end;
  645. {$endif userodata}
  646. { create the reloc section }
  647. {$ifdef i386}
  648. s.relocsect:=TElfObjSection.create_ext('.rel'+s.name,9,0,symtabsect.secshidx,s.secshidx,4,8);
  649. {$endif i386}
  650. {$ifdef x86_64}
  651. s.relocsect:=TElfObjSection.create_ext('.rela'+s.name,4,0,symtabsect.secshidx,s.secshidx,4,3*8);
  652. {$endif x86_64}
  653. {$ifdef sparc}
  654. s.relocsect:=TElfObjSection.create_ext('.rel'+s.name,4,0,symtabsect.secshidx,s.secshidx,4,8);
  655. {$endif sparc}
  656. { add the relocations }
  657. r:=TObjRelocation(s.relocations.first);
  658. while assigned(r) do
  659. begin
  660. rel.address:=r.dataoffset;
  661. if assigned(r.symbol) then
  662. begin
  663. if (r.symbol.bind=AB_LOCAL) then
  664. relsym:=r.symbol.objsection.secsymidx
  665. else
  666. begin
  667. if r.symbol.symidx=-1 then
  668. internalerror(200603012);
  669. relsym:=r.symbol.symidx;
  670. end;
  671. end
  672. else
  673. if r.objsection<>nil then
  674. relsym:=r.objsection.secsymidx
  675. else
  676. relsym:=SHN_UNDEF;
  677. { when things settle down, we can create processor specific
  678. derived classes
  679. }
  680. {$ifdef i386}
  681. case r.typ of
  682. RELOC_RELATIVE :
  683. reltyp:=R_386_PC32;
  684. RELOC_ABSOLUTE :
  685. reltyp:=R_386_32;
  686. end;
  687. {$endif i386}
  688. {$ifdef sparc}
  689. case r.typ of
  690. { RELOC_RELATIVE :
  691. reltyp:=R_386_PC32;
  692. }
  693. RELOC_ABSOLUTE :
  694. reltyp:=R_SPARC_32;
  695. else
  696. internalerror(200410201);
  697. end;
  698. {$endif sparc}
  699. {$ifdef x86_64}
  700. case r.typ of
  701. RELOC_RELATIVE :
  702. begin
  703. reltyp:=R_X86_64_PC32;
  704. { length of the relocated location is handled here }
  705. rel.addend:=-4;
  706. end;
  707. RELOC_ABSOLUTE :
  708. reltyp:=R_X86_64_64;
  709. else
  710. internalerror(200602261);
  711. end;
  712. {$endif x86_64}
  713. {$ifdef cpu64bit}
  714. rel.info:=(qword(relsym) shl 32) or reltyp;
  715. {$else cpu64bit}
  716. rel.info:=(relsym shl 8) or reltyp;
  717. {$endif cpu64bit}
  718. { write reloc }
  719. {$ifdef ver2_0_0}
  720. relnative:=MaybeSwapElfReloc(rel);
  721. s.relocsect.write(relnative,sizeof(rel));
  722. {$else}
  723. s.relocsect.write(MaybeSwapElfReloc(rel),sizeof(rel));
  724. {$endif ver2_0_0}
  725. r:=TObjRelocation(r.next);
  726. end;
  727. end;
  728. end;
  729. procedure TElfObjectOutput.write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  730. var
  731. {$ifdef ver2_0_0}
  732. elfsymnative,
  733. {$endif ver2_0_0}
  734. elfsym : telfsymbol;
  735. begin
  736. fillchar(elfsym,sizeof(elfsym),0);
  737. elfsym.st_name:=astridx;
  738. elfsym.st_info:=ainfo;
  739. elfsym.st_shndx:=ashndx;
  740. inc(symidx);
  741. inc(localsyms);
  742. {$ifdef ver2_0_0}
  743. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  744. elf32data.symtabsect.write(elfsymnative,sizeof(elfsym));
  745. {$else}
  746. elf32data.symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  747. {$endif ver2_0_0}
  748. end;
  749. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  750. begin
  751. TObjSection(p).secsymidx:=symidx;
  752. write_internal_symbol(TElfObjSection(p).shstridx,STT_SECTION,TElfObjSection(p).secshidx);
  753. end;
  754. procedure TElfObjectOutput.createsymtab;
  755. var
  756. {$ifdef ver2_0_0}
  757. elfsymnative,
  758. {$endif}
  759. elfsym : telfsymbol;
  760. i : longint;
  761. objsym : TObjSymbol;
  762. begin
  763. with elf32data do
  764. begin
  765. symidx:=0;
  766. localsyms:=0;
  767. { empty entry }
  768. write_internal_symbol(0,0,0);
  769. { filename entry }
  770. write_internal_symbol(1,STT_FILE,SHN_ABS);
  771. { section }
  772. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  773. { ObjSymbols }
  774. for i:=0 to ObjSymbolList.Count-1 do
  775. begin
  776. objsym:=TObjSymbol(ObjSymbolList[i]);
  777. if objsym.bind<>AB_LOCAL then
  778. begin
  779. fillchar(elfsym,sizeof(elfsym),0);
  780. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  781. elfsym.st_name:=strtabsect.Size;
  782. strtabsect.writestr(objsym.name);
  783. strtabsect.writestr(#0);
  784. elfsym.st_size:=objsym.size;
  785. case objsym.bind of
  786. AB_LOCAL :
  787. begin
  788. elfsym.st_value:=objsym.address;
  789. elfsym.st_info:=STB_LOCAL shl 4;
  790. inc(localsyms);
  791. end;
  792. AB_COMMON :
  793. begin
  794. elfsym.st_value:=$10;
  795. elfsym.st_info:=STB_GLOBAL shl 4;
  796. end;
  797. AB_EXTERNAL :
  798. elfsym.st_info:=STB_GLOBAL shl 4;
  799. AB_GLOBAL :
  800. begin
  801. elfsym.st_value:=objsym.address;
  802. elfsym.st_info:=STB_GLOBAL shl 4;
  803. end;
  804. end;
  805. if (objsym.bind<>AB_EXTERNAL) and
  806. not(assigned(objsym.objsection) and
  807. not(oso_data in objsym.objsection.secoptions)) then
  808. begin
  809. case objsym.typ of
  810. AT_FUNCTION :
  811. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  812. AT_DATA :
  813. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  814. end;
  815. end;
  816. if objsym.bind=AB_COMMON then
  817. elfsym.st_shndx:=SHN_COMMON
  818. else
  819. begin
  820. if assigned(objsym.objsection) then
  821. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  822. else
  823. elfsym.st_shndx:=SHN_UNDEF;
  824. end;
  825. objsym.symidx:=symidx;
  826. inc(symidx);
  827. {$ifdef ver2_0_0}
  828. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  829. symtabsect.write(elfsymnative,sizeof(elfsym));
  830. {$else}
  831. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  832. {$endif ver2_0_0}
  833. end;
  834. end;
  835. { update the .symtab section header }
  836. symtabsect.shlink:=strtabsect.secshidx;
  837. symtabsect.shinfo:=localsyms;
  838. end;
  839. end;
  840. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  841. begin
  842. TElfObjSection(p).shstridx:=elf32data.shstrtabsect.writestr(TObjSection(p).name+#0);
  843. if assigned(TElfObjSection(p).relocsect) then
  844. TElfObjSection(p).relocsect.shstridx:=elf32data.shstrtabsect.writestr(TElfObjSection(p).relocsect.name+#0);
  845. end;
  846. procedure TElfObjectOutput.createshstrtab;
  847. begin
  848. with elf32data do
  849. begin
  850. with shstrtabsect do
  851. begin
  852. writestr(#0);
  853. symtabsect.shstridx:=writestr('.symtab'#0);
  854. strtabsect.shstridx:=writestr('.strtab'#0);
  855. shstrtabsect.shstridx:=writestr('.shstrtab'#0);
  856. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  857. end;
  858. end;
  859. end;
  860. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  861. var
  862. {$ifdef ver2_0_0}
  863. sechdrnative,
  864. {$endif ver2_0_0}
  865. sechdr : telfsechdr;
  866. begin
  867. fillchar(sechdr,sizeof(sechdr),0);
  868. sechdr.sh_name:=s.shstridx;
  869. sechdr.sh_type:=s.shtype;
  870. sechdr.sh_flags:=s.shflags;
  871. sechdr.sh_offset:=s.datapos;
  872. sechdr.sh_size:=s.Size;
  873. sechdr.sh_link:=s.shlink;
  874. sechdr.sh_info:=s.shinfo;
  875. sechdr.sh_addralign:=s.secalign;
  876. sechdr.sh_entsize:=s.shentsize;
  877. {$ifdef ver2_0_0}
  878. sechdrnative:=MaybeSwapSecHeader(sechdr);
  879. writer.write(sechdrnative,sizeof(sechdr));
  880. {$else}
  881. writer.write(MaybeSwapSecHeader(sechdr),sizeof(sechdr));
  882. {$endif ver2_0_0}
  883. end;
  884. procedure TElfObjectOutput.writesectiondata(s:TElfObjSection);
  885. begin
  886. FWriter.writezeros(s.dataalignbytes);
  887. FWriter.writearray(s.data);
  888. end;
  889. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  890. begin
  891. TElfObjSection(p).secshidx:=pword(arg)^;
  892. inc(pword(arg)^);
  893. if TElfObjSection(p).relocations.count>0 then
  894. inc(pword(arg)^);
  895. end;
  896. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  897. begin
  898. if (TElfObjSection(p).relocations.count>0) then
  899. createrelocsection(TElfObjSection(p));
  900. end;
  901. procedure TElfObjectOutput.section_set_datapos(p:TObject;arg:pointer);
  902. begin
  903. TObjSection(p).setdatapos(paint(arg)^);
  904. end;
  905. procedure TElfObjectOutput.section_relocsec_set_datapos(p:TObject;arg:pointer);
  906. begin
  907. if assigned(TElfObjSection(p).relocsect) then
  908. TElfObjSection(p).relocsect.setdatapos(paint(arg)^);
  909. end;
  910. procedure TElfObjectOutput.section_write_data(p:TObject;arg:pointer);
  911. begin
  912. if (oso_data in TObjSection(p).secoptions) then
  913. begin
  914. if TObjSection(p).data=nil then
  915. internalerror(200403073);
  916. writesectiondata(TElfObjSection(p));
  917. end;
  918. end;
  919. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  920. begin
  921. writesectionheader(TElfObjSection(p));
  922. if assigned(TElfObjSection(p).relocsect) then
  923. writesectionheader(TElfObjSection(p).relocsect);
  924. end;
  925. procedure TElfObjectOutput.section_write_relocsec(p:TObject;arg:pointer);
  926. begin
  927. if assigned(TElfObjSection(p).relocsect) then
  928. writesectiondata(TElfObjSection(p).relocsect);
  929. end;
  930. function TElfObjectOutput.writedata(data:TObjData):boolean;
  931. var
  932. {$ifdef ver2_0_0}
  933. headernative,
  934. {$endif ver2_0_0}
  935. header : telfheader;
  936. shoffset,
  937. datapos : aint;
  938. nsections : word;
  939. begin
  940. result:=false;
  941. elf32data:=TElfObjData(data);
  942. with elf32data do
  943. begin
  944. { calc amount of sections we have }
  945. nsections:=1;
  946. { also create the index in the section header table }
  947. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  948. { add default sections }
  949. shstrtabsect.secshidx:=nsections;
  950. inc(nsections);
  951. symtabsect.secshidx:=nsections;
  952. inc(nsections);
  953. strtabsect.secshidx:=nsections;
  954. inc(nsections);
  955. { create .symtab and .strtab }
  956. createsymtab;
  957. { Create the relocation sections }
  958. ObjSectionList.ForEachCall(@section_create_relocsec,nil);
  959. { create .shstrtab }
  960. createshstrtab;
  961. { Calculate the filepositions }
  962. datapos:=$40; { elfheader + alignment }
  963. { sections first }
  964. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  965. { shstrtab }
  966. shstrtabsect.setdatapos(datapos);
  967. { section headers }
  968. shoffset:=datapos;
  969. inc(datapos,nsections*sizeof(telfsechdr));
  970. { symtab }
  971. symtabsect.setdatapos(datapos);
  972. { strtab }
  973. strtabsect.setdatapos(datapos);
  974. { .rel sections }
  975. ObjSectionList.ForEachCall(@section_relocsec_set_datapos,@datapos);
  976. { Write ELF Header }
  977. fillchar(header,sizeof(header),0);
  978. header.magic0123:=$464c457f; { = #127'ELF' }
  979. {$ifdef cpu64bit}
  980. header.file_class:=2;
  981. {$else cpu64bit}
  982. header.file_class:=1;
  983. {$endif cpu64bit}
  984. if target_info.endian=endian_big then
  985. header.data_encoding:=2
  986. else
  987. header.data_encoding:=1;
  988. header.file_version:=1;
  989. header.e_type:=1;
  990. {$ifdef sparc}
  991. header.e_machine:=2;
  992. {$endif sparc}
  993. {$ifdef i386}
  994. header.e_machine:=3;
  995. {$endif i386}
  996. {$ifdef m68k}
  997. header.e_machine:=4;
  998. {$endif m68k}
  999. {$ifdef powerpc}
  1000. header.e_machine:=20;
  1001. {$endif powerpc}
  1002. {$ifdef arm}
  1003. header.e_machine:=40;
  1004. {$endif arm}
  1005. {$ifdef x86_64}
  1006. header.e_machine:=62;
  1007. {$endif x86_64}
  1008. header.e_version:=1;
  1009. header.e_shoff:=shoffset;
  1010. header.e_shstrndx:=shstrtabsect.secshidx;
  1011. header.e_shnum:=nsections;
  1012. header.e_ehsize:=sizeof(telfheader);
  1013. header.e_shentsize:=sizeof(telfsechdr);
  1014. {$ifdef ver2_0_0}
  1015. headernative:=MaybeSwapHeader(header);
  1016. writer.write(headernative,sizeof(header));
  1017. {$else}
  1018. writer.write(MaybeSwapHeader(header),sizeof(header));
  1019. {$endif ver2_0_0}
  1020. writer.writezeros($40-sizeof(header)); { align }
  1021. { Sections }
  1022. ObjSectionList.ForEachCall(@section_write_data,nil);
  1023. { .shstrtab }
  1024. writesectiondata(shstrtabsect);
  1025. { section headers, start with an empty header for sh_undef }
  1026. writer.writezeros(sizeof(telfsechdr));
  1027. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1028. writesectionheader(shstrtabsect);
  1029. writesectionheader(symtabsect);
  1030. writesectionheader(strtabsect);
  1031. { .symtab }
  1032. writesectiondata(symtabsect);
  1033. { .strtab }
  1034. writesectiondata(strtabsect);
  1035. { .rel sections }
  1036. ObjSectionList.ForEachCall(@section_write_relocsec,nil);
  1037. end;
  1038. result:=true;
  1039. end;
  1040. {****************************************************************************
  1041. TELFAssembler
  1042. ****************************************************************************}
  1043. constructor TElfAssembler.Create(smart:boolean);
  1044. begin
  1045. inherited Create(smart);
  1046. CObjOutput:=TElfObjectOutput;
  1047. end;
  1048. {*****************************************************************************
  1049. Initialize
  1050. *****************************************************************************}
  1051. {$ifdef i386}
  1052. const
  1053. as_i386_elf32_info : tasminfo =
  1054. (
  1055. id : as_i386_elf32;
  1056. idtxt : 'ELF';
  1057. asmbin : '';
  1058. asmcmd : '';
  1059. supported_target : system_any; //target_i386_linux;
  1060. flags : [af_outputbinary,af_smartlink_sections];
  1061. labelprefix : '.L';
  1062. comment : '';
  1063. );
  1064. {$endif i386}
  1065. {$ifdef x86_64}
  1066. const
  1067. as_x86_64_elf64_info : tasminfo =
  1068. (
  1069. id : as_x86_64_elf64;
  1070. idtxt : 'ELF';
  1071. asmbin : '';
  1072. asmcmd : '';
  1073. supported_target : system_any; //target_i386_linux;
  1074. flags : [af_outputbinary,af_smartlink_sections];
  1075. labelprefix : '.L';
  1076. comment : '';
  1077. );
  1078. {$endif x86_64}
  1079. {$ifdef sparc}
  1080. const
  1081. as_sparc_elf32_info : tasminfo =
  1082. (
  1083. id : as_sparc_elf32;
  1084. idtxt : 'ELF';
  1085. asmbin : '';
  1086. asmcmd : '';
  1087. supported_target : system_any; //target_i386_linux;
  1088. // flags : [af_outputbinary,af_smartlink_sections];
  1089. flags : [af_outputbinary];
  1090. labelprefix : '.L';
  1091. comment : '';
  1092. );
  1093. {$endif sparc}
  1094. initialization
  1095. {$ifdef i386}
  1096. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1097. {$endif i386}
  1098. {$ifdef sparc}
  1099. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1100. {$endif sparc}
  1101. {$ifdef x86_64}
  1102. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1103. {$endif x86_64}
  1104. end.