aggas.pas 73 KB

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