aggas.pas 73 KB

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