123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- {
- Copyright (c) 2015 by Nikolay Nikolov
- Contains the stuff for writing Relocatable Object Module Format (OMF)
- libraries directly. This is the object format used on the i8086-msdos
- platform (also known as .lib files in the dos world, even though Free
- Pascal uses the extension .a).
- 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 owomflib;
- {$i fpcdefs.inc}
- interface
- uses
- cclasses,
- globtype,
- owbase;
- type
- { TOmfLibDictionaryEntry }
- TOmfLibDictionaryEntry=class(TFPHashObject)
- private
- FPageNum: Word;
- public
- constructor Create(HashObjectList:TFPHashObjectList;const aName:TSymStr;aPageNum:Word);
- property PageNum: Word read FPageNum write FPageNum;
- end;
- { TOmfLibObjectWriter }
- TOmfLibObjectWriter=class(TObjectWriter)
- private
- FPageSize: Integer;
- FLibName: string;
- FLibData: TDynamicArray;
- FObjFileName: string;
- FObjData: TDynamicArray;
- FObjStartPage: Word;
- FDictionary: TFPHashObjectList;
- procedure WriteHeader(DictStart: DWord; DictBlocks: Word);
- procedure WriteFooter;
- procedure WriteLib;
- function WriteDictionary: byte;
- function TryWriteDictionaryWithSize(nblocks: Byte): Boolean;
- public
- constructor createAr(const Aarfn:string);override;
- constructor createAr(const Aarfn:string;PageSize:Integer);
- destructor destroy;override;
- function createfile(const fn:string):boolean;override;
- procedure closefile;override;
- procedure writesym(const sym:string);override;
- procedure write(const b;len:longword);override;
- end;
- { TOmfLibObjectReader }
- TOmfLibObjectReader=class(TObjectReader)
- private
- LibSymbols : TFPHashObjectList;
- islib: boolean;
- CurrMemberPos : longint;
- CurrMemberName : string;
- FPageSize: Integer;
- procedure ReadLibrary;
- procedure ReadDictionary(DictionaryOffset: DWord; DictionarySizeInBlocks: Word);
- protected
- function getfilename:string;override;
- function GetPos: longint;override;
- function GetIsArchive: boolean;override;
- public
- constructor createAr(const Aarfn:string;allow_nonar:boolean=false);override;
- destructor destroy;override;
- function openfile(const fn:string):boolean;override;
- procedure closefile;override;
- procedure seek(len:longint);override;
- end;
- implementation
- uses
- SysUtils,
- cstreams,
- globals,
- verbose,
- omfbase;
- const
- libbufsize = 65536;
- objbufsize = 65536;
- {*****************************************************************************
- Helpers
- *****************************************************************************}
- function ModName2DictEntry(const modnm: string): string;
- begin
- if Copy(modnm,Length(modnm)-1,2)='.o' then
- Result:=Copy(modnm,1,Length(modnm)-2)+'!'
- else
- Result:=modnm;
- end;
- {*****************************************************************************
- TOmfLibDictionaryEntry
- *****************************************************************************}
- constructor TOmfLibDictionaryEntry.Create(HashObjectList: TFPHashObjectList; const aName: TSymStr; aPageNum: Word);
- begin
- inherited Create(HashObjectList,aName);
- PageNum:=aPageNum;
- end;
- {*****************************************************************************
- TOmfLibObjectWriter
- *****************************************************************************}
- constructor TOmfLibObjectWriter.createAr(const Aarfn: string);
- begin
- createAr(Aarfn,512);
- end;
- constructor TOmfLibObjectWriter.createAr(const Aarfn: string;PageSize: Integer);
- begin
- FPageSize:=PageSize;
- FLibName:=Aarfn;
- FLibData:=TDynamicArray.Create(libbufsize);
- FDictionary:=TFPHashObjectList.Create;
- { header is at page 0, so first module starts at page 1 }
- FObjStartPage:=1;
- end;
- destructor TOmfLibObjectWriter.destroy;
- begin
- if Errorcount=0 then
- WriteLib;
- FLibData.Free;
- FObjData.Free;
- FDictionary.Free;
- inherited destroy;
- end;
- function TOmfLibObjectWriter.createfile(const fn: string): boolean;
- begin
- FObjFileName:=fn;
- FreeAndNil(FObjData);
- FObjData:=TDynamicArray.Create(objbufsize);
- createfile:=true;
- fobjsize:=0;
- end;
- procedure TOmfLibObjectWriter.closefile;
- var
- RawRec: TOmfRawRecord;
- ObjHeader: TOmfRecord_THEADR;
- begin
- FLibData.seek(FObjStartPage*FPageSize);
- FObjData.seek(0);
- RawRec:=TOmfRawRecord.Create;
- repeat
- RawRec.ReadFrom(FObjData);
- if RawRec.RecordType=RT_THEADR then
- begin
- ObjHeader:=TOmfRecord_THEADR.Create;
- ObjHeader.DecodeFrom(RawRec);
- { create a dictionary entry with the module name }
- TOmfLibDictionaryEntry.Create(FDictionary,ModName2DictEntry(ObjHeader.ModuleName),FObjStartPage);
- ObjHeader.Free;
- end;
- RawRec.WriteTo(FLibData);
- until RawRec.RecordType in [RT_MODEND,RT_MODEND32];
- RawRec.Free;
- { calculate start page of next module }
- FObjStartPage:=(FLibData.Pos+FPageSize-1) div FPageSize;
- fobjsize:=0;
- end;
- procedure TOmfLibObjectWriter.writesym(const sym: string);
- begin
- TOmfLibDictionaryEntry.Create(FDictionary,sym,FObjStartPage);
- end;
- procedure TOmfLibObjectWriter.write(const b; len: longword);
- begin
- inc(fobjsize,len);
- inc(fsize,len);
- FObjData.write(b,len);
- end;
- procedure TOmfLibObjectWriter.WriteHeader(DictStart: DWord; DictBlocks: Word);
- var
- Header: TOmfRecord_LIBHEAD;
- RawRec: TOmfRawRecord;
- begin
- { set header properties }
- Header:=TOmfRecord_LIBHEAD.Create;
- Header.PageSize:=FPageSize;
- Header.DictionaryOffset:=DictStart;
- Header.DictionarySizeInBlocks:=DictBlocks;
- Header.CaseSensitive:=true;
- { write header }
- RawRec:=TOmfRawRecord.Create;
- Header.EncodeTo(RawRec);
- FLibData.seek(0);
- RawRec.WriteTo(FLibData);
- Header.Free;
- RawRec.Free;
- end;
- procedure TOmfLibObjectWriter.WriteFooter;
- var
- Footer: TOmfRecord_LIBEND;
- RawRec: TOmfRawRecord;
- begin
- FLibData.seek(FObjStartPage*FPageSize);
- Footer:=TOmfRecord_LIBEND.Create;
- Footer.CalculatePaddingBytes(FLibData.Pos);
- RawRec:=TOmfRawRecord.Create;
- Footer.EncodeTo(RawRec);
- RawRec.WriteTo(FLibData);
- Footer.Free;
- RawRec.Free;
- end;
- procedure TOmfLibObjectWriter.WriteLib;
- var
- libf: TCCustomFileStream;
- DictStart: LongWord;
- DictBlocks: Byte;
- begin
- libf:=CFileStreamClass.Create(FLibName,fmCreate);
- if CStreamError<>0 then
- begin
- Message1(exec_e_cant_create_archivefile,FLibName);
- exit;
- end;
- WriteFooter;
- DictStart:=FLibData.Pos;
- DictBlocks:=WriteDictionary;
- WriteHeader(DictStart,DictBlocks);
- FLibData.WriteStream(libf);
- libf.Free;
- end;
- function TOmfLibObjectWriter.WriteDictionary: byte;
- var
- nb: Byte;
- begin
- for nb in OmfLibDictionaryBlockCounts do
- if TryWriteDictionaryWithSize(nb) then
- exit(nb);
- { could not write dictionary, even with the largest number of blocks }
- internalerror(2015042201);
- end;
- function TOmfLibObjectWriter.TryWriteDictionaryWithSize(nblocks: Byte): Boolean;
- const
- nbuckets=37;
- freespace=nbuckets;
- type
- PBlock=^TBlock;
- TBlock=array[0..511] of byte;
- var
- blocks: array of TBlock;
- i: Integer;
- N: TSymStr;
- length_of_string: Integer;
- h: TOmfLibHash;
- start_block,start_bucket: Integer;
- space_required: Integer;
- pb: PBlock;
- success: Boolean;
- store_at: Integer;
- PageNum: Word;
- begin
- SetLength(blocks,nblocks);
- for i:=0 to nblocks-1 do
- begin
- FillChar(blocks[i],SizeOf(blocks[i]),0);
- blocks[i][freespace]:=(freespace+1) div 2;
- end;
- for i:=0 to FDictionary.Count-1 do
- begin
- N:=TOmfLibDictionaryEntry(FDictionary[i]).Name;
- PageNum:=TOmfLibDictionaryEntry(FDictionary[i]).PageNum;
- length_of_string:=Length(N);
- h:=compute_omf_lib_hash(N,nblocks);
- start_block:=h.block_x;
- start_bucket:=h.bucket_x;
- space_required:=1+length_of_string+2;
- if odd(space_required) then
- Inc(space_required);
- repeat
- pb:=@blocks[h.block_x];
- success:=false;
- repeat
- if pb^[h.bucket_x]=0 then
- begin
- if (512-pb^[freespace]*2)<space_required then
- break;
- pb^[h.bucket_x]:=pb^[freespace];
- store_at:=2*pb^[h.bucket_x];
- pb^[store_at]:=length_of_string;
- Move(N[1],pb^[store_at+1],length_of_string);
- pb^[store_at+1+length_of_string]:=Byte(PageNum);
- pb^[store_at+1+length_of_string+1]:=Byte(PageNum shr 8);
- Inc(pb^[freespace],space_required div 2);
- if pb^[freespace]=0 then
- pb^[freespace]:=255;
- success:=true;
- break;
- end;
- h.bucket_x:=(h.bucket_x+h.bucket_d) mod nbuckets;
- until h.bucket_x=start_bucket;
- if not success then
- begin
- h.block_x:=(h.block_x+h.block_d) mod nblocks;
- if h.block_x=start_block then
- exit(false); // not enough blocks
- pb^[freespace]:=255;
- end;
- until success;
- end;
- FLibData.write(blocks[0],nblocks*SizeOf(TBlock));
- Result:=true;
- end;
- {*****************************************************************************
- TOmfLibObjectReader
- *****************************************************************************}
- procedure TOmfLibObjectReader.ReadLibrary;
- var
- RawRecord: TOmfRawRecord;
- Header: TOmfRecord_LIBHEAD;
- FIsCaseSensitive: Boolean;
- begin
- RawRecord:=TOmfRawRecord.Create;
- RawRecord.ReadFrom(Self);
- Header:=TOmfRecord_LIBHEAD.Create;
- Header.DecodeFrom(RawRecord);
- FPageSize:=Header.PageSize;
- FIsCaseSensitive:=Header.CaseSensitive;
- ReadDictionary(Header.DictionaryOffset, Header.DictionarySizeInBlocks);
- end;
- procedure TOmfLibObjectReader.ReadDictionary(DictionaryOffset: DWord; DictionarySizeInBlocks: Word);
- const
- nbuckets=37;
- freespace=nbuckets;
- type
- PBlock=^TBlock;
- TBlock=array[0..511] of byte;
- var
- blocks: array of TBlock;
- blocknr: Integer;
- block: PBlock;
- ofs: Integer;
- bucket: Integer;
- length_of_string: Byte;
- name: string;
- PageNum: Integer;
- begin
- seek(DictionaryOffset);
- SetLength(blocks,DictionarySizeInBlocks);
- read(blocks[0],DictionarySizeInBlocks*SizeOf(TBlock));
- for blocknr:=0 to DictionarySizeInBlocks-1 do
- begin
- block:=@(blocks[blocknr]);
- for bucket:=0 to nbuckets-1 do
- if block^[bucket]<>0 then
- begin
- ofs:=2*block^[bucket];
- length_of_string:=block^[ofs];
- if (ofs+1+length_of_string+1)>High(TBlock) then
- begin
- Comment(V_Error,'OMF dictionary entry goes beyond end of block');
- continue;
- end;
- SetLength(name,length_of_string);
- Move(block^[ofs+1],name[1],length_of_string);
- PageNum:=block^[ofs+1+length_of_string]+
- block^[ofs+1+length_of_string+1] shl 8;
- TOmfLibDictionaryEntry.create(LibSymbols,name,PageNum);
- end;
- end;
- end;
- function TOmfLibObjectReader.getfilename: string;
- begin
- Result:=inherited getfilename;
- if CurrMemberName<>'' then
- result:=result+'('+CurrMemberName+')';
- end;
- function TOmfLibObjectReader.GetPos: longint;
- begin
- result:=inherited GetPos-CurrMemberPos;
- end;
- function TOmfLibObjectReader.GetIsArchive: boolean;
- begin
- result:=islib;
- end;
- constructor TOmfLibObjectReader.createAr(const Aarfn: string; allow_nonar: boolean);
- var
- RecType: Byte;
- begin
- inherited Create;
- LibSymbols:=TFPHashObjectList.Create(true);
- CurrMemberPos:=0;
- CurrMemberName:='';
- if inherited openfile(Aarfn) then
- begin
- Read(RecType,1);
- Seek(0);
- islib:=RecType=RT_LIBHEAD;
- if islib then
- ReadLibrary
- else if (not allow_nonar) then
- Comment(V_Error,'Not an OMF library file, illegal magic: '+filename);
- end;
- end;
- destructor TOmfLibObjectReader.destroy;
- begin
- inherited closefile;
- LibSymbols.Free;
- inherited Destroy;
- end;
- function TOmfLibObjectReader.openfile(const fn: string): boolean;
- var
- libsym: TOmfLibDictionaryEntry;
- RawRec: TOmfRawRecord;
- Header: TOmfRecord_THEADR;
- begin
- result:=false;
- libsym:=TOmfLibDictionaryEntry(LibSymbols.Find(ModName2DictEntry(fn)));
- if not assigned(libsym) then
- exit;
- CurrMemberPos:=libsym.PageNum*FPageSize;
- inherited Seek(CurrMemberPos);
- { read the header, to obtain the module name }
- RawRec:=TOmfRawRecord.Create;
- RawRec.ReadFrom(self);
- Header:=TOmfRecord_THEADR.Create;
- Header.DecodeFrom(RawRec);
- CurrMemberName:=Header.ModuleName;
- Header.Free;
- RawRec.Free;
- { go back to the beginning of the file }
- inherited Seek(CurrMemberPos);
- result:=true;
- end;
- procedure TOmfLibObjectReader.closefile;
- begin
- CurrMemberPos:=0;
- CurrMemberName:='';
- end;
- procedure TOmfLibObjectReader.seek(len: longint);
- begin
- inherited Seek(CurrMemberPos+len);
- end;
- end.
|