aggas.pas 76 KB

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