aggas.pas 73 KB

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