aggas.pas 78 KB

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