ogomf.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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 if (Aname='debug_frame') or
  250. (Aname='debug_info') or
  251. (Aname='debug_line') or
  252. (Aname='debug_abbrev') then
  253. begin
  254. FClassName:='DWARF';
  255. FUse:=suUse32;
  256. dgroup:=false;
  257. end
  258. else
  259. begin
  260. FClassName:='data';
  261. dgroup:=true;
  262. end;
  263. if dgroup then
  264. FPrimaryGroup:='dgroup'
  265. else
  266. FPrimaryGroup:='';
  267. end;
  268. {****************************************************************************
  269. TOmfObjData
  270. ****************************************************************************}
  271. class function TOmfObjData.CodeSectionName(const aname: string): string;
  272. begin
  273. {$ifdef i8086}
  274. if current_settings.x86memorymodel in x86_far_code_models then
  275. begin
  276. if cs_huge_code in current_settings.moduleswitches then
  277. result:=aname + '_TEXT'
  278. else
  279. result:=current_module.modulename^ + '_TEXT';
  280. end
  281. else
  282. {$endif}
  283. result:='text';
  284. end;
  285. constructor TOmfObjData.create(const n: string);
  286. begin
  287. inherited create(n);
  288. CObjSection:=TOmfObjSection;
  289. end;
  290. function TOmfObjData.sectiontype2align(atype: TAsmSectiontype): shortint;
  291. begin
  292. case atype of
  293. sec_stabstr,sec_debug_info,sec_debug_line,sec_debug_abbrev:
  294. result:=1;
  295. sec_code:
  296. result:=1;
  297. sec_bss:
  298. result:=1;
  299. sec_data:
  300. result:=2;
  301. { For idata (at least idata2) it must be 4 bytes, because
  302. an entry is always (also in win64) 20 bytes and aligning
  303. on 8 bytes will insert 4 bytes between the entries resulting
  304. in a corrupt idata section.
  305. Same story with .pdata, it has 4-byte elements which should
  306. be packed without gaps. }
  307. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_pdata:
  308. result:=4;
  309. sec_stack,
  310. sec_heap:
  311. result:=16;
  312. else
  313. result:=1;
  314. end;
  315. end;
  316. function TOmfObjData.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  317. const
  318. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  319. 'text',
  320. 'data',
  321. 'data',
  322. 'rodata',
  323. 'bss',
  324. 'tbss',
  325. 'pdata',
  326. 'text','data','data','data','data',
  327. 'stab',
  328. 'stabstr',
  329. 'idata2','idata4','idata5','idata6','idata7','edata',
  330. 'eh_frame',
  331. 'debug_frame','debug_info','debug_line','debug_abbrev',
  332. 'fpc',
  333. '',
  334. 'init',
  335. 'fini',
  336. 'objc_class',
  337. 'objc_meta_class',
  338. 'objc_cat_cls_meth',
  339. 'objc_cat_inst_meth',
  340. 'objc_protocol',
  341. 'objc_string_object',
  342. 'objc_cls_meth',
  343. 'objc_inst_meth',
  344. 'objc_cls_refs',
  345. 'objc_message_refs',
  346. 'objc_symbols',
  347. 'objc_category',
  348. 'objc_class_vars',
  349. 'objc_instance_vars',
  350. 'objc_module_info',
  351. 'objc_class_names',
  352. 'objc_meth_var_types',
  353. 'objc_meth_var_names',
  354. 'objc_selector_strs',
  355. 'objc_protocol_ext',
  356. 'objc_class_ext',
  357. 'objc_property',
  358. 'objc_image_info',
  359. 'objc_cstring_object',
  360. 'objc_sel_fixup',
  361. '__DATA,__objc_data',
  362. '__DATA,__objc_const',
  363. 'objc_superrefs',
  364. '__DATA, __datacoal_nt,coalesced',
  365. 'objc_classlist',
  366. 'objc_nlclasslist',
  367. 'objc_catlist',
  368. 'obcj_nlcatlist',
  369. 'objc_protolist',
  370. 'stack',
  371. 'heap'
  372. );
  373. begin
  374. if (atype=sec_user) then
  375. Result:=aname
  376. else if secnames[atype]='text' then
  377. Result:=CodeSectionName(aname)
  378. else
  379. Result:=secnames[atype];
  380. end;
  381. procedure TOmfObjData.writeReloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  382. var
  383. objreloc: TOmfRelocation;
  384. symaddr: AWord;
  385. begin
  386. { Write('writeReloc(', data, ',', len, ',');
  387. if p<>nil then
  388. write(p.Name)
  389. else
  390. write('nil');
  391. Writeln(',',Reloctype,')');}
  392. { RELOC_FARPTR = RELOC_ABSOLUTE+RELOC_SEG }
  393. if Reloctype=RELOC_FARPTR then
  394. begin
  395. if len<>4 then
  396. internalerror(2015041502);
  397. writeReloc(Data,2,p,RELOC_ABSOLUTE);
  398. writeReloc(0,2,p,RELOC_SEG);
  399. exit;
  400. end;
  401. if CurrObjSec=nil then
  402. internalerror(200403072);
  403. objreloc:=nil;
  404. if assigned(p) then
  405. begin
  406. { real address of the symbol }
  407. symaddr:=p.address;
  408. if p.bind=AB_EXTERNAL then
  409. begin
  410. objreloc:=TOmfRelocation.CreateSymbol(CurrObjSec.Size,p,Reloctype);
  411. CurrObjSec.ObjRelocations.Add(objreloc);
  412. end
  413. else
  414. begin
  415. objreloc:=TOmfRelocation.CreateSection(CurrObjSec.Size,p.objsection,Reloctype);
  416. CurrObjSec.ObjRelocations.Add(objreloc);
  417. inc(data,symaddr);
  418. end;
  419. end;
  420. CurrObjSec.write(data,len);
  421. end;
  422. {****************************************************************************
  423. TOmfObjOutput
  424. ****************************************************************************}
  425. procedure TOmfObjOutput.AddSegment(const name, segclass, ovlname: string;
  426. Alignment: TOmfSegmentAlignment; Combination: TOmfSegmentCombination;
  427. Use: TOmfSegmentUse; Size: aword);
  428. var
  429. s: TOmfRecord_SEGDEF;
  430. begin
  431. s:=TOmfRecord_SEGDEF.Create;
  432. Segments.Add(name,s);
  433. s.SegmentNameIndex:=LNames.Add(name);
  434. s.ClassNameIndex:=LNames.Add(segclass);
  435. s.OverlayNameIndex:=LNames.Add(ovlname);
  436. s.Alignment:=Alignment;
  437. s.Combination:=Combination;
  438. s.Use:=Use;
  439. s.SegmentLength:=Size;
  440. end;
  441. procedure TOmfObjOutput.AddGroup(const groupname: string; seglist: array of const);
  442. var
  443. g: TOmfRecord_GRPDEF;
  444. I: Integer;
  445. SegListStr: TSegmentList;
  446. begin
  447. g:=TOmfRecord_GRPDEF.Create;
  448. Groups.Add(groupname,g);
  449. g.GroupNameIndex:=LNames.Add(groupname);
  450. SetLength(SegListStr,Length(seglist));
  451. for I:=0 to High(seglist) do
  452. begin
  453. case seglist[I].VType of
  454. vtString:
  455. SegListStr[I]:=Segments.FindIndexOf(seglist[I].VString^);
  456. vtAnsiString:
  457. SegListStr[I]:=Segments.FindIndexOf(AnsiString(seglist[I].VAnsiString));
  458. vtWideString:
  459. SegListStr[I]:=Segments.FindIndexOf(AnsiString(WideString(seglist[I].VWideString)));
  460. vtUnicodeString:
  461. SegListStr[I]:=Segments.FindIndexOf(AnsiString(UnicodeString(seglist[I].VUnicodeString)));
  462. else
  463. internalerror(2015040402);
  464. end;
  465. end;
  466. g.SegmentList:=SegListStr;
  467. end;
  468. procedure TOmfObjOutput.AddGroup(const groupname: string; seglist: TSegmentList);
  469. var
  470. g: TOmfRecord_GRPDEF;
  471. begin
  472. g:=TOmfRecord_GRPDEF.Create;
  473. Groups.Add(groupname,g);
  474. g.GroupNameIndex:=LNames.Add(groupname);
  475. g.SegmentList:=Copy(seglist);
  476. end;
  477. procedure TOmfObjOutput.WriteSections(Data: TObjData);
  478. var
  479. i:longint;
  480. sec:TObjSection;
  481. begin
  482. for i:=0 to Data.ObjSectionList.Count-1 do
  483. begin
  484. sec:=TObjSection(Data.ObjSectionList[i]);
  485. WriteSectionContentAndFixups(sec);
  486. end;
  487. end;
  488. procedure TOmfObjOutput.WriteSectionContentAndFixups(sec: TObjSection);
  489. const
  490. MaxChunkSize=$3fa;
  491. var
  492. RawRecord: TOmfRawRecord;
  493. ChunkStart,ChunkLen: DWord;
  494. ChunkFixupStart,ChunkFixupEnd: Integer;
  495. SegIndex: Integer;
  496. NextOfs: Integer;
  497. I: Integer;
  498. begin
  499. if (oso_data in sec.SecOptions) then
  500. begin
  501. if sec.Data=nil then
  502. internalerror(200403073);
  503. SegIndex:=Segments.FindIndexOf(sec.Name);
  504. RawRecord:=TOmfRawRecord.Create;
  505. sec.data.seek(0);
  506. ChunkFixupStart:=0;
  507. ChunkFixupEnd:=-1;
  508. ChunkStart:=0;
  509. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  510. while ChunkLen>0 do
  511. begin
  512. { write LEDATA record }
  513. RawRecord.RecordType:=RT_LEDATA;
  514. NextOfs:=RawRecord.WriteIndexedRef(0,SegIndex);
  515. RawRecord.RawData[NextOfs]:=Byte(ChunkStart);
  516. RawRecord.RawData[NextOfs+1]:=Byte(ChunkStart shr 8);
  517. Inc(NextOfs,2);
  518. sec.data.read(RawRecord.RawData[NextOfs], ChunkLen);
  519. Inc(NextOfs, ChunkLen);
  520. RawRecord.RecordLength:=NextOfs+1;
  521. RawRecord.CalculateChecksumByte;
  522. RawRecord.WriteTo(FWriter);
  523. { write FIXUPP record }
  524. while (ChunkFixupEnd<(sec.ObjRelocations.Count-1)) and
  525. (TOmfRelocation(sec.ObjRelocations[ChunkFixupEnd+1]).DataOffset<(ChunkStart+ChunkLen)) do
  526. inc(ChunkFixupEnd);
  527. if ChunkFixupEnd>=ChunkFixupStart then
  528. begin
  529. RawRecord.RecordType:=RT_FIXUPP;
  530. NextOfs:=0;
  531. for I:=ChunkFixupStart to ChunkFixupEnd do
  532. begin
  533. TOmfRelocation(sec.ObjRelocations[I]).BuildOmfFixup;
  534. TOmfRelocation(sec.ObjRelocations[I]).OmfFixup.DataRecordStartOffset:=ChunkStart;
  535. NextOfs:=TOmfRelocation(sec.ObjRelocations[I]).OmfFixup.WriteAt(RawRecord,NextOfs);
  536. end;
  537. RawRecord.RecordLength:=NextOfs+1;
  538. RawRecord.CalculateChecksumByte;
  539. RawRecord.WriteTo(FWriter);
  540. end;
  541. { prepare next chunk }
  542. Inc(ChunkStart, ChunkLen);
  543. ChunkLen:=Min(MaxChunkSize, sec.Data.size-ChunkStart);
  544. ChunkFixupStart:=ChunkFixupEnd+1;
  545. end;
  546. RawRecord.Free;
  547. end;
  548. end;
  549. procedure TOmfObjOutput.section_count_sections(p: TObject; arg: pointer);
  550. begin
  551. TOmfObjSection(p).index:=pinteger(arg)^;
  552. inc(pinteger(arg)^);
  553. end;
  554. procedure TOmfObjOutput.WritePUBDEFs(Data: TObjData);
  555. var
  556. PubNamesForSection: array of TFPHashObjectList;
  557. i: Integer;
  558. objsym: TObjSymbol;
  559. PublicNameElem: TOmfPublicNameElement;
  560. RawRecord: TOmfRawRecord;
  561. PubDefRec: TOmfRecord_PUBDEF;
  562. PrimaryGroupName: string;
  563. begin
  564. RawRecord:=TOmfRawRecord.Create;
  565. SetLength(PubNamesForSection,Data.ObjSectionList.Count);
  566. for i:=0 to Data.ObjSectionList.Count-1 do
  567. PubNamesForSection[i]:=TFPHashObjectList.Create;
  568. for i:=0 to Data.ObjSymbolList.Count-1 do
  569. begin
  570. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  571. if objsym.bind=AB_GLOBAL then
  572. begin
  573. PublicNameElem:=TOmfPublicNameElement.Create(PubNamesForSection[objsym.objsection.index-1],objsym.Name);
  574. PublicNameElem.PublicOffset:=objsym.offset;
  575. end;
  576. end;
  577. for i:=0 to Data.ObjSectionList.Count-1 do
  578. if PubNamesForSection[i].Count>0 then
  579. begin
  580. PubDefRec:=TOmfRecord_PUBDEF.Create;
  581. PubDefRec.BaseSegmentIndex:=i+1;
  582. PrimaryGroupName:=TOmfObjSection(Data.ObjSectionList[i]).PrimaryGroup;
  583. if PrimaryGroupName<>'' then
  584. PubDefRec.BaseGroupIndex:=Groups.FindIndexOf(PrimaryGroupName)
  585. else
  586. PubDefRec.BaseGroupIndex:=0;
  587. PubDefRec.PublicNames:=PubNamesForSection[i];
  588. while PubDefRec.NextIndex<PubDefRec.PublicNames.Count do
  589. begin
  590. PubDefRec.EncodeTo(RawRecord);
  591. RawRecord.WriteTo(FWriter);
  592. end;
  593. PubDefRec.Free;
  594. end;
  595. for i:=0 to Data.ObjSectionList.Count-1 do
  596. FreeAndNil(PubNamesForSection[i]);
  597. RawRecord.Free;
  598. end;
  599. procedure TOmfObjOutput.WriteEXTDEFs(Data: TObjData);
  600. var
  601. ExtNames: TFPHashObjectList;
  602. RawRecord: TOmfRawRecord;
  603. i,idx: Integer;
  604. objsym: TObjSymbol;
  605. ExternalNameElem: TOmfExternalNameElement;
  606. ExtDefRec: TOmfRecord_EXTDEF;
  607. begin
  608. ExtNames:=TFPHashObjectList.Create;
  609. RawRecord:=TOmfRawRecord.Create;
  610. idx:=1;
  611. for i:=0 to Data.ObjSymbolList.Count-1 do
  612. begin
  613. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  614. if objsym.bind=AB_EXTERNAL then
  615. begin
  616. ExternalNameElem:=TOmfExternalNameElement.Create(ExtNames,objsym.Name);
  617. objsym.symidx:=idx;
  618. Inc(idx);
  619. end;
  620. end;
  621. if ExtNames.Count>0 then
  622. begin
  623. ExtDefRec:=TOmfRecord_EXTDEF.Create;
  624. ExtDefRec.ExternalNames:=ExtNames;
  625. while ExtDefRec.NextIndex<ExtDefRec.ExternalNames.Count do
  626. begin
  627. ExtDefRec.EncodeTo(RawRecord);
  628. RawRecord.WriteTo(FWriter);
  629. end;
  630. ExtDefRec.Free;
  631. end;
  632. ExtNames.Free;
  633. RawRecord.Free;
  634. end;
  635. function TOmfObjOutput.writeData(Data:TObjData):boolean;
  636. var
  637. RawRecord: TOmfRawRecord;
  638. Header: TOmfRecord_THEADR;
  639. Translator_COMENT: TOmfRecord_COMENT;
  640. LinkPassSeparator_COMENT: TOmfRecord_COMENT;
  641. LNamesRec: TOmfRecord_LNAMES;
  642. ModEnd: TOmfRecord_MODEND;
  643. I: Integer;
  644. SegDef: TOmfRecord_SEGDEF;
  645. GrpDef: TOmfRecord_GRPDEF;
  646. DGroupSegments: TSegmentList;
  647. nsections: Integer;
  648. begin
  649. { calc amount of sections we have and set their index, starting with 1 }
  650. nsections:=1;
  651. data.ObjSectionList.ForEachCall(@section_count_sections,@nsections);
  652. { maximum amount of sections supported in the omf format is $7fff }
  653. if (nsections-1)>$7fff then
  654. internalerror(2015040701);
  655. { write header record }
  656. RawRecord:=TOmfRawRecord.Create;
  657. Header:=TOmfRecord_THEADR.Create;
  658. Header.ModuleName:=Data.Name;
  659. Header.EncodeTo(RawRecord);
  660. RawRecord.WriteTo(FWriter);
  661. Header.Free;
  662. { write translator COMENT header }
  663. Translator_COMENT:=TOmfRecord_COMENT.Create;
  664. Translator_COMENT.CommentClass:=CC_Translator;
  665. Translator_COMENT.CommentString:='FPC '+full_version_string+
  666. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname;
  667. Translator_COMENT.EncodeTo(RawRecord);
  668. RawRecord.WriteTo(FWriter);
  669. Translator_COMENT.Free;
  670. LNames.Clear;
  671. LNames.Add(''); { insert an empty string, which has index 1 }
  672. FSegments.Clear;
  673. FSegments.Add('',nil);
  674. FGroups.Clear;
  675. FGroups.Add('',nil);
  676. for i:=0 to Data.ObjSectionList.Count-1 do
  677. with TOmfObjSection(Data.ObjSectionList[I]) do
  678. AddSegment(Name,ClassName,OverlayName,OmfAlignment,Combination,Use,Size);
  679. { create group "dgroup" }
  680. SetLength(DGroupSegments,0);
  681. for i:=0 to Data.ObjSectionList.Count-1 do
  682. with TOmfObjSection(Data.ObjSectionList[I]) do
  683. if PrimaryGroup='dgroup' then
  684. begin
  685. SetLength(DGroupSegments,Length(DGroupSegments)+1);
  686. DGroupSegments[High(DGroupSegments)]:=index;
  687. end;
  688. AddGroup('dgroup',DGroupSegments);
  689. { write LNAMES record(s) }
  690. LNamesRec:=TOmfRecord_LNAMES.Create;
  691. LNamesRec.Names:=LNames;
  692. while LNamesRec.NextIndex<=LNames.Count do
  693. begin
  694. LNamesRec.EncodeTo(RawRecord);
  695. RawRecord.WriteTo(FWriter);
  696. end;
  697. LNamesRec.Free;
  698. { write SEGDEF record(s) }
  699. for I:=1 to Segments.Count-1 do
  700. begin
  701. SegDef:=TOmfRecord_SEGDEF(Segments[I]);
  702. SegDef.EncodeTo(RawRecord);
  703. RawRecord.WriteTo(FWriter);
  704. end;
  705. { write GRPDEF record(s) }
  706. for I:=1 to Groups.Count-1 do
  707. begin
  708. GrpDef:=TOmfRecord_GRPDEF(Groups[I]);
  709. GrpDef.EncodeTo(RawRecord);
  710. RawRecord.WriteTo(FWriter);
  711. end;
  712. { write PUBDEF record(s) }
  713. WritePUBDEFs(Data);
  714. { write EXTDEF record(s) }
  715. WriteEXTDEFs(Data);
  716. { write link pass separator }
  717. LinkPassSeparator_COMENT:=TOmfRecord_COMENT.Create;
  718. LinkPassSeparator_COMENT.CommentClass:=CC_LinkPassSeparator;
  719. LinkPassSeparator_COMENT.CommentString:=#1;
  720. LinkPassSeparator_COMENT.NoList:=True;
  721. LinkPassSeparator_COMENT.EncodeTo(RawRecord);
  722. RawRecord.WriteTo(FWriter);
  723. LinkPassSeparator_COMENT.Free;
  724. { write section content, interleaved with fixups }
  725. WriteSections(Data);
  726. { write MODEND record }
  727. ModEnd:=TOmfRecord_MODEND.Create;
  728. ModEnd.EncodeTo(RawRecord);
  729. RawRecord.WriteTo(FWriter);
  730. ModEnd.Free;
  731. RawRecord.Free;
  732. result:=true;
  733. end;
  734. constructor TOmfObjOutput.create(AWriter:TObjectWriter);
  735. begin
  736. inherited create(AWriter);
  737. cobjdata:=TOmfObjData;
  738. FLNames:=TOmfOrderedNameCollection.Create;
  739. FSegments:=TFPHashObjectList.Create;
  740. FSegments.Add('',nil);
  741. FGroups:=TFPHashObjectList.Create;
  742. FGroups.Add('',nil);
  743. end;
  744. destructor TOmfObjOutput.Destroy;
  745. begin
  746. FGroups.Free;
  747. FSegments.Free;
  748. FLNames.Free;
  749. inherited Destroy;
  750. end;
  751. {****************************************************************************
  752. TOmfAssembler
  753. ****************************************************************************}
  754. constructor TOmfAssembler.Create(smart:boolean);
  755. begin
  756. inherited Create(smart);
  757. CObjOutput:=TOmfObjOutput;
  758. end;
  759. {*****************************************************************************
  760. Initialize
  761. *****************************************************************************}
  762. {$ifdef i8086}
  763. const
  764. as_i8086_omf_info : tasminfo =
  765. (
  766. id : as_i8086_omf;
  767. idtxt : 'OMF';
  768. asmbin : '';
  769. asmcmd : '';
  770. supported_targets : [system_i8086_msdos];
  771. flags : [af_outputbinary,af_needar,af_no_debug];
  772. labelprefix : '..@';
  773. comment : '; ';
  774. dollarsign: '$';
  775. );
  776. {$endif i8086}
  777. initialization
  778. {$ifdef i8086}
  779. RegisterAssembler(as_i8086_omf_info,TOmfAssembler);
  780. {$endif i8086}
  781. end.