ogrel.pas 14 KB

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