omfbase.pas 37 KB

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