ogomf.pas 25 KB

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