ogelf.pas 48 KB

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