ogelf.pas 40 KB

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