omfbase.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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. aasmbase,
  25. owbase;
  26. const
  27. { OMF segment names for the object sections, produced by the FPC code generator }
  28. omf_secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  29. '_TEXT',
  30. 'data',
  31. 'data',
  32. 'rodata',
  33. 'bss',
  34. 'tbss',
  35. 'pdata',
  36. '_TEXT','data','data','data','data',
  37. 'stab',
  38. 'stabstr',
  39. 'idata2','idata4','idata5','idata6','idata7','edata',
  40. 'eh_frame',
  41. 'debug_frame','debug_info','debug_line','debug_abbrev',
  42. 'fpc',
  43. '',
  44. 'init',
  45. 'fini',
  46. 'objc_class',
  47. 'objc_meta_class',
  48. 'objc_cat_cls_meth',
  49. 'objc_cat_inst_meth',
  50. 'objc_protocol',
  51. 'objc_string_object',
  52. 'objc_cls_meth',
  53. 'objc_inst_meth',
  54. 'objc_cls_refs',
  55. 'objc_message_refs',
  56. 'objc_symbols',
  57. 'objc_category',
  58. 'objc_class_vars',
  59. 'objc_instance_vars',
  60. 'objc_module_info',
  61. 'objc_class_names',
  62. 'objc_meth_var_types',
  63. 'objc_meth_var_names',
  64. 'objc_selector_strs',
  65. 'objc_protocol_ext',
  66. 'objc_class_ext',
  67. 'objc_property',
  68. 'objc_image_info',
  69. 'objc_cstring_object',
  70. 'objc_sel_fixup',
  71. '__DATA,__objc_data',
  72. '__DATA,__objc_const',
  73. 'objc_superrefs',
  74. '__DATA, __datacoal_nt,coalesced',
  75. 'objc_classlist',
  76. 'objc_nlclasslist',
  77. 'objc_catlist',
  78. 'obcj_nlcatlist',
  79. 'objc_protolist',
  80. 'stack',
  81. 'heap'
  82. );
  83. { OMF segment class names for the object sections, produced by the FPC code generator }
  84. omf_segclass : array[TAsmSectiontype] of string[length('DWARF')] = ('',
  85. {user} 'DATA',
  86. {code} 'CODE',
  87. {data} 'DATA',
  88. {rodata} 'DATA',
  89. {rodata_norel} 'DATA',
  90. {bss} 'BSS',
  91. {threadvar} 'DATA',
  92. {pdata} 'DATA',
  93. {stub} 'CODE',
  94. {data_nonlazy} 'DATA',
  95. {data_lazy} 'DATA',
  96. {init_func} 'DATA',
  97. {term_func} 'DATA',
  98. {stab} 'DATA',
  99. {stabstr} 'DATA',
  100. {idata2} 'DATA',
  101. {idata4} 'DATA',
  102. {idata5} 'DATA',
  103. {idata6} 'DATA',
  104. {idata7} 'DATA',
  105. {edata} 'DATA',
  106. {eh_frame} 'DATA',
  107. {debug_frame} 'DWARF',
  108. {debug_info} 'DWARF',
  109. {debug_line} 'DWARF',
  110. {debug_abbrev} 'DWARF',
  111. {fpc} 'DATA',
  112. {toc} 'DATA',
  113. {init} 'CODE',
  114. {fini} 'CODE',
  115. {objc_class} 'DATA',
  116. {objc_meta_class} 'DATA',
  117. {objc_cat_cls_meth} 'DATA',
  118. {objc_cat_inst_meth} 'DATA',
  119. {objc_protocol} 'DATA',
  120. {objc_string_object} 'DATA',
  121. {objc_cls_meth} 'DATA',
  122. {objc_inst_meth} 'DATA',
  123. {objc_cls_refs} 'DATA',
  124. {objc_message_refs} 'DATA',
  125. {objc_symbols} 'DATA',
  126. {objc_category} 'DATA',
  127. {objc_class_vars} 'DATA',
  128. {objc_instance_vars} 'DATA',
  129. {objc_module_info} 'DATA',
  130. {objc_class_names} 'DATA',
  131. {objc_meth_var_types} 'DATA',
  132. {objc_meth_var_names} 'DATA',
  133. {objc_selector_strs} 'DATA',
  134. {objc_protocol_ext} 'DATA',
  135. {objc_class_ext} 'DATA',
  136. {objc_property} 'DATA',
  137. {objc_image_info} 'DATA',
  138. {objc_cstring_object} 'DATA',
  139. {objc_sel_fixup} 'DATA',
  140. {objc_data} 'DATA',
  141. {objc_const} 'DATA',
  142. {objc_sup_refs} 'DATA',
  143. {data_coalesced} 'DATA',
  144. {objc_classlist} 'DATA',
  145. {objc_nlclasslist} 'DATA',
  146. {objc_catlist} 'DATA',
  147. {objc_nlcatlist} 'DATA',
  148. {objc_protolist} 'DATA',
  149. {stack} 'STACK',
  150. {heap} 'HEAP'
  151. );
  152. { OMF record types }
  153. RT_THEADR = $80; { Translator Header Record }
  154. RT_LHEADR = $82; { Library Module Header Record }
  155. RT_COMENT = $88; { Comment Record }
  156. RT_MODEND = $8A; { Module End Record }
  157. RT_MODEND32 = $8B;
  158. RT_EXTDEF = $8C; { External Names Definition Record }
  159. RT_PUBDEF = $90; { Public Names Definition Record }
  160. RT_PUBDEF32 = $91;
  161. RT_LINNUM = $94; { Line Numbers Record }
  162. RT_LINNUM32 = $95;
  163. RT_LNAMES = $96; { List of Names Record }
  164. RT_SEGDEF = $98; { Segment Definition Record }
  165. RT_SEGDEF32 = $99;
  166. RT_GRPDEF = $9A; { Group Definition Record }
  167. RT_FIXUPP = $9C; { Fixup Record }
  168. RT_FIXUPP32 = $9D;
  169. RT_LEDATA = $A0; { Logical Enumerated Data Record }
  170. RT_LEDATA32 = $A1;
  171. RT_LIDATA = $A2; { Logical Iterated Data Record }
  172. RT_LIDATA32 = $A3;
  173. RT_COMDEF = $B0; { Communal Names Definition Record }
  174. RT_BAKPAT = $B2; { Backpatch Record }
  175. RT_BAKPAT32 = $B3;
  176. RT_LEXTDEF = $B4; { Local External Names Definition Record }
  177. RT_LEXTDEF32 = $B5;
  178. RT_LPUBDEF = $B6; { Local Public Names Definition Record }
  179. RT_LPUBDEF32 = $B7;
  180. RT_LCOMDEF = $B8; { Local Communal Names Definition Record }
  181. RT_CEXTDEF = $BC; { COMDAT External Names Definition Record }
  182. RT_COMDAT = $C2; { Initialized Communal Data Record }
  183. RT_COMDAT32 = $C3;
  184. RT_LINSYM = $C4; { Symbol Line Numbers Record }
  185. RT_LINSYM32 = $C5;
  186. RT_ALIAS = $C6; { Alias Definition Record }
  187. RT_NBKPAT = $C8; { Named Backpatch Record }
  188. RT_NBKPAT32 = $C9;
  189. RT_LLNAMES = $CA; { Local Logical Names Definition Record }
  190. RT_VERNUM = $CC; { OMF Version Number Record }
  191. RT_VENDEXT = $CE; { Vendor-specific OMF Extension Record }
  192. RT_LIBHEAD = $F0; { Library Header Record }
  193. RT_LIBEND = $F1; { Library End Record (marks end of objects and beginning of dictionary) }
  194. { OMF comment class }
  195. CC_Translator = $00; { language translator (compiler or assembler) name }
  196. CC_IntelCopyright = $01;
  197. CC_IntelReservedRangeStart = $02;
  198. CC_IntelReservedRangeEnd = $9B;
  199. CC_LibrarySpecifierObsolete = $81;
  200. CC_MsDosVersionObsolete = $9C;
  201. CC_MemoryModel = $9D;
  202. CC_DOSSEG = $9E;
  203. CC_DefaultLibrarySearchName = $9F;
  204. CC_OmfExtension = $A0;
  205. CC_NewOmfExtension = $A1;
  206. CC_LinkPassSeparator = $A2;
  207. CC_LIBMOD = $A3;
  208. CC_EXESTR = $A4;
  209. CC_INCERR = $A6;
  210. CC_NOPAD = $A7;
  211. CC_WKEXT = $A8;
  212. CC_LZEXT = $A9;
  213. CC_Comment = $DA;
  214. CC_Compiler = $DB;
  215. CC_Date = $DC;
  216. CC_Timestamp = $DD;
  217. CC_User = $DF;
  218. CC_DependencyFileBorland = $E9;
  219. CC_CommandLineMicrosoft = $FF;
  220. type
  221. TOmfSegmentAlignment = (
  222. saAbsolute = 0,
  223. saRelocatableByteAligned = 1,
  224. saRelocatableWordAligned = 2,
  225. saRelocatableParaAligned = 3,
  226. saRelocatablePageAligned = 4, { 32-bit linkers extension }
  227. saRelocatableDWordAligned = 5, { 32-bit linkers extension }
  228. saNotSupported = 6,
  229. saNotDefined = 7);
  230. TOmfSegmentCombination = (
  231. scPrivate = 0,
  232. scReserved1 = 1,
  233. scPublic = 2,
  234. scReserved3 = 3,
  235. scPublic4 = 4, { same as scPublic }
  236. scStack = 5,
  237. scCommon = 6,
  238. scPublic7 = 7); { same as scPublic }
  239. TOmfSegmentUse = (suUse16, suUse32);
  240. TOmfFixupThread = (ftThread0, ftThread1, ftThread2, ftThread3);
  241. TOmfFixupMode = (fmSelfRelative, fmSegmentRelative);
  242. TOmfFixupLocationType = (
  243. fltLoByte = 0, { low 8 bits of 16-bit offset }
  244. fltOffset = 1, { 16-bit offset }
  245. fltBase = 2, { 16-bit base (segment) }
  246. fltFarPointer = 3, { 16-bit base:16-bit offset }
  247. fltHiByte = 4, { high 8 bits of 16-bit offset }
  248. fltLoaderResolvedOffset = 5, { PharLap: Offset32 }
  249. fltUndefined6 = 6, { PharLap: Pointer48 }
  250. fltUndefined7 = 7,
  251. fltUndefined8 = 8,
  252. fltOffset32 = 9, { 32-bit offset }
  253. fltUndefined10 = 10,
  254. fltFarPointer48 = 11, { 16-bit base:32-bit offset }
  255. fltUndefined12 = 12,
  256. fltLoaderResolvedOffset32 = 13,
  257. fltUndefined14 = 14,
  258. fltUndefined15 = 15);
  259. TOmfFixupFrameMethod = (
  260. ffmSegmentIndex = 0, { SI(<segment name>) - The frame is the canonic frame of the logical
  261. segment segment defined by the index }
  262. ffmGroupIndex = 1, { GI(<group name>) - The frame is the canonic frame of the group
  263. (= the canonic frame of the logical segment from the group,
  264. located at the lowest memory address) }
  265. ffmExternalIndex = 2, { EI(<symbol name>) - The frame is determined depending on the external's public definition:
  266. * if the symbol is defined relative to a logical segment and no defined group,
  267. the frame of the logical segment is used
  268. * if the symbol is defined absolutely, without reference to a logical segment and
  269. no defined group, the FRAME NUMBER from the symbol's PUBDEF record is used
  270. * regardless of how the symbol is specified, if there's an associated group,
  271. that group's canonic frame is used }
  272. ffmFrameNumber = 3, { <FRAME NUMBER> - The frame is a directly specified constant. }
  273. ffmLocation = 4, { LOCATION - The frame is determined by the location (i.e. the canonic frame of the logical
  274. segment where the fixup location is) }
  275. ffmTarget = 5, { TARGET - The frame is determined by the target. }
  276. ffmNone = 6, { NONE - There is no frame. Used for 8089 self-relative references. }
  277. ffmUndefined = 7);
  278. TOmfFixupTargetMethod = (
  279. ftmSegmentIndex = 0, { SI(<segment name>),<displacement> }
  280. ftmGroupIndex = 1, { GI(<group name>),<displacement> }
  281. ftmExternalIndex = 2, { EI(<symbol name>),<displacement> }
  282. ftmFrameNumber = 3, { <FRAME NUMBER>,<displacement> }
  283. ftmSegmentIndexNoDisp = 4, { SI(<segment name>) }
  284. ftmGroupIndexNoDisp = 5, { GI(<group name>) }
  285. ftmExternalIndexNoDisp = 6, { EI(<symbol name>) }
  286. ftmFrameNumberNoDisp = 7); { <FRAME NUMBER> }
  287. { TOmfOrderedNameCollection }
  288. TOmfOrderedNameCollection = class
  289. private
  290. FStringList: array of string;
  291. function GetCount: Integer;
  292. function GetString(Index: Integer): string;
  293. procedure SetString(Index: Integer; AValue: string);
  294. public
  295. function Add(const S: string): Integer;
  296. procedure Clear;
  297. property Strings [Index: Integer]: string read GetString write SetString; default;
  298. property Count: Integer read GetCount;
  299. end;
  300. { TOmfRawRecord }
  301. TOmfRawRecord = class
  302. private
  303. function GetChecksumByte: Byte;
  304. function GetRecordLength: Word;
  305. function GetRecordType: Byte;
  306. procedure SetChecksumByte(AValue: Byte);
  307. procedure SetRecordLength(AValue: Word);
  308. procedure SetRecordType(AValue: Byte);
  309. public
  310. RawData: array [-3..65535] of Byte;
  311. property RecordType: Byte read GetRecordType write SetRecordType;
  312. property RecordLength: Word read GetRecordLength write SetRecordLength;
  313. function ReadStringAt(Offset: Integer; out s: string): Integer;
  314. function WriteStringAt(Offset: Integer; s: string): Integer;
  315. function ReadIndexedRef(Offset: Integer; out IndexedRef: Integer): Integer;
  316. function WriteIndexedRef(Offset: Integer; IndexedRef: Integer): Integer;
  317. procedure CalculateChecksumByte;
  318. function VerifyChecksumByte: boolean;
  319. property ChecksumByte: Byte read GetChecksumByte write SetChecksumByte;
  320. procedure ReadFrom(aReader: TObjectReader);
  321. procedure ReadFrom(aReader: TDynamicArray);
  322. procedure WriteTo(aWriter: TObjectWriter);
  323. procedure WriteTo(aWriter: TDynamicArray);
  324. end;
  325. { TOmfParsedRecord }
  326. TOmfParsedRecord = class
  327. public
  328. procedure DecodeFrom(RawRecord: TOmfRawRecord);virtual;abstract;
  329. procedure EncodeTo(RawRecord: TOmfRawRecord);virtual;abstract;
  330. end;
  331. { TOmfRecord_THEADR }
  332. TOmfRecord_THEADR = class(TOmfParsedRecord)
  333. private
  334. FModuleName: string;
  335. public
  336. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  337. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  338. property ModuleName: string read FModuleName write FModuleName;
  339. end;
  340. { TOmfRecord_COMENT }
  341. TOmfRecord_COMENT = class(TOmfParsedRecord)
  342. private
  343. FCommentType: Byte;
  344. FCommentClass: Byte;
  345. FCommentString: string;
  346. function GetNoList: Boolean;
  347. function GetNoPurge: Boolean;
  348. procedure SetNoList(AValue: Boolean);
  349. procedure SetNoPurge(AValue: Boolean);
  350. public
  351. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  352. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  353. property CommentType: Byte read FCommentType write FCommentType;
  354. property CommentClass: Byte read FCommentClass write FCommentClass;
  355. property CommentString: string read FCommentString write FCommentString;
  356. property NoPurge: Boolean read GetNoPurge write SetNoPurge;
  357. property NoList: Boolean read GetNoList write SetNoList;
  358. end;
  359. { TOmfRecord_LNAMES }
  360. TOmfRecord_LNAMES = class(TOmfParsedRecord)
  361. private
  362. FNames: TOmfOrderedNameCollection;
  363. FNextIndex: Integer;
  364. public
  365. constructor Create;
  366. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  367. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  368. property Names: TOmfOrderedNameCollection read FNames write FNames;
  369. property NextIndex: Integer read FNextIndex write FNextIndex;
  370. end;
  371. { TOmfRecord_SEGDEF }
  372. TOmfRecord_SEGDEF = class(TOmfParsedRecord)
  373. private
  374. FAlignment: TOmfSegmentAlignment;
  375. FCombination: TOmfSegmentCombination;
  376. FUse: TOmfSegmentUse;
  377. FFrameNumber: Word;
  378. FOffset: Byte;
  379. FIs32Bit: Boolean;
  380. FSegmentLength: Int64; { int64, because it can be 2**32 }
  381. FSegmentNameIndex: Integer;
  382. FClassNameIndex: Integer;
  383. FOverlayNameIndex: Integer;
  384. public
  385. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  386. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  387. property Alignment: TOmfSegmentAlignment read FAlignment write FAlignment;
  388. property Combination: TOmfSegmentCombination read FCombination write FCombination;
  389. property Use: TOmfSegmentUse read FUse write FUse;
  390. property FrameNumber: Word read FFrameNumber write FFrameNumber;
  391. property Offset: Byte read FOffset write FOffset;
  392. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  393. property SegmentLength: Int64 read FSegmentLength write FSegmentLength;
  394. property SegmentNameIndex: Integer read FSegmentNameIndex write FSegmentNameIndex;
  395. property ClassNameIndex: Integer read FClassNameIndex write FClassNameIndex;
  396. property OverlayNameIndex: Integer read FOverlayNameIndex write FOverlayNameIndex;
  397. end;
  398. TSegmentList = array of Integer;
  399. { TOmfRecord_GRPDEF }
  400. TOmfRecord_GRPDEF = class(TOmfParsedRecord)
  401. private
  402. FGroupNameIndex: Integer;
  403. FSegmentList: TSegmentList;
  404. public
  405. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  406. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  407. property GroupNameIndex: Integer read FGroupNameIndex write FGroupNameIndex;
  408. property SegmentList: TSegmentList read FSegmentList write FSegmentList;
  409. end;
  410. { TOmfPublicNameElement }
  411. TOmfPublicNameElement = class(TFPHashObject)
  412. private
  413. FPublicOffset: DWord;
  414. FTypeIndex: Integer;
  415. public
  416. function GetLengthInFile(Is32Bit: Boolean): Integer;
  417. property PublicOffset: DWord read FPublicOffset write FPublicOffset;
  418. property TypeIndex: Integer read FTypeIndex write FTypeIndex;
  419. end;
  420. { TOmfRecord_PUBDEF }
  421. TOmfRecord_PUBDEF = class(TOmfParsedRecord)
  422. private
  423. FIs32Bit: Boolean;
  424. FBaseGroupIndex: Integer;
  425. FBaseSegmentIndex: Integer;
  426. FBaseFrame: Word;
  427. FPublicNames: TFPHashObjectList;
  428. FNextIndex: Integer;
  429. public
  430. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  431. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  432. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  433. property BaseGroupIndex: Integer read FBaseGroupIndex write FBaseGroupIndex;
  434. property BaseSegmentIndex: Integer read FBaseSegmentIndex write FBaseSegmentIndex;
  435. property BaseFrame: Word read FBaseFrame write FBaseFrame;
  436. property PublicNames: TFPHashObjectList read FPublicNames write FPublicNames;
  437. property NextIndex: Integer read FNextIndex write FNextIndex;
  438. end;
  439. { TOmfExternalNameElement }
  440. TOmfExternalNameElement = class(TFPHashObject)
  441. private
  442. FTypeIndex: Integer;
  443. public
  444. function GetLengthInFile: Integer;
  445. property TypeIndex: Integer read FTypeIndex write FTypeIndex;
  446. end;
  447. { TOmfRecord_EXTDEF }
  448. TOmfRecord_EXTDEF = class(TOmfParsedRecord)
  449. private
  450. FExternalNames: TFPHashObjectList;
  451. FNextIndex: Integer;
  452. public
  453. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  454. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  455. property ExternalNames: TFPHashObjectList read FExternalNames write FExternalNames;
  456. property NextIndex: Integer read FNextIndex write FNextIndex;
  457. end;
  458. { TOmfRecord_MODEND }
  459. TOmfRecord_MODEND = class(TOmfParsedRecord)
  460. private
  461. FIs32Bit: Boolean;
  462. FIsMainModule: Boolean;
  463. FHasStartAddress: Boolean;
  464. FSegmentBit: Boolean;
  465. FLogicalStartAddress: Boolean;
  466. FFrameMethod: TOmfFixupFrameMethod;
  467. FFrameDatum: Integer;
  468. FTargetMethod: TOmfFixupTargetMethod;
  469. FTargetDatum: Integer;
  470. FTargetDisplacement: DWord;
  471. FPhysFrameNumber: Word;
  472. FPhysOffset: DWord;
  473. public
  474. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  475. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  476. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  477. property IsMainModule: Boolean read FIsMainModule write FIsMainModule;
  478. property HasStartAddress: Boolean read FHasStartAddress write FHasStartAddress;
  479. property SegmentBit: Boolean read FSegmentBit write FSegmentBit;
  480. property LogicalStartAddress: Boolean read FLogicalStartAddress write FLogicalStartAddress;
  481. { properties, specifying a logical start address (used when LogicalStartAddress=true) }
  482. property FrameMethod: TOmfFixupFrameMethod read FFrameMethod write FFrameMethod;
  483. property FrameDatum: Integer read FFrameDatum write FFrameDatum;
  484. property TargetMethod: TOmfFixupTargetMethod read FTargetMethod write FTargetMethod;
  485. property TargetDatum: Integer read FTargetDatum write FTargetDatum;
  486. property TargetDisplacement: DWord read FTargetDisplacement write FTargetDisplacement;
  487. { properties, specifying a physical start address (used when LogicalStartAddress=false) }
  488. property PhysFrameNumber: Word read FPhysFrameNumber write FPhysFrameNumber;
  489. property PhysOffset: DWord read FPhysOffset write FPhysOffset;
  490. end;
  491. { TOmfSubRecord_FIXUP }
  492. TOmfSubRecord_FIXUP = class
  493. private
  494. FIs32Bit: Boolean;
  495. FMode: TOmfFixupMode;
  496. FLocationType: TOmfFixupLocationType;
  497. FLocationOffset: DWord;
  498. FDataRecordStartOffset: DWord;
  499. FTargetDeterminedByThread: Boolean;
  500. FTargetThread: TOmfFixupThread;
  501. FTargetThreadDisplacementPresent: Boolean;
  502. FTargetMethod: TOmfFixupTargetMethod;
  503. FTargetDatum: Integer;
  504. FTargetDisplacement: DWord;
  505. FFrameDeterminedByThread: Boolean;
  506. FFrameThread: TOmfFixupThread;
  507. FFrameMethod: TOmfFixupFrameMethod;
  508. FFrameDatum: Integer;
  509. function GetDataRecordOffset: Integer;
  510. function GetLocationSize: Integer;
  511. procedure SetDataRecordOffset(AValue: Integer);
  512. public
  513. function ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  514. function WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  515. property Is32Bit: Boolean read FIs32Bit write FIs32Bit;
  516. property Mode: TOmfFixupMode read FMode write FMode;
  517. property LocationType: TOmfFixupLocationType read FLocationType write FLocationType;
  518. property LocationOffset: DWord read FLocationOffset write FLocationOffset;
  519. property LocationSize: Integer read GetLocationSize;
  520. property DataRecordStartOffset: DWord read FDataRecordStartOffset write FDataRecordStartOffset;
  521. property DataRecordOffset: Integer read GetDataRecordOffset write SetDataRecordOffset;
  522. property TargetDeterminedByThread: Boolean read FTargetDeterminedByThread write FTargetDeterminedByThread;
  523. property TargetThread: TOmfFixupThread read FTargetThread write FTargetThread;
  524. property TargetThreadDisplacementPresent: Boolean read FTargetThreadDisplacementPresent write FTargetThreadDisplacementPresent;
  525. property TargetMethod: TOmfFixupTargetMethod read FTargetMethod write FTargetMethod;
  526. property TargetDatum: Integer read FTargetDatum write FTargetDatum;
  527. property TargetDisplacement: DWord read FTargetDisplacement write FTargetDisplacement;
  528. property FrameDeterminedByThread: Boolean read FFrameDeterminedByThread write FFrameDeterminedByThread;
  529. property FrameThread: TOmfFixupThread read FFrameThread write FFrameThread;
  530. property FrameMethod: TOmfFixupFrameMethod read FFrameMethod write FFrameMethod;
  531. property FrameDatum: Integer read FFrameDatum write FFrameDatum;
  532. end;
  533. { TOmfRecord_LIBHEAD }
  534. TOmfRecord_LIBHEAD = class(TOmfParsedRecord)
  535. private
  536. FPageSize: Integer;
  537. FDictionaryOffset: DWord;
  538. FDictionarySizeInBlocks: Word;
  539. FFlags: Byte;
  540. function IsCaseSensitive: Boolean;
  541. procedure SetCaseSensitive(AValue: Boolean);
  542. procedure SetPageSize(AValue: Integer);
  543. public
  544. constructor Create;
  545. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  546. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  547. property PageSize: Integer read FPageSize write SetPageSize;
  548. property DictionaryOffset: DWord read FDictionaryOffset write FDictionaryOffset;
  549. property DictionarySizeInBlocks: Word read FDictionarySizeInBlocks write FDictionarySizeInBlocks;
  550. property Flags: Byte read FFlags write FFlags;
  551. property CaseSensitive: Boolean read IsCaseSensitive write SetCaseSensitive;
  552. end;
  553. { TOmfRecord_LIBEND }
  554. TOmfRecord_LIBEND = class(TOmfParsedRecord)
  555. private
  556. FPaddingBytes: Word;
  557. public
  558. procedure DecodeFrom(RawRecord: TOmfRawRecord);override;
  559. procedure EncodeTo(RawRecord: TOmfRawRecord);override;
  560. procedure CalculatePaddingBytes(RecordStartOffset: DWord);
  561. property PaddingBytes: Word read FPaddingBytes write FPaddingBytes;
  562. end;
  563. const
  564. { list of all the possible omf library dictionary block counts - contains
  565. all the prime numbers less than 255 }
  566. OmfLibDictionaryBlockCounts: array [0..53] of Byte =
  567. (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
  568. 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,
  569. 193,197,199,211,223,227,229,233,239,241,251);
  570. type
  571. TOmfLibHash = record
  572. block_x: Integer;
  573. block_d: Integer;
  574. bucket_x: Integer;
  575. bucket_d: Integer;
  576. end;
  577. function compute_omf_lib_hash(const name: string; blocks: Integer): TOmfLibHash;
  578. implementation
  579. uses
  580. cutils,
  581. verbose;
  582. { TOmfOrderedNameCollection }
  583. function TOmfOrderedNameCollection.GetString(Index: Integer): string;
  584. begin
  585. Result:=FStringList[Index-1];
  586. end;
  587. function TOmfOrderedNameCollection.GetCount: Integer;
  588. begin
  589. Result:=Length(FStringList);
  590. end;
  591. procedure TOmfOrderedNameCollection.SetString(Index: Integer; AValue: string);
  592. begin
  593. FStringList[Index-1]:=AValue;
  594. end;
  595. function TOmfOrderedNameCollection.Add(const S: string): Integer;
  596. begin
  597. Result:=Length(FStringList)+1;
  598. SetLength(FStringList,Result);
  599. FStringList[Result-1]:=S;
  600. end;
  601. procedure TOmfOrderedNameCollection.Clear;
  602. begin
  603. SetLength(FStringList,0);
  604. end;
  605. { TOmfRawRecord }
  606. function TOmfRawRecord.GetRecordType: Byte;
  607. begin
  608. Result:=RawData[-3];
  609. end;
  610. procedure TOmfRawRecord.SetRecordType(AValue: Byte);
  611. begin
  612. RawData[-3]:=AValue;
  613. end;
  614. function TOmfRawRecord.GetRecordLength: Word;
  615. begin
  616. Result:=RawData[-2] or (RawData[-1] shl 8);
  617. end;
  618. procedure TOmfRawRecord.SetRecordLength(AValue: Word);
  619. begin
  620. RawData[-2]:=Byte(AValue);
  621. RawData[-1]:=Byte(AValue shr 8);
  622. end;
  623. function TOmfRawRecord.ReadStringAt(Offset: Integer; out s: string): Integer;
  624. var
  625. len: Byte;
  626. begin
  627. len:=RawData[Offset];
  628. Result:=Offset+len+1;
  629. if result>RecordLength then
  630. internalerror(2015033103);
  631. SetLength(s, len);
  632. UniqueString(s);
  633. Move(RawData[Offset+1],s[1],len);
  634. end;
  635. function TOmfRawRecord.WriteStringAt(Offset: Integer; s: string): Integer;
  636. begin
  637. if Length(s)>255 then
  638. internalerror(2015033101);
  639. result:=Offset+Length(s)+1;
  640. if result>High(RawData) then
  641. internalerror(2015033102);
  642. RawData[Offset]:=Length(s);
  643. Move(s[1], RawData[Offset+1], Length(s));
  644. end;
  645. function TOmfRawRecord.ReadIndexedRef(Offset: Integer; out IndexedRef: Integer): Integer;
  646. begin
  647. Result:=Offset+1;
  648. if result>RecordLength then
  649. internalerror(2015033103);
  650. IndexedRef:=RawData[Offset];
  651. if IndexedRef<=$7f then
  652. exit;
  653. Result:=Offset+2;
  654. if result>RecordLength then
  655. internalerror(2015033103);
  656. IndexedRef:=((IndexedRef and $7f) shl 8)+RawData[Offset+1];
  657. end;
  658. function TOmfRawRecord.WriteIndexedRef(Offset: Integer; IndexedRef: Integer): Integer;
  659. begin
  660. if (IndexedRef<0) or (IndexedRef>$7FFF) then
  661. internalerror(2015040303);
  662. if IndexedRef<=$7f then
  663. begin
  664. Result:=Offset+1;
  665. if Result>High(RawData) then
  666. internalerror(2015033102);
  667. RawData[Offset]:=IndexedRef;
  668. end
  669. else
  670. begin
  671. Result:=Offset+2;
  672. if Result>High(RawData) then
  673. internalerror(2015033102);
  674. RawData[Offset]:=$80+(IndexedRef shr 8);
  675. RawData[Offset+1]:=Byte(IndexedRef);
  676. end;
  677. end;
  678. function TOmfRawRecord.GetChecksumByte: Byte;
  679. begin
  680. if RecordLength>0 then
  681. Result:=RawData[RecordLength-1]
  682. else
  683. Result:=0;
  684. end;
  685. procedure TOmfRawRecord.SetChecksumByte(AValue: Byte);
  686. begin
  687. if RecordLength>0 then
  688. RawData[RecordLength-1]:=AValue;
  689. end;
  690. procedure TOmfRawRecord.CalculateChecksumByte;
  691. var
  692. I: Integer;
  693. b: Byte;
  694. begin
  695. b:=0;
  696. for I:=-3 to RecordLength-2 do
  697. b:=byte(b+RawData[I]);
  698. SetChecksumByte($100-b);
  699. end;
  700. function TOmfRawRecord.VerifyChecksumByte: boolean;
  701. var
  702. I: Integer;
  703. b: Byte;
  704. begin
  705. { according to the OMF spec, some tools always write a 0 rather than
  706. computing the checksum, so it should also be accepted as correct }
  707. if ChecksumByte=0 then
  708. exit(true);
  709. b:=0;
  710. for I:=-3 to RecordLength-1 do
  711. b:=byte(b+RawData[I]);
  712. Result:=(b=0);
  713. end;
  714. procedure TOmfRawRecord.ReadFrom(aReader: TObjectReader);
  715. begin
  716. aReader.read(RawData, 3);
  717. aReader.read(RawData[0], RecordLength);
  718. end;
  719. procedure TOmfRawRecord.ReadFrom(aReader: TDynamicArray);
  720. begin
  721. aReader.read(RawData, 3);
  722. aReader.read(RawData[0], RecordLength);
  723. end;
  724. procedure TOmfRawRecord.WriteTo(aWriter: TObjectWriter);
  725. begin
  726. aWriter.write(RawData, RecordLength+3);
  727. end;
  728. procedure TOmfRawRecord.WriteTo(aWriter: TDynamicArray);
  729. begin
  730. aWriter.write(RawData, RecordLength+3);
  731. end;
  732. { TOmfRecord_THEADR }
  733. procedure TOmfRecord_THEADR.DecodeFrom(RawRecord: TOmfRawRecord);
  734. begin
  735. if RawRecord.RecordType<>RT_THEADR then
  736. internalerror(2015040301);
  737. RawRecord.ReadStringAt(0,FModuleName);
  738. end;
  739. procedure TOmfRecord_THEADR.EncodeTo(RawRecord: TOmfRawRecord);
  740. var
  741. NextOfs: Integer;
  742. begin
  743. RawRecord.RecordType:=RT_THEADR;
  744. NextOfs:=RawRecord.WriteStringAt(0,ModuleName);
  745. RawRecord.RecordLength:=NextOfs+1;
  746. RawRecord.CalculateChecksumByte;
  747. end;
  748. { TOmfRecord_COMENT }
  749. function TOmfRecord_COMENT.GetNoList: Boolean;
  750. begin
  751. Result:=(CommentType and $40)<>0;
  752. end;
  753. function TOmfRecord_COMENT.GetNoPurge: Boolean;
  754. begin
  755. Result:=(CommentType and $80)<>0;
  756. end;
  757. procedure TOmfRecord_COMENT.SetNoList(AValue: Boolean);
  758. begin
  759. if AValue then
  760. CommentType:=CommentType or $40
  761. else
  762. CommentType:=CommentType and $BF;
  763. end;
  764. procedure TOmfRecord_COMENT.SetNoPurge(AValue: Boolean);
  765. begin
  766. if AValue then
  767. CommentType:=CommentType or $80
  768. else
  769. CommentType:=CommentType and $7F;
  770. end;
  771. procedure TOmfRecord_COMENT.DecodeFrom(RawRecord: TOmfRawRecord);
  772. begin
  773. if RawRecord.RecordType<>RT_COMENT then
  774. internalerror(2015040301);
  775. if RawRecord.RecordLength<3 then
  776. internalerror(2015033104);
  777. CommentType:=RawRecord.RawData[0];
  778. CommentClass:=RawRecord.RawData[1];
  779. SetLength(FCommentString,RawRecord.RecordLength-3);
  780. UniqueString(FCommentString);
  781. Move(RawRecord.RawData[2],FCommentString[1],Length(FCommentString));
  782. end;
  783. procedure TOmfRecord_COMENT.EncodeTo(RawRecord: TOmfRawRecord);
  784. begin
  785. RawRecord.RecordType:=RT_COMENT;
  786. if (Length(FCommentString)+3)>High(RawRecord.RawData) then
  787. internalerror(2015033105);
  788. RawRecord.RecordLength:=Length(FCommentString)+3;
  789. RawRecord.RawData[0]:=CommentType;
  790. RawRecord.RawData[1]:=CommentClass;
  791. Move(FCommentString[1],RawRecord.RawData[2],Length(FCommentString));
  792. RawRecord.CalculateChecksumByte;
  793. end;
  794. { TOmfRecord_LNAMES }
  795. constructor TOmfRecord_LNAMES.Create;
  796. begin
  797. FNextIndex:=1;
  798. end;
  799. procedure TOmfRecord_LNAMES.DecodeFrom(RawRecord: TOmfRawRecord);
  800. var
  801. NextOfs: Integer;
  802. Name: string;
  803. begin
  804. if RawRecord.RecordType<>RT_LNAMES then
  805. internalerror(2015040301);
  806. NextOfs:=0;
  807. while NextOfs<(RawRecord.RecordLength-1) do
  808. begin
  809. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  810. Names.Add(Name);
  811. end;
  812. end;
  813. procedure TOmfRecord_LNAMES.EncodeTo(RawRecord: TOmfRawRecord);
  814. const
  815. RecordLengthLimit = 1024;
  816. var
  817. Len,LastIncludedIndex,NextOfs,I: Integer;
  818. begin
  819. RawRecord.RecordType:=RT_LNAMES;
  820. { find out how many strings can we include until we reach the length limit }
  821. Len:=1;
  822. LastIncludedIndex:=NextIndex-1;
  823. repeat
  824. Inc(LastIncludedIndex);
  825. Inc(Len,Length(Names[LastIncludedIndex])+1);
  826. until (LastIncludedIndex>=Names.Count) or ((Len+Length(Names[LastIncludedIndex+1])+1)>=RecordLengthLimit);
  827. { write the strings... }
  828. NextOfs:=0;
  829. for I:=NextIndex to LastIncludedIndex do
  830. NextOfs:=RawRecord.WriteStringAt(NextOfs,Names[I]);
  831. RawRecord.RecordLength:=Len;
  832. RawRecord.CalculateChecksumByte;
  833. { update NextIndex }
  834. NextIndex:=LastIncludedIndex+1;
  835. end;
  836. { TOmfRecord_SEGDEF }
  837. procedure TOmfRecord_SEGDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  838. var
  839. B: Byte;
  840. Big: Boolean;
  841. NextOfs: Integer;
  842. MinLen: Integer;
  843. begin
  844. if not (RawRecord.RecordType in [RT_SEGDEF,RT_SEGDEF32]) then
  845. internalerror(2015040301);
  846. Is32Bit:=RawRecord.RecordType=RT_SEGDEF32;
  847. MinLen:=7; { b(1)+seglength(2..4)+segnameindex(1..2)+classnameindex(1..2)+overlaynameindex(1..2)+checksum }
  848. if Is32Bit then
  849. inc(MinLen,2);
  850. if RawRecord.RecordLength<MinLen then
  851. internalerror(2015040305);
  852. B:=RawRecord.RawData[0];
  853. Alignment:=TOmfSegmentAlignment(B shr 5);
  854. Combination:=TOmfSegmentCombination((B shr 2) and 7);
  855. Big:=(B and 2)<>0;
  856. Use:=TOmfSegmentUse(B and 1);
  857. NextOfs:=1;
  858. if Alignment=saAbsolute then
  859. begin
  860. inc(MinLen,3);
  861. if RawRecord.RecordLength<MinLen then
  862. internalerror(2015040305);
  863. FrameNumber:=RawRecord.RawData[1]+(RawRecord.RawData[2] shl 8);
  864. Offset:=RawRecord.RawData[3];
  865. NextOfs:=4;
  866. end
  867. else
  868. begin
  869. FrameNumber:=0;
  870. Offset:=0;
  871. end;
  872. if Is32Bit then
  873. begin
  874. SegmentLength:=RawRecord.RawData[NextOfs]+
  875. (RawRecord.RawData[NextOfs+1] shl 8)+
  876. (RawRecord.RawData[NextOfs+2] shl 16)+
  877. (RawRecord.RawData[NextOfs+3] shl 24);
  878. if Big then
  879. if SegmentLength=0 then
  880. SegmentLength:=4294967296
  881. else
  882. internalerror(2015040306);
  883. Inc(NextOfs,4);
  884. end
  885. else
  886. begin
  887. SegmentLength:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  888. if Big then
  889. if SegmentLength=0 then
  890. SegmentLength:=65536
  891. else
  892. internalerror(2015040306);
  893. Inc(NextOfs,2);
  894. end;
  895. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FSegmentNameIndex);
  896. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FClassNameIndex);
  897. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FOverlayNameIndex);
  898. end;
  899. procedure TOmfRecord_SEGDEF.EncodeTo(RawRecord: TOmfRawRecord);
  900. var
  901. Big: Boolean;
  902. NextOfs: Integer;
  903. begin
  904. if Is32Bit then
  905. begin
  906. RawRecord.RecordType:=RT_SEGDEF32;
  907. if SegmentLength>4294967296 then
  908. internalerror(2015040302);
  909. Big:=SegmentLength=4294967296;
  910. end
  911. else
  912. begin
  913. RawRecord.RecordType:=RT_SEGDEF;
  914. if SegmentLength>65536 then
  915. internalerror(2015040302);
  916. Big:=SegmentLength=65536;
  917. end;
  918. RawRecord.RawData[0]:=(Ord(Alignment) shl 5) or (Ord(Combination) shl 2) or (Ord(Big) shl 1) or Ord(Use);
  919. NextOfs:=1;
  920. if Alignment=saAbsolute then
  921. begin
  922. RawRecord.RawData[1]:=Byte(FrameNumber);
  923. RawRecord.RawData[2]:=Byte(FrameNumber shr 8);
  924. RawRecord.RawData[3]:=Offset;
  925. NextOfs:=4;
  926. end;
  927. if Is32Bit then
  928. begin
  929. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  930. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  931. RawRecord.RawData[NextOfs+2]:=Byte(SegmentLength shr 16);
  932. RawRecord.RawData[NextOfs+3]:=Byte(SegmentLength shr 24);
  933. Inc(NextOfs,4);
  934. end
  935. else
  936. begin
  937. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  938. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  939. Inc(NextOfs,2);
  940. end;
  941. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,SegmentNameIndex);
  942. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ClassNameIndex);
  943. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,OverlayNameIndex);
  944. RawRecord.RecordLength:=NextOfs+1;
  945. RawRecord.CalculateChecksumByte;
  946. end;
  947. { TOmfRecord_GRPDEF }
  948. procedure TOmfRecord_GRPDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  949. var
  950. NextOfs: Integer;
  951. Segment: Integer;
  952. begin
  953. if RawRecord.RecordType<>RT_GRPDEF then
  954. internalerror(2015040301);
  955. NextOfs:=RawRecord.ReadIndexedRef(0,FGroupNameIndex);
  956. SetLength(FSegmentList,0);
  957. while NextOfs<RawRecord.RecordLength-1 do
  958. begin
  959. if RawRecord.RawData[NextOfs]<>$ff then
  960. internalerror(2015040901);
  961. NextOfs:=RawRecord.ReadIndexedRef(NextOfs+1,Segment);
  962. SetLength(FSegmentList,Length(FSegmentList)+1);
  963. FSegmentList[High(FSegmentList)]:=Segment;
  964. end;
  965. end;
  966. procedure TOmfRecord_GRPDEF.EncodeTo(RawRecord: TOmfRawRecord);
  967. var
  968. NextOfs: Integer;
  969. Segment: Integer;
  970. begin
  971. RawRecord.RecordType:=RT_GRPDEF;
  972. NextOfs:=RawRecord.WriteIndexedRef(0,GroupNameIndex);
  973. for Segment in SegmentList do
  974. begin
  975. if NextOfs>High(RawRecord.RawData) then
  976. internalerror(2015040401);
  977. RawRecord.RawData[NextOfs]:=$ff;
  978. NextOfs:=RawRecord.WriteIndexedRef(NextOfs+1,Segment);
  979. end;
  980. RawRecord.RecordLength:=NextOfs+1;
  981. RawRecord.CalculateChecksumByte;
  982. end;
  983. { TOmfPublicNameElement }
  984. function TOmfPublicNameElement.GetLengthInFile(Is32Bit: Boolean): Integer;
  985. begin
  986. Result:=1+Length(Name)+2+1;
  987. if Is32Bit then
  988. Inc(Result,2);
  989. if TypeIndex>=$80 then
  990. Inc(Result);
  991. end;
  992. { TOmfRecord_PUBDEF }
  993. procedure TOmfRecord_PUBDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  994. var
  995. NextOfs: Integer;
  996. Name: string;
  997. TypeIndex: Integer;
  998. PublicOffset: DWord;
  999. PubName: TOmfPublicNameElement;
  1000. begin
  1001. if not (RawRecord.RecordType in [RT_PUBDEF,RT_PUBDEF32]) then
  1002. internalerror(2015040301);
  1003. Is32Bit:=RawRecord.RecordType=RT_PUBDEF32;
  1004. NextOfs:=RawRecord.ReadIndexedRef(0,FBaseGroupIndex);
  1005. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FBaseSegmentIndex);
  1006. if BaseSegmentIndex=0 then
  1007. begin
  1008. if (NextOfs+1)>=RawRecord.RecordLength then
  1009. internalerror(2015041401);
  1010. BaseFrame:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1011. Inc(NextOfs,2);
  1012. end
  1013. else
  1014. BaseFrame:=0;
  1015. while NextOfs<(RawRecord.RecordLength-1) do
  1016. begin
  1017. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  1018. if Is32Bit then
  1019. begin
  1020. if (NextOfs+3)>=RawRecord.RecordLength then
  1021. internalerror(2015041401);
  1022. PublicOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8)+
  1023. (RawRecord.RawData[NextOfs+2] shl 16)+(RawRecord.RawData[NextOfs+3] shl 24);
  1024. Inc(NextOfs,4);
  1025. end
  1026. else
  1027. begin
  1028. if (NextOfs+1)>=RawRecord.RecordLength then
  1029. internalerror(2015041401);
  1030. PublicOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1031. Inc(NextOfs,2);
  1032. end;
  1033. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,TypeIndex);
  1034. PubName:=TOmfPublicNameElement.Create(PublicNames,Name);
  1035. PubName.PublicOffset:=PublicOffset;
  1036. PubName.TypeIndex:=TypeIndex;
  1037. end;
  1038. end;
  1039. procedure TOmfRecord_PUBDEF.EncodeTo(RawRecord: TOmfRawRecord);
  1040. const
  1041. RecordLengthLimit = 1024;
  1042. var
  1043. Len,LastIncludedIndex,NextOfs,I: Integer;
  1044. PubName: TOmfPublicNameElement;
  1045. begin
  1046. if Is32Bit then
  1047. RawRecord.RecordType:=RT_PUBDEF32
  1048. else
  1049. RawRecord.RecordType:=RT_PUBDEF;
  1050. NextOfs:=RawRecord.WriteIndexedRef(0,BaseGroupIndex);
  1051. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,BaseSegmentIndex);
  1052. if BaseSegmentIndex=0 then
  1053. begin
  1054. RawRecord.RawData[NextOfs]:=Byte(BaseFrame);
  1055. RawRecord.RawData[NextOfs+1]:=Byte(BaseFrame shr 8);
  1056. Inc(NextOfs,2);
  1057. end;
  1058. { find out how many public names can we include until we reach the length limit }
  1059. Len:=NextOfs;
  1060. LastIncludedIndex:=NextIndex-1;
  1061. repeat
  1062. Inc(LastIncludedIndex);
  1063. Inc(Len,TOmfPublicNameElement(PublicNames[LastIncludedIndex]).GetLengthInFile(Is32Bit));
  1064. until (LastIncludedIndex>=(PublicNames.Count-1)) or ((Len+TOmfPublicNameElement(PublicNames[LastIncludedIndex+1]).GetLengthInFile(Is32Bit))>=RecordLengthLimit);
  1065. { write the public names... }
  1066. for I:=NextIndex to LastIncludedIndex do
  1067. begin
  1068. PubName:=TOmfPublicNameElement(PublicNames[I]);
  1069. NextOfs:=RawRecord.WriteStringAt(NextOfs,PubName.Name);
  1070. if Is32Bit then
  1071. begin
  1072. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  1073. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  1074. RawRecord.RawData[NextOfs+2]:=Byte(PubName.PublicOffset shr 16);
  1075. RawRecord.RawData[NextOfs+3]:=Byte(PubName.PublicOffset shr 24);
  1076. Inc(NextOfs,4);
  1077. end
  1078. else
  1079. begin
  1080. if PubName.PublicOffset>$ffff then
  1081. internalerror(2015041403);
  1082. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  1083. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  1084. Inc(NextOfs,2);
  1085. end;
  1086. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,PubName.TypeIndex);
  1087. end;
  1088. RawRecord.RecordLength:=Len+1;
  1089. RawRecord.CalculateChecksumByte;
  1090. { update NextIndex }
  1091. NextIndex:=LastIncludedIndex+1;
  1092. end;
  1093. { TOmfExternalNameElement }
  1094. function TOmfExternalNameElement.GetLengthInFile: Integer;
  1095. begin
  1096. Result:=1+Length(Name)+1;
  1097. if TypeIndex>=$80 then
  1098. Inc(Result);
  1099. end;
  1100. { TOmfRecord_EXTDEF }
  1101. procedure TOmfRecord_EXTDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  1102. var
  1103. NextOfs: Integer;
  1104. Name: string;
  1105. TypeIndex: Integer;
  1106. ExtName: TOmfExternalNameElement;
  1107. begin
  1108. if RawRecord.RecordType<>RT_EXTDEF then
  1109. internalerror(2015040301);
  1110. NextOfs:=0;
  1111. while NextOfs<(RawRecord.RecordLength-1) do
  1112. begin
  1113. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  1114. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,TypeIndex);
  1115. ExtName:=TOmfExternalNameElement.Create(ExternalNames,Name);
  1116. ExtName.TypeIndex:=TypeIndex;
  1117. end;
  1118. end;
  1119. procedure TOmfRecord_EXTDEF.EncodeTo(RawRecord: TOmfRawRecord);
  1120. const
  1121. RecordLengthLimit = 1024;
  1122. var
  1123. Len,LastIncludedIndex,NextOfs,I: Integer;
  1124. ExtName: TOmfExternalNameElement;
  1125. begin
  1126. RawRecord.RecordType:=RT_EXTDEF;
  1127. NextOfs:=0;
  1128. { find out how many external names can we include until we reach the length limit }
  1129. Len:=NextOfs;
  1130. LastIncludedIndex:=NextIndex-1;
  1131. repeat
  1132. Inc(LastIncludedIndex);
  1133. Inc(Len,TOmfExternalNameElement(ExternalNames[LastIncludedIndex]).GetLengthInFile);
  1134. until (LastIncludedIndex>=(ExternalNames.Count-1)) or ((Len+TOmfExternalNameElement(ExternalNames[LastIncludedIndex+1]).GetLengthInFile)>=RecordLengthLimit);
  1135. { write the external names... }
  1136. for I:=NextIndex to LastIncludedIndex do
  1137. begin
  1138. ExtName:=TOmfExternalNameElement(ExternalNames[I]);
  1139. NextOfs:=RawRecord.WriteStringAt(NextOfs,ExtName.Name);
  1140. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ExtName.TypeIndex);
  1141. end;
  1142. RawRecord.RecordLength:=Len+1;
  1143. RawRecord.CalculateChecksumByte;
  1144. { update NextIndex }
  1145. NextIndex:=LastIncludedIndex+1;
  1146. end;
  1147. { TOmfRecord_MODEND }
  1148. procedure TOmfRecord_MODEND.DecodeFrom(RawRecord: TOmfRawRecord);
  1149. var
  1150. ModTyp: Byte;
  1151. NextOfs: Integer;
  1152. EndData: Byte;
  1153. begin
  1154. if not (RawRecord.RecordType in [RT_MODEND,RT_MODEND32]) then
  1155. internalerror(2015040301);
  1156. Is32Bit:=RawRecord.RecordType=RT_MODEND32;
  1157. if RawRecord.RecordLength<2 then
  1158. internalerror(2015040305);
  1159. ModTyp:=RawRecord.RawData[0];
  1160. IsMainModule:=(ModTyp and $80)<>0;
  1161. HasStartAddress:=(ModTyp and $40)<>0;
  1162. SegmentBit:=(ModTyp and $20)<>0;
  1163. LogicalStartAddress:=(ModTyp and $01)<>0;
  1164. if (ModTyp and $1E)<>0 then
  1165. internalerror(2015041404);
  1166. NextOfs:=1;
  1167. { clear all the start address properties first }
  1168. FrameMethod:=Low(FrameMethod);
  1169. FrameDatum:=0;
  1170. TargetMethod:=Low(TargetMethod);
  1171. TargetDatum:=0;
  1172. TargetDisplacement:=0;
  1173. PhysFrameNumber:=0;
  1174. PhysOffset:=0;
  1175. if HasStartAddress then
  1176. begin
  1177. if LogicalStartAddress then
  1178. begin
  1179. if NextOfs>=RawRecord.RecordLength then
  1180. internalerror(2015040305);
  1181. EndData:=RawRecord.RawData[NextOfs];
  1182. Inc(NextOfs);
  1183. { frame and target method determined by thread is not allowed in MODEND records }
  1184. if (EndData and $88)<>0 then
  1185. internalerror(2015041406);
  1186. FrameMethod:=TOmfFixupFrameMethod((EndData shr 4) and 7);
  1187. TargetMethod:=TOmfFixupTargetMethod(EndData and 7);
  1188. { frame method ffmLocation is not allowed in an MODEND record }
  1189. if FrameMethod=ffmLocation then
  1190. internalerror(2015041402);
  1191. { read Frame Datum? }
  1192. if FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber] then
  1193. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FFrameDatum);
  1194. { read Target Datum? }
  1195. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FTargetDatum);
  1196. { read Target Displacement? }
  1197. if TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber] then
  1198. begin
  1199. if Is32Bit then
  1200. begin
  1201. if (NextOfs+3)>=RawRecord.RecordLength then
  1202. internalerror(2015040504);
  1203. TargetDisplacement := RawRecord.RawData[NextOfs]+
  1204. (RawRecord.RawData[NextOfs+1] shl 8)+
  1205. (RawRecord.RawData[NextOfs+2] shl 16)+
  1206. (RawRecord.RawData[NextOfs+3] shl 24);
  1207. Inc(NextOfs,4);
  1208. end
  1209. else
  1210. begin
  1211. if (NextOfs+1)>=RawRecord.RecordLength then
  1212. internalerror(2015040504);
  1213. TargetDisplacement := RawRecord.RawData[NextOfs]+
  1214. (RawRecord.RawData[NextOfs+1] shl 8);
  1215. Inc(NextOfs,2);
  1216. end;
  1217. end;
  1218. end
  1219. else
  1220. begin
  1221. { physical start address }
  1222. if (NextOfs+1)>=RawRecord.RecordLength then
  1223. internalerror(2015040305);
  1224. PhysFrameNumber:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1225. Inc(NextOfs,2);
  1226. if Is32Bit then
  1227. begin
  1228. if (NextOfs+3)>=RawRecord.RecordLength then
  1229. internalerror(2015040305);
  1230. PhysOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8)+
  1231. (RawRecord.RawData[NextOfs+2] shl 16)+(RawRecord.RawData[NextOfs+3] shl 24);
  1232. Inc(NextOfs,4);
  1233. end
  1234. else
  1235. begin
  1236. if (NextOfs+1)>=RawRecord.RecordLength then
  1237. internalerror(2015040305);
  1238. PhysOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1239. Inc(NextOfs,2);
  1240. end;
  1241. end;
  1242. end;
  1243. end;
  1244. procedure TOmfRecord_MODEND.EncodeTo(RawRecord: TOmfRawRecord);
  1245. var
  1246. ModTyp: Byte;
  1247. NextOfs: Integer;
  1248. EndData: Byte;
  1249. begin
  1250. if Is32Bit then
  1251. RawRecord.RecordType:=RT_MODEND32
  1252. else
  1253. RawRecord.RecordType:=RT_MODEND;
  1254. ModTyp:=(Ord(IsMainModule) shl 7)+(Ord(HasStartAddress) shl 6)+(Ord(SegmentBit) shl 5)+Ord(LogicalStartAddress);
  1255. RawRecord.RawData[0]:=ModTyp;
  1256. NextOfs:=1;
  1257. if HasStartAddress then
  1258. begin
  1259. if LogicalStartAddress then
  1260. begin
  1261. { frame method ffmLocation is not allowed in an MODEND record }
  1262. if FrameMethod=ffmLocation then
  1263. internalerror(2015041402);
  1264. EndData:=(Ord(FrameMethod) shl 4)+Ord(TargetMethod);
  1265. RawRecord.RawData[NextOfs]:=EndData;
  1266. Inc(NextOfs);
  1267. { save Frame Datum? }
  1268. if FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber] then
  1269. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,FrameDatum);
  1270. { save Target Datum? }
  1271. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,TargetDatum);
  1272. { save Target Displacement? }
  1273. if TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber] then
  1274. begin
  1275. if Is32Bit then
  1276. begin
  1277. RawRecord.RawData[NextOfs]:=Byte(TargetDisplacement);
  1278. RawRecord.RawData[NextOfs+1]:=Byte(TargetDisplacement shr 8);
  1279. RawRecord.RawData[NextOfs+2]:=Byte(TargetDisplacement shr 16);
  1280. RawRecord.RawData[NextOfs+3]:=Byte(TargetDisplacement shr 24);
  1281. Inc(NextOfs,4);
  1282. end
  1283. else
  1284. begin
  1285. if TargetDisplacement>$ffff then
  1286. internalerror(2015040502);
  1287. RawRecord.RawData[NextOfs]:=Byte(TargetDisplacement);
  1288. RawRecord.RawData[NextOfs+1]:=Byte(TargetDisplacement shr 8);
  1289. Inc(NextOfs,2);
  1290. end;
  1291. end;
  1292. end
  1293. else
  1294. begin
  1295. { physical start address }
  1296. RawRecord.RawData[NextOfs]:=Byte(PhysFrameNumber);
  1297. RawRecord.RawData[NextOfs+1]:=Byte(PhysFrameNumber shr 8);
  1298. Inc(NextOfs,2);
  1299. if Is32Bit then
  1300. begin
  1301. RawRecord.RawData[NextOfs]:=Byte(PhysOffset);
  1302. RawRecord.RawData[NextOfs+1]:=Byte(PhysOffset shr 8);
  1303. RawRecord.RawData[NextOfs+2]:=Byte(PhysOffset shr 16);
  1304. RawRecord.RawData[NextOfs+3]:=Byte(PhysOffset shr 24);
  1305. Inc(NextOfs,4);
  1306. end
  1307. else
  1308. begin
  1309. if PhysOffset>$ffff then
  1310. internalerror(2015040502);
  1311. RawRecord.RawData[NextOfs]:=Byte(PhysOffset);
  1312. RawRecord.RawData[NextOfs+1]:=Byte(PhysOffset shr 8);
  1313. Inc(NextOfs,2);
  1314. end;
  1315. end;
  1316. end;
  1317. RawRecord.RecordLength:=NextOfs+1;
  1318. RawRecord.CalculateChecksumByte;
  1319. end;
  1320. { TOmfSubRecord_FIXUP }
  1321. function TOmfSubRecord_FIXUP.GetDataRecordOffset: Integer;
  1322. begin
  1323. Result:=FLocationOffset-FDataRecordStartOffset;
  1324. end;
  1325. function TOmfSubRecord_FIXUP.GetLocationSize: Integer;
  1326. const
  1327. OmfLocationType2Size: array [TOmfFixupLocationType] of Integer=
  1328. (1, // fltLoByte
  1329. 2, // fltOffset
  1330. 2, // fltBase
  1331. 4, // fltFarPointer
  1332. 1, // fltHiByte
  1333. 2, // fltLoaderResolvedOffset (PharLap: Offset32)
  1334. 0, // fltUndefined6 (PharLap: Pointer48)
  1335. 0, // fltUndefined7
  1336. 0, // fltUndefined8
  1337. 4, // fltOffset32
  1338. 0, // fltUndefined10
  1339. 6, // fltFarPointer48
  1340. 0, // fltUndefined12
  1341. 4, // fltLoaderResolvedOffset32
  1342. 0, // fltUndefined14
  1343. 0); // fltUndefined15
  1344. begin
  1345. Result:=OmfLocationType2Size[LocationType];
  1346. end;
  1347. procedure TOmfSubRecord_FIXUP.SetDataRecordOffset(AValue: Integer);
  1348. begin
  1349. FLocationOffset:=AValue+FDataRecordStartOffset;
  1350. end;
  1351. function TOmfSubRecord_FIXUP.ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  1352. var
  1353. Locat: Word;
  1354. FixData: Byte;
  1355. begin
  1356. if (Offset+2)>=RawRecord.RecordLength then
  1357. internalerror(2015040504);
  1358. { unlike other fields in the OMF format, this one is big endian }
  1359. Locat:=(RawRecord.RawData[Offset] shl 8) or RawRecord.RawData[Offset+1];
  1360. FixData:=RawRecord.RawData[Offset+2];
  1361. Inc(Offset,3);
  1362. if (Locat and $8000)=0 then
  1363. internalerror(2015040503);
  1364. DataRecordOffset:=Locat and $3FF;
  1365. LocationType:=TOmfFixupLocationType((Locat shr 10) and 15);
  1366. Mode:=TOmfFixupMode((Locat shr 14) and 1);
  1367. FrameDeterminedByThread:=(FixData and $80)<>0;
  1368. TargetDeterminedByThread:=(FixData and $08)<>0;
  1369. if FrameDeterminedByThread then
  1370. FrameThread:=TOmfFixupThread((FixData shr 4) and 3)
  1371. else
  1372. FrameMethod:=TOmfFixupFrameMethod((FixData shr 4) and 7);
  1373. if TargetDeterminedByThread then
  1374. begin
  1375. TargetThread:=TOmfFixupThread(FixData and 3);
  1376. TargetThreadDisplacementPresent:=(FixData and $40)=0;
  1377. end
  1378. else
  1379. TargetMethod:=TOmfFixupTargetMethod(FixData and 7);
  1380. { read Frame Datum? }
  1381. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  1382. Offset:=RawRecord.ReadIndexedRef(Offset,FFrameDatum)
  1383. else
  1384. FrameDatum:=0;
  1385. { read Target Datum? }
  1386. if not TargetDeterminedByThread then
  1387. Offset:=RawRecord.ReadIndexedRef(Offset,FTargetDatum)
  1388. else
  1389. TargetDatum:=0;
  1390. { read Target Displacement? }
  1391. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  1392. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  1393. begin
  1394. if Is32Bit then
  1395. begin
  1396. if (Offset+3)>=RawRecord.RecordLength then
  1397. internalerror(2015040504);
  1398. TargetDisplacement := RawRecord.RawData[Offset]+
  1399. (RawRecord.RawData[Offset+1] shl 8)+
  1400. (RawRecord.RawData[Offset+2] shl 16)+
  1401. (RawRecord.RawData[Offset+3] shl 24);
  1402. Inc(Offset,4);
  1403. end
  1404. else
  1405. begin
  1406. if (Offset+1)>=RawRecord.RecordLength then
  1407. internalerror(2015040504);
  1408. TargetDisplacement := RawRecord.RawData[Offset]+
  1409. (RawRecord.RawData[Offset+1] shl 8);
  1410. Inc(Offset,2);
  1411. end;
  1412. end;
  1413. Result:=Offset;
  1414. end;
  1415. function TOmfSubRecord_FIXUP.WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  1416. var
  1417. Locat: Word;
  1418. FixData: Byte;
  1419. begin
  1420. if (DataRecordOffset<0) or (DataRecordOffset>1023) then
  1421. internalerror(2015040501);
  1422. Locat:=$8000+(Ord(Mode) shl 14)+(Ord(LocationType) shl 10)+DataRecordOffset;
  1423. { unlike other fields in the OMF format, this one is big endian }
  1424. RawRecord.RawData[Offset]:=Byte(Locat shr 8);
  1425. RawRecord.RawData[Offset+1]:=Byte(Locat);
  1426. Inc(Offset, 2);
  1427. FixData:=(Ord(FrameDeterminedByThread) shl 7)+(Ord(TargetDeterminedByThread) shl 3);
  1428. if FrameDeterminedByThread then
  1429. FixData:=FixData+(Ord(FrameThread) shl 4)
  1430. else
  1431. FixData:=FixData+(Ord(FrameMethod) shl 4);
  1432. if TargetDeterminedByThread then
  1433. FixData:=FixData+Ord(TargetThread)+(Ord(not TargetThreadDisplacementPresent) shl 2)
  1434. else
  1435. FixData:=FixData+Ord(TargetMethod);
  1436. RawRecord.RawData[Offset]:=FixData;
  1437. Inc(Offset);
  1438. { save Frame Datum? }
  1439. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  1440. Offset:=RawRecord.WriteIndexedRef(Offset,FrameDatum);
  1441. { save Target Datum? }
  1442. if not TargetDeterminedByThread then
  1443. Offset:=RawRecord.WriteIndexedRef(Offset,TargetDatum);
  1444. { save Target Displacement? }
  1445. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  1446. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  1447. begin
  1448. if Is32Bit then
  1449. begin
  1450. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1451. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1452. RawRecord.RawData[Offset+2]:=Byte(TargetDisplacement shr 16);
  1453. RawRecord.RawData[Offset+3]:=Byte(TargetDisplacement shr 24);
  1454. Inc(Offset,4);
  1455. end
  1456. else
  1457. begin
  1458. if TargetDisplacement>$ffff then
  1459. internalerror(2015040502);
  1460. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1461. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1462. Inc(Offset,2);
  1463. end;
  1464. end;
  1465. Result:=Offset;
  1466. end;
  1467. { TOmfRecord_LIBHEAD }
  1468. constructor TOmfRecord_LIBHEAD.Create;
  1469. begin
  1470. PageSize:=512;
  1471. DictionarySizeInBlocks:=2;
  1472. CaseSensitive:=true;
  1473. end;
  1474. procedure TOmfRecord_LIBHEAD.SetPageSize(AValue: Integer);
  1475. var
  1476. p: longint;
  1477. begin
  1478. { valid library page sizes are powers of two, between 2**4 and 2**15 }
  1479. if not ispowerof2(AValue,p) then
  1480. internalerror(2015041802);
  1481. if (p<4) or (p>15) then
  1482. internalerror(2015041802);
  1483. FPageSize:=AValue;
  1484. end;
  1485. procedure TOmfRecord_LIBHEAD.DecodeFrom(RawRecord: TOmfRawRecord);
  1486. begin
  1487. if RawRecord.RecordType<>RT_LIBHEAD then
  1488. internalerror(2015040301);
  1489. { this will also range check PageSize and will ensure that RecordLength>=13 }
  1490. PageSize:=RawRecord.RecordLength+3;
  1491. DictionaryOffset:=RawRecord.RawData[0]+
  1492. (RawRecord.RawData[1] shl 8)+
  1493. (RawRecord.RawData[2] shl 16)+
  1494. (RawRecord.RawData[3] shl 24);
  1495. DictionarySizeInBlocks:=RawRecord.RawData[4]+
  1496. (RawRecord.RawData[5] shl 8);
  1497. Flags:=RawRecord.RawData[6];
  1498. end;
  1499. procedure TOmfRecord_LIBHEAD.EncodeTo(RawRecord: TOmfRawRecord);
  1500. begin
  1501. { make sure the LIBHEAD record is padded with zeros at the end }
  1502. FillChar(RawRecord.RawData,SizeOf(RawRecord.RawData),0);
  1503. RawRecord.RecordType:=RT_LIBHEAD;
  1504. RawRecord.RecordLength:=PageSize-3;
  1505. RawRecord.RawData[0]:=Byte(DictionaryOffset);
  1506. RawRecord.RawData[1]:=Byte(DictionaryOffset shr 8);
  1507. RawRecord.RawData[2]:=Byte(DictionaryOffset shr 16);
  1508. RawRecord.RawData[3]:=Byte(DictionaryOffset shr 24);
  1509. RawRecord.RawData[4]:=Byte(DictionarySizeInBlocks);
  1510. RawRecord.RawData[5]:=Byte(DictionarySizeInBlocks shr 8);
  1511. RawRecord.RawData[6]:=Flags;
  1512. { the LIBHEAD record contains no checksum byte, so no need to call
  1513. RawRecord.CalculateChecksumByte }
  1514. end;
  1515. function TOmfRecord_LIBHEAD.IsCaseSensitive: Boolean;
  1516. begin
  1517. Result:=(FFlags and 1)<>0;
  1518. end;
  1519. procedure TOmfRecord_LIBHEAD.SetCaseSensitive(AValue: Boolean);
  1520. begin
  1521. FFlags:=(FFlags and $FE) or Ord(AValue);
  1522. end;
  1523. { TOmfRecord_LIBEND }
  1524. procedure TOmfRecord_LIBEND.DecodeFrom(RawRecord: TOmfRawRecord);
  1525. begin
  1526. if RawRecord.RecordType<>RT_LIBEND then
  1527. internalerror(2015040301);
  1528. FPaddingBytes:=RawRecord.RecordLength;
  1529. end;
  1530. procedure TOmfRecord_LIBEND.EncodeTo(RawRecord: TOmfRawRecord);
  1531. begin
  1532. { make sure the LIBEND record is padded with zeros at the end }
  1533. FillChar(RawRecord.RawData,SizeOf(RawRecord.RawData),0);
  1534. RawRecord.RecordType:=RT_LIBEND;
  1535. RawRecord.RecordLength:=FPaddingBytes;
  1536. { the LIBEND record contains no checksum byte, so no need to call
  1537. RawRecord.CalculateChecksumByte }
  1538. end;
  1539. procedure TOmfRecord_LIBEND.CalculatePaddingBytes(RecordStartOffset: DWord);
  1540. var
  1541. DictionaryStartOffset: Integer;
  1542. begin
  1543. { padding must be calculated, so that the dictionary begins on a 512-byte boundary }
  1544. Inc(RecordStartOffset,3); // padding begins _after_ the record header (3 bytes)
  1545. DictionaryStartOffset:=(RecordStartOffset+511) and $fffffe00;
  1546. PaddingBytes:=DictionaryStartOffset-RecordStartOffset;
  1547. end;
  1548. function compute_omf_lib_hash(const name: string; blocks: Integer): TOmfLibHash;
  1549. const
  1550. blank=$20; // ASCII blank
  1551. nbuckets=37;
  1552. var
  1553. block_x: Integer;
  1554. block_d: Integer;
  1555. bucket_x: Integer;
  1556. bucket_d: Integer;
  1557. len: Integer;
  1558. pbidx,peidx: Integer;
  1559. cback,cfront: Byte;
  1560. begin
  1561. len:=Length(name);
  1562. if len=0 then
  1563. internalerror(2015041801);
  1564. pbidx:=1;
  1565. peidx:=len+1;
  1566. { left to right scan }
  1567. block_x:=len or blank;
  1568. bucket_d:=block_x;
  1569. { right to left scan }
  1570. block_d:=0;
  1571. bucket_x:=0;
  1572. while true do
  1573. begin
  1574. { blank -> convert to LC }
  1575. Dec(peidx);
  1576. cback:=Byte(name[peidx]) or blank;
  1577. bucket_x:=RorWord(bucket_x,2) xor cback;
  1578. block_d:=RolWord(block_d,2) xor cback;
  1579. Dec(len);
  1580. if len=0 then
  1581. break;
  1582. cfront:=Byte(name[pbidx]) or blank;
  1583. Inc(pbidx);
  1584. block_x:=RolWord(block_x,2) xor cfront;
  1585. bucket_d:=RorWord(bucket_d,2) xor cfront;
  1586. end;
  1587. Result.block_x:=block_x mod blocks;
  1588. Result.block_d:=max(block_d mod blocks,1);
  1589. Result.bucket_x:=bucket_x mod nbuckets;
  1590. Result.bucket_d:=max(bucket_d mod nbuckets,1);
  1591. end;
  1592. end.