ogomf.pas 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100
  1. {
  2. Copyright (c) 2015 by Nikolay Nikolov
  3. Contains the binary Relocatable Object Module Format (OMF) reader and writer
  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 ogomf;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,globtype,
  24. { target }
  25. systems,
  26. { assembler }
  27. cpuinfo,cpubase,aasmbase,assemble,link,
  28. { OMF definitions }
  29. omfbase,
  30. { output }
  31. ogbase,
  32. owbase;
  33. type
  34. { TOmfObjSymbol }
  35. TOmfObjSymbol = class(TObjSymbol)
  36. public
  37. { string representation for the linker map file }
  38. function AddressStr(AImageBase: qword): string;override;
  39. end;
  40. { TOmfRelocation }
  41. TOmfRelocation = class(TObjRelocation)
  42. private
  43. FFrameGroup: string;
  44. FOmfFixup: TOmfSubRecord_FIXUP;
  45. function GetGroupIndex(const groupname: string): Integer;
  46. public
  47. destructor Destroy; override;
  48. procedure BuildOmfFixup;
  49. property FrameGroup: string read FFrameGroup write FFrameGroup;
  50. property OmfFixup: TOmfSubRecord_FIXUP read FOmfFixup;
  51. end;
  52. TMZExeUnifiedLogicalSegment=class;
  53. { TOmfObjSection }
  54. TOmfObjSection = class(TObjSection)
  55. private
  56. FClassName: string;
  57. FOverlayName: string;
  58. FCombination: TOmfSegmentCombination;
  59. FUse: TOmfSegmentUse;
  60. FPrimaryGroup: string;
  61. FSortOrder: Integer;
  62. FMZExeUnifiedLogicalSegment: TMZExeUnifiedLogicalSegment;
  63. FLinNumEntries: TOmfSubRecord_LINNUM_MsLink_LineNumberList;
  64. function GetOmfAlignment: TOmfSegmentAlignment;
  65. public
  66. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:longint;Aoptions:TObjSectionOptions);override;
  67. destructor destroy;override;
  68. function MemPosStr(AImageBase: qword): string;override;
  69. property ClassName: string read FClassName;
  70. property OverlayName: string read FOverlayName;
  71. property OmfAlignment: TOmfSegmentAlignment read GetOmfAlignment;
  72. property Combination: TOmfSegmentCombination read FCombination;
  73. property Use: TOmfSegmentUse read FUse;
  74. property PrimaryGroup: string read FPrimaryGroup;
  75. property SortOrder: Integer read FSortOrder write FSortOrder;
  76. property MZExeUnifiedLogicalSegment: TMZExeUnifiedLogicalSegment read FMZExeUnifiedLogicalSegment write FMZExeUnifiedLogicalSegment;
  77. property LinNumEntries: TOmfSubRecord_LINNUM_MsLink_LineNumberList read FLinNumEntries;
  78. end;
  79. { TOmfObjData }
  80. TOmfObjData = class(TObjData)
  81. private
  82. FMainSource: TPathStr;
  83. class function CodeSectionName(const aname:string): string;
  84. public
  85. constructor create(const n:string);override;
  86. function sectiontype2options(atype:TAsmSectiontype):TObjSectionOptions;override;
  87. function sectiontype2align(atype:TAsmSectiontype):longint;override;
  88. function sectiontype2class(atype:TAsmSectiontype):string;
  89. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  90. function createsection(atype:TAsmSectionType;const aname:string='';aorder:TAsmSectionOrder=secorder_default):TObjSection;override;
  91. function reffardatasection:TObjSection;
  92. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  93. property MainSource: TPathStr read FMainSource;
  94. end;
  95. { TOmfObjOutput }
  96. TOmfObjOutput = class(tObjOutput)
  97. private
  98. FLNames: TOmfOrderedNameCollection;
  99. FSegments: TFPHashObjectList;
  100. FGroups: TFPHashObjectList;
  101. procedure AddSegment(const name,segclass,ovlname: string;
  102. Alignment: TOmfSegmentAlignment; Combination: TOmfSegmentCombination;
  103. Use: TOmfSegmentUse; Size: TObjSectionOfs);
  104. procedure AddGroup(const groupname: string; seglist: array of const);
  105. procedure AddGroup(const groupname: string; seglist: TSegmentList);
  106. procedure WriteSections(Data:TObjData);
  107. procedure WriteSectionContentAndFixups(sec: TObjSection);
  108. procedure WriteLinNumRecords(sec: TOmfObjSection);
  109. procedure section_count_sections(p:TObject;arg:pointer);
  110. procedure WritePUBDEFs(Data: TObjData);
  111. procedure WriteEXTDEFs(Data: TObjData);
  112. property LNames: TOmfOrderedNameCollection read FLNames;
  113. property Segments: TFPHashObjectList read FSegments;
  114. property Groups: TFPHashObjectList read FGroups;
  115. protected
  116. function writeData(Data:TObjData):boolean;override;
  117. public
  118. constructor create(AWriter:TObjectWriter);override;
  119. destructor Destroy;override;
  120. procedure WriteDllImport(const dllname,afuncname,mangledname:string;ordnr:longint;isvar:boolean);
  121. end;
  122. { TOmfObjInput }
  123. TOmfObjInput = class(TObjInput)
  124. private
  125. FLNames: TOmfOrderedNameCollection;
  126. FExtDefs: TFPHashObjectList;
  127. FPubDefs: TFPHashObjectList;
  128. FFixupThreads: TOmfThreads;
  129. FRawRecord: TOmfRawRecord;
  130. FCaseSensitiveSegments: Boolean;
  131. FCaseSensitiveSymbols: Boolean;
  132. function PeekNextRecordType: Byte;
  133. function ReadLNames(RawRec: TOmfRawRecord): Boolean;
  134. function ReadSegDef(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  135. function ReadGrpDef(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  136. function ReadExtDef(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  137. function ReadPubDef(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  138. function ReadModEnd(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  139. function ReadLeOrLiDataAndFixups(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  140. function ImportOmfFixup(objdata: TObjData; objsec: TOmfObjSection; Fixup: TOmfSubRecord_FIXUP): Boolean;
  141. property LNames: TOmfOrderedNameCollection read FLNames;
  142. property ExtDefs: TFPHashObjectList read FExtDefs;
  143. property PubDefs: TFPHashObjectList read FPubDefs;
  144. { Specifies whether we're case sensitive in regards to segment, class, overlay and group names. }
  145. property CaseSensitiveSegments: Boolean read FCaseSensitiveSegments write FCaseSensitiveSegments;
  146. { Specifies whether symbol names (in EXTDEF and PUBDEF records) are case sensitive. }
  147. property CaseSensitiveSymbols: Boolean read FCaseSensitiveSymbols write FCaseSensitiveSymbols;
  148. public
  149. constructor create;override;
  150. destructor destroy;override;
  151. class function CanReadObjData(AReader:TObjectreader):boolean;override;
  152. function ReadObjData(AReader:TObjectreader;out objdata:TObjData):boolean;override;
  153. end;
  154. { TMZExeRelocation }
  155. TMZExeRelocation = record
  156. offset: Word;
  157. segment: Word;
  158. end;
  159. TMZExeRelocations = array of TMZExeRelocation;
  160. TMZExeExtraHeaderData = array of Byte;
  161. { TMZExeHeader }
  162. TMZExeHeader = class
  163. private
  164. FChecksum: Word;
  165. FExtraHeaderData: TMZExeExtraHeaderData;
  166. FHeaderSizeAlignment: Integer;
  167. FInitialCS: Word;
  168. FInitialIP: Word;
  169. FInitialSP: Word;
  170. FInitialSS: Word;
  171. FLoadableImageSize: DWord;
  172. FMaxExtraParagraphs: Word;
  173. FMinExtraParagraphs: Word;
  174. FOverlayNumber: Word;
  175. FRelocations: TMZExeRelocations;
  176. procedure SetHeaderSizeAlignment(AValue: Integer);
  177. public
  178. constructor Create;
  179. procedure WriteTo(aWriter: TObjectWriter);
  180. procedure AddRelocation(aSegment,aOffset: Word);
  181. property HeaderSizeAlignment: Integer read FHeaderSizeAlignment write SetHeaderSizeAlignment; {default=16, must be multiple of 16}
  182. property Relocations: TMZExeRelocations read FRelocations write FRelocations;
  183. property ExtraHeaderData: TMZExeExtraHeaderData read FExtraHeaderData write FExtraHeaderData;
  184. property LoadableImageSize: DWord read FLoadableImageSize write FLoadableImageSize;
  185. property MinExtraParagraphs: Word read FMinExtraParagraphs write FMinExtraParagraphs;
  186. property MaxExtraParagraphs: Word read FMaxExtraParagraphs write FMaxExtraParagraphs;
  187. property InitialSS: Word read FInitialSS write FInitialSS;
  188. property InitialSP: Word read FInitialSP write FInitialSP;
  189. property Checksum: Word read FChecksum write FChecksum;
  190. property InitialIP: Word read FInitialIP write FInitialIP;
  191. property InitialCS: Word read FInitialCS write FInitialCS;
  192. property OverlayNumber: Word read FOverlayNumber write FOverlayNumber;
  193. end;
  194. { TMZExeSection }
  195. TMZExeSection=class(TExeSection)
  196. public
  197. procedure AddObjSection(objsec:TObjSection;ignoreprops:boolean=false);override;
  198. end;
  199. { TMZExeUnifiedLogicalSegment }
  200. TMZExeUnifiedLogicalSegment=class(TFPHashObject)
  201. private
  202. FObjSectionList: TFPObjectList;
  203. FSegName: TSymStr;
  204. FSegClass: TSymStr;
  205. FPrimaryGroup: string;
  206. public
  207. Size,
  208. MemPos,
  209. MemBasePos: qword;
  210. IsStack: Boolean;
  211. constructor create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  212. destructor destroy;override;
  213. procedure AddObjSection(ObjSec: TOmfObjSection);
  214. procedure CalcMemPos;
  215. function MemPosStr:string;
  216. property ObjSectionList: TFPObjectList read FObjSectionList;
  217. property SegName: TSymStr read FSegName;
  218. property SegClass: TSymStr read FSegClass;
  219. property PrimaryGroup: string read FPrimaryGroup write FPrimaryGroup;
  220. end;
  221. { TMZExeUnifiedLogicalGroup }
  222. TMZExeUnifiedLogicalGroup=class(TFPHashObject)
  223. private
  224. FSegmentList: TFPHashObjectList;
  225. public
  226. Size,
  227. MemPos: qword;
  228. constructor create(HashObjectList:TFPHashObjectList;const s:TSymStr);
  229. destructor destroy;override;
  230. procedure CalcMemPos;
  231. function MemPosStr:string;
  232. procedure AddSegment(UniSeg: TMZExeUnifiedLogicalSegment);
  233. property SegmentList: TFPHashObjectList read FSegmentList;
  234. end;
  235. { TMZExeOutput }
  236. TMZExeOutput = class(TExeOutput)
  237. private
  238. FMZFlatContentSection: TMZExeSection;
  239. FExeUnifiedLogicalSegments: TFPHashObjectList;
  240. FExeUnifiedLogicalGroups: TFPHashObjectList;
  241. FDwarfUnifiedLogicalSegments: TFPHashObjectList;
  242. FHeader: TMZExeHeader;
  243. function GetMZFlatContentSection: TMZExeSection;
  244. procedure CalcDwarfUnifiedLogicalSegmentsForSection(const SecName: TSymStr);
  245. procedure CalcExeUnifiedLogicalSegments;
  246. procedure CalcExeGroups;
  247. procedure CalcSegments_MemBasePos;
  248. procedure WriteMap_SegmentsAndGroups;
  249. procedure WriteMap_HeaderData;
  250. function FindStackSegment: TMZExeUnifiedLogicalSegment;
  251. procedure FillLoadableImageSize;
  252. procedure FillMinExtraParagraphs;
  253. procedure FillMaxExtraParagraphs;
  254. procedure FillStartAddress;
  255. procedure FillStackAddress;
  256. procedure FillHeaderData;
  257. function writeExe:boolean;
  258. function writeCom:boolean;
  259. function writeDebugElf:boolean;
  260. property ExeUnifiedLogicalSegments: TFPHashObjectList read FExeUnifiedLogicalSegments;
  261. property ExeUnifiedLogicalGroups: TFPHashObjectList read FExeUnifiedLogicalGroups;
  262. property DwarfUnifiedLogicalSegments: TFPHashObjectList read FExeUnifiedLogicalSegments;
  263. property Header: TMZExeHeader read FHeader;
  264. protected
  265. procedure Load_Symbol(const aname:string);override;
  266. procedure DoRelocationFixup(objsec:TObjSection);override;
  267. procedure Order_ObjSectionList(ObjSectionList : TFPObjectList;const aPattern:string);override;
  268. procedure MemPos_EndExeSection;override;
  269. function writeData:boolean;override;
  270. public
  271. constructor create;override;
  272. destructor destroy;override;
  273. property MZFlatContentSection: TMZExeSection read GetMZFlatContentSection;
  274. end;
  275. TOmfAssembler = class(tinternalassembler)
  276. constructor create(info: pasminfo; smart:boolean);override;
  277. end;
  278. implementation
  279. uses
  280. SysUtils,
  281. cutils,verbose,globals,
  282. fmodule,aasmtai,aasmdata,
  283. ogmap,owomflib,elfbase,
  284. version
  285. ;
  286. const win16stub : array[0..255] of byte=(
  287. $4d,$5a,$00,$01,$01,$00,$00,$00,$08,$00,$10,$00,$ff,$ff,$08,$00,
  288. $00,$01,$00,$00,$00,$00,$00,$00,$40,$00,$00,$00,$00,$00,$00,$00,
  289. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  290. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$01,$00,$00,
  291. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  292. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  293. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  294. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,
  295. $ba,$10,$00,$0e,$1f,$b4,$09,$cd,$21,$b8,$01,$4c,$cd,$21,$90,$90,
  296. $54,$68,$69,$73,$20,$70,$72,$6f,$67,$72,$61,$6d,$20,$72,$65,$71,
  297. $75,$69,$72,$65,$73,$20,$4d,$69,$63,$72,$6f,$73,$6f,$66,$74,$20,
  298. $57,$69,$6e,$64,$6f,$77,$73,$2e,$0d,$0a,$24,$20,$20,$20,$20,$20,
  299. $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,
  300. $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,
  301. $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,
  302. $20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20,$20);
  303. {****************************************************************************
  304. TOmfObjSymbol
  305. ****************************************************************************}
  306. function TOmfObjSymbol.AddressStr(AImageBase: qword): string;
  307. var
  308. base: qword;
  309. begin
  310. if assigned(TOmfObjSection(objsection).MZExeUnifiedLogicalSegment) then
  311. base:=TOmfObjSection(objsection).MZExeUnifiedLogicalSegment.MemBasePos
  312. else
  313. base:=(address shr 4) shl 4;
  314. Result:=HexStr(base shr 4,4)+':'+HexStr(address-base,4);
  315. end;
  316. {****************************************************************************
  317. TOmfRelocation
  318. ****************************************************************************}
  319. function TOmfRelocation.GetGroupIndex(const groupname: string): Integer;
  320. begin
  321. if groupname='DGROUP' then
  322. Result:=1
  323. else
  324. internalerror(2014040703);
  325. end;
  326. destructor TOmfRelocation.Destroy;
  327. begin
  328. FOmfFixup.Free;
  329. inherited Destroy;
  330. end;
  331. procedure TOmfRelocation.BuildOmfFixup;
  332. begin
  333. FreeAndNil(FOmfFixup);
  334. FOmfFixup:=TOmfSubRecord_FIXUP.Create;
  335. if ObjSection<>nil then
  336. begin
  337. FOmfFixup.LocationOffset:=DataOffset;
  338. if typ in [RELOC_ABSOLUTE16,RELOC_RELATIVE16] then
  339. FOmfFixup.LocationType:=fltOffset
  340. else if typ in [RELOC_ABSOLUTE32,RELOC_RELATIVE32] then
  341. FOmfFixup.LocationType:=fltOffset32
  342. else if typ in [RELOC_SEG,RELOC_SEGREL] then
  343. FOmfFixup.LocationType:=fltBase
  344. else
  345. internalerror(2015041501);
  346. FOmfFixup.FrameDeterminedByThread:=False;
  347. FOmfFixup.TargetDeterminedByThread:=False;
  348. if typ in [RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_SEG] then
  349. FOmfFixup.Mode:=fmSegmentRelative
  350. else if typ in [RELOC_RELATIVE16,RELOC_RELATIVE32,RELOC_SEGREL] then
  351. FOmfFixup.Mode:=fmSelfRelative
  352. else
  353. internalerror(2015041401);
  354. if typ in [RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_RELATIVE16,RELOC_RELATIVE32] then
  355. begin
  356. FOmfFixup.TargetMethod:=ftmSegmentIndexNoDisp;
  357. FOmfFixup.TargetDatum:=ObjSection.Index;
  358. if TOmfObjSection(ObjSection).PrimaryGroup<>'' then
  359. begin
  360. FOmfFixup.FrameMethod:=ffmGroupIndex;
  361. FOmfFixup.FrameDatum:=GetGroupIndex(TOmfObjSection(ObjSection).PrimaryGroup);
  362. end
  363. else
  364. FOmfFixup.FrameMethod:=ffmTarget;
  365. end
  366. else
  367. begin
  368. FOmfFixup.FrameMethod:=ffmTarget;
  369. if TOmfObjSection(ObjSection).PrimaryGroup<>'' then
  370. begin
  371. FOmfFixup.TargetMethod:=ftmGroupIndexNoDisp;
  372. FOmfFixup.TargetDatum:=GetGroupIndex(TOmfObjSection(ObjSection).PrimaryGroup);
  373. end
  374. else
  375. begin
  376. FOmfFixup.TargetMethod:=ftmSegmentIndexNoDisp;
  377. FOmfFixup.TargetDatum:=ObjSection.Index;
  378. end;
  379. end;
  380. end
  381. else if symbol<>nil then
  382. begin
  383. FOmfFixup.LocationOffset:=DataOffset;
  384. if typ in [RELOC_ABSOLUTE16,RELOC_RELATIVE16] then
  385. FOmfFixup.LocationType:=fltOffset
  386. else if typ in [RELOC_ABSOLUTE32,RELOC_RELATIVE32] then
  387. FOmfFixup.LocationType:=fltOffset32
  388. else if typ in [RELOC_SEG,RELOC_SEGREL] then
  389. FOmfFixup.LocationType:=fltBase
  390. else
  391. internalerror(2015041501);
  392. FOmfFixup.FrameDeterminedByThread:=False;
  393. FOmfFixup.TargetDeterminedByThread:=False;
  394. if typ in [RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_SEG] then
  395. FOmfFixup.Mode:=fmSegmentRelative
  396. else if typ in [RELOC_RELATIVE16,RELOC_RELATIVE32,RELOC_SEGREL] then
  397. FOmfFixup.Mode:=fmSelfRelative
  398. else
  399. internalerror(2015041401);
  400. FOmfFixup.TargetMethod:=ftmExternalIndexNoDisp;
  401. FOmfFixup.TargetDatum:=symbol.symidx;
  402. FOmfFixup.FrameMethod:=ffmTarget;
  403. end
  404. else if group<>nil then
  405. begin
  406. FOmfFixup.LocationOffset:=DataOffset;
  407. if typ in [RELOC_ABSOLUTE16,RELOC_RELATIVE16] then
  408. FOmfFixup.LocationType:=fltOffset
  409. else if typ in [RELOC_ABSOLUTE32,RELOC_RELATIVE32] then
  410. FOmfFixup.LocationType:=fltOffset32
  411. else if typ in [RELOC_SEG,RELOC_SEGREL] then
  412. FOmfFixup.LocationType:=fltBase
  413. else
  414. internalerror(2015041501);
  415. FOmfFixup.FrameDeterminedByThread:=False;
  416. FOmfFixup.TargetDeterminedByThread:=False;
  417. if typ in [RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_SEG] then
  418. FOmfFixup.Mode:=fmSegmentRelative
  419. else if typ in [RELOC_RELATIVE16,RELOC_RELATIVE32,RELOC_SEGREL] then
  420. FOmfFixup.Mode:=fmSelfRelative
  421. else
  422. internalerror(2015041401);
  423. FOmfFixup.FrameMethod:=ffmTarget;
  424. FOmfFixup.TargetMethod:=ftmGroupIndexNoDisp;
  425. FOmfFixup.TargetDatum:=GetGroupIndex(group.Name);
  426. end
  427. else
  428. internalerror(2015040702);
  429. end;
  430. {****************************************************************************
  431. TOmfObjSection
  432. ****************************************************************************}
  433. function TOmfObjSection.GetOmfAlignment: TOmfSegmentAlignment;
  434. begin
  435. case SecAlign of
  436. 1:
  437. result:=saRelocatableByteAligned;
  438. 2:
  439. result:=saRelocatableWordAligned;
  440. 4:
  441. result:=saRelocatableDWordAligned;
  442. 16:
  443. result:=saRelocatableParaAligned;
  444. 256:
  445. result:=saRelocatablePageAligned;
  446. 4096:
  447. result:=saNotSupported;
  448. else
  449. internalerror(2015041504);
  450. end;
  451. end;
  452. constructor TOmfObjSection.create(AList: TFPHashObjectList;
  453. const Aname: string; Aalign: longint; Aoptions: TObjSectionOptions);
  454. begin
  455. inherited create(AList, Aname, Aalign, Aoptions);
  456. FCombination:=scPublic;
  457. FUse:=suUse16;
  458. FLinNumEntries:=TOmfSubRecord_LINNUM_MsLink_LineNumberList.Create;
  459. end;
  460. destructor TOmfObjSection.destroy;
  461. begin
  462. FLinNumEntries.Free;
  463. inherited destroy;
  464. end;
  465. function TOmfObjSection.MemPosStr(AImageBase: qword): string;
  466. begin
  467. Result:=HexStr(MZExeUnifiedLogicalSegment.MemBasePos shr 4,4)+':'+
  468. HexStr(MemPos-MZExeUnifiedLogicalSegment.MemBasePos,4);
  469. end;
  470. {****************************************************************************
  471. TOmfObjData
  472. ****************************************************************************}
  473. class function TOmfObjData.CodeSectionName(const aname: string): string;
  474. begin
  475. {$ifdef i8086}
  476. if current_settings.x86memorymodel in x86_far_code_models then
  477. begin
  478. if cs_huge_code in current_settings.moduleswitches then
  479. result:=aname + '_TEXT'
  480. else
  481. result:=current_module.modulename^ + '_TEXT';
  482. end
  483. else
  484. {$endif}
  485. result:='_TEXT';
  486. end;
  487. constructor TOmfObjData.create(const n: string);
  488. begin
  489. inherited create(n);
  490. CObjSymbol:=TOmfObjSymbol;
  491. CObjSection:=TOmfObjSection;
  492. createsectiongroup('DGROUP');
  493. FMainSource:=current_module.mainsource;
  494. end;
  495. function TOmfObjData.sectiontype2options(atype: TAsmSectiontype): TObjSectionOptions;
  496. begin
  497. Result:=inherited sectiontype2options(atype);
  498. { in the huge memory model, BSS data is actually written in the regular
  499. FAR_DATA segment of the module }
  500. if sectiontype2class(atype)='FAR_DATA' then
  501. Result:=Result+[oso_data,oso_sparse_data];
  502. end;
  503. function TOmfObjData.sectiontype2align(atype: TAsmSectiontype): longint;
  504. begin
  505. Result:=omf_sectiontype2align(atype);
  506. end;
  507. function TOmfObjData.sectiontype2class(atype: TAsmSectiontype): string;
  508. begin
  509. Result:=omf_segclass(atype);
  510. end;
  511. function TOmfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  512. var
  513. sep : string[3];
  514. secname : string;
  515. begin
  516. if (atype=sec_user) then
  517. Result:=aname
  518. else
  519. begin
  520. if omf_secnames[atype]=omf_secnames[sec_code] then
  521. secname:=CodeSectionName(aname)
  522. else if omf_segclass(atype)='FAR_DATA' then
  523. secname:=current_module.modulename^ + '_DATA'
  524. else
  525. secname:=omf_secnames[atype];
  526. if create_smartlink_sections and (aname<>'') then
  527. begin
  528. case aorder of
  529. secorder_begin :
  530. sep:='.b_';
  531. secorder_end :
  532. sep:='.z_';
  533. else
  534. sep:='.n_';
  535. end;
  536. result:=UpCase(secname+sep+aname);
  537. end
  538. else
  539. result:=secname;
  540. end;
  541. end;
  542. function TOmfObjData.createsection(atype: TAsmSectionType; const aname: string; aorder: TAsmSectionOrder): TObjSection;
  543. begin
  544. Result:=inherited createsection(atype, aname, aorder);
  545. TOmfObjSection(Result).FClassName:=sectiontype2class(atype);
  546. if atype=sec_stack then
  547. TOmfObjSection(Result).FCombination:=scStack
  548. else if atype in [sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges] then
  549. begin
  550. TOmfObjSection(Result).FUse:=suUse32;
  551. TOmfObjSection(Result).SizeLimit:=high(longword);
  552. end;
  553. if section_belongs_to_dgroup(atype) then
  554. TOmfObjSection(Result).FPrimaryGroup:='DGROUP';
  555. end;
  556. function TOmfObjData.reffardatasection: TObjSection;
  557. var
  558. secname: string;
  559. begin
  560. secname:=current_module.modulename^ + '_DATA';
  561. result:=TObjSection(ObjSectionList.Find(secname));
  562. if not assigned(result) then
  563. begin
  564. result:=CObjSection.create(ObjSectionList,secname,2,[oso_Data,oso_load,oso_write]);
  565. result.ObjData:=self;
  566. TOmfObjSection(Result).FClassName:='FAR_DATA';
  567. end;
  568. end;
  569. procedure TOmfObjData.writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  570. var
  571. objreloc: TOmfRelocation;
  572. symaddr: AWord;
  573. begin
  574. { RELOC_FARPTR = RELOC_ABSOLUTE16+RELOC_SEG }
  575. if Reloctype=RELOC_FARPTR then
  576. begin
  577. if len<>4 then
  578. internalerror(2015041502);
  579. writeReloc(Data,2,p,RELOC_ABSOLUTE16);
  580. writeReloc(0,2,p,RELOC_SEG);
  581. exit;
  582. end
  583. { RELOC_FARPTR48 = RELOC_ABSOLUTE16+RELOC_SEG }
  584. else if Reloctype=RELOC_FARPTR48 then
  585. begin
  586. if len<>6 then
  587. internalerror(2015041502);
  588. writeReloc(Data,4,p,RELOC_ABSOLUTE32);
  589. writeReloc(0,2,p,RELOC_SEG);
  590. exit;
  591. end;
  592. if CurrObjSec=nil then
  593. internalerror(200403072);
  594. objreloc:=nil;
  595. if Reloctype in [RELOC_FARDATASEG,RELOC_FARDATASEGREL] then
  596. begin
  597. if Reloctype=RELOC_FARDATASEG then
  598. objreloc:=TOmfRelocation.CreateSection(CurrObjSec.Size,reffardatasection,RELOC_SEG)
  599. else
  600. objreloc:=TOmfRelocation.CreateSection(CurrObjSec.Size,reffardatasection,RELOC_SEGREL);
  601. CurrObjSec.ObjRelocations.Add(objreloc);
  602. end
  603. else if assigned(p) then
  604. begin
  605. { real address of the symbol }
  606. symaddr:=p.address;
  607. if p.bind=AB_EXTERNAL then
  608. begin
  609. objreloc:=TOmfRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  610. CurrObjSec.ObjRelocations.Add(objreloc);
  611. end
  612. { relative relocations within the same section can be calculated directly,
  613. without the need to emit a relocation entry }
  614. else if (p.objsection=CurrObjSec) and
  615. (p.bind<>AB_COMMON) and
  616. (Reloctype=RELOC_RELATIVE) then
  617. begin
  618. data:=data+symaddr-len-CurrObjSec.Size;
  619. end
  620. else
  621. begin
  622. objreloc:=TOmfRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
  623. CurrObjSec.ObjRelocations.Add(objreloc);
  624. if not (Reloctype in [RELOC_SEG,RELOC_SEGREL]) then
  625. inc(data,symaddr);
  626. end;
  627. end
  628. else if Reloctype in [RELOC_DGROUP,RELOC_DGROUPREL] then
  629. begin
  630. if Reloctype=RELOC_DGROUP then
  631. objreloc:=TOmfRelocation.CreateGroup(CurrObjSec.Size,TObjSectionGroup(GroupsList.Find('DGROUP')),RELOC_SEG)
  632. else
  633. objreloc:=TOmfRelocation.CreateGroup(CurrObjSec.Size,TObjSectionGroup(GroupsList.Find('DGROUP')),RELOC_SEGREL);
  634. CurrObjSec.ObjRelocations.Add(objreloc);
  635. end;
  636. CurrObjSec.write(data,len);
  637. end;
  638. {****************************************************************************
  639. TOmfObjOutput
  640. ****************************************************************************}
  641. procedure TOmfObjOutput.AddSegment(const name, segclass, ovlname: string;
  642. Alignment: TOmfSegmentAlignment; Combination: TOmfSegmentCombination;
  643. Use: TOmfSegmentUse; Size: TObjSectionOfs);
  644. var
  645. s: TOmfRecord_SEGDEF;
  646. begin
  647. s:=TOmfRecord_SEGDEF.Create;
  648. Segments.Add(name,s);
  649. s.SegmentNameIndex:=LNames.Add(name);
  650. s.ClassNameIndex:=LNames.Add(segclass);
  651. s.OverlayNameIndex:=LNames.Add(ovlname);
  652. s.Alignment:=Alignment;
  653. s.Combination:=Combination;
  654. s.Use:=Use;
  655. s.SegmentLength:=Size;
  656. end;
  657. procedure TOmfObjOutput.AddGroup(const groupname: string; seglist: array of const);
  658. var
  659. g: TOmfRecord_GRPDEF;
  660. I: Integer;
  661. SegListStr: TSegmentList;
  662. begin
  663. g:=TOmfRecord_GRPDEF.Create;
  664. Groups.Add(groupname,g);
  665. g.GroupNameIndex:=LNames.Add(groupname);
  666. SetLength(SegListStr,Length(seglist));
  667. for I:=0 to High(seglist) do
  668. begin
  669. case seglist[I].VType of
  670. vtString:
  671. SegListStr[I]:=Segments.FindIndexOf(seglist[I].VString^);
  672. vtAnsiString:
  673. SegListStr[I]:=Segments.FindIndexOf(AnsiString(seglist[I].VAnsiString));
  674. vtWideString:
  675. SegListStr[I]:=Segments.FindIndexOf(AnsiString(WideString(seglist[I].VWideString)));
  676. vtUnicodeString:
  677. SegListStr[I]:=Segments.FindIndexOf(AnsiString(UnicodeString(seglist[I].VUnicodeString)));
  678. else
  679. internalerror(2015040402);
  680. end;
  681. end;
  682. g.SegmentList:=SegListStr;
  683. end;
  684. procedure TOmfObjOutput.AddGroup(const groupname: string; seglist: TSegmentList);
  685. var
  686. g: TOmfRecord_GRPDEF;
  687. begin
  688. g:=TOmfRecord_GRPDEF.Create;
  689. Groups.Add(groupname,g);
  690. g.GroupNameIndex:=LNames.Add(groupname);
  691. g.SegmentList:=Copy(seglist);
  692. end;
  693. procedure TOmfObjOutput.WriteSections(Data: TObjData);
  694. var
  695. i:longint;
  696. sec:TObjSection;
  697. begin
  698. for i:=0 to Data.ObjSectionList.Count-1 do
  699. begin
  700. sec:=TObjSection(Data.ObjSectionList[i]);
  701. WriteSectionContentAndFixups(sec);
  702. WriteLinNumRecords(TOmfObjSection(sec));
  703. end;
  704. end;
  705. procedure TOmfObjOutput.WriteSectionContentAndFixups(sec: TObjSection);
  706. const
  707. MaxChunkSize=$3fa;
  708. var
  709. RawRecord: TOmfRawRecord;
  710. ChunkStart,ChunkLen: DWord;
  711. ChunkFixupStart,ChunkFixupEnd: Integer;
  712. SegIndex: Integer;
  713. NextOfs: Integer;
  714. Is32BitLEDATA: Boolean;
  715. I: Integer;
  716. begin
  717. if (oso_data in sec.SecOptions) then
  718. begin
  719. if sec.Data=nil then
  720. internalerror(200403073);
  721. for I:=0 to sec.ObjRelocations.Count-1 do
  722. TOmfRelocation(sec.ObjRelocations[I]).BuildOmfFixup;
  723. SegIndex:=Segments.FindIndexOf(sec.Name);
  724. RawRecord:=TOmfRawRecord.Create;
  725. sec.data.seek(0);
  726. ChunkFixupStart:=0;
  727. ChunkFixupEnd:=-1;
  728. ChunkStart:=0;
  729. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  730. while ChunkLen>0 do
  731. begin
  732. { find last fixup in the chunk }
  733. while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
  734. (TOmfRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
  735. inc(ChunkFixupEnd);
  736. { check if last chunk is crossing the chunk boundary, and trim ChunkLen if necessary }
  737. if (ChunkFixupEnd>=ChunkFixupStart) and
  738. ((TOmfRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset+
  739. TOmfRelocation(sec.ObjRelocations[ChunkFixupEnd]).OmfFixup.LocationSize)>(ChunkStart+ChunkLen)) then
  740. begin
  741. ChunkLen:=TOmfRelocation(sec.ObjRelocations[ChunkFixupEnd]).DataOffset-ChunkStart;
  742. Dec(ChunkFixupEnd);
  743. end;
  744. { write LEDATA record }
  745. Is32BitLEDATA:=TOmfObjSection(sec).Use=suUse32;
  746. if Is32BitLEDATA then
  747. RawRecord.RecordType:=RT_LEDATA32
  748. else
  749. RawRecord.RecordType:=RT_LEDATA;
  750. NextOfs:=RawRecord.WriteIndexedRef(0,SegIndex);
  751. if Is32BitLEDATA then
  752. begin
  753. RawRecord.RawData[NextOfs]:=Byte(ChunkStart);
  754. RawRecord.RawData[NextOfs+1]:=Byte(ChunkStart shr 8);
  755. RawRecord.RawData[NextOfs+2]:=Byte(ChunkStart shr 16);
  756. RawRecord.RawData[NextOfs+3]:=Byte(ChunkStart shr 24);
  757. Inc(NextOfs,4);
  758. end
  759. else
  760. begin
  761. if ChunkStart>$ffff then
  762. internalerror(2018052201);
  763. RawRecord.RawData[NextOfs]:=Byte(ChunkStart);
  764. RawRecord.RawData[NextOfs+1]:=Byte(ChunkStart shr 8);
  765. Inc(NextOfs,2);
  766. end;
  767. sec.data.read(RawRecord.RawData[NextOfs], ChunkLen);
  768. Inc(NextOfs, ChunkLen);
  769. RawRecord.RecordLength:=NextOfs+1;
  770. RawRecord.CalculateChecksumByte;
  771. RawRecord.WriteTo(FWriter);
  772. { write FIXUPP record }
  773. if ChunkFixupEnd>=ChunkFixupStart then
  774. begin
  775. RawRecord.RecordType:=RT_FIXUPP;
  776. NextOfs:=0;
  777. for I:=ChunkFixupStart to ChunkFixupEnd do
  778. begin
  779. TOmfRelocation(sec.ObjRelocations[I]).OmfFixup.DataRecordStartOffset:=ChunkStart;
  780. NextOfs:=TOmfRelocation(sec.ObjRelocations[I]).OmfFixup.WriteAt(RawRecord,NextOfs);
  781. end;
  782. RawRecord.RecordLength:=NextOfs+1;
  783. RawRecord.CalculateChecksumByte;
  784. RawRecord.WriteTo(FWriter);
  785. end;
  786. { prepare next chunk }
  787. Inc(ChunkStart, ChunkLen);
  788. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  789. ChunkFixupStart:=ChunkFixupEnd+1;
  790. end;
  791. RawRecord.Free;
  792. end;
  793. end;
  794. procedure TOmfObjOutput.WriteLinNumRecords(sec: TOmfObjSection);
  795. var
  796. SegIndex: Integer;
  797. RawRecord: TOmfRawRecord;
  798. LinNumRec: TOmfRecord_LINNUM_MsLink;
  799. begin
  800. if (oso_data in sec.SecOptions) then
  801. begin
  802. if sec.Data=nil then
  803. internalerror(200403073);
  804. if sec.LinNumEntries.Count=0 then
  805. exit;
  806. SegIndex:=Segments.FindIndexOf(sec.Name);
  807. RawRecord:=TOmfRawRecord.Create;
  808. LinNumRec:=TOmfRecord_LINNUM_MsLink.Create;
  809. LinNumRec.BaseGroup:=0;
  810. LinNumRec.BaseSegment:=SegIndex;
  811. LinNumRec.LineNumberList:=sec.LinNumEntries;
  812. while LinNumRec.NextIndex<sec.LinNumEntries.Count do
  813. begin
  814. LinNumRec.EncodeTo(RawRecord);
  815. RawRecord.WriteTo(FWriter);
  816. end;
  817. LinNumRec.Free;
  818. RawRecord.Free;
  819. end;
  820. end;
  821. procedure TOmfObjOutput.section_count_sections(p: TObject; arg: pointer);
  822. begin
  823. TOmfObjSection(p).index:=pinteger(arg)^;
  824. inc(pinteger(arg)^);
  825. end;
  826. procedure TOmfObjOutput.WritePUBDEFs(Data: TObjData);
  827. var
  828. PubNamesForSection: array of TFPHashObjectList;
  829. i: Integer;
  830. objsym: TObjSymbol;
  831. PublicNameElem: TOmfPublicNameElement;
  832. RawRecord: TOmfRawRecord;
  833. PubDefRec: TOmfRecord_PUBDEF;
  834. PrimaryGroupName: string;
  835. begin
  836. RawRecord:=TOmfRawRecord.Create;
  837. SetLength(PubNamesForSection,Data.ObjSectionList.Count);
  838. for i:=0 to Data.ObjSectionList.Count-1 do
  839. PubNamesForSection[i]:=TFPHashObjectList.Create;
  840. for i:=0 to Data.ObjSymbolList.Count-1 do
  841. begin
  842. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  843. if objsym.bind=AB_GLOBAL then
  844. begin
  845. PublicNameElem:=TOmfPublicNameElement.Create(PubNamesForSection[objsym.objsection.index-1],objsym.Name);
  846. PublicNameElem.PublicOffset:=objsym.offset;
  847. PublicNameElem.IsLocal:=False;
  848. end
  849. else if objsym.bind=AB_LOCAL then
  850. begin
  851. PublicNameElem:=TOmfPublicNameElement.Create(PubNamesForSection[objsym.objsection.index-1],objsym.Name);
  852. PublicNameElem.PublicOffset:=objsym.offset;
  853. PublicNameElem.IsLocal:=True;
  854. end
  855. end;
  856. for i:=0 to Data.ObjSectionList.Count-1 do
  857. if PubNamesForSection[i].Count>0 then
  858. begin
  859. PubDefRec:=TOmfRecord_PUBDEF.Create;
  860. PubDefRec.BaseSegmentIndex:=i+1;
  861. PrimaryGroupName:=TOmfObjSection(Data.ObjSectionList[i]).PrimaryGroup;
  862. if PrimaryGroupName<>'' then
  863. PubDefRec.BaseGroupIndex:=Groups.FindIndexOf(PrimaryGroupName)
  864. else
  865. PubDefRec.BaseGroupIndex:=0;
  866. PubDefRec.PublicNames:=PubNamesForSection[i];
  867. while PubDefRec.NextIndex<PubDefRec.PublicNames.Count do
  868. begin
  869. PubDefRec.EncodeTo(RawRecord);
  870. RawRecord.WriteTo(FWriter);
  871. end;
  872. PubDefRec.Free;
  873. end;
  874. for i:=0 to Data.ObjSectionList.Count-1 do
  875. FreeAndNil(PubNamesForSection[i]);
  876. RawRecord.Free;
  877. end;
  878. procedure TOmfObjOutput.WriteEXTDEFs(Data: TObjData);
  879. var
  880. ExtNames: TFPHashObjectList;
  881. RawRecord: TOmfRawRecord;
  882. i,idx: Integer;
  883. objsym: TObjSymbol;
  884. ExternalNameElem: TOmfExternalNameElement;
  885. ExtDefRec: TOmfRecord_EXTDEF;
  886. begin
  887. ExtNames:=TFPHashObjectList.Create;
  888. RawRecord:=TOmfRawRecord.Create;
  889. idx:=1;
  890. for i:=0 to Data.ObjSymbolList.Count-1 do
  891. begin
  892. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  893. if objsym.bind=AB_EXTERNAL then
  894. begin
  895. ExternalNameElem:=TOmfExternalNameElement.Create(ExtNames,objsym.Name);
  896. objsym.symidx:=idx;
  897. Inc(idx);
  898. end;
  899. end;
  900. if ExtNames.Count>0 then
  901. begin
  902. ExtDefRec:=TOmfRecord_EXTDEF.Create;
  903. ExtDefRec.ExternalNames:=ExtNames;
  904. while ExtDefRec.NextIndex<ExtDefRec.ExternalNames.Count do
  905. begin
  906. ExtDefRec.EncodeTo(RawRecord);
  907. RawRecord.WriteTo(FWriter);
  908. end;
  909. ExtDefRec.Free;
  910. end;
  911. ExtNames.Free;
  912. RawRecord.Free;
  913. end;
  914. function TOmfObjOutput.writeData(Data:TObjData):boolean;
  915. var
  916. RawRecord: TOmfRawRecord;
  917. Header: TOmfRecord_THEADR;
  918. Translator_COMENT: TOmfRecord_COMENT;
  919. DebugFormat_COMENT: TOmfRecord_COMENT;
  920. LinkPassSeparator_COMENT: TOmfRecord_COMENT;
  921. LNamesRec: TOmfRecord_LNAMES;
  922. ModEnd: TOmfRecord_MODEND;
  923. I: Integer;
  924. SegDef: TOmfRecord_SEGDEF;
  925. GrpDef: TOmfRecord_GRPDEF;
  926. DGroupSegments: TSegmentList;
  927. nsections: Integer;
  928. objsym: TObjSymbol;
  929. begin
  930. { calc amount of sections we have and set their index, starting with 1 }
  931. nsections:=1;
  932. data.ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  933. { maximum amount of sections supported in the omf format is $7fff }
  934. if (nsections-1)>$7fff then
  935. internalerror(2015040701);
  936. { write header record }
  937. RawRecord:=TOmfRawRecord.Create;
  938. Header:=TOmfRecord_THEADR.Create;
  939. if cs_debuginfo in current_settings.moduleswitches then
  940. Header.ModuleName:=TOmfObjData(Data).MainSource
  941. else
  942. Header.ModuleName:=Data.Name;
  943. Header.EncodeTo(RawRecord);
  944. RawRecord.WriteTo(FWriter);
  945. Header.Free;
  946. { write translator COMENT header }
  947. Translator_COMENT:=TOmfRecord_COMENT.Create;
  948. Translator_COMENT.CommentClass:=CC_Translator;
  949. Translator_COMENT.CommentString:='FPC '+full_version_string+
  950. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname;
  951. Translator_COMENT.EncodeTo(RawRecord);
  952. RawRecord.WriteTo(FWriter);
  953. Translator_COMENT.Free;
  954. if (target_dbg.id=dbg_codeview) or
  955. ((ds_dwarf_omf_linnum in current_settings.debugswitches) and
  956. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4])) then
  957. begin
  958. DebugFormat_COMENT:=TOmfRecord_COMENT.Create;
  959. DebugFormat_COMENT.CommentClass:=CC_NewOmfExtension;
  960. DebugFormat_COMENT.CommentString:='';
  961. DebugFormat_COMENT.EncodeTo(RawRecord);
  962. RawRecord.WriteTo(FWriter);
  963. DebugFormat_COMENT.Free;
  964. end;
  965. LNames.Clear;
  966. LNames.Add(''); { insert an empty string, which has index 1 }
  967. FSegments.Clear;
  968. FSegments.Add('',nil);
  969. FGroups.Clear;
  970. FGroups.Add('',nil);
  971. for i:=0 to Data.ObjSectionList.Count-1 do
  972. with TOmfObjSection(Data.ObjSectionList[I]) do
  973. AddSegment(Name,ClassName,OverlayName,OmfAlignment,Combination,Use,Size);
  974. { create group "DGROUP" }
  975. SetLength(DGroupSegments,0);
  976. for i:=0 to Data.ObjSectionList.Count-1 do
  977. with TOmfObjSection(Data.ObjSectionList[I]) do
  978. if PrimaryGroup='DGROUP' then
  979. begin
  980. SetLength(DGroupSegments,Length(DGroupSegments)+1);
  981. DGroupSegments[High(DGroupSegments)]:=index;
  982. end;
  983. AddGroup('DGROUP',DGroupSegments);
  984. { write LNAMES record(s) }
  985. LNamesRec:=TOmfRecord_LNAMES.Create;
  986. LNamesRec.Names:=LNames;
  987. while LNamesRec.NextIndex<=LNames.Count do
  988. begin
  989. LNamesRec.EncodeTo(RawRecord);
  990. RawRecord.WriteTo(FWriter);
  991. end;
  992. LNamesRec.Free;
  993. { write SEGDEF record(s) }
  994. for I:=1 to Segments.Count-1 do
  995. begin
  996. SegDef:=TOmfRecord_SEGDEF(Segments[I]);
  997. SegDef.EncodeTo(RawRecord);
  998. RawRecord.WriteTo(FWriter);
  999. end;
  1000. { write GRPDEF record(s) }
  1001. for I:=1 to Groups.Count-1 do
  1002. begin
  1003. GrpDef:=TOmfRecord_GRPDEF(Groups[I]);
  1004. GrpDef.EncodeTo(RawRecord);
  1005. RawRecord.WriteTo(FWriter);
  1006. end;
  1007. { write PUBDEF record(s) }
  1008. WritePUBDEFs(Data);
  1009. { write EXTDEF record(s) }
  1010. WriteEXTDEFs(Data);
  1011. { write link pass separator }
  1012. LinkPassSeparator_COMENT:=TOmfRecord_COMENT.Create;
  1013. LinkPassSeparator_COMENT.CommentClass:=CC_LinkPassSeparator;
  1014. LinkPassSeparator_COMENT.CommentString:=#1;
  1015. LinkPassSeparator_COMENT.NoList:=True;
  1016. LinkPassSeparator_COMENT.EncodeTo(RawRecord);
  1017. RawRecord.WriteTo(FWriter);
  1018. LinkPassSeparator_COMENT.Free;
  1019. { write section content, interleaved with fixups }
  1020. WriteSections(Data);
  1021. { write MODEND record }
  1022. ModEnd:=TOmfRecord_MODEND.Create;
  1023. ModEnd.EncodeTo(RawRecord);
  1024. RawRecord.WriteTo(FWriter);
  1025. ModEnd.Free;
  1026. RawRecord.Free;
  1027. result:=true;
  1028. end;
  1029. constructor TOmfObjOutput.create(AWriter:TObjectWriter);
  1030. begin
  1031. inherited create(AWriter);
  1032. cobjdata:=TOmfObjData;
  1033. FLNames:=TOmfOrderedNameCollection.Create(False);
  1034. FSegments:=TFPHashObjectList.Create;
  1035. FSegments.Add('',nil);
  1036. FGroups:=TFPHashObjectList.Create;
  1037. FGroups.Add('',nil);
  1038. end;
  1039. destructor TOmfObjOutput.Destroy;
  1040. begin
  1041. FGroups.Free;
  1042. FSegments.Free;
  1043. FLNames.Free;
  1044. inherited Destroy;
  1045. end;
  1046. procedure TOmfObjOutput.WriteDllImport(const dllname,afuncname,mangledname: string; ordnr: longint; isvar: boolean);
  1047. var
  1048. RawRecord: TOmfRawRecord;
  1049. Header: TOmfRecord_THEADR;
  1050. DllImport_COMENT: TOmfRecord_COMENT;
  1051. ModEnd: TOmfRecord_MODEND;
  1052. begin
  1053. { write header record }
  1054. RawRecord:=TOmfRawRecord.Create;
  1055. Header:=TOmfRecord_THEADR.Create;
  1056. Header.ModuleName:=mangledname;
  1057. Header.EncodeTo(RawRecord);
  1058. RawRecord.WriteTo(FWriter);
  1059. Header.Free;
  1060. { write IMPDEF record }
  1061. DllImport_COMENT:=TOmfRecord_COMENT.Create;
  1062. DllImport_COMENT.CommentClass:=CC_OmfExtension;
  1063. if ordnr <= 0 then
  1064. begin
  1065. if afuncname=mangledname then
  1066. DllImport_COMENT.CommentString:=#1#0+Chr(Length(mangledname))+mangledname+Chr(Length(dllname))+dllname+#0
  1067. else
  1068. DllImport_COMENT.CommentString:=#1#0+Chr(Length(mangledname))+mangledname+Chr(Length(dllname))+dllname+Chr(Length(afuncname))+afuncname;
  1069. end
  1070. else
  1071. DllImport_COMENT.CommentString:=#1#1+Chr(Length(mangledname))+mangledname+Chr(Length(dllname))+dllname+Chr(ordnr and $ff)+Chr((ordnr shr 8) and $ff);
  1072. DllImport_COMENT.EncodeTo(RawRecord);
  1073. RawRecord.WriteTo(FWriter);
  1074. DllImport_COMENT.Free;
  1075. { write MODEND record }
  1076. ModEnd:=TOmfRecord_MODEND.Create;
  1077. ModEnd.EncodeTo(RawRecord);
  1078. RawRecord.WriteTo(FWriter);
  1079. ModEnd.Free;
  1080. RawRecord.Free;
  1081. end;
  1082. {****************************************************************************
  1083. TOmfObjInput
  1084. ****************************************************************************}
  1085. function TOmfObjInput.PeekNextRecordType: Byte;
  1086. var
  1087. OldPos: LongInt;
  1088. begin
  1089. OldPos:=FReader.Pos;
  1090. if not FReader.read(Result, 1) then
  1091. begin
  1092. InputError('Unexpected end of file');
  1093. Result:=0;
  1094. exit;
  1095. end;
  1096. FReader.seek(OldPos);
  1097. end;
  1098. function TOmfObjInput.ReadLNames(RawRec: TOmfRawRecord): Boolean;
  1099. var
  1100. LNamesRec: TOmfRecord_LNAMES;
  1101. begin
  1102. Result:=False;
  1103. LNamesRec:=TOmfRecord_LNAMES.Create;
  1104. LNamesRec.Names:=LNames;
  1105. LNamesRec.DecodeFrom(RawRec);
  1106. LNamesRec.Free;
  1107. Result:=True;
  1108. end;
  1109. function TOmfObjInput.ReadSegDef(RawRec: TOmfRawRecord; objdata: TObjData): Boolean;
  1110. var
  1111. SegDefRec: TOmfRecord_SEGDEF;
  1112. SegmentName,SegClassName,OverlayName: string;
  1113. SecAlign: LongInt;
  1114. secoptions: TObjSectionOptions;
  1115. objsec: TOmfObjSection;
  1116. begin
  1117. Result:=False;
  1118. SegDefRec:=TOmfRecord_SEGDEF.Create;
  1119. SegDefRec.DecodeFrom(RawRec);
  1120. if (SegDefRec.SegmentNameIndex<1) or (SegDefRec.SegmentNameIndex>LNames.Count) then
  1121. begin
  1122. InputError('Segment name index out of range');
  1123. SegDefRec.Free;
  1124. exit;
  1125. end;
  1126. SegmentName:=LNames[SegDefRec.SegmentNameIndex];
  1127. if (SegDefRec.ClassNameIndex<1) or (SegDefRec.ClassNameIndex>LNames.Count) then
  1128. begin
  1129. InputError('Segment class name index out of range');
  1130. SegDefRec.Free;
  1131. exit;
  1132. end;
  1133. SegClassName:=LNames[SegDefRec.ClassNameIndex];
  1134. if (SegDefRec.OverlayNameIndex<1) or (SegDefRec.OverlayNameIndex>LNames.Count) then
  1135. begin
  1136. InputError('Segment overlay name index out of range');
  1137. SegDefRec.Free;
  1138. exit;
  1139. end;
  1140. OverlayName:=LNames[SegDefRec.OverlayNameIndex];
  1141. SecAlign:=1; // otherwise warning prohibits compilation
  1142. case SegDefRec.Alignment of
  1143. saRelocatableByteAligned:
  1144. SecAlign:=1;
  1145. saRelocatableWordAligned:
  1146. SecAlign:=2;
  1147. saRelocatableParaAligned:
  1148. SecAlign:=16;
  1149. saRelocatableDWordAligned:
  1150. SecAlign:=4;
  1151. saRelocatablePageAligned:
  1152. SecAlign:=256;
  1153. saNotSupported:
  1154. SecAlign:=4096;
  1155. saAbsolute:
  1156. begin
  1157. InputError('Absolute segment alignment not supported');
  1158. SegDefRec.Free;
  1159. exit;
  1160. end;
  1161. saNotDefined:
  1162. begin
  1163. InputError('Invalid (unsupported/undefined) OMF segment alignment');
  1164. SegDefRec.Free;
  1165. exit;
  1166. end;
  1167. end;
  1168. if not CaseSensitiveSegments then
  1169. begin
  1170. SegmentName:=UpCase(SegmentName);
  1171. SegClassName:=UpCase(SegClassName);
  1172. OverlayName:=UpCase(OverlayName);
  1173. end;
  1174. { hack for supporting object modules, generated by Borland's BINOBJ tool }
  1175. if (SegClassName='') and (SegmentName='CODE') then
  1176. begin
  1177. SegmentName:=InputFileName;
  1178. SegClassName:='CODE';
  1179. end;
  1180. secoptions:=[];
  1181. objsec:=TOmfObjSection(objdata.createsection(SegmentName+'||'+SegClassName,SecAlign,secoptions,false));
  1182. objsec.FClassName:=SegClassName;
  1183. objsec.FOverlayName:=OverlayName;
  1184. objsec.FCombination:=SegDefRec.Combination;
  1185. objsec.FUse:=SegDefRec.Use;
  1186. if SegDefRec.SegmentLength>High(objsec.Size) then
  1187. begin
  1188. InputError('Segment too large');
  1189. SegDefRec.Free;
  1190. exit;
  1191. end;
  1192. objsec.Size:=SegDefRec.SegmentLength;
  1193. if SegClassName='DWARF' then
  1194. objsec.SecOptions:=objsec.SecOptions+[oso_debug];
  1195. if (SegClassName='HEAP') or
  1196. (SegClassName='STACK') or (SegDefRec.Combination=scStack) or
  1197. (SegClassName='BEGDATA') or
  1198. (SegmentName='FPC') then
  1199. objsec.SecOptions:=objsec.SecOptions+[oso_keep];
  1200. SegDefRec.Free;
  1201. Result:=True;
  1202. end;
  1203. function TOmfObjInput.ReadGrpDef(RawRec: TOmfRawRecord; objdata: TObjData): Boolean;
  1204. var
  1205. GrpDefRec: TOmfRecord_GRPDEF;
  1206. GroupName: string;
  1207. SecGroup: TObjSectionGroup;
  1208. i,SegIndex: Integer;
  1209. begin
  1210. Result:=False;
  1211. GrpDefRec:=TOmfRecord_GRPDEF.Create;
  1212. GrpDefRec.DecodeFrom(RawRec);
  1213. if (GrpDefRec.GroupNameIndex<1) or (GrpDefRec.GroupNameIndex>LNames.Count) then
  1214. begin
  1215. InputError('Group name index out of range');
  1216. GrpDefRec.Free;
  1217. exit;
  1218. end;
  1219. GroupName:=LNames[GrpDefRec.GroupNameIndex];
  1220. if not CaseSensitiveSegments then
  1221. GroupName:=UpCase(GroupName);
  1222. SecGroup:=objdata.createsectiongroup(GroupName);
  1223. SetLength(SecGroup.members,Length(GrpDefRec.SegmentList));
  1224. for i:=0 to Length(GrpDefRec.SegmentList)-1 do
  1225. begin
  1226. SegIndex:=GrpDefRec.SegmentList[i];
  1227. if (SegIndex<1) or (SegIndex>objdata.ObjSectionList.Count) then
  1228. begin
  1229. InputError('Segment name index out of range in group definition');
  1230. GrpDefRec.Free;
  1231. exit;
  1232. end;
  1233. SecGroup.members[i]:=TOmfObjSection(objdata.ObjSectionList[SegIndex-1]);
  1234. end;
  1235. GrpDefRec.Free;
  1236. Result:=True;
  1237. end;
  1238. function TOmfObjInput.ReadExtDef(RawRec: TOmfRawRecord; objdata: TObjData): Boolean;
  1239. var
  1240. ExtDefRec: TOmfRecord_EXTDEF;
  1241. ExtDefElem: TOmfExternalNameElement;
  1242. OldCount,NewCount,i: Integer;
  1243. objsym: TObjSymbol;
  1244. symname: TSymStr;
  1245. begin
  1246. Result:=False;
  1247. ExtDefRec:=TOmfRecord_EXTDEF.Create;
  1248. ExtDefRec.ExternalNames:=ExtDefs;
  1249. OldCount:=ExtDefs.Count;
  1250. ExtDefRec.DecodeFrom(RawRec);
  1251. NewCount:=ExtDefs.Count;
  1252. for i:=OldCount to NewCount-1 do
  1253. begin
  1254. ExtDefElem:=TOmfExternalNameElement(ExtDefs[i]);
  1255. symname:=ExtDefElem.Name;
  1256. if not CaseSensitiveSymbols then
  1257. symname:=UpCase(symname);
  1258. objsym:=objdata.CreateSymbol(symname);
  1259. objsym.bind:=AB_EXTERNAL;
  1260. objsym.typ:=AT_FUNCTION;
  1261. objsym.objsection:=nil;
  1262. objsym.offset:=0;
  1263. objsym.size:=0;
  1264. end;
  1265. ExtDefRec.Free;
  1266. Result:=True;
  1267. end;
  1268. function TOmfObjInput.ReadPubDef(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  1269. var
  1270. PubDefRec: TOmfRecord_PUBDEF;
  1271. PubDefElem: TOmfPublicNameElement;
  1272. OldCount,NewCount,i: Integer;
  1273. basegroup: TObjSectionGroup;
  1274. objsym: TObjSymbol;
  1275. objsec: TOmfObjSection;
  1276. symname: TSymStr;
  1277. begin
  1278. Result:=False;
  1279. PubDefRec:=TOmfRecord_PUBDEF.Create;
  1280. PubDefRec.PublicNames:=PubDefs;
  1281. OldCount:=PubDefs.Count;
  1282. PubDefRec.DecodeFrom(RawRec);
  1283. NewCount:=PubDefs.Count;
  1284. if (PubDefRec.BaseGroupIndex<0) or (PubDefRec.BaseGroupIndex>objdata.GroupsList.Count) then
  1285. begin
  1286. InputError('Public symbol''s group name index out of range');
  1287. PubDefRec.Free;
  1288. exit;
  1289. end;
  1290. if PubDefRec.BaseGroupIndex<>0 then
  1291. basegroup:=TObjSectionGroup(objdata.GroupsList[PubDefRec.BaseGroupIndex-1])
  1292. else
  1293. basegroup:=nil;
  1294. if (PubDefRec.BaseSegmentIndex<0) or (PubDefRec.BaseSegmentIndex>objdata.ObjSectionList.Count) then
  1295. begin
  1296. InputError('Public symbol''s segment name index out of range');
  1297. PubDefRec.Free;
  1298. exit;
  1299. end;
  1300. if PubDefRec.BaseSegmentIndex=0 then
  1301. begin
  1302. InputError('Public symbol uses absolute addressing, which is not supported by this linker');
  1303. PubDefRec.Free;
  1304. exit;
  1305. end;
  1306. objsec:=TOmfObjSection(objdata.ObjSectionList[PubDefRec.BaseSegmentIndex-1]);
  1307. for i:=OldCount to NewCount-1 do
  1308. begin
  1309. PubDefElem:=TOmfPublicNameElement(PubDefs[i]);
  1310. symname:=PubDefElem.Name;
  1311. if not CaseSensitiveSymbols then
  1312. symname:=UpCase(symname);
  1313. objsym:=objdata.CreateSymbol(symname);
  1314. if PubDefElem.IsLocal then
  1315. objsym.bind:=AB_LOCAL
  1316. else
  1317. objsym.bind:=AB_GLOBAL;
  1318. objsym.typ:=AT_FUNCTION;
  1319. objsym.group:=basegroup;
  1320. objsym.objsection:=objsec;
  1321. objsym.offset:=PubDefElem.PublicOffset;
  1322. objsym.size:=0;
  1323. end;
  1324. PubDefRec.Free;
  1325. Result:=True;
  1326. end;
  1327. function TOmfObjInput.ReadModEnd(RawRec: TOmfRawRecord; objdata:TObjData): Boolean;
  1328. var
  1329. ModEndRec: TOmfRecord_MODEND;
  1330. objsym: TObjSymbol;
  1331. objsec: TOmfObjSection;
  1332. basegroup: TObjSectionGroup;
  1333. begin
  1334. Result:=False;
  1335. ModEndRec:=TOmfRecord_MODEND.Create;
  1336. ModEndRec.DecodeFrom(RawRec);
  1337. if ModEndRec.HasStartAddress then
  1338. begin
  1339. if not ModEndRec.LogicalStartAddress then
  1340. begin
  1341. InputError('Physical start address not supported');
  1342. ModEndRec.Free;
  1343. exit;
  1344. end;
  1345. if not (ModEndRec.TargetMethod in [ftmSegmentIndex,ftmSegmentIndexNoDisp]) then
  1346. begin
  1347. InputError('Target method for start address other than "Segment Index" is not supported');
  1348. ModEndRec.Free;
  1349. exit;
  1350. end;
  1351. if (ModEndRec.TargetDatum<1) or (ModEndRec.TargetDatum>objdata.ObjSectionList.Count) then
  1352. begin
  1353. InputError('Segment name index for start address out of range');
  1354. ModEndRec.Free;
  1355. exit;
  1356. end;
  1357. case ModEndRec.FrameMethod of
  1358. ffmSegmentIndex:
  1359. begin
  1360. if (ModEndRec.FrameDatum<1) or (ModEndRec.FrameDatum>objdata.ObjSectionList.Count) then
  1361. begin
  1362. InputError('Frame segment name index for start address out of range');
  1363. ModEndRec.Free;
  1364. exit;
  1365. end;
  1366. if ModEndRec.FrameDatum<>ModEndRec.TargetDatum then
  1367. begin
  1368. InputError('Frame segment different than target segment is not supported supported for start address');
  1369. ModEndRec.Free;
  1370. exit;
  1371. end;
  1372. basegroup:=nil;
  1373. end;
  1374. ffmGroupIndex:
  1375. begin
  1376. if (ModEndRec.FrameDatum<1) or (ModEndRec.FrameDatum>objdata.GroupsList.Count) then
  1377. begin
  1378. InputError('Frame group name index for start address out of range');
  1379. ModEndRec.Free;
  1380. exit;
  1381. end;
  1382. basegroup:=TObjSectionGroup(objdata.GroupsList[ModEndRec.FrameDatum-1]);
  1383. end;
  1384. else
  1385. begin
  1386. InputError('Frame method for start address other than "Segment Index" or "Group Index" is not supported');
  1387. ModEndRec.Free;
  1388. exit;
  1389. end;
  1390. end;
  1391. objsec:=TOmfObjSection(objdata.ObjSectionList[ModEndRec.TargetDatum-1]);
  1392. objsym:=objdata.CreateSymbol('..start');
  1393. objsym.bind:=AB_GLOBAL;
  1394. objsym.typ:=AT_FUNCTION;
  1395. objsym.group:=basegroup;
  1396. objsym.objsection:=objsec;
  1397. objsym.offset:=ModEndRec.TargetDisplacement;
  1398. objsym.size:=0;
  1399. end;
  1400. ModEndRec.Free;
  1401. Result:=True;
  1402. end;
  1403. function TOmfObjInput.ReadLeOrLiDataAndFixups(RawRec: TOmfRawRecord; objdata: TObjData): Boolean;
  1404. var
  1405. Is32Bit: Boolean;
  1406. NextOfs: Integer;
  1407. SegmentIndex: Integer;
  1408. EnumeratedDataOffset: DWord;
  1409. BlockLength: Integer;
  1410. objsec: TOmfObjSection;
  1411. FixupRawRec: TOmfRawRecord=nil;
  1412. Fixup: TOmfSubRecord_FIXUP;
  1413. Thread: TOmfSubRecord_THREAD;
  1414. FixuppWithoutLeOrLiData: Boolean=False;
  1415. begin
  1416. Result:=False;
  1417. case RawRec.RecordType of
  1418. RT_LEDATA,RT_LEDATA32:
  1419. begin
  1420. Is32Bit:=RawRec.RecordType=RT_LEDATA32;
  1421. NextOfs:=RawRec.ReadIndexedRef(0,SegmentIndex);
  1422. if Is32Bit then
  1423. begin
  1424. if (NextOfs+3)>=RawRec.RecordLength then
  1425. internalerror(2015040504);
  1426. EnumeratedDataOffset := RawRec.RawData[NextOfs]+
  1427. (RawRec.RawData[NextOfs+1] shl 8)+
  1428. (RawRec.RawData[NextOfs+2] shl 16)+
  1429. (RawRec.RawData[NextOfs+3] shl 24);
  1430. Inc(NextOfs,4);
  1431. end
  1432. else
  1433. begin
  1434. if (NextOfs+1)>=RawRec.RecordLength then
  1435. internalerror(2015040504);
  1436. EnumeratedDataOffset := RawRec.RawData[NextOfs]+
  1437. (RawRec.RawData[NextOfs+1] shl 8);
  1438. Inc(NextOfs,2);
  1439. end;
  1440. BlockLength:=RawRec.RecordLength-NextOfs-1;
  1441. if BlockLength<0 then
  1442. internalerror(2015060501);
  1443. if BlockLength>1024 then
  1444. begin
  1445. InputError('LEDATA contains more than 1024 bytes of data');
  1446. exit;
  1447. end;
  1448. if (SegmentIndex<1) or (SegmentIndex>objdata.ObjSectionList.Count) then
  1449. begin
  1450. InputError('Segment index in LEDATA field is out of range');
  1451. exit;
  1452. end;
  1453. objsec:=TOmfObjSection(objdata.ObjSectionList[SegmentIndex-1]);
  1454. objsec.SecOptions:=objsec.SecOptions+[oso_Data];
  1455. if (objsec.Data.Size>EnumeratedDataOffset) then
  1456. begin
  1457. InputError('LEDATA enumerated data offset field out of sequence');
  1458. exit;
  1459. end;
  1460. if (EnumeratedDataOffset+BlockLength)>objsec.Size then
  1461. begin
  1462. InputError('LEDATA goes beyond the segment size declared in the SEGDEF record');
  1463. exit;
  1464. end;
  1465. objsec.Data.seek(EnumeratedDataOffset);
  1466. objsec.Data.write(RawRec.RawData[NextOfs],BlockLength);
  1467. end;
  1468. RT_LIDATA,RT_LIDATA32:
  1469. begin
  1470. InputError('LIDATA records are not supported');
  1471. exit;
  1472. end;
  1473. RT_FIXUPP,RT_FIXUPP32:
  1474. begin
  1475. FixuppWithoutLeOrLiData:=True;
  1476. { a hack, used to indicate, that we must process this record }
  1477. { (RawRec) first in the FIXUPP record processing loop that follows }
  1478. FixupRawRec:=RawRec;
  1479. end;
  1480. else
  1481. internalerror(2015040301);
  1482. end;
  1483. { also read all the FIXUPP records that may follow; }
  1484. { (FixupRawRec=RawRec) indicates that we must process RawRec first, but }
  1485. { without freeing it }
  1486. while (FixupRawRec=RawRec) or (PeekNextRecordType in [RT_FIXUPP,RT_FIXUPP32]) do
  1487. begin
  1488. if FixupRawRec<>RawRec then
  1489. begin
  1490. FixupRawRec:=TOmfRawRecord.Create;
  1491. FixupRawRec.ReadFrom(FReader);
  1492. if not FRawRecord.VerifyChecksumByte then
  1493. begin
  1494. InputError('Invalid checksum in OMF record');
  1495. FixupRawRec.Free;
  1496. exit;
  1497. end;
  1498. end;
  1499. NextOfs:=0;
  1500. Thread:=TOmfSubRecord_THREAD.Create;
  1501. Fixup:=TOmfSubRecord_FIXUP.Create;
  1502. Fixup.Is32Bit:=FixupRawRec.RecordType=RT_FIXUPP32;
  1503. Fixup.DataRecordStartOffset:=EnumeratedDataOffset;
  1504. while NextOfs<(FixupRawRec.RecordLength-1) do
  1505. begin
  1506. if (FixupRawRec.RawData[NextOfs] and $80)<>0 then
  1507. begin
  1508. { FIXUP subrecord }
  1509. if FixuppWithoutLeOrLiData then
  1510. begin
  1511. InputError('FIXUP subrecord without previous LEDATA or LIDATA record');
  1512. Fixup.Free;
  1513. Thread.Free;
  1514. if FixupRawRec<>RawRec then
  1515. FixupRawRec.Free;
  1516. exit;
  1517. end;
  1518. NextOfs:=Fixup.ReadAt(FixupRawRec,NextOfs);
  1519. Fixup.ResolveByThread(FFixupThreads);
  1520. ImportOmfFixup(objdata,objsec,Fixup);
  1521. end
  1522. else
  1523. begin
  1524. { THREAD subrecord }
  1525. NextOfs:=Thread.ReadAt(FixupRawRec,NextOfs);
  1526. Thread.ApplyTo(FFixupThreads);
  1527. end;
  1528. end;
  1529. Fixup.Free;
  1530. Thread.Free;
  1531. if FixupRawRec<>RawRec then
  1532. FixupRawRec.Free;
  1533. { always set it to null, so that we read the next record on the next }
  1534. { loop iteration (this ensures that FixupRawRec<>RawRec, without }
  1535. { freeing RawRec) }
  1536. FixupRawRec:=nil;
  1537. end;
  1538. Result:=True;
  1539. end;
  1540. function TOmfObjInput.ImportOmfFixup(objdata: TObjData; objsec: TOmfObjSection; Fixup: TOmfSubRecord_FIXUP): Boolean;
  1541. var
  1542. reloc: TOmfRelocation;
  1543. sym: TObjSymbol;
  1544. RelocType: TObjRelocationType;
  1545. target_section: TOmfObjSection;
  1546. target_group: TObjSectionGroup;
  1547. begin
  1548. Result:=False;
  1549. { range check location }
  1550. if (Fixup.LocationOffset+Fixup.LocationSize)>objsec.Size then
  1551. begin
  1552. InputError('Fixup location exceeds the current segment boundary');
  1553. exit;
  1554. end;
  1555. { range check target datum }
  1556. case Fixup.TargetMethod of
  1557. ftmSegmentIndex:
  1558. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>objdata.ObjSectionList.Count) then
  1559. begin
  1560. InputError('Segment name index in SI(<segment name>),<displacement> fixup target is out of range');
  1561. exit;
  1562. end;
  1563. ftmSegmentIndexNoDisp:
  1564. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>objdata.ObjSectionList.Count) then
  1565. begin
  1566. InputError('Segment name index in SI(<segment name>) fixup target is out of range');
  1567. exit;
  1568. end;
  1569. ftmGroupIndex:
  1570. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>objdata.GroupsList.Count) then
  1571. begin
  1572. InputError('Group name index in GI(<group name>),<displacement> fixup target is out of range');
  1573. exit;
  1574. end;
  1575. ftmGroupIndexNoDisp:
  1576. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>objdata.GroupsList.Count) then
  1577. begin
  1578. InputError('Group name index in GI(<group name>) fixup target is out of range');
  1579. exit;
  1580. end;
  1581. ftmExternalIndex:
  1582. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>ExtDefs.Count) then
  1583. begin
  1584. InputError('External symbol name index in EI(<symbol name>),<displacement> fixup target is out of range');
  1585. exit;
  1586. end;
  1587. ftmExternalIndexNoDisp:
  1588. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>ExtDefs.Count) then
  1589. begin
  1590. InputError('External symbol name index in EI(<symbol name>) fixup target is out of range');
  1591. exit;
  1592. end;
  1593. end;
  1594. { range check frame datum }
  1595. case Fixup.FrameMethod of
  1596. ffmSegmentIndex:
  1597. if (Fixup.FrameDatum<1) or (Fixup.FrameDatum>objdata.ObjSectionList.Count) then
  1598. begin
  1599. InputError('Segment name index in SI(<segment name>) fixup frame is out of range');
  1600. exit;
  1601. end;
  1602. ffmGroupIndex:
  1603. if (Fixup.FrameDatum<1) or (Fixup.FrameDatum>objdata.GroupsList.Count) then
  1604. begin
  1605. InputError('Group name index in GI(<group name>) fixup frame is out of range');
  1606. exit;
  1607. end;
  1608. ffmExternalIndex:
  1609. if (Fixup.TargetDatum<1) or (Fixup.TargetDatum>ExtDefs.Count) then
  1610. begin
  1611. InputError('External symbol name index in EI(<symbol name>) fixup frame is out of range');
  1612. exit;
  1613. end;
  1614. end;
  1615. if Fixup.TargetMethod in [ftmExternalIndex,ftmExternalIndexNoDisp] then
  1616. begin
  1617. sym:=objdata.symbolref(TOmfExternalNameElement(ExtDefs[Fixup.TargetDatum-1]).Name);
  1618. RelocType:=RELOC_NONE;
  1619. case Fixup.LocationType of
  1620. fltOffset:
  1621. case Fixup.Mode of
  1622. fmSegmentRelative:
  1623. RelocType:=RELOC_ABSOLUTE16;
  1624. fmSelfRelative:
  1625. RelocType:=RELOC_RELATIVE16;
  1626. end;
  1627. fltOffset32:
  1628. case Fixup.Mode of
  1629. fmSegmentRelative:
  1630. RelocType:=RELOC_ABSOLUTE32;
  1631. fmSelfRelative:
  1632. RelocType:=RELOC_RELATIVE32;
  1633. end;
  1634. fltBase:
  1635. case Fixup.Mode of
  1636. fmSegmentRelative:
  1637. RelocType:=RELOC_SEG;
  1638. fmSelfRelative:
  1639. RelocType:=RELOC_SEGREL;
  1640. end;
  1641. fltFarPointer:
  1642. case Fixup.Mode of
  1643. fmSegmentRelative:
  1644. RelocType:=RELOC_FARPTR;
  1645. fmSelfRelative:
  1646. RelocType:=RELOC_FARPTR_RELATIVEOFFSET;
  1647. end;
  1648. fltFarPointer48:
  1649. case Fixup.Mode of
  1650. fmSegmentRelative:
  1651. RelocType:=RELOC_FARPTR48;
  1652. fmSelfRelative:
  1653. RelocType:=RELOC_FARPTR48_RELATIVEOFFSET;
  1654. end;
  1655. end;
  1656. if RelocType=RELOC_NONE then
  1657. begin
  1658. InputError('Unsupported fixup location type '+tostr(Ord(Fixup.LocationType))+' with mode '+tostr(ord(Fixup.Mode))+' in external reference to '+sym.Name);
  1659. exit;
  1660. end;
  1661. reloc:=TOmfRelocation.CreateSymbol(Fixup.LocationOffset,sym,RelocType);
  1662. objsec.ObjRelocations.Add(reloc);
  1663. case Fixup.FrameMethod of
  1664. ffmTarget:
  1665. {nothing};
  1666. ffmGroupIndex:
  1667. reloc.FrameGroup:=TObjSectionGroup(objdata.GroupsList[Fixup.FrameDatum-1]).Name;
  1668. else
  1669. begin
  1670. InputError('Unsupported frame method '+IntToStr(Ord(Fixup.FrameMethod))+' in external reference to '+sym.Name);
  1671. exit;
  1672. end;
  1673. end;
  1674. if Fixup.TargetDisplacement<>0 then
  1675. begin
  1676. InputError('Unsupported nonzero target displacement '+IntToStr(Fixup.TargetDisplacement)+' in external reference to '+sym.Name);
  1677. exit;
  1678. end;
  1679. end
  1680. else if Fixup.TargetMethod in [ftmSegmentIndex,ftmSegmentIndexNoDisp] then
  1681. begin
  1682. target_section:=TOmfObjSection(objdata.ObjSectionList[Fixup.TargetDatum-1]);
  1683. RelocType:=RELOC_NONE;
  1684. case Fixup.LocationType of
  1685. fltOffset:
  1686. case Fixup.Mode of
  1687. fmSegmentRelative:
  1688. RelocType:=RELOC_ABSOLUTE16;
  1689. fmSelfRelative:
  1690. RelocType:=RELOC_RELATIVE16;
  1691. end;
  1692. fltOffset32:
  1693. case Fixup.Mode of
  1694. fmSegmentRelative:
  1695. RelocType:=RELOC_ABSOLUTE32;
  1696. fmSelfRelative:
  1697. RelocType:=RELOC_RELATIVE32;
  1698. end;
  1699. fltBase:
  1700. case Fixup.Mode of
  1701. fmSegmentRelative:
  1702. RelocType:=RELOC_SEG;
  1703. fmSelfRelative:
  1704. RelocType:=RELOC_SEGREL;
  1705. end;
  1706. fltFarPointer:
  1707. case Fixup.Mode of
  1708. fmSegmentRelative:
  1709. RelocType:=RELOC_FARPTR;
  1710. fmSelfRelative:
  1711. RelocType:=RELOC_FARPTR_RELATIVEOFFSET;
  1712. end;
  1713. fltFarPointer48:
  1714. case Fixup.Mode of
  1715. fmSegmentRelative:
  1716. RelocType:=RELOC_FARPTR48;
  1717. fmSelfRelative:
  1718. RelocType:=RELOC_FARPTR48_RELATIVEOFFSET;
  1719. end;
  1720. end;
  1721. if RelocType=RELOC_NONE then
  1722. begin
  1723. InputError('Unsupported fixup location type '+tostr(Ord(Fixup.LocationType))+' with mode '+tostr(ord(Fixup.Mode)));
  1724. exit;
  1725. end;
  1726. reloc:=TOmfRelocation.CreateSection(Fixup.LocationOffset,target_section,RelocType);
  1727. objsec.ObjRelocations.Add(reloc);
  1728. case Fixup.FrameMethod of
  1729. ffmTarget:
  1730. {nothing};
  1731. ffmGroupIndex:
  1732. reloc.FrameGroup:=TObjSectionGroup(objdata.GroupsList[Fixup.FrameDatum-1]).Name;
  1733. else
  1734. begin
  1735. InputError('Unsupported frame method '+IntToStr(Ord(Fixup.FrameMethod))+' in reference to segment '+target_section.Name);
  1736. exit;
  1737. end;
  1738. end;
  1739. if Fixup.TargetDisplacement<>0 then
  1740. begin
  1741. InputError('Unsupported nonzero target displacement '+IntToStr(Fixup.TargetDisplacement)+' in reference to segment '+target_section.Name);
  1742. exit;
  1743. end;
  1744. end
  1745. else if Fixup.TargetMethod in [ftmGroupIndex,ftmGroupIndexNoDisp] then
  1746. begin
  1747. target_group:=TObjSectionGroup(objdata.GroupsList[Fixup.TargetDatum-1]);
  1748. RelocType:=RELOC_NONE;
  1749. case Fixup.LocationType of
  1750. fltOffset:
  1751. case Fixup.Mode of
  1752. fmSegmentRelative:
  1753. RelocType:=RELOC_ABSOLUTE16;
  1754. fmSelfRelative:
  1755. RelocType:=RELOC_RELATIVE16;
  1756. end;
  1757. fltOffset32:
  1758. case Fixup.Mode of
  1759. fmSegmentRelative:
  1760. RelocType:=RELOC_ABSOLUTE32;
  1761. fmSelfRelative:
  1762. RelocType:=RELOC_RELATIVE32;
  1763. end;
  1764. fltBase:
  1765. case Fixup.Mode of
  1766. fmSegmentRelative:
  1767. RelocType:=RELOC_SEG;
  1768. fmSelfRelative:
  1769. RelocType:=RELOC_SEGREL;
  1770. end;
  1771. fltFarPointer:
  1772. case Fixup.Mode of
  1773. fmSegmentRelative:
  1774. RelocType:=RELOC_FARPTR;
  1775. fmSelfRelative:
  1776. RelocType:=RELOC_FARPTR_RELATIVEOFFSET;
  1777. end;
  1778. fltFarPointer48:
  1779. case Fixup.Mode of
  1780. fmSegmentRelative:
  1781. RelocType:=RELOC_FARPTR48;
  1782. fmSelfRelative:
  1783. RelocType:=RELOC_FARPTR48_RELATIVEOFFSET;
  1784. end;
  1785. end;
  1786. if RelocType=RELOC_NONE then
  1787. begin
  1788. InputError('Unsupported fixup location type '+tostr(Ord(Fixup.LocationType))+' with mode '+tostr(ord(Fixup.Mode)));
  1789. exit;
  1790. end;
  1791. reloc:=TOmfRelocation.CreateGroup(Fixup.LocationOffset,target_group,RelocType);
  1792. objsec.ObjRelocations.Add(reloc);
  1793. case Fixup.FrameMethod of
  1794. ffmTarget:
  1795. {nothing};
  1796. else
  1797. begin
  1798. InputError('Unsupported frame method '+IntToStr(Ord(Fixup.FrameMethod))+' in reference to group '+target_group.Name);
  1799. exit;
  1800. end;
  1801. end;
  1802. if Fixup.TargetDisplacement<>0 then
  1803. begin
  1804. InputError('Unsupported nonzero target displacement '+IntToStr(Fixup.TargetDisplacement)+' in reference to group '+target_group.Name);
  1805. exit;
  1806. end;
  1807. end
  1808. else
  1809. begin
  1810. {todo: convert other fixup types as well }
  1811. InputError('Unsupported fixup target method '+IntToStr(Ord(Fixup.TargetMethod)));
  1812. exit;
  1813. end;
  1814. Result:=True;
  1815. end;
  1816. constructor TOmfObjInput.create;
  1817. begin
  1818. inherited create;
  1819. cobjdata:=TOmfObjData;
  1820. FLNames:=TOmfOrderedNameCollection.Create(True);
  1821. FExtDefs:=TFPHashObjectList.Create;
  1822. FPubDefs:=TFPHashObjectList.Create;
  1823. FFixupThreads:=TOmfThreads.Create;
  1824. FRawRecord:=TOmfRawRecord.Create;
  1825. CaseSensitiveSegments:=False;
  1826. CaseSensitiveSymbols:=True;
  1827. end;
  1828. destructor TOmfObjInput.destroy;
  1829. begin
  1830. FRawRecord.Free;
  1831. FFixupThreads.Free;
  1832. FPubDefs.Free;
  1833. FExtDefs.Free;
  1834. FLNames.Free;
  1835. inherited destroy;
  1836. end;
  1837. class function TOmfObjInput.CanReadObjData(AReader: TObjectreader): boolean;
  1838. var
  1839. b: Byte;
  1840. begin
  1841. result:=false;
  1842. if AReader.Read(b,sizeof(b)) then
  1843. begin
  1844. if b=RT_THEADR then
  1845. { TODO: check additional fields }
  1846. result:=true;
  1847. end;
  1848. AReader.Seek(0);
  1849. end;
  1850. function TOmfObjInput.ReadObjData(AReader: TObjectreader; out objdata: TObjData): boolean;
  1851. begin
  1852. FReader:=AReader;
  1853. InputFileName:=AReader.FileName;
  1854. objdata:=CObjData.Create(InputFileName);
  1855. result:=false;
  1856. LNames.Clear;
  1857. ExtDefs.Clear;
  1858. FRawRecord.ReadFrom(FReader);
  1859. if not FRawRecord.VerifyChecksumByte then
  1860. begin
  1861. InputError('Invalid checksum in OMF record');
  1862. exit;
  1863. end;
  1864. if FRawRecord.RecordType<>RT_THEADR then
  1865. begin
  1866. InputError('Can''t read OMF header');
  1867. exit;
  1868. end;
  1869. repeat
  1870. FRawRecord.ReadFrom(FReader);
  1871. if not FRawRecord.VerifyChecksumByte then
  1872. begin
  1873. InputError('Invalid checksum in OMF record');
  1874. exit;
  1875. end;
  1876. case FRawRecord.RecordType of
  1877. RT_LNAMES:
  1878. if not ReadLNames(FRawRecord) then
  1879. exit;
  1880. RT_SEGDEF,RT_SEGDEF32:
  1881. if not ReadSegDef(FRawRecord,objdata) then
  1882. exit;
  1883. RT_GRPDEF:
  1884. if not ReadGrpDef(FRawRecord,objdata) then
  1885. exit;
  1886. RT_COMENT:
  1887. begin
  1888. {todo}
  1889. end;
  1890. RT_EXTDEF:
  1891. if not ReadExtDef(FRawRecord,objdata) then
  1892. exit;
  1893. RT_LPUBDEF,RT_LPUBDEF32,
  1894. RT_PUBDEF,RT_PUBDEF32:
  1895. if not ReadPubDef(FRawRecord,objdata) then
  1896. exit;
  1897. RT_LEDATA,RT_LEDATA32,
  1898. RT_LIDATA,RT_LIDATA32,
  1899. RT_FIXUPP,RT_FIXUPP32:
  1900. if not ReadLeOrLiDataAndFixups(FRawRecord,objdata) then
  1901. exit;
  1902. RT_MODEND,RT_MODEND32:
  1903. if not ReadModEnd(FRawRecord,objdata) then
  1904. exit;
  1905. RT_LINNUM,RT_LINNUM32:
  1906. ;
  1907. else
  1908. begin
  1909. InputError('Unsupported OMF record type $'+HexStr(FRawRecord.RecordType,2));
  1910. exit;
  1911. end;
  1912. end;
  1913. until FRawRecord.RecordType in [RT_MODEND,RT_MODEND32];
  1914. result:=true;
  1915. end;
  1916. {****************************************************************************
  1917. TMZExeHeader
  1918. ****************************************************************************}
  1919. procedure TMZExeHeader.SetHeaderSizeAlignment(AValue: Integer);
  1920. begin
  1921. if (AValue<16) or ((AValue mod 16) <> 0) then
  1922. Internalerror(2015060601);
  1923. FHeaderSizeAlignment:=AValue;
  1924. end;
  1925. constructor TMZExeHeader.Create;
  1926. begin
  1927. FHeaderSizeAlignment:=16;
  1928. end;
  1929. procedure TMZExeHeader.WriteTo(aWriter: TObjectWriter);
  1930. var
  1931. NumRelocs: Word;
  1932. HeaderSizeInBytes: DWord;
  1933. HeaderParagraphs: Word;
  1934. RelocTableOffset: Word;
  1935. BytesInLastBlock: Word;
  1936. BlocksInFile: Word;
  1937. HeaderBytes: array [0..$1B] of Byte;
  1938. RelocBytes: array [0..3] of Byte;
  1939. TotalExeSize: DWord;
  1940. i: Integer;
  1941. begin
  1942. NumRelocs:=Length(Relocations);
  1943. RelocTableOffset:=$1C+Length(ExtraHeaderData);
  1944. HeaderSizeInBytes:=Align(RelocTableOffset+4*NumRelocs,16);
  1945. HeaderParagraphs:=HeaderSizeInBytes div 16;
  1946. TotalExeSize:=HeaderSizeInBytes+LoadableImageSize;
  1947. BlocksInFile:=(TotalExeSize+511) div 512;
  1948. BytesInLastBlock:=TotalExeSize mod 512;
  1949. HeaderBytes[$00]:=$4D; { 'M' }
  1950. HeaderBytes[$01]:=$5A; { 'Z' }
  1951. HeaderBytes[$02]:=Byte(BytesInLastBlock);
  1952. HeaderBytes[$03]:=Byte(BytesInLastBlock shr 8);
  1953. HeaderBytes[$04]:=Byte(BlocksInFile);
  1954. HeaderBytes[$05]:=Byte(BlocksInFile shr 8);
  1955. HeaderBytes[$06]:=Byte(NumRelocs);
  1956. HeaderBytes[$07]:=Byte(NumRelocs shr 8);
  1957. HeaderBytes[$08]:=Byte(HeaderParagraphs);
  1958. HeaderBytes[$09]:=Byte(HeaderParagraphs shr 8);
  1959. HeaderBytes[$0A]:=Byte(MinExtraParagraphs);
  1960. HeaderBytes[$0B]:=Byte(MinExtraParagraphs shr 8);
  1961. HeaderBytes[$0C]:=Byte(MaxExtraParagraphs);
  1962. HeaderBytes[$0D]:=Byte(MaxExtraParagraphs shr 8);
  1963. HeaderBytes[$0E]:=Byte(InitialSS);
  1964. HeaderBytes[$0F]:=Byte(InitialSS shr 8);
  1965. HeaderBytes[$10]:=Byte(InitialSP);
  1966. HeaderBytes[$11]:=Byte(InitialSP shr 8);
  1967. HeaderBytes[$12]:=Byte(Checksum);
  1968. HeaderBytes[$13]:=Byte(Checksum shr 8);
  1969. HeaderBytes[$14]:=Byte(InitialIP);
  1970. HeaderBytes[$15]:=Byte(InitialIP shr 8);
  1971. HeaderBytes[$16]:=Byte(InitialCS);
  1972. HeaderBytes[$17]:=Byte(InitialCS shr 8);
  1973. HeaderBytes[$18]:=Byte(RelocTableOffset);
  1974. HeaderBytes[$19]:=Byte(RelocTableOffset shr 8);
  1975. HeaderBytes[$1A]:=Byte(OverlayNumber);
  1976. HeaderBytes[$1B]:=Byte(OverlayNumber shr 8);
  1977. aWriter.write(HeaderBytes[0],$1C);
  1978. aWriter.write(ExtraHeaderData[0],Length(ExtraHeaderData));
  1979. for i:=0 to NumRelocs-1 do
  1980. with Relocations[i] do
  1981. begin
  1982. RelocBytes[0]:=Byte(offset);
  1983. RelocBytes[1]:=Byte(offset shr 8);
  1984. RelocBytes[2]:=Byte(segment);
  1985. RelocBytes[3]:=Byte(segment shr 8);
  1986. aWriter.write(RelocBytes[0],4);
  1987. end;
  1988. { pad with zeros until the end of header (paragraph aligned) }
  1989. aWriter.WriteZeros(HeaderSizeInBytes-aWriter.Size);
  1990. end;
  1991. procedure TMZExeHeader.AddRelocation(aSegment, aOffset: Word);
  1992. begin
  1993. SetLength(FRelocations,Length(FRelocations)+1);
  1994. with FRelocations[High(FRelocations)] do
  1995. begin
  1996. segment:=aSegment;
  1997. offset:=aOffset;
  1998. end;
  1999. end;
  2000. {****************************************************************************
  2001. TMZExeSection
  2002. ****************************************************************************}
  2003. procedure TMZExeSection.AddObjSection(objsec: TObjSection; ignoreprops: boolean);
  2004. begin
  2005. { allow mixing initialized and uninitialized data in the same section
  2006. => set ignoreprops=true }
  2007. inherited AddObjSection(objsec,true);
  2008. end;
  2009. {****************************************************************************
  2010. TMZExeUnifiedLogicalSegment
  2011. ****************************************************************************}
  2012. constructor TMZExeUnifiedLogicalSegment.create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  2013. var
  2014. Separator: SizeInt;
  2015. begin
  2016. inherited create(HashObjectList,s);
  2017. FObjSectionList:=TFPObjectList.Create(false);
  2018. { name format is 'SegName||ClassName' }
  2019. Separator:=Pos('||',s);
  2020. if Separator>0 then
  2021. begin
  2022. FSegName:=Copy(s,1,Separator-1);
  2023. FSegClass:=Copy(s,Separator+2,Length(s)-Separator-1);
  2024. end
  2025. else
  2026. begin
  2027. FSegName:=Name;
  2028. FSegClass:='';
  2029. end;
  2030. { wlink recognizes the stack segment by the class name 'STACK' }
  2031. { let's be compatible with wlink }
  2032. IsStack:=FSegClass='STACK';
  2033. end;
  2034. destructor TMZExeUnifiedLogicalSegment.destroy;
  2035. begin
  2036. FObjSectionList.Free;
  2037. inherited destroy;
  2038. end;
  2039. procedure TMZExeUnifiedLogicalSegment.AddObjSection(ObjSec: TOmfObjSection);
  2040. begin
  2041. ObjSectionList.Add(ObjSec);
  2042. ObjSec.MZExeUnifiedLogicalSegment:=self;
  2043. { tlink (and ms link?) use the scStack segment combination to recognize
  2044. the stack segment.
  2045. let's be compatible with tlink as well }
  2046. if ObjSec.Combination=scStack then
  2047. IsStack:=True;
  2048. end;
  2049. procedure TMZExeUnifiedLogicalSegment.CalcMemPos;
  2050. var
  2051. MinMemPos: qword=high(qword);
  2052. MaxMemPos: qword=0;
  2053. objsec: TOmfObjSection;
  2054. i: Integer;
  2055. begin
  2056. if ObjSectionList.Count=0 then
  2057. internalerror(2015082201);
  2058. for i:=0 to ObjSectionList.Count-1 do
  2059. begin
  2060. objsec:=TOmfObjSection(ObjSectionList[i]);
  2061. if objsec.MemPos<MinMemPos then
  2062. MinMemPos:=objsec.MemPos;
  2063. if (objsec.MemPos+objsec.Size)>MaxMemPos then
  2064. MaxMemPos:=objsec.MemPos+objsec.Size;
  2065. end;
  2066. MemPos:=MinMemPos;
  2067. Size:=MaxMemPos-MemPos;
  2068. end;
  2069. function TMZExeUnifiedLogicalSegment.MemPosStr: string;
  2070. begin
  2071. Result:=HexStr(MemBasePos shr 4,4)+':'+HexStr((MemPos-MemBasePos),4);
  2072. end;
  2073. {****************************************************************************
  2074. TMZExeUnifiedLogicalGroup
  2075. ****************************************************************************}
  2076. constructor TMZExeUnifiedLogicalGroup.create(HashObjectList: TFPHashObjectList; const s: TSymStr);
  2077. begin
  2078. inherited create(HashObjectList,s);
  2079. FSegmentList:=TFPHashObjectList.Create(false);
  2080. end;
  2081. destructor TMZExeUnifiedLogicalGroup.destroy;
  2082. begin
  2083. FSegmentList.Free;
  2084. inherited destroy;
  2085. end;
  2086. procedure TMZExeUnifiedLogicalGroup.CalcMemPos;
  2087. var
  2088. MinMemPos: qword=high(qword);
  2089. MaxMemPos: qword=0;
  2090. UniSeg: TMZExeUnifiedLogicalSegment;
  2091. i: Integer;
  2092. begin
  2093. if SegmentList.Count=0 then
  2094. internalerror(2015082201);
  2095. for i:=0 to SegmentList.Count-1 do
  2096. begin
  2097. UniSeg:=TMZExeUnifiedLogicalSegment(SegmentList[i]);
  2098. if UniSeg.MemPos<MinMemPos then
  2099. MinMemPos:=UniSeg.MemPos;
  2100. if (UniSeg.MemPos+UniSeg.Size)>MaxMemPos then
  2101. MaxMemPos:=UniSeg.MemPos+UniSeg.Size;
  2102. end;
  2103. { align *down* on a paragraph boundary }
  2104. MemPos:=(MinMemPos shr 4) shl 4;
  2105. Size:=MaxMemPos-MemPos;
  2106. end;
  2107. function TMZExeUnifiedLogicalGroup.MemPosStr: string;
  2108. begin
  2109. Result:=HexStr(MemPos shr 4,4)+':'+HexStr(MemPos and $f,4);
  2110. end;
  2111. procedure TMZExeUnifiedLogicalGroup.AddSegment(UniSeg: TMZExeUnifiedLogicalSegment);
  2112. begin
  2113. SegmentList.Add(UniSeg.Name,UniSeg);
  2114. if UniSeg.PrimaryGroup='' then
  2115. UniSeg.PrimaryGroup:=Name;
  2116. end;
  2117. {****************************************************************************
  2118. TMZExeOutput
  2119. ****************************************************************************}
  2120. function TMZExeOutput.GetMZFlatContentSection: TMZExeSection;
  2121. begin
  2122. if not assigned(FMZFlatContentSection) then
  2123. FMZFlatContentSection:=TMZExeSection(FindExeSection('.MZ_flat_content'));
  2124. result:=FMZFlatContentSection;
  2125. end;
  2126. procedure TMZExeOutput.CalcDwarfUnifiedLogicalSegmentsForSection(const SecName: TSymStr);
  2127. var
  2128. ExeSec: TMZExeSection;
  2129. ObjSec: TOmfObjSection;
  2130. UniSeg: TMZExeUnifiedLogicalSegment;
  2131. i: Integer;
  2132. begin
  2133. ExeSec:=TMZExeSection(FindExeSection(SecName));
  2134. for i:=0 to ExeSec.ObjSectionList.Count-1 do
  2135. begin
  2136. ObjSec:=TOmfObjSection(ExeSec.ObjSectionList[i]);
  2137. UniSeg:=TMZExeUnifiedLogicalSegment(DwarfUnifiedLogicalSegments.Find(ObjSec.Name));
  2138. if not assigned(UniSeg) then
  2139. begin
  2140. UniSeg:=TMZExeUnifiedLogicalSegment.Create(DwarfUnifiedLogicalSegments,ObjSec.Name);
  2141. UniSeg.MemPos:=0;
  2142. end;
  2143. UniSeg.AddObjSection(ObjSec);
  2144. end;
  2145. for i:=0 to DwarfUnifiedLogicalSegments.Count-1 do
  2146. begin
  2147. UniSeg:=TMZExeUnifiedLogicalSegment(DwarfUnifiedLogicalSegments[i]);
  2148. UniSeg.CalcMemPos;
  2149. end;
  2150. end;
  2151. procedure TMZExeOutput.CalcExeUnifiedLogicalSegments;
  2152. var
  2153. ExeSec: TMZExeSection;
  2154. ObjSec: TOmfObjSection;
  2155. UniSeg: TMZExeUnifiedLogicalSegment;
  2156. i: Integer;
  2157. begin
  2158. ExeSec:=MZFlatContentSection;
  2159. for i:=0 to ExeSec.ObjSectionList.Count-1 do
  2160. begin
  2161. ObjSec:=TOmfObjSection(ExeSec.ObjSectionList[i]);
  2162. UniSeg:=TMZExeUnifiedLogicalSegment(ExeUnifiedLogicalSegments.Find(ObjSec.Name));
  2163. if not assigned(UniSeg) then
  2164. UniSeg:=TMZExeUnifiedLogicalSegment.Create(ExeUnifiedLogicalSegments,ObjSec.Name);
  2165. UniSeg.AddObjSection(ObjSec);
  2166. end;
  2167. for i:=0 to ExeUnifiedLogicalSegments.Count-1 do
  2168. begin
  2169. UniSeg:=TMZExeUnifiedLogicalSegment(ExeUnifiedLogicalSegments[i]);
  2170. UniSeg.CalcMemPos;
  2171. if UniSeg.Size>$10000 then
  2172. begin
  2173. if current_settings.x86memorymodel=mm_tiny then
  2174. Message1(link_e_program_segment_too_large,IntToStr(UniSeg.Size-$10000))
  2175. else if UniSeg.SegClass='CODE' then
  2176. Message2(link_e_code_segment_too_large,UniSeg.SegName,IntToStr(UniSeg.Size-$10000))
  2177. else if UniSeg.SegClass='DATA' then
  2178. Message2(link_e_data_segment_too_large,UniSeg.SegName,IntToStr(UniSeg.Size-$10000))
  2179. else
  2180. Message2(link_e_segment_too_large,UniSeg.SegName,IntToStr(UniSeg.Size-$10000)+' '+UniSeg.SegName);
  2181. end;
  2182. end;
  2183. end;
  2184. procedure TMZExeOutput.CalcExeGroups;
  2185. procedure AddToGroup(UniSeg:TMZExeUnifiedLogicalSegment;GroupName:TSymStr);
  2186. var
  2187. Group: TMZExeUnifiedLogicalGroup;
  2188. begin
  2189. Group:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(GroupName));
  2190. if not assigned(Group) then
  2191. Group:=TMZExeUnifiedLogicalGroup.Create(ExeUnifiedLogicalGroups,GroupName);
  2192. Group.AddSegment(UniSeg);
  2193. end;
  2194. var
  2195. objdataidx,groupidx,secidx: Integer;
  2196. ObjData: TObjData;
  2197. ObjGroup: TObjSectionGroup;
  2198. ObjSec: TOmfObjSection;
  2199. UniGrp: TMZExeUnifiedLogicalGroup;
  2200. begin
  2201. for objdataidx:=0 to ObjDataList.Count-1 do
  2202. begin
  2203. ObjData:=TObjData(ObjDataList[objdataidx]);
  2204. if assigned(ObjData.GroupsList) then
  2205. for groupidx:=0 to ObjData.GroupsList.Count-1 do
  2206. begin
  2207. ObjGroup:=TObjSectionGroup(ObjData.GroupsList[groupidx]);
  2208. for secidx:=low(ObjGroup.members) to high(ObjGroup.members) do
  2209. begin
  2210. ObjSec:=TOmfObjSection(ObjGroup.members[secidx]);
  2211. if assigned(ObjSec.MZExeUnifiedLogicalSegment) then
  2212. AddToGroup(ObjSec.MZExeUnifiedLogicalSegment,ObjGroup.Name);
  2213. end;
  2214. end;
  2215. end;
  2216. for groupidx:=0 to ExeUnifiedLogicalGroups.Count-1 do
  2217. begin
  2218. UniGrp:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups[groupidx]);
  2219. UniGrp.CalcMemPos;
  2220. if UniGrp.Size>$10000 then
  2221. begin
  2222. if current_settings.x86memorymodel=mm_tiny then
  2223. Message1(link_e_program_segment_too_large,IntToStr(UniGrp.Size-$10000))
  2224. else if UniGrp.Name='DGROUP' then
  2225. Message2(link_e_data_segment_too_large,UniGrp.Name,IntToStr(UniGrp.Size-$10000))
  2226. else
  2227. Message2(link_e_group_too_large,UniGrp.Name,IntToStr(UniGrp.Size-$10000));
  2228. end;
  2229. end;
  2230. end;
  2231. procedure TMZExeOutput.CalcSegments_MemBasePos;
  2232. var
  2233. lastbase:qword=0;
  2234. i: Integer;
  2235. UniSeg: TMZExeUnifiedLogicalSegment;
  2236. begin
  2237. for i:=0 to ExeUnifiedLogicalSegments.Count-1 do
  2238. begin
  2239. UniSeg:=TMZExeUnifiedLogicalSegment(ExeUnifiedLogicalSegments[i]);
  2240. if (UniSeg.PrimaryGroup<>'') or (UniSeg.IsStack) or
  2241. (((UniSeg.MemPos+UniSeg.Size-1)-lastbase)>$ffff) then
  2242. lastbase:=(UniSeg.MemPos shr 4) shl 4;
  2243. UniSeg.MemBasePos:=lastbase;
  2244. end;
  2245. end;
  2246. procedure TMZExeOutput.WriteMap_SegmentsAndGroups;
  2247. var
  2248. i: Integer;
  2249. UniSeg: TMZExeUnifiedLogicalSegment;
  2250. UniGrp: TMZExeUnifiedLogicalGroup;
  2251. begin
  2252. exemap.AddHeader('Groups list');
  2253. exemap.Add('');
  2254. exemap.Add(PadSpace('Group',32)+PadSpace('Address',21)+'Size');
  2255. exemap.Add(PadSpace('=====',32)+PadSpace('=======',21)+'====');
  2256. exemap.Add('');
  2257. for i:=0 to ExeUnifiedLogicalGroups.Count-1 do
  2258. begin
  2259. UniGrp:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups[i]);
  2260. exemap.Add(PadSpace(UniGrp.Name,32)+PadSpace(UniGrp.MemPosStr,21)+HexStr(UniGrp.Size,8));
  2261. end;
  2262. exemap.Add('');
  2263. exemap.AddHeader('Segments list');
  2264. exemap.Add('');
  2265. exemap.Add(PadSpace('Segment',23)+PadSpace('Class',15)+PadSpace('Group',15)+PadSpace('Address',16)+'Size');
  2266. exemap.Add(PadSpace('=======',23)+PadSpace('=====',15)+PadSpace('=====',15)+PadSpace('=======',16)+'====');
  2267. exemap.Add('');
  2268. for i:=0 to ExeUnifiedLogicalSegments.Count-1 do
  2269. begin
  2270. UniSeg:=TMZExeUnifiedLogicalSegment(ExeUnifiedLogicalSegments[i]);
  2271. exemap.Add(PadSpace(UniSeg.SegName,23)+PadSpace(UniSeg.SegClass,15)+PadSpace(UniSeg.PrimaryGroup,15)+PadSpace(UniSeg.MemPosStr,16)+HexStr(UniSeg.Size,8));
  2272. end;
  2273. exemap.Add('');
  2274. end;
  2275. procedure TMZExeOutput.WriteMap_HeaderData;
  2276. begin
  2277. exemap.AddHeader('Header data');
  2278. exemap.Add('Loadable image size: '+HexStr(Header.LoadableImageSize,8));
  2279. exemap.Add('Min extra paragraphs: '+HexStr(Header.MinExtraParagraphs,4));
  2280. exemap.Add('Max extra paragraphs: '+HexStr(Header.MaxExtraParagraphs,4));
  2281. exemap.Add('Initial stack pointer: '+HexStr(Header.InitialSS,4)+':'+HexStr(Header.InitialSP,4));
  2282. exemap.Add('Entry point address: '+HexStr(Header.InitialCS,4)+':'+HexStr(Header.InitialIP,4));
  2283. end;
  2284. function TMZExeOutput.FindStackSegment: TMZExeUnifiedLogicalSegment;
  2285. var
  2286. i: Integer;
  2287. stackseg_wannabe: TMZExeUnifiedLogicalSegment;
  2288. begin
  2289. Result:=nil;
  2290. for i:=0 to ExeUnifiedLogicalSegments.Count-1 do
  2291. begin
  2292. stackseg_wannabe:=TMZExeUnifiedLogicalSegment(ExeUnifiedLogicalSegments[i]);
  2293. { if there are multiple stack segments, choose the largest one.
  2294. In theory, we're probably supposed to combine them all and put
  2295. them in a contiguous location in memory, but we don't care }
  2296. if stackseg_wannabe.IsStack and
  2297. (not assigned(result) or (Result.Size<stackseg_wannabe.Size)) then
  2298. Result:=stackseg_wannabe;
  2299. end;
  2300. end;
  2301. procedure TMZExeOutput.FillLoadableImageSize;
  2302. var
  2303. i: Integer;
  2304. ExeSec: TMZExeSection;
  2305. ObjSec: TOmfObjSection;
  2306. StartDataPos: LongWord;
  2307. buf: array [0..1023] of byte;
  2308. bytesread: LongWord;
  2309. begin
  2310. Header.LoadableImageSize:=0;
  2311. ExeSec:=MZFlatContentSection;
  2312. for i:=0 to ExeSec.ObjSectionList.Count-1 do
  2313. begin
  2314. ObjSec:=TOmfObjSection(ExeSec.ObjSectionList[i]);
  2315. if (ObjSec.Size>0) and assigned(ObjSec.Data) then
  2316. if (ObjSec.MemPos+ObjSec.Size)>Header.LoadableImageSize then
  2317. Header.LoadableImageSize:=ObjSec.MemPos+ObjSec.Size;
  2318. end;
  2319. end;
  2320. procedure TMZExeOutput.FillMinExtraParagraphs;
  2321. var
  2322. ExeSec: TMZExeSection;
  2323. begin
  2324. ExeSec:=MZFlatContentSection;
  2325. Header.MinExtraParagraphs:=(align(ExeSec.Size,16)-align(Header.LoadableImageSize,16)) div 16;
  2326. end;
  2327. procedure TMZExeOutput.FillMaxExtraParagraphs;
  2328. var
  2329. heapmin_paragraphs: Integer;
  2330. heapmax_paragraphs: Integer;
  2331. begin
  2332. if current_settings.x86memorymodel in x86_far_data_models then
  2333. begin
  2334. { calculate the additional number of paragraphs needed }
  2335. heapmin_paragraphs:=(heapsize + 15) div 16;
  2336. heapmax_paragraphs:=(maxheapsize + 15) div 16;
  2337. Header.MaxExtraParagraphs:=min(Header.MinExtraParagraphs-heapmin_paragraphs+heapmax_paragraphs,$FFFF);
  2338. end
  2339. else
  2340. Header.MaxExtraParagraphs:=$FFFF;
  2341. end;
  2342. procedure TMZExeOutput.FillStartAddress;
  2343. var
  2344. EntryMemPos: qword;
  2345. EntryMemBasePos: qword;
  2346. begin
  2347. EntryMemPos:=EntrySym.address;
  2348. if assigned(EntrySym.group) then
  2349. EntryMemBasePos:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(EntrySym.group.Name)).MemPos
  2350. else
  2351. EntryMemBasePos:=TOmfObjSection(EntrySym.objsection).MZExeUnifiedLogicalSegment.MemBasePos;
  2352. Header.InitialIP:=EntryMemPos-EntryMemBasePos;
  2353. Header.InitialCS:=EntryMemBasePos shr 4;
  2354. end;
  2355. procedure TMZExeOutput.FillStackAddress;
  2356. var
  2357. stackseg: TMZExeUnifiedLogicalSegment;
  2358. begin
  2359. stackseg:=FindStackSegment;
  2360. if assigned(stackseg) then
  2361. begin
  2362. Header.InitialSS:=stackseg.MemBasePos shr 4;
  2363. Header.InitialSP:=stackseg.MemPos+stackseg.Size-stackseg.MemBasePos;
  2364. end
  2365. else
  2366. begin
  2367. Header.InitialSS:=0;
  2368. Header.InitialSP:=0;
  2369. end;
  2370. end;
  2371. procedure TMZExeOutput.FillHeaderData;
  2372. begin
  2373. Header.MaxExtraParagraphs:=$FFFF;
  2374. FillLoadableImageSize;
  2375. FillMinExtraParagraphs;
  2376. FillMaxExtraParagraphs;
  2377. FillStartAddress;
  2378. FillStackAddress;
  2379. if assigned(exemap) then
  2380. WriteMap_HeaderData;
  2381. end;
  2382. function TMZExeOutput.writeExe: boolean;
  2383. var
  2384. ExeSec: TMZExeSection;
  2385. i: Integer;
  2386. ObjSec: TOmfObjSection;
  2387. begin
  2388. Result:=False;
  2389. FillHeaderData;
  2390. Header.WriteTo(FWriter);
  2391. ExeSec:=MZFlatContentSection;
  2392. ExeSec.DataPos:=FWriter.Size;
  2393. for i:=0 to ExeSec.ObjSectionList.Count-1 do
  2394. begin
  2395. ObjSec:=TOmfObjSection(ExeSec.ObjSectionList[i]);
  2396. if ObjSec.MemPos<Header.LoadableImageSize then
  2397. begin
  2398. FWriter.WriteZeros(max(0,ObjSec.MemPos-FWriter.Size+ExeSec.DataPos));
  2399. if assigned(ObjSec.Data) then
  2400. FWriter.writearray(ObjSec.Data);
  2401. end;
  2402. end;
  2403. Result:=True;
  2404. end;
  2405. function TMZExeOutput.writeCom: boolean;
  2406. const
  2407. ComFileOffset=$100;
  2408. var
  2409. i: Integer;
  2410. ExeSec: TMZExeSection;
  2411. ObjSec: TOmfObjSection;
  2412. StartDataPos: LongWord;
  2413. buf: array [0..1023] of byte;
  2414. bytesread: LongWord;
  2415. begin
  2416. FillHeaderData;
  2417. if Length(Header.Relocations)>0 then
  2418. begin
  2419. Message(link_e_com_program_uses_segment_relocations);
  2420. exit(False);
  2421. end;
  2422. ExeSec:=MZFlatContentSection;
  2423. for i:=0 to ExeSec.ObjSectionList.Count-1 do
  2424. begin
  2425. ObjSec:=TOmfObjSection(ExeSec.ObjSectionList[i]);
  2426. if ObjSec.MemPos<Header.LoadableImageSize then
  2427. begin
  2428. FWriter.WriteZeros(max(0,ObjSec.MemPos-ComFileOffset-FWriter.Size));
  2429. if assigned(ObjSec.Data) then
  2430. begin
  2431. if ObjSec.MemPos<ComFileOffset then
  2432. begin
  2433. ObjSec.Data.seek(ComFileOffset-ObjSec.MemPos);
  2434. repeat
  2435. bytesread:=ObjSec.Data.read(buf,sizeof(buf));
  2436. if bytesread<>0 then
  2437. FWriter.write(buf,bytesread);
  2438. until bytesread=0;
  2439. end
  2440. else
  2441. FWriter.writearray(ObjSec.Data);
  2442. end;
  2443. end;
  2444. end;
  2445. Result:=True;
  2446. end;
  2447. function TMZExeOutput.writeDebugElf: boolean;
  2448. label
  2449. cleanup;
  2450. var
  2451. debugsections: array of TMZExeSection;
  2452. debugsections_count: Word;
  2453. elfsections_count: Word;
  2454. elfsechdrs: array of TElf32sechdr;
  2455. shstrndx: Word;
  2456. next_section_ofs: LongWord;
  2457. ElfHeader: TElf32header;
  2458. shstrtabsect_data: TDynamicArray=Nil;
  2459. I, elfsecidx, J: Integer;
  2460. ObjSec: TOmfObjSection;
  2461. begin
  2462. { count the debug sections }
  2463. debugsections_count:=0;
  2464. for I:=0 to ExeSectionList.Count-1 do
  2465. if oso_debug in TMZExeSection(ExeSectionList[I]).SecOptions then
  2466. Inc(debugsections_count);
  2467. { extract them into the debugsections array }
  2468. SetLength(debugsections,debugsections_count);
  2469. debugsections_count:=0;
  2470. for I:=0 to ExeSectionList.Count-1 do
  2471. if oso_debug in TMZExeSection(ExeSectionList[I]).SecOptions then
  2472. begin
  2473. debugsections[debugsections_count]:=TMZExeSection(ExeSectionList[I]);
  2474. Inc(debugsections_count);
  2475. end;
  2476. { prepare/allocate elf section headers }
  2477. elfsections_count:=debugsections_count+2;
  2478. SetLength(elfsechdrs,elfsections_count);
  2479. for I:=0 to elfsections_count-1 do
  2480. FillChar(elfsechdrs[I],SizeOf(elfsechdrs[I]),0);
  2481. shstrndx:=elfsections_count-1;
  2482. shstrtabsect_data:=tdynamicarray.Create(SectionDataMaxGrow);
  2483. shstrtabsect_data.writestr(#0);
  2484. next_section_ofs:=SizeOf(ElfHeader)+elfsections_count*SizeOf(TElf32sechdr);
  2485. for I:=0 to debugsections_count-1 do
  2486. begin
  2487. elfsecidx:=I+1;
  2488. with elfsechdrs[elfsecidx] do
  2489. begin
  2490. sh_name:=shstrtabsect_data.Pos;
  2491. sh_type:=SHT_PROGBITS;
  2492. sh_flags:=0;
  2493. sh_addr:=0;
  2494. sh_offset:=next_section_ofs;
  2495. sh_size:=debugsections[I].Size;
  2496. sh_link:=0;
  2497. sh_info:=0;
  2498. sh_addralign:=0;
  2499. sh_entsize:=0;
  2500. end;
  2501. Inc(next_section_ofs,debugsections[I].Size);
  2502. shstrtabsect_data.writestr(debugsections[I].Name+#0);
  2503. end;
  2504. with elfsechdrs[shstrndx] do
  2505. begin
  2506. sh_name:=shstrtabsect_data.Pos;
  2507. shstrtabsect_data.writestr('.shstrtab'#0);
  2508. sh_type:=SHT_STRTAB;
  2509. sh_flags:=0;
  2510. sh_addr:=0;
  2511. sh_offset:=next_section_ofs;
  2512. sh_size:=shstrtabsect_data.Size;
  2513. sh_link:=0;
  2514. sh_info:=0;
  2515. sh_addralign:=0;
  2516. sh_entsize:=0;
  2517. end;
  2518. { write header }
  2519. FillChar(ElfHeader,SizeOf(ElfHeader),0);
  2520. ElfHeader.e_ident[EI_MAG0]:=ELFMAG0; { = #127'ELF' }
  2521. ElfHeader.e_ident[EI_MAG1]:=ELFMAG1;
  2522. ElfHeader.e_ident[EI_MAG2]:=ELFMAG2;
  2523. ElfHeader.e_ident[EI_MAG3]:=ELFMAG3;
  2524. ElfHeader.e_ident[EI_CLASS]:=ELFCLASS32;
  2525. ElfHeader.e_ident[EI_DATA]:=ELFDATA2LSB;
  2526. ElfHeader.e_ident[EI_VERSION]:=1;
  2527. ElfHeader.e_ident[EI_OSABI]:=ELFOSABI_NONE;
  2528. ElfHeader.e_ident[EI_ABIVERSION]:=0;
  2529. ElfHeader.e_type:=ET_EXEC;
  2530. ElfHeader.e_machine:=EM_386;
  2531. ElfHeader.e_version:=1;
  2532. ElfHeader.e_entry:=0;
  2533. ElfHeader.e_phoff:=0;
  2534. ElfHeader.e_shoff:=SizeOf(ElfHeader);
  2535. ElfHeader.e_flags:=0;
  2536. ElfHeader.e_ehsize:=SizeOf(ElfHeader);
  2537. ElfHeader.e_phentsize:=SizeOf(TElf32proghdr);
  2538. ElfHeader.e_phnum:=0;
  2539. ElfHeader.e_shentsize:=SizeOf(TElf32sechdr);
  2540. ElfHeader.e_shnum:=elfsections_count;
  2541. ElfHeader.e_shstrndx:=shstrndx;
  2542. MaybeSwapHeader(ElfHeader);
  2543. Writer.write(ElfHeader,sizeof(ElfHeader));
  2544. { write section headers }
  2545. for I:=0 to elfsections_count-1 do
  2546. begin
  2547. MaybeSwapSecHeader(elfsechdrs[I]);
  2548. Writer.write(elfsechdrs[I],SizeOf(elfsechdrs[I]));
  2549. end;
  2550. { write section data }
  2551. for J:=0 to debugsections_count-1 do
  2552. begin
  2553. debugsections[J].DataPos:=Writer.Size;
  2554. for i:=0 to debugsections[J].ObjSectionList.Count-1 do
  2555. begin
  2556. ObjSec:=TOmfObjSection(debugsections[J].ObjSectionList[i]);
  2557. if ObjSec.MemPos<Header.LoadableImageSize then
  2558. begin
  2559. FWriter.WriteZeros(max(0,ObjSec.MemPos-FWriter.Size+debugsections[J].DataPos));
  2560. if assigned(ObjSec.Data) then
  2561. FWriter.writearray(ObjSec.Data);
  2562. end;
  2563. end;
  2564. end;
  2565. { write .shstrtab section data }
  2566. Writer.writearray(shstrtabsect_data);
  2567. Result:=True;
  2568. cleanup:
  2569. shstrtabsect_data.Free;
  2570. end;
  2571. procedure TMZExeOutput.Load_Symbol(const aname: string);
  2572. var
  2573. dgroup: TObjSectionGroup;
  2574. sym: TObjSymbol;
  2575. begin
  2576. { special handling for the '_edata' and '_end' symbols, which are
  2577. internally added by the linker }
  2578. if (aname='_edata') or (aname='_end') then
  2579. begin
  2580. { create an internal segment with the 'BSS' class }
  2581. internalObjData.createsection('*'+aname+'||BSS',0,[]);
  2582. { add to group 'DGROUP' }
  2583. dgroup:=nil;
  2584. if assigned(internalObjData.GroupsList) then
  2585. dgroup:=TObjSectionGroup(internalObjData.GroupsList.Find('DGROUP'));
  2586. if dgroup=nil then
  2587. dgroup:=internalObjData.createsectiongroup('DGROUP');
  2588. SetLength(dgroup.members,Length(dgroup.members)+1);
  2589. dgroup.members[Length(dgroup.members)-1]:=internalObjData.CurrObjSec;
  2590. { define the symbol itself }
  2591. sym:=internalObjData.SymbolDefine(aname,AB_GLOBAL,AT_DATA);
  2592. sym.group:=dgroup;
  2593. end
  2594. else
  2595. inherited;
  2596. end;
  2597. procedure TMZExeOutput.DoRelocationFixup(objsec: TObjSection);
  2598. var
  2599. i: Integer;
  2600. omfsec: TOmfObjSection absolute objsec;
  2601. objreloc: TOmfRelocation;
  2602. target: DWord;
  2603. framebase: DWord;
  2604. fixupamount: Integer;
  2605. target_group: TMZExeUnifiedLogicalGroup;
  2606. procedure FixupOffset;
  2607. var
  2608. w: Word;
  2609. begin
  2610. omfsec.Data.seek(objreloc.DataOffset);
  2611. omfsec.Data.read(w,2);
  2612. w:=LEtoN(w);
  2613. Inc(w,fixupamount);
  2614. w:=LEtoN(w);
  2615. omfsec.Data.seek(objreloc.DataOffset);
  2616. omfsec.Data.write(w,2);
  2617. end;
  2618. procedure FixupOffset32;
  2619. var
  2620. lw: LongWord;
  2621. begin
  2622. omfsec.Data.seek(objreloc.DataOffset);
  2623. omfsec.Data.read(lw,4);
  2624. lw:=LEtoN(lw);
  2625. Inc(lw,fixupamount);
  2626. lw:=LEtoN(lw);
  2627. omfsec.Data.seek(objreloc.DataOffset);
  2628. omfsec.Data.write(lw,4);
  2629. end;
  2630. procedure FixupBase(DataOffset: LongWord);
  2631. var
  2632. w: Word;
  2633. begin
  2634. omfsec.Data.seek(DataOffset);
  2635. omfsec.Data.read(w,2);
  2636. w:=LEtoN(w);
  2637. Inc(w,framebase shr 4);
  2638. w:=LEtoN(w);
  2639. omfsec.Data.seek(DataOffset);
  2640. omfsec.Data.write(w,2);
  2641. Header.AddRelocation(omfsec.MZExeUnifiedLogicalSegment.MemBasePos shr 4,
  2642. omfsec.MemPos+DataOffset-omfsec.MZExeUnifiedLogicalSegment.MemBasePos);
  2643. end;
  2644. begin
  2645. for i:=0 to objsec.ObjRelocations.Count-1 do
  2646. begin
  2647. objreloc:=TOmfRelocation(objsec.ObjRelocations[i]);
  2648. if assigned(objreloc.symbol) then
  2649. begin
  2650. target:=objreloc.symbol.address;
  2651. if objreloc.FrameGroup<>'' then
  2652. framebase:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(objreloc.FrameGroup)).MemPos
  2653. else if assigned(objreloc.symbol.group) then
  2654. framebase:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(objreloc.symbol.group.Name)).MemPos
  2655. else
  2656. framebase:=TOmfObjSection(objreloc.symbol.objsection).MZExeUnifiedLogicalSegment.MemBasePos;
  2657. case objreloc.typ of
  2658. RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_SEG,RELOC_FARPTR,RELOC_FARPTR48:
  2659. fixupamount:=target-framebase;
  2660. RELOC_RELATIVE16,RELOC_SEGREL,RELOC_FARPTR_RELATIVEOFFSET:
  2661. fixupamount:=target-(omfsec.MemPos+objreloc.DataOffset)-2;
  2662. RELOC_RELATIVE32,RELOC_FARPTR48_RELATIVEOFFSET:
  2663. fixupamount:=target-(omfsec.MemPos+objreloc.DataOffset)-4;
  2664. else
  2665. internalerror(2015082402);
  2666. end;
  2667. case objreloc.typ of
  2668. RELOC_ABSOLUTE16,
  2669. RELOC_RELATIVE16:
  2670. FixupOffset;
  2671. RELOC_ABSOLUTE32,
  2672. RELOC_RELATIVE32:
  2673. FixupOffset32;
  2674. RELOC_SEG,
  2675. RELOC_SEGREL:
  2676. FixupBase(objreloc.DataOffset);
  2677. RELOC_FARPTR,
  2678. RELOC_FARPTR_RELATIVEOFFSET:
  2679. begin
  2680. FixupOffset;
  2681. FixupBase(objreloc.DataOffset+2);
  2682. end;
  2683. RELOC_FARPTR48,
  2684. RELOC_FARPTR48_RELATIVEOFFSET:
  2685. begin
  2686. FixupOffset32;
  2687. FixupBase(objreloc.DataOffset+4);
  2688. end;
  2689. else
  2690. internalerror(2015082403);
  2691. end;
  2692. end
  2693. else if assigned(objreloc.objsection) then
  2694. begin
  2695. target:=objreloc.objsection.MemPos;
  2696. if objreloc.FrameGroup<>'' then
  2697. framebase:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(objreloc.FrameGroup)).MemPos
  2698. else
  2699. framebase:=TOmfObjSection(objreloc.objsection).MZExeUnifiedLogicalSegment.MemBasePos;
  2700. case objreloc.typ of
  2701. RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_SEG,RELOC_FARPTR,RELOC_FARPTR48:
  2702. fixupamount:=target-framebase;
  2703. RELOC_RELATIVE16,RELOC_SEGREL,RELOC_FARPTR_RELATIVEOFFSET:
  2704. fixupamount:=target-(omfsec.MemPos+objreloc.DataOffset)-2;
  2705. RELOC_RELATIVE32,RELOC_FARPTR48_RELATIVEOFFSET:
  2706. fixupamount:=target-(omfsec.MemPos+objreloc.DataOffset)-4;
  2707. else
  2708. internalerror(2015082405);
  2709. end;
  2710. case objreloc.typ of
  2711. RELOC_ABSOLUTE16,
  2712. RELOC_RELATIVE16:
  2713. FixupOffset;
  2714. RELOC_ABSOLUTE32,
  2715. RELOC_RELATIVE32:
  2716. FixupOffset32;
  2717. RELOC_SEG,
  2718. RELOC_SEGREL:
  2719. FixupBase(objreloc.DataOffset);
  2720. RELOC_FARPTR,
  2721. RELOC_FARPTR_RELATIVEOFFSET:
  2722. begin
  2723. FixupOffset;
  2724. FixupBase(objreloc.DataOffset+2);
  2725. end;
  2726. RELOC_FARPTR48,
  2727. RELOC_FARPTR48_RELATIVEOFFSET:
  2728. begin
  2729. FixupOffset32;
  2730. FixupBase(objreloc.DataOffset+4);
  2731. end;
  2732. else
  2733. internalerror(2015082406);
  2734. end;
  2735. end
  2736. else if assigned(objreloc.group) then
  2737. begin
  2738. target_group:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(objreloc.group.Name));
  2739. target:=target_group.MemPos;
  2740. if objreloc.FrameGroup<>'' then
  2741. framebase:=TMZExeUnifiedLogicalGroup(ExeUnifiedLogicalGroups.Find(objreloc.FrameGroup)).MemPos
  2742. else
  2743. framebase:=target_group.MemPos;
  2744. case objreloc.typ of
  2745. RELOC_ABSOLUTE16,RELOC_ABSOLUTE32,RELOC_SEG,RELOC_FARPTR,RELOC_FARPTR48:
  2746. fixupamount:=target-framebase;
  2747. RELOC_RELATIVE16,RELOC_SEGREL,RELOC_FARPTR_RELATIVEOFFSET:
  2748. fixupamount:=target-(omfsec.MemPos+objreloc.DataOffset)-2;
  2749. RELOC_RELATIVE32,RELOC_FARPTR48_RELATIVEOFFSET:
  2750. fixupamount:=target-(omfsec.MemPos+objreloc.DataOffset)-4;
  2751. else
  2752. internalerror(2015111202);
  2753. end;
  2754. case objreloc.typ of
  2755. RELOC_ABSOLUTE16,
  2756. RELOC_RELATIVE16:
  2757. FixupOffset;
  2758. RELOC_ABSOLUTE32,
  2759. RELOC_RELATIVE32:
  2760. FixupOffset32;
  2761. RELOC_SEG,
  2762. RELOC_SEGREL:
  2763. FixupBase(objreloc.DataOffset);
  2764. RELOC_FARPTR,
  2765. RELOC_FARPTR_RELATIVEOFFSET:
  2766. begin
  2767. FixupOffset;
  2768. FixupBase(objreloc.DataOffset+2);
  2769. end;
  2770. RELOC_FARPTR48,
  2771. RELOC_FARPTR48_RELATIVEOFFSET:
  2772. begin
  2773. FixupOffset32;
  2774. FixupBase(objreloc.DataOffset+4);
  2775. end;
  2776. else
  2777. internalerror(2015111203);
  2778. end;
  2779. end
  2780. else
  2781. internalerror(2015082407);
  2782. end;
  2783. end;
  2784. function IOmfObjSectionClassNameCompare(Item1, Item2: Pointer): Integer;
  2785. var
  2786. I1 : TOmfObjSection absolute Item1;
  2787. I2 : TOmfObjSection absolute Item2;
  2788. begin
  2789. Result:=CompareStr(I1.ClassName,I2.ClassName);
  2790. if Result=0 then
  2791. Result:=CompareStr(I1.Name,I2.Name);
  2792. if Result=0 then
  2793. Result:=I1.SortOrder-I2.SortOrder;
  2794. end;
  2795. procedure TMZExeOutput.Order_ObjSectionList(ObjSectionList: TFPObjectList; const aPattern: string);
  2796. var
  2797. i: Integer;
  2798. begin
  2799. for i:=0 to ObjSectionList.Count-1 do
  2800. TOmfObjSection(ObjSectionList[i]).SortOrder:=i;
  2801. ObjSectionList.Sort(@IOmfObjSectionClassNameCompare);
  2802. end;
  2803. procedure TMZExeOutput.MemPos_EndExeSection;
  2804. var
  2805. SecName: TSymStr='';
  2806. begin
  2807. if assigned(CurrExeSec) then
  2808. SecName:=CurrExeSec.Name;
  2809. inherited MemPos_EndExeSection;
  2810. case SecName of
  2811. '.MZ_flat_content':
  2812. begin
  2813. CalcExeUnifiedLogicalSegments;
  2814. CalcExeGroups;
  2815. CalcSegments_MemBasePos;
  2816. if assigned(exemap) then
  2817. WriteMap_SegmentsAndGroups;
  2818. end;
  2819. '.debug_info',
  2820. '.debug_abbrev',
  2821. '.debug_line',
  2822. '.debug_aranges':
  2823. begin
  2824. CalcDwarfUnifiedLogicalSegmentsForSection(SecName);
  2825. with TMZExeSection(FindExeSection(SecName)) do
  2826. SecOptions:=SecOptions+[oso_debug];
  2827. end;
  2828. else
  2829. internalerror(2018061401);
  2830. end;
  2831. end;
  2832. function TMZExeOutput.writeData: boolean;
  2833. begin
  2834. Result:=False;
  2835. if ExeWriteMode in [ewm_exefull,ewm_exeonly] then
  2836. begin
  2837. if apptype=app_com then
  2838. Result:=WriteCom
  2839. else
  2840. Result:=WriteExe;
  2841. if not Result then
  2842. exit;
  2843. end;
  2844. if ((cs_debuginfo in current_settings.moduleswitches) and
  2845. (target_dbg.id in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4])) and
  2846. ((ExeWriteMode=ewm_dbgonly) or
  2847. ((ExeWriteMode=ewm_exefull) and
  2848. not(cs_link_strip in current_settings.globalswitches))) then
  2849. Result:=writeDebugElf;
  2850. end;
  2851. constructor TMZExeOutput.create;
  2852. begin
  2853. inherited create;
  2854. CExeSection:=TMZExeSection;
  2855. CObjData:=TOmfObjData;
  2856. CObjSymbol:=TOmfObjSymbol;
  2857. { "640K ought to be enough for anybody" :) }
  2858. MaxMemPos:=$9FFFF;
  2859. FExeUnifiedLogicalSegments:=TFPHashObjectList.Create;
  2860. FExeUnifiedLogicalGroups:=TFPHashObjectList.Create;
  2861. FDwarfUnifiedLogicalSegments:=TFPHashObjectList.Create;
  2862. FHeader:=TMZExeHeader.Create;
  2863. end;
  2864. destructor TMZExeOutput.destroy;
  2865. begin
  2866. FHeader.Free;
  2867. FDwarfUnifiedLogicalSegments.Free;
  2868. FExeUnifiedLogicalGroups.Free;
  2869. FExeUnifiedLogicalSegments.Free;
  2870. inherited destroy;
  2871. end;
  2872. {****************************************************************************
  2873. TOmfAssembler
  2874. ****************************************************************************}
  2875. constructor TOmfAssembler.Create(info: pasminfo; smart:boolean);
  2876. begin
  2877. inherited;
  2878. CObjOutput:=TOmfObjOutput;
  2879. CInternalAr:=TOmfLibObjectWriter;
  2880. end;
  2881. {*****************************************************************************
  2882. Initialize
  2883. *****************************************************************************}
  2884. {$ifdef i8086}
  2885. const
  2886. as_i8086_omf_info : tasminfo =
  2887. (
  2888. id : as_i8086_omf;
  2889. idtxt : 'OMF';
  2890. asmbin : '';
  2891. asmcmd : '';
  2892. supported_targets : [system_i8086_msdos,system_i8086_embedded];
  2893. flags : [af_outputbinary,af_smartlink_sections];
  2894. labelprefix : '..@';
  2895. comment : '; ';
  2896. dollarsign: '$';
  2897. );
  2898. {$endif i8086}
  2899. initialization
  2900. {$ifdef i8086}
  2901. RegisterAssembler(as_i8086_omf_info,TOmfAssembler);
  2902. {$endif i8086}
  2903. end.