ogomf.pas 32 KB

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