aggas.pas 72 KB

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