ogomf.pas 106 KB

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