ogelf.pas 40 KB

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