ogomf.pas 32 KB

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