aggas.pas 73 KB

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