ogelf.pas 50 KB

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