ogomf.pas 29 KB

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