ogomf.pas 28 KB

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