owomflib.pas 15 KB

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