ogelf.pas 41 KB

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