ogelf.pas 40 KB

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