ogrel.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. {
  2. Copyright (c) 2020 by Nikolay Nikolov
  3. Contains the ASCII relocatable object file format (*.rel) reader and writer
  4. This is the object format used on the Z80 platforms.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ogrel;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,globtype,
  24. { target }
  25. systems,
  26. { assembler }
  27. cpuinfo,cpubase,aasmbase,assemble,link,
  28. { output }
  29. ogbase,
  30. owbase;
  31. type
  32. TRelRelocationFlag=(
  33. rrfByte, { bit 0 }
  34. rrfSymbol, { bit 1 }
  35. rrfPcRelative, { bit 2 }
  36. rrfTwoByteObjectFormatForByteData, { bit 3 }
  37. rrfUnsignedByteData, { bit 4 }
  38. rrfPage0Reference, { bit 5 }
  39. rrfPageNNNReference, { bit 6 }
  40. rrfMSBWith2ByteMode, { bit 7 }
  41. rrfThreeByteObjectFormatForByteData, { bit 8 }
  42. rrfRealMSBForThreeByteMode, { bit 9 }
  43. rrfReserved10, { bit 10 }
  44. rrfReserved11); { bit 11 }
  45. TRelRelocationFlags=set of TRelRelocationFlag;
  46. { TRelRelocation }
  47. TRelRelocation = class(TObjRelocation)
  48. private
  49. function GetSecOrSymIdx: longint;
  50. public
  51. RelFlags: TRelRelocationFlags;
  52. HiByte: Byte;
  53. constructor CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
  54. constructor CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
  55. function EncodeFlags: string;
  56. property SecOrSymIdx: longint read GetSecOrSymIdx;
  57. end;
  58. { TRelObjData }
  59. TRelObjData = class(TObjData)
  60. public
  61. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  62. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  63. end;
  64. { TRelObjOutput }
  65. TRelObjOutput = class(tObjOutput)
  66. private
  67. procedure writeString(const S: ansistring);
  68. procedure writeLine(const S: ansistring);
  69. procedure WriteAreaContentAndRelocations(sec: TObjSection);
  70. protected
  71. function writeData(Data:TObjData):boolean;override;
  72. public
  73. constructor create(AWriter:TObjectWriter);override;
  74. end;
  75. { TRelAssembler }
  76. TRelAssembler = class(tinternalassembler)
  77. constructor create(info: pasminfo; smart:boolean);override;
  78. end;
  79. implementation
  80. uses
  81. SysUtils,
  82. cutils,verbose,globals,
  83. fmodule,aasmtai,aasmdata,
  84. ogmap,
  85. version
  86. ;
  87. function tohex(q: qword): string;
  88. begin
  89. result:=HexStr(q,16);
  90. while (Length(result)>1) and (result[1]='0') do
  91. delete(result,1,1);
  92. end;
  93. {*****************************************************************************
  94. TRelRelocation
  95. *****************************************************************************}
  96. function TRelRelocation.GetSecOrSymIdx: longint;
  97. begin
  98. if assigned(symbol) then
  99. result:=symbol.symidx
  100. else if assigned(objsection) then
  101. result:=objsection.SecSymIdx
  102. else
  103. internalerror(2020050502);
  104. end;
  105. constructor TRelRelocation.CreateSymbol(ADataOffset: TObjSectionOfs; s: TObjSymbol; Atyp: TObjRelocationType);
  106. begin
  107. inherited;
  108. case Atyp of
  109. RELOC_ABSOLUTE_HI8:
  110. begin
  111. size:=1;
  112. RelFlags:=[rrfSymbol,rrfByte,rrfTwoByteObjectFormatForByteData,rrfMSBWith2ByteMode];
  113. end;
  114. RELOC_ABSOLUTE_LO8:
  115. begin
  116. size:=1;
  117. RelFlags:=[rrfSymbol,rrfByte,rrfTwoByteObjectFormatForByteData];
  118. end;
  119. RELOC_ABSOLUTE:
  120. begin
  121. size:=2;
  122. RelFlags:=[rrfSymbol];
  123. end;
  124. else
  125. internalerror(2020050601);
  126. end;
  127. end;
  128. constructor TRelRelocation.CreateSection(ADataOffset: TObjSectionOfs; aobjsec: TObjSection; Atyp: TObjRelocationType);
  129. begin
  130. inherited;
  131. case Atyp of
  132. RELOC_ABSOLUTE_HI8:
  133. begin
  134. size:=1;
  135. RelFlags:=[rrfByte,rrfTwoByteObjectFormatForByteData,rrfMSBWith2ByteMode];
  136. end;
  137. RELOC_ABSOLUTE_LO8:
  138. begin
  139. size:=1;
  140. RelFlags:=[rrfByte,rrfTwoByteObjectFormatForByteData];
  141. end;
  142. RELOC_ABSOLUTE:
  143. begin
  144. size:=2;
  145. RelFlags:=[];
  146. end;
  147. else
  148. internalerror(2020050601);
  149. end;
  150. end;
  151. function TRelRelocation.EncodeFlags: string;
  152. var
  153. FlagsWord: Word;
  154. begin
  155. FlagsWord:=0;
  156. if rrfByte in RelFlags then
  157. Inc(FlagsWord,1);
  158. if rrfSymbol in RelFlags then
  159. Inc(FlagsWord,2);
  160. if rrfPcRelative in RelFlags then
  161. Inc(FlagsWord,4);
  162. if rrfTwoByteObjectFormatForByteData in RelFlags then
  163. Inc(FlagsWord,8);
  164. if rrfUnsignedByteData in RelFlags then
  165. Inc(FlagsWord,16);
  166. if rrfPage0Reference in RelFlags then
  167. Inc(FlagsWord,32);
  168. if rrfPageNNNReference in RelFlags then
  169. Inc(FlagsWord,64);
  170. if rrfMSBWith2ByteMode in RelFlags then
  171. Inc(FlagsWord,128);
  172. if rrfThreeByteObjectFormatForByteData in RelFlags then
  173. Inc(FlagsWord,256);
  174. if rrfRealMSBForThreeByteMode in RelFlags then
  175. Inc(FlagsWord,512);
  176. if rrfReserved10 in RelFlags then
  177. Inc(FlagsWord,1024);
  178. if rrfReserved11 in RelFlags then
  179. Inc(FlagsWord,2048);
  180. if (FlagsWord<=255) and ((FlagsWord and $F0)<>$F0) then
  181. Result:=HexStr(FlagsWord,2)
  182. else
  183. Result:=HexStr($F0 or Byte(FlagsWord shr 8),2)+' '+HexStr(Byte(FlagsWord),2);
  184. end;
  185. {*****************************************************************************
  186. TRelObjData
  187. *****************************************************************************}
  188. function TRelObjData.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  189. const
  190. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  191. '_CODE',
  192. '_DATA',
  193. '_DATA',
  194. '.rodata',
  195. '.bss',
  196. '.threadvar',
  197. '.pdata',
  198. '', { stubs }
  199. '__DATA,__nl_symbol_ptr',
  200. '__DATA,__la_symbol_ptr',
  201. '__DATA,__mod_init_func',
  202. '__DATA,__mod_term_func',
  203. '.stab',
  204. '.stabstr',
  205. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  206. '.eh_frame',
  207. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  208. '.fpc',
  209. '.toc',
  210. '.init',
  211. '.fini',
  212. '.objc_class',
  213. '.objc_meta_class',
  214. '.objc_cat_cls_meth',
  215. '.objc_cat_inst_meth',
  216. '.objc_protocol',
  217. '.objc_string_object',
  218. '.objc_cls_meth',
  219. '.objc_inst_meth',
  220. '.objc_cls_refs',
  221. '.objc_message_refs',
  222. '.objc_symbols',
  223. '.objc_category',
  224. '.objc_class_vars',
  225. '.objc_instance_vars',
  226. '.objc_module_info',
  227. '.objc_class_names',
  228. '.objc_meth_var_types',
  229. '.objc_meth_var_names',
  230. '.objc_selector_strs',
  231. '.objc_protocol_ext',
  232. '.objc_class_ext',
  233. '.objc_property',
  234. '.objc_image_info',
  235. '.objc_cstring_object',
  236. '.objc_sel_fixup',
  237. '__DATA,__objc_data',
  238. '__DATA,__objc_const',
  239. '.objc_superrefs',
  240. '__DATA, __datacoal_nt,coalesced',
  241. '.objc_classlist',
  242. '.objc_nlclasslist',
  243. '.objc_catlist',
  244. '.obcj_nlcatlist',
  245. '.objc_protolist',
  246. '.stack',
  247. '.heap',
  248. '.gcc_except_table',
  249. '.ARM.attributes'
  250. );
  251. begin
  252. result:=secnames[atype];
  253. end;
  254. procedure TRelObjData.writeReloc(Data: TRelocDataInt; len: aword; p: TObjSymbol; Reloctype: TObjRelocationType);
  255. var
  256. bytes: array [0..1] of Byte;
  257. symaddr: QWord;
  258. objreloc: TRelRelocation;
  259. begin
  260. if CurrObjSec=nil then
  261. internalerror(200403072);
  262. objreloc:=nil;
  263. if assigned(p) then
  264. begin
  265. { real address of the symbol }
  266. symaddr:=p.address;
  267. if p.bind=AB_EXTERNAL then
  268. begin
  269. objreloc:=TRelRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  270. if Reloctype in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
  271. objreloc.HiByte:=Byte(Data shr 8);
  272. CurrObjSec.ObjRelocations.Add(objreloc);
  273. end
  274. { relative relocations within the same section can be calculated directly,
  275. without the need to emit a relocation entry }
  276. else if (p.objsection=CurrObjSec) and
  277. (p.bind<>AB_COMMON) and
  278. (Reloctype=RELOC_RELATIVE) then
  279. begin
  280. data:=data+symaddr-len-CurrObjSec.Size;
  281. end
  282. else
  283. begin
  284. objreloc:=TRelRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
  285. if Reloctype in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
  286. objreloc.HiByte:=Byte(Data shr 8);
  287. CurrObjSec.ObjRelocations.Add(objreloc);
  288. end;
  289. end;
  290. case len of
  291. 2:
  292. begin
  293. bytes[0]:=Byte(Data);
  294. bytes[1]:=Byte(Data shr 8);
  295. writebytes(bytes,2);
  296. end;
  297. 1:
  298. begin
  299. bytes[0]:=Byte(Data);
  300. writebytes(bytes,1);
  301. end;
  302. else
  303. internalerror(2020050423);
  304. end;
  305. end;
  306. {*****************************************************************************
  307. TRelObjOutput
  308. *****************************************************************************}
  309. procedure TRelObjOutput.writeString(const S: ansistring);
  310. begin
  311. FWriter.write(S[1],Length(S));
  312. end;
  313. procedure TRelObjOutput.writeLine(const S: ansistring);
  314. begin
  315. writeString(S+#10)
  316. end;
  317. procedure TRelObjOutput.WriteAreaContentAndRelocations(sec: TObjSection);
  318. const
  319. MaxChunkSize={14}7;
  320. var
  321. ChunkStart,ChunkLen, i: LongWord;
  322. ChunkFixupStart,ChunkFixupEnd, j, st_ofs: Integer;
  323. st,sr: ansistring;
  324. buf: array [0..MaxChunkSize-1] of Byte;
  325. reloc: TRelRelocation;
  326. begin
  327. if oso_data in sec.SecOptions then
  328. begin
  329. if sec.Data=nil then
  330. internalerror(200403073);
  331. sec.data.seek(0);
  332. ChunkFixupStart:=0;
  333. ChunkFixupEnd:=-1;
  334. ChunkStart:=0;
  335. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  336. while ChunkLen>0 do
  337. begin
  338. { find last fixup in the chunk }
  339. while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
  340. (TRelRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
  341. inc(ChunkFixupEnd);
  342. { check if last chunk is crossing the chunk boundary, and trim ChunkLen if necessary }
  343. if (ChunkFixupEnd>=ChunkFixupStart) and
  344. ((TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset+
  345. TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).size)>(ChunkStart+ChunkLen)) then
  346. begin
  347. ChunkLen:=TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset-ChunkStart;
  348. Dec(ChunkFixupEnd);
  349. end;
  350. if ChunkLen>SizeOf(buf) then
  351. internalerror(2020050501);
  352. st:='T '+HexStr(Byte(ChunkStart),2)+' '+HexStr(Byte(ChunkStart shr 8),2);
  353. sr:='R 00 00 '+HexStr(Byte(sec.SecSymIdx),2)+' '+HexStr(Byte(sec.SecSymIdx shr 8),2);
  354. sec.Data.read(buf,ChunkLen);
  355. st_ofs:=1;
  356. { relocations present in the current chunk? }
  357. if ChunkFixupEnd>=ChunkFixupStart then
  358. begin
  359. j:=ChunkFixupStart;
  360. reloc:=TRelRelocation(sec.ObjRelocations[j]);
  361. end
  362. else
  363. begin
  364. j:=-1;
  365. reloc:=nil;
  366. end;
  367. for i:=0 to ChunkLen-1 do
  368. begin
  369. st:=st+' '+HexStr(buf[i],2);
  370. Inc(st_ofs);
  371. if assigned(reloc) then
  372. begin
  373. { advance to the current relocation }
  374. while (reloc.DataOffset<(ChunkStart+i)) and (j<ChunkFixupEnd) do
  375. begin
  376. Inc(j);
  377. reloc:=TRelRelocation(sec.ObjRelocations[j]);
  378. end;
  379. { is there a relocation at the current position? }
  380. if reloc.DataOffset=(ChunkStart+i) then
  381. begin
  382. sr:=sr+' '+reloc.EncodeFlags+' '+HexStr(st_ofs,2)+' '+HexStr(Byte(reloc.SecOrSymIdx),2)+' '+HexStr(Byte(reloc.SecOrSymIdx shr 8),2);
  383. if reloc.typ in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
  384. begin
  385. st:=st+' '+HexStr(reloc.HiByte,2);
  386. Inc(st_ofs);
  387. end;
  388. end;
  389. end;
  390. end;
  391. writeLine(st);
  392. writeLine(sr);
  393. { prepare next chunk }
  394. Inc(ChunkStart, ChunkLen);
  395. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  396. ChunkFixupStart:=ChunkFixupEnd+1;
  397. end;
  398. end;
  399. end;
  400. function TRelObjOutput.writeData(Data: TObjData): boolean;
  401. var
  402. global_symbols_count: Integer = 0;
  403. secidx, idx, i, j: Integer;
  404. objsym: TObjSymbol;
  405. objsec: TObjSection;
  406. begin
  407. global_symbols_count:=0;
  408. for i:=0 to Data.ObjSymbolList.Count-1 do
  409. begin
  410. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  411. if objsym.bind in [AB_EXTERNAL,AB_GLOBAL] then
  412. Inc(global_symbols_count);
  413. end;
  414. writeLine('XL2');
  415. writeLine('H '+tohex(data.ObjSectionList.Count)+' areas '+tohex(global_symbols_count)+' global symbols');
  416. idx:=0;
  417. for i:=0 to Data.ObjSymbolList.Count-1 do
  418. begin
  419. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  420. if objsym.bind=AB_EXTERNAL then
  421. begin
  422. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Ref0000');
  423. objsym.symidx:=idx;
  424. Inc(idx);
  425. end;
  426. end;
  427. secidx:=0;
  428. for i:=0 to Data.ObjSectionList.Count-1 do
  429. begin
  430. objsec:=TObjSection(Data.ObjSectionList[i]);
  431. writeLine('A '+objsec.Name+' size '+tohex(objsec.Size)+' flags 0 addr 0');
  432. objsec.SecSymIdx:=secidx;
  433. Inc(secidx);
  434. for j:=0 to Data.ObjSymbolList.Count-1 do
  435. begin
  436. objsym:=TObjSymbol(Data.ObjSymbolList[j]);
  437. if (objsym.bind=AB_GLOBAL) and (objsym.objsection=objsec) then
  438. begin
  439. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Def'+HexStr(objsym.offset,4));
  440. objsym.symidx:=idx;
  441. Inc(idx);
  442. end;
  443. end;
  444. end;
  445. for i:=0 to Data.ObjSectionList.Count-1 do
  446. begin
  447. objsec:=TObjSection(Data.ObjSectionList[i]);
  448. WriteAreaContentAndRelocations(objsec);
  449. end;
  450. result:=true;
  451. end;
  452. constructor TRelObjOutput.create(AWriter: TObjectWriter);
  453. begin
  454. inherited;
  455. cobjdata:=TRelObjData;
  456. end;
  457. {*****************************************************************************
  458. TRelAssembler
  459. *****************************************************************************}
  460. constructor TRelAssembler.create(info: pasminfo; smart: boolean);
  461. begin
  462. inherited;
  463. CObjOutput:=TRelObjOutput;
  464. end;
  465. {*****************************************************************************
  466. Initialize
  467. *****************************************************************************}
  468. const
  469. as_z80_rel_info : tasminfo =
  470. (
  471. id : as_z80_rel;
  472. idtxt : 'REL';
  473. asmbin : '';
  474. asmcmd : '';
  475. supported_targets : [system_z80_embedded,system_z80_zxspectrum];
  476. flags : [af_outputbinary,af_smartlink_sections];
  477. labelprefix : '..@';
  478. labelmaxlen : 79;
  479. comment : '; ';
  480. dollarsign: '$';
  481. );
  482. initialization
  483. RegisterAssembler(as_z80_rel_info,TRelAssembler);
  484. end.