aggas.pas 74 KB

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