omfbase.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. { returns whether the specified section type belongs to the group DGROUP in
  579. the current memory model. DGROUP is the segment group pointed by DS }
  580. function section_belongs_to_dgroup(atype:TAsmSectiontype): Boolean;
  581. implementation
  582. uses
  583. cutils,
  584. globals,globtype,
  585. verbose,
  586. cpuinfo;
  587. { TOmfOrderedNameCollection }
  588. function TOmfOrderedNameCollection.GetString(Index: Integer): string;
  589. begin
  590. Result:=FStringList[Index-1];
  591. end;
  592. function TOmfOrderedNameCollection.GetCount: Integer;
  593. begin
  594. Result:=Length(FStringList);
  595. end;
  596. procedure TOmfOrderedNameCollection.SetString(Index: Integer; AValue: string);
  597. begin
  598. FStringList[Index-1]:=AValue;
  599. end;
  600. function TOmfOrderedNameCollection.Add(const S: string): Integer;
  601. begin
  602. Result:=Length(FStringList)+1;
  603. SetLength(FStringList,Result);
  604. FStringList[Result-1]:=S;
  605. end;
  606. procedure TOmfOrderedNameCollection.Clear;
  607. begin
  608. SetLength(FStringList,0);
  609. end;
  610. { TOmfRawRecord }
  611. function TOmfRawRecord.GetRecordType: Byte;
  612. begin
  613. Result:=RawData[-3];
  614. end;
  615. procedure TOmfRawRecord.SetRecordType(AValue: Byte);
  616. begin
  617. RawData[-3]:=AValue;
  618. end;
  619. function TOmfRawRecord.GetRecordLength: Word;
  620. begin
  621. Result:=RawData[-2] or (RawData[-1] shl 8);
  622. end;
  623. procedure TOmfRawRecord.SetRecordLength(AValue: Word);
  624. begin
  625. RawData[-2]:=Byte(AValue);
  626. RawData[-1]:=Byte(AValue shr 8);
  627. end;
  628. function TOmfRawRecord.ReadStringAt(Offset: Integer; out s: string): Integer;
  629. var
  630. len: Byte;
  631. begin
  632. len:=RawData[Offset];
  633. Result:=Offset+len+1;
  634. if result>RecordLength then
  635. internalerror(2015033103);
  636. SetLength(s, len);
  637. UniqueString(s);
  638. Move(RawData[Offset+1],s[1],len);
  639. end;
  640. function TOmfRawRecord.WriteStringAt(Offset: Integer; s: string): Integer;
  641. begin
  642. if Length(s)>255 then
  643. internalerror(2015033101);
  644. result:=Offset+Length(s)+1;
  645. if result>High(RawData) then
  646. internalerror(2015033102);
  647. RawData[Offset]:=Length(s);
  648. Move(s[1], RawData[Offset+1], Length(s));
  649. end;
  650. function TOmfRawRecord.ReadIndexedRef(Offset: Integer; out IndexedRef: Integer): Integer;
  651. begin
  652. Result:=Offset+1;
  653. if result>RecordLength then
  654. internalerror(2015033103);
  655. IndexedRef:=RawData[Offset];
  656. if IndexedRef<=$7f then
  657. exit;
  658. Result:=Offset+2;
  659. if result>RecordLength then
  660. internalerror(2015033103);
  661. IndexedRef:=((IndexedRef and $7f) shl 8)+RawData[Offset+1];
  662. end;
  663. function TOmfRawRecord.WriteIndexedRef(Offset: Integer; IndexedRef: Integer): Integer;
  664. begin
  665. if (IndexedRef<0) or (IndexedRef>$7FFF) then
  666. internalerror(2015040303);
  667. if IndexedRef<=$7f then
  668. begin
  669. Result:=Offset+1;
  670. if Result>High(RawData) then
  671. internalerror(2015033102);
  672. RawData[Offset]:=IndexedRef;
  673. end
  674. else
  675. begin
  676. Result:=Offset+2;
  677. if Result>High(RawData) then
  678. internalerror(2015033102);
  679. RawData[Offset]:=$80+(IndexedRef shr 8);
  680. RawData[Offset+1]:=Byte(IndexedRef);
  681. end;
  682. end;
  683. function TOmfRawRecord.GetChecksumByte: Byte;
  684. begin
  685. if RecordLength>0 then
  686. Result:=RawData[RecordLength-1]
  687. else
  688. Result:=0;
  689. end;
  690. procedure TOmfRawRecord.SetChecksumByte(AValue: Byte);
  691. begin
  692. if RecordLength>0 then
  693. RawData[RecordLength-1]:=AValue;
  694. end;
  695. procedure TOmfRawRecord.CalculateChecksumByte;
  696. var
  697. I: Integer;
  698. b: Byte;
  699. begin
  700. b:=0;
  701. for I:=-3 to RecordLength-2 do
  702. b:=byte(b+RawData[I]);
  703. SetChecksumByte($100-b);
  704. end;
  705. function TOmfRawRecord.VerifyChecksumByte: boolean;
  706. var
  707. I: Integer;
  708. b: Byte;
  709. begin
  710. { according to the OMF spec, some tools always write a 0 rather than
  711. computing the checksum, so it should also be accepted as correct }
  712. if ChecksumByte=0 then
  713. exit(true);
  714. b:=0;
  715. for I:=-3 to RecordLength-1 do
  716. b:=byte(b+RawData[I]);
  717. Result:=(b=0);
  718. end;
  719. procedure TOmfRawRecord.ReadFrom(aReader: TObjectReader);
  720. begin
  721. aReader.read(RawData, 3);
  722. aReader.read(RawData[0], RecordLength);
  723. end;
  724. procedure TOmfRawRecord.ReadFrom(aReader: TDynamicArray);
  725. begin
  726. aReader.read(RawData, 3);
  727. aReader.read(RawData[0], RecordLength);
  728. end;
  729. procedure TOmfRawRecord.WriteTo(aWriter: TObjectWriter);
  730. begin
  731. aWriter.write(RawData, RecordLength+3);
  732. end;
  733. procedure TOmfRawRecord.WriteTo(aWriter: TDynamicArray);
  734. begin
  735. aWriter.write(RawData, RecordLength+3);
  736. end;
  737. { TOmfRecord_THEADR }
  738. procedure TOmfRecord_THEADR.DecodeFrom(RawRecord: TOmfRawRecord);
  739. begin
  740. if RawRecord.RecordType<>RT_THEADR then
  741. internalerror(2015040301);
  742. RawRecord.ReadStringAt(0,FModuleName);
  743. end;
  744. procedure TOmfRecord_THEADR.EncodeTo(RawRecord: TOmfRawRecord);
  745. var
  746. NextOfs: Integer;
  747. begin
  748. RawRecord.RecordType:=RT_THEADR;
  749. NextOfs:=RawRecord.WriteStringAt(0,ModuleName);
  750. RawRecord.RecordLength:=NextOfs+1;
  751. RawRecord.CalculateChecksumByte;
  752. end;
  753. { TOmfRecord_COMENT }
  754. function TOmfRecord_COMENT.GetNoList: Boolean;
  755. begin
  756. Result:=(CommentType and $40)<>0;
  757. end;
  758. function TOmfRecord_COMENT.GetNoPurge: Boolean;
  759. begin
  760. Result:=(CommentType and $80)<>0;
  761. end;
  762. procedure TOmfRecord_COMENT.SetNoList(AValue: Boolean);
  763. begin
  764. if AValue then
  765. CommentType:=CommentType or $40
  766. else
  767. CommentType:=CommentType and $BF;
  768. end;
  769. procedure TOmfRecord_COMENT.SetNoPurge(AValue: Boolean);
  770. begin
  771. if AValue then
  772. CommentType:=CommentType or $80
  773. else
  774. CommentType:=CommentType and $7F;
  775. end;
  776. procedure TOmfRecord_COMENT.DecodeFrom(RawRecord: TOmfRawRecord);
  777. begin
  778. if RawRecord.RecordType<>RT_COMENT then
  779. internalerror(2015040301);
  780. if RawRecord.RecordLength<3 then
  781. internalerror(2015033104);
  782. CommentType:=RawRecord.RawData[0];
  783. CommentClass:=RawRecord.RawData[1];
  784. SetLength(FCommentString,RawRecord.RecordLength-3);
  785. UniqueString(FCommentString);
  786. Move(RawRecord.RawData[2],FCommentString[1],Length(FCommentString));
  787. end;
  788. procedure TOmfRecord_COMENT.EncodeTo(RawRecord: TOmfRawRecord);
  789. begin
  790. RawRecord.RecordType:=RT_COMENT;
  791. if (Length(FCommentString)+3)>High(RawRecord.RawData) then
  792. internalerror(2015033105);
  793. RawRecord.RecordLength:=Length(FCommentString)+3;
  794. RawRecord.RawData[0]:=CommentType;
  795. RawRecord.RawData[1]:=CommentClass;
  796. Move(FCommentString[1],RawRecord.RawData[2],Length(FCommentString));
  797. RawRecord.CalculateChecksumByte;
  798. end;
  799. { TOmfRecord_LNAMES }
  800. constructor TOmfRecord_LNAMES.Create;
  801. begin
  802. FNextIndex:=1;
  803. end;
  804. procedure TOmfRecord_LNAMES.DecodeFrom(RawRecord: TOmfRawRecord);
  805. var
  806. NextOfs: Integer;
  807. Name: string;
  808. begin
  809. if RawRecord.RecordType<>RT_LNAMES then
  810. internalerror(2015040301);
  811. NextOfs:=0;
  812. while NextOfs<(RawRecord.RecordLength-1) do
  813. begin
  814. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  815. Names.Add(Name);
  816. end;
  817. end;
  818. procedure TOmfRecord_LNAMES.EncodeTo(RawRecord: TOmfRawRecord);
  819. const
  820. RecordLengthLimit = 1024;
  821. var
  822. Len,LastIncludedIndex,NextOfs,I: Integer;
  823. begin
  824. RawRecord.RecordType:=RT_LNAMES;
  825. { find out how many strings can we include until we reach the length limit }
  826. Len:=1;
  827. LastIncludedIndex:=NextIndex-1;
  828. repeat
  829. Inc(LastIncludedIndex);
  830. Inc(Len,Length(Names[LastIncludedIndex])+1);
  831. until (LastIncludedIndex>=Names.Count) or ((Len+Length(Names[LastIncludedIndex+1])+1)>=RecordLengthLimit);
  832. { write the strings... }
  833. NextOfs:=0;
  834. for I:=NextIndex to LastIncludedIndex do
  835. NextOfs:=RawRecord.WriteStringAt(NextOfs,Names[I]);
  836. RawRecord.RecordLength:=Len;
  837. RawRecord.CalculateChecksumByte;
  838. { update NextIndex }
  839. NextIndex:=LastIncludedIndex+1;
  840. end;
  841. { TOmfRecord_SEGDEF }
  842. procedure TOmfRecord_SEGDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  843. var
  844. B: Byte;
  845. Big: Boolean;
  846. NextOfs: Integer;
  847. MinLen: Integer;
  848. begin
  849. if not (RawRecord.RecordType in [RT_SEGDEF,RT_SEGDEF32]) then
  850. internalerror(2015040301);
  851. Is32Bit:=RawRecord.RecordType=RT_SEGDEF32;
  852. MinLen:=7; { b(1)+seglength(2..4)+segnameindex(1..2)+classnameindex(1..2)+overlaynameindex(1..2)+checksum }
  853. if Is32Bit then
  854. inc(MinLen,2);
  855. if RawRecord.RecordLength<MinLen then
  856. internalerror(2015040305);
  857. B:=RawRecord.RawData[0];
  858. Alignment:=TOmfSegmentAlignment(B shr 5);
  859. Combination:=TOmfSegmentCombination((B shr 2) and 7);
  860. Big:=(B and 2)<>0;
  861. Use:=TOmfSegmentUse(B and 1);
  862. NextOfs:=1;
  863. if Alignment=saAbsolute then
  864. begin
  865. inc(MinLen,3);
  866. if RawRecord.RecordLength<MinLen then
  867. internalerror(2015040305);
  868. FrameNumber:=RawRecord.RawData[1]+(RawRecord.RawData[2] shl 8);
  869. Offset:=RawRecord.RawData[3];
  870. NextOfs:=4;
  871. end
  872. else
  873. begin
  874. FrameNumber:=0;
  875. Offset:=0;
  876. end;
  877. if Is32Bit then
  878. begin
  879. SegmentLength:=RawRecord.RawData[NextOfs]+
  880. (RawRecord.RawData[NextOfs+1] shl 8)+
  881. (RawRecord.RawData[NextOfs+2] shl 16)+
  882. (RawRecord.RawData[NextOfs+3] shl 24);
  883. if Big then
  884. if SegmentLength=0 then
  885. SegmentLength:=4294967296
  886. else
  887. internalerror(2015040306);
  888. Inc(NextOfs,4);
  889. end
  890. else
  891. begin
  892. SegmentLength:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  893. if Big then
  894. if SegmentLength=0 then
  895. SegmentLength:=65536
  896. else
  897. internalerror(2015040306);
  898. Inc(NextOfs,2);
  899. end;
  900. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FSegmentNameIndex);
  901. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FClassNameIndex);
  902. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FOverlayNameIndex);
  903. end;
  904. procedure TOmfRecord_SEGDEF.EncodeTo(RawRecord: TOmfRawRecord);
  905. var
  906. Big: Boolean;
  907. NextOfs: Integer;
  908. begin
  909. if Is32Bit then
  910. begin
  911. RawRecord.RecordType:=RT_SEGDEF32;
  912. if SegmentLength>4294967296 then
  913. internalerror(2015040302);
  914. Big:=SegmentLength=4294967296;
  915. end
  916. else
  917. begin
  918. RawRecord.RecordType:=RT_SEGDEF;
  919. if SegmentLength>65536 then
  920. internalerror(2015040302);
  921. Big:=SegmentLength=65536;
  922. end;
  923. RawRecord.RawData[0]:=(Ord(Alignment) shl 5) or (Ord(Combination) shl 2) or (Ord(Big) shl 1) or Ord(Use);
  924. NextOfs:=1;
  925. if Alignment=saAbsolute then
  926. begin
  927. RawRecord.RawData[1]:=Byte(FrameNumber);
  928. RawRecord.RawData[2]:=Byte(FrameNumber shr 8);
  929. RawRecord.RawData[3]:=Offset;
  930. NextOfs:=4;
  931. end;
  932. if Is32Bit then
  933. begin
  934. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  935. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  936. RawRecord.RawData[NextOfs+2]:=Byte(SegmentLength shr 16);
  937. RawRecord.RawData[NextOfs+3]:=Byte(SegmentLength shr 24);
  938. Inc(NextOfs,4);
  939. end
  940. else
  941. begin
  942. RawRecord.RawData[NextOfs]:=Byte(SegmentLength);
  943. RawRecord.RawData[NextOfs+1]:=Byte(SegmentLength shr 8);
  944. Inc(NextOfs,2);
  945. end;
  946. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,SegmentNameIndex);
  947. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ClassNameIndex);
  948. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,OverlayNameIndex);
  949. RawRecord.RecordLength:=NextOfs+1;
  950. RawRecord.CalculateChecksumByte;
  951. end;
  952. { TOmfRecord_GRPDEF }
  953. procedure TOmfRecord_GRPDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  954. var
  955. NextOfs: Integer;
  956. Segment: Integer;
  957. begin
  958. if RawRecord.RecordType<>RT_GRPDEF then
  959. internalerror(2015040301);
  960. NextOfs:=RawRecord.ReadIndexedRef(0,FGroupNameIndex);
  961. SetLength(FSegmentList,0);
  962. while NextOfs<RawRecord.RecordLength-1 do
  963. begin
  964. if RawRecord.RawData[NextOfs]<>$ff then
  965. internalerror(2015040901);
  966. NextOfs:=RawRecord.ReadIndexedRef(NextOfs+1,Segment);
  967. SetLength(FSegmentList,Length(FSegmentList)+1);
  968. FSegmentList[High(FSegmentList)]:=Segment;
  969. end;
  970. end;
  971. procedure TOmfRecord_GRPDEF.EncodeTo(RawRecord: TOmfRawRecord);
  972. var
  973. NextOfs: Integer;
  974. Segment: Integer;
  975. begin
  976. RawRecord.RecordType:=RT_GRPDEF;
  977. NextOfs:=RawRecord.WriteIndexedRef(0,GroupNameIndex);
  978. for Segment in SegmentList do
  979. begin
  980. if NextOfs>High(RawRecord.RawData) then
  981. internalerror(2015040401);
  982. RawRecord.RawData[NextOfs]:=$ff;
  983. NextOfs:=RawRecord.WriteIndexedRef(NextOfs+1,Segment);
  984. end;
  985. RawRecord.RecordLength:=NextOfs+1;
  986. RawRecord.CalculateChecksumByte;
  987. end;
  988. { TOmfPublicNameElement }
  989. function TOmfPublicNameElement.GetLengthInFile(Is32Bit: Boolean): Integer;
  990. begin
  991. Result:=1+Length(Name)+2+1;
  992. if Is32Bit then
  993. Inc(Result,2);
  994. if TypeIndex>=$80 then
  995. Inc(Result);
  996. end;
  997. { TOmfRecord_PUBDEF }
  998. procedure TOmfRecord_PUBDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  999. var
  1000. NextOfs: Integer;
  1001. Name: string;
  1002. TypeIndex: Integer;
  1003. PublicOffset: DWord;
  1004. PubName: TOmfPublicNameElement;
  1005. begin
  1006. if not (RawRecord.RecordType in [RT_PUBDEF,RT_PUBDEF32]) then
  1007. internalerror(2015040301);
  1008. Is32Bit:=RawRecord.RecordType=RT_PUBDEF32;
  1009. NextOfs:=RawRecord.ReadIndexedRef(0,FBaseGroupIndex);
  1010. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FBaseSegmentIndex);
  1011. if BaseSegmentIndex=0 then
  1012. begin
  1013. if (NextOfs+1)>=RawRecord.RecordLength then
  1014. internalerror(2015041401);
  1015. BaseFrame:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1016. Inc(NextOfs,2);
  1017. end
  1018. else
  1019. BaseFrame:=0;
  1020. while NextOfs<(RawRecord.RecordLength-1) do
  1021. begin
  1022. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  1023. if Is32Bit then
  1024. begin
  1025. if (NextOfs+3)>=RawRecord.RecordLength then
  1026. internalerror(2015041401);
  1027. PublicOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8)+
  1028. (RawRecord.RawData[NextOfs+2] shl 16)+(RawRecord.RawData[NextOfs+3] shl 24);
  1029. Inc(NextOfs,4);
  1030. end
  1031. else
  1032. begin
  1033. if (NextOfs+1)>=RawRecord.RecordLength then
  1034. internalerror(2015041401);
  1035. PublicOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1036. Inc(NextOfs,2);
  1037. end;
  1038. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,TypeIndex);
  1039. PubName:=TOmfPublicNameElement.Create(PublicNames,Name);
  1040. PubName.PublicOffset:=PublicOffset;
  1041. PubName.TypeIndex:=TypeIndex;
  1042. end;
  1043. end;
  1044. procedure TOmfRecord_PUBDEF.EncodeTo(RawRecord: TOmfRawRecord);
  1045. const
  1046. RecordLengthLimit = 1024;
  1047. var
  1048. Len,LastIncludedIndex,NextOfs,I: Integer;
  1049. PubName: TOmfPublicNameElement;
  1050. begin
  1051. if Is32Bit then
  1052. RawRecord.RecordType:=RT_PUBDEF32
  1053. else
  1054. RawRecord.RecordType:=RT_PUBDEF;
  1055. NextOfs:=RawRecord.WriteIndexedRef(0,BaseGroupIndex);
  1056. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,BaseSegmentIndex);
  1057. if BaseSegmentIndex=0 then
  1058. begin
  1059. RawRecord.RawData[NextOfs]:=Byte(BaseFrame);
  1060. RawRecord.RawData[NextOfs+1]:=Byte(BaseFrame shr 8);
  1061. Inc(NextOfs,2);
  1062. end;
  1063. { find out how many public names can we include until we reach the length limit }
  1064. Len:=NextOfs;
  1065. LastIncludedIndex:=NextIndex-1;
  1066. repeat
  1067. Inc(LastIncludedIndex);
  1068. Inc(Len,TOmfPublicNameElement(PublicNames[LastIncludedIndex]).GetLengthInFile(Is32Bit));
  1069. until (LastIncludedIndex>=(PublicNames.Count-1)) or ((Len+TOmfPublicNameElement(PublicNames[LastIncludedIndex+1]).GetLengthInFile(Is32Bit))>=RecordLengthLimit);
  1070. { write the public names... }
  1071. for I:=NextIndex to LastIncludedIndex do
  1072. begin
  1073. PubName:=TOmfPublicNameElement(PublicNames[I]);
  1074. NextOfs:=RawRecord.WriteStringAt(NextOfs,PubName.Name);
  1075. if Is32Bit then
  1076. begin
  1077. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  1078. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  1079. RawRecord.RawData[NextOfs+2]:=Byte(PubName.PublicOffset shr 16);
  1080. RawRecord.RawData[NextOfs+3]:=Byte(PubName.PublicOffset shr 24);
  1081. Inc(NextOfs,4);
  1082. end
  1083. else
  1084. begin
  1085. if PubName.PublicOffset>$ffff then
  1086. internalerror(2015041403);
  1087. RawRecord.RawData[NextOfs]:=Byte(PubName.PublicOffset);
  1088. RawRecord.RawData[NextOfs+1]:=Byte(PubName.PublicOffset shr 8);
  1089. Inc(NextOfs,2);
  1090. end;
  1091. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,PubName.TypeIndex);
  1092. end;
  1093. RawRecord.RecordLength:=Len+1;
  1094. RawRecord.CalculateChecksumByte;
  1095. { update NextIndex }
  1096. NextIndex:=LastIncludedIndex+1;
  1097. end;
  1098. { TOmfExternalNameElement }
  1099. function TOmfExternalNameElement.GetLengthInFile: Integer;
  1100. begin
  1101. Result:=1+Length(Name)+1;
  1102. if TypeIndex>=$80 then
  1103. Inc(Result);
  1104. end;
  1105. { TOmfRecord_EXTDEF }
  1106. procedure TOmfRecord_EXTDEF.DecodeFrom(RawRecord: TOmfRawRecord);
  1107. var
  1108. NextOfs: Integer;
  1109. Name: string;
  1110. TypeIndex: Integer;
  1111. ExtName: TOmfExternalNameElement;
  1112. begin
  1113. if RawRecord.RecordType<>RT_EXTDEF then
  1114. internalerror(2015040301);
  1115. NextOfs:=0;
  1116. while NextOfs<(RawRecord.RecordLength-1) do
  1117. begin
  1118. NextOfs:=RawRecord.ReadStringAt(NextOfs,Name);
  1119. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,TypeIndex);
  1120. ExtName:=TOmfExternalNameElement.Create(ExternalNames,Name);
  1121. ExtName.TypeIndex:=TypeIndex;
  1122. end;
  1123. end;
  1124. procedure TOmfRecord_EXTDEF.EncodeTo(RawRecord: TOmfRawRecord);
  1125. const
  1126. RecordLengthLimit = 1024;
  1127. var
  1128. Len,LastIncludedIndex,NextOfs,I: Integer;
  1129. ExtName: TOmfExternalNameElement;
  1130. begin
  1131. RawRecord.RecordType:=RT_EXTDEF;
  1132. NextOfs:=0;
  1133. { find out how many external names can we include until we reach the length limit }
  1134. Len:=NextOfs;
  1135. LastIncludedIndex:=NextIndex-1;
  1136. repeat
  1137. Inc(LastIncludedIndex);
  1138. Inc(Len,TOmfExternalNameElement(ExternalNames[LastIncludedIndex]).GetLengthInFile);
  1139. until (LastIncludedIndex>=(ExternalNames.Count-1)) or ((Len+TOmfExternalNameElement(ExternalNames[LastIncludedIndex+1]).GetLengthInFile)>=RecordLengthLimit);
  1140. { write the external names... }
  1141. for I:=NextIndex to LastIncludedIndex do
  1142. begin
  1143. ExtName:=TOmfExternalNameElement(ExternalNames[I]);
  1144. NextOfs:=RawRecord.WriteStringAt(NextOfs,ExtName.Name);
  1145. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,ExtName.TypeIndex);
  1146. end;
  1147. RawRecord.RecordLength:=Len+1;
  1148. RawRecord.CalculateChecksumByte;
  1149. { update NextIndex }
  1150. NextIndex:=LastIncludedIndex+1;
  1151. end;
  1152. { TOmfRecord_MODEND }
  1153. procedure TOmfRecord_MODEND.DecodeFrom(RawRecord: TOmfRawRecord);
  1154. var
  1155. ModTyp: Byte;
  1156. NextOfs: Integer;
  1157. EndData: Byte;
  1158. begin
  1159. if not (RawRecord.RecordType in [RT_MODEND,RT_MODEND32]) then
  1160. internalerror(2015040301);
  1161. Is32Bit:=RawRecord.RecordType=RT_MODEND32;
  1162. if RawRecord.RecordLength<2 then
  1163. internalerror(2015040305);
  1164. ModTyp:=RawRecord.RawData[0];
  1165. IsMainModule:=(ModTyp and $80)<>0;
  1166. HasStartAddress:=(ModTyp and $40)<>0;
  1167. SegmentBit:=(ModTyp and $20)<>0;
  1168. LogicalStartAddress:=(ModTyp and $01)<>0;
  1169. if (ModTyp and $1E)<>0 then
  1170. internalerror(2015041404);
  1171. NextOfs:=1;
  1172. { clear all the start address properties first }
  1173. FrameMethod:=Low(FrameMethod);
  1174. FrameDatum:=0;
  1175. TargetMethod:=Low(TargetMethod);
  1176. TargetDatum:=0;
  1177. TargetDisplacement:=0;
  1178. PhysFrameNumber:=0;
  1179. PhysOffset:=0;
  1180. if HasStartAddress then
  1181. begin
  1182. if LogicalStartAddress then
  1183. begin
  1184. if NextOfs>=RawRecord.RecordLength then
  1185. internalerror(2015040305);
  1186. EndData:=RawRecord.RawData[NextOfs];
  1187. Inc(NextOfs);
  1188. { frame and target method determined by thread is not allowed in MODEND records }
  1189. if (EndData and $88)<>0 then
  1190. internalerror(2015041406);
  1191. FrameMethod:=TOmfFixupFrameMethod((EndData shr 4) and 7);
  1192. TargetMethod:=TOmfFixupTargetMethod(EndData and 7);
  1193. { frame method ffmLocation is not allowed in an MODEND record }
  1194. if FrameMethod=ffmLocation then
  1195. internalerror(2015041402);
  1196. { read Frame Datum? }
  1197. if FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber] then
  1198. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FFrameDatum);
  1199. { read Target Datum? }
  1200. NextOfs:=RawRecord.ReadIndexedRef(NextOfs,FTargetDatum);
  1201. { read Target Displacement? }
  1202. if TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber] then
  1203. begin
  1204. if Is32Bit then
  1205. begin
  1206. if (NextOfs+3)>=RawRecord.RecordLength then
  1207. internalerror(2015040504);
  1208. TargetDisplacement := RawRecord.RawData[NextOfs]+
  1209. (RawRecord.RawData[NextOfs+1] shl 8)+
  1210. (RawRecord.RawData[NextOfs+2] shl 16)+
  1211. (RawRecord.RawData[NextOfs+3] shl 24);
  1212. Inc(NextOfs,4);
  1213. end
  1214. else
  1215. begin
  1216. if (NextOfs+1)>=RawRecord.RecordLength then
  1217. internalerror(2015040504);
  1218. TargetDisplacement := RawRecord.RawData[NextOfs]+
  1219. (RawRecord.RawData[NextOfs+1] shl 8);
  1220. Inc(NextOfs,2);
  1221. end;
  1222. end;
  1223. end
  1224. else
  1225. begin
  1226. { physical start address }
  1227. if (NextOfs+1)>=RawRecord.RecordLength then
  1228. internalerror(2015040305);
  1229. PhysFrameNumber:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1230. Inc(NextOfs,2);
  1231. if Is32Bit then
  1232. begin
  1233. if (NextOfs+3)>=RawRecord.RecordLength then
  1234. internalerror(2015040305);
  1235. PhysOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8)+
  1236. (RawRecord.RawData[NextOfs+2] shl 16)+(RawRecord.RawData[NextOfs+3] shl 24);
  1237. Inc(NextOfs,4);
  1238. end
  1239. else
  1240. begin
  1241. if (NextOfs+1)>=RawRecord.RecordLength then
  1242. internalerror(2015040305);
  1243. PhysOffset:=RawRecord.RawData[NextOfs]+(RawRecord.RawData[NextOfs+1] shl 8);
  1244. Inc(NextOfs,2);
  1245. end;
  1246. end;
  1247. end;
  1248. end;
  1249. procedure TOmfRecord_MODEND.EncodeTo(RawRecord: TOmfRawRecord);
  1250. var
  1251. ModTyp: Byte;
  1252. NextOfs: Integer;
  1253. EndData: Byte;
  1254. begin
  1255. if Is32Bit then
  1256. RawRecord.RecordType:=RT_MODEND32
  1257. else
  1258. RawRecord.RecordType:=RT_MODEND;
  1259. ModTyp:=(Ord(IsMainModule) shl 7)+(Ord(HasStartAddress) shl 6)+(Ord(SegmentBit) shl 5)+Ord(LogicalStartAddress);
  1260. RawRecord.RawData[0]:=ModTyp;
  1261. NextOfs:=1;
  1262. if HasStartAddress then
  1263. begin
  1264. if LogicalStartAddress then
  1265. begin
  1266. { frame method ffmLocation is not allowed in an MODEND record }
  1267. if FrameMethod=ffmLocation then
  1268. internalerror(2015041402);
  1269. EndData:=(Ord(FrameMethod) shl 4)+Ord(TargetMethod);
  1270. RawRecord.RawData[NextOfs]:=EndData;
  1271. Inc(NextOfs);
  1272. { save Frame Datum? }
  1273. if FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber] then
  1274. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,FrameDatum);
  1275. { save Target Datum? }
  1276. NextOfs:=RawRecord.WriteIndexedRef(NextOfs,TargetDatum);
  1277. { save Target Displacement? }
  1278. if TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber] then
  1279. begin
  1280. if Is32Bit then
  1281. begin
  1282. RawRecord.RawData[NextOfs]:=Byte(TargetDisplacement);
  1283. RawRecord.RawData[NextOfs+1]:=Byte(TargetDisplacement shr 8);
  1284. RawRecord.RawData[NextOfs+2]:=Byte(TargetDisplacement shr 16);
  1285. RawRecord.RawData[NextOfs+3]:=Byte(TargetDisplacement shr 24);
  1286. Inc(NextOfs,4);
  1287. end
  1288. else
  1289. begin
  1290. if TargetDisplacement>$ffff then
  1291. internalerror(2015040502);
  1292. RawRecord.RawData[NextOfs]:=Byte(TargetDisplacement);
  1293. RawRecord.RawData[NextOfs+1]:=Byte(TargetDisplacement shr 8);
  1294. Inc(NextOfs,2);
  1295. end;
  1296. end;
  1297. end
  1298. else
  1299. begin
  1300. { physical start address }
  1301. RawRecord.RawData[NextOfs]:=Byte(PhysFrameNumber);
  1302. RawRecord.RawData[NextOfs+1]:=Byte(PhysFrameNumber shr 8);
  1303. Inc(NextOfs,2);
  1304. if Is32Bit then
  1305. begin
  1306. RawRecord.RawData[NextOfs]:=Byte(PhysOffset);
  1307. RawRecord.RawData[NextOfs+1]:=Byte(PhysOffset shr 8);
  1308. RawRecord.RawData[NextOfs+2]:=Byte(PhysOffset shr 16);
  1309. RawRecord.RawData[NextOfs+3]:=Byte(PhysOffset shr 24);
  1310. Inc(NextOfs,4);
  1311. end
  1312. else
  1313. begin
  1314. if PhysOffset>$ffff then
  1315. internalerror(2015040502);
  1316. RawRecord.RawData[NextOfs]:=Byte(PhysOffset);
  1317. RawRecord.RawData[NextOfs+1]:=Byte(PhysOffset shr 8);
  1318. Inc(NextOfs,2);
  1319. end;
  1320. end;
  1321. end;
  1322. RawRecord.RecordLength:=NextOfs+1;
  1323. RawRecord.CalculateChecksumByte;
  1324. end;
  1325. { TOmfSubRecord_FIXUP }
  1326. function TOmfSubRecord_FIXUP.GetDataRecordOffset: Integer;
  1327. begin
  1328. Result:=FLocationOffset-FDataRecordStartOffset;
  1329. end;
  1330. function TOmfSubRecord_FIXUP.GetLocationSize: Integer;
  1331. const
  1332. OmfLocationType2Size: array [TOmfFixupLocationType] of Integer=
  1333. (1, // fltLoByte
  1334. 2, // fltOffset
  1335. 2, // fltBase
  1336. 4, // fltFarPointer
  1337. 1, // fltHiByte
  1338. 2, // fltLoaderResolvedOffset (PharLap: Offset32)
  1339. 0, // fltUndefined6 (PharLap: Pointer48)
  1340. 0, // fltUndefined7
  1341. 0, // fltUndefined8
  1342. 4, // fltOffset32
  1343. 0, // fltUndefined10
  1344. 6, // fltFarPointer48
  1345. 0, // fltUndefined12
  1346. 4, // fltLoaderResolvedOffset32
  1347. 0, // fltUndefined14
  1348. 0); // fltUndefined15
  1349. begin
  1350. Result:=OmfLocationType2Size[LocationType];
  1351. end;
  1352. procedure TOmfSubRecord_FIXUP.SetDataRecordOffset(AValue: Integer);
  1353. begin
  1354. FLocationOffset:=AValue+FDataRecordStartOffset;
  1355. end;
  1356. function TOmfSubRecord_FIXUP.ReadAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  1357. var
  1358. Locat: Word;
  1359. FixData: Byte;
  1360. begin
  1361. if (Offset+2)>=RawRecord.RecordLength then
  1362. internalerror(2015040504);
  1363. { unlike other fields in the OMF format, this one is big endian }
  1364. Locat:=(RawRecord.RawData[Offset] shl 8) or RawRecord.RawData[Offset+1];
  1365. FixData:=RawRecord.RawData[Offset+2];
  1366. Inc(Offset,3);
  1367. if (Locat and $8000)=0 then
  1368. internalerror(2015040503);
  1369. DataRecordOffset:=Locat and $3FF;
  1370. LocationType:=TOmfFixupLocationType((Locat shr 10) and 15);
  1371. Mode:=TOmfFixupMode((Locat shr 14) and 1);
  1372. FrameDeterminedByThread:=(FixData and $80)<>0;
  1373. TargetDeterminedByThread:=(FixData and $08)<>0;
  1374. if FrameDeterminedByThread then
  1375. FrameThread:=TOmfFixupThread((FixData shr 4) and 3)
  1376. else
  1377. FrameMethod:=TOmfFixupFrameMethod((FixData shr 4) and 7);
  1378. if TargetDeterminedByThread then
  1379. begin
  1380. TargetThread:=TOmfFixupThread(FixData and 3);
  1381. TargetThreadDisplacementPresent:=(FixData and $40)=0;
  1382. end
  1383. else
  1384. TargetMethod:=TOmfFixupTargetMethod(FixData and 7);
  1385. { read Frame Datum? }
  1386. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  1387. Offset:=RawRecord.ReadIndexedRef(Offset,FFrameDatum)
  1388. else
  1389. FrameDatum:=0;
  1390. { read Target Datum? }
  1391. if not TargetDeterminedByThread then
  1392. Offset:=RawRecord.ReadIndexedRef(Offset,FTargetDatum)
  1393. else
  1394. TargetDatum:=0;
  1395. { read Target Displacement? }
  1396. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  1397. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  1398. begin
  1399. if Is32Bit then
  1400. begin
  1401. if (Offset+3)>=RawRecord.RecordLength then
  1402. internalerror(2015040504);
  1403. TargetDisplacement := RawRecord.RawData[Offset]+
  1404. (RawRecord.RawData[Offset+1] shl 8)+
  1405. (RawRecord.RawData[Offset+2] shl 16)+
  1406. (RawRecord.RawData[Offset+3] shl 24);
  1407. Inc(Offset,4);
  1408. end
  1409. else
  1410. begin
  1411. if (Offset+1)>=RawRecord.RecordLength then
  1412. internalerror(2015040504);
  1413. TargetDisplacement := RawRecord.RawData[Offset]+
  1414. (RawRecord.RawData[Offset+1] shl 8);
  1415. Inc(Offset,2);
  1416. end;
  1417. end;
  1418. Result:=Offset;
  1419. end;
  1420. function TOmfSubRecord_FIXUP.WriteAt(RawRecord: TOmfRawRecord; Offset: Integer): Integer;
  1421. var
  1422. Locat: Word;
  1423. FixData: Byte;
  1424. begin
  1425. if (DataRecordOffset<0) or (DataRecordOffset>1023) then
  1426. internalerror(2015040501);
  1427. Locat:=$8000+(Ord(Mode) shl 14)+(Ord(LocationType) shl 10)+DataRecordOffset;
  1428. { unlike other fields in the OMF format, this one is big endian }
  1429. RawRecord.RawData[Offset]:=Byte(Locat shr 8);
  1430. RawRecord.RawData[Offset+1]:=Byte(Locat);
  1431. Inc(Offset, 2);
  1432. FixData:=(Ord(FrameDeterminedByThread) shl 7)+(Ord(TargetDeterminedByThread) shl 3);
  1433. if FrameDeterminedByThread then
  1434. FixData:=FixData+(Ord(FrameThread) shl 4)
  1435. else
  1436. FixData:=FixData+(Ord(FrameMethod) shl 4);
  1437. if TargetDeterminedByThread then
  1438. FixData:=FixData+Ord(TargetThread)+(Ord(not TargetThreadDisplacementPresent) shl 2)
  1439. else
  1440. FixData:=FixData+Ord(TargetMethod);
  1441. RawRecord.RawData[Offset]:=FixData;
  1442. Inc(Offset);
  1443. { save Frame Datum? }
  1444. if not FrameDeterminedByThread and (FrameMethod in [ffmSegmentIndex,ffmGroupIndex,ffmExternalIndex,ffmFrameNumber]) then
  1445. Offset:=RawRecord.WriteIndexedRef(Offset,FrameDatum);
  1446. { save Target Datum? }
  1447. if not TargetDeterminedByThread then
  1448. Offset:=RawRecord.WriteIndexedRef(Offset,TargetDatum);
  1449. { save Target Displacement? }
  1450. if (TargetDeterminedByThread and TargetThreadDisplacementPresent) or
  1451. (TargetMethod in [ftmSegmentIndex,ftmGroupIndex,ftmExternalIndex,ftmFrameNumber]) then
  1452. begin
  1453. if Is32Bit then
  1454. begin
  1455. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1456. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1457. RawRecord.RawData[Offset+2]:=Byte(TargetDisplacement shr 16);
  1458. RawRecord.RawData[Offset+3]:=Byte(TargetDisplacement shr 24);
  1459. Inc(Offset,4);
  1460. end
  1461. else
  1462. begin
  1463. if TargetDisplacement>$ffff then
  1464. internalerror(2015040502);
  1465. RawRecord.RawData[Offset]:=Byte(TargetDisplacement);
  1466. RawRecord.RawData[Offset+1]:=Byte(TargetDisplacement shr 8);
  1467. Inc(Offset,2);
  1468. end;
  1469. end;
  1470. Result:=Offset;
  1471. end;
  1472. { TOmfRecord_LIBHEAD }
  1473. constructor TOmfRecord_LIBHEAD.Create;
  1474. begin
  1475. PageSize:=512;
  1476. DictionarySizeInBlocks:=2;
  1477. CaseSensitive:=true;
  1478. end;
  1479. procedure TOmfRecord_LIBHEAD.SetPageSize(AValue: Integer);
  1480. var
  1481. p: longint;
  1482. begin
  1483. { valid library page sizes are powers of two, between 2**4 and 2**15 }
  1484. if not ispowerof2(AValue,p) then
  1485. internalerror(2015041802);
  1486. if (p<4) or (p>15) then
  1487. internalerror(2015041802);
  1488. FPageSize:=AValue;
  1489. end;
  1490. procedure TOmfRecord_LIBHEAD.DecodeFrom(RawRecord: TOmfRawRecord);
  1491. begin
  1492. if RawRecord.RecordType<>RT_LIBHEAD then
  1493. internalerror(2015040301);
  1494. { this will also range check PageSize and will ensure that RecordLength>=13 }
  1495. PageSize:=RawRecord.RecordLength+3;
  1496. DictionaryOffset:=RawRecord.RawData[0]+
  1497. (RawRecord.RawData[1] shl 8)+
  1498. (RawRecord.RawData[2] shl 16)+
  1499. (RawRecord.RawData[3] shl 24);
  1500. DictionarySizeInBlocks:=RawRecord.RawData[4]+
  1501. (RawRecord.RawData[5] shl 8);
  1502. Flags:=RawRecord.RawData[6];
  1503. end;
  1504. procedure TOmfRecord_LIBHEAD.EncodeTo(RawRecord: TOmfRawRecord);
  1505. begin
  1506. { make sure the LIBHEAD record is padded with zeros at the end }
  1507. FillChar(RawRecord.RawData,SizeOf(RawRecord.RawData),0);
  1508. RawRecord.RecordType:=RT_LIBHEAD;
  1509. RawRecord.RecordLength:=PageSize-3;
  1510. RawRecord.RawData[0]:=Byte(DictionaryOffset);
  1511. RawRecord.RawData[1]:=Byte(DictionaryOffset shr 8);
  1512. RawRecord.RawData[2]:=Byte(DictionaryOffset shr 16);
  1513. RawRecord.RawData[3]:=Byte(DictionaryOffset shr 24);
  1514. RawRecord.RawData[4]:=Byte(DictionarySizeInBlocks);
  1515. RawRecord.RawData[5]:=Byte(DictionarySizeInBlocks shr 8);
  1516. RawRecord.RawData[6]:=Flags;
  1517. { the LIBHEAD record contains no checksum byte, so no need to call
  1518. RawRecord.CalculateChecksumByte }
  1519. end;
  1520. function TOmfRecord_LIBHEAD.IsCaseSensitive: Boolean;
  1521. begin
  1522. Result:=(FFlags and 1)<>0;
  1523. end;
  1524. procedure TOmfRecord_LIBHEAD.SetCaseSensitive(AValue: Boolean);
  1525. begin
  1526. FFlags:=(FFlags and $FE) or Ord(AValue);
  1527. end;
  1528. { TOmfRecord_LIBEND }
  1529. procedure TOmfRecord_LIBEND.DecodeFrom(RawRecord: TOmfRawRecord);
  1530. begin
  1531. if RawRecord.RecordType<>RT_LIBEND then
  1532. internalerror(2015040301);
  1533. FPaddingBytes:=RawRecord.RecordLength;
  1534. end;
  1535. procedure TOmfRecord_LIBEND.EncodeTo(RawRecord: TOmfRawRecord);
  1536. begin
  1537. { make sure the LIBEND record is padded with zeros at the end }
  1538. FillChar(RawRecord.RawData,SizeOf(RawRecord.RawData),0);
  1539. RawRecord.RecordType:=RT_LIBEND;
  1540. RawRecord.RecordLength:=FPaddingBytes;
  1541. { the LIBEND record contains no checksum byte, so no need to call
  1542. RawRecord.CalculateChecksumByte }
  1543. end;
  1544. procedure TOmfRecord_LIBEND.CalculatePaddingBytes(RecordStartOffset: DWord);
  1545. var
  1546. DictionaryStartOffset: Integer;
  1547. begin
  1548. { padding must be calculated, so that the dictionary begins on a 512-byte boundary }
  1549. Inc(RecordStartOffset,3); // padding begins _after_ the record header (3 bytes)
  1550. DictionaryStartOffset:=(RecordStartOffset+511) and $fffffe00;
  1551. PaddingBytes:=DictionaryStartOffset-RecordStartOffset;
  1552. end;
  1553. function compute_omf_lib_hash(const name: string; blocks: Integer): TOmfLibHash;
  1554. const
  1555. blank=$20; // ASCII blank
  1556. nbuckets=37;
  1557. var
  1558. block_x: Integer;
  1559. block_d: Integer;
  1560. bucket_x: Integer;
  1561. bucket_d: Integer;
  1562. len: Integer;
  1563. pbidx,peidx: Integer;
  1564. cback,cfront: Byte;
  1565. begin
  1566. len:=Length(name);
  1567. if len=0 then
  1568. internalerror(2015041801);
  1569. pbidx:=1;
  1570. peidx:=len+1;
  1571. { left to right scan }
  1572. block_x:=len or blank;
  1573. bucket_d:=block_x;
  1574. { right to left scan }
  1575. block_d:=0;
  1576. bucket_x:=0;
  1577. while true do
  1578. begin
  1579. { blank -> convert to LC }
  1580. Dec(peidx);
  1581. cback:=Byte(name[peidx]) or blank;
  1582. bucket_x:=RorWord(bucket_x,2) xor cback;
  1583. block_d:=RolWord(block_d,2) xor cback;
  1584. Dec(len);
  1585. if len=0 then
  1586. break;
  1587. cfront:=Byte(name[pbidx]) or blank;
  1588. Inc(pbidx);
  1589. block_x:=RolWord(block_x,2) xor cfront;
  1590. bucket_d:=RorWord(bucket_d,2) xor cfront;
  1591. end;
  1592. Result.block_x:=block_x mod blocks;
  1593. Result.block_d:=max(block_d mod blocks,1);
  1594. Result.bucket_x:=bucket_x mod nbuckets;
  1595. Result.bucket_d:=max(bucket_d mod nbuckets,1);
  1596. end;
  1597. function section_belongs_to_dgroup(atype: TAsmSectiontype): Boolean;
  1598. begin
  1599. {$ifdef i8086}
  1600. case omf_segclass[atype] of
  1601. 'CODE':
  1602. result:=current_settings.x86memorymodel=mm_tiny;
  1603. 'DATA',
  1604. 'BSS':
  1605. result:=true;
  1606. 'STACK':
  1607. result:=current_settings.x86memorymodel in (x86_near_data_models-[mm_tiny]);
  1608. 'HEAP':
  1609. result:=current_settings.x86memorymodel in x86_near_data_models;
  1610. else
  1611. result:=false;
  1612. end;
  1613. {$else i8086}
  1614. result:=false;
  1615. {$endif i8086}
  1616. end;
  1617. end.