ogelf.pas 38 KB

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