ogelf.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  42. end;
  43. TElfSymtabKind = (esk_obj,esk_exe,esk_dyn);
  44. TElfSymtab = class(TElfObjSection)
  45. public
  46. kind: TElfSymtabKind;
  47. fstrsec: TObjSection;
  48. symidx: longint;
  49. constructor create(aObjData:TObjData;aKind:TElfSymtabKind);reintroduce;
  50. procedure writeSymbol(objsym:TObjSymbol);
  51. procedure writeInternalSymbol(astridx:longint;ainfo:byte;ashndx:word);
  52. end;
  53. TElfObjData = class(TObjData)
  54. public
  55. constructor create(const n:string);override;
  56. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  57. procedure CreateDebugSections;override;
  58. procedure writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);override;
  59. end;
  60. TElfObjectOutput = class(tObjOutput)
  61. private
  62. symtabsect: TElfSymtab;
  63. shstrtabsect: TElfObjSection;
  64. procedure createrelocsection(s:TElfObjSection;data:TObjData);
  65. procedure createshstrtab(data:TObjData);
  66. procedure createsymtab(data: TObjData);
  67. procedure writesectionheader(s:TElfObjSection);
  68. procedure section_write_symbol(p:TObject;arg:pointer);
  69. procedure section_write_sh_string(p:TObject;arg:pointer);
  70. procedure section_count_sections(p:TObject;arg:pointer);
  71. procedure section_create_relocsec(p:TObject;arg:pointer);
  72. procedure section_write_sechdr(p:TObject;arg:pointer);
  73. protected
  74. function writedata(data:TObjData):boolean;override;
  75. public
  76. constructor Create(AWriter:TObjectWriter);override;
  77. end;
  78. TElfAssembler = class(tinternalassembler)
  79. constructor create(smart:boolean);override;
  80. end;
  81. implementation
  82. uses
  83. SysUtils,
  84. verbose,
  85. cutils,globals,fmodule;
  86. const
  87. symbolresize = 200*18;
  88. const
  89. { Relocation types }
  90. {$ifdef i386}
  91. R_386_32 = 1; { ordinary absolute relocation }
  92. R_386_PC32 = 2; { PC-relative relocation }
  93. R_386_GOT32 = 3; { an offset into GOT }
  94. R_386_PLT32 = 4; { a PC-relative offset into PLT }
  95. R_386_GOTOFF = 9; { an offset from GOT base }
  96. R_386_GOTPC = 10; { a PC-relative offset _to_ GOT }
  97. R_386_GNU_VTINHERIT = 250;
  98. R_386_GNU_VTENTRY = 251;
  99. {$endif i386}
  100. {$ifdef sparc}
  101. R_SPARC_32 = 3;
  102. R_SPARC_WDISP30 = 7;
  103. R_SPARC_HI22 = 9;
  104. R_SPARC_LO10 = 12;
  105. R_SPARC_GNU_VTINHERIT = 250;
  106. R_SPARC_GNU_VTENTRY = 251;
  107. {$endif sparc}
  108. {$ifdef x86_64}
  109. R_X86_64_NONE = 0;
  110. R_X86_64_64 = 1; { Direct 64 bit }
  111. R_X86_64_PC32 = 2; { PC relative 32 bit signed }
  112. R_X86_64_GOT32 = 3; { 32 bit GOT entry }
  113. R_X86_64_PLT32 = 4; { 32 bit PLT address }
  114. R_X86_64_COPY = 5; { Copy symbol at runtime }
  115. R_X86_64_GLOB_DAT = 6; { Create GOT entry }
  116. R_X86_64_JUMP_SLOT = 7; { Create PLT entry }
  117. R_X86_64_RELATIVE = 8; { Adjust by program base }
  118. R_X86_64_GOTPCREL = 9; { 32 bit signed PC relative offset to GOT }
  119. R_X86_64_32 = 10; { Direct 32 bit zero extended }
  120. R_X86_64_32S = 11; { Direct 32 bit sign extended }
  121. R_X86_64_16 = 12; { Direct 16 bit zero extended }
  122. R_X86_64_PC16 = 13; { 16 bit sign extended PC relative }
  123. R_X86_64_8 = 14; { Direct 8 bit sign extended }
  124. R_X86_64_PC8 = 15; { 8 bit sign extended PC relative }
  125. R_X86_64_DTPMOD64 = 16; { ID of module containing symbol }
  126. R_X86_64_DTPOFF64 = 17; { Offset in module's TLS block }
  127. R_X86_64_TPOFF64 = 18; { Offset in initial TLS block }
  128. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  129. R_X86_64_TLSGD = 19;
  130. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  131. R_X86_64_TLSLD = 20;
  132. R_X86_64_DTPOFF32 = 21; { Offset in TLS block }
  133. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  134. R_X86_64_GOTTPOFF = 22;
  135. R_X86_64_TPOFF32 = 23; { Offset in initial TLS block }
  136. R_X86_64_PC64 = 24; { PC relative 64-bit signed }
  137. R_X86_64_GOTOFF64 = 25; { 64-bit offset from GOT base }
  138. R_X86_64_GOTPC32 = 26; { PC-relative offset GOT }
  139. R_X86_64_GOT64 = 27; { 64-bit GOT entry offset }
  140. R_X86_64_GOTPCREL64 = 28; { 64-bit PC relative offset to GOT entry }
  141. R_X86_64_GOTPC64 = 29; { 64-bit PC relative offset to GOT }
  142. R_X86_64_GOTPLT64 = 30; { Like GOT64, indicates that PLT entry needed }
  143. R_X86_64_PLTOFF64 = 31; { 64-bit GOT relative offset to PLT entry }
  144. R_X86_64_SIZE32 = 32;
  145. R_X86_64_SIZE64 = 33;
  146. R_X86_64_GOTPC32_TLSDESC = 34;
  147. R_X86_64_TLSDESC_CALL = 35;
  148. R_X86_64_TLSDESC = 36;
  149. R_X86_64_IRELATIVE = 37;
  150. R_X86_64_GNU_VTINHERIT = 250; { GNU extension to record C++ vtable hierarchy }
  151. R_X86_64_GNU_VTENTRY = 251; { GNU extension to record C++ vtable member usage }
  152. {$endif x86_64}
  153. { ELFHeader.file_class }
  154. ELFCLASSNONE = 0;
  155. ELFCLASS32 = 1;
  156. ELFCLASS64 = 2;
  157. { ELFHeader.e_type }
  158. ET_NONE = 0;
  159. ET_REL = 1;
  160. ET_EXEC = 2;
  161. ET_DYN = 3;
  162. ET_CORE = 4;
  163. { ELFHeader.e_machine }
  164. EM_SPARC = 2;
  165. EM_386 = 3;
  166. EM_M68K = 4;
  167. EM_PPC = 20;
  168. EM_ARM = 40;
  169. EM_X86_64 = 62;
  170. {$ifdef sparc}
  171. ELFMACHINE = EM_SPARC;
  172. {$endif sparc}
  173. {$ifdef i386}
  174. ELFMACHINE = EM_386;
  175. {$endif i386}
  176. {$ifdef m68k}
  177. ELFMACHINE = EM_M68K;
  178. {$endif m68k}
  179. {$ifdef powerpc}
  180. ELFMACHINE = EM_PPC;
  181. {$endif powerpc}
  182. {$ifdef arm}
  183. ELFMACHINE = EM_ARM;
  184. {$endif arm}
  185. {$ifdef x86_64}
  186. ELFMACHINE = EM_X86_64;
  187. {$endif x86_64}
  188. SHN_UNDEF = 0;
  189. SHN_ABS = $fff1;
  190. SHN_COMMON = $fff2;
  191. SHT_NULL = 0;
  192. SHT_PROGBITS = 1;
  193. SHT_SYMTAB = 2;
  194. SHT_STRTAB = 3;
  195. SHT_RELA = 4;
  196. SHT_HASH = 5;
  197. SHT_DYNAMIC = 6;
  198. SHT_NOTE = 7;
  199. SHT_NOBITS = 8;
  200. SHT_REL = 9;
  201. SHT_SHLIB = 10;
  202. SHT_DYNSYM = 11;
  203. SHF_WRITE = 1;
  204. SHF_ALLOC = 2;
  205. SHF_EXECINSTR = 4;
  206. STB_LOCAL = 0;
  207. STB_GLOBAL = 1;
  208. STB_WEAK = 2;
  209. STT_NOTYPE = 0;
  210. STT_OBJECT = 1;
  211. STT_FUNC = 2;
  212. STT_SECTION = 3;
  213. STT_FILE = 4;
  214. { program header types }
  215. PT_NULL = 0;
  216. PT_LOAD = 1;
  217. PT_DYNAMIC = 2;
  218. PT_INTERP = 3;
  219. PT_NOTE = 4;
  220. PT_SHLIB = 5;
  221. PT_PHDR = 6;
  222. PT_LOPROC = $70000000;
  223. PT_HIPROC = $7FFFFFFF;
  224. { program header flags }
  225. PF_X = 1;
  226. PF_W = 2;
  227. PF_R = 4;
  228. PF_MASKPROC = $F0000000;
  229. { .dynamic tags }
  230. DT_NULL = 0;
  231. DT_NEEDED = 1;
  232. DT_PLTRELSZ = 2;
  233. DT_PLTGOT = 3;
  234. DT_HASH = 4;
  235. DT_STRTAB = 5;
  236. DT_SYMTAB = 6;
  237. DT_RELA = 7;
  238. DT_RELASZ = 8;
  239. DT_RELAENT = 9;
  240. DT_STRSZ = 10;
  241. DT_SYMENT = 11;
  242. DT_INIT = 12;
  243. DT_FINI = 13;
  244. DT_SONAME = 14;
  245. DT_RPATH = 15;
  246. DT_SYMBOLIC = 16;
  247. DT_REL = 17;
  248. DT_RELSZ = 18;
  249. DT_RELENT = 19;
  250. DT_PLTREL = 20;
  251. DT_DEBUG = 21;
  252. DT_TEXTREL = 22;
  253. DT_JMPREL = 23;
  254. DT_BIND_NOW = 24;
  255. DT_INIT_ARRAY = 25;
  256. DT_FINI_ARRAY = 26;
  257. DT_INIT_ARRAYSZ = 27;
  258. DT_FINI_ARRAYSZ = 28;
  259. DT_RUNPATH = 29;
  260. DT_FLAGS = 30;
  261. DT_ENCODING = 32;
  262. DT_PREINIT_ARRAY = 32;
  263. DT_PREINIT_ARRAYSZ = 33;
  264. DT_NUM = 34;
  265. DT_LOOS = $6000000D;
  266. DT_HIOS = $6ffff000;
  267. DT_LOPROC = $70000000;
  268. DT_HIPROC = $7fffffff;
  269. type
  270. { Structures which are written directly to the output file }
  271. TElf32header=packed record
  272. magic : array[0..3] of byte;
  273. file_class : byte;
  274. data_encoding : byte;
  275. file_version : byte;
  276. padding : array[$07..$0f] of byte;
  277. e_type : word;
  278. e_machine : word;
  279. e_version : longint;
  280. e_entry : longint; { entrypoint }
  281. e_phoff : longint; { program header offset }
  282. e_shoff : longint; { sections header offset }
  283. e_flags : longint;
  284. e_ehsize : word; { elf header size in bytes }
  285. e_phentsize : word; { size of an entry in the program header array }
  286. e_phnum : word; { 0..e_phnum-1 of entrys }
  287. e_shentsize : word; { size of an entry in sections header array }
  288. e_shnum : word; { 0..e_shnum-1 of entrys }
  289. e_shstrndx : word; { index of string section header }
  290. end;
  291. TElf32sechdr=packed record
  292. sh_name : longint;
  293. sh_type : longint;
  294. sh_flags : longint;
  295. sh_addr : longint;
  296. sh_offset : longint;
  297. sh_size : longint;
  298. sh_link : longint;
  299. sh_info : longint;
  300. sh_addralign : longint;
  301. sh_entsize : longint;
  302. end;
  303. TElf32proghdr=packed record
  304. p_type : longword;
  305. p_offset : longword;
  306. p_vaddr : longword;
  307. p_paddr : longword;
  308. p_filesz : longword;
  309. p_memsz : longword;
  310. p_flags : longword;
  311. p_align : longword;
  312. end;
  313. TElf32reloc=packed record
  314. address : longint;
  315. info : longint; { bit 0-7: type, 8-31: symbol }
  316. addend : longint;
  317. end;
  318. TElf32symbol=packed record
  319. st_name : longint;
  320. st_value : longint;
  321. st_size : longint;
  322. st_info : byte; { bit 0-3: type, 4-7: bind }
  323. st_other : byte;
  324. st_shndx : word;
  325. end;
  326. TElf32Dyn=packed record
  327. d_tag: longword;
  328. case integer of
  329. 0: (d_val: longword);
  330. 1: (d_ptr: longword);
  331. end;
  332. telf64header=packed record
  333. magic : array[0..3] of byte;
  334. file_class : byte;
  335. data_encoding : byte;
  336. file_version : byte;
  337. padding : array[$07..$0f] of byte;
  338. e_type : word;
  339. e_machine : word;
  340. e_version : longint;
  341. e_entry : qword; { entrypoint }
  342. e_phoff : qword; { program header offset }
  343. e_shoff : qword; { sections header offset }
  344. e_flags : longint;
  345. e_ehsize : word; { elf header size in bytes }
  346. e_phentsize : word; { size of an entry in the program header array }
  347. e_phnum : word; { 0..e_phnum-1 of entrys }
  348. e_shentsize : word; { size of an entry in sections header array }
  349. e_shnum : word; { 0..e_shnum-1 of entrys }
  350. e_shstrndx : word; { index of string section header }
  351. end;
  352. telf64sechdr=packed record
  353. sh_name : longint;
  354. sh_type : longint;
  355. sh_flags : qword;
  356. sh_addr : qword;
  357. sh_offset : qword;
  358. sh_size : qword;
  359. sh_link : longint;
  360. sh_info : longint;
  361. sh_addralign : qword;
  362. sh_entsize : qword;
  363. end;
  364. telf64proghdr=packed record
  365. p_type : longword;
  366. p_flags : longword;
  367. p_offset : qword;
  368. p_vaddr : qword;
  369. p_paddr : qword;
  370. p_filesz : qword;
  371. p_memsz : qword;
  372. p_align : qword;
  373. end;
  374. telf64reloc=packed record
  375. address : qword;
  376. info : qword; { bit 0-31: type, 32-63: symbol }
  377. addend : int64; { signed! }
  378. end;
  379. telf64symbol=packed record
  380. st_name : longint;
  381. st_info : byte; { bit 0-3: type, 4-7: bind }
  382. st_other : byte;
  383. st_shndx : word;
  384. st_value : qword;
  385. st_size : qword;
  386. end;
  387. TElf64Dyn=packed record
  388. d_tag: qword;
  389. case integer of
  390. 0: (d_val: qword);
  391. 1: (d_ptr: qword);
  392. end;
  393. {$ifdef cpu64bitaddr}
  394. const
  395. ELFCLASS = ELFCLASS64;
  396. type
  397. telfheader = telf64header;
  398. telfreloc = telf64reloc;
  399. telfsymbol = telf64symbol;
  400. telfsechdr = telf64sechdr;
  401. telfproghdr = telf64proghdr;
  402. telfdyn = telf64dyn;
  403. {$else cpu64bitaddr}
  404. const
  405. ELFCLASS = ELFCLASS32;
  406. type
  407. telfheader = telf32header;
  408. telfreloc = telf32reloc;
  409. telfsymbol = telf32symbol;
  410. telfsechdr = telf32sechdr;
  411. telfproghdr = telf32proghdr;
  412. telfdyn = telf32dyn;
  413. {$endif cpu64bitaddr}
  414. {$ifdef x86_64}
  415. const
  416. relocs_use_addend:Boolean=True;
  417. {$else x86_64}
  418. const
  419. relocs_use_addend:Boolean=False;
  420. {$endif x86_64}
  421. procedure MayBeSwapHeader(var h : telf32header);
  422. begin
  423. if source_info.endian<>target_info.endian then
  424. with h do
  425. begin
  426. e_type:=swapendian(e_type);
  427. e_machine:=swapendian(e_machine);
  428. e_version:=swapendian(e_version);
  429. e_entry:=swapendian(e_entry);
  430. e_phoff:=swapendian(e_phoff);
  431. e_shoff:=swapendian(e_shoff);
  432. e_flags:=swapendian(e_flags);
  433. e_ehsize:=swapendian(e_ehsize);
  434. e_phentsize:=swapendian(e_phentsize);
  435. e_phnum:=swapendian(e_phnum);
  436. e_shentsize:=swapendian(e_shentsize);
  437. e_shnum:=swapendian(e_shnum);
  438. e_shstrndx:=swapendian(e_shstrndx);
  439. end;
  440. end;
  441. procedure MayBeSwapHeader(var h : telf64header);
  442. begin
  443. if source_info.endian<>target_info.endian then
  444. with h do
  445. begin
  446. e_type:=swapendian(e_type);
  447. e_machine:=swapendian(e_machine);
  448. e_version:=swapendian(e_version);
  449. e_entry:=swapendian(e_entry);
  450. e_phoff:=swapendian(e_phoff);
  451. e_shoff:=swapendian(e_shoff);
  452. e_flags:=swapendian(e_flags);
  453. e_ehsize:=swapendian(e_ehsize);
  454. e_phentsize:=swapendian(e_phentsize);
  455. e_phnum:=swapendian(e_phnum);
  456. e_shentsize:=swapendian(e_shentsize);
  457. e_shnum:=swapendian(e_shnum);
  458. e_shstrndx:=swapendian(e_shstrndx);
  459. end;
  460. end;
  461. procedure MayBeSwapHeader(var h : telf32proghdr);
  462. begin
  463. if source_info.endian<>target_info.endian then
  464. with h do
  465. begin
  466. p_align:=swapendian(p_align);
  467. p_filesz:=swapendian(p_filesz);
  468. p_flags:=swapendian(p_flags);
  469. p_memsz:=swapendian(p_memsz);
  470. p_offset:=swapendian(p_offset);
  471. p_paddr:=swapendian(p_paddr);
  472. p_type:=swapendian(p_type);
  473. p_vaddr:=swapendian(p_vaddr);
  474. end;
  475. end;
  476. procedure MayBeSwapHeader(var h : telf64proghdr);
  477. begin
  478. if source_info.endian<>target_info.endian then
  479. with h do
  480. begin
  481. p_align:=swapendian(p_align);
  482. p_filesz:=swapendian(p_filesz);
  483. p_flags:=swapendian(p_flags);
  484. p_memsz:=swapendian(p_memsz);
  485. p_offset:=swapendian(p_offset);
  486. p_paddr:=swapendian(p_paddr);
  487. p_type:=swapendian(p_type);
  488. p_vaddr:=swapendian(p_vaddr);
  489. end;
  490. end;
  491. procedure MaybeSwapSecHeader(var h : telf32sechdr);
  492. begin
  493. if source_info.endian<>target_info.endian then
  494. with h do
  495. begin
  496. sh_name:=swapendian(sh_name);
  497. sh_type:=swapendian(sh_type);
  498. sh_flags:=swapendian(sh_flags);
  499. sh_addr:=swapendian(sh_addr);
  500. sh_offset:=swapendian(sh_offset);
  501. sh_size:=swapendian(sh_size);
  502. sh_link:=swapendian(sh_link);
  503. sh_info:=swapendian(sh_info);
  504. sh_addralign:=swapendian(sh_addralign);
  505. sh_entsize:=swapendian(sh_entsize);
  506. end;
  507. end;
  508. procedure MaybeSwapSecHeader(var h : telf64sechdr);
  509. begin
  510. if source_info.endian<>target_info.endian then
  511. with h do
  512. begin
  513. sh_name:=swapendian(sh_name);
  514. sh_type:=swapendian(sh_type);
  515. sh_flags:=swapendian(sh_flags);
  516. sh_addr:=swapendian(sh_addr);
  517. sh_offset:=swapendian(sh_offset);
  518. sh_size:=swapendian(sh_size);
  519. sh_link:=swapendian(sh_link);
  520. sh_info:=swapendian(sh_info);
  521. sh_addralign:=swapendian(sh_addralign);
  522. sh_entsize:=swapendian(sh_entsize);
  523. end;
  524. end;
  525. procedure MaybeSwapElfSymbol(var h : telf32symbol);
  526. begin
  527. if source_info.endian<>target_info.endian then
  528. with h do
  529. begin
  530. st_name:=swapendian(st_name);
  531. st_value:=swapendian(st_value);
  532. st_size:=swapendian(st_size);
  533. st_shndx:=swapendian(st_shndx);
  534. end;
  535. end;
  536. procedure MaybeSwapElfSymbol(var h : telf64symbol);
  537. begin
  538. if source_info.endian<>target_info.endian then
  539. with h do
  540. begin
  541. st_name:=swapendian(st_name);
  542. st_value:=swapendian(st_value);
  543. st_size:=swapendian(st_size);
  544. st_shndx:=swapendian(st_shndx);
  545. end;
  546. end;
  547. procedure MaybeSwapElfReloc(var h : telf32reloc);
  548. begin
  549. if source_info.endian<>target_info.endian then
  550. with h do
  551. begin
  552. address:=swapendian(address);
  553. info:=swapendian(info);
  554. addend:=swapendian(addend);
  555. end;
  556. end;
  557. procedure MaybeSwapElfReloc(var h : telf64reloc);
  558. begin
  559. if source_info.endian<>target_info.endian then
  560. with h do
  561. begin
  562. address:=swapendian(address);
  563. info:=swapendian(info);
  564. addend:=swapendian(addend);
  565. end;
  566. end;
  567. procedure MaybeSwapElfDyn(var h : telf32dyn);
  568. begin
  569. if source_info.endian<>target_info.endian then
  570. with h do
  571. begin
  572. d_tag:=swapendian(d_tag);
  573. d_val:=swapendian(d_val);
  574. end;
  575. end;
  576. procedure MaybeSwapElfDyn(var h : telf64dyn);
  577. begin
  578. if source_info.endian<>target_info.endian then
  579. with h do
  580. begin
  581. d_tag:=swapendian(d_tag);
  582. d_val:=swapendian(d_val);
  583. end;
  584. end;
  585. {****************************************************************************
  586. Helpers
  587. ****************************************************************************}
  588. procedure encodesechdrflags(aoptions:TObjSectionOptions;out AshType:longint;out Ashflags:longint);
  589. begin
  590. { Section Type }
  591. AshType:=SHT_PROGBITS;
  592. if oso_strings in aoptions then
  593. AshType:=SHT_STRTAB
  594. else if not(oso_data in aoptions) then
  595. AshType:=SHT_NOBITS;
  596. { Section Flags }
  597. Ashflags:=0;
  598. if oso_load in aoptions then
  599. Ashflags:=Ashflags or SHF_ALLOC;
  600. if oso_executable in aoptions then
  601. Ashflags:=Ashflags or SHF_EXECINSTR;
  602. if oso_write in aoptions then
  603. Ashflags:=Ashflags or SHF_WRITE;
  604. end;
  605. procedure decodesechdrflags(AshType:longint;Ashflags:longint;out aoptions:TObjSectionOptions);
  606. begin
  607. aoptions:=[];
  608. { Section Type }
  609. if AshType<>SHT_NOBITS then
  610. include(aoptions,oso_data);
  611. if AshType=SHT_STRTAB then
  612. include(aoptions,oso_strings);
  613. { Section Flags }
  614. if Ashflags and SHF_ALLOC<>0 then
  615. include(aoptions,oso_load)
  616. else
  617. include(aoptions,oso_noload);
  618. if Ashflags and SHF_WRITE<>0 then
  619. include(aoptions,oso_write)
  620. else
  621. include(aoptions,oso_readonly);
  622. if Ashflags and SHF_EXECINSTR<>0 then
  623. include(aoptions,oso_executable);
  624. end;
  625. {****************************************************************************
  626. TElfObjSection
  627. ****************************************************************************}
  628. constructor TElfObjSection.create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);
  629. begin
  630. inherited create(AList,Aname,Aalign,aoptions);
  631. secshidx:=0;
  632. shstridx:=0;
  633. encodesechdrflags(aoptions,shtype,shflags);
  634. shlink:=0;
  635. shinfo:=0;
  636. if name='.stab' then
  637. shentsize:=sizeof(TObjStabEntry);
  638. end;
  639. constructor TElfObjSection.create_ext(aobjdata:TObjData;const Aname:string;Ashtype,Ashflags,Ashlink,Ashinfo:longint;Aalign:shortint;Aentsize:longint);
  640. var
  641. aoptions : TObjSectionOptions;
  642. begin
  643. decodesechdrflags(Ashtype,Ashflags,aoptions);
  644. inherited create(aobjdata.ObjSectionList,Aname,Aalign,aoptions);
  645. objdata:=aobjdata;
  646. secshidx:=0;
  647. shstridx:=0;
  648. shtype:=AshType;
  649. shflags:=AshFlags;
  650. shlink:=Ashlink;
  651. shinfo:=Ashinfo;
  652. shentsize:=Aentsize;
  653. end;
  654. {****************************************************************************
  655. TElfObjData
  656. ****************************************************************************}
  657. constructor TElfObjData.create(const n:string);
  658. begin
  659. inherited create(n);
  660. CObjSection:=TElfObjSection;
  661. { we need at least the following sections }
  662. createsection(sec_code);
  663. { always a non-PIC data section (will remain empty if doing PIC) }
  664. createsection('.data',sectiontype2align(sec_data),sectiontype2options(sec_data));
  665. createsection(sec_bss);
  666. if (cs_create_pic in current_settings.moduleswitches) and
  667. not(target_info.system in systems_darwin) then
  668. createsection(sec_data);
  669. if tf_section_threadvars in target_info.flags then
  670. createsection(sec_threadvar);
  671. if (tf_needs_dwarf_cfi in target_info.flags) and
  672. (af_supports_dwarf in target_asm.flags) then
  673. createsection(sec_debug_frame);
  674. end;
  675. function TElfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  676. const
  677. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  678. {$ifdef userodata}
  679. '.text','.data','.data','.rodata','.bss','.threadvar',
  680. {$else userodata}
  681. '.text','.data','.data','.data','.bss','.threadvar',
  682. {$endif userodata}
  683. '.pdata',
  684. '.text', { darwin stubs }
  685. '__DATA,__nl_symbol_ptr',
  686. '__DATA,__la_symbol_ptr',
  687. '__DATA,__mod_init_func',
  688. '__DATA,__mod_term_func',
  689. '.stab','.stabstr',
  690. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  691. '.eh_frame',
  692. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  693. '.fpc',
  694. '.toc',
  695. '.init',
  696. '.fini',
  697. '.objc_class',
  698. '.objc_meta_class',
  699. '.objc_cat_cls_meth',
  700. '.objc_cat_inst_meth',
  701. '.objc_protocol',
  702. '.objc_string_object',
  703. '.objc_cls_meth',
  704. '.objc_inst_meth',
  705. '.objc_cls_refs',
  706. '.objc_message_refs',
  707. '.objc_symbols',
  708. '.objc_category',
  709. '.objc_class_vars',
  710. '.objc_instance_vars',
  711. '.objc_module_info',
  712. '.objc_class_names',
  713. '.objc_meth_var_types',
  714. '.objc_meth_var_names',
  715. '.objc_selector_strs',
  716. '.objc_protocol_ext',
  717. '.objc_class_ext',
  718. '.objc_property',
  719. '.objc_image_info',
  720. '.objc_cstring_object',
  721. '.objc_sel_fixup',
  722. '__DATA,__objc_data',
  723. '__DATA,__objc_const',
  724. '.objc_superrefs',
  725. '__DATA, __datacoal_nt,coalesced',
  726. '.objc_classlist',
  727. '.objc_nlclasslist',
  728. '.objc_catlist',
  729. '.obcj_nlcatlist',
  730. '.objc_protolist'
  731. );
  732. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  733. '.text',
  734. '.data.rel',
  735. '.data.rel',
  736. '.data.rel',
  737. '.bss',
  738. '.threadvar',
  739. '.pdata',
  740. '', { stubs }
  741. '__DATA,__nl_symbol_ptr',
  742. '__DATA,__la_symbol_ptr',
  743. '__DATA,__mod_init_func',
  744. '__DATA,__mod_term_func',
  745. '.stab',
  746. '.stabstr',
  747. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  748. '.eh_frame',
  749. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  750. '.fpc',
  751. '.toc',
  752. '.init',
  753. '.fini',
  754. '.objc_class',
  755. '.objc_meta_class',
  756. '.objc_cat_cls_meth',
  757. '.objc_cat_inst_meth',
  758. '.objc_protocol',
  759. '.objc_string_object',
  760. '.objc_cls_meth',
  761. '.objc_inst_meth',
  762. '.objc_cls_refs',
  763. '.objc_message_refs',
  764. '.objc_symbols',
  765. '.objc_category',
  766. '.objc_class_vars',
  767. '.objc_instance_vars',
  768. '.objc_module_info',
  769. '.objc_class_names',
  770. '.objc_meth_var_types',
  771. '.objc_meth_var_names',
  772. '.objc_selector_strs',
  773. '.objc_protocol_ext',
  774. '.objc_class_ext',
  775. '.objc_property',
  776. '.objc_image_info',
  777. '.objc_cstring_object',
  778. '.objc_sel_fixup',
  779. '__DATA,__objc_data',
  780. '__DATA,__objc_const',
  781. '.objc_superrefs',
  782. '__DATA, __datacoal_nt,coalesced',
  783. '.objc_classlist',
  784. '.objc_nlclasslist',
  785. '.objc_catlist',
  786. '.obcj_nlcatlist',
  787. '.objc_protolist'
  788. );
  789. var
  790. sep : string[3];
  791. secname : string;
  792. begin
  793. { section type user gives the user full controll on the section name }
  794. if atype=sec_user then
  795. result:=aname
  796. else
  797. begin
  798. if (cs_create_pic in current_settings.moduleswitches) and
  799. not(target_info.system in systems_darwin) then
  800. secname:=secnames_pic[atype]
  801. else
  802. secname:=secnames[atype];
  803. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  804. begin
  805. result:=secname+'.'+aname;
  806. exit;
  807. end;
  808. if create_smartlink_sections and (aname<>'') then
  809. begin
  810. case aorder of
  811. secorder_begin :
  812. sep:='.b_';
  813. secorder_end :
  814. sep:='.z_';
  815. else
  816. sep:='.n_';
  817. end;
  818. result:=secname+sep+aname
  819. end
  820. else
  821. result:=secname;
  822. end;
  823. end;
  824. procedure TElfObjData.CreateDebugSections;
  825. begin
  826. if target_dbg.id=dbg_stabs then
  827. begin
  828. stabssec:=createsection(sec_stab);
  829. stabstrsec:=createsection(sec_stabstr);
  830. end;
  831. end;
  832. procedure TElfObjData.writereloc(data:aint;len:aword;p:TObjSymbol;reltype:TObjRelocationType);
  833. var
  834. symaddr : aint;
  835. objreloc: TObjRelocation;
  836. begin
  837. if CurrObjSec=nil then
  838. internalerror(200403292);
  839. objreloc:=nil;
  840. if assigned(p) then
  841. begin
  842. { real address of the symbol }
  843. symaddr:=p.address;
  844. { Local ObjSymbols can be resolved already or need a section reloc }
  845. if (p.bind=AB_LOCAL) and
  846. (reltype in [RELOC_RELATIVE,RELOC_ABSOLUTE{$ifdef x86_64},RELOC_ABSOLUTE32{$endif x86_64}]) then
  847. begin
  848. { For a reltype relocation in the same section the
  849. value can be calculated }
  850. if (p.objsection=CurrObjSec) and
  851. (reltype=RELOC_RELATIVE) then
  852. inc(data,symaddr-len-CurrObjSec.Size)
  853. else
  854. begin
  855. objreloc:=TObjRelocation.CreateSection(CurrObjSec.Size,p.objsection,reltype);
  856. CurrObjSec.ObjRelocations.Add(objreloc);
  857. inc(data,symaddr);
  858. end;
  859. end
  860. else
  861. begin
  862. objreloc:=TObjRelocation.CreateSymbol(CurrObjSec.Size,p,reltype);
  863. CurrObjSec.ObjRelocations.Add(objreloc);
  864. end;
  865. end;
  866. if assigned(objreloc) then
  867. begin
  868. objreloc.size:=len;
  869. if reltype in [RELOC_RELATIVE,RELOC_PLT32{$ifdef x86_64},RELOC_GOTPCREL{$endif}] then
  870. dec(data,len);
  871. if relocs_use_addend then
  872. begin
  873. objreloc.orgsize:=data;
  874. data:=0;
  875. end;
  876. end;
  877. CurrObjSec.write(data,len);
  878. end;
  879. {****************************************************************************
  880. TElfSymtab
  881. ****************************************************************************}
  882. const
  883. symsecnames: array[boolean] of string[8] = ('.symtab','.dynsym');
  884. strsecnames: array[boolean] of string[8] = ('.strtab','.dynstr');
  885. symsectypes: array[boolean] of longint = (SHT_SYMTAB,SHT_DYNSYM);
  886. symsecattrs: array[boolean] of longint = (0,SHF_ALLOC);
  887. constructor TElfSymtab.create(aObjData:TObjData;aKind:TElfSymtabKind);
  888. var
  889. dyn:boolean;
  890. begin
  891. dyn:=(aKind=esk_dyn);
  892. create_ext(aObjData,symsecnames[dyn],symsectypes[dyn],symsecattrs[dyn],0,0,sizeof(pint),sizeof(TElfSymbol));
  893. fstrsec:=TElfObjSection.create_ext(aObjData,strsecnames[dyn],SHT_STRTAB,symsecattrs[dyn],0,0,1,0);
  894. fstrsec.writestr(#0);
  895. writezeros(sizeof(TElfSymbol));
  896. symidx:=1;
  897. shinfo:=1;
  898. kind:=aKind;
  899. end;
  900. procedure TElfSymtab.writeInternalSymbol(astridx:longint;ainfo:byte;ashndx:word);
  901. var
  902. elfsym:TElfSymbol;
  903. begin
  904. fillchar(elfsym,sizeof(elfsym),0);
  905. elfsym.st_name:=astridx;
  906. elfsym.st_info:=ainfo;
  907. elfsym.st_shndx:=ashndx;
  908. inc(symidx);
  909. inc(shinfo);
  910. MaybeSwapElfSymbol(elfsym);
  911. write(elfsym,sizeof(elfsym));
  912. end;
  913. procedure TElfSymtab.writeSymbol(objsym:TObjSymbol);
  914. var
  915. elfsym:TElfSymbol;
  916. begin
  917. fillchar(elfsym,sizeof(elfsym),0);
  918. { symbolname, write the #0 separate to overcome 255+1 char not possible }
  919. elfsym.st_name:=fstrsec.writestr(objsym.name);
  920. fstrsec.writestr(#0);
  921. elfsym.st_size:=objsym.size;
  922. case objsym.bind of
  923. AB_LOCAL :
  924. begin
  925. elfsym.st_value:=objsym.address;
  926. elfsym.st_info:=STB_LOCAL shl 4;
  927. inc(shinfo);
  928. end;
  929. AB_COMMON :
  930. begin
  931. elfsym.st_value:=$10; { ?? should not be hardcoded }
  932. elfsym.st_info:=STB_GLOBAL shl 4;
  933. elfsym.st_shndx:=SHN_COMMON;
  934. end;
  935. AB_EXTERNAL :
  936. elfsym.st_info:=STB_GLOBAL shl 4;
  937. AB_WEAK_EXTERNAL :
  938. elfsym.st_info:=STB_WEAK shl 4;
  939. AB_GLOBAL :
  940. begin
  941. elfsym.st_value:=objsym.address;
  942. elfsym.st_info:=STB_GLOBAL shl 4;
  943. end;
  944. end;
  945. if (objsym.bind<>AB_EXTERNAL) {and
  946. not(assigned(objsym.objsection) and
  947. not(oso_data in objsym.objsection.secoptions))} then
  948. begin
  949. case objsym.typ of
  950. AT_FUNCTION :
  951. elfsym.st_info:=elfsym.st_info or STT_FUNC;
  952. AT_DATA :
  953. elfsym.st_info:=elfsym.st_info or STT_OBJECT;
  954. end;
  955. end;
  956. if objsym.bind<>AB_COMMON then
  957. begin
  958. if kind<>esk_obj then
  959. begin
  960. { TODO }
  961. end
  962. else
  963. begin
  964. if assigned(objsym.objsection) then
  965. elfsym.st_shndx:=TElfObjSection(objsym.objsection).secshidx
  966. else
  967. elfsym.st_shndx:=SHN_UNDEF;
  968. objsym.symidx:=symidx;
  969. end;
  970. end;
  971. inc(symidx);
  972. MaybeSwapElfSymbol(elfsym);
  973. write(elfsym,sizeof(TElfSymbol));
  974. end;
  975. {****************************************************************************
  976. TElfObjectOutput
  977. ****************************************************************************}
  978. constructor TElfObjectOutput.create(AWriter:TObjectWriter);
  979. begin
  980. inherited Create(AWriter);
  981. CObjData:=TElfObjData;
  982. end;
  983. procedure TElfObjectOutput.createrelocsection(s:TElfObjSection;data:TObjData);
  984. var
  985. i : longint;
  986. rel : telfreloc;
  987. objreloc : TObjRelocation;
  988. relsym,
  989. reltyp : longint;
  990. relocsect : TElfObjSection;
  991. begin
  992. with data do
  993. begin
  994. { create the reloc section }
  995. if relocs_use_addend then
  996. relocsect:=TElfObjSection.create_ext(data,'.rela'+s.name,SHT_RELA,0,symtabsect.secshidx,s.secshidx,4,3*sizeof(pint))
  997. else
  998. relocsect:=TElfObjSection.create_ext(data,'.rel'+s.name,SHT_REL,0,symtabsect.secshidx,s.secshidx,4,2*sizeof(pint));
  999. { add the relocations }
  1000. for i:=0 to s.Objrelocations.count-1 do
  1001. begin
  1002. objreloc:=TObjRelocation(s.Objrelocations[i]);
  1003. fillchar(rel,sizeof(rel),0);
  1004. rel.address:=objreloc.dataoffset;
  1005. rel.addend:=objreloc.orgsize;
  1006. { when things settle down, we can create processor specific
  1007. derived classes }
  1008. case objreloc.typ of
  1009. {$ifdef i386}
  1010. RELOC_RELATIVE :
  1011. reltyp:=R_386_PC32;
  1012. RELOC_ABSOLUTE :
  1013. reltyp:=R_386_32;
  1014. RELOC_GOT32 :
  1015. reltyp:=R_386_GOT32;
  1016. RELOC_GOTPC :
  1017. reltyp:=R_386_GOTPC;
  1018. RELOC_PLT32 :
  1019. reltyp:=R_386_PLT32;
  1020. {$endif i386}
  1021. {$ifdef sparc}
  1022. RELOC_ABSOLUTE :
  1023. reltyp:=R_SPARC_32;
  1024. {$endif sparc}
  1025. {$ifdef x86_64}
  1026. { Note: 8 and 16-bit relocations are known to be non-conformant with
  1027. AMD64 ABI, so they aren't handled. }
  1028. RELOC_RELATIVE :
  1029. if objreloc.size=8 then
  1030. reltyp:=R_X86_64_PC64
  1031. else if objreloc.size=4 then
  1032. reltyp:=R_X86_64_PC32
  1033. else
  1034. InternalError(2012061900);
  1035. RELOC_ABSOLUTE :
  1036. if objreloc.size=8 then
  1037. reltyp:=R_X86_64_64
  1038. else if objreloc.size=4 then
  1039. reltyp:=R_X86_64_32
  1040. else
  1041. InternalError(2012061901);
  1042. RELOC_ABSOLUTE32 :
  1043. reltyp:=R_X86_64_32S;
  1044. RELOC_GOTPCREL :
  1045. reltyp:=R_X86_64_GOTPCREL;
  1046. RELOC_PLT32 :
  1047. reltyp:=R_X86_64_PLT32;
  1048. {$endif x86_64}
  1049. else
  1050. internalerror(200602261);
  1051. end;
  1052. { Symbol }
  1053. if assigned(objreloc.symbol) then
  1054. begin
  1055. if objreloc.symbol.symidx=-1 then
  1056. begin
  1057. writeln(objreloc.symbol.Name);
  1058. internalerror(200603012);
  1059. end;
  1060. relsym:=objreloc.symbol.symidx;
  1061. end
  1062. else
  1063. begin
  1064. if objreloc.objsection<>nil then
  1065. relsym:=objreloc.objsection.secsymidx
  1066. else
  1067. relsym:=SHN_UNDEF;
  1068. end;
  1069. {$ifdef cpu64bitaddr}
  1070. rel.info:=(qword(relsym) shl 32) or reltyp;
  1071. {$else cpu64bitaddr}
  1072. rel.info:=(relsym shl 8) or reltyp;
  1073. {$endif cpu64bitaddr}
  1074. { write reloc }
  1075. { ElfXX_Rel is essentially ElfXX_Rela without the addend field. }
  1076. MaybeSwapElfReloc(rel);
  1077. relocsect.write(rel,relocsect.shentsize);
  1078. end;
  1079. end;
  1080. end;
  1081. procedure TElfObjectOutput.section_write_symbol(p:TObject;arg:pointer);
  1082. begin
  1083. { Must not write symbols for internal sections like .symtab }
  1084. { TODO: maybe use inclusive list of section types instead }
  1085. if (TElfObjSection(p).shtype in [SHT_SYMTAB,SHT_STRTAB,SHT_REL,SHT_RELA]) then
  1086. exit;
  1087. TObjSection(p).secsymidx:=symtabsect.symidx;
  1088. symtabsect.writeInternalSymbol(0,STT_SECTION,TElfObjSection(p).secshidx);
  1089. end;
  1090. procedure TElfObjectOutput.createsymtab(data: TObjData);
  1091. var
  1092. i : longint;
  1093. objsym : TObjSymbol;
  1094. begin
  1095. with data do
  1096. begin
  1097. { filename entry }
  1098. symtabsect.writeInternalSymbol(1,STT_FILE,SHN_ABS);
  1099. { section }
  1100. ObjSectionList.ForEachCall(@section_write_symbol,nil);
  1101. { First the Local Symbols, this is required by ELF. The localsyms
  1102. count stored in shinfo is used to skip the local symbols
  1103. when traversing the symtab }
  1104. for i:=0 to ObjSymbolList.Count-1 do
  1105. begin
  1106. objsym:=TObjSymbol(ObjSymbolList[i]);
  1107. if (objsym.bind=AB_LOCAL) and (objsym.typ<>AT_LABEL) then
  1108. symtabsect.WriteSymbol(objsym);
  1109. end;
  1110. { Global Symbols }
  1111. for i:=0 to ObjSymbolList.Count-1 do
  1112. begin
  1113. objsym:=TObjSymbol(ObjSymbolList[i]);
  1114. if (objsym.bind<>AB_LOCAL) then
  1115. symtabsect.WriteSymbol(objsym);
  1116. end;
  1117. { update the .symtab section header }
  1118. symtabsect.shlink:=TElfObjSection(symtabsect.fstrsec).secshidx;
  1119. end;
  1120. end;
  1121. procedure TElfObjectOutput.section_write_sh_string(p:TObject;arg:pointer);
  1122. begin
  1123. TElfObjSection(p).shstridx:=shstrtabsect.writestr(TObjSection(p).name+#0);
  1124. end;
  1125. procedure TElfObjectOutput.createshstrtab(data: TObjData);
  1126. begin
  1127. with data do
  1128. begin
  1129. shstrtabsect.writestr(#0);
  1130. ObjSectionList.ForEachCall(@section_write_sh_string,nil);
  1131. end;
  1132. end;
  1133. procedure TElfObjectOutput.writesectionheader(s:TElfObjSection);
  1134. var
  1135. sechdr : telfsechdr;
  1136. begin
  1137. fillchar(sechdr,sizeof(sechdr),0);
  1138. sechdr.sh_name:=s.shstridx;
  1139. sechdr.sh_type:=s.shtype;
  1140. sechdr.sh_flags:=s.shflags;
  1141. sechdr.sh_offset:=s.datapos;
  1142. sechdr.sh_size:=s.Size;
  1143. sechdr.sh_link:=s.shlink;
  1144. sechdr.sh_info:=s.shinfo;
  1145. sechdr.sh_addralign:=s.secalign;
  1146. sechdr.sh_entsize:=s.shentsize;
  1147. MaybeSwapSecHeader(sechdr);
  1148. writer.write(sechdr,sizeof(sechdr));
  1149. end;
  1150. procedure TElfObjectOutput.section_count_sections(p:TObject;arg:pointer);
  1151. begin
  1152. TElfObjSection(p).secshidx:=pword(arg)^;
  1153. inc(pword(arg)^);
  1154. end;
  1155. procedure TElfObjectOutput.section_create_relocsec(p:TObject;arg:pointer);
  1156. begin
  1157. if (TElfObjSection(p).ObjRelocations.count>0) then
  1158. createrelocsection(TElfObjSection(p),TObjData(arg));
  1159. end;
  1160. procedure TElfObjectOutput.section_write_sechdr(p:TObject;arg:pointer);
  1161. begin
  1162. writesectionheader(TElfObjSection(p));
  1163. end;
  1164. function TElfObjectOutput.writedata(data:TObjData):boolean;
  1165. var
  1166. header : telfheader;
  1167. shoffset,
  1168. datapos : aword;
  1169. nsections : word;
  1170. begin
  1171. result:=false;
  1172. with data do
  1173. begin
  1174. { default sections }
  1175. symtabsect:=TElfSymtab.create(data,esk_obj);
  1176. shstrtabsect:=TElfObjSection.create_ext(data,'.shstrtab',SHT_STRTAB,0,0,0,1,0);
  1177. { "no executable stack" marker for Linux }
  1178. if (target_info.system in systems_linux) and
  1179. not(cs_executable_stack in current_settings.moduleswitches) then
  1180. TElfObjSection.create_ext(data,'.note.GNU-stack',SHT_PROGBITS,0,0,0,1,0);
  1181. { insert filename as first in strtab }
  1182. symtabsect.fstrsec.writestr(ExtractFileName(current_module.mainsource));
  1183. symtabsect.fstrsec.writestr(#0);
  1184. { calc amount of sections we have }
  1185. nsections:=1;
  1186. { also create the index in the section header table }
  1187. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1188. { create .symtab and .strtab }
  1189. createsymtab(data);
  1190. { Create the relocation sections, this needs valid secidx and symidx }
  1191. ObjSectionList.ForEachCall(@section_create_relocsec,data);
  1192. { recalc nsections to incude the reloc sections }
  1193. nsections:=1;
  1194. ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  1195. { create .shstrtab }
  1196. createshstrtab(data);
  1197. { Calculate the filepositions }
  1198. datapos:=$40; { elfheader + alignment }
  1199. { section data }
  1200. layoutsections(datapos);
  1201. { section headers }
  1202. shoffset:=datapos;
  1203. inc(datapos,(nsections+1)*sizeof(telfsechdr));
  1204. { Write ELF Header }
  1205. fillchar(header,sizeof(header),0);
  1206. header.magic[0]:=$7f; { = #127'ELF' }
  1207. header.magic[1]:=$45;
  1208. header.magic[2]:=$4c;
  1209. header.magic[3]:=$46;
  1210. header.file_class:=ELFCLASS;
  1211. if target_info.endian=endian_big then
  1212. header.data_encoding:=2
  1213. else
  1214. header.data_encoding:=1;
  1215. header.file_version:=1;
  1216. header.e_type:=ET_REL;
  1217. header.e_machine:=ELFMACHINE;
  1218. {$ifdef arm}
  1219. if (current_settings.fputype=fpu_soft) then
  1220. header.e_flags:=$600;
  1221. {$endif arm}
  1222. header.e_version:=1;
  1223. header.e_shoff:=shoffset;
  1224. header.e_shstrndx:=shstrtabsect.secshidx;
  1225. header.e_shnum:=nsections;
  1226. header.e_ehsize:=sizeof(telfheader);
  1227. header.e_shentsize:=sizeof(telfsechdr);
  1228. MaybeSwapHeader(header);
  1229. writer.write(header,sizeof(header));
  1230. writer.writezeros($40-sizeof(header)); { align }
  1231. { Sections }
  1232. WriteSectionContent(data);
  1233. { section headers, start with an empty header for sh_undef }
  1234. writer.writezeros(sizeof(telfsechdr));
  1235. ObjSectionList.ForEachCall(@section_write_sechdr,nil);
  1236. end;
  1237. result:=true;
  1238. end;
  1239. {****************************************************************************
  1240. TELFAssembler
  1241. ****************************************************************************}
  1242. constructor TElfAssembler.Create(smart:boolean);
  1243. begin
  1244. inherited Create(smart);
  1245. CObjOutput:=TElfObjectOutput;
  1246. end;
  1247. {*****************************************************************************
  1248. Initialize
  1249. *****************************************************************************}
  1250. {$ifdef i386}
  1251. const
  1252. as_i386_elf32_info : tasminfo =
  1253. (
  1254. id : as_i386_elf32;
  1255. idtxt : 'ELF';
  1256. asmbin : '';
  1257. asmcmd : '';
  1258. supported_targets : [system_i386_linux,system_i386_beos,
  1259. system_i386_freebsd,system_i386_haiku,
  1260. system_i386_openbsd,system_i386_netbsd,
  1261. system_i386_Netware,system_i386_netwlibc,
  1262. system_i386_solaris,system_i386_embedded];
  1263. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1264. labelprefix : '.L';
  1265. comment : '';
  1266. dollarsign: '$';
  1267. );
  1268. {$endif i386}
  1269. {$ifdef x86_64}
  1270. const
  1271. as_x86_64_elf64_info : tasminfo =
  1272. (
  1273. id : as_x86_64_elf64;
  1274. idtxt : 'ELF';
  1275. asmbin : '';
  1276. asmcmd : '';
  1277. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  1278. system_x86_64_openbsd,system_x86_64_netbsd];
  1279. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  1280. labelprefix : '.L';
  1281. comment : '';
  1282. dollarsign: '$';
  1283. );
  1284. {$endif x86_64}
  1285. {$ifdef sparc}
  1286. const
  1287. as_sparc_elf32_info : tasminfo =
  1288. (
  1289. id : as_sparc_elf32;
  1290. idtxt : 'ELF';
  1291. asmbin : '';
  1292. asmcmd : '';
  1293. supported_targets : [];
  1294. // flags : [af_outputbinary,af_smartlink_sections];
  1295. flags : [af_outputbinary,af_supports_dwarf];
  1296. labelprefix : '.L';
  1297. comment : '';
  1298. dollarsign: '$';
  1299. );
  1300. {$endif sparc}
  1301. initialization
  1302. {$ifdef i386}
  1303. RegisterAssembler(as_i386_elf32_info,TElfAssembler);
  1304. {$endif i386}
  1305. {$ifdef sparc}
  1306. RegisterAssembler(as_sparc_elf32_info,TElfAssembler);
  1307. {$endif sparc}
  1308. {$ifdef x86_64}
  1309. RegisterAssembler(as_x86_64_elf64_info,TElfAssembler);
  1310. {$endif x86_64}
  1311. end.