omfbase.pas 40 KB

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