ogrel.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. function sectiontype2align(atype:TAsmSectiontype):longint;override;
  63. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  64. end;
  65. { TRelObjOutput }
  66. TRelObjOutput = class(tObjOutput)
  67. private
  68. procedure writeString(const S: ansistring);
  69. procedure writeLine(const S: ansistring);
  70. procedure WriteAreaContentAndRelocations(sec: TObjSection);
  71. protected
  72. function writeData(Data:TObjData):boolean;override;
  73. public
  74. constructor create(AWriter:TObjectWriter);override;
  75. end;
  76. { TRelAssembler }
  77. TRelAssembler = class(tinternalassembler)
  78. constructor create(info: pasminfo; smart:boolean);override;
  79. end;
  80. { TRelObjInput }
  81. TRelObjInput = class(TObjInput)
  82. private const
  83. MaxBufSize=512;
  84. private
  85. FBuf: array [0..MaxBufSize-1] of Char;
  86. FBufSize: Integer;
  87. FBufPos: Integer;
  88. function FillBuf: boolean;
  89. function AtEndOfBuf: boolean;
  90. function AtEoF: boolean;
  91. function ReadChar(out c: char): boolean;
  92. function PeekChar(out c: char): boolean;
  93. function ReadLine(out s: string): boolean;
  94. public
  95. constructor create;override;
  96. function ReadObjData(AReader:TObjectreader;out Data:TObjData):boolean;override;
  97. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  98. end;
  99. { TIntelHexExeOutput }
  100. TIntelHexExeOutput = class(TExeOutput)
  101. private
  102. procedure writeString(const S: ansistring);
  103. procedure writeLine(const S: ansistring);
  104. protected
  105. function writeData:boolean;override;
  106. procedure DoRelocationFixup(objsec:TObjSection);override;
  107. public
  108. constructor create;override;
  109. end;
  110. { TZXSpectrumIntelHexExeOutput }
  111. TZXSpectrumIntelHexExeOutput = class(TIntelHexExeOutput)
  112. public
  113. constructor create;override;
  114. end;
  115. implementation
  116. uses
  117. SysUtils,
  118. cutils,verbose,globals,
  119. fmodule,aasmtai,aasmdata,
  120. ogmap,owar,
  121. version
  122. ;
  123. function tohex(q: qword): string;
  124. begin
  125. result:=HexStr(q,16);
  126. while (Length(result)>1) and (result[1]='0') do
  127. delete(result,1,1);
  128. end;
  129. {*****************************************************************************
  130. TRelRelocation
  131. *****************************************************************************}
  132. function TRelRelocation.GetSecOrSymIdx: longint;
  133. begin
  134. if assigned(symbol) then
  135. result:=symbol.symidx
  136. else if assigned(objsection) then
  137. result:=objsection.SecSymIdx
  138. else
  139. internalerror(2020050502);
  140. end;
  141. constructor TRelRelocation.CreateSymbol(ADataOffset: TObjSectionOfs; s: TObjSymbol; Atyp: TObjRelocationType);
  142. begin
  143. inherited;
  144. case Atyp of
  145. RELOC_ABSOLUTE_HI8:
  146. begin
  147. size:=1;
  148. RelFlags:=[rrfSymbol,rrfByte,rrfTwoByteObjectFormatForByteData,rrfMSBWith2ByteMode];
  149. end;
  150. RELOC_ABSOLUTE_LO8:
  151. begin
  152. size:=1;
  153. RelFlags:=[rrfSymbol,rrfByte,rrfTwoByteObjectFormatForByteData];
  154. end;
  155. RELOC_ABSOLUTE:
  156. begin
  157. size:=2;
  158. RelFlags:=[rrfSymbol];
  159. end;
  160. else
  161. internalerror(2020050601);
  162. end;
  163. end;
  164. constructor TRelRelocation.CreateSection(ADataOffset: TObjSectionOfs; aobjsec: TObjSection; Atyp: TObjRelocationType);
  165. begin
  166. inherited;
  167. case Atyp of
  168. RELOC_ABSOLUTE_HI8:
  169. begin
  170. size:=1;
  171. RelFlags:=[rrfByte,rrfTwoByteObjectFormatForByteData,rrfMSBWith2ByteMode];
  172. end;
  173. RELOC_ABSOLUTE_LO8:
  174. begin
  175. size:=1;
  176. RelFlags:=[rrfByte,rrfTwoByteObjectFormatForByteData];
  177. end;
  178. RELOC_ABSOLUTE:
  179. begin
  180. size:=2;
  181. RelFlags:=[];
  182. end;
  183. else
  184. internalerror(2020050613);
  185. end;
  186. end;
  187. function TRelRelocation.EncodeFlags: string;
  188. var
  189. FlagsWord: Word;
  190. begin
  191. FlagsWord:=0;
  192. if rrfByte in RelFlags then
  193. Inc(FlagsWord,1);
  194. if rrfSymbol in RelFlags then
  195. Inc(FlagsWord,2);
  196. if rrfPcRelative in RelFlags then
  197. Inc(FlagsWord,4);
  198. if rrfTwoByteObjectFormatForByteData in RelFlags then
  199. Inc(FlagsWord,8);
  200. if rrfUnsignedByteData in RelFlags then
  201. Inc(FlagsWord,16);
  202. if rrfPage0Reference in RelFlags then
  203. Inc(FlagsWord,32);
  204. if rrfPageNNNReference in RelFlags then
  205. Inc(FlagsWord,64);
  206. if rrfMSBWith2ByteMode in RelFlags then
  207. Inc(FlagsWord,128);
  208. if rrfThreeByteObjectFormatForByteData in RelFlags then
  209. Inc(FlagsWord,256);
  210. if rrfRealMSBForThreeByteMode in RelFlags then
  211. Inc(FlagsWord,512);
  212. if rrfReserved10 in RelFlags then
  213. Inc(FlagsWord,1024);
  214. if rrfReserved11 in RelFlags then
  215. Inc(FlagsWord,2048);
  216. if (FlagsWord<=255) and ((FlagsWord and $F0)<>$F0) then
  217. Result:=HexStr(FlagsWord,2)
  218. else
  219. Result:=HexStr($F0 or Byte(FlagsWord shr 8),2)+' '+HexStr(Byte(FlagsWord),2);
  220. end;
  221. {*****************************************************************************
  222. TRelObjData
  223. *****************************************************************************}
  224. function TRelObjData.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  225. const
  226. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  227. '_CODE',
  228. '_DATA',
  229. '_DATA',
  230. '_DATA',
  231. '_BSS',
  232. '.threadvar',
  233. '.pdata',
  234. '', { stubs }
  235. '__DATA,__nl_symbol_ptr',
  236. '__DATA,__la_symbol_ptr',
  237. '__DATA,__mod_init_func',
  238. '__DATA,__mod_term_func',
  239. '.stab',
  240. '.stabstr',
  241. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  242. '.eh_frame',
  243. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  244. '.fpc',
  245. '.toc',
  246. '.init',
  247. '.fini',
  248. '.objc_class',
  249. '.objc_meta_class',
  250. '.objc_cat_cls_meth',
  251. '.objc_cat_inst_meth',
  252. '.objc_protocol',
  253. '.objc_string_object',
  254. '.objc_cls_meth',
  255. '.objc_inst_meth',
  256. '.objc_cls_refs',
  257. '.objc_message_refs',
  258. '.objc_symbols',
  259. '.objc_category',
  260. '.objc_class_vars',
  261. '.objc_instance_vars',
  262. '.objc_module_info',
  263. '.objc_class_names',
  264. '.objc_meth_var_types',
  265. '.objc_meth_var_names',
  266. '.objc_selector_strs',
  267. '.objc_protocol_ext',
  268. '.objc_class_ext',
  269. '.objc_property',
  270. '.objc_image_info',
  271. '.objc_cstring_object',
  272. '.objc_sel_fixup',
  273. '__DATA,__objc_data',
  274. '__DATA,__objc_const',
  275. '.objc_superrefs',
  276. '__DATA, __datacoal_nt,coalesced',
  277. '.objc_classlist',
  278. '.objc_nlclasslist',
  279. '.objc_catlist',
  280. '.obcj_nlcatlist',
  281. '.objc_protolist',
  282. '_STACK',
  283. '_HEAP',
  284. '.gcc_except_table',
  285. '.ARM.attributes'
  286. );
  287. begin
  288. if atype=sec_user then
  289. result:=aname
  290. else
  291. result:=secnames[atype];
  292. end;
  293. function TRelObjData.sectiontype2align(atype:TAsmSectiontype):longint;
  294. begin
  295. result:=1;
  296. end;
  297. procedure TRelObjData.writeReloc(Data: TRelocDataInt; len: aword; p: TObjSymbol; Reloctype: TObjRelocationType);
  298. var
  299. bytes: array [0..1] of Byte;
  300. symaddr: QWord;
  301. objreloc: TRelRelocation;
  302. begin
  303. if CurrObjSec=nil then
  304. internalerror(200403072);
  305. objreloc:=nil;
  306. if assigned(p) then
  307. begin
  308. { real address of the symbol }
  309. symaddr:=p.address;
  310. if p.bind=AB_EXTERNAL then
  311. begin
  312. objreloc:=TRelRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  313. if Reloctype in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
  314. objreloc.HiByte:=Byte(Data shr 8);
  315. CurrObjSec.ObjRelocations.Add(objreloc);
  316. end
  317. { relative relocations within the same section can be calculated directly,
  318. without the need to emit a relocation entry }
  319. else if (p.objsection=CurrObjSec) and
  320. (p.bind<>AB_COMMON) and
  321. (Reloctype=RELOC_RELATIVE) then
  322. {$push} {$R-}{$Q-}
  323. begin
  324. data:=data+symaddr-len-CurrObjSec.Size;
  325. end
  326. {$pop}
  327. else
  328. begin
  329. objreloc:=TRelRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
  330. inc(data,symaddr);
  331. if Reloctype in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
  332. objreloc.HiByte:=Byte(Data shr 8);
  333. CurrObjSec.ObjRelocations.Add(objreloc);
  334. end;
  335. end;
  336. case len of
  337. 2:
  338. begin
  339. bytes[0]:=Byte(Data);
  340. bytes[1]:=Byte(Data shr 8);
  341. writebytes(bytes,2);
  342. end;
  343. 1:
  344. begin
  345. bytes[0]:=Byte(Data);
  346. writebytes(bytes,1);
  347. end;
  348. else
  349. internalerror(2020050423);
  350. end;
  351. end;
  352. {*****************************************************************************
  353. TRelObjOutput
  354. *****************************************************************************}
  355. procedure TRelObjOutput.writeString(const S: ansistring);
  356. begin
  357. FWriter.write(S[1],Length(S));
  358. end;
  359. procedure TRelObjOutput.writeLine(const S: ansistring);
  360. begin
  361. writeString(S+#10)
  362. end;
  363. procedure TRelObjOutput.WriteAreaContentAndRelocations(sec: TObjSection);
  364. const
  365. MaxChunkSize={14}7;
  366. var
  367. ChunkStart,ChunkLen, i: LongWord;
  368. ChunkFixupStart,ChunkFixupEnd, j, st_ofs: Integer;
  369. st,sr: ansistring;
  370. buf: array [0..MaxChunkSize-1] of Byte;
  371. reloc: TRelRelocation;
  372. begin
  373. if (oso_data in sec.SecOptions) and (sec.Data=nil) then
  374. internalerror(200403073);
  375. if assigned(sec.data) then
  376. sec.data.seek(0);
  377. ChunkFixupStart:=0;
  378. ChunkFixupEnd:=-1;
  379. ChunkStart:=0;
  380. ChunkLen:=Min(MaxChunkSize, sec.size-ChunkStart);
  381. while ChunkLen>0 do
  382. begin
  383. { find last fixup in the chunk }
  384. while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
  385. (TRelRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
  386. inc(ChunkFixupEnd);
  387. { check if last chunk is crossing the chunk boundary, and trim ChunkLen if necessary }
  388. if (ChunkFixupEnd>=ChunkFixupStart) and
  389. ((TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset+
  390. TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).size)>(ChunkStart+ChunkLen)) then
  391. begin
  392. ChunkLen:=TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset-ChunkStart;
  393. Dec(ChunkFixupEnd);
  394. end;
  395. if ChunkLen>SizeOf(buf) then
  396. internalerror(2020050501);
  397. st:='T '+HexStr(Byte(ChunkStart),2)+' '+HexStr(Byte(ChunkStart shr 8),2);
  398. sr:='R 00 00 '+HexStr(Byte(sec.SecSymIdx),2)+' '+HexStr(Byte(sec.SecSymIdx shr 8),2);
  399. if assigned(sec.Data) then
  400. sec.Data.read(buf,ChunkLen)
  401. else
  402. FillChar(buf,ChunkLen,0);
  403. st_ofs:=1;
  404. { relocations present in the current chunk? }
  405. if ChunkFixupEnd>=ChunkFixupStart then
  406. begin
  407. j:=ChunkFixupStart;
  408. reloc:=TRelRelocation(sec.ObjRelocations[j]);
  409. end
  410. else
  411. begin
  412. j:=-1;
  413. reloc:=nil;
  414. end;
  415. for i:=0 to ChunkLen-1 do
  416. begin
  417. st:=st+' '+HexStr(buf[i],2);
  418. Inc(st_ofs);
  419. if assigned(reloc) then
  420. begin
  421. { advance to the current relocation }
  422. while (reloc.DataOffset<(ChunkStart+i)) and (j<ChunkFixupEnd) do
  423. begin
  424. Inc(j);
  425. reloc:=TRelRelocation(sec.ObjRelocations[j]);
  426. end;
  427. { is there a relocation at the current position? }
  428. if reloc.DataOffset=(ChunkStart+i) then
  429. begin
  430. sr:=sr+' '+reloc.EncodeFlags+' '+HexStr(st_ofs,2)+' '+HexStr(Byte(reloc.SecOrSymIdx),2)+' '+HexStr(Byte(reloc.SecOrSymIdx shr 8),2);
  431. if reloc.typ in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
  432. begin
  433. st:=st+' '+HexStr(reloc.HiByte,2);
  434. Inc(st_ofs);
  435. end;
  436. end;
  437. end;
  438. end;
  439. writeLine(st);
  440. writeLine(sr);
  441. { prepare next chunk }
  442. Inc(ChunkStart, ChunkLen);
  443. ChunkLen:=Min(MaxChunkSize, sec.size-ChunkStart);
  444. ChunkFixupStart:=ChunkFixupEnd+1;
  445. end;
  446. end;
  447. function TRelObjOutput.writeData(Data: TObjData): boolean;
  448. var
  449. global_symbols_count: Integer = 0;
  450. secidx, idx, i, j: Integer;
  451. objsym: TObjSymbol;
  452. objsec: TObjSection;
  453. begin
  454. global_symbols_count:=0;
  455. for i:=0 to Data.ObjSymbolList.Count-1 do
  456. begin
  457. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  458. if objsym.bind in [AB_EXTERNAL,AB_GLOBAL] then
  459. Inc(global_symbols_count);
  460. end;
  461. writeLine('XL2');
  462. writeLine('H '+tohex(data.ObjSectionList.Count)+' areas '+tohex(global_symbols_count)+' global symbols');
  463. idx:=0;
  464. for i:=0 to Data.ObjSymbolList.Count-1 do
  465. begin
  466. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  467. if objsym.bind=AB_EXTERNAL then
  468. begin
  469. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Ref0000');
  470. objsym.symidx:=idx;
  471. Inc(idx);
  472. end;
  473. end;
  474. secidx:=0;
  475. for i:=0 to Data.ObjSectionList.Count-1 do
  476. begin
  477. objsec:=TObjSection(Data.ObjSectionList[i]);
  478. writeLine('A '+objsec.Name+' size '+tohex(objsec.Size)+' flags 0 addr 0');
  479. objsec.SecSymIdx:=secidx;
  480. Inc(secidx);
  481. for j:=0 to Data.ObjSymbolList.Count-1 do
  482. begin
  483. objsym:=TObjSymbol(Data.ObjSymbolList[j]);
  484. if (objsym.bind=AB_GLOBAL) and (objsym.objsection=objsec) then
  485. begin
  486. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Def'+HexStr(objsym.offset,4));
  487. objsym.symidx:=idx;
  488. Inc(idx);
  489. end;
  490. end;
  491. end;
  492. for i:=0 to Data.ObjSectionList.Count-1 do
  493. begin
  494. objsec:=TObjSection(Data.ObjSectionList[i]);
  495. WriteAreaContentAndRelocations(objsec);
  496. end;
  497. result:=true;
  498. end;
  499. constructor TRelObjOutput.create(AWriter: TObjectWriter);
  500. begin
  501. inherited;
  502. cobjdata:=TRelObjData;
  503. end;
  504. {*****************************************************************************
  505. TRelAssembler
  506. *****************************************************************************}
  507. constructor TRelAssembler.create(info: pasminfo; smart: boolean);
  508. begin
  509. inherited;
  510. CObjOutput:=TRelObjOutput;
  511. CInternalAr:=tarobjectwriter;
  512. end;
  513. {*****************************************************************************
  514. TRelObjInput
  515. *****************************************************************************}
  516. function TRelObjInput.FillBuf: boolean;
  517. begin
  518. FBufPos:=0;
  519. FBufSize:=min(FReader.size-FReader.Pos,MaxBufSize);
  520. if FBufSize>0 then
  521. result:=FReader.read(FBuf,FBufSize)
  522. else
  523. result:=true;
  524. end;
  525. function TRelObjInput.AtEndOfBuf: boolean;
  526. begin
  527. result:=FBufPos=FBufSize;
  528. end;
  529. function TRelObjInput.AtEoF: boolean;
  530. begin
  531. result:=AtEndOfBuf and (FReader.Pos=FReader.size);
  532. end;
  533. function TRelObjInput.ReadChar(out c: char): boolean;
  534. begin
  535. c:=#0;
  536. if AtEndOfBuf then
  537. begin
  538. result:=FillBuf;
  539. if not result then
  540. exit;
  541. end;
  542. if not AtEndOfBuf then
  543. begin
  544. c:=FBuf[FBufPos];
  545. Inc(FBufPos);
  546. result:=true;
  547. end
  548. else
  549. result:=false;
  550. end;
  551. function TRelObjInput.PeekChar(out c: char): boolean;
  552. begin
  553. c:=#0;
  554. if AtEndOfBuf then
  555. begin
  556. result:=FillBuf;
  557. if not result then
  558. exit;
  559. end;
  560. if not AtEndOfBuf then
  561. begin
  562. c:=FBuf[FBufPos];
  563. result:=true;
  564. end
  565. else
  566. result:=false;
  567. end;
  568. function TRelObjInput.ReadLine(out s: string): boolean;
  569. var
  570. c: Char;
  571. begin
  572. s:='';
  573. c:=#0;
  574. if AtEoF then
  575. begin
  576. result:=false;
  577. exit;
  578. end;
  579. repeat
  580. if not AtEoF then
  581. begin
  582. if not ReadChar(c) then
  583. begin
  584. result:=false;
  585. exit;
  586. end;
  587. if not (c in [#13,#10]) then
  588. s:=s+c;
  589. end;
  590. until (c in [#13,#10]) or AtEoF;
  591. if (c=#13) and not AtEoF then
  592. begin
  593. if not PeekChar(c) then
  594. begin
  595. result:=false;
  596. exit;
  597. end;
  598. if c=#10 then
  599. begin
  600. if not ReadChar(c) then
  601. begin
  602. result:=false;
  603. exit;
  604. end;
  605. end;
  606. end;
  607. result:=true;
  608. end;
  609. constructor TRelObjInput.create;
  610. begin
  611. inherited create;
  612. cobjdata:=TRelObjData;
  613. FBufSize:=0;
  614. FBufPos:=0;
  615. end;
  616. function TRelObjInput.ReadObjData(AReader: TObjectreader; out Data: TObjData): boolean;
  617. function DecodeRelFlags(n1: Word): TRelRelocationFlags;
  618. begin
  619. result:=[];
  620. if (n1 and (1 shl 0))<>0 then
  621. include(result,rrfByte);
  622. if (n1 and (1 shl 1))<>0 then
  623. include(result,rrfSymbol);
  624. if (n1 and (1 shl 2))<>0 then
  625. include(result,rrfPcRelative);
  626. if (n1 and (1 shl 3))<>0 then
  627. include(result,rrfTwoByteObjectFormatForByteData);
  628. if (n1 and (1 shl 4))<>0 then
  629. include(result,rrfUnsignedByteData);
  630. if (n1 and (1 shl 5))<>0 then
  631. include(result,rrfPage0Reference);
  632. if (n1 and (1 shl 6))<>0 then
  633. include(result,rrfPageNNNReference);
  634. if (n1 and (1 shl 7))<>0 then
  635. include(result,rrfMSBWith2ByteMode);
  636. if (n1 and (1 shl 8))<>0 then
  637. include(result,rrfThreeByteObjectFormatForByteData);
  638. if (n1 and (1 shl 9))<>0 then
  639. include(result,rrfRealMSBForThreeByteMode);
  640. if (n1 and (1 shl 10))<>0 then
  641. include(result,rrfReserved10);
  642. if (n1 and (1 shl 11))<>0 then
  643. include(result,rrfReserved11);
  644. end;
  645. function HandleTR(const T,R: string): boolean;
  646. const
  647. GenericTErrMsg='Invalid T record';
  648. GenericRErrMsg='Invalid R record';
  649. UnsupportedRelocationFlags=[rrfPcRelative,rrfUnsignedByteData,
  650. rrfPage0Reference,rrfPageNNNReference,rrfThreeByteObjectFormatForByteData,
  651. rrfRealMSBForThreeByteMode,rrfReserved10,rrfReserved11];
  652. var
  653. ArrT, ArrR: array of byte;
  654. ArrTIsRelocHiByte: array of boolean;
  655. tmpint: Longint;
  656. i: Integer;
  657. AreaIndex, AreaOffset: Word;
  658. LastDataOfsIndex: Integer;
  659. LastDataOfsValue: TObjSectionOfs;
  660. ObjSec: TObjSection;
  661. n1, xx_xx: Word;
  662. n1x, n2, RelHiByte: Byte;
  663. RelFlags: TRelRelocationFlags;
  664. reloc:TRelRelocation;
  665. RelocDataOffset: TObjSectionOfs;
  666. RelocTyp: TObjRelocationType;
  667. zeros_only: Boolean;
  668. begin
  669. result:=false;
  670. if (length(T)<5) or (((length(T)-2) mod 3)<>0) then
  671. begin
  672. InputError(GenericTErrMsg);
  673. exit;
  674. end;
  675. if (length(R)<11) or (((length(R)-2) mod 3)<>0) then
  676. begin
  677. InputError(GenericRErrMsg);
  678. exit;
  679. end;
  680. SetLength(ArrT,((length(T)-2) div 3)+1);
  681. for i:=0 to length(ArrT)-1 do
  682. begin
  683. if (i>0) and (T[i*3]<>' ') then
  684. begin
  685. InputError(GenericTErrMsg);
  686. exit;
  687. end;
  688. if not TryStrToInt('$'+copy(T,1+i*3,2),tmpint) then
  689. begin
  690. InputError(GenericTErrMsg);
  691. exit;
  692. end;
  693. if (tmpint<0) or (tmpint>255) then
  694. begin
  695. InputError(GenericTErrMsg);
  696. exit;
  697. end;
  698. ArrT[i]:=tmpint;
  699. end;
  700. SetLength(ArrR,((length(R)-2) div 3)+1);
  701. for i:=0 to length(ArrR)-1 do
  702. begin
  703. if (i>0) and (R[i*3]<>' ') then
  704. begin
  705. InputError(GenericRErrMsg);
  706. exit;
  707. end;
  708. if not TryStrToInt('$'+copy(R,1+i*3,2),tmpint) then
  709. begin
  710. InputError(GenericRErrMsg);
  711. exit;
  712. end;
  713. if (tmpint<0) or (tmpint>255) then
  714. begin
  715. InputError(GenericRErrMsg);
  716. exit;
  717. end;
  718. ArrR[i]:=tmpint;
  719. end;
  720. if (length(ArrT)<2) or (length(ArrR)<4) then
  721. internalerror(2020060201);
  722. if (ArrR[0]<>0) or (ArrR[1]<>0) then
  723. begin
  724. InputError(GenericRErrMsg);
  725. exit;
  726. end;
  727. AreaIndex:=(ArrR[3] shl 8) or ArrR[2];
  728. AreaOffset:=(ArrT[1] shl 8) or ArrT[0];
  729. if AreaIndex>=Data.ObjSectionList.Count then
  730. begin
  731. InputError('Area index in R record out of bounds');
  732. exit;
  733. end;
  734. ObjSec:=TObjSection(Data.ObjSectionList[AreaIndex]);
  735. if AreaOffset>ObjSec.Size then
  736. begin
  737. InputError('Area offset in T exceeds area size');
  738. exit;
  739. end;
  740. { section name is '_BSS'/'_STACK'/'_HEAP' and there are no relocations }
  741. if ((ObjSec.Name='_BSS') or (ObjSec.Name='_STACK') or (ObjSec.Name='_HEAP')) and
  742. (length(ArrR)=4) then
  743. begin
  744. zeros_only:=true;
  745. for i:=2 to length(ArrT)-1 do
  746. if ArrT[i]<>0 then
  747. begin
  748. zeros_only:=false;
  749. break;
  750. end;
  751. { avoid setting the oso_Data flag on .bss sections, if there are no relocations and all data is zero }
  752. if zeros_only then
  753. exit;
  754. end;
  755. { parse relocations }
  756. SetLength(ArrTIsRelocHiByte,Length(ArrT));
  757. LastDataOfsIndex:=2;
  758. LastDataOfsValue:=AreaOffset;
  759. i:=4;
  760. while i<length(ArrR) do
  761. begin
  762. n1:=ArrR[i];
  763. Inc(i);
  764. if (n1 and $F0)=$F0 then
  765. begin
  766. if i>=length(ArrR) then
  767. begin
  768. InputError(GenericRErrMsg);
  769. exit;
  770. end;
  771. n1x:=ArrR[i];
  772. Inc(i);
  773. n1:=((n1 and $0F) shl 8) or n1x;
  774. end;
  775. if (i+2)>=length(ArrR) then
  776. begin
  777. InputError(GenericRErrMsg);
  778. exit;
  779. end;
  780. n2:=ArrR[i];
  781. xx_xx:=ArrR[i+1] or (ArrR[i+2] shl 8);
  782. Inc(i,3);
  783. RelFlags:=DecodeRelFlags(n1);
  784. if ((RelFlags*UnsupportedRelocationFlags)<>[]) or
  785. ((rrfByte in RelFlags) xor (rrfTwoByteObjectFormatForByteData in RelFlags)) then
  786. begin
  787. InputError('Unsupported relocation flags ($'+HexStr(n1,3)+')');
  788. exit;
  789. end;
  790. if n2<=1 then
  791. begin
  792. InputError('Invalid relocation data offset');
  793. exit;
  794. end;
  795. if rrfByte in RelFlags then
  796. begin
  797. if rrfMSBWith2ByteMode in RelFlags then
  798. RelocTyp:=RELOC_ABSOLUTE_HI8
  799. else
  800. RelocTyp:=RELOC_ABSOLUTE_LO8;
  801. if (n2+1)>=length(ArrT) then
  802. begin
  803. InputError('Invalid relocation data offset');
  804. exit;
  805. end;
  806. ArrTIsRelocHiByte[n2+1]:=true;
  807. RelHiByte:=ArrT[n2+1];
  808. end
  809. else
  810. begin
  811. RelocTyp:=RELOC_ABSOLUTE;
  812. if n2>=length(ArrT) then
  813. begin
  814. InputError('Invalid relocation data offset');
  815. exit;
  816. end;
  817. RelHiByte:=0;
  818. end;
  819. while LastDataOfsIndex<n2 do
  820. begin
  821. if not ArrTIsRelocHiByte[LastDataOfsIndex] then
  822. Inc(LastDataOfsValue);
  823. Inc(LastDataOfsIndex);
  824. end;
  825. RelocDataOffset:=LastDataOfsValue;
  826. if rrfSymbol in RelFlags then
  827. begin
  828. if xx_xx>=Data.ObjSymbolList.Count then
  829. begin
  830. InputError('Relocation to symbol with invalid index');
  831. exit;
  832. end;
  833. reloc:=TRelRelocation.CreateSymbol(RelocDataOffset,TObjSymbol(Data.ObjSymbolList[xx_xx]),RelocTyp);
  834. end
  835. else
  836. begin
  837. if xx_xx>=Data.ObjSectionlist.Count then
  838. begin
  839. InputError('Relocation to area with invalid index');
  840. exit;
  841. end;
  842. reloc:=TRelRelocation.CreateSection(RelocDataOffset,TObjSection(Data.ObjSectionlist[xx_xx]),RelocTyp);
  843. end;
  844. reloc.RelFlags:=RelFlags;
  845. reloc.HiByte:=RelHiByte;
  846. objsec.ObjRelocations.Add(reloc);
  847. end;
  848. { read the data }
  849. objsec.SecOptions:=objsec.SecOptions+[oso_Data];
  850. objsec.Data.seek(AreaOffset);
  851. for i:=2 to length(ArrT)-1 do
  852. if not ArrTIsRelocHiByte[i] then
  853. objsec.Data.write(ArrT[i],1);
  854. result:=true;
  855. end;
  856. const
  857. GenericRelErrMsg='Error reading REL file';
  858. var
  859. s, AreaName, SymbolName: string;
  860. RecType: Char;
  861. HeaderFound: Boolean=false;
  862. ExpectedAreas,ExpectedSymbols,AreaSize,AreaFlags,AreaAddr,
  863. SymbolOfs: LongInt;
  864. tmpint: SizeInt;
  865. CurrSec: TObjSection=nil;
  866. objsym: TObjSymbol;
  867. LastT: string='';
  868. begin
  869. FReader:=AReader;
  870. InputFileName:=AReader.FileName;
  871. Data:=CObjData.Create(InputFileName);
  872. ExpectedAreas:=-1;
  873. ExpectedSymbols:=-1;
  874. result:=false;
  875. s:='';
  876. repeat
  877. if AtEoF or not ReadLine(s) then
  878. begin
  879. InputError(GenericRelErrMsg);
  880. exit;
  881. end;
  882. s:=Trim(s);
  883. until s<>'';
  884. if s<>'XL2' then
  885. begin
  886. InputError('Invalid or unsupported REL format identifier');
  887. exit;
  888. end;
  889. while not AtEoF do
  890. begin
  891. if not ReadLine(s) then
  892. begin
  893. InputError(GenericRelErrMsg);
  894. exit;
  895. end;
  896. s:=Trim(s);
  897. if s<>'' then
  898. begin
  899. RecType:=s[1];
  900. if (length(s)<3) or (s[2]<>' ') then
  901. begin
  902. InputError('Invalid or unsupported REL record');
  903. exit;
  904. end;
  905. delete(s,1,2);
  906. case RecType of
  907. 'H': { header }
  908. begin
  909. if HeaderFound then
  910. begin
  911. InputError('Duplicated header');
  912. exit;
  913. end;
  914. HeaderFound:=true;
  915. tmpint:=Pos(' ',s);
  916. if not TryStrToInt('$'+Copy(s,1,tmpint-1),ExpectedAreas) then
  917. begin
  918. InputError('Invalid area count in header');
  919. exit;
  920. end;
  921. delete(s,1,tmpint);
  922. if copy(s,1,6)<>'areas ' then
  923. begin
  924. InputError('Invalid header');
  925. exit;
  926. end;
  927. delete(s,1,6);
  928. tmpint:=Pos(' ',s);
  929. if not TryStrToInt('$'+Copy(s,1,tmpint-1),ExpectedSymbols) then
  930. begin
  931. InputError('Invalid symbol count in header');
  932. exit;
  933. end;
  934. delete(s,1,tmpint);
  935. if s<>'global symbols' then
  936. begin
  937. InputError('Invalid header');
  938. exit;
  939. end;
  940. end;
  941. 'M': { module }
  942. begin
  943. { we ignore this for now }
  944. end;
  945. 'S': { symbol }
  946. begin
  947. if not HeaderFound then
  948. begin
  949. InputError('Symbol record encountered before header');
  950. exit;
  951. end;
  952. tmpint:=Pos(' ',s);
  953. if tmpint<=1 then
  954. begin
  955. InputError('Invalid symbol record');
  956. exit;
  957. end;
  958. SymbolName:=copy(s,1,tmpint-1);
  959. delete(s,1,tmpint);
  960. if Length(s)<4 then
  961. begin
  962. InputError('Invalid symbol record');
  963. exit;
  964. end;
  965. if not TryStrToInt('$'+Copy(s,4,Length(s)-3),SymbolOfs) then
  966. begin
  967. InputError('Invalid symbol offset');
  968. exit;
  969. end;
  970. case Copy(s,1,3) of
  971. 'Def':
  972. begin
  973. if CurrSec=nil then
  974. begin
  975. InputError('Public symbol defined outside any area');
  976. exit;
  977. end;
  978. if (SymbolOfs<0) or (SymbolOfs>CurrSec.Size) then
  979. begin
  980. InputError('Public symbol offset outside the range of the current area');
  981. exit;
  982. end;
  983. objsym:=Data.CreateSymbol(SymbolName);
  984. objsym.bind:=AB_GLOBAL;
  985. objsym.typ:=AT_FUNCTION;
  986. objsym.objsection:=CurrSec;
  987. objsym.offset:=SymbolOfs;
  988. objsym.size:=0;
  989. end;
  990. 'Ref':
  991. begin
  992. if CurrSec<>nil then
  993. begin
  994. InputError('External symbols must be defined before the first area');
  995. exit;
  996. end;
  997. if SymbolOfs<>0 then
  998. begin
  999. InputError('External symbols must be declared with an offset of 0');
  1000. exit;
  1001. end;
  1002. objsym:=Data.CreateSymbol(SymbolName);
  1003. objsym.bind:=AB_EXTERNAL;
  1004. objsym.typ:=AT_FUNCTION;
  1005. objsym.objsection:=nil;
  1006. objsym.offset:=0;
  1007. objsym.size:=0;
  1008. end;
  1009. else
  1010. begin
  1011. InputError('Invalid or unsupported symbol record');
  1012. exit;
  1013. end;
  1014. end;
  1015. if Data.ObjSymbolList.Count>ExpectedSymbols then
  1016. begin
  1017. InputError('Number of symbols exceeds the number, declared in header');
  1018. exit;
  1019. end;
  1020. end;
  1021. 'A': { area }
  1022. begin
  1023. if not HeaderFound then
  1024. begin
  1025. InputError('Area record encountered before header');
  1026. exit;
  1027. end;
  1028. tmpint:=Pos(' ',s);
  1029. if tmpint<=1 then
  1030. begin
  1031. InputError('Invalid area record');
  1032. exit;
  1033. end;
  1034. AreaName:=copy(s,1,tmpint-1);
  1035. delete(s,1,tmpint);
  1036. if copy(s,1,5)<>'size ' then
  1037. begin
  1038. InputError('Invalid area record');
  1039. exit;
  1040. end;
  1041. delete(s,1,5);
  1042. tmpint:=Pos(' ',s);
  1043. if not TryStrToInt('$'+Copy(s,1,tmpint-1),AreaSize) then
  1044. begin
  1045. InputError('Invalid area size');
  1046. exit;
  1047. end;
  1048. delete(s,1,tmpint);
  1049. if copy(s,1,6)<>'flags ' then
  1050. begin
  1051. InputError('Invalid area record');
  1052. exit;
  1053. end;
  1054. delete(s,1,6);
  1055. tmpint:=Pos(' ',s);
  1056. if not TryStrToInt('$'+Copy(s,1,tmpint-1),AreaFlags) then
  1057. begin
  1058. InputError('Invalid area flags');
  1059. exit;
  1060. end;
  1061. delete(s,1,tmpint);
  1062. if copy(s,1,5)<>'addr ' then
  1063. begin
  1064. InputError('Invalid area record');
  1065. exit;
  1066. end;
  1067. delete(s,1,5);
  1068. if not TryStrToInt('$'+Copy(s,1,tmpint-1),AreaAddr) then
  1069. begin
  1070. InputError('Invalid area address');
  1071. exit;
  1072. end;
  1073. if AreaFlags<>0 then
  1074. begin
  1075. InputError('Unsupported area flags ('+tostr(AreaFlags)+')');
  1076. exit;
  1077. end;
  1078. if AreaAddr<>0 then
  1079. begin
  1080. InputError('Area address<>0 not supported');
  1081. exit;
  1082. end;
  1083. CurrSec:=Data.createsection(AreaName,1,[],false);
  1084. CurrSec.alloc(AreaSize);
  1085. if Data.ObjSectionList.Count>ExpectedAreas then
  1086. begin
  1087. InputError('Number of areas exceeds the number, declared in header');
  1088. exit;
  1089. end;
  1090. end;
  1091. 'T': { T line () }
  1092. begin
  1093. if LastT<>'' then
  1094. begin
  1095. InputError('T record not followed by R record');
  1096. exit;
  1097. end;
  1098. LastT:=s;
  1099. end;
  1100. 'R': { R line (relocation information) }
  1101. begin
  1102. if LastT='' then
  1103. begin
  1104. InputError('R record without T record');
  1105. exit;
  1106. end;
  1107. if not HandleTR(LastT,s) then
  1108. exit;
  1109. LastT:='';
  1110. end;
  1111. 'P': { P line (paging information) }
  1112. begin
  1113. InputError('P line records are not supported');
  1114. exit;
  1115. end;
  1116. else
  1117. begin
  1118. InputError('Unsupported REL record type: #'+tostr(Ord(RecType)));
  1119. exit;
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. result:=true;
  1125. end;
  1126. class function TRelObjInput.CanReadObjData(AReader: TObjectreader): boolean;
  1127. var
  1128. s: string;
  1129. instance: TRelObjInput;
  1130. begin
  1131. result:=false;
  1132. instance:=TRelObjInput.Create;
  1133. instance.FReader:=AReader;
  1134. with instance do
  1135. while not AtEoF do
  1136. begin
  1137. if not ReadLine(s) then
  1138. exit;
  1139. s:=Trim(s);
  1140. if s<>'' then
  1141. begin
  1142. result:=s='XL2';
  1143. break;
  1144. end;
  1145. end;
  1146. instance.Free;
  1147. end;
  1148. {*****************************************************************************
  1149. TIntelHexExeOutput
  1150. *****************************************************************************}
  1151. procedure TIntelHexExeOutput.writeString(const S: ansistring);
  1152. begin
  1153. FWriter.write(S[1],Length(S));
  1154. end;
  1155. procedure TIntelHexExeOutput.writeLine(const S: ansistring);
  1156. begin
  1157. writeString(S+#10)
  1158. end;
  1159. function TIntelHexExeOutput.writeData: boolean;
  1160. const
  1161. MaxRecLen=16;
  1162. var
  1163. exesec: TExeSection;
  1164. objsec: TObjSection;
  1165. exesec_i, objsec_i: Integer;
  1166. s: string;
  1167. blocklen, i: integer;
  1168. buf: array [0..MaxRecLen-1] of Byte;
  1169. blockaddr: Word;
  1170. checksum: Byte;
  1171. begin
  1172. result:=false;
  1173. for exesec_i:=0 to ExeSectionList.Count-1 do
  1174. begin
  1175. exesec:=TExeSection(ExeSectionList[exesec_i]);
  1176. for objsec_i:=0 to exesec.ObjSectionList.Count-1 do
  1177. begin
  1178. objsec:=TObjSection(exesec.ObjSectionList[objsec_i]);
  1179. if oso_Data in objsec.SecOptions then
  1180. begin
  1181. objsec.Data.seek(0);
  1182. while objsec.Data.Pos<objsec.Data.size do
  1183. begin
  1184. blocklen:=Min(objsec.Data.size-objsec.Data.Pos,MaxRecLen);
  1185. blockaddr:=objsec.Data.Pos+objsec.MemPos+ImageBase;
  1186. s:=':'+HexStr(blocklen,2)+HexStr(blockaddr,4)+'00';
  1187. checksum:=Byte(blocklen)+Byte(blockaddr shr 8)+Byte(blockaddr)+0;
  1188. if objsec.Data.read(buf,blocklen)<>blocklen then
  1189. internalerror(2020060301);
  1190. for i:=0 to blocklen-1 do
  1191. begin
  1192. s:=s+HexStr(buf[i],2);
  1193. checksum:=Byte(checksum+buf[i]);
  1194. end;
  1195. checksum:=$100-checksum;
  1196. s:=s+HexStr(checksum,2);
  1197. writeLine(s);
  1198. end;
  1199. end;
  1200. end;
  1201. end;
  1202. writeLine(':00000001FF');
  1203. result:=true;
  1204. end;
  1205. procedure TIntelHexExeOutput.DoRelocationFixup(objsec: TObjSection);
  1206. var
  1207. i: Integer;
  1208. objreloc: TRelRelocation;
  1209. target,w: Word;
  1210. b: Byte;
  1211. begin
  1212. for i:=0 to objsec.ObjRelocations.Count-1 do
  1213. begin
  1214. objreloc:=TRelRelocation(objsec.ObjRelocations[i]);
  1215. if assigned(objreloc.symbol) then
  1216. target:=objreloc.symbol.address+ImageBase
  1217. else if assigned(objreloc.objsection) then
  1218. target:=objreloc.objsection.MemPos+ImageBase
  1219. else
  1220. internalerror(2020060302);
  1221. case objreloc.typ of
  1222. RELOC_ABSOLUTE:
  1223. begin
  1224. objsec.Data.seek(objreloc.DataOffset);
  1225. objsec.Data.read(w,2);
  1226. w:=LEtoN(w);
  1227. Inc(w,target);
  1228. w:=LEtoN(w);
  1229. objsec.Data.seek(objreloc.DataOffset);
  1230. objsec.Data.write(w,2);
  1231. end;
  1232. RELOC_ABSOLUTE_HI8:
  1233. begin
  1234. objsec.Data.seek(objreloc.DataOffset);
  1235. objsec.Data.read(b,1);
  1236. w:=b or (objreloc.HiByte shl 8);
  1237. Inc(w,target);
  1238. b:=Byte(w shr 8);
  1239. objsec.Data.seek(objreloc.DataOffset);
  1240. objsec.Data.write(b,1);
  1241. end;
  1242. RELOC_ABSOLUTE_LO8:
  1243. begin
  1244. objsec.Data.seek(objreloc.DataOffset);
  1245. objsec.Data.read(b,1);
  1246. w:=b or (objreloc.HiByte shl 8);
  1247. Inc(w,target);
  1248. b:=Byte(w);
  1249. objsec.Data.seek(objreloc.DataOffset);
  1250. objsec.Data.write(b,1);
  1251. end;
  1252. else
  1253. internalerror(2020060303);
  1254. end;
  1255. end;
  1256. end;
  1257. constructor TIntelHexExeOutput.create;
  1258. begin
  1259. inherited create;
  1260. CObjData:=TRelObjData;
  1261. MaxMemPos:=$FFFF;
  1262. end;
  1263. {*****************************************************************************
  1264. TZXSpectrumIntelHexExeOutput
  1265. *****************************************************************************}
  1266. constructor TZXSpectrumIntelHexExeOutput.create;
  1267. begin
  1268. inherited create;
  1269. { The ZX Spectrum RTL switches to interrupt mode 2, and install an
  1270. interrupt handler + table, starting at address $FDFD, so we must limit
  1271. program size to $FDFC }
  1272. MaxMemPos:=$FDFC;
  1273. end;
  1274. {*****************************************************************************
  1275. Initialize
  1276. *****************************************************************************}
  1277. const
  1278. as_z80_rel_info : tasminfo =
  1279. (
  1280. id : as_z80_rel;
  1281. idtxt : 'REL';
  1282. asmbin : '';
  1283. asmcmd : '';
  1284. supported_targets : [system_z80_embedded,system_z80_zxspectrum,system_z80_msxdos];
  1285. flags : [af_outputbinary,af_smartlink_sections];
  1286. labelprefix : '..@';
  1287. labelmaxlen : 79;
  1288. comment : '; ';
  1289. dollarsign: '$';
  1290. );
  1291. initialization
  1292. RegisterAssembler(as_z80_rel_info,TRelAssembler);
  1293. end.