aggas.pas 78 KB

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