aggas.pas 81 KB

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