omfbase.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. RawRecord.ReadStringAt(0,FModuleName);
  533. end;
  534. procedure TOmfRecord_THEADR.EncodeTo(RawRecord: TOmfRawRecord);
  535. var
  536. NextOfs: Integer;
  537. begin
  538. RawRecord.RecordType:=RT_THEADR;
  539. NextOfs:=RawRecord.WriteStringAt(0,ModuleName);
  540. RawRecord.RecordLength:=NextOfs+1;
  541. RawRecord.CalculateChecksumByte;
  542. end;
  543. { TOmfRecord_COMENT }
  544. function TOmfRecord_COMENT.GetNoList: Boolean;
  545. begin
  546. Result:=(CommentType and $40)<>0;
  547. end;
  548. function TOmfRecord_COMENT.GetNoPurge: Boolean;
  549. begin
  550. Result:=(CommentType and $80)<>0;
  551. end;
  552. procedure TOmfRecord_COMENT.SetNoList(AValue: Boolean);
  553. begin
  554. if AValue then
  555. CommentType:=CommentType or $40
  556. else
  557. CommentType:=CommentType and $BF;
  558. end;
  559. procedure TOmfRecord_COMENT.SetNoPurge(AValue: Boolean);
  560. begin
  561. if AValue then
  562. CommentType:=CommentType or $80
  563. else
  564. CommentType:=CommentType and $7F;
  565. end;
  566. procedure TOmfRecord_COMENT.DecodeFrom(RawRecord: TOmfRawRecord);
  567. begin
  568. if RawRecord.RecordType<>RT_COMENT then
  569. internalerror(2015040301);
  570. if RawRecord.RecordLength<3 then
  571. internalerror(2015033104);
  572. CommentType:=RawRecord.RawData[0];
  573. CommentClass:=RawRecord.RawData[1];
  574. SetLength(FCommentString,RawRecord.RecordLength-3);
  575. UniqueString(FCommentString);
  576. Move(RawRecord.RawData[2],FCommentString[1],Length(FCommentString));
  577. end;
  578. procedure TOmfRecord_COMENT.EncodeTo(RawRecord: TOmfRawRecord);
  579. begin
  580. RawRecord.RecordType:=RT_COMENT;
  581. if (Length(FCommentString)+3)>High(RawRecord.RawData) then
  582. internalerror(2015033105);
  583. RawRecord.RecordLength:=Length(FCommentString)+3;
  584. RawRecord.RawData[0]:=CommentType;
  585. RawRecord.RawData[1]:=CommentClass;
  586. Move(FCommentString[1],RawRecord.RawData[2],Length(FCommentString));
  587. RawRecord.CalculateChecksumByte;
  588. end;
  589. { TOmfRecord_LNAMES }
  590. constructor TOmfRecord_LNAMES.Create;
  591. begin
  592. FNextIndex:=1;
  593. end;
  594. procedure TOmfRecord_LNAMES.DecodeFrom(RawRecord: TOmfRawRecord);
  595. begin
  596. {TODO: implement}
  597. internalerror(2015040101);
  598. end;
  599. procedure TOmfRecord_LNAMES.EncodeTo(RawRecord: TOmfRawRecord);
  600. const
  601. RecordLengthLimit = 1024;
  602. var
  603. Len,LastIncludedIndex,NextOfs,I: Integer;
  604. begin
  605. RawRecord.RecordType:=RT_LNAMES;
  606. { find out how many strings can we include until we reach the length limit }
  607. Len:=1;
  608. LastIncludedIndex:=NextIndex-1;
  609. repeat
  610. Inc(LastIncludedIndex);
  611. Inc(Len,Length(Names[LastIncludedIndex])+1);
  612. until (LastIncludedIndex>=Names.Count) or ((Len+Length(Names[LastIncludedIndex+1])+1)>=RecordLengthLimit);
  613. { write the strings... }
  614. NextOfs:=0;
  615. for I:=NextIndex to LastIncludedIndex do
  616. NextOfs:=RawRecord.WriteStringAt(NextOfs,Names[I]);
  617. RawRecord.RecordLength:=Len;
  618. RawRecord.CalculateChecksumByte;
  619. { update NextIndex }
  620. NextIndex:=LastIncludedIndex+1;
  621. end;
  622. { TOmfRecord_SEGDEF }
  623. procedure TOmfRecord_SEGDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  624. var
  625. B: Byte;
  626. Big: Boolean;
  627. NextOfs: Integer;
  628. MinLen: Integer;
  629. begin
  630. if not (RawRecord.RecordType in [RT_SEGDEF,RT_SEGDEF32]) then
  631. internalerror(2015040301);
  632. Is32Bit:=RawRecord.RecordType=RT_SEGDEF32;
  633. MinLen:=7; { b(1)+seglength(2..4)+segnameindex(1..2)+classnameindex(1..2)+overlaynameindex(1..2)+checksum }
  634. if Is32Bit then
  635. inc(MinLen,2);
  636. if RawRecord.RecordLength<MinLen then
  637. internalerror(2015040305);
  638. B:=RawRecord.RawData[0];
  639. Alignment:=TOmfSegmentAlignment(B shr 5);
  640. Combination:=TOmfSegmentCombination((B shr 2) and 7);
  641. Big:=(B and 2)<>0;
  642. Use:=TOmfSegmentUse(B and 1);
  643. NextOfs:=1;
  644. if Alignment=saAbsolute then
  645. begin
  646. inc(MinLen,3);
  647. if RawRecord.RecordLength<MinLen then
  648. internalerror(2015040305);
  649. FrameNumber:=RawRecord.RawData[1]+(RawRecord.RawData[2] shl 8);
  650. Offset:=RawRecord.RawData[3];
  651. NextOfs:=4;
  652. end
  653. else
  654. begin
  655. FrameNumber:=0;
  656. Offset:=0;
  657. end;
  658. if Is32Bit then
  659. begin
  660. SegmentLength:=RawRecord.RawData[NextOfs]+
  661. (RawRecord.RawData[NextOfs+1] shl 8)+
  662. (RawRecord.RawData[NextOfs+2] shl 16)+
  663. (RawRecord.RawData[NextOfs+3] shl 24);
  664. if Big then
  665. if SegmentLength=0 then
  666. SegmentLength:=4294967296
  667. else
  668. internalerror(2015040306);
  669. Inc(NextOfs,4);
  670. end
  671. else
  672. begin
  673. SegmentLength:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  674. if Big then
  675. if SegmentLength=0 then
  676. SegmentLength:=65536
  677. else
  678. internalerror(2015040306);
  679. Inc(NextOfs,2);
  680. end;
  681. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FSegmentNameIndex);
  682. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FClassNameIndex);
  683. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FOverlayNameIndex);
  684. end;
  685. procedure TOmfRecord_SEGDEF.EncodeTo(RawRecord: TOmfRawRecord);
  686. var
  687. Big: Boolean;
  688. NextOfs: Integer;
  689. begin
  690. if Is32Bit then
  691. begin
  692. RawRecord.RecordType:=RT_SEGDEF32;
  693. if SegmentLength>4294967296 then
  694. internalerror(2015040302);
  695. Big:=SegmentLength=4294967296;
  696. end
  697. else
  698. begin
  699. RawRecord.RecordType:=RT_SEGDEF;
  700. if SegmentLength>65536 then
  701. internalerror(2015040302);
  702. Big:=SegmentLength=65536;
  703. end;
  704. RawRecord.RawData[0]:=(Ord(Alignment) shl 5) or (Ord(Combination) shl 2) or (Ord(Big) shl 1) or Ord(Use);
  705. NextOfs:=1;
  706. if Alignment=saAbsolute then
  707. begin
  708. RawRecord.RawData[1]:=Byte(FrameNumber);
  709. RawRecord.RawData[2]:=Byte(FrameNumber shr 8);
  710. RawRecord.RawData[3]:=Offset;
  711. NextOfs:=4;
  712. end;
  713. if Is32Bit then
  714. begin
  715. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  716. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  717. RawRecord.RawData[NextOfs+2]:=Byte(SegmentLength shr 16);
  718. RawRecord.RawData[NextOfs+3]:=Byte(SegmentLength shr 24);
  719. Inc(NextOfs,4);
  720. end
  721. else
  722. begin
  723. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  724. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  725. Inc(NextOfs,2);
  726. end;
  727. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,SegmentNameIndex);
  728. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ClassNameIndex);
  729. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,OverlayNameIndex);
  730. RawRecord.RecordLength:=NextOfs+1;
  731. RawRecord.CalculateChecksumByte;
  732. end;
  733. { TOmfRecord_GRPDEF }
  734. procedure TOmfRecord_GRPDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  735. var
  736. NextOfs: Integer;
  737. Segment: Integer;
  738. begin
  739. if RawRecord.RecordType<>RT_GRPDEF then
  740. internalerror(2015040301);
  741. NextOfs:=RawRecord.ReadIndexedRef(0,FGroupNameIndex);
  742. SetLength(FSegmentList,0);
  743. while NextOfs<RawRecord.RecordLength-1 do
  744. begin
  745. if RawRecord.RawData[NextOfs]<>$ff then
  746. internalerror(2015040901);
  747. NextOfs:=RawRecord.ReadIndexedRef(NextOfs+1,Segment);
  748. SetLength(FSegmentList,Length(FSegmentList)+1);
  749. FSegmentList[High(FSegmentList)]:=Segment;
  750. end;
  751. end;
  752. procedure TOmfRecord_GRPDEF.EncodeTo(RawRecord: TOmfRawRecord);
  753. var
  754. NextOfs: Integer;
  755. Segment: Integer;
  756. begin
  757. RawRecord.RecordType:=RT_GRPDEF;
  758. NextOfs:=RawRecord.WriteIndexedRef(0,GroupNameIndex);
  759. for Segment in SegmentList do
  760. begin
  761. if NextOfs>High(RawRecord.RawData) then
  762. internalerror(2015040401);
  763. RawRecord.RawData[NextOfs]:=$ff;
  764. NextOfs:=RawRecord.WriteIndexedRef(NextOfs+1,Segment);
  765. end;
  766. RawRecord.RecordLength:=NextOfs+1;
  767. RawRecord.CalculateChecksumByte;
  768. end;
  769. { TOmfPublicNameElement }
  770. function TOmfPublicNameElement.GetLengthInFile(Is32Bit: Boolean): Integer;
  771. begin
  772. Result:=1+Length(Name)+2+1;
  773. if Is32Bit then
  774. Inc(Result,2);
  775. if TypeIndex>=$80 then
  776. Inc(Result);
  777. end;
  778. { TOmfRecord_PUBDEF }
  779. procedure TOmfRecord_PUBDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  780. begin
  781. {TODO: implement}
  782. internalerror(2015040101);
  783. end;
  784. procedure TOmfRecord_PUBDEF.EncodeTo(RawRecord: TOmfRawRecord);
  785. const
  786. RecordLengthLimit = 1024;
  787. var
  788. Len,LastIncludedIndex,NextOfs,I: Integer;
  789. PubName: TOmfPublicNameElement;
  790. begin
  791. if Is32Bit then
  792. RawRecord.RecordType:=RT_PUBDEF32
  793. else
  794. RawRecord.RecordType:=RT_PUBDEF;
  795. NextOfs:=RawRecord.WriteIndexedRef(0,BaseGroupIndex);
  796. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,BaseSegmentIndex);
  797. if BaseSegmentIndex=0 then
  798. begin
  799. RawRecord.RawData[NextOfs]:=Byte(BaseFrame);
  800. RawRecord.RawData[NextOfs+1]:=Byte(BaseFrame shr 8);
  801. Inc(NextOfs,2);
  802. end;
  803. { find out how many public names can we include until we reach the length limit }
  804. Len:=NextOfs;
  805. LastIncludedIndex:=NextIndex-1;
  806. repeat
  807. Inc(LastIncludedIndex);
  808. Inc(Len,TOmfPublicNameElement(PublicNames[LastIncludedIndex]).GetLengthInFile(Is32Bit));
  809. until (LastIncludedIndex>=(PublicNames.Count-1)) or ((Len+TOmfPublicNameElement(PublicNames[LastIncludedIndex+1]).GetLengthInFile(Is32Bit))>=RecordLengthLimit);
  810. { write the public names... }
  811. for I:=NextIndex to LastIncludedIndex do
  812. begin
  813. PubName:=TOmfPublicNameElement(PublicNames[I]);
  814. NextOfs:=RawRecord.WriteStringAt(NextOfs,PubName.Name);
  815. if Is32Bit then
  816. begin
  817. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  818. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  819. RawRecord.RawData[NextOfs+2]:=Byte(PubName.PublicOffset shr 16);
  820. RawRecord.RawData[NextOfs+3]:=Byte(PubName.PublicOffset shr 24);
  821. Inc(NextOfs,4);
  822. end
  823. else
  824. begin
  825. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  826. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  827. Inc(NextOfs,2);
  828. end;
  829. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,PubName.TypeIndex);
  830. end;
  831. RawRecord.RecordLength:=Len+1;
  832. RawRecord.CalculateChecksumByte;
  833. { update NextIndex }
  834. NextIndex:=LastIncludedIndex+1;
  835. end;
  836. { TOmfExternalNameElement }
  837. function TOmfExternalNameElement.GetLengthInFile: Integer;
  838. begin
  839. Result:=1+Length(Name)+1;
  840. if TypeIndex>=$80 then
  841. Inc(Result);
  842. end;
  843. { TOmfRecord_EXTDEF }
  844. procedure TOmfRecord_EXTDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  845. begin
  846. {TODO: implement}
  847. internalerror(2015040101);
  848. end;
  849. procedure TOmfRecord_EXTDEF.EncodeTo(RawRecord: TOmfRawRecord);
  850. const
  851. RecordLengthLimit = 1024;
  852. var
  853. Len,LastIncludedIndex,NextOfs,I: Integer;
  854. ExtName: TOmfExternalNameElement;
  855. begin
  856. RawRecord.RecordType:=RT_EXTDEF;
  857. NextOfs:=0;
  858. { find out how many external names can we include until we reach the length limit }
  859. Len:=NextOfs;
  860. LastIncludedIndex:=NextIndex-1;
  861. repeat
  862. Inc(LastIncludedIndex);
  863. Inc(Len,TOmfExternalNameElement(ExternalNames[LastIncludedIndex]).GetLengthInFile);
  864. until (LastIncludedIndex>=(ExternalNames.Count-1)) or ((Len+TOmfExternalNameElement(ExternalNames[LastIncludedIndex+1]).GetLengthInFile)>=RecordLengthLimit);
  865. { write the external names... }
  866. for I:=NextIndex to LastIncludedIndex do
  867. begin
  868. ExtName:=TOmfExternalNameElement(ExternalNames[I]);
  869. NextOfs:=RawRecord.WriteStringAt(NextOfs,ExtName.Name);
  870. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ExtName.TypeIndex);
  871. end;
  872. RawRecord.RecordLength:=Len+1;
  873. RawRecord.CalculateChecksumByte;
  874. { update NextIndex }
  875. NextIndex:=LastIncludedIndex+1;
  876. end;
  877. { TOmfRecord_MODEND }
  878. procedure TOmfRecord_MODEND.DecodeFrom(RawRecord: TOmfRawRecord);
  879. begin
  880. {TODO: implement}
  881. internalerror(2015040101);
  882. end;
  883. procedure TOmfRecord_MODEND.EncodeTo(RawRecord: TOmfRawRecord);
  884. var
  885. ModTyp: Byte;
  886. NextOfs: Integer;
  887. begin
  888. if Is32Bit then
  889. RawRecord.RecordType:=RT_MODEND32
  890. else
  891. RawRecord.RecordType:=RT_MODEND;
  892. ModTyp:=(Ord(IsMainModule) shl 7)+(Ord(HasStartAddress) shl 6)+(Ord(SegmentBit) shl 5)+Ord(LogicalStartAddress);
  893. RawRecord.RawData[0]:=ModTyp;
  894. NextOfs:=1;
  895. if HasStartAddress then
  896. begin
  897. {TODO: implement writing a start address}
  898. internalerror(2015040101);
  899. end;
  900. RawRecord.RecordLength:=NextOfs+1;
  901. RawRecord.CalculateChecksumByte;
  902. end;
  903. { TOmfSubRecord_FIXUP }
  904. function TOmfSubRecord_FIXUP.GetDataRecordOffset: Integer;
  905. begin
  906. Result:=FLocationOffset-FDataRecordStartOffset;
  907. end;
  908. procedure TOmfSubRecord_FIXUP.SetDataRecordOffset(AValue: Integer);
  909. begin
  910. FLocationOffset:=AValue+FDataRecordStartOffset;
  911. end;
  912. function TOmfSubRecord_FIXUP.ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  913. var
  914. Locat: Word;
  915. FixData: Byte;
  916. begin
  917. if (Offset+2)>High(RawRecord.RawData) then
  918. internalerror(2015040504);
  919. { unlike other fields in the OMF format, this one is big endian }
  920. Locat:=(RawRecord.RawData[Offset] shl 8) or RawRecord.RawData[Offset+1];
  921. FixData:=RawRecord.RawData[Offset+2];
  922. Inc(Offset,3);
  923. if (Locat and $8000)=0 then
  924. internalerror(2015040503);
  925. DataRecordOffset:=Locat and $3FF;
  926. LocationType:=TOmfFixupLocationType((Locat shr 10) and 15);
  927. Mode:=TOmfFixupMode((Locat shr 14) and 1);
  928. FrameDeterminedByThread:=(FixData and $80)<>0;
  929. TargetDeterminedByThread:=(FixData and $08)<>0;
  930. if FrameDeterminedByThread then
  931. FrameThread:=TOmfFixupThread((FixData shr 4) and 3)
  932. else
  933. FrameMethod:=TOmfFixupFrameMethod((FixData shr 4) and 7);
  934. if TargetDeterminedByThread then
  935. begin
  936. TargetThread:=TOmfFixupThread(FixData and 3);
  937. TargetThreadDisplacementPresent:=(FixData and $40)=0;
  938. end
  939. else
  940. TargetMethod:=TOmfFixupTargetMethod(FixData and 7);
  941. { read Frame Datum? }
  942. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  943. Offset:=RawRecord.ReadIndexedRef(Offset,FFrameDatum)
  944. else
  945. FrameDatum:=0;
  946. { read Target Datum? }
  947. if not TargetDeterminedByThread then
  948. Offset:=RawRecord.ReadIndexedRef(Offset,FTargetDatum)
  949. else
  950. TargetDatum:=0;
  951. { read Target Displacement? }
  952. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  953. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  954. begin
  955. if Is32Bit then
  956. begin
  957. if (Offset+3)>High(RawRecord.RawData) then
  958. internalerror(2015040504);
  959. TargetDisplacement := RawRecord.RawData[Offset]+
  960. (RawRecord.RawData[Offset+1] shl 8)+
  961. (RawRecord.RawData[Offset+2] shl 16)+
  962. (RawRecord.RawData[Offset+3] shl 24);
  963. Inc(Offset,4);
  964. end
  965. else
  966. begin
  967. if (Offset+1)>High(RawRecord.RawData) then
  968. internalerror(2015040504);
  969. TargetDisplacement := RawRecord.RawData[Offset]+
  970. (RawRecord.RawData[Offset+1] shl 8);
  971. Inc(Offset,2);
  972. end;
  973. end;
  974. Result:=Offset;
  975. end;
  976. function TOmfSubRecord_FIXUP.WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  977. var
  978. Locat: Word;
  979. FixData: Byte;
  980. begin
  981. if (DataRecordOffset<0) or (DataRecordOffset>1023) then
  982. internalerror(2015040501);
  983. Locat:=$8000+(Ord(Mode) shl 14)+(Ord(LocationType) shl 10)+DataRecordOffset;
  984. { unlike other fields in the OMF format, this one is big endian }
  985. RawRecord.RawData[Offset]:=Byte(Locat shr 8);
  986. RawRecord.RawData[Offset+1]:=Byte(Locat);
  987. Inc(Offset, 2);
  988. FixData:=(Ord(FrameDeterminedByThread) shl 7)+(Ord(TargetDeterminedByThread) shl 3);
  989. if FrameDeterminedByThread then
  990. FixData:=FixData+(Ord(FrameThread) shl 4)
  991. else
  992. FixData:=FixData+(Ord(FrameMethod) shl 4);
  993. if TargetDeterminedByThread then
  994. FixData:=FixData+Ord(TargetThread)+(Ord(not TargetThreadDisplacementPresent) shl 2)
  995. else
  996. FixData:=FixData+Ord(TargetMethod);
  997. RawRecord.RawData[Offset]:=FixData;
  998. Inc(Offset);
  999. { save Frame Datum? }
  1000. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  1001. Offset:=RawRecord.WriteIndexedRef(Offset,FrameDatum);
  1002. { save Target Datum? }
  1003. if not TargetDeterminedByThread then
  1004. Offset:=RawRecord.WriteIndexedRef(Offset,TargetDatum);
  1005. { save Target Displacement? }
  1006. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  1007. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  1008. begin
  1009. if Is32Bit then
  1010. begin
  1011. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1012. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1013. RawRecord.RawData[Offset+2]:=Byte(TargetDisplacement shr 16);
  1014. RawRecord.RawData[Offset+3]:=Byte(TargetDisplacement shr 24);
  1015. Inc(Offset,4);
  1016. end
  1017. else
  1018. begin
  1019. if TargetDisplacement>$ffff then
  1020. internalerror(2015040502);
  1021. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1022. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1023. Inc(Offset,2);
  1024. end;
  1025. end;
  1026. Result:=Offset;
  1027. end;
  1028. end.