ogelf.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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;relative: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;relative: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) then
  564. begin
  565. { For a relative relocation in the same section the
  566. value can be calculated }
  567. if (p.objsection=CurrObjSec) and
  568. (relative=RELOC_RELATIVE) then
  569. inc(data,symaddr-len-CurrObjSec.Size)
  570. else
  571. begin
  572. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,relative);
  573. inc(data,symaddr);
  574. end;
  575. end
  576. else
  577. begin
  578. CurrObjSec.addsymreloc(CurrObjSec.Size,p,relative);
  579. {$ifndef x86_64}
  580. if relative=RELOC_RELATIVE then
  581. dec(data,len);
  582. {$endif x86_64}
  583. end;
  584. end;
  585. CurrObjSec.write(data,len);
  586. end;
  587. procedure TElfObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  588. var
  589. stab : TObjStabEntry;
  590. begin
  591. if not assigned(StabsSec) then
  592. internalerror(200602271);
  593. fillchar(stab,sizeof(TObjStabEntry),0);
  594. if assigned(p) and (p[0]<>#0) then
  595. begin
  596. stab.strpos:=stabstrsec.Size;
  597. stabstrsec.write(p^,strlen(p)+1);
  598. end;
  599. stab.ntype:=nidx;
  600. stab.ndesc:=ndesc;
  601. stab.nother:=nother;
  602. stab.nvalue:=offset;
  603. stabssec.write(stab,sizeof(stab));
  604. if assigned(ps) then
  605. stabssec.addsymreloc(stabssec.Size-4,ps,RELOC_ABSOLUTE);
  606. end;
  607. {****************************************************************************
  608. TElfObjectOutput
  609. ****************************************************************************}
  610. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  611. begin
  612. inherited Create(AWriter);
  613. CObjData:=TElfObjData;
  614. end;
  615. destructor TElfObjectOutput.destroy;
  616. begin
  617. inherited destroy;
  618. end;
  619. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection);
  620. var
  621. i : longint;
  622. {$ifdef ver2_0_0}
  623. relnative,
  624. {$endif ver2_0_0}
  625. rel : telfreloc;
  626. objreloc : TObjRelocation;
  627. relsym,
  628. reltyp : longint;
  629. relocsect : TObjSection;
  630. begin
  631. with elf32data do
  632. begin
  633. {$ifdef userodata}
  634. { rodata can't have relocations }
  635. if s.sectype=sec_rodata then
  636. begin
  637. if assigned(s.relocations.first) then
  638. internalerror(200408251);
  639. exit;
  640. end;
  641. {$endif userodata}
  642. { create the reloc section }
  643. {$ifdef i386}
  644. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  645. {$else i386}
  646. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rela'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  647. {$endif i386}
  648. { add the relocations }
  649. for i:=0 to s.Objrelocations.count-1 do
  650. begin
  651. objreloc:=TObjRelocation(s.Objrelocations[i]);
  652. fillchar(rel,sizeof(rel),0);
  653. rel.address:=objreloc.dataoffset;
  654. if assigned(objreloc.symbol) then
  655. begin
  656. if (objreloc.symbol.bind=AB_LOCAL) then
  657. relsym:=objreloc.symbol.objsection.secsymidx
  658. else
  659. begin
  660. if objreloc.symbol.symidx=-1 then
  661. internalerror(200603012);
  662. relsym:=objreloc.symbol.symidx;
  663. end;
  664. end
  665. else
  666. begin
  667. if objreloc.objsection<>nil then
  668. relsym:=objreloc.objsection.secsymidx
  669. else
  670. relsym:=SHN_UNDEF;
  671. end;
  672. { when things settle down, we can create processor specific
  673. derived classes }
  674. case objreloc.typ of
  675. {$ifdef i386}
  676. RELOC_RELATIVE :
  677. reltyp:=R_386_PC32;
  678. RELOC_ABSOLUTE :
  679. reltyp:=R_386_32;
  680. {$endif i386}
  681. {$ifdef sparc}
  682. RELOC_ABSOLUTE :
  683. reltyp:=R_SPARC_32;
  684. {$endif sparc}
  685. {$ifdef x86_64}
  686. RELOC_RELATIVE :
  687. begin
  688. reltyp:=R_X86_64_PC32;
  689. { length of the relocated location is handled here }
  690. rel.addend:=qword(-4);
  691. end;
  692. RELOC_ABSOLUTE :
  693. reltyp:=R_X86_64_64;
  694. RELOC_ABSOLUTE32 :
  695. reltyp:=R_X86_64_32S;
  696. {$endif x86_64}
  697. else
  698. internalerror(200602261);
  699. end;
  700. {$ifdef cpu64bit}
  701. rel.info:=(qword(relsym) shl 32) or reltyp;
  702. {$else cpu64bit}
  703. rel.info:=(relsym shl 8) or reltyp;
  704. {$endif cpu64bit}
  705. { write reloc }
  706. {$ifdef ver2_0_0}
  707. relnative:=MaybeSwapElfReloc(rel);
  708. relocsect.write(relnative,sizeof(rel));
  709. {$else}
  710. relocsect.write(MaybeSwapElfReloc(rel),sizeof(rel));
  711. {$endif ver2_0_0}
  712. end;
  713. end;
  714. end;
  715. procedure TElfObjectOutput.write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  716. var
  717. {$ifdef ver2_0_0}
  718. elfsymnative,
  719. {$endif ver2_0_0}
  720. elfsym : telfsymbol;
  721. begin
  722. fillchar(elfsym,sizeof(elfsym),0);
  723. elfsym.st_name:=astridx;
  724. elfsym.st_info:=ainfo;
  725. elfsym.st_shndx:=ashndx;
  726. inc(symidx);
  727. inc(localsyms);
  728. {$ifdef ver2_0_0}
  729. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  730. symtabsect.write(elfsymnative,sizeof(elfsym));
  731. {$else}
  732. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  733. {$endif ver2_0_0}
  734. end;
  735. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  736. begin
  737. TObjSection(p).secsymidx:=symidx;
  738. write_internal_symbol(TElfObjSection(p).shstridx,STT_SECTION,TElfObjSection(p).secshidx);
  739. end;
  740. procedure TElfObjectOutput.createsymtab;
  741. procedure WriteSym(objsym:TObjSymbol);
  742. var
  743. {$ifdef ver2_0_0}
  744. elfsymnative,
  745. {$endif}
  746. elfsym : telfsymbol;
  747. begin
  748. with elf32data do
  749. begin
  750. fillchar(elfsym,sizeof(elfsym),0);
  751. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  752. elfsym.st_name:=strtabsect.Size;
  753. strtabsect.writestr(objsym.name);
  754. strtabsect.writestr(#0);
  755. elfsym.st_size:=objsym.size;
  756. case objsym.bind of
  757. AB_LOCAL :
  758. begin
  759. elfsym.st_value:=objsym.address;
  760. elfsym.st_info:=STB_LOCAL shl 4;
  761. inc(localsyms);
  762. end;
  763. AB_COMMON :
  764. begin
  765. elfsym.st_value:=$10;
  766. elfsym.st_info:=STB_GLOBAL shl 4;
  767. end;
  768. AB_EXTERNAL :
  769. elfsym.st_info:=STB_GLOBAL shl 4;
  770. AB_GLOBAL :
  771. begin
  772. elfsym.st_value:=objsym.address;
  773. elfsym.st_info:=STB_GLOBAL shl 4;
  774. end;
  775. end;
  776. if (objsym.bind<>AB_EXTERNAL) {and
  777. not(assigned(objsym.objsection) and
  778. not(oso_data in objsym.objsection.secoptions))} then
  779. begin
  780. case objsym.typ of
  781. AT_FUNCTION :
  782. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  783. AT_DATA :
  784. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  785. end;
  786. end;
  787. if objsym.bind=AB_COMMON then
  788. elfsym.st_shndx:=SHN_COMMON
  789. else
  790. begin
  791. if assigned(objsym.objsection) then
  792. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  793. else
  794. elfsym.st_shndx:=SHN_UNDEF;
  795. end;
  796. objsym.symidx:=symidx;
  797. inc(symidx);
  798. {$ifdef ver2_0_0}
  799. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  800. symtabsect.write(elfsymnative,sizeof(elfsym));
  801. {$else}
  802. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  803. {$endif ver2_0_0}
  804. end;
  805. end;
  806. var
  807. i : longint;
  808. objsym : TObjSymbol;
  809. begin
  810. with elf32data do
  811. begin
  812. symidx:=0;
  813. localsyms:=0;
  814. { empty entry }
  815. write_internal_symbol(0,0,0);
  816. { filename entry }
  817. write_internal_symbol(1,STT_FILE,SHN_ABS);
  818. { section }
  819. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  820. { First the Local Symbols, this is required by ELF. The localsyms
  821. count stored in shinfo is used to skip the local symbols
  822. when traversing the symtab }
  823. for i:=0 to ObjSymbolList.Count-1 do
  824. begin
  825. objsym:=TObjSymbol(ObjSymbolList[i]);
  826. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  827. WriteSym(objsym);
  828. end;
  829. { Global Symbols }
  830. for i:=0 to ObjSymbolList.Count-1 do
  831. begin
  832. objsym:=TObjSymbol(ObjSymbolList[i]);
  833. if (objsym.bind<>AB_LOCAL) then
  834. WriteSym(objsym);
  835. end;
  836. { update the .symtab section header }
  837. symtabsect.shlink:=strtabsect.secshidx;
  838. symtabsect.shinfo:=localsyms;
  839. end;
  840. end;
  841. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  842. begin
  843. TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name+#0);
  844. end;
  845. procedure TElfObjectOutput.createshstrtab;
  846. begin
  847. with elf32data do
  848. begin
  849. shstrtabsect.writestr(#0);
  850. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  851. end;
  852. end;
  853. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  854. var
  855. {$ifdef ver2_0_0}
  856. sechdrnative,
  857. {$endif ver2_0_0}
  858. sechdr : telfsechdr;
  859. begin
  860. fillchar(sechdr,sizeof(sechdr),0);
  861. sechdr.sh_name:=s.shstridx;
  862. sechdr.sh_type:=s.shtype;
  863. sechdr.sh_flags:=s.shflags;
  864. sechdr.sh_offset:=s.datapos;
  865. sechdr.sh_size:=s.Size;
  866. sechdr.sh_link:=s.shlink;
  867. sechdr.sh_info:=s.shinfo;
  868. sechdr.sh_addralign:=s.secalign;
  869. sechdr.sh_entsize:=s.shentsize;
  870. {$ifdef ver2_0_0}
  871. sechdrnative:=MaybeSwapSecHeader(sechdr);
  872. writer.write(sechdrnative,sizeof(sechdr));
  873. {$else}
  874. writer.write(MaybeSwapSecHeader(sechdr),sizeof(sechdr));
  875. {$endif ver2_0_0}
  876. end;
  877. procedure TElfObjectOutput.writesectiondata(s:TElfObjSection);
  878. begin
  879. FWriter.writezeros(s.dataalignbytes);
  880. if s.Datapos<>FWriter.ObjSize then
  881. internalerror(200604031);
  882. FWriter.writearray(s.data);
  883. end;
  884. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  885. begin
  886. TElfObjSection(p).secshidx:=pword(arg)^;
  887. inc(pword(arg)^);
  888. end;
  889. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  890. begin
  891. if (TElfObjSection(p).ObjRelocations.count>0) then
  892. createrelocsection(TElfObjSection(p));
  893. end;
  894. procedure TElfObjectOutput.section_set_datapos(p:TObject;arg:pointer);
  895. begin
  896. TObjSection(p).setdatapos(paint(arg)^);
  897. end;
  898. procedure TElfObjectOutput.section_write_data(p:TObject;arg:pointer);
  899. begin
  900. if (oso_data in TObjSection(p).secoptions) then
  901. begin
  902. if TObjSection(p).data=nil then
  903. internalerror(200403073);
  904. writesectiondata(TElfObjSection(p));
  905. end;
  906. end;
  907. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  908. begin
  909. writesectionheader(TElfObjSection(p));
  910. end;
  911. function TElfObjectOutput.writedata(data:TObjData):boolean;
  912. var
  913. {$ifdef ver2_0_0}
  914. headernative,
  915. {$endif ver2_0_0}
  916. header : telfheader;
  917. shoffset,
  918. datapos : aint;
  919. nsections : word;
  920. begin
  921. result:=false;
  922. elf32data:=TElfObjData(data);
  923. with elf32data do
  924. begin
  925. { default sections }
  926. symtabsect:=TElfObjSection.create_ext(ObjSectionList,'.symtab',SHT_SYMTAB,0,0,0,4,sizeof(telfsymbol));
  927. strtabsect:=TElfObjSection.create_ext(ObjSectionList,'.strtab',SHT_STRTAB,0,0,0,1,0);
  928. shstrtabsect:=TElfObjSection.create_ext(ObjSectionList,'.shstrtab',SHT_STRTAB,0,0,0,1,0);
  929. { insert the empty and filename as first in strtab }
  930. strtabsect.writestr(#0);
  931. strtabsect.writestr(SplitFileName(current_module.mainsource^)+#0);
  932. { calc amount of sections we have }
  933. nsections:=1;
  934. { also create the index in the section header table }
  935. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  936. { create .symtab and .strtab }
  937. createsymtab;
  938. { Create the relocation sections, this needs valid secidx and symidx }
  939. ObjSectionList.ForEachCall(@section_create_relocsec,nil);
  940. { recalc nsections to incude the reloc sections }
  941. nsections:=1;
  942. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  943. { create .shstrtab }
  944. createshstrtab;
  945. { Calculate the filepositions }
  946. datapos:=$40; { elfheader + alignment }
  947. { section data }
  948. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  949. { section headers }
  950. shoffset:=datapos;
  951. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  952. { Write ELF Header }
  953. fillchar(header,sizeof(header),0);
  954. header.magic0123:=$464c457f; { = #127'ELF' }
  955. {$ifdef cpu64bit}
  956. header.file_class:=2;
  957. {$else cpu64bit}
  958. header.file_class:=1;
  959. {$endif cpu64bit}
  960. if target_info.endian=endian_big then
  961. header.data_encoding:=2
  962. else
  963. header.data_encoding:=1;
  964. header.file_version:=1;
  965. header.e_type:=1;
  966. {$ifdef sparc}
  967. header.e_machine:=2;
  968. {$endif sparc}
  969. {$ifdef i386}
  970. header.e_machine:=3;
  971. {$endif i386}
  972. {$ifdef m68k}
  973. header.e_machine:=4;
  974. {$endif m68k}
  975. {$ifdef powerpc}
  976. header.e_machine:=20;
  977. {$endif powerpc}
  978. {$ifdef arm}
  979. header.e_machine:=40;
  980. {$endif arm}
  981. {$ifdef x86_64}
  982. header.e_machine:=62;
  983. {$endif x86_64}
  984. header.e_version:=1;
  985. header.e_shoff:=shoffset;
  986. header.e_shstrndx:=shstrtabsect.secshidx;
  987. header.e_shnum:=nsections;
  988. header.e_ehsize:=sizeof(telfheader);
  989. header.e_shentsize:=sizeof(telfsechdr);
  990. {$ifdef ver2_0_0}
  991. headernative:=MaybeSwapHeader(header);
  992. writer.write(headernative,sizeof(header));
  993. {$else}
  994. writer.write(MaybeSwapHeader(header),sizeof(header));
  995. {$endif ver2_0_0}
  996. writer.writezeros($40-sizeof(header)); { align }
  997. { Sections }
  998. ObjSectionList.ForEachCall(@section_write_data,nil);
  999. { section headers, start with an empty header for sh_undef }
  1000. writer.writezeros(sizeof(telfsechdr));
  1001. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1002. end;
  1003. result:=true;
  1004. end;
  1005. {****************************************************************************
  1006. TELFAssembler
  1007. ****************************************************************************}
  1008. constructor TElfAssembler.Create(smart:boolean);
  1009. begin
  1010. inherited Create(smart);
  1011. CObjOutput:=TElfObjectOutput;
  1012. end;
  1013. {*****************************************************************************
  1014. Initialize
  1015. *****************************************************************************}
  1016. {$ifdef i386}
  1017. const
  1018. as_i386_elf32_info : tasminfo =
  1019. (
  1020. id : as_i386_elf32;
  1021. idtxt : 'ELF';
  1022. asmbin : '';
  1023. asmcmd : '';
  1024. supported_target : system_any; //target_i386_linux;
  1025. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1026. labelprefix : '.L';
  1027. comment : '';
  1028. );
  1029. {$endif i386}
  1030. {$ifdef x86_64}
  1031. const
  1032. as_x86_64_elf64_info : tasminfo =
  1033. (
  1034. id : as_x86_64_elf64;
  1035. idtxt : 'ELF';
  1036. asmbin : '';
  1037. asmcmd : '';
  1038. supported_target : system_any; //target_i386_linux;
  1039. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1040. labelprefix : '.L';
  1041. comment : '';
  1042. );
  1043. {$endif x86_64}
  1044. {$ifdef sparc}
  1045. const
  1046. as_sparc_elf32_info : tasminfo =
  1047. (
  1048. id : as_sparc_elf32;
  1049. idtxt : 'ELF';
  1050. asmbin : '';
  1051. asmcmd : '';
  1052. supported_target : system_any; //target_i386_linux;
  1053. // flags : [af_outputbinary,af_smartlink_sections];
  1054. flags : [af_outputbinary,af_supports_dwarf];
  1055. labelprefix : '.L';
  1056. comment : '';
  1057. );
  1058. {$endif sparc}
  1059. initialization
  1060. {$ifdef i386}
  1061. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1062. {$endif i386}
  1063. {$ifdef sparc}
  1064. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1065. {$endif sparc}
  1066. {$ifdef x86_64}
  1067. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1068. {$endif x86_64}
  1069. end.