aggas.pas 80 KB

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