2
0

ogomf.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. { TOmfRelocation }
  35. TOmfRelocation = class(TObjRelocation)
  36. private
  37. FOmfFixup: TOmfSubRecord_FIXUP;
  38. function GetGroupIndex(const groupname: string): Integer;
  39. public
  40. destructor Destroy; override;
  41. procedure BuildOmfFixup;
  42. property OmfFixup: TOmfSubRecord_FIXUP read FOmfFixup;
  43. end;
  44. { TOmfObjSection }
  45. TOmfObjSection = class(TObjSection)
  46. private
  47. FClassName: string;
  48. FOverlayName: string;
  49. FOmfAlignment: TOmfSegmentAlignment;
  50. FCombination: TOmfSegmentCombination;
  51. FUse: TOmfSegmentUse;
  52. FPrimaryGroup: string;
  53. public
  54. constructor create(AList:TFPHashObjectList;const Aname:string;Aalign:shortint;Aoptions:TObjSectionOptions);override;
  55. property ClassName: string read FClassName;
  56. property OverlayName: string read FOverlayName;
  57. property OmfAlignment: TOmfSegmentAlignment read FOmfAlignment;
  58. property Combination: TOmfSegmentCombination read FCombination;
  59. property Use: TOmfSegmentUse read FUse;
  60. property PrimaryGroup: string read FPrimaryGroup;
  61. end;
  62. { TOmfObjData }
  63. TOmfObjData = class(TObjData)
  64. private
  65. class function CodeSectionName(const aname:string): string;
  66. public
  67. constructor create(const n:string);override;
  68. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  69. procedure writeReloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  70. end;
  71. { TOmfObjOutput }
  72. TOmfObjOutput = class(tObjOutput)
  73. private
  74. FLNames: TOmfOrderedNameCollection;
  75. FSegments: TFPHashObjectList;
  76. FGroups: TFPHashObjectList;
  77. procedure AddSegment(const name,segclass,ovlname: string;
  78. Alignment: TOmfSegmentAlignment; Combination: TOmfSegmentCombination;
  79. Use: TOmfSegmentUse; Size: aword);
  80. procedure AddGroup(const groupname: string; seglist: array of const);
  81. procedure AddGroup(const groupname: string; seglist: TSegmentList);
  82. procedure WriteSections(Data:TObjData);
  83. procedure WriteSectionContentAndFixups(sec: TObjSection);
  84. procedure section_count_sections(p:TObject;arg:pointer);
  85. procedure WritePUBDEFs(Data: TObjData);
  86. property LNames: TOmfOrderedNameCollection read FLNames;
  87. property Segments: TFPHashObjectList read FSegments;
  88. property Groups: TFPHashObjectList read FGroups;
  89. protected
  90. function writeData(Data:TObjData):boolean;override;
  91. public
  92. constructor create(AWriter:TObjectWriter);override;
  93. destructor Destroy;override;
  94. end;
  95. TOmfAssembler = class(tinternalassembler)
  96. constructor create(smart:boolean);override;
  97. end;
  98. implementation
  99. uses
  100. SysUtils,
  101. cutils,verbose,globals,
  102. fmodule,aasmtai,aasmdata,
  103. ogmap,
  104. version
  105. ;
  106. {****************************************************************************
  107. TOmfRelocation
  108. ****************************************************************************}
  109. function TOmfRelocation.GetGroupIndex(const groupname: string): Integer;
  110. begin
  111. if groupname='dgroup' then
  112. Result:=1
  113. else
  114. internalerror(2014040703);
  115. end;
  116. destructor TOmfRelocation.Destroy;
  117. begin
  118. FOmfFixup.Free;
  119. inherited Destroy;
  120. end;
  121. procedure TOmfRelocation.BuildOmfFixup;
  122. begin
  123. FreeAndNil(FOmfFixup);
  124. FOmfFixup:=TOmfSubRecord_FIXUP.Create;
  125. if ObjSection<>nil then
  126. begin
  127. FOmfFixup.LocationOffset:=DataOffset;
  128. FOmfFixup.LocationType:=fltOffset;
  129. FOmfFixup.FrameDeterminedByThread:=False;
  130. FOmfFixup.TargetDeterminedByThread:=False;
  131. FOmfFixup.Mode:=fmSegmentRelative;
  132. FOmfFixup.TargetMethod:=ftmSegmentIndexNoDisp;
  133. FOmfFixup.TargetDatum:=ObjSection.Index;
  134. if TOmfObjSection(ObjSection).PrimaryGroup<>'' then
  135. begin
  136. FOmfFixup.FrameMethod:=ffmGroupIndex;
  137. FOmfFixup.FrameDatum:=GetGroupIndex(TOmfObjSection(ObjSection).PrimaryGroup);
  138. end
  139. else
  140. FOmfFixup.FrameMethod:=ffmTarget;
  141. end
  142. else
  143. internalerror(2015040702);
  144. end;
  145. {****************************************************************************
  146. TOmfObjSection
  147. ****************************************************************************}
  148. constructor TOmfObjSection.create(AList: TFPHashObjectList;
  149. const Aname: string; Aalign: shortint; Aoptions: TObjSectionOptions);
  150. var
  151. dgroup: Boolean;
  152. begin
  153. inherited create(AList, Aname, Aalign, Aoptions);
  154. FCombination:=scPublic;
  155. FUse:=suUse16;
  156. FOmfAlignment:=saRelocatableByteAligned;
  157. if oso_executable in Aoptions then
  158. begin
  159. FClassName:='code';
  160. dgroup:=(current_settings.x86memorymodel=mm_tiny);
  161. end
  162. else if Aname='stack' then
  163. begin
  164. FClassName:='stack';
  165. FCombination:=scStack;
  166. FOmfAlignment:=saRelocatableParaAligned;
  167. dgroup:=current_settings.x86memorymodel in (x86_near_data_models-[mm_tiny]);
  168. end
  169. else if Aname='heap' then
  170. begin
  171. FClassName:='heap';
  172. FOmfAlignment:=saRelocatableParaAligned;
  173. dgroup:=current_settings.x86memorymodel in x86_near_data_models;
  174. end
  175. else if Aname='bss' then
  176. begin
  177. FClassName:='bss';
  178. dgroup:=true;
  179. end
  180. else if Aname='data' then
  181. begin
  182. FClassName:='data';
  183. FOmfAlignment:=saRelocatableWordAligned;
  184. dgroup:=true;
  185. end
  186. else
  187. begin
  188. FClassName:='data';
  189. dgroup:=true;
  190. end;
  191. if dgroup then
  192. FPrimaryGroup:='dgroup'
  193. else
  194. FPrimaryGroup:='';
  195. end;
  196. {****************************************************************************
  197. TOmfObjData
  198. ****************************************************************************}
  199. class function TOmfObjData.CodeSectionName(const aname: string): string;
  200. begin
  201. {$ifdef i8086}
  202. if current_settings.x86memorymodel in x86_far_code_models then
  203. begin
  204. if cs_huge_code in current_settings.moduleswitches then
  205. result:=aname + '_TEXT'
  206. else
  207. result:=current_module.modulename^ + '_TEXT';
  208. end
  209. else
  210. {$endif}
  211. result:='text';
  212. end;
  213. constructor TOmfObjData.create(const n: string);
  214. begin
  215. inherited create(n);
  216. CObjSection:=TOmfObjSection;
  217. end;
  218. function TOmfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  219. const
  220. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  221. 'text',
  222. 'data',
  223. 'data',
  224. 'rodata',
  225. 'bss',
  226. 'tbss',
  227. 'pdata',
  228. 'text','data','data','data','data',
  229. 'stab',
  230. 'stabstr',
  231. 'idata2','idata4','idata5','idata6','idata7','edata',
  232. 'eh_frame',
  233. 'debug_frame','debug_info','debug_line','debug_abbrev',
  234. 'fpc',
  235. '',
  236. 'init',
  237. 'fini',
  238. 'objc_class',
  239. 'objc_meta_class',
  240. 'objc_cat_cls_meth',
  241. 'objc_cat_inst_meth',
  242. 'objc_protocol',
  243. 'objc_string_object',
  244. 'objc_cls_meth',
  245. 'objc_inst_meth',
  246. 'objc_cls_refs',
  247. 'objc_message_refs',
  248. 'objc_symbols',
  249. 'objc_category',
  250. 'objc_class_vars',
  251. 'objc_instance_vars',
  252. 'objc_module_info',
  253. 'objc_class_names',
  254. 'objc_meth_var_types',
  255. 'objc_meth_var_names',
  256. 'objc_selector_strs',
  257. 'objc_protocol_ext',
  258. 'objc_class_ext',
  259. 'objc_property',
  260. 'objc_image_info',
  261. 'objc_cstring_object',
  262. 'objc_sel_fixup',
  263. '__DATA,__objc_data',
  264. '__DATA,__objc_const',
  265. 'objc_superrefs',
  266. '__DATA, __datacoal_nt,coalesced',
  267. 'objc_classlist',
  268. 'objc_nlclasslist',
  269. 'objc_catlist',
  270. 'obcj_nlcatlist',
  271. 'objc_protolist',
  272. 'stack',
  273. 'heap'
  274. );
  275. begin
  276. if (atype=sec_user) then
  277. Result:=aname
  278. else if secnames[atype]='text' then
  279. Result:=CodeSectionName(aname)
  280. else
  281. Result:=secnames[atype];
  282. end;
  283. procedure TOmfObjData.writeReloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  284. var
  285. objreloc: TOmfRelocation;
  286. symaddr: AWord;
  287. begin
  288. { Write('writeReloc(', data, ',', len, ',');
  289. if p<>nil then
  290. write(p.Name)
  291. else
  292. write('nil');
  293. Writeln(',',Reloctype,')');}
  294. if CurrObjSec=nil then
  295. internalerror(200403072);
  296. objreloc:=nil;
  297. if assigned(p) then
  298. begin
  299. { real address of the symbol }
  300. symaddr:=p.address;
  301. objreloc:=TOmfRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
  302. CurrObjSec.ObjRelocations.Add(objreloc);
  303. inc(data,symaddr);
  304. end;
  305. CurrObjSec.write(data,len);
  306. end;
  307. {****************************************************************************
  308. TOmfObjOutput
  309. ****************************************************************************}
  310. procedure TOmfObjOutput.AddSegment(const name, segclass, ovlname: string;
  311. Alignment: TOmfSegmentAlignment; Combination: TOmfSegmentCombination;
  312. Use: TOmfSegmentUse; Size: aword);
  313. var
  314. s: TOmfRecord_SEGDEF;
  315. begin
  316. s:=TOmfRecord_SEGDEF.Create;
  317. Segments.Add(name,s);
  318. s.SegmentNameIndex:=LNames.Add(name);
  319. s.ClassNameIndex:=LNames.Add(segclass);
  320. s.OverlayNameIndex:=LNames.Add(ovlname);
  321. s.Alignment:=Alignment;
  322. s.Combination:=Combination;
  323. s.Use:=Use;
  324. s.SegmentLength:=Size;
  325. end;
  326. procedure TOmfObjOutput.AddGroup(const groupname: string; seglist: array of const);
  327. var
  328. g: TOmfRecord_GRPDEF;
  329. I: Integer;
  330. SegListStr: TSegmentList;
  331. begin
  332. g:=TOmfRecord_GRPDEF.Create;
  333. Groups.Add(groupname,g);
  334. g.GroupNameIndex:=LNames.Add(groupname);
  335. SetLength(SegListStr,Length(seglist));
  336. for I:=0 to High(seglist) do
  337. begin
  338. case seglist[I].VType of
  339. vtString:
  340. SegListStr[I]:=Segments.FindIndexOf(seglist[I].VString^);
  341. vtAnsiString:
  342. SegListStr[I]:=Segments.FindIndexOf(AnsiString(seglist[I].VAnsiString));
  343. vtWideString:
  344. SegListStr[I]:=Segments.FindIndexOf(AnsiString(WideString(seglist[I].VWideString)));
  345. vtUnicodeString:
  346. SegListStr[I]:=Segments.FindIndexOf(AnsiString(UnicodeString(seglist[I].VUnicodeString)));
  347. else
  348. internalerror(2015040402);
  349. end;
  350. end;
  351. g.SegmentList:=SegListStr;
  352. end;
  353. procedure TOmfObjOutput.AddGroup(const groupname: string; seglist: TSegmentList);
  354. var
  355. g: TOmfRecord_GRPDEF;
  356. begin
  357. g:=TOmfRecord_GRPDEF.Create;
  358. Groups.Add(groupname,g);
  359. g.GroupNameIndex:=LNames.Add(groupname);
  360. g.SegmentList:=Copy(seglist);
  361. end;
  362. procedure TOmfObjOutput.WriteSections(Data: TObjData);
  363. var
  364. i:longint;
  365. sec:TObjSection;
  366. begin
  367. for i:=0 to Data.ObjSectionList.Count-1 do
  368. begin
  369. sec:=TObjSection(Data.ObjSectionList[i]);
  370. WriteSectionContentAndFixups(sec);
  371. end;
  372. end;
  373. procedure TOmfObjOutput.WriteSectionContentAndFixups(sec: TObjSection);
  374. const
  375. MaxChunkSize=$3fa;
  376. var
  377. RawRecord: TOmfRawRecord;
  378. ChunkStart,ChunkLen: DWord;
  379. ChunkFixupStart,ChunkFixupEnd: Integer;
  380. SegIndex: Integer;
  381. NextOfs: Integer;
  382. I: Integer;
  383. begin
  384. if (oso_data in sec.SecOptions) then
  385. begin
  386. if sec.Data=nil then
  387. internalerror(200403073);
  388. SegIndex:=Segments.FindIndexOf(sec.Name);
  389. RawRecord:=TOmfRawRecord.Create;
  390. sec.data.seek(0);
  391. ChunkFixupStart:=0;
  392. ChunkFixupEnd:=-1;
  393. ChunkStart:=0;
  394. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  395. while ChunkLen>0 do
  396. begin
  397. { write LEDATA record }
  398. RawRecord.RecordType:=RT_LEDATA;
  399. NextOfs:=RawRecord.WriteIndexedRef(0,SegIndex);
  400. RawRecord.RawData[NextOfs]:=Byte(ChunkStart);
  401. RawRecord.RawData[NextOfs+1]:=Byte(ChunkStart shr 8);
  402. Inc(NextOfs,2);
  403. sec.data.read(RawRecord.RawData[NextOfs], ChunkLen);
  404. Inc(NextOfs, ChunkLen);
  405. RawRecord.RecordLength:=NextOfs+1;
  406. RawRecord.CalculateChecksumByte;
  407. RawRecord.WriteTo(FWriter);
  408. { write FIXUPP record }
  409. while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
  410. (TOmfRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
  411. inc(ChunkFixupEnd);
  412. if ChunkFixupEnd>=ChunkFixupStart then
  413. begin
  414. RawRecord.RecordType:=RT_FIXUPP;
  415. NextOfs:=0;
  416. for I:=ChunkFixupStart to ChunkFixupEnd do
  417. begin
  418. TOmfRelocation(sec.ObjRelocations[I]).BuildOmfFixup;
  419. TOmfRelocation(sec.ObjRelocations[I]).OmfFixup.DataRecordStartOffset:=ChunkStart;
  420. NextOfs:=TOmfRelocation(sec.ObjRelocations[I]).OmfFixup.WriteAt(RawRecord,NextOfs);
  421. end;
  422. RawRecord.RecordLength:=NextOfs+1;
  423. RawRecord.CalculateChecksumByte;
  424. RawRecord.WriteTo(FWriter);
  425. end;
  426. { prepare next chunk }
  427. Inc(ChunkStart, ChunkLen);
  428. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  429. ChunkFixupStart:=ChunkFixupEnd+1;
  430. end;
  431. RawRecord.Free;
  432. end;
  433. end;
  434. procedure TOmfObjOutput.section_count_sections(p: TObject; arg: pointer);
  435. begin
  436. TOmfObjSection(p).index:=pinteger(arg)^;
  437. inc(pinteger(arg)^);
  438. end;
  439. procedure TOmfObjOutput.WritePUBDEFs(Data: TObjData);
  440. var
  441. PubNamesForSection: array of TFPHashObjectList;
  442. i: Integer;
  443. objsym: TObjSymbol;
  444. PublicNameElem: TOmfPublicNameElement;
  445. RawRecord: TOmfRawRecord;
  446. PubDefRec: TOmfRecord_PUBDEF;
  447. PrimaryGroupName: string;
  448. begin
  449. RawRecord:=TOmfRawRecord.Create;
  450. SetLength(PubNamesForSection,Data.ObjSectionList.Count);
  451. for i:=0 to Data.ObjSectionList.Count-1 do
  452. PubNamesForSection[i]:=TFPHashObjectList.Create;
  453. for i:=0 to Data.ObjSymbolList.Count-1 do
  454. begin
  455. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  456. if objsym.bind=AB_GLOBAL then
  457. begin
  458. PublicNameElem:=TOmfPublicNameElement.Create(PubNamesForSection[objsym.objsection.index-1],objsym.Name);
  459. PublicNameElem.PublicOffset:=objsym.offset;
  460. end;
  461. end;
  462. for i:=0 to Data.ObjSectionList.Count-1 do
  463. if PubNamesForSection[i].Count>0 then
  464. begin
  465. PubDefRec:=TOmfRecord_PUBDEF.Create;
  466. PubDefRec.BaseSegmentIndex:=i+1;
  467. PrimaryGroupName:=TOmfObjSection(Data.ObjSectionList[i]).PrimaryGroup;
  468. if PrimaryGroupName<>'' then
  469. PubDefRec.BaseGroupIndex:=Groups.FindIndexOf(PrimaryGroupName)
  470. else
  471. PubDefRec.BaseGroupIndex:=0;
  472. PubDefRec.PublicNames:=PubNamesForSection[i];
  473. while PubDefRec.NextIndex<PubDefRec.PublicNames.Count do
  474. begin
  475. PubDefRec.EncodeTo(RawRecord);
  476. RawRecord.WriteTo(FWriter);
  477. end;
  478. PubDefRec.Free;
  479. end;
  480. for i:=0 to Data.ObjSectionList.Count-1 do
  481. FreeAndNil(PubNamesForSection[i]);
  482. RawRecord.Free;
  483. end;
  484. function TOmfObjOutput.writeData(Data:TObjData):boolean;
  485. var
  486. RawRecord: TOmfRawRecord;
  487. Header: TOmfRecord_THEADR;
  488. Translator_COMENT: TOmfRecord_COMENT;
  489. LinkPassSeparator_COMENT: TOmfRecord_COMENT;
  490. LNamesRec: TOmfRecord_LNAMES;
  491. ModEnd: TOmfRecord_MODEND;
  492. I: Integer;
  493. SegDef: TOmfRecord_SEGDEF;
  494. GrpDef: TOmfRecord_GRPDEF;
  495. DGroupSegments: TSegmentList;
  496. nsections: Integer;
  497. begin
  498. { calc amount of sections we have and set their index, starting with 1 }
  499. nsections:=1;
  500. data.ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  501. { maximum amount of sections supported in the omf format is $7fff }
  502. if (nsections-1)>$7fff then
  503. internalerror(2015040701);
  504. { write header record }
  505. RawRecord:=TOmfRawRecord.Create;
  506. Header:=TOmfRecord_THEADR.Create;
  507. Header.ModuleName:=Data.Name;
  508. Header.EncodeTo(RawRecord);
  509. RawRecord.WriteTo(FWriter);
  510. Header.Free;
  511. { write translator COMENT header }
  512. Translator_COMENT:=TOmfRecord_COMENT.Create;
  513. Translator_COMENT.CommentClass:=CC_Translator;
  514. Translator_COMENT.CommentString:='FPC '+full_version_string+
  515. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname;
  516. Translator_COMENT.EncodeTo(RawRecord);
  517. RawRecord.WriteTo(FWriter);
  518. Translator_COMENT.Free;
  519. LNames.Clear;
  520. LNames.Add(''); { insert an empty string, which has index 1 }
  521. for i:=0 to Data.ObjSectionList.Count-1 do
  522. with TOmfObjSection(Data.ObjSectionList[I]) do
  523. AddSegment(Name,ClassName,OverlayName,OmfAlignment,Combination,Use,Size);
  524. { create group "dgroup" }
  525. SetLength(DGroupSegments,0);
  526. for i:=0 to Data.ObjSectionList.Count-1 do
  527. with TOmfObjSection(Data.ObjSectionList[I]) do
  528. if PrimaryGroup='dgroup' then
  529. begin
  530. SetLength(DGroupSegments,Length(DGroupSegments)+1);
  531. DGroupSegments[High(DGroupSegments)]:=index;
  532. end;
  533. AddGroup('dgroup',DGroupSegments);
  534. { write LNAMES record(s) }
  535. LNamesRec:=TOmfRecord_LNAMES.Create;
  536. LNamesRec.Names:=LNames;
  537. while LNamesRec.NextIndex<=LNames.Count do
  538. begin
  539. LNamesRec.EncodeTo(RawRecord);
  540. RawRecord.WriteTo(FWriter);
  541. end;
  542. LNamesRec.Free;
  543. { write SEGDEF record(s) }
  544. for I:=1 to Segments.Count-1 do
  545. begin
  546. SegDef:=TOmfRecord_SEGDEF(Segments[I]);
  547. SegDef.EncodeTo(RawRecord);
  548. RawRecord.WriteTo(FWriter);
  549. end;
  550. { write GRPDEF record(s) }
  551. for I:=1 to Groups.Count-1 do
  552. begin
  553. GrpDef:=TOmfRecord_GRPDEF(Groups[I]);
  554. GrpDef.EncodeTo(RawRecord);
  555. RawRecord.WriteTo(FWriter);
  556. end;
  557. { write PUBDEF record(s) }
  558. WritePUBDEFs(Data);
  559. { write link pass separator }
  560. LinkPassSeparator_COMENT:=TOmfRecord_COMENT.Create;
  561. LinkPassSeparator_COMENT.CommentClass:=CC_LinkPassSeparator;
  562. LinkPassSeparator_COMENT.CommentString:=#1;
  563. LinkPassSeparator_COMENT.NoList:=True;
  564. LinkPassSeparator_COMENT.EncodeTo(RawRecord);
  565. RawRecord.WriteTo(FWriter);
  566. LinkPassSeparator_COMENT.Free;
  567. { write section content, interleaved with fixups }
  568. WriteSections(Data);
  569. { write MODEND record }
  570. ModEnd:=TOmfRecord_MODEND.Create;
  571. ModEnd.EncodeTo(RawRecord);
  572. RawRecord.WriteTo(FWriter);
  573. ModEnd.Free;
  574. RawRecord.Free;
  575. result:=true;
  576. end;
  577. constructor TOmfObjOutput.create(AWriter:TObjectWriter);
  578. begin
  579. inherited create(AWriter);
  580. cobjdata:=TOmfObjData;
  581. FLNames:=TOmfOrderedNameCollection.Create;
  582. FSegments:=TFPHashObjectList.Create;
  583. FSegments.Add('',nil);
  584. FGroups:=TFPHashObjectList.Create;
  585. FGroups.Add('',nil);
  586. end;
  587. destructor TOmfObjOutput.Destroy;
  588. begin
  589. FGroups.Free;
  590. FSegments.Free;
  591. FLNames.Free;
  592. inherited Destroy;
  593. end;
  594. {****************************************************************************
  595. TOmfAssembler
  596. ****************************************************************************}
  597. constructor TOmfAssembler.Create(smart:boolean);
  598. begin
  599. inherited Create(smart);
  600. CObjOutput:=TOmfObjOutput;
  601. end;
  602. {*****************************************************************************
  603. Initialize
  604. *****************************************************************************}
  605. {$ifdef i8086}
  606. const
  607. as_i8086_omf_info : tasminfo =
  608. (
  609. id : as_i8086_omf;
  610. idtxt : 'OMF';
  611. asmbin : '';
  612. asmcmd : '';
  613. supported_targets : [system_i8086_msdos];
  614. flags : [af_outputbinary,af_needar,af_no_debug];
  615. labelprefix : '..@';
  616. comment : '; ';
  617. dollarsign: '$';
  618. );
  619. {$endif i8086}
  620. initialization
  621. {$ifdef i8086}
  622. RegisterAssembler(as_i8086_omf_info,TOmfAssembler);
  623. {$endif i8086}
  624. end.