ogelf.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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,aasmdata,assemble,
  27. { output }
  28. ogbase,
  29. owbase;
  30. type
  31. TElfObjSection = class(TObjSection)
  32. public
  33. secshidx : longint; { index for the section in symtab }
  34. shstridx,
  35. shtype,
  36. shflags,
  37. shlink,
  38. shinfo,
  39. shentsize : longint;
  40. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  41. constructor create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  42. end;
  43. TElfSymtabKind = (esk_obj,esk_exe,esk_dyn);
  44. TElfSymtab = class(TElfObjSection)
  45. public
  46. kind: TElfSymtabKind;
  47. fstrsec: TObjSection;
  48. symidx: longint;
  49. constructor create(aObjData:TObjData;aKind:TElfSymtabKind);reintroduce;
  50. procedure writeSymbol(objsym:TObjSymbol);
  51. procedure writeInternalSymbol(astridx:longint;ainfo:byte;ashndx:word);
  52. end;
  53. TElfObjData = class(TObjData)
  54. public
  55. constructor create(const n:string);override;
  56. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  57. procedure CreateDebugSections;override;
  58. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);override;
  59. end;
  60. TElfObjectOutput = class(tObjOutput)
  61. private
  62. symtabsect: TElfSymtab;
  63. shstrtabsect: TElfObjSection;
  64. procedure createrelocsection(s:TElfObjSection;data:TObjData);
  65. procedure createshstrtab(data:TObjData);
  66. procedure createsymtab(data: TObjData);
  67. procedure writesectionheader(s:TElfObjSection);
  68. procedure section_write_symbol(p:TObject;arg:pointer);
  69. procedure section_write_sh_string(p:TObject;arg:pointer);
  70. procedure section_count_sections(p:TObject;arg:pointer);
  71. procedure section_create_relocsec(p:TObject;arg:pointer);
  72. procedure section_write_sechdr(p:TObject;arg:pointer);
  73. protected
  74. function writedata(data:TObjData):boolean;override;
  75. public
  76. constructor Create(AWriter:TObjectWriter);override;
  77. end;
  78. TElfAssembler = class(tinternalassembler)
  79. constructor create(smart:boolean);override;
  80. end;
  81. implementation
  82. uses
  83. SysUtils,
  84. verbose,
  85. cutils,globals,fmodule;
  86. const
  87. symbolresize = 200*18;
  88. const
  89. { Relocation types }
  90. {$ifdef i386}
  91. R_386_32 = 1; { ordinary absolute relocation }
  92. R_386_PC32 = 2; { PC-relative relocation }
  93. R_386_GOT32 = 3; { an offset into GOT }
  94. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  95. R_386_GOTOFF = 9; { an offset from GOT base }
  96. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  97. R_386_GNU_VTINHERIT = 250;
  98. R_386_GNU_VTENTRY = 251;
  99. {$endif i386}
  100. {$ifdef sparc}
  101. R_SPARC_32 = 3;
  102. R_SPARC_WDISP30 = 7;
  103. R_SPARC_HI22 = 9;
  104. R_SPARC_LO10 = 12;
  105. R_SPARC_GNU_VTINHERIT = 250;
  106. R_SPARC_GNU_VTENTRY = 251;
  107. {$endif sparc}
  108. {$ifdef x86_64}
  109. R_X86_64_NONE = 0;
  110. R_X86_64_64 = 1; { Direct 64 bit }
  111. R_X86_64_PC32 = 2; { PC relative 32 bit signed }
  112. R_X86_64_GOT32 = 3; { 32 bit GOT entry }
  113. R_X86_64_PLT32 = 4; { 32 bit PLT address }
  114. R_X86_64_COPY = 5; { Copy symbol at runtime }
  115. R_X86_64_GLOB_DAT = 6; { Create GOT entry }
  116. R_X86_64_JUMP_SLOT = 7; { Create PLT entry }
  117. R_X86_64_RELATIVE = 8; { Adjust by program base }
  118. R_X86_64_GOTPCREL = 9; { 32 bit signed PC relative offset to GOT }
  119. R_X86_64_32 = 10; { Direct 32 bit zero extended }
  120. R_X86_64_32S = 11; { Direct 32 bit sign extended }
  121. R_X86_64_16 = 12; { Direct 16 bit zero extended }
  122. R_X86_64_PC16 = 13; { 16 bit sign extended PC relative }
  123. R_X86_64_8 = 14; { Direct 8 bit sign extended }
  124. R_X86_64_PC8 = 15; { 8 bit sign extended PC relative }
  125. R_X86_64_DTPMOD64 = 16; { ID of module containing symbol }
  126. R_X86_64_DTPOFF64 = 17; { Offset in module's TLS block }
  127. R_X86_64_TPOFF64 = 18; { Offset in initial TLS block }
  128. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  129. R_X86_64_TLSGD = 19;
  130. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  131. R_X86_64_TLSLD = 20;
  132. R_X86_64_DTPOFF32 = 21; { Offset in TLS block }
  133. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  134. R_X86_64_GOTTPOFF = 22;
  135. R_X86_64_TPOFF32 = 23; { Offset in initial TLS block }
  136. R_X86_64_GNU_VTINHERIT = 24; { GNU extension to record C++ vtable hierarchy }
  137. R_X86_64_GNU_VTENTRY = 25; { GNU extension to record C++ vtable member usage }
  138. {$endif x86_64}
  139. { ELFHeader.file_class }
  140. ELFCLASSNONE = 0;
  141. ELFCLASS32 = 1;
  142. ELFCLASS64 = 2;
  143. { ELFHeader.e_type }
  144. ET_NONE = 0;
  145. ET_REL = 1;
  146. ET_EXEC = 2;
  147. ET_DYN = 3;
  148. ET_CORE = 4;
  149. { ELFHeader.e_machine }
  150. EM_SPARC = 2;
  151. EM_386 = 3;
  152. EM_M68K = 4;
  153. EM_PPC = 20;
  154. EM_ARM = 40;
  155. EM_X86_64 = 62;
  156. {$ifdef sparc}
  157. ELFMACHINE = EM_SPARC;
  158. {$endif sparc}
  159. {$ifdef i386}
  160. ELFMACHINE = EM_386;
  161. {$endif i386}
  162. {$ifdef m68k}
  163. ELFMACHINE = EM_M68K;
  164. {$endif m68k}
  165. {$ifdef powerpc}
  166. ELFMACHINE = EM_PPC;
  167. {$endif powerpc}
  168. {$ifdef arm}
  169. ELFMACHINE = EM_ARM;
  170. {$endif arm}
  171. {$ifdef x86_64}
  172. ELFMACHINE = EM_X86_64;
  173. {$endif x86_64}
  174. SHN_UNDEF = 0;
  175. SHN_ABS = $fff1;
  176. SHN_COMMON = $fff2;
  177. SHT_NULL = 0;
  178. SHT_PROGBITS = 1;
  179. SHT_SYMTAB = 2;
  180. SHT_STRTAB = 3;
  181. SHT_RELA = 4;
  182. SHT_HASH = 5;
  183. SHT_DYNAMIC = 6;
  184. SHT_NOTE = 7;
  185. SHT_NOBITS = 8;
  186. SHT_REL = 9;
  187. SHT_SHLIB = 10;
  188. SHT_DYNSYM = 11;
  189. SHF_WRITE = 1;
  190. SHF_ALLOC = 2;
  191. SHF_EXECINSTR = 4;
  192. STB_LOCAL = 0;
  193. STB_GLOBAL = 1;
  194. STB_WEAK = 2;
  195. STT_NOTYPE = 0;
  196. STT_OBJECT = 1;
  197. STT_FUNC = 2;
  198. STT_SECTION = 3;
  199. STT_FILE = 4;
  200. { program header types }
  201. PT_NULL = 0;
  202. PT_LOAD = 1;
  203. PT_DYNAMIC = 2;
  204. PT_INTERP = 3;
  205. PT_NOTE = 4;
  206. PT_SHLIB = 5;
  207. PT_PHDR = 6;
  208. PT_LOPROC = $70000000;
  209. PT_HIPROC = $7FFFFFFF;
  210. { program header flags }
  211. PF_X = 1;
  212. PF_W = 2;
  213. PF_R = 4;
  214. PF_MASKPROC = $F0000000;
  215. { .dynamic tags }
  216. DT_NULL = 0;
  217. DT_NEEDED = 1;
  218. DT_PLTRELSZ = 2;
  219. DT_PLTGOT = 3;
  220. DT_HASH = 4;
  221. DT_STRTAB = 5;
  222. DT_SYMTAB = 6;
  223. DT_RELA = 7;
  224. DT_RELASZ = 8;
  225. DT_RELAENT = 9;
  226. DT_STRSZ = 10;
  227. DT_SYMENT = 11;
  228. DT_INIT = 12;
  229. DT_FINI = 13;
  230. DT_SONAME = 14;
  231. DT_RPATH = 15;
  232. DT_SYMBOLIC = 16;
  233. DT_REL = 17;
  234. DT_RELSZ = 18;
  235. DT_RELENT = 19;
  236. DT_PLTREL = 20;
  237. DT_DEBUG = 21;
  238. DT_TEXTREL = 22;
  239. DT_JMPREL = 23;
  240. DT_BIND_NOW = 24;
  241. DT_INIT_ARRAY = 25;
  242. DT_FINI_ARRAY = 26;
  243. DT_INIT_ARRAYSZ = 27;
  244. DT_FINI_ARRAYSZ = 28;
  245. DT_RUNPATH = 29;
  246. DT_FLAGS = 30;
  247. DT_ENCODING = 32;
  248. DT_PREINIT_ARRAY = 32;
  249. DT_PREINIT_ARRAYSZ = 33;
  250. DT_NUM = 34;
  251. DT_LOOS = $6000000D;
  252. DT_HIOS = $6ffff000;
  253. DT_LOPROC = $70000000;
  254. DT_HIPROC = $7fffffff;
  255. type
  256. { Structures which are written directly to the output file }
  257. TElf32header=packed record
  258. magic : array[0..3] of byte;
  259. file_class : byte;
  260. data_encoding : byte;
  261. file_version : byte;
  262. padding : array[$07..$0f] of byte;
  263. e_type : word;
  264. e_machine : word;
  265. e_version : longint;
  266. e_entry : longint; { entrypoint }
  267. e_phoff : longint; { program header offset }
  268. e_shoff : longint; { sections header offset }
  269. e_flags : longint;
  270. e_ehsize : word; { elf header size in bytes }
  271. e_phentsize : word; { size of an entry in the program header array }
  272. e_phnum : word; { 0..e_phnum-1 of entrys }
  273. e_shentsize : word; { size of an entry in sections header array }
  274. e_shnum : word; { 0..e_shnum-1 of entrys }
  275. e_shstrndx : word; { index of string section header }
  276. end;
  277. TElf32sechdr=packed record
  278. sh_name : longint;
  279. sh_type : longint;
  280. sh_flags : longint;
  281. sh_addr : longint;
  282. sh_offset : longint;
  283. sh_size : longint;
  284. sh_link : longint;
  285. sh_info : longint;
  286. sh_addralign : longint;
  287. sh_entsize : longint;
  288. end;
  289. TElf32proghdr=packed record
  290. p_type : longword;
  291. p_offset : longword;
  292. p_vaddr : longword;
  293. p_paddr : longword;
  294. p_filesz : longword;
  295. p_memsz : longword;
  296. p_flags : longword;
  297. p_align : longword;
  298. end;
  299. TElf32reloc=packed record
  300. address : longint;
  301. info : longint; { bit 0-7: type, 8-31: symbol }
  302. end;
  303. TElf32symbol=packed record
  304. st_name : longint;
  305. st_value : longint;
  306. st_size : longint;
  307. st_info : byte; { bit 0-3: type, 4-7: bind }
  308. st_other : byte;
  309. st_shndx : word;
  310. end;
  311. TElf32Dyn=packed record
  312. d_tag: longword;
  313. case integer of
  314. 0: (d_val: longword);
  315. 1: (d_ptr: longword);
  316. end;
  317. telf64header=packed record
  318. magic : array[0..3] of byte;
  319. file_class : byte;
  320. data_encoding : byte;
  321. file_version : byte;
  322. padding : array[$07..$0f] of byte;
  323. e_type : word;
  324. e_machine : word;
  325. e_version : longint;
  326. e_entry : qword; { entrypoint }
  327. e_phoff : qword; { program header offset }
  328. e_shoff : qword; { sections header offset }
  329. e_flags : longint;
  330. e_ehsize : word; { elf header size in bytes }
  331. e_phentsize : word; { size of an entry in the program header array }
  332. e_phnum : word; { 0..e_phnum-1 of entrys }
  333. e_shentsize : word; { size of an entry in sections header array }
  334. e_shnum : word; { 0..e_shnum-1 of entrys }
  335. e_shstrndx : word; { index of string section header }
  336. end;
  337. telf64sechdr=packed record
  338. sh_name : longint;
  339. sh_type : longint;
  340. sh_flags : qword;
  341. sh_addr : qword;
  342. sh_offset : qword;
  343. sh_size : qword;
  344. sh_link : longint;
  345. sh_info : longint;
  346. sh_addralign : qword;
  347. sh_entsize : qword;
  348. end;
  349. telf64proghdr=packed record
  350. p_type : longword;
  351. p_flags : longword;
  352. p_offset : qword;
  353. p_vaddr : qword;
  354. p_paddr : qword;
  355. p_filesz : qword;
  356. p_memsz : qword;
  357. p_align : qword;
  358. end;
  359. telf64reloc=packed record
  360. address : qword;
  361. info : qword; { bit 0-31: type, 32-63: symbol }
  362. addend : int64; { signed! }
  363. end;
  364. telf64symbol=packed record
  365. st_name : longint;
  366. st_info : byte; { bit 0-3: type, 4-7: bind }
  367. st_other : byte;
  368. st_shndx : word;
  369. st_value : qword;
  370. st_size : qword;
  371. end;
  372. TElf64Dyn=packed record
  373. d_tag: qword;
  374. case integer of
  375. 0: (d_val: qword);
  376. 1: (d_ptr: qword);
  377. end;
  378. {$ifdef cpu64bitaddr}
  379. const
  380. ELFCLASS = ELFCLASS64;
  381. type
  382. telfheader = telf64header;
  383. telfreloc = telf64reloc;
  384. telfsymbol = telf64symbol;
  385. telfsechdr = telf64sechdr;
  386. telfproghdr = telf64proghdr;
  387. telfdyn = telf64dyn;
  388. {$else cpu64bitaddr}
  389. const
  390. ELFCLASS = ELFCLASS32;
  391. type
  392. telfheader = telf32header;
  393. telfreloc = telf32reloc;
  394. telfsymbol = telf32symbol;
  395. telfsechdr = telf32sechdr;
  396. telfproghdr = telf32proghdr;
  397. telfdyn = telf32dyn;
  398. {$endif cpu64bitaddr}
  399. procedure MayBeSwapHeader(var h : telf32header);
  400. begin
  401. if source_info.endian<>target_info.endian then
  402. with h do
  403. begin
  404. e_type:=swapendian(e_type);
  405. e_machine:=swapendian(e_machine);
  406. e_version:=swapendian(e_version);
  407. e_entry:=swapendian(e_entry);
  408. e_phoff:=swapendian(e_phoff);
  409. e_shoff:=swapendian(e_shoff);
  410. e_flags:=swapendian(e_flags);
  411. e_ehsize:=swapendian(e_ehsize);
  412. e_phentsize:=swapendian(e_phentsize);
  413. e_phnum:=swapendian(e_phnum);
  414. e_shentsize:=swapendian(e_shentsize);
  415. e_shnum:=swapendian(e_shnum);
  416. e_shstrndx:=swapendian(e_shstrndx);
  417. end;
  418. end;
  419. procedure MayBeSwapHeader(var h : telf64header);
  420. begin
  421. if source_info.endian<>target_info.endian then
  422. with h do
  423. begin
  424. e_type:=swapendian(e_type);
  425. e_machine:=swapendian(e_machine);
  426. e_version:=swapendian(e_version);
  427. e_entry:=swapendian(e_entry);
  428. e_phoff:=swapendian(e_phoff);
  429. e_shoff:=swapendian(e_shoff);
  430. e_flags:=swapendian(e_flags);
  431. e_ehsize:=swapendian(e_ehsize);
  432. e_phentsize:=swapendian(e_phentsize);
  433. e_phnum:=swapendian(e_phnum);
  434. e_shentsize:=swapendian(e_shentsize);
  435. e_shnum:=swapendian(e_shnum);
  436. e_shstrndx:=swapendian(e_shstrndx);
  437. end;
  438. end;
  439. procedure MayBeSwapHeader(var h : telf32proghdr);
  440. begin
  441. if source_info.endian<>target_info.endian then
  442. with h do
  443. begin
  444. p_align:=swapendian(p_align);
  445. p_filesz:=swapendian(p_filesz);
  446. p_flags:=swapendian(p_flags);
  447. p_memsz:=swapendian(p_memsz);
  448. p_offset:=swapendian(p_offset);
  449. p_paddr:=swapendian(p_paddr);
  450. p_type:=swapendian(p_type);
  451. p_vaddr:=swapendian(p_vaddr);
  452. end;
  453. end;
  454. procedure MayBeSwapHeader(var h : telf64proghdr);
  455. begin
  456. if source_info.endian<>target_info.endian then
  457. with h do
  458. begin
  459. p_align:=swapendian(p_align);
  460. p_filesz:=swapendian(p_filesz);
  461. p_flags:=swapendian(p_flags);
  462. p_memsz:=swapendian(p_memsz);
  463. p_offset:=swapendian(p_offset);
  464. p_paddr:=swapendian(p_paddr);
  465. p_type:=swapendian(p_type);
  466. p_vaddr:=swapendian(p_vaddr);
  467. end;
  468. end;
  469. procedure MaybeSwapSecHeader(var h : telf32sechdr);
  470. begin
  471. if source_info.endian<>target_info.endian then
  472. with h do
  473. begin
  474. sh_name:=swapendian(sh_name);
  475. sh_type:=swapendian(sh_type);
  476. sh_flags:=swapendian(sh_flags);
  477. sh_addr:=swapendian(sh_addr);
  478. sh_offset:=swapendian(sh_offset);
  479. sh_size:=swapendian(sh_size);
  480. sh_link:=swapendian(sh_link);
  481. sh_info:=swapendian(sh_info);
  482. sh_addralign:=swapendian(sh_addralign);
  483. sh_entsize:=swapendian(sh_entsize);
  484. end;
  485. end;
  486. procedure MaybeSwapSecHeader(var h : telf64sechdr);
  487. begin
  488. if source_info.endian<>target_info.endian then
  489. with h do
  490. begin
  491. sh_name:=swapendian(sh_name);
  492. sh_type:=swapendian(sh_type);
  493. sh_flags:=swapendian(sh_flags);
  494. sh_addr:=swapendian(sh_addr);
  495. sh_offset:=swapendian(sh_offset);
  496. sh_size:=swapendian(sh_size);
  497. sh_link:=swapendian(sh_link);
  498. sh_info:=swapendian(sh_info);
  499. sh_addralign:=swapendian(sh_addralign);
  500. sh_entsize:=swapendian(sh_entsize);
  501. end;
  502. end;
  503. procedure MaybeSwapElfSymbol(var h : telf32symbol);
  504. begin
  505. if source_info.endian<>target_info.endian then
  506. with h do
  507. begin
  508. st_name:=swapendian(st_name);
  509. st_value:=swapendian(st_value);
  510. st_size:=swapendian(st_size);
  511. st_shndx:=swapendian(st_shndx);
  512. end;
  513. end;
  514. procedure MaybeSwapElfSymbol(var h : telf64symbol);
  515. begin
  516. if source_info.endian<>target_info.endian then
  517. with h do
  518. begin
  519. st_name:=swapendian(st_name);
  520. st_value:=swapendian(st_value);
  521. st_size:=swapendian(st_size);
  522. st_shndx:=swapendian(st_shndx);
  523. end;
  524. end;
  525. procedure MaybeSwapElfReloc(var h : telf32reloc);
  526. begin
  527. if source_info.endian<>target_info.endian then
  528. with h do
  529. begin
  530. address:=swapendian(address);
  531. info:=swapendian(info);
  532. end;
  533. end;
  534. procedure MaybeSwapElfReloc(var h : telf64reloc);
  535. begin
  536. if source_info.endian<>target_info.endian then
  537. with h do
  538. begin
  539. address:=swapendian(address);
  540. info:=swapendian(info);
  541. addend:=swapendian(addend);
  542. end;
  543. end;
  544. procedure MaybeSwapElfDyn(var h : telf32dyn);
  545. begin
  546. if source_info.endian<>target_info.endian then
  547. with h do
  548. begin
  549. d_tag:=swapendian(d_tag);
  550. d_val:=swapendian(d_val);
  551. end;
  552. end;
  553. procedure MaybeSwapElfDyn(var h : telf64dyn);
  554. begin
  555. if source_info.endian<>target_info.endian then
  556. with h do
  557. begin
  558. d_tag:=swapendian(d_tag);
  559. d_val:=swapendian(d_val);
  560. end;
  561. end;
  562. {****************************************************************************
  563. Helpers
  564. ****************************************************************************}
  565. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  566. begin
  567. { Section Type }
  568. AshType:=SHT_PROGBITS;
  569. if oso_strings in aoptions then
  570. AshType:=SHT_STRTAB
  571. else if not(oso_data in aoptions) then
  572. AshType:=SHT_NOBITS;
  573. { Section Flags }
  574. Ashflags:=0;
  575. if oso_load in aoptions then
  576. Ashflags:=Ashflags or SHF_ALLOC;
  577. if oso_executable in aoptions then
  578. Ashflags:=Ashflags or SHF_EXECINSTR;
  579. if oso_write in aoptions then
  580. Ashflags:=Ashflags or SHF_WRITE;
  581. end;
  582. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  583. begin
  584. aoptions:=[];
  585. { Section Type }
  586. if AshType<>SHT_NOBITS then
  587. include(aoptions,oso_data);
  588. if AshType=SHT_STRTAB then
  589. include(aoptions,oso_strings);
  590. { Section Flags }
  591. if Ashflags and SHF_ALLOC<>0 then
  592. include(aoptions,oso_load)
  593. else
  594. include(aoptions,oso_noload);
  595. if Ashflags and SHF_WRITE<>0 then
  596. include(aoptions,oso_write)
  597. else
  598. include(aoptions,oso_readonly);
  599. if Ashflags and SHF_EXECINSTR<>0 then
  600. include(aoptions,oso_executable);
  601. end;
  602. {****************************************************************************
  603. TElfObjSection
  604. ****************************************************************************}
  605. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  606. begin
  607. inherited create(AList,Aname,Aalign,aoptions);
  608. secshidx:=0;
  609. shstridx:=0;
  610. encodesechdrflags(aoptions,shtype,shflags);
  611. shlink:=0;
  612. shinfo:=0;
  613. if name='.stab' then
  614. shentsize:=sizeof(TObjStabEntry);
  615. end;
  616. constructor TElfObjSection.create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  617. var
  618. aoptions : TObjSectionOptions;
  619. begin
  620. decodesechdrflags(Ashtype,Ashflags,aoptions);
  621. inherited create(aobjdata.ObjSectionList,Aname,Aalign,aoptions);
  622. objdata:=aobjdata;
  623. secshidx:=0;
  624. shstridx:=0;
  625. shtype:=AshType;
  626. shflags:=AshFlags;
  627. shlink:=Ashlink;
  628. shinfo:=Ashinfo;
  629. shentsize:=Aentsize;
  630. end;
  631. {****************************************************************************
  632. TElfObjData
  633. ****************************************************************************}
  634. constructor TElfObjData.create(const n:string);
  635. begin
  636. inherited create(n);
  637. CObjSection:=TElfObjSection;
  638. { we need at least the following sections }
  639. createsection(sec_code);
  640. { always a non-PIC data section (will remain empty if doing PIC) }
  641. createsection('.data',sizeof(pint),sectiontype2options(sec_data));
  642. createsection(sec_bss);
  643. if (cs_create_pic in current_settings.moduleswitches) and
  644. not(target_info.system in systems_darwin) then
  645. createsection(sec_data);
  646. if tf_section_threadvars in target_info.flags then
  647. createsection(sec_threadvar);
  648. if (tf_needs_dwarf_cfi in target_info.flags) and
  649. (af_supports_dwarf in target_asm.flags) then
  650. createsection(sec_debug_frame);
  651. end;
  652. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  653. const
  654. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  655. {$ifdef userodata}
  656. '.text','.data','.data','.rodata','.bss','.threadvar',
  657. {$else userodata}
  658. '.text','.data','.data','.data','.bss','.threadvar',
  659. {$endif userodata}
  660. '.pdata',
  661. '.text', { darwin stubs }
  662. '__DATA,__nl_symbol_ptr',
  663. '__DATA,__la_symbol_ptr',
  664. '__DATA,__mod_init_func',
  665. '__DATA,__mod_term_func',
  666. '.stab','.stabstr',
  667. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  668. '.eh_frame',
  669. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  670. '.fpc',
  671. '.toc',
  672. '.init',
  673. '.fini',
  674. '.objc_class',
  675. '.objc_meta_class',
  676. '.objc_cat_cls_meth',
  677. '.objc_cat_inst_meth',
  678. '.objc_protocol',
  679. '.objc_string_object',
  680. '.objc_cls_meth',
  681. '.objc_inst_meth',
  682. '.objc_cls_refs',
  683. '.objc_message_refs',
  684. '.objc_symbols',
  685. '.objc_category',
  686. '.objc_class_vars',
  687. '.objc_instance_vars',
  688. '.objc_module_info',
  689. '.objc_class_names',
  690. '.objc_meth_var_types',
  691. '.objc_meth_var_names',
  692. '.objc_selector_strs',
  693. '.objc_protocol_ext',
  694. '.objc_class_ext',
  695. '.objc_property',
  696. '.objc_image_info',
  697. '.objc_cstring_object',
  698. '.objc_sel_fixup',
  699. '__DATA,__objc_data',
  700. '__DATA,__objc_const',
  701. '.objc_superrefs',
  702. '__DATA, __datacoal_nt,coalesced',
  703. '.objc_classlist',
  704. '.objc_nlclasslist',
  705. '.objc_catlist',
  706. '.obcj_nlcatlist',
  707. '.objc_protolist'
  708. );
  709. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  710. '.text',
  711. '.data.rel',
  712. '.data.rel',
  713. '.data.rel',
  714. '.bss',
  715. '.threadvar',
  716. '.pdata',
  717. '', { stubs }
  718. '__DATA,__nl_symbol_ptr',
  719. '__DATA,__la_symbol_ptr',
  720. '__DATA,__mod_init_func',
  721. '__DATA,__mod_term_func',
  722. '.stab',
  723. '.stabstr',
  724. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  725. '.eh_frame',
  726. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  727. '.fpc',
  728. '.toc',
  729. '.init',
  730. '.fini',
  731. '.objc_class',
  732. '.objc_meta_class',
  733. '.objc_cat_cls_meth',
  734. '.objc_cat_inst_meth',
  735. '.objc_protocol',
  736. '.objc_string_object',
  737. '.objc_cls_meth',
  738. '.objc_inst_meth',
  739. '.objc_cls_refs',
  740. '.objc_message_refs',
  741. '.objc_symbols',
  742. '.objc_category',
  743. '.objc_class_vars',
  744. '.objc_instance_vars',
  745. '.objc_module_info',
  746. '.objc_class_names',
  747. '.objc_meth_var_types',
  748. '.objc_meth_var_names',
  749. '.objc_selector_strs',
  750. '.objc_protocol_ext',
  751. '.objc_class_ext',
  752. '.objc_property',
  753. '.objc_image_info',
  754. '.objc_cstring_object',
  755. '.objc_sel_fixup',
  756. '__DATA,__objc_data',
  757. '__DATA,__objc_const',
  758. '.objc_superrefs',
  759. '__DATA, __datacoal_nt,coalesced',
  760. '.objc_classlist',
  761. '.objc_nlclasslist',
  762. '.objc_catlist',
  763. '.obcj_nlcatlist',
  764. '.objc_protolist'
  765. );
  766. var
  767. sep : string[3];
  768. secname : string;
  769. begin
  770. { section type user gives the user full controll on the section name }
  771. if atype=sec_user then
  772. result:=aname
  773. else
  774. begin
  775. if (cs_create_pic in current_settings.moduleswitches) and
  776. not(target_info.system in systems_darwin) then
  777. secname:=secnames_pic[atype]
  778. else
  779. secname:=secnames[atype];
  780. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  781. begin
  782. result:=secname+'.'+aname;
  783. exit;
  784. end;
  785. if create_smartlink_sections and (aname<>'') then
  786. begin
  787. case aorder of
  788. secorder_begin :
  789. sep:='.b_';
  790. secorder_end :
  791. sep:='.z_';
  792. else
  793. sep:='.n_';
  794. end;
  795. result:=secname+sep+aname
  796. end
  797. else
  798. result:=secname;
  799. end;
  800. end;
  801. procedure TElfObjData.CreateDebugSections;
  802. begin
  803. if target_dbg.id=dbg_stabs then
  804. begin
  805. stabssec:=createsection(sec_stab);
  806. stabstrsec:=createsection(sec_stabstr);
  807. end;
  808. end;
  809. procedure TElfObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);
  810. var
  811. symaddr : aint;
  812. begin
  813. if CurrObjSec=nil then
  814. internalerror(200403292);
  815. {$ifdef userodata}
  816. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  817. internalerror(200408252);
  818. {$endif userodata}
  819. { Using RELOC_RVA to map 32-bit RELOC_ABSOLUTE to R_X86_64_32
  820. (RELOC_ABSOLUTE maps to R_X86_64_32S) }
  821. if (reltype=RELOC_ABSOLUTE) and (len<>sizeof(pint)) then
  822. reltype:=RELOC_RVA;
  823. if assigned(p) then
  824. begin
  825. { real address of the symbol }
  826. symaddr:=p.address;
  827. { Local ObjSymbols can be resolved already or need a section reloc }
  828. if (p.bind=AB_LOCAL) and
  829. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32,RELOC_RVA{$endif x86_64}]) then
  830. begin
  831. { For a reltype relocation in the same section the
  832. value can be calculated }
  833. if (p.objsection=CurrObjSec) and
  834. (reltype=RELOC_RELATIVE) then
  835. inc(data,symaddr-len-CurrObjSec.Size)
  836. else
  837. begin
  838. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,reltype);
  839. inc(data,symaddr);
  840. end;
  841. end
  842. else
  843. begin
  844. CurrObjSec.addsymreloc(CurrObjSec.Size,p,reltype);
  845. {$ifndef x86_64}
  846. if (reltype=RELOC_RELATIVE) or (reltype=RELOC_PLT32) then
  847. dec(data,len);
  848. {$endif x86_64}
  849. end;
  850. end;
  851. CurrObjSec.write(data,len);
  852. end;
  853. {****************************************************************************
  854. TElfSymtab
  855. ****************************************************************************}
  856. const
  857. symsecnames: array[boolean] of string[8] = ('.symtab','.dynsym');
  858. strsecnames: array[boolean] of string[8] = ('.strtab','.dynstr');
  859. symsectypes: array[boolean] of longint = (SHT_SYMTAB,SHT_DYNSYM);
  860. symsecattrs: array[boolean] of longint = (0,SHF_ALLOC);
  861. constructor TElfSymtab.create(aObjData:TObjData;aKind:TElfSymtabKind);
  862. var
  863. dyn:boolean;
  864. begin
  865. dyn:=(aKind=esk_dyn);
  866. create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],0,0,sizeof(pint),sizeof(TElfSymbol));
  867. fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],0,0,1,0);
  868. fstrsec.writestr(#0);
  869. writezeros(sizeof(TElfSymbol));
  870. symidx:=1;
  871. shinfo:=1;
  872. kind:=aKind;
  873. end;
  874. procedure TElfSymtab.writeInternalSymbol(astridx:longint;ainfo:byte;ashndx:word);
  875. var
  876. elfsym:TElfSymbol;
  877. begin
  878. fillchar(elfsym,sizeof(elfsym),0);
  879. elfsym.st_name:=astridx;
  880. elfsym.st_info:=ainfo;
  881. elfsym.st_shndx:=ashndx;
  882. inc(symidx);
  883. inc(shinfo);
  884. MaybeSwapElfSymbol(elfsym);
  885. write(elfsym,sizeof(elfsym));
  886. end;
  887. procedure TElfSymtab.writeSymbol(objsym:TObjSymbol);
  888. var
  889. elfsym:TElfSymbol;
  890. begin
  891. fillchar(elfsym,sizeof(elfsym),0);
  892. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  893. elfsym.st_name:=fstrsec.writestr(objsym.name);
  894. fstrsec.writestr(#0);
  895. elfsym.st_size:=objsym.size;
  896. case objsym.bind of
  897. AB_LOCAL :
  898. begin
  899. elfsym.st_value:=objsym.address;
  900. elfsym.st_info:=STB_LOCAL shl 4;
  901. inc(shinfo);
  902. end;
  903. AB_COMMON :
  904. begin
  905. elfsym.st_value:=$10; { ?? should not be hardcoded }
  906. elfsym.st_info:=STB_GLOBAL shl 4;
  907. elfsym.st_shndx:=SHN_COMMON;
  908. end;
  909. AB_EXTERNAL :
  910. elfsym.st_info:=STB_GLOBAL shl 4;
  911. AB_WEAK_EXTERNAL :
  912. elfsym.st_info:=STB_WEAK shl 4;
  913. AB_GLOBAL :
  914. begin
  915. elfsym.st_value:=objsym.address;
  916. elfsym.st_info:=STB_GLOBAL shl 4;
  917. end;
  918. end;
  919. if (objsym.bind<>AB_EXTERNAL) {and
  920. not(assigned(objsym.objsection) and
  921. not(oso_data in objsym.objsection.secoptions))} then
  922. begin
  923. case objsym.typ of
  924. AT_FUNCTION :
  925. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  926. AT_DATA :
  927. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  928. end;
  929. end;
  930. if objsym.bind<>AB_COMMON then
  931. begin
  932. if kind<>esk_obj then
  933. begin
  934. { TODO }
  935. end
  936. else
  937. begin
  938. if assigned(objsym.objsection) then
  939. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  940. else
  941. elfsym.st_shndx:=SHN_UNDEF;
  942. objsym.symidx:=symidx;
  943. end;
  944. end;
  945. inc(symidx);
  946. MaybeSwapElfSymbol(elfsym);
  947. write(elfsym,sizeof(TElfSymbol));
  948. end;
  949. {****************************************************************************
  950. TElfObjectOutput
  951. ****************************************************************************}
  952. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  953. begin
  954. inherited Create(AWriter);
  955. CObjData:=TElfObjData;
  956. end;
  957. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection;data:TObjData);
  958. var
  959. i : longint;
  960. rel : telfreloc;
  961. objreloc : TObjRelocation;
  962. relsym,
  963. reltyp : longint;
  964. relocsect : TObjSection;
  965. {$ifdef x86_64}
  966. tmp: aint;
  967. asize: longint;
  968. {$endif x86_64}
  969. begin
  970. with data do
  971. begin
  972. {$ifdef userodata}
  973. { rodata can't have relocations }
  974. if s.sectype=sec_rodata then
  975. begin
  976. if assigned(s.relocations.first) then
  977. internalerror(200408251);
  978. exit;
  979. end;
  980. {$endif userodata}
  981. { create the reloc section }
  982. {$ifdef i386}
  983. relocsect:=TElfObjSection.create_ext(data,'.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  984. {$else i386}
  985. relocsect:=TElfObjSection.create_ext(data,'.rela'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  986. {$endif i386}
  987. { add the relocations }
  988. for i:=0 to s.Objrelocations.count-1 do
  989. begin
  990. objreloc:=TObjRelocation(s.Objrelocations[i]);
  991. fillchar(rel,sizeof(rel),0);
  992. rel.address:=objreloc.dataoffset;
  993. { when things settle down, we can create processor specific
  994. derived classes }
  995. case objreloc.typ of
  996. {$ifdef i386}
  997. RELOC_RELATIVE :
  998. reltyp:=R_386_PC32;
  999. RELOC_ABSOLUTE :
  1000. reltyp:=R_386_32;
  1001. RELOC_GOT32 :
  1002. reltyp:=R_386_GOT32;
  1003. RELOC_GOTPC :
  1004. reltyp:=R_386_GOTPC;
  1005. RELOC_PLT32 :
  1006. begin
  1007. reltyp:=R_386_PLT32;
  1008. end;
  1009. {$endif i386}
  1010. {$ifdef sparc}
  1011. RELOC_ABSOLUTE :
  1012. reltyp:=R_SPARC_32;
  1013. {$endif sparc}
  1014. {$ifdef x86_64}
  1015. RELOC_RELATIVE :
  1016. begin
  1017. reltyp:=R_X86_64_PC32;
  1018. { length of the relocated location is handled here }
  1019. rel.addend:=-4;
  1020. end;
  1021. RELOC_ABSOLUTE :
  1022. reltyp:=R_X86_64_64;
  1023. RELOC_ABSOLUTE32 :
  1024. reltyp:=R_X86_64_32S;
  1025. RELOC_RVA :
  1026. reltyp:=R_X86_64_32;
  1027. RELOC_GOTPCREL :
  1028. begin
  1029. reltyp:=R_X86_64_GOTPCREL;
  1030. { length of the relocated location is handled here }
  1031. rel.addend:=-4;
  1032. end;
  1033. RELOC_PLT32 :
  1034. begin
  1035. reltyp:=R_X86_64_PLT32;
  1036. { length of the relocated location is handled here }
  1037. rel.addend:=-4;
  1038. end;
  1039. {$endif x86_64}
  1040. else
  1041. internalerror(200602261);
  1042. end;
  1043. { This handles ELF 'rela'-styled relocations, which are currently used only for x86_64,
  1044. but can be used other targets, too. }
  1045. {$ifdef x86_64}
  1046. s.Data.Seek(objreloc.dataoffset);
  1047. if objreloc.typ=RELOC_ABSOLUTE then
  1048. begin
  1049. asize:=8;
  1050. s.Data.Read(tmp,8);
  1051. rel.addend:=rel.addend+tmp;
  1052. end
  1053. else
  1054. begin
  1055. asize:=4;
  1056. s.Data.Read(tmp,4);
  1057. rel.addend:=rel.addend+longint(tmp);
  1058. end;
  1059. { and zero the data member out }
  1060. tmp:=0;
  1061. s.Data.Seek(objreloc.dataoffset);
  1062. s.Data.Write(tmp,asize);
  1063. {$endif}
  1064. { Symbol }
  1065. if assigned(objreloc.symbol) then
  1066. begin
  1067. if objreloc.symbol.symidx=-1 then
  1068. begin
  1069. writeln(objreloc.symbol.Name);
  1070. internalerror(200603012);
  1071. end;
  1072. relsym:=objreloc.symbol.symidx;
  1073. end
  1074. else
  1075. begin
  1076. if objreloc.objsection<>nil then
  1077. relsym:=objreloc.objsection.secsymidx
  1078. else
  1079. relsym:=SHN_UNDEF;
  1080. end;
  1081. {$ifdef cpu64bitaddr}
  1082. rel.info:=(qword(relsym) shl 32) or reltyp;
  1083. {$else cpu64bitaddr}
  1084. rel.info:=(relsym shl 8) or reltyp;
  1085. {$endif cpu64bitaddr}
  1086. { write reloc }
  1087. MaybeSwapElfReloc(rel);
  1088. relocsect.write(rel,sizeof(rel));
  1089. end;
  1090. end;
  1091. end;
  1092. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  1093. begin
  1094. { Must not write symbols for internal sections like .symtab }
  1095. { TODO: maybe use inclusive list of section types instead }
  1096. if (TElfObjSection(p).shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  1097. exit;
  1098. TObjSection(p).secsymidx:=symtabsect.symidx;
  1099. symtabsect.writeInternalSymbol(0,STT_SECTION,TElfObjSection(p).secshidx);
  1100. end;
  1101. procedure TElfObjectOutput.createsymtab(data: TObjData);
  1102. var
  1103. i : longint;
  1104. objsym : TObjSymbol;
  1105. begin
  1106. with data do
  1107. begin
  1108. { filename entry }
  1109. symtabsect.writeInternalSymbol(1,STT_FILE,SHN_ABS);
  1110. { section }
  1111. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  1112. { First the Local Symbols, this is required by ELF. The localsyms
  1113. count stored in shinfo is used to skip the local symbols
  1114. when traversing the symtab }
  1115. for i:=0 to ObjSymbolList.Count-1 do
  1116. begin
  1117. objsym:=TObjSymbol(ObjSymbolList[i]);
  1118. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  1119. symtabsect.WriteSymbol(objsym);
  1120. end;
  1121. { Global Symbols }
  1122. for i:=0 to ObjSymbolList.Count-1 do
  1123. begin
  1124. objsym:=TObjSymbol(ObjSymbolList[i]);
  1125. if (objsym.bind<>AB_LOCAL) then
  1126. symtabsect.WriteSymbol(objsym);
  1127. end;
  1128. { update the .symtab section header }
  1129. symtabsect.shlink:=TElfObjSection(symtabsect.fstrsec).secshidx;
  1130. end;
  1131. end;
  1132. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  1133. begin
  1134. TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name+#0);
  1135. end;
  1136. procedure TElfObjectOutput.createshstrtab(data: TObjData);
  1137. begin
  1138. with data do
  1139. begin
  1140. shstrtabsect.writestr(#0);
  1141. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  1142. end;
  1143. end;
  1144. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  1145. var
  1146. sechdr : telfsechdr;
  1147. begin
  1148. fillchar(sechdr,sizeof(sechdr),0);
  1149. sechdr.sh_name:=s.shstridx;
  1150. sechdr.sh_type:=s.shtype;
  1151. sechdr.sh_flags:=s.shflags;
  1152. sechdr.sh_offset:=s.datapos;
  1153. sechdr.sh_size:=s.Size;
  1154. sechdr.sh_link:=s.shlink;
  1155. sechdr.sh_info:=s.shinfo;
  1156. sechdr.sh_addralign:=s.secalign;
  1157. sechdr.sh_entsize:=s.shentsize;
  1158. MaybeSwapSecHeader(sechdr);
  1159. writer.write(sechdr,sizeof(sechdr));
  1160. end;
  1161. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  1162. begin
  1163. TElfObjSection(p).secshidx:=pword(arg)^;
  1164. inc(pword(arg)^);
  1165. end;
  1166. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  1167. begin
  1168. if (TElfObjSection(p).ObjRelocations.count>0) then
  1169. createrelocsection(TElfObjSection(p),TObjData(arg));
  1170. end;
  1171. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  1172. begin
  1173. writesectionheader(TElfObjSection(p));
  1174. end;
  1175. function TElfObjectOutput.writedata(data:TObjData):boolean;
  1176. var
  1177. header : telfheader;
  1178. shoffset,
  1179. datapos : aword;
  1180. nsections : word;
  1181. begin
  1182. result:=false;
  1183. with data do
  1184. begin
  1185. { default sections }
  1186. symtabsect:=TElfSymtab.create(data,esk_obj);
  1187. shstrtabsect:=TElfObjSection.create_ext(data,'.shstrtab',SHT_STRTAB,0,0,0,1,0);
  1188. { "no executable stack" marker for Linux }
  1189. if (target_info.system in systems_linux) and
  1190. not(cs_executable_stack in current_settings.moduleswitches) then
  1191. TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,0,0,1,0);
  1192. { insert filename as first in strtab }
  1193. symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
  1194. symtabsect.fstrsec.writestr(#0);
  1195. { calc amount of sections we have }
  1196. nsections:=1;
  1197. { also create the index in the section header table }
  1198. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1199. { create .symtab and .strtab }
  1200. createsymtab(data);
  1201. { Create the relocation sections, this needs valid secidx and symidx }
  1202. ObjSectionList.ForEachCall(@section_create_relocsec,data);
  1203. { recalc nsections to incude the reloc sections }
  1204. nsections:=1;
  1205. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1206. { create .shstrtab }
  1207. createshstrtab(data);
  1208. { Calculate the filepositions }
  1209. datapos:=$40; { elfheader + alignment }
  1210. { section data }
  1211. layoutsections(datapos);
  1212. { section headers }
  1213. shoffset:=datapos;
  1214. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  1215. { Write ELF Header }
  1216. fillchar(header,sizeof(header),0);
  1217. header.magic[0]:=$7f; { = #127'ELF' }
  1218. header.magic[1]:=$45;
  1219. header.magic[2]:=$4c;
  1220. header.magic[3]:=$46;
  1221. header.file_class:=ELFCLASS;
  1222. if target_info.endian=endian_big then
  1223. header.data_encoding:=2
  1224. else
  1225. header.data_encoding:=1;
  1226. header.file_version:=1;
  1227. header.e_type:=ET_REL;
  1228. header.e_machine:=ELFMACHINE;
  1229. {$ifdef arm}
  1230. if (current_settings.fputype=fpu_soft) then
  1231. header.e_flags:=$600;
  1232. {$endif arm}
  1233. header.e_version:=1;
  1234. header.e_shoff:=shoffset;
  1235. header.e_shstrndx:=shstrtabsect.secshidx;
  1236. header.e_shnum:=nsections;
  1237. header.e_ehsize:=sizeof(telfheader);
  1238. header.e_shentsize:=sizeof(telfsechdr);
  1239. MaybeSwapHeader(header);
  1240. writer.write(header,sizeof(header));
  1241. writer.writezeros($40-sizeof(header)); { align }
  1242. { Sections }
  1243. WriteSectionContent(data);
  1244. { section headers, start with an empty header for sh_undef }
  1245. writer.writezeros(sizeof(telfsechdr));
  1246. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1247. end;
  1248. result:=true;
  1249. end;
  1250. {****************************************************************************
  1251. TELFAssembler
  1252. ****************************************************************************}
  1253. constructor TElfAssembler.Create(smart:boolean);
  1254. begin
  1255. inherited Create(smart);
  1256. CObjOutput:=TElfObjectOutput;
  1257. end;
  1258. {*****************************************************************************
  1259. Initialize
  1260. *****************************************************************************}
  1261. {$ifdef i386}
  1262. const
  1263. as_i386_elf32_info : tasminfo =
  1264. (
  1265. id : as_i386_elf32;
  1266. idtxt : 'ELF';
  1267. asmbin : '';
  1268. asmcmd : '';
  1269. supported_targets : [system_i386_linux,system_i386_beos,
  1270. system_i386_freebsd,system_i386_haiku,
  1271. system_i386_openbsd,system_i386_netbsd,
  1272. system_i386_Netware,system_i386_netwlibc,
  1273. system_i386_solaris,system_i386_embedded];
  1274. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1275. labelprefix : '.L';
  1276. comment : '';
  1277. dollarsign: '$';
  1278. );
  1279. {$endif i386}
  1280. {$ifdef x86_64}
  1281. const
  1282. as_x86_64_elf64_info : tasminfo =
  1283. (
  1284. id : as_x86_64_elf64;
  1285. idtxt : 'ELF';
  1286. asmbin : '';
  1287. asmcmd : '';
  1288. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  1289. system_x86_64_openbsd,system_x86_64_netbsd];
  1290. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1291. labelprefix : '.L';
  1292. comment : '';
  1293. dollarsign: '$';
  1294. );
  1295. {$endif x86_64}
  1296. {$ifdef sparc}
  1297. const
  1298. as_sparc_elf32_info : tasminfo =
  1299. (
  1300. id : as_sparc_elf32;
  1301. idtxt : 'ELF';
  1302. asmbin : '';
  1303. asmcmd : '';
  1304. supported_targets : [];
  1305. // flags : [af_outputbinary,af_smartlink_sections];
  1306. flags : [af_outputbinary,af_supports_dwarf];
  1307. labelprefix : '.L';
  1308. comment : '';
  1309. dollarsign: '$';
  1310. );
  1311. {$endif sparc}
  1312. initialization
  1313. {$ifdef i386}
  1314. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1315. {$endif i386}
  1316. {$ifdef sparc}
  1317. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1318. {$endif sparc}
  1319. {$ifdef x86_64}
  1320. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1321. {$endif x86_64}
  1322. end.