1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- {
- Copyright (c) 2020 by Nikolay Nikolov
- Contains the ASCII relocatable object file format (*.rel) reader and writer
- This is the object format used on the Z80 platforms.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit ogrel;
- {$i fpcdefs.inc}
- interface
- uses
- { common }
- cclasses,globtype,
- { target }
- systems,
- { assembler }
- cpuinfo,cpubase,aasmbase,assemble,link,
- { output }
- ogbase,
- owbase;
- type
- TRelRelocationFlag=(
- rrfByte, { bit 0 }
- rrfSymbol, { bit 1 }
- rrfPcRelative, { bit 2 }
- rrfTwoByteObjectFormatForByteData, { bit 3 }
- rrfUnsignedByteData, { bit 4 }
- rrfPage0Reference, { bit 5 }
- rrfPageNNNReference, { bit 6 }
- rrfMSBWith2ByteMode, { bit 7 }
- rrfThreeByteObjectFormatForByteData, { bit 8 }
- rrfRealMSBForThreeByteMode, { bit 9 }
- rrfReserved10, { bit 10 }
- rrfReserved11); { bit 11 }
- TRelRelocationFlags=set of TRelRelocationFlag;
- { TRelRelocation }
- TRelRelocation = class(TObjRelocation)
- private
- function GetSecOrSymIdx: longint;
- public
- RelFlags: TRelRelocationFlags;
- HiByte: Byte;
- constructor CreateSymbol(ADataOffset:TObjSectionOfs;s:TObjSymbol;Atyp:TObjRelocationType);
- constructor CreateSection(ADataOffset:TObjSectionOfs;aobjsec:TObjSection;Atyp:TObjRelocationType);
- function EncodeFlags: string;
- property SecOrSymIdx: longint read GetSecOrSymIdx;
- end;
- { TRelObjData }
- TRelObjData = class(TObjData)
- public
- function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
- function sectiontype2align(atype:TAsmSectiontype):longint;override;
- procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
- end;
- { TRelObjOutput }
- TRelObjOutput = class(tObjOutput)
- private
- procedure writeString(const S: ansistring);
- procedure writeLine(const S: ansistring);
- procedure WriteAreaContentAndRelocations(sec: TObjSection);
- protected
- function writeData(Data:TObjData):boolean;override;
- public
- constructor create(AWriter:TObjectWriter);override;
- end;
- { TRelAssembler }
- TRelAssembler = class(tinternalassembler)
- constructor create(info: pasminfo; smart:boolean);override;
- end;
- { TRelObjInput }
- TRelObjInput = class(TObjInput)
- private const
- MaxBufSize=512;
- private
- FBuf: array [0..MaxBufSize-1] of Char;
- FBufSize: Integer;
- FBufPos: Integer;
- function FillBuf: boolean;
- function AtEndOfBuf: boolean;
- function AtEoF: boolean;
- function ReadChar(out c: char): boolean;
- function PeekChar(out c: char): boolean;
- function ReadLine(out s: string): boolean;
- public
- constructor create;override;
- function ReadObjData(AReader:TObjectreader;out Data:TObjData):boolean;override;
- class function CanReadObjData(AReader:TObjectreader):boolean;override;
- end;
- { TIntelHexExeOutput }
- TIntelHexExeOutput = class(TExeOutput)
- protected
- function writeData:boolean;override;
- procedure DoRelocationFixup(objsec:TObjSection);override;
- public
- constructor create;override;
- end;
- implementation
- uses
- SysUtils,
- cutils,verbose,globals,
- fmodule,aasmtai,aasmdata,
- ogmap,owar,
- version
- ;
- function tohex(q: qword): string;
- begin
- result:=HexStr(q,16);
- while (Length(result)>1) and (result[1]='0') do
- delete(result,1,1);
- end;
- {*****************************************************************************
- TRelRelocation
- *****************************************************************************}
- function TRelRelocation.GetSecOrSymIdx: longint;
- begin
- if assigned(symbol) then
- result:=symbol.symidx
- else if assigned(objsection) then
- result:=objsection.SecSymIdx
- else
- internalerror(2020050502);
- end;
- constructor TRelRelocation.CreateSymbol(ADataOffset: TObjSectionOfs; s: TObjSymbol; Atyp: TObjRelocationType);
- begin
- inherited;
- case Atyp of
- RELOC_ABSOLUTE_HI8:
- begin
- size:=1;
- RelFlags:=[rrfSymbol,rrfByte,rrfTwoByteObjectFormatForByteData,rrfMSBWith2ByteMode];
- end;
- RELOC_ABSOLUTE_LO8:
- begin
- size:=1;
- RelFlags:=[rrfSymbol,rrfByte,rrfTwoByteObjectFormatForByteData];
- end;
- RELOC_ABSOLUTE:
- begin
- size:=2;
- RelFlags:=[rrfSymbol];
- end;
- else
- internalerror(2020050601);
- end;
- end;
- constructor TRelRelocation.CreateSection(ADataOffset: TObjSectionOfs; aobjsec: TObjSection; Atyp: TObjRelocationType);
- begin
- inherited;
- case Atyp of
- RELOC_ABSOLUTE_HI8:
- begin
- size:=1;
- RelFlags:=[rrfByte,rrfTwoByteObjectFormatForByteData,rrfMSBWith2ByteMode];
- end;
- RELOC_ABSOLUTE_LO8:
- begin
- size:=1;
- RelFlags:=[rrfByte,rrfTwoByteObjectFormatForByteData];
- end;
- RELOC_ABSOLUTE:
- begin
- size:=2;
- RelFlags:=[];
- end;
- else
- internalerror(2020050601);
- end;
- end;
- function TRelRelocation.EncodeFlags: string;
- var
- FlagsWord: Word;
- begin
- FlagsWord:=0;
- if rrfByte in RelFlags then
- Inc(FlagsWord,1);
- if rrfSymbol in RelFlags then
- Inc(FlagsWord,2);
- if rrfPcRelative in RelFlags then
- Inc(FlagsWord,4);
- if rrfTwoByteObjectFormatForByteData in RelFlags then
- Inc(FlagsWord,8);
- if rrfUnsignedByteData in RelFlags then
- Inc(FlagsWord,16);
- if rrfPage0Reference in RelFlags then
- Inc(FlagsWord,32);
- if rrfPageNNNReference in RelFlags then
- Inc(FlagsWord,64);
- if rrfMSBWith2ByteMode in RelFlags then
- Inc(FlagsWord,128);
- if rrfThreeByteObjectFormatForByteData in RelFlags then
- Inc(FlagsWord,256);
- if rrfRealMSBForThreeByteMode in RelFlags then
- Inc(FlagsWord,512);
- if rrfReserved10 in RelFlags then
- Inc(FlagsWord,1024);
- if rrfReserved11 in RelFlags then
- Inc(FlagsWord,2048);
- if (FlagsWord<=255) and ((FlagsWord and $F0)<>$F0) then
- Result:=HexStr(FlagsWord,2)
- else
- Result:=HexStr($F0 or Byte(FlagsWord shr 8),2)+' '+HexStr(Byte(FlagsWord),2);
- end;
- {*****************************************************************************
- TRelObjData
- *****************************************************************************}
- function TRelObjData.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
- const
- secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
- '_CODE',
- '_DATA',
- '_DATA',
- '.rodata',
- '.bss',
- '.threadvar',
- '.pdata',
- '', { stubs }
- '__DATA,__nl_symbol_ptr',
- '__DATA,__la_symbol_ptr',
- '__DATA,__mod_init_func',
- '__DATA,__mod_term_func',
- '.stab',
- '.stabstr',
- '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
- '.eh_frame',
- '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
- '.fpc',
- '.toc',
- '.init',
- '.fini',
- '.objc_class',
- '.objc_meta_class',
- '.objc_cat_cls_meth',
- '.objc_cat_inst_meth',
- '.objc_protocol',
- '.objc_string_object',
- '.objc_cls_meth',
- '.objc_inst_meth',
- '.objc_cls_refs',
- '.objc_message_refs',
- '.objc_symbols',
- '.objc_category',
- '.objc_class_vars',
- '.objc_instance_vars',
- '.objc_module_info',
- '.objc_class_names',
- '.objc_meth_var_types',
- '.objc_meth_var_names',
- '.objc_selector_strs',
- '.objc_protocol_ext',
- '.objc_class_ext',
- '.objc_property',
- '.objc_image_info',
- '.objc_cstring_object',
- '.objc_sel_fixup',
- '__DATA,__objc_data',
- '__DATA,__objc_const',
- '.objc_superrefs',
- '__DATA, __datacoal_nt,coalesced',
- '.objc_classlist',
- '.objc_nlclasslist',
- '.objc_catlist',
- '.obcj_nlcatlist',
- '.objc_protolist',
- '.stack',
- '.heap',
- '.gcc_except_table',
- '.ARM.attributes'
- );
- begin
- result:=secnames[atype];
- end;
- function TRelObjData.sectiontype2align(atype:TAsmSectiontype):longint;
- begin
- result:=1;
- end;
- procedure TRelObjData.writeReloc(Data: TRelocDataInt; len: aword; p: TObjSymbol; Reloctype: TObjRelocationType);
- var
- bytes: array [0..1] of Byte;
- symaddr: QWord;
- objreloc: TRelRelocation;
- begin
- if CurrObjSec=nil then
- internalerror(200403072);
- objreloc:=nil;
- if assigned(p) then
- begin
- { real address of the symbol }
- symaddr:=p.address;
- if p.bind=AB_EXTERNAL then
- begin
- objreloc:=TRelRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
- if Reloctype in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
- objreloc.HiByte:=Byte(Data shr 8);
- CurrObjSec.ObjRelocations.Add(objreloc);
- end
- { relative relocations within the same section can be calculated directly,
- without the need to emit a relocation entry }
- else if (p.objsection=CurrObjSec) and
- (p.bind<>AB_COMMON) and
- (Reloctype=RELOC_RELATIVE) then
- begin
- data:=data+symaddr-len-CurrObjSec.Size;
- end
- else
- begin
- objreloc:=TRelRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
- inc(data,symaddr);
- if Reloctype in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
- objreloc.HiByte:=Byte(Data shr 8);
- CurrObjSec.ObjRelocations.Add(objreloc);
- end;
- end;
- case len of
- 2:
- begin
- bytes[0]:=Byte(Data);
- bytes[1]:=Byte(Data shr 8);
- writebytes(bytes,2);
- end;
- 1:
- begin
- bytes[0]:=Byte(Data);
- writebytes(bytes,1);
- end;
- else
- internalerror(2020050423);
- end;
- end;
- {*****************************************************************************
- TRelObjOutput
- *****************************************************************************}
- procedure TRelObjOutput.writeString(const S: ansistring);
- begin
- FWriter.write(S[1],Length(S));
- end;
- procedure TRelObjOutput.writeLine(const S: ansistring);
- begin
- writeString(S+#10)
- end;
- procedure TRelObjOutput.WriteAreaContentAndRelocations(sec: TObjSection);
- const
- MaxChunkSize={14}7;
- var
- ChunkStart,ChunkLen, i: LongWord;
- ChunkFixupStart,ChunkFixupEnd, j, st_ofs: Integer;
- st,sr: ansistring;
- buf: array [0..MaxChunkSize-1] of Byte;
- reloc: TRelRelocation;
- begin
- if (oso_data in sec.SecOptions) and (sec.Data=nil) then
- internalerror(200403073);
- if assigned(sec.data) then
- sec.data.seek(0);
- ChunkFixupStart:=0;
- ChunkFixupEnd:=-1;
- ChunkStart:=0;
- ChunkLen:=Min(MaxChunkSize, sec.size-ChunkStart);
- while ChunkLen>0 do
- begin
- { find last fixup in the chunk }
- while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
- (TRelRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
- inc(ChunkFixupEnd);
- { check if last chunk is crossing the chunk boundary, and trim ChunkLen if necessary }
- if (ChunkFixupEnd>=ChunkFixupStart) and
- ((TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset+
- TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).size)>(ChunkStart+ChunkLen)) then
- begin
- ChunkLen:=TRelRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset-ChunkStart;
- Dec(ChunkFixupEnd);
- end;
- if ChunkLen>SizeOf(buf) then
- internalerror(2020050501);
- st:='T '+HexStr(Byte(ChunkStart),2)+' '+HexStr(Byte(ChunkStart shr 8),2);
- sr:='R 00 00 '+HexStr(Byte(sec.SecSymIdx),2)+' '+HexStr(Byte(sec.SecSymIdx shr 8),2);
- if assigned(sec.Data) then
- sec.Data.read(buf,ChunkLen)
- else
- FillChar(buf,ChunkLen,0);
- st_ofs:=1;
- { relocations present in the current chunk? }
- if ChunkFixupEnd>=ChunkFixupStart then
- begin
- j:=ChunkFixupStart;
- reloc:=TRelRelocation(sec.ObjRelocations[j]);
- end
- else
- begin
- j:=-1;
- reloc:=nil;
- end;
- for i:=0 to ChunkLen-1 do
- begin
- st:=st+' '+HexStr(buf[i],2);
- Inc(st_ofs);
- if assigned(reloc) then
- begin
- { advance to the current relocation }
- while (reloc.DataOffset<(ChunkStart+i)) and (j<ChunkFixupEnd) do
- begin
- Inc(j);
- reloc:=TRelRelocation(sec.ObjRelocations[j]);
- end;
- { is there a relocation at the current position? }
- if reloc.DataOffset=(ChunkStart+i) then
- begin
- sr:=sr+' '+reloc.EncodeFlags+' '+HexStr(st_ofs,2)+' '+HexStr(Byte(reloc.SecOrSymIdx),2)+' '+HexStr(Byte(reloc.SecOrSymIdx shr 8),2);
- if reloc.typ in [RELOC_ABSOLUTE_HI8,RELOC_ABSOLUTE_LO8] then
- begin
- st:=st+' '+HexStr(reloc.HiByte,2);
- Inc(st_ofs);
- end;
- end;
- end;
- end;
- writeLine(st);
- writeLine(sr);
- { prepare next chunk }
- Inc(ChunkStart, ChunkLen);
- ChunkLen:=Min(MaxChunkSize, sec.size-ChunkStart);
- ChunkFixupStart:=ChunkFixupEnd+1;
- end;
- end;
- function TRelObjOutput.writeData(Data: TObjData): boolean;
- var
- global_symbols_count: Integer = 0;
- secidx, idx, i, j: Integer;
- objsym: TObjSymbol;
- objsec: TObjSection;
- begin
- global_symbols_count:=0;
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TObjSymbol(Data.ObjSymbolList[i]);
- if objsym.bind in [AB_EXTERNAL,AB_GLOBAL] then
- Inc(global_symbols_count);
- end;
- writeLine('XL2');
- writeLine('H '+tohex(data.ObjSectionList.Count)+' areas '+tohex(global_symbols_count)+' global symbols');
- idx:=0;
- for i:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TObjSymbol(Data.ObjSymbolList[i]);
- if objsym.bind=AB_EXTERNAL then
- begin
- writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Ref0000');
- objsym.symidx:=idx;
- Inc(idx);
- end;
- end;
- secidx:=0;
- for i:=0 to Data.ObjSectionList.Count-1 do
- begin
- objsec:=TObjSection(Data.ObjSectionList[i]);
- writeLine('A '+objsec.Name+' size '+tohex(objsec.Size)+' flags 0 addr 0');
- objsec.SecSymIdx:=secidx;
- Inc(secidx);
- for j:=0 to Data.ObjSymbolList.Count-1 do
- begin
- objsym:=TObjSymbol(Data.ObjSymbolList[j]);
- if (objsym.bind=AB_GLOBAL) and (objsym.objsection=objsec) then
- begin
- writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Def'+HexStr(objsym.offset,4));
- objsym.symidx:=idx;
- Inc(idx);
- end;
- end;
- end;
- for i:=0 to Data.ObjSectionList.Count-1 do
- begin
- objsec:=TObjSection(Data.ObjSectionList[i]);
- WriteAreaContentAndRelocations(objsec);
- end;
- result:=true;
- end;
- constructor TRelObjOutput.create(AWriter: TObjectWriter);
- begin
- inherited;
- cobjdata:=TRelObjData;
- end;
- {*****************************************************************************
- TRelAssembler
- *****************************************************************************}
- constructor TRelAssembler.create(info: pasminfo; smart: boolean);
- begin
- inherited;
- CObjOutput:=TRelObjOutput;
- CInternalAr:=tarobjectwriter;
- end;
- {*****************************************************************************
- TRelObjInput
- *****************************************************************************}
- function TRelObjInput.FillBuf: boolean;
- begin
- FBufPos:=0;
- FBufSize:=min(FReader.size-FReader.Pos,MaxBufSize);
- if FBufSize>0 then
- result:=FReader.read(FBuf,FBufSize)
- else
- result:=true;
- end;
- function TRelObjInput.AtEndOfBuf: boolean;
- begin
- result:=FBufPos=FBufSize;
- end;
- function TRelObjInput.AtEoF: boolean;
- begin
- result:=AtEndOfBuf and (FReader.Pos=FReader.size);
- end;
- function TRelObjInput.ReadChar(out c: char): boolean;
- begin
- c:=#0;
- if AtEndOfBuf then
- begin
- result:=FillBuf;
- if not result then
- exit;
- end;
- if not AtEndOfBuf then
- begin
- c:=FBuf[FBufPos];
- Inc(FBufPos);
- result:=true;
- end
- else
- result:=false;
- end;
- function TRelObjInput.PeekChar(out c: char): boolean;
- begin
- c:=#0;
- if AtEndOfBuf then
- begin
- result:=FillBuf;
- if not result then
- exit;
- end;
- if not AtEndOfBuf then
- begin
- c:=FBuf[FBufPos];
- result:=true;
- end
- else
- result:=false;
- end;
- function TRelObjInput.ReadLine(out s: string): boolean;
- var
- c: Char;
- begin
- s:='';
- if AtEoF then
- begin
- result:=false;
- exit;
- end;
- repeat
- if not AtEoF then
- begin
- if not ReadChar(c) then
- begin
- result:=false;
- exit;
- end;
- if not (c in [#13,#10]) then
- s:=s+c;
- end;
- until (c in [#13,#10]) or AtEoF;
- if (c=#13) and not AtEoF then
- begin
- if not PeekChar(c) then
- begin
- result:=false;
- exit;
- end;
- if c=#10 then
- begin
- if not ReadChar(c) then
- begin
- result:=false;
- exit;
- end;
- end;
- end;
- result:=true;
- end;
- constructor TRelObjInput.create;
- begin
- inherited create;
- cobjdata:=TRelObjData;
- FBufSize:=0;
- FBufPos:=0;
- end;
- function TRelObjInput.ReadObjData(AReader: TObjectreader; out Data: TObjData): boolean;
- function HandleTR(const T,R: string): boolean;
- begin
- { todo: implement }
- result:=true;
- end;
- const
- GenericRelErrMsg='Error reading REL file';
- var
- s, AreaName, SymbolName: string;
- RecType: Char;
- HeaderFound: Boolean=false;
- ExpectedAreas,ExpectedSymbols,AreaSize,AreaFlags,AreaAddr,
- SymbolOfs: LongInt;
- tmpint: SizeInt;
- CurrSec: TObjSection=nil;
- objsym: TObjSymbol;
- LastT: string='';
- begin
- FReader:=AReader;
- InputFileName:=AReader.FileName;
- Data:=CObjData.Create(InputFileName);
- result:=false;
- s:='';
- repeat
- if AtEoF or not ReadLine(s) then
- begin
- InputError(GenericRelErrMsg);
- exit;
- end;
- s:=Trim(s);
- until s<>'';
- if s<>'XL2' then
- begin
- InputError('Invalid or unsupported REL format identifier');
- exit;
- end;
- while not AtEoF do
- begin
- if not ReadLine(s) then
- begin
- InputError(GenericRelErrMsg);
- exit;
- end;
- s:=Trim(s);
- if s<>'' then
- begin
- RecType:=s[1];
- if (length(s)<3) or (s[2]<>' ') then
- begin
- InputError('Invalid or unsupported REL record');
- exit;
- end;
- delete(s,1,2);
- case RecType of
- 'H': { header }
- begin
- if HeaderFound then
- begin
- InputError('Duplicated header');
- exit;
- end;
- HeaderFound:=true;
- tmpint:=Pos(' ',s);
- if not TryStrToInt('$'+Copy(s,1,tmpint-1),ExpectedAreas) then
- begin
- InputError('Invalid area count in header');
- exit;
- end;
- delete(s,1,tmpint);
- if copy(s,1,6)<>'areas ' then
- begin
- InputError('Invalid header');
- exit;
- end;
- delete(s,1,6);
- tmpint:=Pos(' ',s);
- if not TryStrToInt('$'+Copy(s,1,tmpint-1),ExpectedSymbols) then
- begin
- InputError('Invalid symbol count in header');
- exit;
- end;
- delete(s,1,tmpint);
- if s<>'global symbols' then
- begin
- InputError('Invalid header');
- exit;
- end;
- end;
- 'M': { module }
- begin
- { we ignore this for now }
- end;
- 'S': { symbol }
- begin
- if not HeaderFound then
- begin
- InputError('Symbol record encountered before header');
- exit;
- end;
- tmpint:=Pos(' ',s);
- if tmpint<=1 then
- begin
- InputError('Invalid symbol record');
- exit;
- end;
- SymbolName:=copy(s,1,tmpint-1);
- delete(s,1,tmpint);
- if Length(s)<4 then
- begin
- InputError('Invalid symbol record');
- exit;
- end;
- if not TryStrToInt('$'+Copy(s,4,Length(s)-4),SymbolOfs) then
- begin
- InputError('Invalid symbol offset');
- exit;
- end;
- case Copy(s,1,3) of
- 'Def':
- begin
- if CurrSec=nil then
- begin
- InputError('Public symbol defined outside any area');
- exit;
- end;
- if (SymbolOfs<0) or (SymbolOfs>=CurrSec.Size) then
- begin
- InputError('Public symbol offset outside the range of the current area');
- exit;
- end;
- objsym:=Data.CreateSymbol(SymbolName);
- objsym.bind:=AB_GLOBAL;
- objsym.typ:=AT_FUNCTION;
- objsym.objsection:=CurrSec;
- objsym.offset:=SymbolOfs;
- objsym.size:=0;
- end;
- 'Ref':
- begin
- if CurrSec<>nil then
- begin
- InputError('External symbols must be defined before the first area');
- exit;
- end;
- if SymbolOfs<>0 then
- begin
- InputError('External symbols must be declared with an offset of 0');
- exit;
- end;
- objsym:=Data.CreateSymbol(SymbolName);
- objsym.bind:=AB_EXTERNAL;
- objsym.typ:=AT_FUNCTION;
- objsym.objsection:=nil;
- objsym.offset:=0;
- objsym.size:=0;
- end;
- else
- begin
- InputError('Invalid or unsupported symbol record');
- exit;
- end;
- end;
- if Data.ObjSymbolList.Count>ExpectedSymbols then
- begin
- InputError('Number of symbols exceeds the number, declared in header');
- exit;
- end;
- end;
- 'A': { area }
- begin
- if not HeaderFound then
- begin
- InputError('Area record encountered before header');
- exit;
- end;
- tmpint:=Pos(' ',s);
- if tmpint<=1 then
- begin
- InputError('Invalid area record');
- exit;
- end;
- AreaName:=copy(s,1,tmpint-1);
- delete(s,1,tmpint);
- if copy(s,1,5)<>'size ' then
- begin
- InputError('Invalid area record');
- exit;
- end;
- delete(s,1,5);
- tmpint:=Pos(' ',s);
- if not TryStrToInt('$'+Copy(s,1,tmpint-1),AreaSize) then
- begin
- InputError('Invalid area size');
- exit;
- end;
- delete(s,1,tmpint);
- if copy(s,1,6)<>'flags ' then
- begin
- InputError('Invalid area record');
- exit;
- end;
- delete(s,1,6);
- tmpint:=Pos(' ',s);
- if not TryStrToInt('$'+Copy(s,1,tmpint-1),AreaFlags) then
- begin
- InputError('Invalid area flags');
- exit;
- end;
- delete(s,1,tmpint);
- if copy(s,1,5)<>'addr ' then
- begin
- InputError('Invalid area record');
- exit;
- end;
- delete(s,1,5);
- if not TryStrToInt('$'+Copy(s,1,tmpint-1),AreaAddr) then
- begin
- InputError('Invalid area address');
- exit;
- end;
- if AreaFlags<>0 then
- begin
- InputError('Unsupported area flags ('+tostr(AreaFlags)+')');
- exit;
- end;
- if AreaAddr<>0 then
- begin
- InputError('Area address<>0 not supported');
- exit;
- end;
- CurrSec:=Data.createsection(AreaName,1,[],false);
- CurrSec.alloc(AreaSize);
- if Data.ObjSectionList.Count>ExpectedAreas then
- begin
- InputError('Number of areas exceeds the number, declared in header');
- exit;
- end;
- end;
- 'T': { T line () }
- begin
- if LastT<>'' then
- begin
- InputError('T record not followed by R record');
- exit;
- end;
- LastT:=s;
- end;
- 'R': { R line (relocation information) }
- begin
- if LastT='' then
- begin
- InputError('R record without T record');
- exit;
- end;
- if not HandleTR(LastT,s) then
- exit;
- LastT:='';
- end;
- 'P': { P line (paging information) }
- begin
- InputError('P line records are not supported');
- exit;
- end;
- else
- begin
- InputError('Unsupported REL record type: #'+tostr(Ord(RecType)));
- exit;
- end;
- end;
- end;
- end;
- result:=true;
- end;
- class function TRelObjInput.CanReadObjData(AReader: TObjectreader): boolean;
- var
- s: string;
- instance: TRelObjInput;
- begin
- result:=false;
- instance:=TRelObjInput.Create;
- instance.FReader:=AReader;
- with instance do
- while not AtEoF do
- begin
- if not ReadLine(s) then
- exit;
- s:=Trim(s);
- if s<>'' then
- begin
- result:=s='XL2';
- break;
- end;
- end;
- instance.Free;
- end;
- {*****************************************************************************
- TIntelHexExeOutput
- *****************************************************************************}
- function TIntelHexExeOutput.writeData: boolean;
- begin
- result:=false;
- end;
- procedure TIntelHexExeOutput.DoRelocationFixup(objsec: TObjSection);
- begin
- end;
- constructor TIntelHexExeOutput.create;
- begin
- inherited create;
- CObjData:=TRelObjData;
- MaxMemPos:=$FFFF;
- end;
- {*****************************************************************************
- Initialize
- *****************************************************************************}
- const
- as_z80_rel_info : tasminfo =
- (
- id : as_z80_rel;
- idtxt : 'REL';
- asmbin : '';
- asmcmd : '';
- supported_targets : [system_z80_embedded,system_z80_zxspectrum];
- flags : [af_outputbinary,af_smartlink_sections];
- labelprefix : '..@';
- labelmaxlen : 79;
- comment : '; ';
- dollarsign: '$';
- );
- initialization
- RegisterAssembler(as_z80_rel_info,TRelAssembler);
- end.
|