ogomf.pas 25 KB

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