aggas.pas 73 KB

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