ogelf.pas 40 KB

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