omfbase.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. { TOmfPublicNameElement }
  281. TOmfPublicNameElement = class(TFPHashObject)
  282. private
  283. FPublicOffset: DWord;
  284. FTypeIndex: Integer;
  285. public
  286. function GetLengthInFile(Is32Bit: Boolean): Integer;
  287. property PublicOffset: DWord read FPublicOffset write FPublicOffset;
  288. property TypeIndex: Integer read FTypeIndex write FTypeIndex;
  289. end;
  290. { TOmfRecord_PUBDEF }
  291. TOmfRecord_PUBDEF = class(TOmfParsedRecord)
  292. private
  293. FIs32Bit: Boolean;
  294. FBaseGroupIndex: Integer;
  295. FBaseSegmentIndex: Integer;
  296. FBaseFrame: Word;
  297. FPublicNames: TFPHashObjectList;
  298. FNextIndex: Integer;
  299. public
  300. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  301. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  302. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  303. property BaseGroupIndex: Integer read FBaseGroupIndex write FBaseGroupIndex;
  304. property BaseSegmentIndex: Integer read FBaseSegmentIndex write FBaseSegmentIndex;
  305. property BaseFrame: Word read FBaseFrame write FBaseFrame;
  306. property PublicNames: TFPHashObjectList read FPublicNames write FPublicNames;
  307. property NextIndex: Integer read FNextIndex write FNextIndex;
  308. end;
  309. { TOmfExternalNameElement }
  310. TOmfExternalNameElement = class(TFPHashObject)
  311. private
  312. FTypeIndex: Integer;
  313. public
  314. function GetLengthInFile: Integer;
  315. property TypeIndex: Integer read FTypeIndex write FTypeIndex;
  316. end;
  317. { TOmfRecord_EXTDEF }
  318. TOmfRecord_EXTDEF = class(TOmfParsedRecord)
  319. private
  320. FExternalNames: TFPHashObjectList;
  321. FNextIndex: Integer;
  322. public
  323. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  324. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  325. property ExternalNames: TFPHashObjectList read FExternalNames write FExternalNames;
  326. property NextIndex: Integer read FNextIndex write FNextIndex;
  327. end;
  328. { TOmfRecord_MODEND }
  329. TOmfRecord_MODEND = class(TOmfParsedRecord)
  330. private
  331. FIs32Bit: Boolean;
  332. FIsMainModule: Boolean;
  333. FHasStartAddress: Boolean;
  334. FSegmentBit: Boolean;
  335. FLogicalStartAddress: Boolean;
  336. public
  337. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  338. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  339. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  340. property IsMainModule: Boolean read FIsMainModule write FIsMainModule;
  341. property HasStartAddress: Boolean read FHasStartAddress write FHasStartAddress;
  342. property SegmentBit: Boolean read FSegmentBit write FSegmentBit;
  343. property LogicalStartAddress: Boolean read FLogicalStartAddress write FLogicalStartAddress;
  344. end;
  345. { TOmfSubRecord_FIXUP }
  346. TOmfSubRecord_FIXUP = class
  347. private
  348. FIs32Bit: Boolean;
  349. FMode: TOmfFixupMode;
  350. FLocationType: TOmfFixupLocationType;
  351. FLocationOffset: DWord;
  352. FDataRecordStartOffset: DWord;
  353. FTargetDeterminedByThread: Boolean;
  354. FTargetThread: TOmfFixupThread;
  355. FTargetThreadDisplacementPresent: Boolean;
  356. FTargetMethod: TOmfFixupTargetMethod;
  357. FTargetDatum: Integer;
  358. FTargetDisplacement: DWord;
  359. FFrameDeterminedByThread: Boolean;
  360. FFrameThread: TOmfFixupThread;
  361. FFrameMethod: TOmfFixupFrameMethod;
  362. FFrameDatum: Integer;
  363. function GetDataRecordOffset: Integer;
  364. procedure SetDataRecordOffset(AValue: Integer);
  365. public
  366. function ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  367. function WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  368. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  369. property Mode: TOmfFixupMode read FMode write FMode;
  370. property LocationType: TOmfFixupLocationType read FLocationType write FLocationType;
  371. property LocationOffset: DWord read FLocationOffset write FLocationOffset;
  372. property DataRecordStartOffset: DWord read FDataRecordStartOffset write FDataRecordStartOffset;
  373. property DataRecordOffset: Integer read GetDataRecordOffset write SetDataRecordOffset;
  374. property TargetDeterminedByThread: Boolean read FTargetDeterminedByThread write FTargetDeterminedByThread;
  375. property TargetThread: TOmfFixupThread read FTargetThread write FTargetThread;
  376. property TargetThreadDisplacementPresent: Boolean read FTargetThreadDisplacementPresent write FTargetThreadDisplacementPresent;
  377. property TargetMethod: TOmfFixupTargetMethod read FTargetMethod write FTargetMethod;
  378. property TargetDatum: Integer read FTargetDatum write FTargetDatum;
  379. property TargetDisplacement: DWord read FTargetDisplacement write FTargetDisplacement;
  380. property FrameDeterminedByThread: Boolean read FFrameDeterminedByThread write FFrameDeterminedByThread;
  381. property FrameThread: TOmfFixupThread read FFrameThread write FFrameThread;
  382. property FrameMethod: TOmfFixupFrameMethod read FFrameMethod write FFrameMethod;
  383. property FrameDatum: Integer read FFrameDatum write FFrameDatum;
  384. end;
  385. implementation
  386. uses
  387. verbose;
  388. { TOmfOrderedNameCollection }
  389. function TOmfOrderedNameCollection.GetString(Index: Integer): string;
  390. begin
  391. Result:=FStringList[Index-1];
  392. end;
  393. function TOmfOrderedNameCollection.GetCount: Integer;
  394. begin
  395. Result:=Length(FStringList);
  396. end;
  397. procedure TOmfOrderedNameCollection.SetString(Index: Integer; AValue: string);
  398. begin
  399. FStringList[Index-1]:=AValue;
  400. end;
  401. function TOmfOrderedNameCollection.Add(const S: string): Integer;
  402. begin
  403. Result:=Length(FStringList)+1;
  404. SetLength(FStringList,Result);
  405. FStringList[Result-1]:=S;
  406. end;
  407. procedure TOmfOrderedNameCollection.Clear;
  408. begin
  409. SetLength(FStringList,0);
  410. end;
  411. { TOmfRawRecord }
  412. function TOmfRawRecord.GetRecordType: Byte;
  413. begin
  414. Result:=RawData[-3];
  415. end;
  416. procedure TOmfRawRecord.SetRecordType(AValue: Byte);
  417. begin
  418. RawData[-3]:=AValue;
  419. end;
  420. function TOmfRawRecord.GetRecordLength: Word;
  421. begin
  422. Result:=RawData[-2] or (RawData[-1] shl 8);
  423. end;
  424. procedure TOmfRawRecord.SetRecordLength(AValue: Word);
  425. begin
  426. RawData[-2]:=Byte(AValue);
  427. RawData[-1]:=Byte(AValue shr 8);
  428. end;
  429. function TOmfRawRecord.ReadStringAt(Offset: Integer; out s: string): Integer;
  430. var
  431. len: Byte;
  432. begin
  433. len:=RawData[Offset];
  434. Result:=Offset+len+1;
  435. if result>RecordLength then
  436. internalerror(2015033103);
  437. SetLength(s, len);
  438. UniqueString(s);
  439. Move(RawData[Offset+1],s[1],len);
  440. end;
  441. function TOmfRawRecord.WriteStringAt(Offset: Integer; s: string): Integer;
  442. begin
  443. if Length(s)>255 then
  444. internalerror(2015033101);
  445. result:=Offset+Length(s)+1;
  446. if result>High(RawData) then
  447. internalerror(2015033102);
  448. RawData[Offset]:=Length(s);
  449. Move(s[1], RawData[Offset+1], Length(s));
  450. end;
  451. function TOmfRawRecord.ReadIndexedRef(Offset: Integer; out IndexedRef: Integer): Integer;
  452. begin
  453. Result:=Offset+1;
  454. if result>RecordLength then
  455. internalerror(2015033103);
  456. IndexedRef:=RawData[Offset];
  457. if IndexedRef<=$7f then
  458. exit;
  459. Result:=Offset+2;
  460. if result>RecordLength then
  461. internalerror(2015033103);
  462. IndexedRef:=((IndexedRef and $7f) shl 8)+RawData[Offset+1];
  463. end;
  464. function TOmfRawRecord.WriteIndexedRef(Offset: Integer; IndexedRef: Integer): Integer;
  465. begin
  466. if (IndexedRef<0) or (IndexedRef>$7FFF) then
  467. internalerror(2015040303);
  468. if IndexedRef<=$7f then
  469. begin
  470. Result:=Offset+1;
  471. if Result>High(RawData) then
  472. internalerror(2015033102);
  473. RawData[Offset]:=IndexedRef;
  474. end
  475. else
  476. begin
  477. Result:=Offset+2;
  478. if Result>High(RawData) then
  479. internalerror(2015033102);
  480. RawData[Offset]:=$80+(IndexedRef shr 8);
  481. RawData[Offset+1]:=Byte(IndexedRef);
  482. end;
  483. end;
  484. function TOmfRawRecord.GetChecksumByte: Byte;
  485. begin
  486. if RecordLength>0 then
  487. Result:=RawData[RecordLength-1]
  488. else
  489. Result:=0;
  490. end;
  491. procedure TOmfRawRecord.SetChecksumByte(AValue: Byte);
  492. begin
  493. if RecordLength>0 then
  494. RawData[RecordLength-1]:=AValue;
  495. end;
  496. procedure TOmfRawRecord.CalculateChecksumByte;
  497. var
  498. I: Integer;
  499. b: Byte;
  500. begin
  501. b:=0;
  502. for I:=-3 to RecordLength-2 do
  503. b:=byte(b+RawData[I]);
  504. SetChecksumByte($100-b);
  505. end;
  506. function TOmfRawRecord.VerifyChecksumByte: boolean;
  507. var
  508. I: Integer;
  509. b: Byte;
  510. begin
  511. { according to the OMF spec, some tools always write a 0 rather than
  512. computing the checksum, so it should also be accepted as correct }
  513. if ChecksumByte=0 then
  514. exit(true);
  515. b:=0;
  516. for I:=-3 to RecordLength-1 do
  517. b:=byte(b+RawData[I]);
  518. Result:=(b=0);
  519. end;
  520. procedure TOmfRawRecord.ReadFrom(aReader: TObjectReader);
  521. begin
  522. aReader.read(RawData, 3);
  523. aReader.read(RawData[0], RecordLength);
  524. end;
  525. procedure TOmfRawRecord.WriteTo(aWriter: TObjectWriter);
  526. begin
  527. aWriter.write(RawData, RecordLength+3);
  528. end;
  529. { TOmfRecord_THEADR }
  530. procedure TOmfRecord_THEADR.DecodeFrom(RawRecord: TOmfRawRecord);
  531. begin
  532. if RawRecord.RecordType<>RT_THEADR then
  533. internalerror(2015040301);
  534. RawRecord.ReadStringAt(0,FModuleName);
  535. end;
  536. procedure TOmfRecord_THEADR.EncodeTo(RawRecord: TOmfRawRecord);
  537. var
  538. NextOfs: Integer;
  539. begin
  540. RawRecord.RecordType:=RT_THEADR;
  541. NextOfs:=RawRecord.WriteStringAt(0,ModuleName);
  542. RawRecord.RecordLength:=NextOfs+1;
  543. RawRecord.CalculateChecksumByte;
  544. end;
  545. { TOmfRecord_COMENT }
  546. function TOmfRecord_COMENT.GetNoList: Boolean;
  547. begin
  548. Result:=(CommentType and $40)<>0;
  549. end;
  550. function TOmfRecord_COMENT.GetNoPurge: Boolean;
  551. begin
  552. Result:=(CommentType and $80)<>0;
  553. end;
  554. procedure TOmfRecord_COMENT.SetNoList(AValue: Boolean);
  555. begin
  556. if AValue then
  557. CommentType:=CommentType or $40
  558. else
  559. CommentType:=CommentType and $BF;
  560. end;
  561. procedure TOmfRecord_COMENT.SetNoPurge(AValue: Boolean);
  562. begin
  563. if AValue then
  564. CommentType:=CommentType or $80
  565. else
  566. CommentType:=CommentType and $7F;
  567. end;
  568. procedure TOmfRecord_COMENT.DecodeFrom(RawRecord: TOmfRawRecord);
  569. begin
  570. if RawRecord.RecordType<>RT_COMENT then
  571. internalerror(2015040301);
  572. if RawRecord.RecordLength<3 then
  573. internalerror(2015033104);
  574. CommentType:=RawRecord.RawData[0];
  575. CommentClass:=RawRecord.RawData[1];
  576. SetLength(FCommentString,RawRecord.RecordLength-3);
  577. UniqueString(FCommentString);
  578. Move(RawRecord.RawData[2],FCommentString[1],Length(FCommentString));
  579. end;
  580. procedure TOmfRecord_COMENT.EncodeTo(RawRecord: TOmfRawRecord);
  581. begin
  582. RawRecord.RecordType:=RT_COMENT;
  583. if (Length(FCommentString)+3)>High(RawRecord.RawData) then
  584. internalerror(2015033105);
  585. RawRecord.RecordLength:=Length(FCommentString)+3;
  586. RawRecord.RawData[0]:=CommentType;
  587. RawRecord.RawData[1]:=CommentClass;
  588. Move(FCommentString[1],RawRecord.RawData[2],Length(FCommentString));
  589. RawRecord.CalculateChecksumByte;
  590. end;
  591. { TOmfRecord_LNAMES }
  592. constructor TOmfRecord_LNAMES.Create;
  593. begin
  594. FNextIndex:=1;
  595. end;
  596. procedure TOmfRecord_LNAMES.DecodeFrom(RawRecord: TOmfRawRecord);
  597. var
  598. NextOfs: Integer;
  599. Name: string;
  600. begin
  601. if RawRecord.RecordType<>RT_LNAMES then
  602. internalerror(2015040301);
  603. NextOfs:=0;
  604. while NextOfs<(RawRecord.RecordLength-1) do
  605. begin
  606. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  607. Names.Add(Name);
  608. end;
  609. end;
  610. procedure TOmfRecord_LNAMES.EncodeTo(RawRecord: TOmfRawRecord);
  611. const
  612. RecordLengthLimit = 1024;
  613. var
  614. Len,LastIncludedIndex,NextOfs,I: Integer;
  615. begin
  616. RawRecord.RecordType:=RT_LNAMES;
  617. { find out how many strings can we include until we reach the length limit }
  618. Len:=1;
  619. LastIncludedIndex:=NextIndex-1;
  620. repeat
  621. Inc(LastIncludedIndex);
  622. Inc(Len,Length(Names[LastIncludedIndex])+1);
  623. until (LastIncludedIndex>=Names.Count) or ((Len+Length(Names[LastIncludedIndex+1])+1)>=RecordLengthLimit);
  624. { write the strings... }
  625. NextOfs:=0;
  626. for I:=NextIndex to LastIncludedIndex do
  627. NextOfs:=RawRecord.WriteStringAt(NextOfs,Names[I]);
  628. RawRecord.RecordLength:=Len;
  629. RawRecord.CalculateChecksumByte;
  630. { update NextIndex }
  631. NextIndex:=LastIncludedIndex+1;
  632. end;
  633. { TOmfRecord_SEGDEF }
  634. procedure TOmfRecord_SEGDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  635. var
  636. B: Byte;
  637. Big: Boolean;
  638. NextOfs: Integer;
  639. MinLen: Integer;
  640. begin
  641. if not (RawRecord.RecordType in [RT_SEGDEF,RT_SEGDEF32]) then
  642. internalerror(2015040301);
  643. Is32Bit:=RawRecord.RecordType=RT_SEGDEF32;
  644. MinLen:=7; { b(1)+seglength(2..4)+segnameindex(1..2)+classnameindex(1..2)+overlaynameindex(1..2)+checksum }
  645. if Is32Bit then
  646. inc(MinLen,2);
  647. if RawRecord.RecordLength<MinLen then
  648. internalerror(2015040305);
  649. B:=RawRecord.RawData[0];
  650. Alignment:=TOmfSegmentAlignment(B shr 5);
  651. Combination:=TOmfSegmentCombination((B shr 2) and 7);
  652. Big:=(B and 2)<>0;
  653. Use:=TOmfSegmentUse(B and 1);
  654. NextOfs:=1;
  655. if Alignment=saAbsolute then
  656. begin
  657. inc(MinLen,3);
  658. if RawRecord.RecordLength<MinLen then
  659. internalerror(2015040305);
  660. FrameNumber:=RawRecord.RawData[1]+(RawRecord.RawData[2] shl 8);
  661. Offset:=RawRecord.RawData[3];
  662. NextOfs:=4;
  663. end
  664. else
  665. begin
  666. FrameNumber:=0;
  667. Offset:=0;
  668. end;
  669. if Is32Bit then
  670. begin
  671. SegmentLength:=RawRecord.RawData[NextOfs]+
  672. (RawRecord.RawData[NextOfs+1] shl 8)+
  673. (RawRecord.RawData[NextOfs+2] shl 16)+
  674. (RawRecord.RawData[NextOfs+3] shl 24);
  675. if Big then
  676. if SegmentLength=0 then
  677. SegmentLength:=4294967296
  678. else
  679. internalerror(2015040306);
  680. Inc(NextOfs,4);
  681. end
  682. else
  683. begin
  684. SegmentLength:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  685. if Big then
  686. if SegmentLength=0 then
  687. SegmentLength:=65536
  688. else
  689. internalerror(2015040306);
  690. Inc(NextOfs,2);
  691. end;
  692. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FSegmentNameIndex);
  693. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FClassNameIndex);
  694. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FOverlayNameIndex);
  695. end;
  696. procedure TOmfRecord_SEGDEF.EncodeTo(RawRecord: TOmfRawRecord);
  697. var
  698. Big: Boolean;
  699. NextOfs: Integer;
  700. begin
  701. if Is32Bit then
  702. begin
  703. RawRecord.RecordType:=RT_SEGDEF32;
  704. if SegmentLength>4294967296 then
  705. internalerror(2015040302);
  706. Big:=SegmentLength=4294967296;
  707. end
  708. else
  709. begin
  710. RawRecord.RecordType:=RT_SEGDEF;
  711. if SegmentLength>65536 then
  712. internalerror(2015040302);
  713. Big:=SegmentLength=65536;
  714. end;
  715. RawRecord.RawData[0]:=(Ord(Alignment) shl 5) or (Ord(Combination) shl 2) or (Ord(Big) shl 1) or Ord(Use);
  716. NextOfs:=1;
  717. if Alignment=saAbsolute then
  718. begin
  719. RawRecord.RawData[1]:=Byte(FrameNumber);
  720. RawRecord.RawData[2]:=Byte(FrameNumber shr 8);
  721. RawRecord.RawData[3]:=Offset;
  722. NextOfs:=4;
  723. end;
  724. if Is32Bit then
  725. begin
  726. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  727. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  728. RawRecord.RawData[NextOfs+2]:=Byte(SegmentLength shr 16);
  729. RawRecord.RawData[NextOfs+3]:=Byte(SegmentLength shr 24);
  730. Inc(NextOfs,4);
  731. end
  732. else
  733. begin
  734. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  735. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  736. Inc(NextOfs,2);
  737. end;
  738. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,SegmentNameIndex);
  739. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ClassNameIndex);
  740. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,OverlayNameIndex);
  741. RawRecord.RecordLength:=NextOfs+1;
  742. RawRecord.CalculateChecksumByte;
  743. end;
  744. { TOmfRecord_GRPDEF }
  745. procedure TOmfRecord_GRPDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  746. var
  747. NextOfs: Integer;
  748. Segment: Integer;
  749. begin
  750. if RawRecord.RecordType<>RT_GRPDEF then
  751. internalerror(2015040301);
  752. NextOfs:=RawRecord.ReadIndexedRef(0,FGroupNameIndex);
  753. SetLength(FSegmentList,0);
  754. while NextOfs<RawRecord.RecordLength-1 do
  755. begin
  756. if RawRecord.RawData[NextOfs]<>$ff then
  757. internalerror(2015040901);
  758. NextOfs:=RawRecord.ReadIndexedRef(NextOfs+1,Segment);
  759. SetLength(FSegmentList,Length(FSegmentList)+1);
  760. FSegmentList[High(FSegmentList)]:=Segment;
  761. end;
  762. end;
  763. procedure TOmfRecord_GRPDEF.EncodeTo(RawRecord: TOmfRawRecord);
  764. var
  765. NextOfs: Integer;
  766. Segment: Integer;
  767. begin
  768. RawRecord.RecordType:=RT_GRPDEF;
  769. NextOfs:=RawRecord.WriteIndexedRef(0,GroupNameIndex);
  770. for Segment in SegmentList do
  771. begin
  772. if NextOfs>High(RawRecord.RawData) then
  773. internalerror(2015040401);
  774. RawRecord.RawData[NextOfs]:=$ff;
  775. NextOfs:=RawRecord.WriteIndexedRef(NextOfs+1,Segment);
  776. end;
  777. RawRecord.RecordLength:=NextOfs+1;
  778. RawRecord.CalculateChecksumByte;
  779. end;
  780. { TOmfPublicNameElement }
  781. function TOmfPublicNameElement.GetLengthInFile(Is32Bit: Boolean): Integer;
  782. begin
  783. Result:=1+Length(Name)+2+1;
  784. if Is32Bit then
  785. Inc(Result,2);
  786. if TypeIndex>=$80 then
  787. Inc(Result);
  788. end;
  789. { TOmfRecord_PUBDEF }
  790. procedure TOmfRecord_PUBDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  791. begin
  792. {TODO: implement}
  793. internalerror(2015040101);
  794. end;
  795. procedure TOmfRecord_PUBDEF.EncodeTo(RawRecord: TOmfRawRecord);
  796. const
  797. RecordLengthLimit = 1024;
  798. var
  799. Len,LastIncludedIndex,NextOfs,I: Integer;
  800. PubName: TOmfPublicNameElement;
  801. begin
  802. if Is32Bit then
  803. RawRecord.RecordType:=RT_PUBDEF32
  804. else
  805. RawRecord.RecordType:=RT_PUBDEF;
  806. NextOfs:=RawRecord.WriteIndexedRef(0,BaseGroupIndex);
  807. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,BaseSegmentIndex);
  808. if BaseSegmentIndex=0 then
  809. begin
  810. RawRecord.RawData[NextOfs]:=Byte(BaseFrame);
  811. RawRecord.RawData[NextOfs+1]:=Byte(BaseFrame shr 8);
  812. Inc(NextOfs,2);
  813. end;
  814. { find out how many public names can we include until we reach the length limit }
  815. Len:=NextOfs;
  816. LastIncludedIndex:=NextIndex-1;
  817. repeat
  818. Inc(LastIncludedIndex);
  819. Inc(Len,TOmfPublicNameElement(PublicNames[LastIncludedIndex]).GetLengthInFile(Is32Bit));
  820. until (LastIncludedIndex>=(PublicNames.Count-1)) or ((Len+TOmfPublicNameElement(PublicNames[LastIncludedIndex+1]).GetLengthInFile(Is32Bit))>=RecordLengthLimit);
  821. { write the public names... }
  822. for I:=NextIndex to LastIncludedIndex do
  823. begin
  824. PubName:=TOmfPublicNameElement(PublicNames[I]);
  825. NextOfs:=RawRecord.WriteStringAt(NextOfs,PubName.Name);
  826. if Is32Bit then
  827. begin
  828. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  829. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  830. RawRecord.RawData[NextOfs+2]:=Byte(PubName.PublicOffset shr 16);
  831. RawRecord.RawData[NextOfs+3]:=Byte(PubName.PublicOffset shr 24);
  832. Inc(NextOfs,4);
  833. end
  834. else
  835. begin
  836. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  837. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  838. Inc(NextOfs,2);
  839. end;
  840. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,PubName.TypeIndex);
  841. end;
  842. RawRecord.RecordLength:=Len+1;
  843. RawRecord.CalculateChecksumByte;
  844. { update NextIndex }
  845. NextIndex:=LastIncludedIndex+1;
  846. end;
  847. { TOmfExternalNameElement }
  848. function TOmfExternalNameElement.GetLengthInFile: Integer;
  849. begin
  850. Result:=1+Length(Name)+1;
  851. if TypeIndex>=$80 then
  852. Inc(Result);
  853. end;
  854. { TOmfRecord_EXTDEF }
  855. procedure TOmfRecord_EXTDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  856. var
  857. NextOfs: Integer;
  858. Name: string;
  859. TypeIndex: Integer;
  860. ExtName: TOmfExternalNameElement;
  861. begin
  862. if RawRecord.RecordType<>RT_EXTDEF then
  863. internalerror(2015040301);
  864. NextOfs:=0;
  865. while NextOfs<(RawRecord.RecordLength-1) do
  866. begin
  867. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  868. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,TypeIndex);
  869. ExtName:=TOmfExternalNameElement.Create(ExternalNames,Name);
  870. ExtName.TypeIndex:=TypeIndex;
  871. end;
  872. end;
  873. procedure TOmfRecord_EXTDEF.EncodeTo(RawRecord: TOmfRawRecord);
  874. const
  875. RecordLengthLimit = 1024;
  876. var
  877. Len,LastIncludedIndex,NextOfs,I: Integer;
  878. ExtName: TOmfExternalNameElement;
  879. begin
  880. RawRecord.RecordType:=RT_EXTDEF;
  881. NextOfs:=0;
  882. { find out how many external names can we include until we reach the length limit }
  883. Len:=NextOfs;
  884. LastIncludedIndex:=NextIndex-1;
  885. repeat
  886. Inc(LastIncludedIndex);
  887. Inc(Len,TOmfExternalNameElement(ExternalNames[LastIncludedIndex]).GetLengthInFile);
  888. until (LastIncludedIndex>=(ExternalNames.Count-1)) or ((Len+TOmfExternalNameElement(ExternalNames[LastIncludedIndex+1]).GetLengthInFile)>=RecordLengthLimit);
  889. { write the external names... }
  890. for I:=NextIndex to LastIncludedIndex do
  891. begin
  892. ExtName:=TOmfExternalNameElement(ExternalNames[I]);
  893. NextOfs:=RawRecord.WriteStringAt(NextOfs,ExtName.Name);
  894. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ExtName.TypeIndex);
  895. end;
  896. RawRecord.RecordLength:=Len+1;
  897. RawRecord.CalculateChecksumByte;
  898. { update NextIndex }
  899. NextIndex:=LastIncludedIndex+1;
  900. end;
  901. { TOmfRecord_MODEND }
  902. procedure TOmfRecord_MODEND.DecodeFrom(RawRecord: TOmfRawRecord);
  903. begin
  904. {TODO: implement}
  905. internalerror(2015040101);
  906. end;
  907. procedure TOmfRecord_MODEND.EncodeTo(RawRecord: TOmfRawRecord);
  908. var
  909. ModTyp: Byte;
  910. NextOfs: Integer;
  911. begin
  912. if Is32Bit then
  913. RawRecord.RecordType:=RT_MODEND32
  914. else
  915. RawRecord.RecordType:=RT_MODEND;
  916. ModTyp:=(Ord(IsMainModule) shl 7)+(Ord(HasStartAddress) shl 6)+(Ord(SegmentBit) shl 5)+Ord(LogicalStartAddress);
  917. RawRecord.RawData[0]:=ModTyp;
  918. NextOfs:=1;
  919. if HasStartAddress then
  920. begin
  921. {TODO: implement writing a start address}
  922. internalerror(2015040101);
  923. end;
  924. RawRecord.RecordLength:=NextOfs+1;
  925. RawRecord.CalculateChecksumByte;
  926. end;
  927. { TOmfSubRecord_FIXUP }
  928. function TOmfSubRecord_FIXUP.GetDataRecordOffset: Integer;
  929. begin
  930. Result:=FLocationOffset-FDataRecordStartOffset;
  931. end;
  932. procedure TOmfSubRecord_FIXUP.SetDataRecordOffset(AValue: Integer);
  933. begin
  934. FLocationOffset:=AValue+FDataRecordStartOffset;
  935. end;
  936. function TOmfSubRecord_FIXUP.ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  937. var
  938. Locat: Word;
  939. FixData: Byte;
  940. begin
  941. if (Offset+2)>High(RawRecord.RawData) then
  942. internalerror(2015040504);
  943. { unlike other fields in the OMF format, this one is big endian }
  944. Locat:=(RawRecord.RawData[Offset] shl 8) or RawRecord.RawData[Offset+1];
  945. FixData:=RawRecord.RawData[Offset+2];
  946. Inc(Offset,3);
  947. if (Locat and $8000)=0 then
  948. internalerror(2015040503);
  949. DataRecordOffset:=Locat and $3FF;
  950. LocationType:=TOmfFixupLocationType((Locat shr 10) and 15);
  951. Mode:=TOmfFixupMode((Locat shr 14) and 1);
  952. FrameDeterminedByThread:=(FixData and $80)<>0;
  953. TargetDeterminedByThread:=(FixData and $08)<>0;
  954. if FrameDeterminedByThread then
  955. FrameThread:=TOmfFixupThread((FixData shr 4) and 3)
  956. else
  957. FrameMethod:=TOmfFixupFrameMethod((FixData shr 4) and 7);
  958. if TargetDeterminedByThread then
  959. begin
  960. TargetThread:=TOmfFixupThread(FixData and 3);
  961. TargetThreadDisplacementPresent:=(FixData and $40)=0;
  962. end
  963. else
  964. TargetMethod:=TOmfFixupTargetMethod(FixData and 7);
  965. { read Frame Datum? }
  966. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  967. Offset:=RawRecord.ReadIndexedRef(Offset,FFrameDatum)
  968. else
  969. FrameDatum:=0;
  970. { read Target Datum? }
  971. if not TargetDeterminedByThread then
  972. Offset:=RawRecord.ReadIndexedRef(Offset,FTargetDatum)
  973. else
  974. TargetDatum:=0;
  975. { read Target Displacement? }
  976. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  977. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  978. begin
  979. if Is32Bit then
  980. begin
  981. if (Offset+3)>High(RawRecord.RawData) then
  982. internalerror(2015040504);
  983. TargetDisplacement := RawRecord.RawData[Offset]+
  984. (RawRecord.RawData[Offset+1] shl 8)+
  985. (RawRecord.RawData[Offset+2] shl 16)+
  986. (RawRecord.RawData[Offset+3] shl 24);
  987. Inc(Offset,4);
  988. end
  989. else
  990. begin
  991. if (Offset+1)>High(RawRecord.RawData) then
  992. internalerror(2015040504);
  993. TargetDisplacement := RawRecord.RawData[Offset]+
  994. (RawRecord.RawData[Offset+1] shl 8);
  995. Inc(Offset,2);
  996. end;
  997. end;
  998. Result:=Offset;
  999. end;
  1000. function TOmfSubRecord_FIXUP.WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  1001. var
  1002. Locat: Word;
  1003. FixData: Byte;
  1004. begin
  1005. if (DataRecordOffset<0) or (DataRecordOffset>1023) then
  1006. internalerror(2015040501);
  1007. Locat:=$8000+(Ord(Mode) shl 14)+(Ord(LocationType) shl 10)+DataRecordOffset;
  1008. { unlike other fields in the OMF format, this one is big endian }
  1009. RawRecord.RawData[Offset]:=Byte(Locat shr 8);
  1010. RawRecord.RawData[Offset+1]:=Byte(Locat);
  1011. Inc(Offset, 2);
  1012. FixData:=(Ord(FrameDeterminedByThread) shl 7)+(Ord(TargetDeterminedByThread) shl 3);
  1013. if FrameDeterminedByThread then
  1014. FixData:=FixData+(Ord(FrameThread) shl 4)
  1015. else
  1016. FixData:=FixData+(Ord(FrameMethod) shl 4);
  1017. if TargetDeterminedByThread then
  1018. FixData:=FixData+Ord(TargetThread)+(Ord(not TargetThreadDisplacementPresent) shl 2)
  1019. else
  1020. FixData:=FixData+Ord(TargetMethod);
  1021. RawRecord.RawData[Offset]:=FixData;
  1022. Inc(Offset);
  1023. { save Frame Datum? }
  1024. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  1025. Offset:=RawRecord.WriteIndexedRef(Offset,FrameDatum);
  1026. { save Target Datum? }
  1027. if not TargetDeterminedByThread then
  1028. Offset:=RawRecord.WriteIndexedRef(Offset,TargetDatum);
  1029. { save Target Displacement? }
  1030. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  1031. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  1032. begin
  1033. if Is32Bit then
  1034. begin
  1035. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1036. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1037. RawRecord.RawData[Offset+2]:=Byte(TargetDisplacement shr 16);
  1038. RawRecord.RawData[Offset+3]:=Byte(TargetDisplacement shr 24);
  1039. Inc(Offset,4);
  1040. end
  1041. else
  1042. begin
  1043. if TargetDisplacement>$ffff then
  1044. internalerror(2015040502);
  1045. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1046. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1047. Inc(Offset,2);
  1048. end;
  1049. end;
  1050. Result:=Offset;
  1051. end;
  1052. end.