omfbase.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. {
  2. Copyright (c) 2015 by Nikolay Nikolov
  3. Contains Relocatable Object Module Format (OMF) definitions
  4. This is the object format used on the i8086-msdos platform.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit omfbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. {$H+}
  22. uses
  23. cclasses,
  24. owbase;
  25. const
  26. { OMF record types }
  27. RT_THEADR = $80; { Translator Header Record }
  28. RT_LHEADR = $82; { Library Module Header Record }
  29. RT_COMENT = $88; { Comment Record }
  30. RT_MODEND = $8A; { Module End Record }
  31. RT_MODEND32 = $8B;
  32. RT_EXTDEF = $8C; { External Names Definition Record }
  33. RT_PUBDEF = $90; { Public Names Definition Record }
  34. RT_PUBDEF32 = $91;
  35. RT_LINNUM = $94; { Line Numbers Record }
  36. RT_LINNUM32 = $95;
  37. RT_LNAMES = $96; { List of Names Record }
  38. RT_SEGDEF = $98; { Segment Definition Record }
  39. RT_SEGDEF32 = $99;
  40. RT_GRPDEF = $9A; { Group Definition Record }
  41. RT_FIXUPP = $9C; { Fixup Record }
  42. RT_FIXUPP32 = $9D;
  43. RT_LEDATA = $A0; { Logical Enumerated Data Record }
  44. RT_LEDATA32 = $A1;
  45. RT_LIDATA = $A2; { Logical Iterated Data Record }
  46. RT_LIDATA32 = $A3;
  47. RT_COMDEF = $B0; { Communal Names Definition Record }
  48. RT_BAKPAT = $B2; { Backpatch Record }
  49. RT_BAKPAT32 = $B3;
  50. RT_LEXTDEF = $B4; { Local External Names Definition Record }
  51. RT_LEXTDEF32 = $B5;
  52. RT_LPUBDEF = $B6; { Local Public Names Definition Record }
  53. RT_LPUBDEF32 = $B7;
  54. RT_LCOMDEF = $B8; { Local Communal Names Definition Record }
  55. RT_CEXTDEF = $BC; { COMDAT External Names Definition Record }
  56. RT_COMDAT = $C2; { Initialized Communal Data Record }
  57. RT_COMDAT32 = $C3;
  58. RT_LINSYM = $C4; { Symbol Line Numbers Record }
  59. RT_LINSYM32 = $C5;
  60. RT_ALIAS = $C6; { Alias Definition Record }
  61. RT_NBKPAT = $C8; { Named Backpatch Record }
  62. RT_NBKPAT32 = $C9;
  63. RT_LLNAMES = $CA; { Local Logical Names Definition Record }
  64. RT_VERNUM = $CC; { OMF Version Number Record }
  65. RT_VENDEXT = $CE; { Vendor-specific OMF Extension Record }
  66. { OMF comment class }
  67. CC_Translator = $00; { language translator (compiler or assembler) name }
  68. CC_IntelCopyright = $01;
  69. CC_IntelReservedRangeStart = $02;
  70. CC_IntelReservedRangeEnd = $9B;
  71. CC_LibrarySpecifierObsolete = $81;
  72. CC_MsDosVersionObsolete = $9C;
  73. CC_MemoryModel = $9D;
  74. CC_DOSSEG = $9E;
  75. CC_DefaultLibrarySearchName = $9F;
  76. CC_OmfExtension = $A0;
  77. CC_NewOmfExtension = $A1;
  78. CC_LinkPassSeparator = $A2;
  79. CC_LIBMOD = $A3;
  80. CC_EXESTR = $A4;
  81. CC_INCERR = $A6;
  82. CC_NOPAD = $A7;
  83. CC_WKEXT = $A8;
  84. CC_LZEXT = $A9;
  85. CC_Comment = $DA;
  86. CC_Compiler = $DB;
  87. CC_Date = $DC;
  88. CC_Timestamp = $DD;
  89. CC_User = $DF;
  90. CC_DependencyFileBorland = $E9;
  91. CC_CommandLineMicrosoft = $FF;
  92. type
  93. TOmfSegmentAlignment = (
  94. saAbsolute = 0,
  95. saRelocatableByteAligned = 1,
  96. saRelocatableWordAligned = 2,
  97. saRelocatableParaAligned = 3,
  98. saRelocatablePageAligned = 4, { 32-bit linkers extension }
  99. saRelocatableDWordAligned = 5, { 32-bit linkers extension }
  100. saNotSupported = 6,
  101. saNotDefined = 7);
  102. TOmfSegmentCombination = (
  103. scPrivate = 0,
  104. scReserved1 = 1,
  105. scPublic = 2,
  106. scReserved3 = 3,
  107. scPublic4 = 4, { same as scPublic }
  108. scStack = 5,
  109. scCommon = 6,
  110. scPublic7 = 7); { same as scPublic }
  111. TOmfSegmentUse = (suUse16, suUse32);
  112. TOmfFixupThread = (ftThread0, ftThread1, ftThread2, ftThread3);
  113. TOmfFixupMode = (fmSelfRelative, fmSegmentRelative);
  114. TOmfFixupLocationType = (
  115. fltLoByte = 0, { low 8 bits of 16-bit offset }
  116. fltOffset = 1, { 16-bit offset }
  117. fltBase = 2, { 16-bit base (segment) }
  118. fltFarPointer = 3, { 16-bit base:16-bit offset }
  119. fltHiByte = 4, { high 8 bits of 16-bit offset }
  120. fltLoaderResolvedOffset = 5, { PharLap: Offset32 }
  121. fltUndefined6 = 6, { PharLap: Pointer48 }
  122. fltUndefined7 = 7,
  123. fltUndefined8 = 8,
  124. fltOffset32 = 9, { 32-bit offset }
  125. fltUndefined10 = 10,
  126. fltFarPointer48 = 11, { 16-bit base:32-bit offset }
  127. fltUndefined12 = 12,
  128. fltLoaderResolvedOffset32 = 13,
  129. fltUndefined14 = 14,
  130. fltUndefined15 = 15);
  131. TOmfFixupFrameMethod = (
  132. ffmSegmentIndex = 0, { SI(<segment name>) - The frame is the canonic frame of the logical
  133. segment segment defined by the index }
  134. ffmGroupIndex = 1, { GI(<group name>) - The frame is the canonic frame of the group
  135. (= the canonic frame of the logical segment from the group,
  136. located at the lowest memory address) }
  137. ffmExternalIndex = 2, { EI(<symbol name>) - The frame is determined depending on the external's public definition:
  138. * if the symbol is defined relative to a logical segment and no defined group,
  139. the frame of the logical segment is used
  140. * if the symbol is defined absolutely, without reference to a logical segment and
  141. no defined group, the FRAME NUMBER from the symbol's PUBDEF record is used
  142. * regardless of how the symbol is specified, if there's an associated group,
  143. that group's canonic frame is used }
  144. ffmFrameNumber = 3, { <FRAME NUMBER> - The frame is a directly specified constant. }
  145. ffmLocation = 4, { LOCATION - The frame is determined by the location (i.e. the canonic frame of the logical
  146. segment where the fixup location is) }
  147. ffmTarget = 5, { TARGET - The frame is determined by the target. }
  148. ffmNone = 6, { NONE - There is no frame. Used for 8089 self-relative references. }
  149. ffmUndefined = 7);
  150. TOmfFixupTargetMethod = (
  151. ftmSegmentIndex = 0, { SI(<segment name>),<displacement> }
  152. ftmGroupIndex = 1, { GI(<group name>),<displacement> }
  153. ftmExternalIndex = 2, { EI(<symbol name>),<displacement> }
  154. ftmFrameNumber = 3, { <FRAME NUMBER>,<displacement> }
  155. ftmSegmentIndexNoDisp = 4, { SI(<segment name>) }
  156. ftmGroupIndexNoDisp = 5, { GI(<group name>) }
  157. ftmExternalIndexNoDisp = 6, { EI(<symbol name>) }
  158. ftmFrameNumberNoDisp = 7); { <FRAME NUMBER> }
  159. { TOmfOrderedNameCollection }
  160. TOmfOrderedNameCollection = class
  161. private
  162. FStringList: array of string;
  163. function GetCount: Integer;
  164. function GetString(Index: Integer): string;
  165. procedure SetString(Index: Integer; AValue: string);
  166. public
  167. function Add(const S: string): Integer;
  168. procedure Clear;
  169. property Strings [Index: Integer]: string read GetString write SetString; default;
  170. property Count: Integer read GetCount;
  171. end;
  172. { TOmfRawRecord }
  173. TOmfRawRecord = class
  174. private
  175. function GetChecksumByte: Byte;
  176. function GetRecordLength: Word;
  177. function GetRecordType: Byte;
  178. procedure SetChecksumByte(AValue: Byte);
  179. procedure SetRecordLength(AValue: Word);
  180. procedure SetRecordType(AValue: Byte);
  181. public
  182. RawData: array [-3..65535] of Byte;
  183. property RecordType: Byte read GetRecordType write SetRecordType;
  184. property RecordLength: Word read GetRecordLength write SetRecordLength;
  185. function ReadStringAt(Offset: Integer; out s: string): Integer;
  186. function WriteStringAt(Offset: Integer; s: string): Integer;
  187. function ReadIndexedRef(Offset: Integer; out IndexedRef: Integer): Integer;
  188. function WriteIndexedRef(Offset: Integer; IndexedRef: Integer): Integer;
  189. procedure CalculateChecksumByte;
  190. function VerifyChecksumByte: boolean;
  191. property ChecksumByte: Byte read GetChecksumByte write SetChecksumByte;
  192. procedure ReadFrom(aReader: TObjectReader);
  193. procedure WriteTo(aWriter: TObjectWriter);
  194. end;
  195. { TOmfParsedRecord }
  196. TOmfParsedRecord = class
  197. public
  198. procedure DecodeFrom(RawRecord: TOmfRawRecord);virtual;abstract;
  199. procedure EncodeTo(RawRecord: TOmfRawRecord);virtual;abstract;
  200. end;
  201. { TOmfRecord_THEADR }
  202. TOmfRecord_THEADR = class(TOmfParsedRecord)
  203. private
  204. FModuleName: string;
  205. public
  206. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  207. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  208. property ModuleName: string read FModuleName write FModuleName;
  209. end;
  210. { TOmfRecord_COMENT }
  211. TOmfRecord_COMENT = class(TOmfParsedRecord)
  212. private
  213. FCommentType: Byte;
  214. FCommentClass: Byte;
  215. FCommentString: string;
  216. function GetNoList: Boolean;
  217. function GetNoPurge: Boolean;
  218. procedure SetNoList(AValue: Boolean);
  219. procedure SetNoPurge(AValue: Boolean);
  220. public
  221. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  222. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  223. property CommentType: Byte read FCommentType write FCommentType;
  224. property CommentClass: Byte read FCommentClass write FCommentClass;
  225. property CommentString: string read FCommentString write FCommentString;
  226. property NoPurge: Boolean read GetNoPurge write SetNoPurge;
  227. property NoList: Boolean read GetNoList write SetNoList;
  228. end;
  229. { TOmfRecord_LNAMES }
  230. TOmfRecord_LNAMES = class(TOmfParsedRecord)
  231. private
  232. FNames: TOmfOrderedNameCollection;
  233. FNextIndex: Integer;
  234. public
  235. constructor Create;
  236. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  237. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  238. property Names: TOmfOrderedNameCollection read FNames write FNames;
  239. property NextIndex: Integer read FNextIndex write FNextIndex;
  240. end;
  241. { TOmfRecord_SEGDEF }
  242. TOmfRecord_SEGDEF = class(TOmfParsedRecord)
  243. private
  244. FAlignment: TOmfSegmentAlignment;
  245. FCombination: TOmfSegmentCombination;
  246. FUse: TOmfSegmentUse;
  247. FFrameNumber: Word;
  248. FOffset: Byte;
  249. FIs32Bit: Boolean;
  250. FSegmentLength: Int64; { int64, because it can be 2**32 }
  251. FSegmentNameIndex: Integer;
  252. FClassNameIndex: Integer;
  253. FOverlayNameIndex: Integer;
  254. public
  255. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  256. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  257. property Alignment: TOmfSegmentAlignment read FAlignment write FAlignment;
  258. property Combination: TOmfSegmentCombination read FCombination write FCombination;
  259. property Use: TOmfSegmentUse read FUse write FUse;
  260. property FrameNumber: Word read FFrameNumber write FFrameNumber;
  261. property Offset: Byte read FOffset write FOffset;
  262. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  263. property SegmentLength: Int64 read FSegmentLength write FSegmentLength;
  264. property SegmentNameIndex: Integer read FSegmentNameIndex write FSegmentNameIndex;
  265. property ClassNameIndex: Integer read FClassNameIndex write FClassNameIndex;
  266. property OverlayNameIndex: Integer read FOverlayNameIndex write FOverlayNameIndex;
  267. end;
  268. TSegmentList = array of Integer;
  269. { TOmfRecord_GRPDEF }
  270. TOmfRecord_GRPDEF = class(TOmfParsedRecord)
  271. private
  272. FGroupNameIndex: Integer;
  273. FSegmentList: TSegmentList;
  274. public
  275. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  276. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  277. property GroupNameIndex: Integer read FGroupNameIndex write FGroupNameIndex;
  278. property SegmentList: TSegmentList read FSegmentList write FSegmentList;
  279. end;
  280. { TOmfSubRecord_FIXUP }
  281. TOmfSubRecord_FIXUP = class
  282. private
  283. FIs32Bit: Boolean;
  284. FMode: TOmfFixupMode;
  285. FLocationType: TOmfFixupLocationType;
  286. FLocationOffset: DWord;
  287. FDataRecordStartOffset: DWord;
  288. FTargetDeterminedByThread: Boolean;
  289. FTargetThread: TOmfFixupThread;
  290. FTargetThreadDisplacementPresent: Boolean;
  291. FTargetMethod: TOmfFixupTargetMethod;
  292. FTargetDatum: Integer;
  293. FTargetDisplacement: DWord;
  294. FFrameDeterminedByThread: Boolean;
  295. FFrameThread: TOmfFixupThread;
  296. FFrameMethod: TOmfFixupFrameMethod;
  297. FFrameDatum: Integer;
  298. function GetDataRecordOffset: Integer;
  299. procedure SetDataRecordOffset(AValue: Integer);
  300. public
  301. function ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  302. function WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  303. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  304. property Mode: TOmfFixupMode read FMode write FMode;
  305. property LocationType: TOmfFixupLocationType read FLocationType write FLocationType;
  306. property LocationOffset: DWord read FLocationOffset write FLocationOffset;
  307. property DataRecordStartOffset: DWord read FDataRecordStartOffset write FDataRecordStartOffset;
  308. property DataRecordOffset: Integer read GetDataRecordOffset write SetDataRecordOffset;
  309. property TargetDeterminedByThread: Boolean read FTargetDeterminedByThread write FTargetDeterminedByThread;
  310. property TargetThread: TOmfFixupThread read FTargetThread write FTargetThread;
  311. property TargetThreadDisplacementPresent: Boolean read FTargetThreadDisplacementPresent write FTargetThreadDisplacementPresent;
  312. property TargetMethod: TOmfFixupTargetMethod read FTargetMethod write FTargetMethod;
  313. property TargetDatum: Integer read FTargetDatum write FTargetDatum;
  314. property TargetDisplacement: DWord read FTargetDisplacement write FTargetDisplacement;
  315. property FrameDeterminedByThread: Boolean read FFrameDeterminedByThread write FFrameDeterminedByThread;
  316. property FrameThread: TOmfFixupThread read FFrameThread write FFrameThread;
  317. property FrameMethod: TOmfFixupFrameMethod read FFrameMethod write FFrameMethod;
  318. property FrameDatum: Integer read FFrameDatum write FFrameDatum;
  319. end;
  320. implementation
  321. uses
  322. verbose;
  323. { TOmfOrderedNameCollection }
  324. function TOmfOrderedNameCollection.GetString(Index: Integer): string;
  325. begin
  326. Result:=FStringList[Index-1];
  327. end;
  328. function TOmfOrderedNameCollection.GetCount: Integer;
  329. begin
  330. Result:=Length(FStringList);
  331. end;
  332. procedure TOmfOrderedNameCollection.SetString(Index: Integer; AValue: string);
  333. begin
  334. FStringList[Index-1]:=AValue;
  335. end;
  336. function TOmfOrderedNameCollection.Add(const S: string): Integer;
  337. begin
  338. Result:=Length(FStringList)+1;
  339. SetLength(FStringList,Result);
  340. FStringList[Result-1]:=S;
  341. end;
  342. procedure TOmfOrderedNameCollection.Clear;
  343. begin
  344. SetLength(FStringList,0);
  345. end;
  346. { TOmfRawRecord }
  347. function TOmfRawRecord.GetRecordType: Byte;
  348. begin
  349. Result:=RawData[-3];
  350. end;
  351. procedure TOmfRawRecord.SetRecordType(AValue: Byte);
  352. begin
  353. RawData[-3]:=AValue;
  354. end;
  355. function TOmfRawRecord.GetRecordLength: Word;
  356. begin
  357. Result:=RawData[-2] or (RawData[-1] shl 8);
  358. end;
  359. procedure TOmfRawRecord.SetRecordLength(AValue: Word);
  360. begin
  361. RawData[-2]:=Byte(AValue);
  362. RawData[-1]:=Byte(AValue shr 8);
  363. end;
  364. function TOmfRawRecord.ReadStringAt(Offset: Integer; out s: string): Integer;
  365. var
  366. len: Byte;
  367. begin
  368. len:=RawData[Offset];
  369. Result:=Offset+len+1;
  370. if result>RecordLength then
  371. internalerror(2015033103);
  372. SetLength(s, len);
  373. UniqueString(s);
  374. Move(RawData[Offset+1],s[1],len);
  375. end;
  376. function TOmfRawRecord.WriteStringAt(Offset: Integer; s: string): Integer;
  377. begin
  378. if Length(s)>255 then
  379. internalerror(2015033101);
  380. result:=Offset+Length(s)+1;
  381. if result>High(RawData) then
  382. internalerror(2015033102);
  383. RawData[Offset]:=Length(s);
  384. Move(s[1], RawData[Offset+1], Length(s));
  385. end;
  386. function TOmfRawRecord.ReadIndexedRef(Offset: Integer; out IndexedRef: Integer): Integer;
  387. begin
  388. Result:=Offset+1;
  389. if result>RecordLength then
  390. internalerror(2015033103);
  391. IndexedRef:=RawData[Offset];
  392. if IndexedRef<=$7f then
  393. exit;
  394. Result:=Offset+2;
  395. if result>RecordLength then
  396. internalerror(2015033103);
  397. IndexedRef:=((IndexedRef and $7f) shl 8)+RawData[Offset+1];
  398. end;
  399. function TOmfRawRecord.WriteIndexedRef(Offset: Integer; IndexedRef: Integer): Integer;
  400. begin
  401. if (IndexedRef<0) or (IndexedRef>$7FFF) then
  402. internalerror(2015040303);
  403. if IndexedRef<=$7f then
  404. begin
  405. Result:=Offset+1;
  406. if Result>High(RawData) then
  407. internalerror(2015033102);
  408. RawData[Offset]:=IndexedRef;
  409. end
  410. else
  411. begin
  412. Result:=Offset+2;
  413. if Result>High(RawData) then
  414. internalerror(2015033102);
  415. RawData[Offset]:=$80+(IndexedRef shr 8);
  416. RawData[Offset+1]:=Byte(IndexedRef);
  417. end;
  418. end;
  419. function TOmfRawRecord.GetChecksumByte: Byte;
  420. begin
  421. if RecordLength>0 then
  422. Result:=RawData[RecordLength-1]
  423. else
  424. Result:=0;
  425. end;
  426. procedure TOmfRawRecord.SetChecksumByte(AValue: Byte);
  427. begin
  428. if RecordLength>0 then
  429. RawData[RecordLength-1]:=AValue;
  430. end;
  431. procedure TOmfRawRecord.CalculateChecksumByte;
  432. var
  433. I: Integer;
  434. b: Byte;
  435. begin
  436. b:=0;
  437. for I:=-3 to RecordLength-2 do
  438. b:=byte(b+RawData[I]);
  439. SetChecksumByte($100-b);
  440. end;
  441. function TOmfRawRecord.VerifyChecksumByte: boolean;
  442. var
  443. I: Integer;
  444. b: Byte;
  445. begin
  446. { according to the OMF spec, some tools always write a 0 rather than
  447. computing the checksum, so it should also be accepted as correct }
  448. if ChecksumByte=0 then
  449. exit(true);
  450. b:=0;
  451. for I:=-3 to RecordLength-1 do
  452. b:=byte(b+RawData[I]);
  453. Result:=(b=0);
  454. end;
  455. procedure TOmfRawRecord.ReadFrom(aReader: TObjectReader);
  456. begin
  457. aReader.read(RawData, 3);
  458. aReader.read(RawData[0], RecordLength);
  459. end;
  460. procedure TOmfRawRecord.WriteTo(aWriter: TObjectWriter);
  461. begin
  462. aWriter.write(RawData, RecordLength+3);
  463. end;
  464. { TOmfRecord_THEADR }
  465. procedure TOmfRecord_THEADR.DecodeFrom(RawRecord: TOmfRawRecord);
  466. begin
  467. RawRecord.ReadStringAt(0,FModuleName);
  468. end;
  469. procedure TOmfRecord_THEADR.EncodeTo(RawRecord: TOmfRawRecord);
  470. var
  471. NextOfs: Integer;
  472. begin
  473. RawRecord.RecordType:=RT_THEADR;
  474. NextOfs:=RawRecord.WriteStringAt(0,ModuleName);
  475. RawRecord.RecordLength:=NextOfs+1;
  476. RawRecord.CalculateChecksumByte;
  477. end;
  478. { TOmfRecord_COMENT }
  479. function TOmfRecord_COMENT.GetNoList: Boolean;
  480. begin
  481. Result:=(CommentType and $40)<>0;
  482. end;
  483. function TOmfRecord_COMENT.GetNoPurge: Boolean;
  484. begin
  485. Result:=(CommentType and $80)<>0;
  486. end;
  487. procedure TOmfRecord_COMENT.SetNoList(AValue: Boolean);
  488. begin
  489. if AValue then
  490. CommentType:=CommentType or $40
  491. else
  492. CommentType:=CommentType and $BF;
  493. end;
  494. procedure TOmfRecord_COMENT.SetNoPurge(AValue: Boolean);
  495. begin
  496. if AValue then
  497. CommentType:=CommentType or $80
  498. else
  499. CommentType:=CommentType and $7F;
  500. end;
  501. procedure TOmfRecord_COMENT.DecodeFrom(RawRecord: TOmfRawRecord);
  502. begin
  503. if RawRecord.RecordLength<3 then
  504. internalerror(2015033104);
  505. CommentType:=RawRecord.RawData[0];
  506. CommentClass:=RawRecord.RawData[1];
  507. SetLength(FCommentString,RawRecord.RecordLength-3);
  508. UniqueString(FCommentString);
  509. Move(RawRecord.RawData[2],FCommentString[1],Length(FCommentString));
  510. end;
  511. procedure TOmfRecord_COMENT.EncodeTo(RawRecord: TOmfRawRecord);
  512. begin
  513. RawRecord.RecordType:=RT_COMENT;
  514. if (Length(FCommentString)+3)>High(RawRecord.RawData) then
  515. internalerror(2015033105);
  516. RawRecord.RecordLength:=Length(FCommentString)+3;
  517. RawRecord.RawData[0]:=CommentType;
  518. RawRecord.RawData[1]:=CommentClass;
  519. Move(FCommentString[1],RawRecord.RawData[2],Length(FCommentString));
  520. RawRecord.CalculateChecksumByte;
  521. end;
  522. { TOmfRecord_LNAMES }
  523. constructor TOmfRecord_LNAMES.Create;
  524. begin
  525. FNextIndex:=1;
  526. end;
  527. procedure TOmfRecord_LNAMES.DecodeFrom(RawRecord: TOmfRawRecord);
  528. begin
  529. {TODO: implement}
  530. internalerror(2015040101);
  531. end;
  532. procedure TOmfRecord_LNAMES.EncodeTo(RawRecord: TOmfRawRecord);
  533. const
  534. RecordLengthLimit = 1024;
  535. var
  536. Len,LastIncludedIndex,NextOfs,I: Integer;
  537. begin
  538. RawRecord.RecordType:=RT_LNAMES;
  539. { find out how many strings can we include until we reach the length limit }
  540. Len:=1;
  541. LastIncludedIndex:=NextIndex-1;
  542. repeat
  543. Inc(LastIncludedIndex);
  544. Inc(Len,Length(Names[LastIncludedIndex])+1);
  545. until (LastIncludedIndex>=Names.Count) or ((Len+Length(Names[LastIncludedIndex+1])+1)>=RecordLengthLimit);
  546. { write the strings... }
  547. NextOfs:=0;
  548. for I:=NextIndex to LastIncludedIndex do
  549. NextOfs:=RawRecord.WriteStringAt(NextOfs,Names[I]);
  550. RawRecord.RecordLength:=Len;
  551. RawRecord.CalculateChecksumByte;
  552. { update NextIndex }
  553. NextIndex:=LastIncludedIndex+1;
  554. end;
  555. { TOmfRecord_SEGDEF }
  556. procedure TOmfRecord_SEGDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  557. var
  558. B: Byte;
  559. Big: Boolean;
  560. NextOfs: Integer;
  561. MinLen: Integer;
  562. begin
  563. if not (RawRecord.RecordType in [RT_SEGDEF,RT_SEGDEF32]) then
  564. internalerror(2015040301);
  565. Is32Bit:=RawRecord.RecordType=RT_SEGDEF32;
  566. MinLen:=7; { b(1)+seglength(2..4)+segnameindex(1..2)+classnameindex(1..2)+overlaynameindex(1..2)+checksum }
  567. if Is32Bit then
  568. inc(MinLen,2);
  569. if RawRecord.RecordLength<MinLen then
  570. internalerror(2015040305);
  571. B:=RawRecord.RawData[0];
  572. Alignment:=TOmfSegmentAlignment(B shr 5);
  573. Combination:=TOmfSegmentCombination((B shr 2) and 7);
  574. Big:=(B and 2)<>0;
  575. Use:=TOmfSegmentUse(B and 1);
  576. NextOfs:=1;
  577. if Alignment=saAbsolute then
  578. begin
  579. inc(MinLen,3);
  580. if RawRecord.RecordLength<MinLen then
  581. internalerror(2015040305);
  582. FrameNumber:=RawRecord.RawData[1]+(RawRecord.RawData[2] shl 8);
  583. Offset:=RawRecord.RawData[3];
  584. NextOfs:=4;
  585. end
  586. else
  587. begin
  588. FrameNumber:=0;
  589. Offset:=0;
  590. end;
  591. if Is32Bit then
  592. begin
  593. SegmentLength:=RawRecord.RawData[NextOfs]+
  594. (RawRecord.RawData[NextOfs+1] shl 8)+
  595. (RawRecord.RawData[NextOfs+2] shl 16)+
  596. (RawRecord.RawData[NextOfs+3] shl 24);
  597. if Big then
  598. if SegmentLength=0 then
  599. SegmentLength:=4294967296
  600. else
  601. internalerror(2015040306);
  602. Inc(NextOfs,4);
  603. end
  604. else
  605. begin
  606. SegmentLength:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  607. if Big then
  608. if SegmentLength=0 then
  609. SegmentLength:=65536
  610. else
  611. internalerror(2015040306);
  612. Inc(NextOfs,2);
  613. end;
  614. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FSegmentNameIndex);
  615. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FClassNameIndex);
  616. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FOverlayNameIndex);
  617. end;
  618. procedure TOmfRecord_SEGDEF.EncodeTo(RawRecord: TOmfRawRecord);
  619. var
  620. Big: Boolean;
  621. NextOfs: Integer;
  622. begin
  623. if Is32Bit then
  624. begin
  625. RawRecord.RecordType:=RT_SEGDEF32;
  626. if SegmentLength>4294967296 then
  627. internalerror(2015040302);
  628. Big:=SegmentLength=4294967296;
  629. end
  630. else
  631. begin
  632. RawRecord.RecordType:=RT_SEGDEF;
  633. if SegmentLength>65536 then
  634. internalerror(2015040302);
  635. Big:=SegmentLength=65536;
  636. end;
  637. RawRecord.RawData[0]:=(Ord(Alignment) shl 5) or (Ord(Combination) shl 2) or (Ord(Big) shl 1) or Ord(Use);
  638. NextOfs:=1;
  639. if Alignment=saAbsolute then
  640. begin
  641. RawRecord.RawData[1]:=Byte(FrameNumber);
  642. RawRecord.RawData[2]:=Byte(FrameNumber shr 8);
  643. RawRecord.RawData[3]:=Offset;
  644. NextOfs:=4;
  645. end;
  646. if Is32Bit then
  647. begin
  648. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  649. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  650. RawRecord.RawData[NextOfs+2]:=Byte(SegmentLength shr 16);
  651. RawRecord.RawData[NextOfs+3]:=Byte(SegmentLength shr 24);
  652. Inc(NextOfs,4);
  653. end
  654. else
  655. begin
  656. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  657. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  658. Inc(NextOfs,2);
  659. end;
  660. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,SegmentNameIndex);
  661. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ClassNameIndex);
  662. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,OverlayNameIndex);
  663. RawRecord.RecordLength:=NextOfs+1;
  664. RawRecord.CalculateChecksumByte;
  665. end;
  666. { TOmfRecord_GRPDEF }
  667. procedure TOmfRecord_GRPDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  668. begin
  669. {TODO: implement}
  670. internalerror(2015040101);
  671. end;
  672. procedure TOmfRecord_GRPDEF.EncodeTo(RawRecord: TOmfRawRecord);
  673. var
  674. NextOfs: Integer;
  675. Segment: Integer;
  676. begin
  677. RawRecord.RecordType:=RT_GRPDEF;
  678. NextOfs:=RawRecord.WriteIndexedRef(0,GroupNameIndex);
  679. for Segment in SegmentList do
  680. begin
  681. if NextOfs>High(RawRecord.RawData) then
  682. internalerror(2015040401);
  683. RawRecord.RawData[NextOfs]:=$ff;
  684. NextOfs:=RawRecord.WriteIndexedRef(NextOfs+1,Segment);
  685. end;
  686. RawRecord.RecordLength:=NextOfs+1;
  687. RawRecord.CalculateChecksumByte;
  688. end;
  689. { TOmfSubRecord_FIXUP }
  690. function TOmfSubRecord_FIXUP.GetDataRecordOffset: Integer;
  691. begin
  692. Result:=FLocationOffset-FDataRecordStartOffset;
  693. end;
  694. procedure TOmfSubRecord_FIXUP.SetDataRecordOffset(AValue: Integer);
  695. begin
  696. FLocationOffset:=AValue+FDataRecordStartOffset;
  697. end;
  698. function TOmfSubRecord_FIXUP.ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  699. var
  700. Locat: Word;
  701. FixData: Byte;
  702. begin
  703. if (Offset+2)>High(RawRecord.RawData) then
  704. internalerror(2015040504);
  705. { unlike other fields in the OMF format, this one is big endian }
  706. Locat:=(RawRecord.RawData[Offset] shl 8) or RawRecord.RawData[Offset+1];
  707. FixData:=RawRecord.RawData[Offset+2];
  708. Inc(Offset,3);
  709. if (Locat and $8000)=0 then
  710. internalerror(2015040503);
  711. DataRecordOffset:=Locat and $3FF;
  712. LocationType:=TOmfFixupLocationType((Locat shr 10) and 15);
  713. Mode:=TOmfFixupMode((Locat shr 14) and 1);
  714. FrameDeterminedByThread:=(FixData and $80)<>0;
  715. TargetDeterminedByThread:=(FixData and $08)<>0;
  716. if FrameDeterminedByThread then
  717. FrameThread:=TOmfFixupThread((FixData shr 4) and 3)
  718. else
  719. FrameMethod:=TOmfFixupFrameMethod((FixData shr 4) and 7);
  720. if TargetDeterminedByThread then
  721. begin
  722. TargetThread:=TOmfFixupThread(FixData and 3);
  723. TargetThreadDisplacementPresent:=(FixData and $40)=0;
  724. end
  725. else
  726. TargetMethod:=TOmfFixupTargetMethod(FixData and 7);
  727. { read Frame Datum? }
  728. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  729. Offset:=RawRecord.ReadIndexedRef(Offset,FFrameDatum)
  730. else
  731. FrameDatum:=0;
  732. { read Target Datum? }
  733. if not TargetDeterminedByThread then
  734. Offset:=RawRecord.ReadIndexedRef(Offset,FTargetDatum)
  735. else
  736. TargetDatum:=0;
  737. { read Target Displacement? }
  738. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  739. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  740. begin
  741. if Is32Bit then
  742. begin
  743. if (Offset+3)>High(RawRecord.RawData) then
  744. internalerror(2015040504);
  745. TargetDisplacement := RawRecord.RawData[Offset]+
  746. (RawRecord.RawData[Offset+1] shl 8)+
  747. (RawRecord.RawData[Offset+2] shl 16)+
  748. (RawRecord.RawData[Offset+3] shl 24);
  749. Inc(Offset,4);
  750. end
  751. else
  752. begin
  753. if (Offset+1)>High(RawRecord.RawData) then
  754. internalerror(2015040504);
  755. TargetDisplacement := RawRecord.RawData[Offset]+
  756. (RawRecord.RawData[Offset+1] shl 8);
  757. Inc(Offset,2);
  758. end;
  759. end;
  760. Result:=Offset;
  761. end;
  762. function TOmfSubRecord_FIXUP.WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  763. var
  764. Locat: Word;
  765. FixData: Byte;
  766. begin
  767. if (DataRecordOffset<0) or (DataRecordOffset>1023) then
  768. internalerror(2015040501);
  769. Locat:=$8000+(Ord(Mode) shl 14)+(Ord(LocationType) shl 10)+DataRecordOffset;
  770. { unlike other fields in the OMF format, this one is big endian }
  771. RawRecord.RawData[Offset]:=Byte(Locat shr 8);
  772. RawRecord.RawData[Offset+1]:=Byte(Locat);
  773. Inc(Offset, 2);
  774. FixData:=(Ord(FrameDeterminedByThread) shl 7)+(Ord(TargetDeterminedByThread) shl 3);
  775. if FrameDeterminedByThread then
  776. FixData:=FixData+(Ord(FrameThread) shl 4)
  777. else
  778. FixData:=FixData+(Ord(FrameMethod) shl 4);
  779. if TargetDeterminedByThread then
  780. FixData:=FixData+Ord(TargetThread)+(Ord(not TargetThreadDisplacementPresent) shl 2)
  781. else
  782. FixData:=FixData+Ord(TargetMethod);
  783. RawRecord.RawData[Offset]:=FixData;
  784. Inc(Offset);
  785. { save Frame Datum? }
  786. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  787. Offset:=RawRecord.WriteIndexedRef(Offset,FrameDatum);
  788. { save Target Datum? }
  789. if not TargetDeterminedByThread then
  790. Offset:=RawRecord.WriteIndexedRef(Offset,TargetDatum);
  791. { save Target Displacement? }
  792. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  793. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  794. begin
  795. if Is32Bit then
  796. begin
  797. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  798. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  799. RawRecord.RawData[Offset+2]:=Byte(TargetDisplacement shr 16);
  800. RawRecord.RawData[Offset+3]:=Byte(TargetDisplacement shr 24);
  801. Inc(Offset,4);
  802. end
  803. else
  804. begin
  805. if TargetDisplacement>$ffff then
  806. internalerror(2015040502);
  807. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  808. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  809. Inc(Offset,2);
  810. end;
  811. end;
  812. Result:=Offset;
  813. end;
  814. end.