ogelf.pas 41 KB

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