ogelf.pas 39 KB

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