ogomf.pas 31 KB

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