owomflib.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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: Word;
  50. function TryWriteDictionaryWithSize(nblocks: Word): Boolean;
  51. public
  52. constructor createAr(const Aarfn:string);override;
  53. constructor createAr(const Aarfn:string;PageSize:Integer);
  54. destructor destroy;override;
  55. function createfile(const fn:string):boolean;override;
  56. procedure closefile;override;
  57. procedure writesym(const sym:string);override;
  58. procedure write(const b;len:longword);override;
  59. end;
  60. { TOmfLibObjectReader }
  61. TOmfLibObjectReader=class(TObjectReader)
  62. private
  63. LibSymbols : TFPHashObjectList;
  64. islib: boolean;
  65. CurrMemberPos : longint;
  66. CurrMemberName : string;
  67. FPageSize: Integer;
  68. FIsCaseSensitive: Boolean;
  69. procedure ReadLibrary;
  70. procedure ReadDictionary(DictionaryOffset: DWord; DictionarySizeInBlocks: Word);
  71. protected
  72. function getfilename:string;override;
  73. function GetPos: longint;override;
  74. function GetIsArchive: boolean;override;
  75. public
  76. constructor createAr(const Aarfn:string;allow_nonar:boolean=false);override;
  77. destructor destroy;override;
  78. function openfile(const fn:string):boolean;override;
  79. procedure closefile;override;
  80. procedure seek(len:longint);override;
  81. property IsCaseSensitive: Boolean read FIsCaseSensitive;
  82. end;
  83. implementation
  84. uses
  85. SysUtils,
  86. cstreams,
  87. verbose,
  88. omfbase;
  89. const
  90. libbufsize = 65536;
  91. objbufsize = 65536;
  92. {*****************************************************************************
  93. Helpers
  94. *****************************************************************************}
  95. function ModName2DictEntry(const modnm: string): string;
  96. begin
  97. if Copy(modnm,Length(modnm)-1,2)='.o' then
  98. Result:=Copy(modnm,1,Length(modnm)-2)+'!'
  99. else
  100. Result:=modnm;
  101. end;
  102. {*****************************************************************************
  103. TOmfLibDictionaryEntry
  104. *****************************************************************************}
  105. constructor TOmfLibDictionaryEntry.Create(HashObjectList: TFPHashObjectList; const aName: TSymStr; aPageNum: Word);
  106. begin
  107. inherited Create(HashObjectList,aName);
  108. PageNum:=aPageNum;
  109. end;
  110. {*****************************************************************************
  111. TOmfLibObjectWriter
  112. *****************************************************************************}
  113. constructor TOmfLibObjectWriter.createAr(const Aarfn: string);
  114. begin
  115. createAr(Aarfn,512);
  116. end;
  117. constructor TOmfLibObjectWriter.createAr(const Aarfn: string;PageSize: Integer);
  118. begin
  119. FPageSize:=PageSize;
  120. FLibName:=Aarfn;
  121. FLibData:=TDynamicArray.Create(libbufsize);
  122. FDictionary:=TFPHashObjectList.Create;
  123. { header is at page 0, so first module starts at page 1 }
  124. FObjStartPage:=1;
  125. end;
  126. destructor TOmfLibObjectWriter.destroy;
  127. begin
  128. if Errorcount=0 then
  129. WriteLib;
  130. FLibData.Free;
  131. FObjData.Free;
  132. FDictionary.Free;
  133. inherited destroy;
  134. end;
  135. function TOmfLibObjectWriter.createfile(const fn: string): boolean;
  136. begin
  137. FObjFileName:=fn;
  138. FreeAndNil(FObjData);
  139. FObjData:=TDynamicArray.Create(objbufsize);
  140. createfile:=true;
  141. fobjsize:=0;
  142. end;
  143. procedure TOmfLibObjectWriter.closefile;
  144. var
  145. RawRec: TOmfRawRecord;
  146. ObjHeader: TOmfRecord_THEADR;
  147. begin
  148. FLibData.seek(FObjStartPage*FPageSize);
  149. FObjData.seek(0);
  150. RawRec:=TOmfRawRecord.Create;
  151. repeat
  152. RawRec.ReadFrom(FObjData);
  153. if RawRec.RecordType=RT_THEADR then
  154. begin
  155. ObjHeader:=TOmfRecord_THEADR.Create;
  156. ObjHeader.DecodeFrom(RawRec);
  157. { create a dictionary entry with the module name }
  158. TOmfLibDictionaryEntry.Create(FDictionary,ModName2DictEntry(ObjHeader.ModuleName),FObjStartPage);
  159. ObjHeader.Free;
  160. end;
  161. RawRec.WriteTo(FLibData);
  162. until RawRec.RecordType in [RT_MODEND,RT_MODEND32];
  163. RawRec.Free;
  164. { calculate start page of next module }
  165. FObjStartPage:=(FLibData.Pos+FPageSize-1) div FPageSize;
  166. fobjsize:=0;
  167. end;
  168. procedure TOmfLibObjectWriter.writesym(const sym: string);
  169. begin
  170. TOmfLibDictionaryEntry.Create(FDictionary,sym,FObjStartPage);
  171. end;
  172. procedure TOmfLibObjectWriter.write(const b; len: longword);
  173. begin
  174. inc(fobjsize,len);
  175. inc(fsize,len);
  176. FObjData.write(b,len);
  177. end;
  178. procedure TOmfLibObjectWriter.WriteHeader(DictStart: DWord; DictBlocks: Word);
  179. var
  180. Header: TOmfRecord_LIBHEAD;
  181. RawRec: TOmfRawRecord;
  182. begin
  183. { set header properties }
  184. Header:=TOmfRecord_LIBHEAD.Create;
  185. Header.PageSize:=FPageSize;
  186. Header.DictionaryOffset:=DictStart;
  187. Header.DictionarySizeInBlocks:=DictBlocks;
  188. Header.CaseSensitive:=true;
  189. { write header }
  190. RawRec:=TOmfRawRecord.Create;
  191. Header.EncodeTo(RawRec);
  192. FLibData.seek(0);
  193. RawRec.WriteTo(FLibData);
  194. Header.Free;
  195. RawRec.Free;
  196. end;
  197. procedure TOmfLibObjectWriter.WriteFooter;
  198. var
  199. Footer: TOmfRecord_LIBEND;
  200. RawRec: TOmfRawRecord;
  201. begin
  202. FLibData.seek(FObjStartPage*FPageSize);
  203. Footer:=TOmfRecord_LIBEND.Create;
  204. Footer.CalculatePaddingBytes(FLibData.Pos);
  205. RawRec:=TOmfRawRecord.Create;
  206. Footer.EncodeTo(RawRec);
  207. RawRec.WriteTo(FLibData);
  208. Footer.Free;
  209. RawRec.Free;
  210. end;
  211. procedure TOmfLibObjectWriter.WriteLib;
  212. var
  213. libf: TCCustomFileStream;
  214. DictStart: LongWord;
  215. DictBlocks: Word;
  216. begin
  217. libf:=CFileStreamClass.Create(FLibName,fmCreate);
  218. if CStreamError<>0 then
  219. begin
  220. Message1(exec_e_cant_create_archivefile,FLibName);
  221. exit;
  222. end;
  223. WriteFooter;
  224. DictStart:=FLibData.Pos;
  225. DictBlocks:=WriteDictionary;
  226. WriteHeader(DictStart,DictBlocks);
  227. FLibData.WriteStream(libf);
  228. libf.Free;
  229. end;
  230. function TOmfLibObjectWriter.WriteDictionary: Word;
  231. var
  232. nb: Word;
  233. begin
  234. for nb in OmfLibDictionaryBlockCounts do
  235. if TryWriteDictionaryWithSize(nb) then
  236. exit(nb);
  237. { could not write dictionary, even with the largest number of blocks }
  238. internalerror(2015042202);
  239. end;
  240. function TOmfLibObjectWriter.TryWriteDictionaryWithSize(nblocks: Word
  241. ): Boolean;
  242. const
  243. nbuckets=37;
  244. freespace=nbuckets;
  245. type
  246. PBlock=^TBlock;
  247. TBlock=array[0..511] of byte;
  248. var
  249. blocks: array of TBlock;
  250. i: Integer;
  251. N: TSymStr;
  252. length_of_string: Integer;
  253. h: TOmfLibHash;
  254. start_block,start_bucket: Integer;
  255. space_required: Integer;
  256. pb: PBlock;
  257. success: Boolean;
  258. store_at: Integer;
  259. PageNum: Word;
  260. begin
  261. SetLength(blocks,nblocks);
  262. for i:=0 to nblocks-1 do
  263. begin
  264. FillChar(blocks[i],SizeOf(blocks[i]),0);
  265. blocks[i][freespace]:=(freespace+1) div 2;
  266. end;
  267. for i:=0 to FDictionary.Count-1 do
  268. begin
  269. N:=TOmfLibDictionaryEntry(FDictionary[i]).Name;
  270. PageNum:=TOmfLibDictionaryEntry(FDictionary[i]).PageNum;
  271. length_of_string:=Length(N);
  272. h:=compute_omf_lib_hash(N,nblocks);
  273. start_block:=h.block_x;
  274. start_bucket:=h.bucket_x;
  275. space_required:=1+length_of_string+2;
  276. if odd(space_required) then
  277. Inc(space_required);
  278. repeat
  279. pb:=@blocks[h.block_x];
  280. success:=false;
  281. repeat
  282. if pb^[h.bucket_x]=0 then
  283. begin
  284. if (512-pb^[freespace]*2)<space_required then
  285. break;
  286. pb^[h.bucket_x]:=pb^[freespace];
  287. store_at:=2*pb^[h.bucket_x];
  288. pb^[store_at]:=length_of_string;
  289. Move(N[1],pb^[store_at+1],length_of_string);
  290. pb^[store_at+1+length_of_string]:=Byte(PageNum);
  291. pb^[store_at+1+length_of_string+1]:=Byte(PageNum shr 8);
  292. Inc(pb^[freespace],space_required div 2);
  293. if pb^[freespace]=0 then
  294. pb^[freespace]:=255;
  295. success:=true;
  296. break;
  297. end;
  298. h.bucket_x:=(h.bucket_x+h.bucket_d) mod nbuckets;
  299. until h.bucket_x=start_bucket;
  300. if not success then
  301. begin
  302. h.block_x:=(h.block_x+h.block_d) mod nblocks;
  303. if h.block_x=start_block then
  304. exit(false); // not enough blocks
  305. pb^[freespace]:=255;
  306. end;
  307. until success;
  308. end;
  309. FLibData.write(blocks[0],nblocks*SizeOf(TBlock));
  310. Result:=true;
  311. end;
  312. {*****************************************************************************
  313. TOmfLibObjectReader
  314. *****************************************************************************}
  315. procedure TOmfLibObjectReader.ReadLibrary;
  316. var
  317. RawRecord: TOmfRawRecord;
  318. Header: TOmfRecord_LIBHEAD;
  319. begin
  320. RawRecord:=TOmfRawRecord.Create;
  321. RawRecord.ReadFrom(Self);
  322. Header:=TOmfRecord_LIBHEAD.Create;
  323. Header.DecodeFrom(RawRecord);
  324. FPageSize:=Header.PageSize;
  325. FIsCaseSensitive:=Header.CaseSensitive;
  326. ReadDictionary(Header.DictionaryOffset, Header.DictionarySizeInBlocks);
  327. Header.Free;
  328. RawRecord.Free;
  329. end;
  330. procedure TOmfLibObjectReader.ReadDictionary(DictionaryOffset: DWord; DictionarySizeInBlocks: Word);
  331. const
  332. nbuckets=37;
  333. freespace=nbuckets;
  334. type
  335. PBlock=^TBlock;
  336. TBlock=array[0..511] of byte;
  337. var
  338. blocks: array of TBlock;
  339. blocknr: Integer;
  340. block: PBlock;
  341. ofs: Integer;
  342. bucket: Integer;
  343. length_of_string: Byte;
  344. name: string;
  345. PageNum: Integer;
  346. begin
  347. seek(DictionaryOffset);
  348. SetLength(blocks,DictionarySizeInBlocks);
  349. read(blocks[0],DictionarySizeInBlocks*SizeOf(TBlock));
  350. for blocknr:=0 to DictionarySizeInBlocks-1 do
  351. begin
  352. block:=@(blocks[blocknr]);
  353. for bucket:=0 to nbuckets-1 do
  354. if block^[bucket]<>0 then
  355. begin
  356. ofs:=2*block^[bucket];
  357. length_of_string:=block^[ofs];
  358. if (ofs+1+length_of_string+1)>High(TBlock) then
  359. begin
  360. Comment(V_Error,'OMF dictionary entry goes beyond end of block');
  361. continue;
  362. end;
  363. SetLength(name,length_of_string);
  364. Move(block^[ofs+1],name[1],length_of_string);
  365. PageNum:=block^[ofs+1+length_of_string]+
  366. block^[ofs+1+length_of_string+1] shl 8;
  367. TOmfLibDictionaryEntry.create(LibSymbols,name,PageNum);
  368. end;
  369. end;
  370. end;
  371. function TOmfLibObjectReader.getfilename: string;
  372. begin
  373. Result:=inherited getfilename;
  374. if CurrMemberName<>'' then
  375. result:=result+'('+CurrMemberName+')';
  376. end;
  377. function TOmfLibObjectReader.GetPos: longint;
  378. begin
  379. result:=inherited GetPos-CurrMemberPos;
  380. end;
  381. function TOmfLibObjectReader.GetIsArchive: boolean;
  382. begin
  383. result:=islib;
  384. end;
  385. constructor TOmfLibObjectReader.createAr(const Aarfn: string; allow_nonar: boolean);
  386. var
  387. RecType: Byte;
  388. begin
  389. inherited Create;
  390. LibSymbols:=TFPHashObjectList.Create(true);
  391. CurrMemberPos:=0;
  392. CurrMemberName:='';
  393. if inherited openfile(Aarfn) then
  394. begin
  395. Read(RecType,1);
  396. Seek(0);
  397. islib:=RecType=RT_LIBHEAD;
  398. if islib then
  399. ReadLibrary
  400. else if (not allow_nonar) then
  401. Comment(V_Error,'Not an OMF library file, illegal magic: '+filename);
  402. end;
  403. end;
  404. destructor TOmfLibObjectReader.destroy;
  405. begin
  406. inherited closefile;
  407. LibSymbols.Free;
  408. inherited Destroy;
  409. end;
  410. function TOmfLibObjectReader.openfile(const fn: string): boolean;
  411. var
  412. libsym: TOmfLibDictionaryEntry;
  413. RawRec: TOmfRawRecord;
  414. Header: TOmfRecord_THEADR;
  415. begin
  416. result:=false;
  417. libsym:=TOmfLibDictionaryEntry(LibSymbols.Find(ModName2DictEntry(fn)));
  418. if not assigned(libsym) then
  419. exit;
  420. CurrMemberPos:=libsym.PageNum*FPageSize;
  421. inherited Seek(CurrMemberPos);
  422. { read the header, to obtain the module name }
  423. RawRec:=TOmfRawRecord.Create;
  424. RawRec.ReadFrom(self);
  425. Header:=TOmfRecord_THEADR.Create;
  426. Header.DecodeFrom(RawRec);
  427. CurrMemberName:=Header.ModuleName;
  428. Header.Free;
  429. RawRec.Free;
  430. { go back to the beginning of the file }
  431. inherited Seek(CurrMemberPos);
  432. result:=true;
  433. end;
  434. procedure TOmfLibObjectReader.closefile;
  435. begin
  436. CurrMemberPos:=0;
  437. CurrMemberName:='';
  438. end;
  439. procedure TOmfLibObjectReader.seek(len: longint);
  440. begin
  441. inherited Seek(CurrMemberPos+len);
  442. end;
  443. end.