ogrel.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. {
  2. Copyright (c) 2020 by Nikolay Nikolov
  3. Contains the ASCII relocatable object file format (*.rel) reader and writer
  4. This is the object format used on the Z80 platforms.
  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 ogrel;
  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. { output }
  29. ogbase,
  30. owbase;
  31. type
  32. { TRelObjData }
  33. TRelObjData = class(TObjData)
  34. public
  35. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  36. procedure writeReloc(Data:TRelocDataInt;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);override;
  37. end;
  38. { TRelObjOutput }
  39. TRelObjOutput = class(tObjOutput)
  40. private
  41. procedure writeString(const S: ansistring);
  42. procedure writeLine(const S: ansistring);
  43. protected
  44. function writeData(Data:TObjData):boolean;override;
  45. public
  46. constructor create(AWriter:TObjectWriter);override;
  47. end;
  48. { TRelAssembler }
  49. TRelAssembler = class(tinternalassembler)
  50. constructor create(info: pasminfo; smart:boolean);override;
  51. end;
  52. implementation
  53. uses
  54. SysUtils,
  55. cutils,verbose,globals,
  56. fmodule,aasmtai,aasmdata,
  57. ogmap,
  58. version
  59. ;
  60. {*****************************************************************************
  61. TRelObjData
  62. *****************************************************************************}
  63. function TRelObjData.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  64. const
  65. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  66. '_CODE',
  67. '_DATA',
  68. '_DATA',
  69. '.rodata',
  70. '.bss',
  71. '.threadvar',
  72. '.pdata',
  73. '', { stubs }
  74. '__DATA,__nl_symbol_ptr',
  75. '__DATA,__la_symbol_ptr',
  76. '__DATA,__mod_init_func',
  77. '__DATA,__mod_term_func',
  78. '.stab',
  79. '.stabstr',
  80. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  81. '.eh_frame',
  82. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  83. '.fpc',
  84. '.toc',
  85. '.init',
  86. '.fini',
  87. '.objc_class',
  88. '.objc_meta_class',
  89. '.objc_cat_cls_meth',
  90. '.objc_cat_inst_meth',
  91. '.objc_protocol',
  92. '.objc_string_object',
  93. '.objc_cls_meth',
  94. '.objc_inst_meth',
  95. '.objc_cls_refs',
  96. '.objc_message_refs',
  97. '.objc_symbols',
  98. '.objc_category',
  99. '.objc_class_vars',
  100. '.objc_instance_vars',
  101. '.objc_module_info',
  102. '.objc_class_names',
  103. '.objc_meth_var_types',
  104. '.objc_meth_var_names',
  105. '.objc_selector_strs',
  106. '.objc_protocol_ext',
  107. '.objc_class_ext',
  108. '.objc_property',
  109. '.objc_image_info',
  110. '.objc_cstring_object',
  111. '.objc_sel_fixup',
  112. '__DATA,__objc_data',
  113. '__DATA,__objc_const',
  114. '.objc_superrefs',
  115. '__DATA, __datacoal_nt,coalesced',
  116. '.objc_classlist',
  117. '.objc_nlclasslist',
  118. '.objc_catlist',
  119. '.obcj_nlcatlist',
  120. '.objc_protolist',
  121. '.stack',
  122. '.heap',
  123. '.gcc_except_table',
  124. '.ARM.attributes'
  125. );
  126. begin
  127. result:=secnames[atype];
  128. end;
  129. procedure TRelObjData.writeReloc(Data: TRelocDataInt; len: aword; p: TObjSymbol; Reloctype: TObjRelocationType);
  130. begin
  131. end;
  132. {*****************************************************************************
  133. TRelObjOutput
  134. *****************************************************************************}
  135. procedure TRelObjOutput.writeString(const S: ansistring);
  136. begin
  137. FWriter.write(S[1],Length(S));
  138. end;
  139. procedure TRelObjOutput.writeLine(const S: ansistring);
  140. begin
  141. writeString(S+#10)
  142. end;
  143. function TRelObjOutput.writeData(Data: TObjData): boolean;
  144. var
  145. global_symbols_count: Integer = 0;
  146. idx, i: Integer;
  147. objsym: TObjSymbol;
  148. begin
  149. global_symbols_count:=0;
  150. for i:=0 to Data.ObjSymbolList.Count-1 do
  151. begin
  152. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  153. if objsym.bind=AB_EXTERNAL then
  154. Inc(global_symbols_count);
  155. end;
  156. writeLine('XL2');
  157. writeLine('H '+tostr(data.ObjSectionList.Count)+' areas '+tostr(global_symbols_count)+' global symbols');
  158. idx:=0;
  159. for i:=0 to Data.ObjSymbolList.Count-1 do
  160. begin
  161. objsym:=TObjSymbol(Data.ObjSymbolList[i]);
  162. if objsym.bind=AB_EXTERNAL then
  163. begin
  164. writeLine('S '+ApplyAsmSymbolRestrictions(objsym.Name)+' Ref0000');
  165. objsym.symidx:=idx;
  166. Inc(idx);
  167. end;
  168. end;
  169. result:=true;
  170. end;
  171. constructor TRelObjOutput.create(AWriter: TObjectWriter);
  172. begin
  173. inherited;
  174. cobjdata:=TRelObjData;
  175. end;
  176. {*****************************************************************************
  177. TRelAssembler
  178. *****************************************************************************}
  179. constructor TRelAssembler.create(info: pasminfo; smart: boolean);
  180. begin
  181. inherited;
  182. CObjOutput:=TRelObjOutput;
  183. end;
  184. {*****************************************************************************
  185. Initialize
  186. *****************************************************************************}
  187. const
  188. as_z80_rel_info : tasminfo =
  189. (
  190. id : as_z80_rel;
  191. idtxt : 'REL';
  192. asmbin : '';
  193. asmcmd : '';
  194. supported_targets : [system_z80_embedded,system_z80_zxspectrum];
  195. flags : [af_outputbinary,af_smartlink_sections];
  196. labelprefix : '..@';
  197. labelmaxlen : 79;
  198. comment : '; ';
  199. dollarsign: '$';
  200. );
  201. initialization
  202. RegisterAssembler(as_z80_rel_info,TRelAssembler);
  203. end.