ogelf.pas 38 KB

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