ogrel.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. public
  49. RelFlags: TRelRelocationFlags;
  50. constructor CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
  51. constructor CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
  52. function EncodeFlags: string;
  53. end;
  54. { TRelObjData }
  55. TRelObjData = class(TObjData)
  56. public
  57. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  58. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  59. end;
  60. { TRelObjOutput }
  61. TRelObjOutput = class(tObjOutput)
  62. private
  63. procedure writeString(const S: ansistring);
  64. procedure writeLine(const S: ansistring);
  65. procedure WriteAreaContentAndRelocations(sec: TObjSection);
  66. protected
  67. function writeData(Data:TObjData):boolean;override;
  68. public
  69. constructor create(AWriter:TObjectWriter);override;
  70. end;
  71. { TRelAssembler }
  72. TRelAssembler = class(tinternalassembler)
  73. constructor create(info: pasminfo; smart:boolean);override;
  74. end;
  75. implementation
  76. uses
  77. SysUtils,
  78. cutils,verbose,globals,
  79. fmodule,aasmtai,aasmdata,
  80. ogmap,
  81. version
  82. ;
  83. function tohex(q: qword): string;
  84. begin
  85. result:=HexStr(q,16);
  86. while (Length(result)>1) and (result[1]='0') do
  87. delete(result,1,1);
  88. end;
  89. {*****************************************************************************
  90. TRelRelocation
  91. *****************************************************************************}
  92. constructor TRelRelocation.CreateSymbol(ADataOffset: TObjSectionOfs; s: TObjSymbol; Atyp: TObjRelocationType);
  93. begin
  94. inherited;
  95. size:=2;
  96. RelFlags:=[rrfSymbol];
  97. end;
  98. constructor TRelRelocation.CreateSection(ADataOffset: TObjSectionOfs; aobjsec: TObjSection; Atyp: TObjRelocationType);
  99. begin
  100. inherited;
  101. size:=2;
  102. RelFlags:=[];
  103. end;
  104. function TRelRelocation.EncodeFlags: string;
  105. var
  106. FlagsWord: Word;
  107. begin
  108. FlagsWord:=0;
  109. if rrfByte in RelFlags then
  110. Inc(FlagsWord,1);
  111. if rrfSymbol in RelFlags then
  112. Inc(FlagsWord,2);
  113. if rrfPcRelative in RelFlags then
  114. Inc(FlagsWord,4);
  115. if rrfTwoByteObjectFormatForByteData in RelFlags then
  116. Inc(FlagsWord,8);
  117. if rrfUnsignedByteData in RelFlags then
  118. Inc(FlagsWord,16);
  119. if rrfPage0Reference in RelFlags then
  120. Inc(FlagsWord,32);
  121. if rrfPageNNNReference in RelFlags then
  122. Inc(FlagsWord,64);
  123. if rrfMSBWith2ByteMode in RelFlags then
  124. Inc(FlagsWord,128);
  125. if rrfThreeByteObjectFormatForByteData in RelFlags then
  126. Inc(FlagsWord,256);
  127. if rrfRealMSBForThreeByteMode in RelFlags then
  128. Inc(FlagsWord,512);
  129. if rrfReserved10 in RelFlags then
  130. Inc(FlagsWord,1024);
  131. if rrfReserved11 in RelFlags then
  132. Inc(FlagsWord,2048);
  133. if (FlagsWord<=255) and ((FlagsWord and $F0)<>$F0) then
  134. Result:=HexStr(FlagsWord,2)
  135. else
  136. Result:=HexStr($F0 or Byte(FlagsWord shr 8),2)+' '+HexStr(Byte(FlagsWord),2);
  137. end;
  138. {*****************************************************************************
  139. TRelObjData
  140. *****************************************************************************}
  141. function TRelObjData.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  142. const
  143. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  144. '_CODE',
  145. '_DATA',
  146. '_DATA',
  147. '.rodata',
  148. '.bss',
  149. '.threadvar',
  150. '.pdata',
  151. '', { stubs }
  152. '__DATA,__nl_symbol_ptr',
  153. '__DATA,__la_symbol_ptr',
  154. '__DATA,__mod_init_func',
  155. '__DATA,__mod_term_func',
  156. '.stab',
  157. '.stabstr',
  158. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  159. '.eh_frame',
  160. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  161. '.fpc',
  162. '.toc',
  163. '.init',
  164. '.fini',
  165. '.objc_class',
  166. '.objc_meta_class',
  167. '.objc_cat_cls_meth',
  168. '.objc_cat_inst_meth',
  169. '.objc_protocol',
  170. '.objc_string_object',
  171. '.objc_cls_meth',
  172. '.objc_inst_meth',
  173. '.objc_cls_refs',
  174. '.objc_message_refs',
  175. '.objc_symbols',
  176. '.objc_category',
  177. '.objc_class_vars',
  178. '.objc_instance_vars',
  179. '.objc_module_info',
  180. '.objc_class_names',
  181. '.objc_meth_var_types',
  182. '.objc_meth_var_names',
  183. '.objc_selector_strs',
  184. '.objc_protocol_ext',
  185. '.objc_class_ext',
  186. '.objc_property',
  187. '.objc_image_info',
  188. '.objc_cstring_object',
  189. '.objc_sel_fixup',
  190. '__DATA,__objc_data',
  191. '__DATA,__objc_const',
  192. '.objc_superrefs',
  193. '__DATA, __datacoal_nt,coalesced',
  194. '.objc_classlist',
  195. '.objc_nlclasslist',
  196. '.objc_catlist',
  197. '.obcj_nlcatlist',
  198. '.objc_protolist',
  199. '.stack',
  200. '.heap',
  201. '.gcc_except_table',
  202. '.ARM.attributes'
  203. );
  204. begin
  205. result:=secnames[atype];
  206. end;
  207. procedure TRelObjData.writeReloc(Data: TRelocDataInt; len: aword; p: TObjSymbol; Reloctype: TObjRelocationType);
  208. var
  209. bytes: array [0..1] of Byte;
  210. symaddr: QWord;
  211. objreloc: TRelRelocation;
  212. begin
  213. if CurrObjSec=nil then
  214. internalerror(200403072);
  215. objreloc:=nil;
  216. if assigned(p) then
  217. begin
  218. { real address of the symbol }
  219. symaddr:=p.address;
  220. if p.bind=AB_EXTERNAL then
  221. begin
  222. objreloc:=TRelRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  223. CurrObjSec.ObjRelocations.Add(objreloc);
  224. end
  225. { relative relocations within the same section can be calculated directly,
  226. without the need to emit a relocation entry }
  227. else if (p.objsection=CurrObjSec) and
  228. (p.bind<>AB_COMMON) and
  229. (Reloctype=RELOC_RELATIVE) then
  230. begin
  231. data:=data+symaddr-len-CurrObjSec.Size;
  232. end
  233. else
  234. begin
  235. objreloc:=TRelRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
  236. CurrObjSec.ObjRelocations.Add(objreloc);
  237. end;
  238. end;
  239. case len of
  240. 2:
  241. begin
  242. bytes[0]:=Byte(Data);
  243. bytes[1]:=Byte(Data shr 8);
  244. writebytes(bytes,2);
  245. end;
  246. 1:
  247. begin
  248. bytes[0]:=Byte(Data);
  249. writebytes(bytes,1);
  250. end;
  251. else
  252. internalerror(2020050423);
  253. end;
  254. end;
  255. {*****************************************************************************
  256. TRelObjOutput
  257. *****************************************************************************}
  258. procedure TRelObjOutput.writeString(const S: ansistring);
  259. begin
  260. FWriter.write(S[1],Length(S));
  261. end;
  262. procedure TRelObjOutput.writeLine(const S: ansistring);
  263. begin
  264. writeString(S+#10)
  265. end;
  266. procedure TRelObjOutput.WriteAreaContentAndRelocations(sec: TObjSection);
  267. const
  268. MaxChunkSize=14;
  269. var
  270. ChunkStart,ChunkLen, i: LongWord;
  271. ChunkFixupStart,ChunkFixupEnd: Integer;
  272. s: ansistring;
  273. buf: array [0..MaxChunkSize-1] of Byte;
  274. begin
  275. if oso_data in sec.SecOptions then
  276. begin
  277. if sec.Data=nil then
  278. internalerror(200403073);
  279. sec.data.seek(0);
  280. ChunkFixupStart:=0;
  281. ChunkFixupEnd:=-1;
  282. ChunkStart:=0;
  283. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  284. while ChunkLen>0 do
  285. begin
  286. { find last fixup in the chunk }
  287. while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
  288. (TRelRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
  289. inc(ChunkFixupEnd);
  290. { check if last chunk is crossing the chunk boundary, and trim ChunkLen if necessary }
  291. if (ChunkFixupEnd>=ChunkFixupStart) and
  292. ((TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset+
  293. TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).size)>(ChunkStart+ChunkLen)) then
  294. begin
  295. ChunkLen:=TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset-ChunkStart;
  296. Dec(ChunkFixupEnd);
  297. end;
  298. s:='T '+HexStr(Byte(ChunkStart),2)+' '+HexStr(Byte(ChunkStart shr 8),2);
  299. if ChunkLen>SizeOf(buf) then
  300. internalerror(2020050501);
  301. sec.Data.read(buf,ChunkLen);
  302. for i:=0 to ChunkLen-1 do
  303. s:=s+' '+HexStr(buf[i],2);
  304. writeLine(s);
  305. writeLine('R 00 00 '+HexStr(Byte(sec.SecSymIdx),2)+' '+HexStr(Byte(sec.SecSymIdx shr 8),2));
  306. { prepare next chunk }
  307. Inc(ChunkStart, ChunkLen);
  308. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  309. ChunkFixupStart:=ChunkFixupEnd+1;
  310. end;
  311. end;
  312. end;
  313. function TRelObjOutput.writeData(Data: TObjData): boolean;
  314. var
  315. global_symbols_count: Integer = 0;
  316. secidx, idx, i, j: Integer;
  317. objsym: TObjSymbol;
  318. objsec: TObjSection;
  319. begin
  320. global_symbols_count:=0;
  321. for i:=0 to Data.ObjSymbolList.Count-1 do
  322. begin
  323. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  324. if objsym.bind in [AB_EXTERNAL,AB_GLOBAL] then
  325. Inc(global_symbols_count);
  326. end;
  327. writeLine('XL2');
  328. writeLine('H '+tohex(data.ObjSectionList.Count)+' areas '+tohex(global_symbols_count)+' global symbols');
  329. idx:=0;
  330. for i:=0 to Data.ObjSymbolList.Count-1 do
  331. begin
  332. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  333. if objsym.bind=AB_EXTERNAL then
  334. begin
  335. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Ref0000');
  336. objsym.symidx:=idx;
  337. Inc(idx);
  338. end;
  339. end;
  340. secidx:=0;
  341. for i:=0 to Data.ObjSectionList.Count-1 do
  342. begin
  343. objsec:=TObjSection(Data.ObjSectionList[i]);
  344. writeLine('A '+objsec.Name+' size '+tohex(objsec.Size)+' flags 0 addr 0');
  345. objsec.SecSymIdx:=secidx;
  346. Inc(secidx);
  347. for j:=0 to Data.ObjSymbolList.Count-1 do
  348. begin
  349. objsym:=TObjSymbol(Data.ObjSymbolList[j]);
  350. if (objsym.bind=AB_GLOBAL) and (objsym.objsection=objsec) then
  351. begin
  352. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Def'+HexStr(objsym.offset,4));
  353. objsym.symidx:=idx;
  354. Inc(idx);
  355. end;
  356. end;
  357. end;
  358. for i:=0 to Data.ObjSectionList.Count-1 do
  359. begin
  360. objsec:=TObjSection(Data.ObjSectionList[i]);
  361. WriteAreaContentAndRelocations(objsec);
  362. end;
  363. result:=true;
  364. end;
  365. constructor TRelObjOutput.create(AWriter: TObjectWriter);
  366. begin
  367. inherited;
  368. cobjdata:=TRelObjData;
  369. end;
  370. {*****************************************************************************
  371. TRelAssembler
  372. *****************************************************************************}
  373. constructor TRelAssembler.create(info: pasminfo; smart: boolean);
  374. begin
  375. inherited;
  376. CObjOutput:=TRelObjOutput;
  377. end;
  378. {*****************************************************************************
  379. Initialize
  380. *****************************************************************************}
  381. const
  382. as_z80_rel_info : tasminfo =
  383. (
  384. id : as_z80_rel;
  385. idtxt : 'REL';
  386. asmbin : '';
  387. asmcmd : '';
  388. supported_targets : [system_z80_embedded,system_z80_zxspectrum];
  389. flags : [af_outputbinary,af_smartlink_sections];
  390. labelprefix : '..@';
  391. labelmaxlen : 79;
  392. comment : '; ';
  393. dollarsign: '$';
  394. );
  395. initialization
  396. RegisterAssembler(as_z80_rel_info,TRelAssembler);
  397. end.