ogelf.pas 47 KB

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