ogrel.pas 46 KB

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