aggas.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. {
  2. Copyright (c) 1998-2006 by the Free Pascal team
  3. This unit implements the generic part of the GNU assembler
  4. (v2.8 or later) writer
  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. { Base unit for writing GNU assembler output.
  19. }
  20. unit aggas;
  21. {$i fpcdefs.inc}
  22. { $define DEBUG_AGGAS}
  23. interface
  24. uses
  25. globtype,globals,
  26. aasmbase,aasmtai,aasmdata,aasmcfi,
  27. assemble;
  28. type
  29. TCPUInstrWriter = class;
  30. {# This is a derived class which is used to write
  31. GAS styled assembler.
  32. }
  33. { TGNUAssembler }
  34. TGNUAssembler=class(texternalassembler)
  35. protected
  36. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
  37. function sectionattrs(atype:TAsmSectiontype):string;virtual;
  38. function sectionattrs_coff(atype:TAsmSectiontype):string;virtual;
  39. function sectionalignment_aix(atype:TAsmSectiontype;secalign: longint):string;
  40. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;
  41. secflags:TSectionFlags=SF_None;secprogbits:TSectionProgbits=SPB_None);virtual;
  42. procedure WriteExtraHeader;virtual;
  43. procedure WriteExtraFooter;virtual;
  44. procedure WriteInstruction(hp: tai);
  45. procedure WriteWeakSymbolRef(s: tasmsymbol); virtual;
  46. procedure WriteAixStringConst(hp: tai_string);
  47. procedure WriteAixIntConst(hp: tai_const);
  48. procedure WriteUnalignedIntConst(hp: tai_const);
  49. procedure WriteDirectiveName(dir: TAsmDirective); virtual;
  50. public
  51. function MakeCmdLine: TCmdStr; override;
  52. procedure WriteTree(p:TAsmList);override;
  53. procedure WriteAsmList;override;
  54. destructor destroy; override;
  55. private
  56. setcount: longint;
  57. procedure WriteDecodedSleb128(a: int64);
  58. procedure WriteDecodedUleb128(a: qword);
  59. procedure WriteCFI(hp: tai_cfi_base);
  60. function NextSetLabel: string;
  61. protected
  62. InstrWriter: TCPUInstrWriter;
  63. end;
  64. {# This is the base class for writing instructions.
  65. The WriteInstruction() method must be overridden
  66. to write a single instruction to the assembler
  67. file.
  68. }
  69. TCPUInstrWriter = class
  70. constructor create(_owner: TGNUAssembler);
  71. procedure WriteInstruction(hp : tai); virtual; abstract;
  72. protected
  73. owner: TGNUAssembler;
  74. end;
  75. { TAppleGNUAssembler }
  76. TAppleGNUAssembler=class(TGNUAssembler)
  77. protected
  78. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  79. procedure WriteWeakSymbolRef(s: tasmsymbol); override;
  80. procedure WriteDirectiveName(dir: TAsmDirective); override;
  81. end;
  82. TAoutGNUAssembler=class(TGNUAssembler)
  83. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  84. end;
  85. implementation
  86. uses
  87. SysUtils,
  88. cutils,cfileutl,systems,
  89. fmodule,verbose,
  90. {$ifndef DISABLE_WIN64_SEH}
  91. itcpugas,
  92. {$endif DISABLE_WIN64_SEH}
  93. {$ifdef m68k}
  94. cpuinfo,aasmcpu,
  95. {$endif m68k}
  96. cpubase,objcasm;
  97. const
  98. line_length = 70;
  99. var
  100. symendcount : longint;
  101. {****************************************************************************}
  102. { Support routines }
  103. {****************************************************************************}
  104. const
  105. ait_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  106. #9'.fixme128'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9,
  107. #9'.sleb128'#9,#9'.uleb128'#9,
  108. #9'.rva'#9,#9'.secrel32'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.short'#9,
  109. #9'.short'#9,#9'.long'#9,#9'.quad'#9
  110. );
  111. ait_solaris_const2str : array[aitconst_128bit..aitconst_64bit_unaligned] of string[20]=(
  112. #9'.fixme128'#9,#9'.8byte'#9,#9'.4byte'#9,#9'.2byte'#9,#9'.byte'#9,
  113. #9'.sleb128'#9,#9'.uleb128'#9,
  114. #9'.rva'#9,#9'.secrel32'#9,#9'.8byte'#9,#9'.4byte'#9,#9'.2byte'#9,#9'.2byte'#9,
  115. #9'.2byte'#9,#9'.4byte'#9,#9'.8byte'#9
  116. );
  117. ait_unaligned_consts = [aitconst_16bit_unaligned..aitconst_64bit_unaligned];
  118. { Sparc type of unaligned pseudo-instructions }
  119. use_ua_sparc_systems = [system_sparc_linux];
  120. ait_ua_sparc_const2str : array[aitconst_16bit_unaligned..aitconst_64bit_unaligned]
  121. of string[20]=(
  122. #9'.uahalf'#9,#9'.uaword'#9,#9'.uaxword'#9
  123. );
  124. { Generic unaligned pseudo-instructions, seems ELF specific }
  125. use_ua_elf_systems = [system_mipsel_linux,system_mipseb_linux,system_mipsel_android,system_mipsel_embedded,system_mipseb_embedded];
  126. ait_ua_elf_const2str : array[aitconst_16bit_unaligned..aitconst_64bit_unaligned]
  127. of string[20]=(
  128. #9'.2byte'#9,#9'.4byte'#9,#9'.8byte'#9
  129. );
  130. {****************************************************************************}
  131. { GNU Assembler writer }
  132. {****************************************************************************}
  133. destructor TGNUAssembler.Destroy;
  134. begin
  135. InstrWriter.free;
  136. inherited destroy;
  137. end;
  138. function TGNUAssembler.MakeCmdLine: TCmdStr;
  139. begin
  140. result := inherited MakeCmdLine;
  141. // MWE: disabled again. It generates dwarf info for the generated .s
  142. // files as well. This conflicts with the info we generate
  143. // if target_dbg.id = dbg_dwarf then
  144. // result := result + ' --gdwarf-2';
  145. end;
  146. function TGNUAssembler.NextSetLabel: string;
  147. begin
  148. inc(setcount);
  149. result := asminfo^.labelprefix+'$set$'+tostr(setcount);
  150. end;
  151. function is_smart_section(atype:TAsmSectiontype):boolean;
  152. begin
  153. { For bss we need to set some flags that are target dependent,
  154. it is easier to disable it for smartlinking. It doesn't take up
  155. filespace }
  156. result:=not(target_info.system in systems_darwin) and
  157. create_smartlink_sections and
  158. (atype<>sec_toc) and
  159. (atype<>sec_user) and
  160. { on embedded systems every byte counts, so smartlink bss too }
  161. ((atype<>sec_bss) or (target_info.system in systems_embedded));
  162. end;
  163. function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  164. const
  165. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  166. '.text',
  167. '.data',
  168. { why doesn't .rodata work? (FK) }
  169. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  170. { vtables (and anything else containing relocations), otherwise those are }
  171. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  172. { vtable for a class called Window: }
  173. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  174. { TODO: .data.ro not yet working}
  175. {$if defined(arm) or defined(riscv64) or defined(powerpc)}
  176. '.rodata',
  177. {$else arm}
  178. '.data',
  179. {$endif arm}
  180. '.rodata',
  181. '.bss',
  182. '.threadvar',
  183. '.pdata',
  184. '', { stubs }
  185. '__DATA,__nl_symbol_ptr',
  186. '__DATA,__la_symbol_ptr',
  187. '__DATA,__mod_init_func',
  188. '__DATA,__mod_term_func',
  189. '.stab',
  190. '.stabstr',
  191. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  192. '.eh_frame',
  193. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  194. '.fpc',
  195. '.toc',
  196. '.init',
  197. '.fini',
  198. '.objc_class',
  199. '.objc_meta_class',
  200. '.objc_cat_cls_meth',
  201. '.objc_cat_inst_meth',
  202. '.objc_protocol',
  203. '.objc_string_object',
  204. '.objc_cls_meth',
  205. '.objc_inst_meth',
  206. '.objc_cls_refs',
  207. '.objc_message_refs',
  208. '.objc_symbols',
  209. '.objc_category',
  210. '.objc_class_vars',
  211. '.objc_instance_vars',
  212. '.objc_module_info',
  213. '.objc_class_names',
  214. '.objc_meth_var_types',
  215. '.objc_meth_var_names',
  216. '.objc_selector_strs',
  217. '.objc_protocol_ext',
  218. '.objc_class_ext',
  219. '.objc_property',
  220. '.objc_image_info',
  221. '.objc_cstring_object',
  222. '.objc_sel_fixup',
  223. '__DATA,__objc_data',
  224. '__DATA,__objc_const',
  225. '.objc_superrefs',
  226. '__DATA, __datacoal_nt,coalesced',
  227. '.objc_classlist',
  228. '.objc_nlclasslist',
  229. '.objc_catlist',
  230. '.obcj_nlcatlist',
  231. '.objc_protolist',
  232. '.stack',
  233. '.heap',
  234. '.gcc_except_table'
  235. );
  236. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  237. '.text',
  238. '.data.rel',
  239. '.data.rel',
  240. '.data.rel',
  241. '.bss',
  242. '.threadvar',
  243. '.pdata',
  244. '', { stubs }
  245. '__DATA,__nl_symbol_ptr',
  246. '__DATA,__la_symbol_ptr',
  247. '__DATA,__mod_init_func',
  248. '__DATA,__mod_term_func',
  249. '.stab',
  250. '.stabstr',
  251. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  252. '.eh_frame',
  253. '.debug_frame','.debug_info','.debug_line','.debug_abbrev','.debug_aranges','.debug_ranges',
  254. '.fpc',
  255. '.toc',
  256. '.init',
  257. '.fini',
  258. '.objc_class',
  259. '.objc_meta_class',
  260. '.objc_cat_cls_meth',
  261. '.objc_cat_inst_meth',
  262. '.objc_protocol',
  263. '.objc_string_object',
  264. '.objc_cls_meth',
  265. '.objc_inst_meth',
  266. '.objc_cls_refs',
  267. '.objc_message_refs',
  268. '.objc_symbols',
  269. '.objc_category',
  270. '.objc_class_vars',
  271. '.objc_instance_vars',
  272. '.objc_module_info',
  273. '.objc_class_names',
  274. '.objc_meth_var_types',
  275. '.objc_meth_var_names',
  276. '.objc_selector_strs',
  277. '.objc_protocol_ext',
  278. '.objc_class_ext',
  279. '.objc_property',
  280. '.objc_image_info',
  281. '.objc_cstring_object',
  282. '.objc_sel_fixup',
  283. '__DATA, __objc_data',
  284. '__DATA, __objc_const',
  285. '.objc_superrefs',
  286. '__DATA, __datacoal_nt,coalesced',
  287. '.objc_classlist',
  288. '.objc_nlclasslist',
  289. '.objc_catlist',
  290. '.obcj_nlcatlist',
  291. '.objc_protolist',
  292. '.stack',
  293. '.heap',
  294. '.gcc_except_table'
  295. );
  296. var
  297. sep : string[3];
  298. secname : string;
  299. begin
  300. if (cs_create_pic in current_settings.moduleswitches) and
  301. not(target_info.system in systems_darwin) then
  302. secname:=secnames_pic[atype]
  303. else
  304. secname:=secnames[atype];
  305. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  306. begin
  307. result:=secname+'.'+aname;
  308. exit;
  309. end;
  310. if atype=sec_threadvar then
  311. begin
  312. if (target_info.system in (systems_windows+systems_wince)) then
  313. secname:='.tls'
  314. else if (target_info.system in systems_linux) then
  315. secname:='.tbss';
  316. end;
  317. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  318. Thus, data which normally goes into .rodata and .rodata_norel sections must
  319. end up in .data section }
  320. if (atype in [sec_rodata,sec_rodata_norel]) and
  321. (target_info.system in [system_i386_go32v2,system_m68k_palmos]) then
  322. secname:='.data';
  323. { Windows correctly handles reallocations in readonly sections }
  324. if (atype=sec_rodata) and
  325. (target_info.system in systems_all_windows+systems_nativent-[system_i8086_win16]) then
  326. secname:='.rodata';
  327. { Use .rodata and .data.rel.ro for Android with PIC }
  328. if (target_info.system in systems_android) and (cs_create_pic in current_settings.moduleswitches) then
  329. begin
  330. case atype of
  331. sec_rodata:
  332. secname:='.data.rel.ro';
  333. sec_rodata_norel:
  334. secname:='.rodata';
  335. end;
  336. end;
  337. { section type user gives the user full controll on the section name }
  338. if atype=sec_user then
  339. secname:=aname;
  340. if is_smart_section(atype) and (aname<>'') then
  341. begin
  342. case aorder of
  343. secorder_begin :
  344. sep:='.b_';
  345. secorder_end :
  346. sep:='.z_';
  347. else
  348. sep:='.n_';
  349. end;
  350. result:=secname+sep+aname
  351. end
  352. else
  353. result:=secname;
  354. end;
  355. function TGNUAssembler.sectionattrs(atype:TAsmSectiontype):string;
  356. begin
  357. result:='';
  358. if (target_info.system in [system_i386_win32,system_x86_64_win64]) then
  359. begin
  360. result:=sectionattrs_coff(atype);
  361. end;
  362. end;
  363. function TGNUAssembler.sectionattrs_coff(atype:TAsmSectiontype):string;
  364. begin
  365. case atype of
  366. sec_code, sec_init, sec_fini, sec_stub:
  367. result:='x';
  368. { TODO: must be individual for each section }
  369. sec_user:
  370. result:='d';
  371. sec_data, sec_data_lazy, sec_data_nonlazy, sec_fpc,
  372. sec_idata2, sec_idata4, sec_idata5, sec_idata6, sec_idata7:
  373. result:='d';
  374. { TODO: these need a fix to become read-only }
  375. sec_rodata, sec_rodata_norel:
  376. result:='d';
  377. sec_bss:
  378. result:='b';
  379. { TODO: Somewhat questionable. FPC does not allow initialized threadvars,
  380. so no sense to mark it as containing data. But Windows allows it to
  381. contain data, and Linux even has .tdata and .tbss }
  382. sec_threadvar:
  383. result:='b';
  384. sec_pdata, sec_edata, sec_eh_frame, sec_toc:
  385. result:='r';
  386. sec_stab,sec_stabstr,
  387. sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev,sec_debug_aranges,sec_debug_ranges:
  388. result:='n';
  389. else
  390. result:=''; { defaults to data+load }
  391. end;
  392. end;
  393. function TGNUAssembler.sectionalignment_aix(atype:TAsmSectiontype;secalign: longint): string;
  394. var
  395. l: longint;
  396. begin
  397. if (secalign=0) or
  398. not(atype in [sec_code,sec_bss,sec_rodata_norel,sec_rodata,sec_data]) then
  399. begin
  400. result:='';
  401. exit;
  402. end;
  403. if not ispowerof2(secalign,l) then
  404. internalerror(2012022201);
  405. result:=tostr(l);
  406. end;
  407. procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder;secalign:longint;secflags:TSectionFlags=SF_None;secprogbits:TSectionProgbits=SPB_None);
  408. var
  409. s : string;
  410. begin
  411. writer.AsmLn;
  412. case target_info.system of
  413. system_i386_OS2,
  414. system_i386_EMX: ;
  415. system_m68k_atari, { atari tos/mint GNU AS also doesn't seem to like .section (KB) }
  416. system_m68k_amiga: { amiga has old GNU AS (2.14), which blews up from .section (KB) }
  417. begin
  418. { ... but vasm is GAS compatible on amiga/atari, and supports named sections }
  419. if create_smartlink_sections then
  420. writer.AsmWrite('.section ');
  421. end;
  422. system_powerpc_darwin,
  423. system_i386_darwin,
  424. system_i386_iphonesim,
  425. system_powerpc64_darwin,
  426. system_x86_64_darwin,
  427. system_arm_darwin,
  428. system_aarch64_darwin,
  429. system_x86_64_iphonesim,
  430. system_powerpc_aix,
  431. system_powerpc64_aix:
  432. begin
  433. if (atype in [sec_stub]) then
  434. writer.AsmWrite('.section ');
  435. end
  436. else
  437. writer.AsmWrite('.section ');
  438. end;
  439. s:=sectionname(atype,aname,aorder);
  440. writer.AsmWrite(s);
  441. { flags explicitly defined? }
  442. if (secflags<>SF_None) or (secprogbits<>SPB_None) then
  443. begin
  444. case secflags of
  445. SF_A:
  446. writer.AsmWrite(',"a"');
  447. SF_W:
  448. writer.AsmWrite(',"w"');
  449. SF_X:
  450. writer.AsmWrite(',"x"');
  451. SF_None:
  452. writer.AsmWrite(',""');
  453. else
  454. Internalerror(2018101502);
  455. end;
  456. case secprogbits of
  457. SPB_PROGBITS:
  458. writer.AsmWrite(',%progbits');
  459. SPB_NOBITS:
  460. writer.AsmWrite(',%nobits');
  461. SPB_None:
  462. ;
  463. else
  464. Internalerror(2018101503);
  465. end;
  466. end
  467. else
  468. case atype of
  469. sec_fpc :
  470. if aname = 'resptrs' then
  471. writer.AsmWrite(', "a", @progbits');
  472. sec_stub :
  473. begin
  474. case target_info.system of
  475. { there are processor-independent shortcuts available }
  476. { for this, namely .symbol_stub and .picsymbol_stub, but }
  477. { they don't work and gcc doesn't use them either... }
  478. system_powerpc_darwin,
  479. system_powerpc64_darwin:
  480. if (cs_create_pic in current_settings.moduleswitches) then
  481. writer.AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  482. else
  483. writer.AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  484. system_i386_darwin,
  485. system_i386_iphonesim:
  486. writer.AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  487. system_arm_darwin:
  488. if (cs_create_pic in current_settings.moduleswitches) then
  489. writer.AsmWriteln('__TEXT,__picsymbolstub4,symbol_stubs,none,16')
  490. else
  491. writer.AsmWriteln('__TEXT,__symbol_stub4,symbol_stubs,none,12')
  492. { darwin/(x86-64/AArch64) uses PC-based GOT addressing, no
  493. explicit symbol stubs }
  494. else
  495. internalerror(2006031101);
  496. end;
  497. end;
  498. else
  499. { GNU AS won't recognize '.text.n_something' section name as belonging
  500. to '.text' and assigns default attributes to it, which is not
  501. always correct. We have to fix it.
  502. TODO: This likely applies to all systems which smartlink without
  503. creating libraries }
  504. begin
  505. if is_smart_section(atype) and (aname<>'') then
  506. begin
  507. s:=sectionattrs(atype);
  508. if (s<>'') then
  509. writer.AsmWrite(',"'+s+'"');
  510. end;
  511. if target_info.system in systems_aix then
  512. begin
  513. s:=sectionalignment_aix(atype,secalign);
  514. if s<>'' then
  515. writer.AsmWrite(','+s);
  516. end;
  517. end;
  518. end;
  519. writer.AsmLn;
  520. LastSecType:=atype;
  521. end;
  522. procedure TGNUAssembler.WriteDecodedUleb128(a: qword);
  523. var
  524. i,len : longint;
  525. buf : array[0..63] of byte;
  526. begin
  527. len:=EncodeUleb128(a,buf);
  528. for i:=0 to len-1 do
  529. begin
  530. if (i > 0) then
  531. writer.AsmWrite(',');
  532. writer.AsmWrite(tostr(buf[i]));
  533. end;
  534. end;
  535. procedure TGNUAssembler.WriteCFI(hp: tai_cfi_base);
  536. begin
  537. writer.AsmWrite(cfi2str[hp.cfityp]);
  538. case hp.cfityp of
  539. cfi_startproc,
  540. cfi_endproc:
  541. ;
  542. cfi_undefined,
  543. cfi_restore,
  544. cfi_def_cfa_register:
  545. begin
  546. writer.AsmWrite(' ');
  547. writer.AsmWrite(gas_regname(tai_cfi_op_reg(hp).reg1));
  548. end;
  549. cfi_def_cfa_offset:
  550. begin
  551. writer.AsmWrite(' ');
  552. writer.AsmWrite(tostr(tai_cfi_op_val(hp).val1));
  553. end;
  554. cfi_offset:
  555. begin
  556. writer.AsmWrite(' ');
  557. writer.AsmWrite(gas_regname(tai_cfi_op_reg_val(hp).reg1));
  558. writer.AsmWrite(',');
  559. writer.AsmWrite(tostr(tai_cfi_op_reg_val(hp).val));
  560. end;
  561. else
  562. internalerror(2019030203);
  563. end;
  564. writer.AsmLn;
  565. end;
  566. procedure TGNUAssembler.WriteDecodedSleb128(a: int64);
  567. var
  568. i,len : longint;
  569. buf : array[0..255] of byte;
  570. begin
  571. len:=EncodeSleb128(a,buf);
  572. for i:=0 to len-1 do
  573. begin
  574. if (i > 0) then
  575. writer.AsmWrite(',');
  576. writer.AsmWrite(tostr(buf[i]));
  577. end;
  578. end;
  579. procedure TGNUAssembler.WriteTree(p:TAsmList);
  580. function needsObject(hp : tai_symbol) : boolean;
  581. begin
  582. needsObject :=
  583. (
  584. assigned(hp.next) and
  585. (tai(hp.next).typ in [ait_const,ait_datablock,ait_realconst])
  586. ) or
  587. (hp.sym.typ in [AT_DATA,AT_METADATA]);
  588. end;
  589. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; maxbytes: byte; out last_align: longint;lasthp:tai);
  590. var
  591. i: longint;
  592. alignment64 : int64;
  593. {$ifdef m68k}
  594. instr : string;
  595. {$endif}
  596. begin
  597. last_align:=alignment;
  598. if alignment>1 then
  599. begin
  600. if not(target_info.system in (systems_darwin+systems_aix)) then
  601. begin
  602. {$ifdef m68k}
  603. if not use_op and (lastsectype=sec_code) then
  604. begin
  605. if not ispowerof2(alignment,i) then
  606. internalerror(2014022201);
  607. { the Coldfire manual suggests the TBF instruction for
  608. alignments, but somehow QEMU does not interpret that
  609. correctly... }
  610. {if current_settings.cputype in cpu_coldfire then
  611. instr:='0x51fc'
  612. else}
  613. instr:='0x4e71';
  614. writer.AsmWrite(#9'.balignw '+tostr(alignment)+','+instr);
  615. end
  616. else
  617. begin
  618. {$endif m68k}
  619. alignment64:=alignment;
  620. if (maxbytes<>alignment) and ispowerof2(alignment64,i) then
  621. begin
  622. if use_op then
  623. begin
  624. writer.AsmWrite(#9'.p2align '+tostr(i)+','+tostr(fillop)+','+tostr(maxbytes));
  625. writer.AsmLn;
  626. writer.AsmWrite(#9'.p2align '+tostr(i-1)+','+tostr(fillop));
  627. end
  628. else
  629. begin
  630. writer.AsmWrite(#9'.p2align '+tostr(i)+',,'+tostr(maxbytes));
  631. writer.AsmLn;
  632. writer.AsmWrite(#9'.p2align '+tostr(i-1));
  633. end
  634. end
  635. else
  636. begin
  637. writer.AsmWrite(#9'.balign '+tostr(alignment));
  638. if use_op then
  639. writer.AsmWrite(','+tostr(fillop))
  640. {$ifdef x86}
  641. { force NOP as alignment op code }
  642. else if (LastSecType=sec_code) and (asminfo^.id<>as_solaris_as) then
  643. writer.AsmWrite(',0x90');
  644. {$endif x86}
  645. end;
  646. {$ifdef m68k}
  647. end;
  648. {$endif m68k}
  649. end
  650. else
  651. begin
  652. { darwin and aix as only support .align }
  653. if not ispowerof2(alignment,i) then
  654. internalerror(2003010305);
  655. writer.AsmWrite(#9'.align '+tostr(i));
  656. last_align:=i;
  657. end;
  658. writer.AsmLn;
  659. end;
  660. end;
  661. var
  662. ch : char;
  663. lasthp,
  664. hp : tai;
  665. constdef : taiconst_type;
  666. s,t : string;
  667. i,pos,l : longint;
  668. InlineLevel : cardinal;
  669. last_align : longint;
  670. do_line : boolean;
  671. sepChar : char;
  672. replaceforbidden: boolean;
  673. begin
  674. if not assigned(p) then
  675. exit;
  676. replaceforbidden:=asminfo^.dollarsign<>'$';
  677. last_align := 2;
  678. InlineLevel:=0;
  679. { lineinfo is only needed for al_procedures (PFV) }
  680. do_line:=(cs_asm_source in current_settings.globalswitches) or
  681. ((cs_lineinfo in current_settings.moduleswitches)
  682. and (p=current_asmdata.asmlists[al_procedures]));
  683. lasthp:=nil;
  684. hp:=tai(p.first);
  685. while assigned(hp) do
  686. begin
  687. prefetch(pointer(hp.next)^);
  688. if not(hp.typ in SkipLineInfo) then
  689. begin
  690. current_filepos:=tailineinfo(hp).fileinfo;
  691. { no line info for inlined code }
  692. if do_line and (inlinelevel=0) then
  693. WriteSourceLine(hp as tailineinfo);
  694. end;
  695. case hp.typ of
  696. ait_comment :
  697. Begin
  698. writer.AsmWrite(asminfo^.comment);
  699. writer.AsmWritePChar(tai_comment(hp).str);
  700. writer.AsmLn;
  701. End;
  702. ait_regalloc :
  703. begin
  704. if (cs_asm_regalloc in current_settings.globalswitches) then
  705. begin
  706. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  707. repeat
  708. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  709. if (hp.next=nil) or
  710. (tai(hp.next).typ<>ait_regalloc) or
  711. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  712. break;
  713. hp:=tai(hp.next);
  714. writer.AsmWrite(',');
  715. until false;
  716. writer.AsmWrite(' ');
  717. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  718. end;
  719. end;
  720. ait_tempalloc :
  721. begin
  722. if (cs_asm_tempalloc in current_settings.globalswitches) then
  723. WriteTempalloc(tai_tempalloc(hp));
  724. end;
  725. ait_align :
  726. begin
  727. doalign(tai_align_abstract(hp).aligntype,tai_align_abstract(hp).use_op,tai_align_abstract(hp).fillop,tai_align_abstract(hp).maxbytes,last_align,lasthp);
  728. end;
  729. ait_section :
  730. begin
  731. if tai_section(hp).sectype<>sec_none then
  732. if replaceforbidden then
  733. WriteSection(tai_section(hp).sectype,ReplaceForbiddenAsmSymbolChars(tai_section(hp).name^),tai_section(hp).secorder,
  734. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  735. else
  736. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder,
  737. tai_section(hp).secalign,tai_section(hp).secflags,tai_section(hp).secprogbits)
  738. else
  739. begin
  740. {$ifdef EXTDEBUG}
  741. writer.AsmWrite(asminfo^.comment);
  742. writer.AsmWriteln(' sec_none');
  743. {$endif EXTDEBUG}
  744. end;
  745. end;
  746. ait_datablock :
  747. begin
  748. if (target_info.system in systems_darwin) then
  749. begin
  750. { On Mac OS X you can't have common symbols in a shared library
  751. since those are in the TEXT section and the text section is
  752. read-only in shared libraries (so it can be shared among different
  753. processes). The alternate code creates some kind of common symbols
  754. in the data segment.
  755. }
  756. if tai_datablock(hp).is_global then
  757. begin
  758. writer.AsmWrite('.globl ');
  759. writer.AsmWriteln(tai_datablock(hp).sym.name);
  760. writer.AsmWriteln('.data');
  761. writer.AsmWrite('.zerofill __DATA, __common, ');
  762. writer.AsmWrite(tai_datablock(hp).sym.name);
  763. writer.AsmWriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  764. if not(LastSecType in [sec_data,sec_none]) then
  765. writesection(LastSecType,'',secorder_default,1 shl last_align);
  766. end
  767. else
  768. begin
  769. writer.AsmWrite(#9'.lcomm'#9);
  770. writer.AsmWrite(tai_datablock(hp).sym.name);
  771. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  772. writer.AsmWrite(','+tostr(last_align));
  773. writer.AsmLn;
  774. end;
  775. end
  776. else if target_info.system in systems_aix then
  777. begin
  778. if tai_datablock(hp).is_global then
  779. begin
  780. writer.AsmWrite(#9'.globl ');
  781. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  782. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  783. writer.AsmWriteln(':');
  784. writer.AsmWrite(#9'.space ');
  785. writer.AsmWriteln(tostr(tai_datablock(hp).size));
  786. if not(LastSecType in [sec_data,sec_none]) then
  787. writesection(LastSecType,'',secorder_default,1 shl last_align);
  788. end
  789. else
  790. begin
  791. writer.AsmWrite(#9'.lcomm ');
  792. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  793. writer.AsmWrite(',');
  794. writer.AsmWrite(tostr(tai_datablock(hp).size)+',');
  795. writer.AsmWrite('_data.bss_,');
  796. writer.AsmWriteln(tostr(last_align));
  797. end;
  798. end
  799. else
  800. begin
  801. {$ifdef USE_COMM_IN_BSS}
  802. if writingpackages then
  803. begin
  804. { The .comm is required for COMMON symbols. These are used
  805. in the shared library loading. All the symbols declared in
  806. the .so file need to resolve to the data allocated in the main
  807. program (PFV) }
  808. if tai_datablock(hp).is_global then
  809. begin
  810. writer.AsmWrite(#9'.comm'#9);
  811. if replaceforbidden then
  812. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name))
  813. else
  814. writer.AsmWrite(tai_datablock(hp).sym.name);
  815. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  816. writer.AsmWrite(','+tostr(last_align));
  817. writer.AsmLn;
  818. end
  819. else
  820. begin
  821. writer.AsmWrite(#9'.lcomm'#9);
  822. if replaceforbidden then
  823. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_datablock(hp).sym.name));
  824. else
  825. writer.AsmWrite(tai_datablock(hp).sym.name);
  826. writer.AsmWrite(','+tostr(tai_datablock(hp).size));
  827. writer.AsmWrite(','+tostr(last_align));
  828. writer.AsmLn;
  829. end
  830. end
  831. else
  832. {$endif USE_COMM_IN_BSS}
  833. begin
  834. if Tai_datablock(hp).is_global then
  835. begin
  836. writer.AsmWrite(#9'.globl ');
  837. if replaceforbidden then
  838. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name))
  839. else
  840. writer.AsmWriteln(Tai_datablock(hp).sym.name);
  841. end;
  842. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  843. sepChar := '@'
  844. else
  845. sepChar := '%';
  846. if replaceforbidden then
  847. begin
  848. if (tf_needs_symbol_type in target_info.flags) then
  849. writer.AsmWriteln(#9'.type '+ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name)+','+sepChar+'object');
  850. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  851. writer.AsmWriteln(#9'.size '+ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name)+','+tostr(Tai_datablock(hp).size));
  852. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(Tai_datablock(hp).sym.name))
  853. end
  854. else
  855. begin
  856. if (tf_needs_symbol_type in target_info.flags) then
  857. writer.AsmWriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  858. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  859. writer.AsmWriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  860. writer.AsmWrite(Tai_datablock(hp).sym.name);
  861. end;
  862. writer.AsmWriteln(':');
  863. writer.AsmWriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  864. end;
  865. end;
  866. end;
  867. ait_const:
  868. begin
  869. constdef:=tai_const(hp).consttype;
  870. case constdef of
  871. {$ifndef cpu64bitaddr}
  872. aitconst_128bit :
  873. begin
  874. internalerror(200404291);
  875. end;
  876. aitconst_64bit :
  877. begin
  878. if assigned(tai_const(hp).sym) then
  879. internalerror(200404292);
  880. if not(target_info.system in systems_aix) then
  881. begin
  882. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  883. if target_info.endian = endian_little then
  884. begin
  885. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  886. writer.AsmWrite(',');
  887. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  888. end
  889. else
  890. begin
  891. writer.AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  892. writer.AsmWrite(',');
  893. writer.AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  894. end;
  895. end
  896. else
  897. WriteAixIntConst(tai_const(hp));
  898. writer.AsmLn;
  899. end;
  900. aitconst_gottpoff:
  901. begin
  902. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(gottpoff)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  903. writer.Asmln;
  904. end;
  905. {$endif cpu64bitaddr}
  906. aitconst_got:
  907. begin
  908. if tai_const(hp).symofs<>0 then
  909. InternalError(2015091401); // No symbol offset is allowed for GOT.
  910. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(GOT)');
  911. writer.AsmLn;
  912. end;
  913. aitconst_gotoff_symbol:
  914. begin
  915. if (tai_const(hp).sym=nil) then
  916. InternalError(2014022601);
  917. case target_info.cpu of
  918. cpu_mipseb,cpu_mipsel:
  919. begin
  920. writer.AsmWrite(#9'.gpword'#9);
  921. writer.AsmWrite(tai_const(hp).sym.name);
  922. end;
  923. cpu_i386:
  924. begin
  925. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  926. writer.AsmWrite(tai_const(hp).sym.name+'-_GLOBAL_OFFSET_TABLE_');
  927. end;
  928. else
  929. InternalError(2014022602);
  930. end;
  931. if (tai_const(hp).value<>0) then
  932. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  933. writer.AsmLn;
  934. end;
  935. aitconst_uleb128bit,
  936. aitconst_sleb128bit,
  937. {$ifdef cpu64bitaddr}
  938. aitconst_128bit,
  939. aitconst_64bit,
  940. {$endif cpu64bitaddr}
  941. aitconst_32bit,
  942. aitconst_16bit,
  943. aitconst_8bit,
  944. aitconst_rva_symbol,
  945. aitconst_secrel32_symbol,
  946. aitconst_darwin_dwarf_delta32,
  947. aitconst_darwin_dwarf_delta64,
  948. aitconst_half16bit,
  949. aitconst_gs,
  950. aitconst_16bit_unaligned,
  951. aitconst_32bit_unaligned,
  952. aitconst_64bit_unaligned:
  953. begin
  954. { the AIX assembler (and for compatibility, the GNU
  955. assembler when targeting AIX) automatically aligns
  956. .short/.long/.llong to a multiple of 2/4/8 bytes. We
  957. don't want that, since this may be data inside a packed
  958. record -> use .vbyte instead (byte stream of fixed
  959. length) }
  960. if (target_info.system in systems_aix) and
  961. (constdef in [aitconst_128bit,aitconst_64bit,aitconst_32bit,aitconst_16bit]) and
  962. not assigned(tai_const(hp).sym) then
  963. begin
  964. WriteAixIntConst(tai_const(hp));
  965. end
  966. else if (target_info.system in systems_darwin) and
  967. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  968. begin
  969. writer.AsmWrite(ait_const2str[aitconst_8bit]);
  970. case tai_const(hp).consttype of
  971. aitconst_uleb128bit:
  972. WriteDecodedUleb128(qword(tai_const(hp).value));
  973. aitconst_sleb128bit:
  974. WriteDecodedSleb128(int64(tai_const(hp).value));
  975. end
  976. end
  977. else
  978. begin
  979. if (constdef in ait_unaligned_consts) and
  980. (target_info.system in use_ua_sparc_systems) then
  981. writer.AsmWrite(ait_ua_sparc_const2str[constdef])
  982. else if (constdef in ait_unaligned_consts) and
  983. (target_info.system in use_ua_elf_systems) then
  984. writer.AsmWrite(ait_ua_elf_const2str[constdef])
  985. { we can also have unaligned pointers in packed record
  986. constants, which don't get translated into
  987. unaligned tai -> always use vbyte }
  988. else if target_info.system in systems_aix then
  989. writer.AsmWrite(#9'.vbyte'#9+tostr(tai_const(hp).size)+',')
  990. else if (asminfo^.id=as_solaris_as) then
  991. writer.AsmWrite(ait_solaris_const2str[constdef])
  992. else
  993. writer.AsmWrite(ait_const2str[constdef]);
  994. l:=0;
  995. t := '';
  996. repeat
  997. if assigned(tai_const(hp).sym) then
  998. begin
  999. if assigned(tai_const(hp).endsym) then
  1000. begin
  1001. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  1002. begin
  1003. s := NextSetLabel;
  1004. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  1005. end
  1006. else
  1007. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  1008. end
  1009. else
  1010. s:=tai_const(hp).sym.name;
  1011. if replaceforbidden then
  1012. s:=ReplaceForbiddenAsmSymbolChars(s);
  1013. if tai_const(hp).value<>0 then
  1014. s:=s+tostr_with_plus(tai_const(hp).value);
  1015. end
  1016. else
  1017. {$ifdef cpu64bitaddr}
  1018. s:=tostr(tai_const(hp).value);
  1019. {$else cpu64bitaddr}
  1020. { 64 bit constants are already handled above in this case }
  1021. s:=tostr(longint(tai_const(hp).value));
  1022. {$endif cpu64bitaddr}
  1023. if constdef = aitconst_half16bit then
  1024. s:='('+s+')/2';
  1025. if constdef = aitconst_gs then
  1026. s:='gs('+s+')';
  1027. writer.AsmWrite(s);
  1028. inc(l,length(s));
  1029. { Values with symbols are written on a single line to improve
  1030. reading of the .s file (PFV) }
  1031. if assigned(tai_const(hp).sym) or
  1032. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  1033. (l>line_length) or
  1034. (hp.next=nil) or
  1035. (tai(hp.next).typ<>ait_const) or
  1036. (tai_const(hp.next).consttype<>constdef) or
  1037. assigned(tai_const(hp.next).sym) then
  1038. break;
  1039. hp:=tai(hp.next);
  1040. writer.AsmWrite(',');
  1041. until false;
  1042. if (t <> '') then
  1043. begin
  1044. writer.AsmLn;
  1045. writer.AsmWrite(t);
  1046. end;
  1047. end;
  1048. writer.AsmLn;
  1049. end;
  1050. else
  1051. internalerror(200704251);
  1052. end;
  1053. end;
  1054. ait_realconst :
  1055. begin
  1056. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  1057. end;
  1058. ait_string :
  1059. begin
  1060. pos:=0;
  1061. if not(target_info.system in systems_aix) then
  1062. begin
  1063. for i:=1 to tai_string(hp).len do
  1064. begin
  1065. if pos=0 then
  1066. begin
  1067. writer.AsmWrite(#9'.ascii'#9'"');
  1068. pos:=20;
  1069. end;
  1070. ch:=tai_string(hp).str[i-1];
  1071. case ch of
  1072. #0, {This can't be done by range, because a bug in FPC}
  1073. #1..#31,
  1074. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  1075. '"' : s:='\"';
  1076. '\' : s:='\\';
  1077. else
  1078. s:=ch;
  1079. end;
  1080. writer.AsmWrite(s);
  1081. inc(pos,length(s));
  1082. if (pos>line_length) or (i=tai_string(hp).len) then
  1083. begin
  1084. writer.AsmWriteLn('"');
  1085. pos:=0;
  1086. end;
  1087. end;
  1088. end
  1089. else
  1090. WriteAixStringConst(tai_string(hp));
  1091. end;
  1092. ait_label :
  1093. begin
  1094. if (tai_label(hp).labsym.is_used) then
  1095. begin
  1096. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1097. begin
  1098. writer.AsmWrite(#9'.private_extern ');
  1099. writer.AsmWriteln(tai_label(hp).labsym.name);
  1100. end;
  1101. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  1102. begin
  1103. {$ifdef arm}
  1104. { do no change arm mode accidently, .globl seems to reset the mode }
  1105. if GenerateThumbCode or GenerateThumb2Code then
  1106. writer.AsmWriteln(#9'.thumb_func'#9);
  1107. {$endif arm}
  1108. writer.AsmWrite('.globl'#9);
  1109. if replaceforbidden then
  1110. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1111. else
  1112. writer.AsmWriteLn(tai_label(hp).labsym.name);
  1113. end;
  1114. if replaceforbidden then
  1115. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1116. else
  1117. writer.AsmWrite(tai_label(hp).labsym.name);
  1118. writer.AsmWriteLn(':');
  1119. end;
  1120. end;
  1121. ait_symbol :
  1122. begin
  1123. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  1124. begin
  1125. writer.AsmWrite(#9'.private_extern ');
  1126. if replaceforbidden then
  1127. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1128. else
  1129. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1130. end;
  1131. if (target_info.system=system_powerpc64_linux) and
  1132. (tai_symbol(hp).sym.typ=AT_FUNCTION) and
  1133. (cs_profile in current_settings.moduleswitches) then
  1134. writer.AsmWriteLn('.globl _mcount');
  1135. if tai_symbol(hp).is_global then
  1136. begin
  1137. writer.AsmWrite('.globl'#9);
  1138. if replaceforbidden then
  1139. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1140. else
  1141. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1142. end;
  1143. if (target_info.system=system_powerpc64_linux) and
  1144. use_dotted_functions and
  1145. (tai_symbol(hp).sym.typ=AT_FUNCTION) then
  1146. begin
  1147. writer.AsmWriteLn('.section ".opd", "aw"');
  1148. writer.AsmWriteLn('.align 3');
  1149. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  1150. writer.AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  1151. writer.AsmWriteLn('.previous');
  1152. writer.AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1153. if (tai_symbol(hp).is_global) then
  1154. writer.AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1155. writer.AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1156. { the dotted name is the name of the actual function entry }
  1157. writer.AsmWrite('.');
  1158. end
  1159. else if (target_info.system in systems_aix) and
  1160. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  1161. begin
  1162. if target_info.system=system_powerpc_aix then
  1163. begin
  1164. s:=#9'.long .';
  1165. ch:='2';
  1166. end
  1167. else
  1168. begin
  1169. s:=#9'.llong .';
  1170. ch:='3';
  1171. end;
  1172. writer.AsmWriteLn(#9'.csect '+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+'[DS],'+ch);
  1173. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+':');
  1174. writer.AsmWriteln(s+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+', TOC[tc0], 0');
  1175. writer.AsmWriteln(#9'.csect .text[PR]');
  1176. if (tai_symbol(hp).is_global) then
  1177. writer.AsmWriteLn('.globl .'+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1178. else
  1179. writer.AsmWriteLn('.lglobl .'+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name));
  1180. { the dotted name is the name of the actual function entry }
  1181. writer.AsmWrite('.');
  1182. end
  1183. else
  1184. begin
  1185. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  1186. sepChar := '@'
  1187. else
  1188. sepChar := '#';
  1189. if (tf_needs_symbol_type in target_info.flags) then
  1190. begin
  1191. writer.AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1192. if (needsObject(tai_symbol(hp))) then
  1193. writer.AsmWriteLn(',' + sepChar + 'object')
  1194. else
  1195. writer.AsmWriteLn(',' + sepChar + 'function');
  1196. end;
  1197. end;
  1198. if replaceforbidden then
  1199. if not(tai_symbol(hp).has_value) then
  1200. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name + ':'))
  1201. else
  1202. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)))
  1203. else if not(tai_symbol(hp).has_value) then
  1204. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1205. else
  1206. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1207. end;
  1208. ait_symbolpair:
  1209. begin
  1210. writer.AsmWrite(#9);
  1211. writer.AsmWrite(symbolpairkindstr[tai_symbolpair(hp).kind]);
  1212. writer.AsmWrite(' ');
  1213. if tai_symbolpair(hp).kind<>spk_localentry then
  1214. s:=', '
  1215. else
  1216. { the .localentry directive has to specify the size from the
  1217. start till here of the non-local entry code as second argument }
  1218. s:=', .-';
  1219. if replaceforbidden then
  1220. begin
  1221. { avoid string truncation }
  1222. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).sym^)+s);
  1223. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).value^));
  1224. end
  1225. else
  1226. begin
  1227. { avoid string truncation }
  1228. writer.AsmWrite(tai_symbolpair(hp).sym^+s);
  1229. writer.AsmWriteLn(tai_symbolpair(hp).value^);
  1230. end;
  1231. end;
  1232. ait_symbol_end :
  1233. begin
  1234. if tf_needs_symbol_size in target_info.flags then
  1235. begin
  1236. s:=asminfo^.labelprefix+'e'+tostr(symendcount);
  1237. inc(symendcount);
  1238. writer.AsmWriteLn(s+':');
  1239. writer.AsmWrite(#9'.size'#9);
  1240. if (target_info.system=system_powerpc64_linux) and
  1241. use_dotted_functions and
  1242. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1243. writer.AsmWrite('.');
  1244. if replaceforbidden then
  1245. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_symbol_end(hp).sym.name))
  1246. else
  1247. writer.AsmWrite(tai_symbol_end(hp).sym.name);
  1248. writer.AsmWrite(', '+s+' - ');
  1249. if (target_info.system=system_powerpc64_linux) and
  1250. use_dotted_functions and
  1251. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1252. writer.AsmWrite('.');
  1253. if replaceforbidden then
  1254. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol_end(hp).sym.name))
  1255. else
  1256. writer.AsmWriteLn(tai_symbol_end(hp).sym.name);
  1257. end;
  1258. end;
  1259. ait_instruction :
  1260. begin
  1261. WriteInstruction(hp);
  1262. end;
  1263. ait_stab :
  1264. begin
  1265. if assigned(tai_stab(hp).str) then
  1266. begin
  1267. writer.AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1268. writer.AsmWritePChar(tai_stab(hp).str);
  1269. writer.AsmLn;
  1270. end;
  1271. end;
  1272. ait_force_line,
  1273. ait_function_name :
  1274. begin
  1275. {$ifdef DEBUG_AGGAS}
  1276. WriteStr(s,hp.typ);
  1277. writer.AsmWriteLn('# '+s);
  1278. {$endif DEBUG_AGGAS}
  1279. end;
  1280. ait_cutobject :
  1281. begin
  1282. {$ifdef DEBUG_AGGAS}
  1283. writer.AsmWriteLn('# ait_cutobject');
  1284. {$endif DEBUG_AGGAS}
  1285. if SmartAsm then
  1286. begin
  1287. { only reset buffer if nothing has changed }
  1288. if not(writer.ClearIfEmpty) then
  1289. begin
  1290. writer.AsmClose;
  1291. DoAssemble;
  1292. writer.AsmCreate(tai_cutobject(hp).place);
  1293. end;
  1294. { avoid empty files }
  1295. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1296. begin
  1297. if tai(hp.next).typ=ait_section then
  1298. LastSecType:=tai_section(hp.next).sectype;
  1299. hp:=tai(hp.next);
  1300. end;
  1301. if LastSecType<>sec_none then
  1302. WriteSection(LastSecType,'',secorder_default,last_align);
  1303. writer.MarkEmpty;
  1304. end;
  1305. end;
  1306. ait_marker :
  1307. begin
  1308. {$ifdef DEBUG_AGGAS}
  1309. WriteStr(s,tai_marker(hp).Kind);
  1310. writer.AsmWriteLn('# ait_marker, kind: '+s);
  1311. {$endif DEBUG_AGGAS}
  1312. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1313. inc(InlineLevel)
  1314. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1315. dec(InlineLevel);
  1316. end;
  1317. ait_directive :
  1318. begin
  1319. WriteDirectiveName(tai_directive(hp).directive);
  1320. if tai_directive(hp).name <>'' then
  1321. begin
  1322. if replaceforbidden then
  1323. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_directive(hp).name))
  1324. else
  1325. writer.AsmWrite(tai_directive(hp).name);
  1326. end;
  1327. writer.AsmLn;
  1328. end;
  1329. ait_seh_directive :
  1330. begin
  1331. {$ifndef DISABLE_WIN64_SEH}
  1332. writer.AsmWrite(sehdirectivestr[tai_seh_directive(hp).kind]);
  1333. case tai_seh_directive(hp).datatype of
  1334. sd_none:;
  1335. sd_string:
  1336. begin
  1337. writer.AsmWrite(' '+tai_seh_directive(hp).data.name^);
  1338. if (tai_seh_directive(hp).data.flags and 1)<>0 then
  1339. writer.AsmWrite(',@except');
  1340. if (tai_seh_directive(hp).data.flags and 2)<>0 then
  1341. writer.AsmWrite(',@unwind');
  1342. end;
  1343. sd_reg:
  1344. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg));
  1345. sd_offset:
  1346. writer.AsmWrite(' '+tostr(tai_seh_directive(hp).data.offset));
  1347. sd_regoffset:
  1348. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg)+', '+
  1349. tostr(tai_seh_directive(hp).data.offset));
  1350. end;
  1351. writer.AsmLn;
  1352. {$endif DISABLE_WIN64_SEH}
  1353. end;
  1354. ait_varloc:
  1355. begin
  1356. if tai_varloc(hp).newlocationhi<>NR_NO then
  1357. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1358. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  1359. else
  1360. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1361. std_regname(tai_varloc(hp).newlocation)));
  1362. writer.AsmLn;
  1363. end;
  1364. ait_cfi:
  1365. begin
  1366. WriteCFI(tai_cfi_base(hp));
  1367. end;
  1368. else
  1369. internalerror(2006012201);
  1370. end;
  1371. lasthp:=hp;
  1372. hp:=tai(hp.next);
  1373. end;
  1374. end;
  1375. procedure TGNUAssembler.WriteExtraHeader;
  1376. begin
  1377. end;
  1378. procedure TGNUAssembler.WriteExtraFooter;
  1379. begin
  1380. end;
  1381. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1382. begin
  1383. InstrWriter.WriteInstruction(hp);
  1384. end;
  1385. procedure TGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1386. begin
  1387. writer.AsmWriteLn(#9'.weak '+s.name);
  1388. end;
  1389. procedure TGNUAssembler.WriteAixStringConst(hp: tai_string);
  1390. type
  1391. tterminationkind = (term_none,term_string,term_nostring);
  1392. var
  1393. i: longint;
  1394. pos: longint;
  1395. s: string;
  1396. ch: char;
  1397. instring: boolean;
  1398. procedure newstatement(terminationkind: tterminationkind);
  1399. begin
  1400. case terminationkind of
  1401. term_none: ;
  1402. term_string:
  1403. writer.AsmWriteLn('"');
  1404. term_nostring:
  1405. writer.AsmLn;
  1406. end;
  1407. writer.AsmWrite(#9'.byte'#9);
  1408. pos:=20;
  1409. instring:=false;
  1410. end;
  1411. begin
  1412. pos:=0;
  1413. instring:=false;
  1414. for i:=1 to hp.len do
  1415. begin
  1416. if pos=0 then
  1417. newstatement(term_none);
  1418. ch:=hp.str[i-1];
  1419. case ch of
  1420. #0..#31,
  1421. #127..#255 :
  1422. begin
  1423. if instring then
  1424. newstatement(term_string);
  1425. if pos=20 then
  1426. s:=tostr(ord(ch))
  1427. else
  1428. s:=', '+tostr(ord(ch))
  1429. end;
  1430. '"' :
  1431. if instring then
  1432. s:='""'
  1433. else
  1434. begin
  1435. if pos<>20 then
  1436. newstatement(term_nostring);
  1437. s:='"""';
  1438. instring:=true;
  1439. end;
  1440. else
  1441. if not instring then
  1442. begin
  1443. if (pos<>20) then
  1444. newstatement(term_nostring);
  1445. s:='"'+ch;
  1446. instring:=true;
  1447. end
  1448. else
  1449. s:=ch;
  1450. end;
  1451. writer.AsmWrite(s);
  1452. inc(pos,length(s));
  1453. if (pos>line_length) or (i=tai_string(hp).len) then
  1454. begin
  1455. if instring then
  1456. writer.AsmWriteLn('"')
  1457. else
  1458. writer.AsmLn;
  1459. pos:=0;
  1460. end;
  1461. end;
  1462. end;
  1463. procedure TGNUAssembler.WriteAixIntConst(hp: tai_const);
  1464. var
  1465. pos, size: longint;
  1466. begin
  1467. { only big endian AIX supported for now }
  1468. if target_info.endian<>endian_big then
  1469. internalerror(2012010401);
  1470. { limitation: can only write 4 bytes at a time }
  1471. pos:=0;
  1472. size:=tai_const(hp).size;
  1473. while pos<(size-4) do
  1474. begin
  1475. writer.AsmWrite(#9'.vbyte'#9'4, ');
  1476. writer.AsmWriteln(tostr(longint(tai_const(hp).value shr ((size-pos-4)*8))));
  1477. inc(pos,4);
  1478. end;
  1479. writer.AsmWrite(#9'.vbyte'#9);
  1480. writer.AsmWrite(tostr(size-pos));
  1481. writer.AsmWrite(', ');
  1482. case size-pos of
  1483. 1: writer.AsmWrite(tostr(byte(tai_const(hp).value)));
  1484. 2: writer.AsmWrite(tostr(word(tai_const(hp).value)));
  1485. 4: writer.AsmWrite(tostr(longint(tai_const(hp).value)));
  1486. else
  1487. internalerror(2012010402);
  1488. end;
  1489. end;
  1490. procedure TGNUAssembler.WriteUnalignedIntConst(hp: tai_const);
  1491. var
  1492. pos, size: longint;
  1493. begin
  1494. size:=tai_const(hp).size;
  1495. writer.AsmWrite(#9'.byte'#9);
  1496. if target_info.endian=endian_big then
  1497. begin
  1498. pos:=size-1;
  1499. while pos>=0 do
  1500. begin
  1501. writer.AsmWrite(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1502. dec(pos);
  1503. if pos>=0 then
  1504. writer.AsmWrite(', ')
  1505. else
  1506. writer.AsmLn;
  1507. end;
  1508. end
  1509. else
  1510. begin
  1511. pos:=0;
  1512. while pos<size do
  1513. begin
  1514. writer.AsmWriteln(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1515. inc(pos);
  1516. if pos<=size then
  1517. writer.AsmWrite(', ')
  1518. else
  1519. writer.AsmLn;
  1520. end;
  1521. end;
  1522. writer.AsmLn;
  1523. end;
  1524. procedure TGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1525. begin
  1526. { TODO: implement asd_cpu for GAS => usually .arch or .cpu, but the CPU
  1527. name has to be translated as well }
  1528. if dir=asd_cpu then
  1529. writer.AsmWrite(asminfo^.comment+' CPU ')
  1530. else
  1531. writer.AsmWrite('.'+directivestr[dir]+' ');
  1532. end;
  1533. procedure TGNUAssembler.WriteAsmList;
  1534. var
  1535. n : string;
  1536. hal : tasmlisttype;
  1537. i: longint;
  1538. begin
  1539. {$ifdef EXTDEBUG}
  1540. if current_module.mainsource<>'' then
  1541. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource);
  1542. {$endif}
  1543. if current_module.mainsource<>'' then
  1544. n:=ExtractFileName(current_module.mainsource)
  1545. else
  1546. n:=InputFileName;
  1547. { gcc does not add it either for Darwin. Grep for
  1548. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1549. }
  1550. if not(target_info.system in systems_darwin) then
  1551. writer.AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1552. WriteExtraHeader;
  1553. writer.MarkEmpty;
  1554. symendcount:=0;
  1555. for hal:=low(TasmlistType) to high(TasmlistType) do
  1556. begin
  1557. if not (current_asmdata.asmlists[hal].empty) then
  1558. begin
  1559. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1560. writetree(current_asmdata.asmlists[hal]);
  1561. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1562. end;
  1563. end;
  1564. { add weak symbol markers }
  1565. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1566. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1567. WriteWeakSymbolRef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1568. if create_smartlink_sections and
  1569. (target_info.system in systems_darwin) then
  1570. writer.AsmWriteLn(#9'.subsections_via_symbols');
  1571. { "no executable stack" marker }
  1572. { TODO: used by OpenBSD/NetBSD as well? }
  1573. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  1574. not(cs_executable_stack in current_settings.moduleswitches) then
  1575. begin
  1576. writer.AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1577. end;
  1578. writer.AsmLn;
  1579. WriteExtraFooter;
  1580. {$ifdef EXTDEBUG}
  1581. if current_module.mainsource<>'' then
  1582. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource);
  1583. {$endif EXTDEBUG}
  1584. end;
  1585. {****************************************************************************}
  1586. { Apple/GNU Assembler writer }
  1587. {****************************************************************************}
  1588. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1589. begin
  1590. if (target_info.system in systems_darwin) then
  1591. case atype of
  1592. sec_user:
  1593. begin
  1594. result:='.section '+aname;
  1595. exit;
  1596. end;
  1597. sec_bss:
  1598. { all bss (lcomm) symbols are automatically put in the right }
  1599. { place by using the lcomm assembler directive }
  1600. atype := sec_none;
  1601. sec_debug_frame,
  1602. sec_eh_frame:
  1603. begin
  1604. result := '.section __DWARF,__debug_info,regular,debug';
  1605. exit;
  1606. end;
  1607. sec_debug_line:
  1608. begin
  1609. result := '.section __DWARF,__debug_line,regular,debug';
  1610. exit;
  1611. end;
  1612. sec_debug_info:
  1613. begin
  1614. result := '.section __DWARF,__debug_info,regular,debug';
  1615. exit;
  1616. end;
  1617. sec_debug_abbrev:
  1618. begin
  1619. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1620. exit;
  1621. end;
  1622. sec_debug_aranges:
  1623. begin
  1624. result := '.section __DWARF,__debug_aranges,regular,debug';
  1625. exit;
  1626. end;
  1627. sec_debug_ranges:
  1628. begin
  1629. result := '.section __DWARF,__debug_ranges,regular,debug';
  1630. exit;
  1631. end;
  1632. sec_rodata:
  1633. begin
  1634. result := '.const_data';
  1635. exit;
  1636. end;
  1637. sec_rodata_norel:
  1638. begin
  1639. result := '.const';
  1640. exit;
  1641. end;
  1642. sec_fpc:
  1643. begin
  1644. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1645. exit;
  1646. end;
  1647. sec_code:
  1648. begin
  1649. if (aname='fpc_geteipasebx') or
  1650. (aname='fpc_geteipasecx') then
  1651. begin
  1652. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1653. #10'.private_extern '+aname;
  1654. exit;
  1655. end;
  1656. end;
  1657. sec_data_nonlazy:
  1658. begin
  1659. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1660. exit;
  1661. end;
  1662. sec_data_lazy:
  1663. begin
  1664. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1665. exit;
  1666. end;
  1667. sec_init_func:
  1668. begin
  1669. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1670. exit;
  1671. end;
  1672. sec_term_func:
  1673. begin
  1674. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1675. exit;
  1676. end;
  1677. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1678. begin
  1679. result:='.section '+objc_section_name(atype);
  1680. exit
  1681. end;
  1682. end;
  1683. result := inherited sectionname(atype,aname,aorder);
  1684. end;
  1685. procedure TAppleGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1686. begin
  1687. writer.AsmWriteLn(#9'.weak_reference '+s.name);
  1688. end;
  1689. procedure TAppleGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1690. begin
  1691. case dir of
  1692. asd_weak_reference:
  1693. writer.AsmWrite('.weak_reference ');
  1694. asd_weak_definition:
  1695. writer.AsmWrite('.weak_definition ');
  1696. else
  1697. inherited;
  1698. end;
  1699. end;
  1700. {****************************************************************************}
  1701. { a.out/GNU Assembler writer }
  1702. {****************************************************************************}
  1703. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1704. const
  1705. (* Translation table - replace unsupported section types with basic ones. *)
  1706. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1707. sec_none,
  1708. sec_none,
  1709. sec_code,
  1710. sec_data,
  1711. sec_data (* sec_rodata *),
  1712. sec_data (* sec_rodata_norel *),
  1713. sec_bss,
  1714. sec_data (* sec_threadvar *),
  1715. { used for wince exception handling }
  1716. sec_code (* sec_pdata *),
  1717. { used for darwin import stubs }
  1718. sec_code (* sec_stub *),
  1719. sec_data,(* sec_data_nonlazy *)
  1720. sec_data,(* sec_data_lazy *)
  1721. sec_data,(* sec_init_func *)
  1722. sec_data,(* sec_term_func *)
  1723. { stabs }
  1724. sec_stab,sec_stabstr,
  1725. { win32 }
  1726. sec_data (* sec_idata2 *),
  1727. sec_data (* sec_idata4 *),
  1728. sec_data (* sec_idata5 *),
  1729. sec_data (* sec_idata6 *),
  1730. sec_data (* sec_idata7 *),
  1731. sec_data (* sec_edata *),
  1732. { C++ exception handling unwinding (uses dwarf) }
  1733. sec_eh_frame,
  1734. { dwarf }
  1735. sec_debug_frame,
  1736. sec_debug_info,
  1737. sec_debug_line,
  1738. sec_debug_abbrev,
  1739. sec_debug_aranges,
  1740. sec_debug_ranges,
  1741. { ELF resources (+ references to stabs debug information sections) }
  1742. sec_code (* sec_fpc *),
  1743. { Table of contents section }
  1744. sec_code (* sec_toc *),
  1745. sec_code (* sec_init *),
  1746. sec_code (* sec_fini *),
  1747. sec_none (* sec_objc_class *),
  1748. sec_none (* sec_objc_meta_class *),
  1749. sec_none (* sec_objc_cat_cls_meth *),
  1750. sec_none (* sec_objc_cat_inst_meth *),
  1751. sec_none (* sec_objc_protocol *),
  1752. sec_none (* sec_objc_string_object *),
  1753. sec_none (* sec_objc_cls_meth *),
  1754. sec_none (* sec_objc_inst_meth *),
  1755. sec_none (* sec_objc_cls_refs *),
  1756. sec_none (* sec_objc_message_refs *),
  1757. sec_none (* sec_objc_symbols *),
  1758. sec_none (* sec_objc_category *),
  1759. sec_none (* sec_objc_class_vars *),
  1760. sec_none (* sec_objc_instance_vars *),
  1761. sec_none (* sec_objc_module_info *),
  1762. sec_none (* sec_objc_class_names *),
  1763. sec_none (* sec_objc_meth_var_types *),
  1764. sec_none (* sec_objc_meth_var_names *),
  1765. sec_none (* sec_objc_selector_strs *),
  1766. sec_none (* sec_objc_protocol_ext *),
  1767. sec_none (* sec_objc_class_ext *),
  1768. sec_none (* sec_objc_property *),
  1769. sec_none (* sec_objc_image_info *),
  1770. sec_none (* sec_objc_cstring_object *),
  1771. sec_none (* sec_objc_sel_fixup *),
  1772. sec_none (* sec_objc_data *),
  1773. sec_none (* sec_objc_const *),
  1774. sec_none (* sec_objc_sup_refs *),
  1775. sec_none (* sec_data_coalesced *),
  1776. sec_none (* sec_objc_classlist *),
  1777. sec_none (* sec_objc_nlclasslist *),
  1778. sec_none (* sec_objc_catlist *),
  1779. sec_none (* sec_objc_nlcatlist *),
  1780. sec_none (* sec_objc_protlist *),
  1781. sec_none (* sec_stack *),
  1782. sec_none (* sec_heap *),
  1783. sec_none (* gcc_except_table *)
  1784. );
  1785. begin
  1786. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1787. end;
  1788. {****************************************************************************}
  1789. { Abstract Instruction Writer }
  1790. {****************************************************************************}
  1791. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1792. begin
  1793. inherited create;
  1794. owner := _owner;
  1795. end;
  1796. end.