ogelf.pas 40 KB

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