ogelf.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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;relative: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. '.text', { darwin stubs }
  525. '.stab','.stabstr',
  526. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  527. '.eh_frame',
  528. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  529. 'fpc',
  530. ''
  531. );
  532. begin
  533. if (use_smartlink_section and
  534. (aname<>'')) or (atype=sec_fpc) then
  535. result:=secnames[atype]+'.'+aname
  536. else
  537. result:=secnames[atype];
  538. end;
  539. procedure TElfObjData.CreateDebugSections;
  540. begin
  541. if target_dbg.id=dbg_stabs then
  542. begin
  543. stabssec:=createsection(sec_stab,'');
  544. stabstrsec:=createsection(sec_stabstr,'');
  545. end;
  546. end;
  547. procedure TElfObjData.writereloc(data,len:aint;p:TObjSymbol;relative:TObjRelocationType);
  548. var
  549. symaddr : longint;
  550. begin
  551. if CurrObjSec=nil then
  552. internalerror(200403292);
  553. {$ifdef userodata}
  554. if CurrObjSec.sectype in [sec_rodata,sec_bss,sec_threadvar] then
  555. internalerror(200408252);
  556. {$endif userodata}
  557. if assigned(p) then
  558. begin
  559. { real address of the symbol }
  560. symaddr:=p.address;
  561. { Local ObjSymbols can be resolved already or need a section reloc }
  562. if (p.bind=AB_LOCAL) then
  563. begin
  564. { For a relative relocation in the same section the
  565. value can be calculated }
  566. if (p.objsection=CurrObjSec) and
  567. (relative=RELOC_RELATIVE) then
  568. inc(data,symaddr-len-CurrObjSec.Size)
  569. else
  570. begin
  571. CurrObjSec.addsectionreloc(CurrObjSec.Size,p.objsection,relative);
  572. inc(data,symaddr);
  573. end;
  574. end
  575. else
  576. begin
  577. CurrObjSec.addsymreloc(CurrObjSec.Size,p,relative);
  578. {$ifndef x86_64}
  579. if relative=RELOC_RELATIVE then
  580. dec(data,len);
  581. {$endif x86_64}
  582. end;
  583. end;
  584. CurrObjSec.write(data,len);
  585. end;
  586. procedure TElfObjData.writestab(offset:aint;ps:TObjSymbol;nidx,nother:byte;ndesc:word;p:pchar);
  587. var
  588. stab : TObjStabEntry;
  589. begin
  590. if not assigned(StabsSec) then
  591. internalerror(200602271);
  592. fillchar(stab,sizeof(TObjStabEntry),0);
  593. if assigned(p) and (p[0]<>#0) then
  594. begin
  595. stab.strpos:=stabstrsec.Size;
  596. stabstrsec.write(p^,strlen(p)+1);
  597. end;
  598. stab.ntype:=nidx;
  599. stab.ndesc:=ndesc;
  600. stab.nother:=nother;
  601. stab.nvalue:=offset;
  602. stabssec.write(stab,sizeof(stab));
  603. if assigned(ps) then
  604. stabssec.addsymreloc(stabssec.Size-4,ps,RELOC_ABSOLUTE);
  605. end;
  606. {****************************************************************************
  607. TElfObjectOutput
  608. ****************************************************************************}
  609. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  610. begin
  611. inherited Create(AWriter);
  612. CObjData:=TElfObjData;
  613. end;
  614. destructor TElfObjectOutput.destroy;
  615. begin
  616. inherited destroy;
  617. end;
  618. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection);
  619. var
  620. i : longint;
  621. {$ifdef ver2_0_0}
  622. relnative,
  623. {$endif ver2_0_0}
  624. rel : telfreloc;
  625. objreloc : TObjRelocation;
  626. relsym,
  627. reltyp : longint;
  628. relocsect : TObjSection;
  629. begin
  630. with elf32data do
  631. begin
  632. {$ifdef userodata}
  633. { rodata can't have relocations }
  634. if s.sectype=sec_rodata then
  635. begin
  636. if assigned(s.relocations.first) then
  637. internalerror(200408251);
  638. exit;
  639. end;
  640. {$endif userodata}
  641. { create the reloc section }
  642. {$ifdef i386}
  643. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  644. {$else i386}
  645. relocsect:=TElfObjSection.create_ext(ObjSectionList,'.rela'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,sizeof(TElfReloc));
  646. {$endif i386}
  647. { add the relocations }
  648. for i:=0 to s.Objrelocations.count-1 do
  649. begin
  650. objreloc:=TObjRelocation(s.Objrelocations[i]);
  651. fillchar(rel,sizeof(rel),0);
  652. rel.address:=objreloc.dataoffset;
  653. if assigned(objreloc.symbol) then
  654. begin
  655. if (objreloc.symbol.bind=AB_LOCAL) then
  656. relsym:=objreloc.symbol.objsection.secsymidx
  657. else
  658. begin
  659. if objreloc.symbol.symidx=-1 then
  660. internalerror(200603012);
  661. relsym:=objreloc.symbol.symidx;
  662. end;
  663. end
  664. else
  665. begin
  666. if objreloc.objsection<>nil then
  667. relsym:=objreloc.objsection.secsymidx
  668. else
  669. relsym:=SHN_UNDEF;
  670. end;
  671. { when things settle down, we can create processor specific
  672. derived classes }
  673. case objreloc.typ of
  674. {$ifdef i386}
  675. RELOC_RELATIVE :
  676. reltyp:=R_386_PC32;
  677. RELOC_ABSOLUTE :
  678. reltyp:=R_386_32;
  679. {$endif i386}
  680. {$ifdef sparc}
  681. RELOC_ABSOLUTE :
  682. reltyp:=R_SPARC_32;
  683. {$endif sparc}
  684. {$ifdef x86_64}
  685. RELOC_RELATIVE :
  686. begin
  687. reltyp:=R_X86_64_PC32;
  688. { length of the relocated location is handled here }
  689. rel.addend:=qword(-4);
  690. end;
  691. RELOC_ABSOLUTE :
  692. reltyp:=R_X86_64_64;
  693. RELOC_ABSOLUTE32 :
  694. reltyp:=R_X86_64_32S;
  695. {$endif x86_64}
  696. else
  697. internalerror(200602261);
  698. end;
  699. {$ifdef cpu64bit}
  700. rel.info:=(qword(relsym) shl 32) or reltyp;
  701. {$else cpu64bit}
  702. rel.info:=(relsym shl 8) or reltyp;
  703. {$endif cpu64bit}
  704. { write reloc }
  705. {$ifdef ver2_0_0}
  706. relnative:=MaybeSwapElfReloc(rel);
  707. relocsect.write(relnative,sizeof(rel));
  708. {$else}
  709. relocsect.write(MaybeSwapElfReloc(rel),sizeof(rel));
  710. {$endif ver2_0_0}
  711. end;
  712. end;
  713. end;
  714. procedure TElfObjectOutput.write_internal_symbol(astridx:longint;ainfo:byte;ashndx:word);
  715. var
  716. {$ifdef ver2_0_0}
  717. elfsymnative,
  718. {$endif ver2_0_0}
  719. elfsym : telfsymbol;
  720. begin
  721. fillchar(elfsym,sizeof(elfsym),0);
  722. elfsym.st_name:=astridx;
  723. elfsym.st_info:=ainfo;
  724. elfsym.st_shndx:=ashndx;
  725. inc(symidx);
  726. inc(localsyms);
  727. {$ifdef ver2_0_0}
  728. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  729. symtabsect.write(elfsymnative,sizeof(elfsym));
  730. {$else}
  731. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  732. {$endif ver2_0_0}
  733. end;
  734. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  735. begin
  736. TObjSection(p).secsymidx:=symidx;
  737. write_internal_symbol(TElfObjSection(p).shstridx,STT_SECTION,TElfObjSection(p).secshidx);
  738. end;
  739. procedure TElfObjectOutput.createsymtab;
  740. procedure WriteSym(objsym:TObjSymbol);
  741. var
  742. {$ifdef ver2_0_0}
  743. elfsymnative,
  744. {$endif}
  745. elfsym : telfsymbol;
  746. begin
  747. with elf32data do
  748. begin
  749. fillchar(elfsym,sizeof(elfsym),0);
  750. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  751. elfsym.st_name:=strtabsect.Size;
  752. strtabsect.writestr(objsym.name);
  753. strtabsect.writestr(#0);
  754. elfsym.st_size:=objsym.size;
  755. case objsym.bind of
  756. AB_LOCAL :
  757. begin
  758. elfsym.st_value:=objsym.address;
  759. elfsym.st_info:=STB_LOCAL shl 4;
  760. inc(localsyms);
  761. end;
  762. AB_COMMON :
  763. begin
  764. elfsym.st_value:=$10;
  765. elfsym.st_info:=STB_GLOBAL shl 4;
  766. end;
  767. AB_EXTERNAL :
  768. elfsym.st_info:=STB_GLOBAL shl 4;
  769. AB_GLOBAL :
  770. begin
  771. elfsym.st_value:=objsym.address;
  772. elfsym.st_info:=STB_GLOBAL shl 4;
  773. end;
  774. end;
  775. if (objsym.bind<>AB_EXTERNAL) {and
  776. not(assigned(objsym.objsection) and
  777. not(oso_data in objsym.objsection.secoptions))} then
  778. begin
  779. case objsym.typ of
  780. AT_FUNCTION :
  781. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  782. AT_DATA :
  783. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  784. end;
  785. end;
  786. if objsym.bind=AB_COMMON then
  787. elfsym.st_shndx:=SHN_COMMON
  788. else
  789. begin
  790. if assigned(objsym.objsection) then
  791. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  792. else
  793. elfsym.st_shndx:=SHN_UNDEF;
  794. end;
  795. objsym.symidx:=symidx;
  796. inc(symidx);
  797. {$ifdef ver2_0_0}
  798. elfsymnative:=MaybeSwapElfSymbol(elfsym);
  799. symtabsect.write(elfsymnative,sizeof(elfsym));
  800. {$else}
  801. symtabsect.write(MaybeSwapElfSymbol(elfsym),sizeof(elfsym));
  802. {$endif ver2_0_0}
  803. end;
  804. end;
  805. var
  806. i : longint;
  807. objsym : TObjSymbol;
  808. begin
  809. with elf32data do
  810. begin
  811. symidx:=0;
  812. localsyms:=0;
  813. { empty entry }
  814. write_internal_symbol(0,0,0);
  815. { filename entry }
  816. write_internal_symbol(1,STT_FILE,SHN_ABS);
  817. { section }
  818. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  819. { First the Local Symbols, this is required by ELF. The localsyms
  820. count stored in shinfo is used to skip the local symbols
  821. when traversing the symtab }
  822. for i:=0 to ObjSymbolList.Count-1 do
  823. begin
  824. objsym:=TObjSymbol(ObjSymbolList[i]);
  825. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  826. WriteSym(objsym);
  827. end;
  828. { Global Symbols }
  829. for i:=0 to ObjSymbolList.Count-1 do
  830. begin
  831. objsym:=TObjSymbol(ObjSymbolList[i]);
  832. if (objsym.bind<>AB_LOCAL) then
  833. WriteSym(objsym);
  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:=shstrtabsect.writestr(TObjSection(p).name+#0);
  843. end;
  844. procedure TElfObjectOutput.createshstrtab;
  845. begin
  846. with elf32data do
  847. begin
  848. shstrtabsect.writestr(#0);
  849. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  850. end;
  851. end;
  852. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  853. var
  854. {$ifdef ver2_0_0}
  855. sechdrnative,
  856. {$endif ver2_0_0}
  857. sechdr : telfsechdr;
  858. begin
  859. fillchar(sechdr,sizeof(sechdr),0);
  860. sechdr.sh_name:=s.shstridx;
  861. sechdr.sh_type:=s.shtype;
  862. sechdr.sh_flags:=s.shflags;
  863. sechdr.sh_offset:=s.datapos;
  864. sechdr.sh_size:=s.Size;
  865. sechdr.sh_link:=s.shlink;
  866. sechdr.sh_info:=s.shinfo;
  867. sechdr.sh_addralign:=s.secalign;
  868. sechdr.sh_entsize:=s.shentsize;
  869. {$ifdef ver2_0_0}
  870. sechdrnative:=MaybeSwapSecHeader(sechdr);
  871. writer.write(sechdrnative,sizeof(sechdr));
  872. {$else}
  873. writer.write(MaybeSwapSecHeader(sechdr),sizeof(sechdr));
  874. {$endif ver2_0_0}
  875. end;
  876. procedure TElfObjectOutput.writesectiondata(s:TElfObjSection);
  877. begin
  878. FWriter.writezeros(s.dataalignbytes);
  879. if s.Datapos<>FWriter.ObjSize then
  880. internalerror(200604031);
  881. FWriter.writearray(s.data);
  882. end;
  883. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  884. begin
  885. TElfObjSection(p).secshidx:=pword(arg)^;
  886. inc(pword(arg)^);
  887. end;
  888. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  889. begin
  890. if (TElfObjSection(p).ObjRelocations.count>0) then
  891. createrelocsection(TElfObjSection(p));
  892. end;
  893. procedure TElfObjectOutput.section_set_datapos(p:TObject;arg:pointer);
  894. begin
  895. TObjSection(p).setdatapos(paint(arg)^);
  896. end;
  897. procedure TElfObjectOutput.section_write_data(p:TObject;arg:pointer);
  898. begin
  899. if (oso_data in TObjSection(p).secoptions) then
  900. begin
  901. if TObjSection(p).data=nil then
  902. internalerror(200403073);
  903. writesectiondata(TElfObjSection(p));
  904. end;
  905. end;
  906. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  907. begin
  908. writesectionheader(TElfObjSection(p));
  909. end;
  910. function TElfObjectOutput.writedata(data:TObjData):boolean;
  911. var
  912. {$ifdef ver2_0_0}
  913. headernative,
  914. {$endif ver2_0_0}
  915. header : telfheader;
  916. shoffset,
  917. datapos : aint;
  918. nsections : word;
  919. begin
  920. result:=false;
  921. elf32data:=TElfObjData(data);
  922. with elf32data do
  923. begin
  924. { default sections }
  925. symtabsect:=TElfObjSection.create_ext(ObjSectionList,'.symtab',SHT_SYMTAB,0,0,0,4,sizeof(telfsymbol));
  926. strtabsect:=TElfObjSection.create_ext(ObjSectionList,'.strtab',SHT_STRTAB,0,0,0,1,0);
  927. shstrtabsect:=TElfObjSection.create_ext(ObjSectionList,'.shstrtab',SHT_STRTAB,0,0,0,1,0);
  928. { insert the empty and filename as first in strtab }
  929. strtabsect.writestr(#0);
  930. strtabsect.writestr(SplitFileName(current_module.mainsource^)+#0);
  931. { calc amount of sections we have }
  932. nsections:=1;
  933. { also create the index in the section header table }
  934. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  935. { create .symtab and .strtab }
  936. createsymtab;
  937. { Create the relocation sections, this needs valid secidx and symidx }
  938. ObjSectionList.ForEachCall(@section_create_relocsec,nil);
  939. { recalc nsections to incude the reloc sections }
  940. nsections:=1;
  941. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  942. { create .shstrtab }
  943. createshstrtab;
  944. { Calculate the filepositions }
  945. datapos:=$40; { elfheader + alignment }
  946. { section data }
  947. ObjSectionList.ForEachCall(@section_set_datapos,@datapos);
  948. { section headers }
  949. shoffset:=datapos;
  950. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  951. { Write ELF Header }
  952. fillchar(header,sizeof(header),0);
  953. header.magic0123:=$464c457f; { = #127'ELF' }
  954. {$ifdef cpu64bit}
  955. header.file_class:=2;
  956. {$else cpu64bit}
  957. header.file_class:=1;
  958. {$endif cpu64bit}
  959. if target_info.endian=endian_big then
  960. header.data_encoding:=2
  961. else
  962. header.data_encoding:=1;
  963. header.file_version:=1;
  964. header.e_type:=1;
  965. {$ifdef sparc}
  966. header.e_machine:=2;
  967. {$endif sparc}
  968. {$ifdef i386}
  969. header.e_machine:=3;
  970. {$endif i386}
  971. {$ifdef m68k}
  972. header.e_machine:=4;
  973. {$endif m68k}
  974. {$ifdef powerpc}
  975. header.e_machine:=20;
  976. {$endif powerpc}
  977. {$ifdef arm}
  978. header.e_machine:=40;
  979. {$endif arm}
  980. {$ifdef x86_64}
  981. header.e_machine:=62;
  982. {$endif x86_64}
  983. header.e_version:=1;
  984. header.e_shoff:=shoffset;
  985. header.e_shstrndx:=shstrtabsect.secshidx;
  986. header.e_shnum:=nsections;
  987. header.e_ehsize:=sizeof(telfheader);
  988. header.e_shentsize:=sizeof(telfsechdr);
  989. {$ifdef ver2_0_0}
  990. headernative:=MaybeSwapHeader(header);
  991. writer.write(headernative,sizeof(header));
  992. {$else}
  993. writer.write(MaybeSwapHeader(header),sizeof(header));
  994. {$endif ver2_0_0}
  995. writer.writezeros($40-sizeof(header)); { align }
  996. { Sections }
  997. ObjSectionList.ForEachCall(@section_write_data,nil);
  998. { section headers, start with an empty header for sh_undef }
  999. writer.writezeros(sizeof(telfsechdr));
  1000. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1001. end;
  1002. result:=true;
  1003. end;
  1004. {****************************************************************************
  1005. TELFAssembler
  1006. ****************************************************************************}
  1007. constructor TElfAssembler.Create(smart:boolean);
  1008. begin
  1009. inherited Create(smart);
  1010. CObjOutput:=TElfObjectOutput;
  1011. end;
  1012. {*****************************************************************************
  1013. Initialize
  1014. *****************************************************************************}
  1015. {$ifdef i386}
  1016. const
  1017. as_i386_elf32_info : tasminfo =
  1018. (
  1019. id : as_i386_elf32;
  1020. idtxt : 'ELF';
  1021. asmbin : '';
  1022. asmcmd : '';
  1023. supported_target : system_any; //target_i386_linux;
  1024. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1025. labelprefix : '.L';
  1026. comment : '';
  1027. );
  1028. {$endif i386}
  1029. {$ifdef x86_64}
  1030. const
  1031. as_x86_64_elf64_info : tasminfo =
  1032. (
  1033. id : as_x86_64_elf64;
  1034. idtxt : 'ELF';
  1035. asmbin : '';
  1036. asmcmd : '';
  1037. supported_target : system_any; //target_i386_linux;
  1038. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1039. labelprefix : '.L';
  1040. comment : '';
  1041. );
  1042. {$endif x86_64}
  1043. {$ifdef sparc}
  1044. const
  1045. as_sparc_elf32_info : tasminfo =
  1046. (
  1047. id : as_sparc_elf32;
  1048. idtxt : 'ELF';
  1049. asmbin : '';
  1050. asmcmd : '';
  1051. supported_target : system_any; //target_i386_linux;
  1052. // flags : [af_outputbinary,af_smartlink_sections];
  1053. flags : [af_outputbinary,af_supports_dwarf];
  1054. labelprefix : '.L';
  1055. comment : '';
  1056. );
  1057. {$endif sparc}
  1058. initialization
  1059. {$ifdef i386}
  1060. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1061. {$endif i386}
  1062. {$ifdef sparc}
  1063. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1064. {$endif sparc}
  1065. {$ifdef x86_64}
  1066. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1067. {$endif x86_64}
  1068. end.