owomflib.pas 15 KB

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