ogrel.pas 13 KB

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