aggas.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  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_tlsdesc:
  914. begin
  915. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsdesc)+(.-'+tai_const(hp).endsym.name+tostr_with_plus(tai_const(hp).symofs)+')');
  916. writer.Asmln;
  917. end;
  918. aitconst_tpoff:
  919. begin
  920. if assigned(tai_const(hp).endsym) or (tai_const(hp).symofs<>0) then
  921. Internalerror(2019092805);
  922. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tpoff)');
  923. writer.Asmln;
  924. end;
  925. {$endif cpu64bitaddr}
  926. aitconst_dtpoff:
  927. begin
  928. {$ifdef arm}
  929. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(tlsldo)');
  930. writer.Asmln;
  931. {$endif arm}
  932. {$ifdef x86_64}
  933. writer.AsmWrite(#9'.long'#9+tai_const(hp).sym.name+'@dtpoff');
  934. writer.Asmln;
  935. {$endif x86_64}
  936. {$ifdef i386}
  937. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'@tdpoff');
  938. writer.Asmln;
  939. {$endif i386}
  940. end;
  941. aitconst_got:
  942. begin
  943. if tai_const(hp).symofs<>0 then
  944. InternalError(2015091401); // No symbol offset is allowed for GOT.
  945. writer.AsmWrite(#9'.word'#9+tai_const(hp).sym.name+'(GOT)');
  946. writer.AsmLn;
  947. end;
  948. aitconst_gotoff_symbol:
  949. begin
  950. if (tai_const(hp).sym=nil) then
  951. InternalError(2014022601);
  952. case target_info.cpu of
  953. cpu_mipseb,cpu_mipsel:
  954. begin
  955. writer.AsmWrite(#9'.gpword'#9);
  956. writer.AsmWrite(tai_const(hp).sym.name);
  957. end;
  958. cpu_i386:
  959. begin
  960. writer.AsmWrite(ait_const2str[aitconst_32bit]);
  961. writer.AsmWrite(tai_const(hp).sym.name+'-_GLOBAL_OFFSET_TABLE_');
  962. end;
  963. else
  964. InternalError(2014022602);
  965. end;
  966. if (tai_const(hp).value<>0) then
  967. writer.AsmWrite(tostr_with_plus(tai_const(hp).value));
  968. writer.AsmLn;
  969. end;
  970. aitconst_uleb128bit,
  971. aitconst_sleb128bit,
  972. {$ifdef cpu64bitaddr}
  973. aitconst_128bit,
  974. aitconst_64bit,
  975. {$endif cpu64bitaddr}
  976. aitconst_32bit,
  977. aitconst_16bit,
  978. aitconst_8bit,
  979. aitconst_rva_symbol,
  980. aitconst_secrel32_symbol,
  981. aitconst_darwin_dwarf_delta32,
  982. aitconst_darwin_dwarf_delta64,
  983. aitconst_half16bit,
  984. aitconst_gs,
  985. aitconst_16bit_unaligned,
  986. aitconst_32bit_unaligned,
  987. aitconst_64bit_unaligned:
  988. begin
  989. { the AIX assembler (and for compatibility, the GNU
  990. assembler when targeting AIX) automatically aligns
  991. .short/.long/.llong to a multiple of 2/4/8 bytes. We
  992. don't want that, since this may be data inside a packed
  993. record -> use .vbyte instead (byte stream of fixed
  994. length) }
  995. if (target_info.system in systems_aix) and
  996. (constdef in [aitconst_128bit,aitconst_64bit,aitconst_32bit,aitconst_16bit]) and
  997. not assigned(tai_const(hp).sym) then
  998. begin
  999. WriteAixIntConst(tai_const(hp));
  1000. end
  1001. else if (target_info.system in systems_darwin) and
  1002. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  1003. begin
  1004. writer.AsmWrite(ait_const2str[aitconst_8bit]);
  1005. case tai_const(hp).consttype of
  1006. aitconst_uleb128bit:
  1007. WriteDecodedUleb128(qword(tai_const(hp).value));
  1008. aitconst_sleb128bit:
  1009. WriteDecodedSleb128(int64(tai_const(hp).value));
  1010. else
  1011. ;
  1012. end
  1013. end
  1014. else
  1015. begin
  1016. if (constdef in ait_unaligned_consts) and
  1017. (target_info.system in use_ua_sparc_systems) then
  1018. writer.AsmWrite(ait_ua_sparc_const2str[constdef])
  1019. else if (constdef in ait_unaligned_consts) and
  1020. (target_info.system in use_ua_elf_systems) then
  1021. writer.AsmWrite(ait_ua_elf_const2str[constdef])
  1022. { we can also have unaligned pointers in packed record
  1023. constants, which don't get translated into
  1024. unaligned tai -> always use vbyte }
  1025. else if target_info.system in systems_aix then
  1026. writer.AsmWrite(#9'.vbyte'#9+tostr(tai_const(hp).size)+',')
  1027. else if (asminfo^.id=as_solaris_as) then
  1028. writer.AsmWrite(ait_solaris_const2str[constdef])
  1029. else
  1030. writer.AsmWrite(ait_const2str[constdef]);
  1031. l:=0;
  1032. t := '';
  1033. repeat
  1034. if assigned(tai_const(hp).sym) then
  1035. begin
  1036. if assigned(tai_const(hp).endsym) then
  1037. begin
  1038. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  1039. begin
  1040. s := NextSetLabel;
  1041. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  1042. end
  1043. else
  1044. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  1045. end
  1046. else
  1047. s:=tai_const(hp).sym.name;
  1048. if replaceforbidden then
  1049. s:=ReplaceForbiddenAsmSymbolChars(s);
  1050. if tai_const(hp).value<>0 then
  1051. s:=s+tostr_with_plus(tai_const(hp).value);
  1052. end
  1053. else
  1054. {$ifdef cpu64bitaddr}
  1055. s:=tostr(tai_const(hp).value);
  1056. {$else cpu64bitaddr}
  1057. { 64 bit constants are already handled above in this case }
  1058. s:=tostr(longint(tai_const(hp).value));
  1059. {$endif cpu64bitaddr}
  1060. if constdef = aitconst_half16bit then
  1061. s:='('+s+')/2';
  1062. if constdef = aitconst_gs then
  1063. s:='gs('+s+')';
  1064. writer.AsmWrite(s);
  1065. inc(l,length(s));
  1066. { Values with symbols are written on a single line to improve
  1067. reading of the .s file (PFV) }
  1068. if assigned(tai_const(hp).sym) or
  1069. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  1070. (l>line_length) or
  1071. (hp.next=nil) or
  1072. (tai(hp.next).typ<>ait_const) or
  1073. (tai_const(hp.next).consttype<>constdef) or
  1074. assigned(tai_const(hp.next).sym) then
  1075. break;
  1076. hp:=tai(hp.next);
  1077. writer.AsmWrite(',');
  1078. until false;
  1079. if (t <> '') then
  1080. begin
  1081. writer.AsmLn;
  1082. writer.AsmWrite(t);
  1083. end;
  1084. end;
  1085. writer.AsmLn;
  1086. end;
  1087. else
  1088. internalerror(200704251);
  1089. end;
  1090. end;
  1091. ait_realconst :
  1092. begin
  1093. WriteRealConstAsBytes(tai_realconst(hp),#9'.byte'#9,do_line);
  1094. end;
  1095. ait_string :
  1096. begin
  1097. pos:=0;
  1098. if not(target_info.system in systems_aix) then
  1099. begin
  1100. for i:=1 to tai_string(hp).len do
  1101. begin
  1102. if pos=0 then
  1103. begin
  1104. writer.AsmWrite(#9'.ascii'#9'"');
  1105. pos:=20;
  1106. end;
  1107. ch:=tai_string(hp).str[i-1];
  1108. case ch of
  1109. #0, {This can't be done by range, because a bug in FPC}
  1110. #1..#31,
  1111. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  1112. '"' : s:='\"';
  1113. '\' : s:='\\';
  1114. else
  1115. s:=ch;
  1116. end;
  1117. writer.AsmWrite(s);
  1118. inc(pos,length(s));
  1119. if (pos>line_length) or (i=tai_string(hp).len) then
  1120. begin
  1121. writer.AsmWriteLn('"');
  1122. pos:=0;
  1123. end;
  1124. end;
  1125. end
  1126. else
  1127. WriteAixStringConst(tai_string(hp));
  1128. end;
  1129. ait_label :
  1130. begin
  1131. if (tai_label(hp).labsym.is_used) then
  1132. begin
  1133. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1134. begin
  1135. writer.AsmWrite(#9'.private_extern ');
  1136. writer.AsmWriteln(tai_label(hp).labsym.name);
  1137. end;
  1138. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  1139. begin
  1140. {$ifdef arm}
  1141. { do no change arm mode accidently, .globl seems to reset the mode }
  1142. if GenerateThumbCode or GenerateThumb2Code then
  1143. writer.AsmWriteln(#9'.thumb_func'#9);
  1144. {$endif arm}
  1145. writer.AsmWrite('.globl'#9);
  1146. if replaceforbidden then
  1147. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1148. else
  1149. writer.AsmWriteLn(tai_label(hp).labsym.name);
  1150. end;
  1151. if replaceforbidden then
  1152. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1153. else
  1154. writer.AsmWrite(tai_label(hp).labsym.name);
  1155. writer.AsmWriteLn(':');
  1156. end;
  1157. end;
  1158. ait_symbol :
  1159. begin
  1160. if (target_info.system=system_powerpc64_linux) and
  1161. (tai_symbol(hp).sym.typ=AT_FUNCTION) and
  1162. (cs_profile in current_settings.moduleswitches) then
  1163. writer.AsmWriteLn('.globl _mcount');
  1164. if tai_symbol(hp).is_global then
  1165. begin
  1166. writer.AsmWrite('.globl'#9);
  1167. if replaceforbidden then
  1168. writer.AsmWriteln(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1169. else
  1170. writer.AsmWriteln(tai_symbol(hp).sym.name);
  1171. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  1172. WriteHiddenSymbol(tai_symbol(hp).sym);
  1173. end;
  1174. if (target_info.system=system_powerpc64_linux) and
  1175. use_dotted_functions and
  1176. (tai_symbol(hp).sym.typ=AT_FUNCTION) then
  1177. begin
  1178. writer.AsmWriteLn('.section ".opd", "aw"');
  1179. writer.AsmWriteLn('.align 3');
  1180. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':');
  1181. writer.AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  1182. writer.AsmWriteLn('.previous');
  1183. writer.AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1184. if (tai_symbol(hp).is_global) then
  1185. writer.AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1186. writer.AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1187. { the dotted name is the name of the actual function entry }
  1188. writer.AsmWrite('.');
  1189. end
  1190. else if (target_info.system in systems_aix) and
  1191. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  1192. begin
  1193. if target_info.system=system_powerpc_aix then
  1194. begin
  1195. s:=#9'.long .';
  1196. ch:='2';
  1197. end
  1198. else
  1199. begin
  1200. s:=#9'.llong .';
  1201. ch:='3';
  1202. end;
  1203. writer.AsmWriteLn(#9'.csect '+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+'[DS],'+ch);
  1204. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+':');
  1205. writer.AsmWriteln(s+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name)+', TOC[tc0], 0');
  1206. writer.AsmWriteln(#9'.csect .text[PR]');
  1207. if (tai_symbol(hp).is_global) then
  1208. writer.AsmWriteLn('.globl .'+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name))
  1209. else
  1210. writer.AsmWriteLn('.lglobl .'+ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name));
  1211. { the dotted name is the name of the actual function entry }
  1212. writer.AsmWrite('.');
  1213. end
  1214. else
  1215. begin
  1216. if ((target_info.system <> system_arm_linux) and (target_info.system <> system_arm_android)) then
  1217. sepChar := '@'
  1218. else
  1219. sepChar := '#';
  1220. if (tf_needs_symbol_type in target_info.flags) then
  1221. begin
  1222. writer.AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1223. if (needsObject(tai_symbol(hp))) then
  1224. writer.AsmWriteLn(',' + sepChar + 'object')
  1225. else
  1226. writer.AsmWriteLn(',' + sepChar + 'function');
  1227. end;
  1228. end;
  1229. if replaceforbidden then
  1230. if not(tai_symbol(hp).has_value) then
  1231. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name + ':'))
  1232. else
  1233. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)))
  1234. else if not(tai_symbol(hp).has_value) then
  1235. writer.AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1236. else
  1237. writer.AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1238. end;
  1239. ait_symbolpair:
  1240. begin
  1241. writer.AsmWrite(#9);
  1242. writer.AsmWrite(symbolpairkindstr[tai_symbolpair(hp).kind]);
  1243. writer.AsmWrite(' ');
  1244. if tai_symbolpair(hp).kind<>spk_localentry then
  1245. s:=', '
  1246. else
  1247. { the .localentry directive has to specify the size from the
  1248. start till here of the non-local entry code as second argument }
  1249. s:=', .-';
  1250. if replaceforbidden then
  1251. begin
  1252. { avoid string truncation }
  1253. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).sym^));
  1254. writer.AsmWrite(s);
  1255. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).value^));
  1256. if tai_symbolpair(hp).kind=spk_set_global then
  1257. begin
  1258. writer.AsmWrite(#9'.globl ');
  1259. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbolpair(hp).sym^));
  1260. end;
  1261. end
  1262. else
  1263. begin
  1264. { avoid string truncation }
  1265. writer.AsmWrite(tai_symbolpair(hp).sym^);
  1266. writer.AsmWrite(s);
  1267. writer.AsmWriteLn(tai_symbolpair(hp).value^);
  1268. if tai_symbolpair(hp).kind=spk_set_global then
  1269. begin
  1270. writer.AsmWrite(#9'.globl ');
  1271. writer.AsmWriteLn(tai_symbolpair(hp).sym^);
  1272. end;
  1273. end;
  1274. end;
  1275. ait_symbol_end :
  1276. begin
  1277. if tf_needs_symbol_size in target_info.flags then
  1278. begin
  1279. s:=asminfo^.labelprefix+'e'+tostr(symendcount);
  1280. inc(symendcount);
  1281. writer.AsmWriteLn(s+':');
  1282. writer.AsmWrite(#9'.size'#9);
  1283. if (target_info.system=system_powerpc64_linux) and
  1284. use_dotted_functions and
  1285. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1286. writer.AsmWrite('.');
  1287. if replaceforbidden then
  1288. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_symbol_end(hp).sym.name))
  1289. else
  1290. writer.AsmWrite(tai_symbol_end(hp).sym.name);
  1291. writer.AsmWrite(', '+s+' - ');
  1292. if (target_info.system=system_powerpc64_linux) and
  1293. use_dotted_functions and
  1294. (tai_symbol_end(hp).sym.typ=AT_FUNCTION) then
  1295. writer.AsmWrite('.');
  1296. if replaceforbidden then
  1297. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(tai_symbol_end(hp).sym.name))
  1298. else
  1299. writer.AsmWriteLn(tai_symbol_end(hp).sym.name);
  1300. end;
  1301. end;
  1302. ait_instruction :
  1303. begin
  1304. WriteInstruction(hp);
  1305. end;
  1306. ait_stab :
  1307. begin
  1308. if assigned(tai_stab(hp).str) then
  1309. begin
  1310. writer.AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1311. writer.AsmWritePChar(tai_stab(hp).str);
  1312. writer.AsmLn;
  1313. end;
  1314. end;
  1315. ait_force_line,
  1316. ait_function_name :
  1317. begin
  1318. {$ifdef DEBUG_AGGAS}
  1319. WriteStr(s,hp.typ);
  1320. writer.AsmWriteLn('# '+s);
  1321. {$endif DEBUG_AGGAS}
  1322. end;
  1323. ait_cutobject :
  1324. begin
  1325. {$ifdef DEBUG_AGGAS}
  1326. writer.AsmWriteLn('# ait_cutobject');
  1327. {$endif DEBUG_AGGAS}
  1328. if SmartAsm then
  1329. begin
  1330. { only reset buffer if nothing has changed }
  1331. if not(writer.ClearIfEmpty) then
  1332. begin
  1333. writer.AsmClose;
  1334. DoAssemble;
  1335. writer.AsmCreate(tai_cutobject(hp).place);
  1336. end;
  1337. { avoid empty files }
  1338. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1339. begin
  1340. if tai(hp.next).typ=ait_section then
  1341. LastSecType:=tai_section(hp.next).sectype;
  1342. hp:=tai(hp.next);
  1343. end;
  1344. if LastSecType<>sec_none then
  1345. WriteSection(LastSecType,'',secorder_default,last_align);
  1346. writer.MarkEmpty;
  1347. end;
  1348. end;
  1349. ait_marker :
  1350. begin
  1351. {$ifdef DEBUG_AGGAS}
  1352. WriteStr(s,tai_marker(hp).Kind);
  1353. writer.AsmWriteLn('# ait_marker, kind: '+s);
  1354. {$endif DEBUG_AGGAS}
  1355. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1356. inc(InlineLevel)
  1357. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1358. dec(InlineLevel);
  1359. end;
  1360. ait_directive :
  1361. begin
  1362. WriteDirectiveName(tai_directive(hp).directive);
  1363. if tai_directive(hp).name <>'' then
  1364. begin
  1365. if replaceforbidden then
  1366. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_directive(hp).name))
  1367. else
  1368. writer.AsmWrite(tai_directive(hp).name);
  1369. end;
  1370. writer.AsmLn;
  1371. end;
  1372. ait_seh_directive :
  1373. begin
  1374. {$ifndef DISABLE_WIN64_SEH}
  1375. writer.AsmWrite(sehdirectivestr[tai_seh_directive(hp).kind]);
  1376. case tai_seh_directive(hp).datatype of
  1377. sd_none:;
  1378. sd_string:
  1379. begin
  1380. writer.AsmWrite(' '+tai_seh_directive(hp).data.name^);
  1381. if (tai_seh_directive(hp).data.flags and 1)<>0 then
  1382. writer.AsmWrite(',@except');
  1383. if (tai_seh_directive(hp).data.flags and 2)<>0 then
  1384. writer.AsmWrite(',@unwind');
  1385. end;
  1386. sd_reg:
  1387. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg));
  1388. sd_offset:
  1389. writer.AsmWrite(' '+tostr(tai_seh_directive(hp).data.offset));
  1390. sd_regoffset:
  1391. writer.AsmWrite(' '+gas_regname(tai_seh_directive(hp).data.reg)+', '+
  1392. tostr(tai_seh_directive(hp).data.offset));
  1393. end;
  1394. writer.AsmLn;
  1395. {$endif DISABLE_WIN64_SEH}
  1396. end;
  1397. ait_varloc:
  1398. begin
  1399. if tai_varloc(hp).newlocationhi<>NR_NO then
  1400. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1401. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  1402. else
  1403. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1404. std_regname(tai_varloc(hp).newlocation)));
  1405. writer.AsmLn;
  1406. end;
  1407. ait_cfi:
  1408. begin
  1409. WriteCFI(tai_cfi_base(hp));
  1410. end;
  1411. else
  1412. internalerror(2006012201);
  1413. end;
  1414. lasthp:=hp;
  1415. hp:=tai(hp.next);
  1416. end;
  1417. end;
  1418. procedure TGNUAssembler.WriteExtraHeader;
  1419. begin
  1420. end;
  1421. procedure TGNUAssembler.WriteExtraFooter;
  1422. begin
  1423. end;
  1424. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1425. begin
  1426. InstrWriter.WriteInstruction(hp);
  1427. end;
  1428. procedure TGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1429. begin
  1430. writer.AsmWrite(#9'.weak ');
  1431. if asminfo^.dollarsign='$' then
  1432. writer.AsmWriteLn(s.name)
  1433. else
  1434. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(s.name))
  1435. end;
  1436. procedure TGNUAssembler.WriteHiddenSymbol(sym: TAsmSymbol);
  1437. begin
  1438. { on Windows/(PE)COFF, global symbols are hidden by default: global
  1439. symbols that are not explicitly exported from an executable/library,
  1440. become hidden }
  1441. if target_info.system in systems_windows then
  1442. exit;
  1443. if target_info.system in systems_darwin then
  1444. writer.AsmWrite(#9'.private_extern ')
  1445. else
  1446. writer.AsmWrite(#9'.hidden ');
  1447. if asminfo^.dollarsign='$' then
  1448. writer.AsmWriteLn(sym.name)
  1449. else
  1450. writer.AsmWriteLn(ReplaceForbiddenAsmSymbolChars(sym.name))
  1451. end;
  1452. procedure TGNUAssembler.WriteAixStringConst(hp: tai_string);
  1453. type
  1454. tterminationkind = (term_none,term_string,term_nostring);
  1455. var
  1456. i: longint;
  1457. pos: longint;
  1458. s: string;
  1459. ch: char;
  1460. instring: boolean;
  1461. procedure newstatement(terminationkind: tterminationkind);
  1462. begin
  1463. case terminationkind of
  1464. term_none: ;
  1465. term_string:
  1466. writer.AsmWriteLn('"');
  1467. term_nostring:
  1468. writer.AsmLn;
  1469. end;
  1470. writer.AsmWrite(#9'.byte'#9);
  1471. pos:=20;
  1472. instring:=false;
  1473. end;
  1474. begin
  1475. pos:=0;
  1476. instring:=false;
  1477. for i:=1 to hp.len do
  1478. begin
  1479. if pos=0 then
  1480. newstatement(term_none);
  1481. ch:=hp.str[i-1];
  1482. case ch of
  1483. #0..#31,
  1484. #127..#255 :
  1485. begin
  1486. if instring then
  1487. newstatement(term_string);
  1488. if pos=20 then
  1489. s:=tostr(ord(ch))
  1490. else
  1491. s:=', '+tostr(ord(ch))
  1492. end;
  1493. '"' :
  1494. if instring then
  1495. s:='""'
  1496. else
  1497. begin
  1498. if pos<>20 then
  1499. newstatement(term_nostring);
  1500. s:='"""';
  1501. instring:=true;
  1502. end;
  1503. else
  1504. if not instring then
  1505. begin
  1506. if (pos<>20) then
  1507. newstatement(term_nostring);
  1508. s:='"'+ch;
  1509. instring:=true;
  1510. end
  1511. else
  1512. s:=ch;
  1513. end;
  1514. writer.AsmWrite(s);
  1515. inc(pos,length(s));
  1516. if (pos>line_length) or (i=tai_string(hp).len) then
  1517. begin
  1518. if instring then
  1519. writer.AsmWriteLn('"')
  1520. else
  1521. writer.AsmLn;
  1522. pos:=0;
  1523. end;
  1524. end;
  1525. end;
  1526. procedure TGNUAssembler.WriteAixIntConst(hp: tai_const);
  1527. var
  1528. pos, size: longint;
  1529. begin
  1530. { only big endian AIX supported for now }
  1531. if target_info.endian<>endian_big then
  1532. internalerror(2012010401);
  1533. { limitation: can only write 4 bytes at a time }
  1534. pos:=0;
  1535. size:=tai_const(hp).size;
  1536. while pos<(size-4) do
  1537. begin
  1538. writer.AsmWrite(#9'.vbyte'#9'4, ');
  1539. writer.AsmWriteln(tostr(longint(tai_const(hp).value shr ((size-pos-4)*8))));
  1540. inc(pos,4);
  1541. end;
  1542. writer.AsmWrite(#9'.vbyte'#9);
  1543. writer.AsmWrite(tostr(size-pos));
  1544. writer.AsmWrite(', ');
  1545. case size-pos of
  1546. 1: writer.AsmWrite(tostr(byte(tai_const(hp).value)));
  1547. 2: writer.AsmWrite(tostr(word(tai_const(hp).value)));
  1548. 4: writer.AsmWrite(tostr(longint(tai_const(hp).value)));
  1549. else
  1550. internalerror(2012010402);
  1551. end;
  1552. end;
  1553. procedure TGNUAssembler.WriteUnalignedIntConst(hp: tai_const);
  1554. var
  1555. pos, size: longint;
  1556. begin
  1557. size:=tai_const(hp).size;
  1558. writer.AsmWrite(#9'.byte'#9);
  1559. if target_info.endian=endian_big then
  1560. begin
  1561. pos:=size-1;
  1562. while pos>=0 do
  1563. begin
  1564. writer.AsmWrite(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1565. dec(pos);
  1566. if pos>=0 then
  1567. writer.AsmWrite(', ')
  1568. else
  1569. writer.AsmLn;
  1570. end;
  1571. end
  1572. else
  1573. begin
  1574. pos:=0;
  1575. while pos<size do
  1576. begin
  1577. writer.AsmWriteln(tostr((tai_const(hp).value shr (pos*8)) and $ff));
  1578. inc(pos);
  1579. if pos<=size then
  1580. writer.AsmWrite(', ')
  1581. else
  1582. writer.AsmLn;
  1583. end;
  1584. end;
  1585. writer.AsmLn;
  1586. end;
  1587. procedure TGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1588. begin
  1589. { TODO: implement asd_cpu for GAS => usually .arch or .cpu, but the CPU
  1590. name has to be translated as well }
  1591. if dir=asd_cpu then
  1592. writer.AsmWrite(asminfo^.comment+' CPU ')
  1593. else
  1594. writer.AsmWrite('.'+directivestr[dir]+' ');
  1595. end;
  1596. procedure TGNUAssembler.WriteAsmList;
  1597. var
  1598. n : string;
  1599. hal : tasmlisttype;
  1600. i: longint;
  1601. begin
  1602. {$ifdef EXTDEBUG}
  1603. if current_module.mainsource<>'' then
  1604. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource);
  1605. {$endif}
  1606. if current_module.mainsource<>'' then
  1607. n:=ExtractFileName(current_module.mainsource)
  1608. else
  1609. n:=InputFileName;
  1610. { gcc does not add it either for Darwin. Grep for
  1611. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1612. }
  1613. if not(target_info.system in systems_darwin) then
  1614. writer.AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1615. WriteExtraHeader;
  1616. writer.MarkEmpty;
  1617. symendcount:=0;
  1618. for hal:=low(TasmlistType) to high(TasmlistType) do
  1619. begin
  1620. if not (current_asmdata.asmlists[hal].empty) then
  1621. begin
  1622. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1623. writetree(current_asmdata.asmlists[hal]);
  1624. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1625. end;
  1626. end;
  1627. { add weak symbol markers }
  1628. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1629. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1630. WriteWeakSymbolRef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1631. if create_smartlink_sections and
  1632. (target_info.system in systems_darwin) then
  1633. writer.AsmWriteLn(#9'.subsections_via_symbols');
  1634. { "no executable stack" marker }
  1635. { TODO: used by OpenBSD/NetBSD as well? }
  1636. if (target_info.system in (systems_linux + systems_android + systems_freebsd + systems_dragonfly)) and
  1637. not(cs_executable_stack in current_settings.moduleswitches) then
  1638. begin
  1639. writer.AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1640. end;
  1641. writer.AsmLn;
  1642. WriteExtraFooter;
  1643. {$ifdef EXTDEBUG}
  1644. if current_module.mainsource<>'' then
  1645. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource);
  1646. {$endif EXTDEBUG}
  1647. end;
  1648. {****************************************************************************}
  1649. { Apple/GNU Assembler writer }
  1650. {****************************************************************************}
  1651. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1652. begin
  1653. if (target_info.system in systems_darwin) then
  1654. case atype of
  1655. sec_user:
  1656. begin
  1657. result:='.section '+aname;
  1658. exit;
  1659. end;
  1660. sec_bss:
  1661. { all bss (lcomm) symbols are automatically put in the right }
  1662. { place by using the lcomm assembler directive }
  1663. atype := sec_none;
  1664. sec_debug_frame,
  1665. sec_eh_frame:
  1666. begin
  1667. result := '.section __DWARF,__debug_info,regular,debug';
  1668. exit;
  1669. end;
  1670. sec_debug_line:
  1671. begin
  1672. result := '.section __DWARF,__debug_line,regular,debug';
  1673. exit;
  1674. end;
  1675. sec_debug_info:
  1676. begin
  1677. result := '.section __DWARF,__debug_info,regular,debug';
  1678. exit;
  1679. end;
  1680. sec_debug_abbrev:
  1681. begin
  1682. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1683. exit;
  1684. end;
  1685. sec_debug_aranges:
  1686. begin
  1687. result := '.section __DWARF,__debug_aranges,regular,debug';
  1688. exit;
  1689. end;
  1690. sec_debug_ranges:
  1691. begin
  1692. result := '.section __DWARF,__debug_ranges,regular,debug';
  1693. exit;
  1694. end;
  1695. sec_rodata:
  1696. begin
  1697. result := '.const_data';
  1698. exit;
  1699. end;
  1700. sec_rodata_norel:
  1701. begin
  1702. result := '.const';
  1703. exit;
  1704. end;
  1705. sec_fpc:
  1706. begin
  1707. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1708. exit;
  1709. end;
  1710. sec_code:
  1711. begin
  1712. if (aname='fpc_geteipasebx') or
  1713. (aname='fpc_geteipasecx') then
  1714. begin
  1715. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1716. #10'.private_extern '+aname;
  1717. exit;
  1718. end;
  1719. end;
  1720. sec_data_nonlazy:
  1721. begin
  1722. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1723. exit;
  1724. end;
  1725. sec_data_lazy:
  1726. begin
  1727. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1728. exit;
  1729. end;
  1730. sec_init_func:
  1731. begin
  1732. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1733. exit;
  1734. end;
  1735. sec_term_func:
  1736. begin
  1737. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1738. exit;
  1739. end;
  1740. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1741. begin
  1742. result:='.section '+objc_section_name(atype);
  1743. exit
  1744. end;
  1745. else
  1746. ;
  1747. end;
  1748. result := inherited sectionname(atype,aname,aorder);
  1749. end;
  1750. procedure TAppleGNUAssembler.WriteWeakSymbolRef(s: tasmsymbol);
  1751. begin
  1752. writer.AsmWriteLn(#9'.weak_reference '+s.name);
  1753. end;
  1754. procedure TAppleGNUAssembler.WriteDirectiveName(dir: TAsmDirective);
  1755. begin
  1756. case dir of
  1757. asd_weak_reference:
  1758. writer.AsmWrite('.weak_reference ');
  1759. asd_weak_definition:
  1760. writer.AsmWrite('.weak_definition ');
  1761. else
  1762. inherited;
  1763. end;
  1764. end;
  1765. {****************************************************************************}
  1766. { a.out/GNU Assembler writer }
  1767. {****************************************************************************}
  1768. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1769. const
  1770. (* Translation table - replace unsupported section types with basic ones. *)
  1771. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1772. sec_none,
  1773. sec_none,
  1774. sec_code,
  1775. sec_data,
  1776. sec_data (* sec_rodata *),
  1777. sec_data (* sec_rodata_norel *),
  1778. sec_bss,
  1779. sec_data (* sec_threadvar *),
  1780. { used for wince exception handling }
  1781. sec_code (* sec_pdata *),
  1782. { used for darwin import stubs }
  1783. sec_code (* sec_stub *),
  1784. sec_data,(* sec_data_nonlazy *)
  1785. sec_data,(* sec_data_lazy *)
  1786. sec_data,(* sec_init_func *)
  1787. sec_data,(* sec_term_func *)
  1788. { stabs }
  1789. sec_stab,sec_stabstr,
  1790. { win32 }
  1791. sec_data (* sec_idata2 *),
  1792. sec_data (* sec_idata4 *),
  1793. sec_data (* sec_idata5 *),
  1794. sec_data (* sec_idata6 *),
  1795. sec_data (* sec_idata7 *),
  1796. sec_data (* sec_edata *),
  1797. { C++ exception handling unwinding (uses dwarf) }
  1798. sec_eh_frame,
  1799. { dwarf }
  1800. sec_debug_frame,
  1801. sec_debug_info,
  1802. sec_debug_line,
  1803. sec_debug_abbrev,
  1804. sec_debug_aranges,
  1805. sec_debug_ranges,
  1806. { ELF resources (+ references to stabs debug information sections) }
  1807. sec_code (* sec_fpc *),
  1808. { Table of contents section }
  1809. sec_code (* sec_toc *),
  1810. sec_code (* sec_init *),
  1811. sec_code (* sec_fini *),
  1812. sec_none (* sec_objc_class *),
  1813. sec_none (* sec_objc_meta_class *),
  1814. sec_none (* sec_objc_cat_cls_meth *),
  1815. sec_none (* sec_objc_cat_inst_meth *),
  1816. sec_none (* sec_objc_protocol *),
  1817. sec_none (* sec_objc_string_object *),
  1818. sec_none (* sec_objc_cls_meth *),
  1819. sec_none (* sec_objc_inst_meth *),
  1820. sec_none (* sec_objc_cls_refs *),
  1821. sec_none (* sec_objc_message_refs *),
  1822. sec_none (* sec_objc_symbols *),
  1823. sec_none (* sec_objc_category *),
  1824. sec_none (* sec_objc_class_vars *),
  1825. sec_none (* sec_objc_instance_vars *),
  1826. sec_none (* sec_objc_module_info *),
  1827. sec_none (* sec_objc_class_names *),
  1828. sec_none (* sec_objc_meth_var_types *),
  1829. sec_none (* sec_objc_meth_var_names *),
  1830. sec_none (* sec_objc_selector_strs *),
  1831. sec_none (* sec_objc_protocol_ext *),
  1832. sec_none (* sec_objc_class_ext *),
  1833. sec_none (* sec_objc_property *),
  1834. sec_none (* sec_objc_image_info *),
  1835. sec_none (* sec_objc_cstring_object *),
  1836. sec_none (* sec_objc_sel_fixup *),
  1837. sec_none (* sec_objc_data *),
  1838. sec_none (* sec_objc_const *),
  1839. sec_none (* sec_objc_sup_refs *),
  1840. sec_none (* sec_data_coalesced *),
  1841. sec_none (* sec_objc_classlist *),
  1842. sec_none (* sec_objc_nlclasslist *),
  1843. sec_none (* sec_objc_catlist *),
  1844. sec_none (* sec_objc_nlcatlist *),
  1845. sec_none (* sec_objc_protlist *),
  1846. sec_none (* sec_stack *),
  1847. sec_none (* sec_heap *),
  1848. sec_none (* gcc_except_table *)
  1849. );
  1850. begin
  1851. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1852. end;
  1853. {****************************************************************************}
  1854. { Abstract Instruction Writer }
  1855. {****************************************************************************}
  1856. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1857. begin
  1858. inherited create;
  1859. owner := _owner;
  1860. end;
  1861. end.