aggas.pas 75 KB

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