owomflib.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. {
  2. Copyright (c) 2015 by Nikolay Nikolov
  3. Contains the stuff for writing Relocatable Object Module Format (OMF)
  4. libraries directly. This is the object format used on the i8086-msdos
  5. platform (also known as .lib files in the dos world, even though Free
  6. Pascal uses the extension .a).
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit owomflib;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cclasses,
  25. globtype,
  26. owbase;
  27. type
  28. { TOmfLibDictionaryEntry }
  29. TOmfLibDictionaryEntry=class(TFPHashObject)
  30. private
  31. FPageNum: Word;
  32. public
  33. constructor Create(HashObjectList:TFPHashObjectList;const aName:TSymStr;aPageNum:Word);
  34. property PageNum: Word read FPageNum write FPageNum;
  35. end;
  36. { TOmfLibObjectWriter }
  37. TOmfLibObjectWriter=class(TObjectWriter)
  38. private
  39. FPageSize: Integer;
  40. FLibName: string;
  41. FLibData: TDynamicArray;
  42. FObjFileName: string;
  43. FObjData: TDynamicArray;
  44. FObjStartPage: Word;
  45. FDictionary: TFPHashObjectList;
  46. procedure WriteHeader(DictStart: DWord; DictBlocks: Word);
  47. procedure WriteFooter;
  48. procedure WriteLib;
  49. function WriteDictionary: byte;
  50. function TryWriteDictionaryWithSize(nblocks: Byte): Boolean;
  51. public
  52. constructor createAr(const Aarfn:string);override;
  53. destructor destroy;override;
  54. function createfile(const fn:string):boolean;override;
  55. procedure closefile;override;
  56. procedure writesym(const sym:string);override;
  57. procedure write(const b;len:longword);override;
  58. end;
  59. { TOmfLibObjectReader }
  60. TOmfLibObjectReader=class(TObjectReader)
  61. private
  62. LibSymbols : TFPHashObjectList;
  63. islib: boolean;
  64. CurrMemberPos : longint;
  65. FPageSize: Integer;
  66. procedure ReadLibrary;
  67. procedure ReadDictionary(DictionaryOffset: DWord; DictionarySizeInBlocks: Word);
  68. public
  69. constructor create(const Aarfn:string;allow_nonar:boolean=false);
  70. destructor destroy;override;
  71. function openfile(const fn:string):boolean;override;
  72. procedure closefile;override;
  73. procedure seek(len:longint);override;
  74. property isarchive: boolean read islib;
  75. end;
  76. implementation
  77. uses
  78. SysUtils,
  79. cstreams,
  80. globals,
  81. verbose,
  82. omfbase;
  83. const
  84. libbufsize = 65536;
  85. objbufsize = 65536;
  86. {*****************************************************************************
  87. Helpers
  88. *****************************************************************************}
  89. function ModName2DictEntry(const modnm: string): string;
  90. begin
  91. if Copy(modnm,Length(modnm)-1,2)='.o' then
  92. Result:=Copy(modnm,1,Length(modnm)-2)+'!'
  93. else
  94. Result:=modnm;
  95. end;
  96. {*****************************************************************************
  97. TOmfLibDictionaryEntry
  98. *****************************************************************************}
  99. constructor TOmfLibDictionaryEntry.Create(HashObjectList: TFPHashObjectList; const aName: TSymStr; aPageNum: Word);
  100. begin
  101. inherited Create(HashObjectList,aName);
  102. PageNum:=aPageNum;
  103. end;
  104. {*****************************************************************************
  105. TOmfLibObjectWriter
  106. *****************************************************************************}
  107. constructor TOmfLibObjectWriter.createAr(const Aarfn: string);
  108. begin
  109. FPageSize:=512;
  110. FLibName:=Aarfn;
  111. FLibData:=TDynamicArray.Create(libbufsize);
  112. FDictionary:=TFPHashObjectList.Create;
  113. { header is at page 0, so first module starts at page 1 }
  114. FObjStartPage:=1;
  115. end;
  116. destructor TOmfLibObjectWriter.destroy;
  117. begin
  118. if Errorcount=0 then
  119. WriteLib;
  120. FLibData.Free;
  121. FObjData.Free;
  122. FDictionary.Free;
  123. inherited destroy;
  124. end;
  125. function TOmfLibObjectWriter.createfile(const fn: string): boolean;
  126. begin
  127. FObjFileName:=fn;
  128. FreeAndNil(FObjData);
  129. FObjData:=TDynamicArray.Create(objbufsize);
  130. createfile:=true;
  131. fobjsize:=0;
  132. end;
  133. procedure TOmfLibObjectWriter.closefile;
  134. var
  135. RawRec: TOmfRawRecord;
  136. ObjHeader: TOmfRecord_THEADR;
  137. begin
  138. FLibData.seek(FObjStartPage*FPageSize);
  139. FObjData.seek(0);
  140. RawRec:=TOmfRawRecord.Create;
  141. repeat
  142. RawRec.ReadFrom(FObjData);
  143. if RawRec.RecordType=RT_THEADR then
  144. begin
  145. ObjHeader:=TOmfRecord_THEADR.Create;
  146. ObjHeader.DecodeFrom(RawRec);
  147. { create a dictionary entry with the module name }
  148. TOmfLibDictionaryEntry.Create(FDictionary,ModName2DictEntry(ObjHeader.ModuleName),FObjStartPage);
  149. ObjHeader.Free;
  150. end;
  151. RawRec.WriteTo(FLibData);
  152. until RawRec.RecordType in [RT_MODEND,RT_MODEND32];
  153. RawRec.Free;
  154. { calculate start page of next module }
  155. FObjStartPage:=(FLibData.Pos+FPageSize-1) div FPageSize;
  156. fobjsize:=0;
  157. end;
  158. procedure TOmfLibObjectWriter.writesym(const sym: string);
  159. begin
  160. TOmfLibDictionaryEntry.Create(FDictionary,sym,FObjStartPage);
  161. end;
  162. procedure TOmfLibObjectWriter.write(const b; len: longword);
  163. begin
  164. inc(fobjsize,len);
  165. inc(fsize,len);
  166. FObjData.write(b,len);
  167. end;
  168. procedure TOmfLibObjectWriter.WriteHeader(DictStart: DWord; DictBlocks: Word);
  169. var
  170. Header: TOmfRecord_LIBHEAD;
  171. RawRec: TOmfRawRecord;
  172. begin
  173. { set header properties }
  174. Header:=TOmfRecord_LIBHEAD.Create;
  175. Header.PageSize:=FPageSize;
  176. Header.DictionaryOffset:=DictStart;
  177. Header.DictionarySizeInBlocks:=DictBlocks;
  178. Header.CaseSensitive:=true;
  179. { write header }
  180. RawRec:=TOmfRawRecord.Create;
  181. Header.EncodeTo(RawRec);
  182. FLibData.seek(0);
  183. RawRec.WriteTo(FLibData);
  184. Header.Free;
  185. RawRec.Free;
  186. end;
  187. procedure TOmfLibObjectWriter.WriteFooter;
  188. var
  189. Footer: TOmfRecord_LIBEND;
  190. RawRec: TOmfRawRecord;
  191. begin
  192. FLibData.seek(FObjStartPage*FPageSize);
  193. Footer:=TOmfRecord_LIBEND.Create;
  194. Footer.CalculatePaddingBytes(FLibData.Pos);
  195. RawRec:=TOmfRawRecord.Create;
  196. Footer.EncodeTo(RawRec);
  197. RawRec.WriteTo(FLibData);
  198. Footer.Free;
  199. RawRec.Free;
  200. end;
  201. procedure TOmfLibObjectWriter.WriteLib;
  202. var
  203. libf: TCCustomFileStream;
  204. DictStart: LongWord;
  205. DictBlocks: Byte;
  206. begin
  207. libf:=CFileStreamClass.Create(FLibName,fmCreate);
  208. if CStreamError<>0 then
  209. begin
  210. Message1(exec_e_cant_create_archivefile,FLibName);
  211. exit;
  212. end;
  213. WriteFooter;
  214. DictStart:=FLibData.Pos;
  215. DictBlocks:=WriteDictionary;
  216. WriteHeader(DictStart,DictBlocks);
  217. FLibData.WriteStream(libf);
  218. libf.Free;
  219. end;
  220. function TOmfLibObjectWriter.WriteDictionary: Byte;
  221. var
  222. nb: Byte;
  223. begin
  224. for nb in OmfLibDictionaryBlockCounts do
  225. if TryWriteDictionaryWithSize(nb) then
  226. exit(nb);
  227. { could not write dictionary, even with the largest number of blocks }
  228. internalerror(2015042201);
  229. end;
  230. function TOmfLibObjectWriter.TryWriteDictionaryWithSize(nblocks: Byte): Boolean;
  231. const
  232. nbuckets=37;
  233. freespace=nbuckets;
  234. type
  235. PBlock=^TBlock;
  236. TBlock=array[0..511] of byte;
  237. var
  238. blocks: array of TBlock;
  239. i: Integer;
  240. N: TSymStr;
  241. length_of_string: Integer;
  242. h: TOmfLibHash;
  243. start_block,start_bucket: Integer;
  244. space_required: Integer;
  245. pb: PBlock;
  246. success: Boolean;
  247. store_at: Integer;
  248. PageNum: Word;
  249. begin
  250. SetLength(blocks,nblocks);
  251. for i:=0 to nblocks-1 do
  252. begin
  253. FillChar(blocks[i],SizeOf(blocks[i]),0);
  254. blocks[i][freespace]:=(freespace+1) div 2;
  255. end;
  256. for i:=0 to FDictionary.Count-1 do
  257. begin
  258. N:=TOmfLibDictionaryEntry(FDictionary[i]).Name;
  259. PageNum:=TOmfLibDictionaryEntry(FDictionary[i]).PageNum;
  260. length_of_string:=Length(N);
  261. h:=compute_omf_lib_hash(N,nblocks);
  262. start_block:=h.block_x;
  263. start_bucket:=h.bucket_x;
  264. space_required:=1+length_of_string+2;
  265. if odd(space_required) then
  266. Inc(space_required);
  267. repeat
  268. pb:=@blocks[h.block_x];
  269. success:=false;
  270. repeat
  271. if pb^[h.bucket_x]=0 then
  272. begin
  273. if (512-pb^[freespace]*2)<space_required then
  274. break;
  275. pb^[h.bucket_x]:=pb^[freespace];
  276. store_at:=2*pb^[h.bucket_x];
  277. pb^[store_at]:=length_of_string;
  278. Move(N[1],pb^[store_at+1],length_of_string);
  279. pb^[store_at+1+length_of_string]:=Byte(PageNum);
  280. pb^[store_at+1+length_of_string+1]:=Byte(PageNum shr 8);
  281. Inc(pb^[freespace],space_required div 2);
  282. if pb^[freespace]=0 then
  283. pb^[freespace]:=255;
  284. success:=true;
  285. break;
  286. end;
  287. h.bucket_x:=(h.bucket_x+h.bucket_d) mod nbuckets;
  288. until h.bucket_x=start_bucket;
  289. if not success then
  290. begin
  291. h.block_x:=(h.block_x+h.block_d) mod nblocks;
  292. if h.block_x=start_block then
  293. exit(false); // not enough blocks
  294. pb^[freespace]:=255;
  295. end;
  296. until success;
  297. end;
  298. FLibData.write(blocks[0],nblocks*SizeOf(TBlock));
  299. Result:=true;
  300. end;
  301. {*****************************************************************************
  302. TOmfLibObjectReader
  303. *****************************************************************************}
  304. procedure TOmfLibObjectReader.ReadLibrary;
  305. var
  306. RawRecord: TOmfRawRecord;
  307. Header: TOmfRecord_LIBHEAD;
  308. FIsCaseSensitive: Boolean;
  309. begin
  310. RawRecord:=TOmfRawRecord.Create;
  311. RawRecord.ReadFrom(Self);
  312. Header:=TOmfRecord_LIBHEAD.Create;
  313. Header.DecodeFrom(RawRecord);
  314. FPageSize:=Header.PageSize;
  315. FIsCaseSensitive:=Header.CaseSensitive;
  316. ReadDictionary(Header.DictionaryOffset, Header.DictionarySizeInBlocks);
  317. end;
  318. procedure TOmfLibObjectReader.ReadDictionary(DictionaryOffset: DWord; DictionarySizeInBlocks: Word);
  319. const
  320. nbuckets=37;
  321. freespace=nbuckets;
  322. type
  323. PBlock=^TBlock;
  324. TBlock=array[0..511] of byte;
  325. var
  326. blocks: array of TBlock;
  327. blocknr: Integer;
  328. block: PBlock;
  329. ofs: Byte;
  330. bucket: Integer;
  331. length_of_string: Byte;
  332. name: string;
  333. PageNum: Integer;
  334. begin
  335. seek(DictionaryOffset);
  336. SetLength(blocks,DictionarySizeInBlocks);
  337. read(blocks[0],DictionarySizeInBlocks*SizeOf(TBlock));
  338. for blocknr:=0 to DictionarySizeInBlocks-1 do
  339. begin
  340. block:=@blocks[blocknr];
  341. for bucket:=0 to nbuckets-1 do
  342. if block^[bucket]<>0 then
  343. begin
  344. ofs:=block^[bucket];
  345. length_of_string:=block^[ofs];
  346. if (ofs+1+length_of_string+1)>High(TBlock) then
  347. Comment(V_Error,'OMF dictionary entry goes beyond end of block');
  348. SetLength(name,length_of_string);
  349. Move(block^[ofs+1],name[1],length_of_string);
  350. PageNum:=block^[ofs+1+length_of_string]+
  351. block^[ofs+1+length_of_string+1] shl 8;
  352. TOmfLibDictionaryEntry.create(LibSymbols,name,PageNum);
  353. end;
  354. end;
  355. end;
  356. constructor TOmfLibObjectReader.create(const Aarfn: string; allow_nonar: boolean);
  357. var
  358. RecType: Byte;
  359. begin
  360. inherited Create;
  361. LibSymbols:=TFPHashObjectList.Create(true);
  362. CurrMemberPos:=0;
  363. if inherited openfile(Aarfn) then
  364. begin
  365. Read(RecType,1);
  366. Seek(0);
  367. islib:=RecType=RT_LIBHEAD;
  368. if islib then
  369. ReadLibrary
  370. else if (not allow_nonar) then
  371. Comment(V_Error,'Not an OMF library file, illegal magic: '+filename);
  372. end;
  373. end;
  374. destructor TOmfLibObjectReader.destroy;
  375. begin
  376. inherited closefile;
  377. LibSymbols.Free;
  378. inherited Destroy;
  379. end;
  380. function TOmfLibObjectReader.openfile(const fn: string): boolean;
  381. var
  382. libsym: TOmfLibDictionaryEntry;
  383. begin
  384. result:=false;
  385. libsym:=TOmfLibDictionaryEntry(LibSymbols.Find(ModName2DictEntry(fn)));
  386. if not assigned(libsym) then
  387. exit;
  388. CurrMemberPos:=libsym.PageNum*FPageSize;
  389. inherited Seek(CurrMemberPos);
  390. result:=true;
  391. end;
  392. procedure TOmfLibObjectReader.closefile;
  393. begin
  394. CurrMemberPos:=0;
  395. end;
  396. procedure TOmfLibObjectReader.seek(len: longint);
  397. begin
  398. inherited Seek(CurrMemberPos+len);
  399. end;
  400. end.