aggas.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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. cclasses,
  25. globtype,globals,
  26. aasmbase,aasmtai,aasmdata,aasmcpu,
  27. assemble;
  28. type
  29. TCPUInstrWriter = class;
  30. {# This is a derived class which is used to write
  31. GAS styled assembler.
  32. }
  33. { TGNUAssembler }
  34. TGNUAssembler=class(texternalassembler)
  35. protected
  36. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;virtual;
  37. function sectionattrs_coff(atype:TAsmSectiontype):string;virtual;
  38. procedure WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
  39. procedure WriteExtraHeader;virtual;
  40. procedure WriteInstruction(hp: tai);
  41. procedure WriteWeakSymbolDef(s: tasmsymbol); virtual;
  42. public
  43. function MakeCmdLine: TCmdStr; override;
  44. procedure WriteTree(p:TAsmList);override;
  45. procedure WriteAsmList;override;
  46. destructor destroy; override;
  47. private
  48. setcount: longint;
  49. procedure WriteDecodedSleb128(a: int64);
  50. procedure WriteDecodedUleb128(a: qword);
  51. function NextSetLabel: string;
  52. protected
  53. InstrWriter: TCPUInstrWriter;
  54. end;
  55. {# This is the base class for writing instructions.
  56. The WriteInstruction() method must be overridden
  57. to write a single instruction to the assembler
  58. file.
  59. }
  60. TCPUInstrWriter = class
  61. constructor create(_owner: TGNUAssembler);
  62. procedure WriteInstruction(hp : tai); virtual; abstract;
  63. protected
  64. owner: TGNUAssembler;
  65. end;
  66. { TAppleGNUAssembler }
  67. TAppleGNUAssembler=class(TGNUAssembler)
  68. protected
  69. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  70. procedure WriteWeakSymbolDef(s: tasmsymbol); override;
  71. end;
  72. TAoutGNUAssembler=class(TGNUAssembler)
  73. function sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;override;
  74. end;
  75. function ReplaceForbiddenChars(const s: string): string;
  76. implementation
  77. uses
  78. SysUtils,
  79. cutils,cfileutl,systems,
  80. fmodule,finput,verbose,
  81. itcpugas,cpubase;
  82. const
  83. line_length = 70;
  84. var
  85. symendcount : longint;
  86. type
  87. {$ifdef cpuextended}
  88. t80bitarray = array[0..9] of byte;
  89. {$endif cpuextended}
  90. t64bitarray = array[0..7] of byte;
  91. t32bitarray = array[0..3] of byte;
  92. {****************************************************************************}
  93. { Support routines }
  94. {****************************************************************************}
  95. function single2str(d : single) : string;
  96. var
  97. hs : string;
  98. begin
  99. str(d,hs);
  100. { replace space with + }
  101. if hs[1]=' ' then
  102. hs[1]:='+';
  103. single2str:='0d'+hs
  104. end;
  105. function double2str(d : double) : 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. double2str:='0d'+hs
  114. end;
  115. function extended2str(e : extended) : string;
  116. var
  117. hs : string;
  118. begin
  119. str(e,hs);
  120. { replace space with + }
  121. if hs[1]=' ' then
  122. hs[1]:='+';
  123. extended2str:='0d'+hs
  124. end;
  125. { convert floating point values }
  126. { to correct endian }
  127. procedure swap64bitarray(var t: t64bitarray);
  128. var
  129. b: byte;
  130. begin
  131. b:= t[7];
  132. t[7] := t[0];
  133. t[0] := b;
  134. b := t[6];
  135. t[6] := t[1];
  136. t[1] := b;
  137. b:= t[5];
  138. t[5] := t[2];
  139. t[2] := b;
  140. b:= t[4];
  141. t[4] := t[3];
  142. t[3] := b;
  143. end;
  144. procedure swap32bitarray(var t: t32bitarray);
  145. var
  146. b: byte;
  147. begin
  148. b:= t[1];
  149. t[1]:= t[2];
  150. t[2]:= b;
  151. b:= t[0];
  152. t[0]:= t[3];
  153. t[3]:= b;
  154. end;
  155. const
  156. ait_const2str : array[aitconst_128bit..aitconst_darwin_dwarf_delta32] of string[20]=(
  157. #9'.fixme128'#9,#9'.quad'#9,#9'.long'#9,#9'.short'#9,#9'.byte'#9,
  158. #9'.sleb128'#9,#9'.uleb128'#9,
  159. #9'.rva'#9,#9'.secrel32'#9,#9'.quad'#9,#9'.long'#9
  160. );
  161. function ReplaceForbiddenChars(const s: string): string;
  162. var
  163. i : longint;
  164. begin
  165. Result:=s;
  166. for i:=1 to Length(Result) do
  167. if Result[i]='$' then
  168. Result[i]:='s';
  169. end;
  170. {****************************************************************************}
  171. { GNU Assembler writer }
  172. {****************************************************************************}
  173. destructor TGNUAssembler.Destroy;
  174. begin
  175. InstrWriter.free;
  176. inherited destroy;
  177. end;
  178. function TGNUAssembler.MakeCmdLine: TCmdStr;
  179. begin
  180. result := inherited MakeCmdLine;
  181. // MWE: disabled again. It generates dwarf info for the generated .s
  182. // files as well. This conflicts with the info we generate
  183. // if target_dbg.id = dbg_dwarf then
  184. // result := result + ' --gdwarf-2';
  185. end;
  186. function TGNUAssembler.NextSetLabel: string;
  187. begin
  188. inc(setcount);
  189. result := target_asm.labelprefix+'$set$'+tostr(setcount);
  190. end;
  191. function is_smart_section(atype:TAsmSectiontype):boolean;
  192. begin
  193. { For bss we need to set some flags that are target dependent,
  194. it is easier to disable it for smartlinking. It doesn't take up
  195. filespace }
  196. result:=not(target_info.system in systems_darwin) and
  197. create_smartlink_sections and
  198. (atype<>sec_toc) and
  199. (atype<>sec_user) and
  200. { on embedded systems every byte counts, so smartlink bss too }
  201. ((atype<>sec_bss) or (target_info.system in systems_embedded));
  202. end;
  203. function TGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  204. const
  205. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  206. '.text',
  207. '.data',
  208. { why doesn't .rodata work? (FK) }
  209. { sometimes we have to create a data.rel.ro instead of .rodata, e.g. for }
  210. { vtables (and anything else containing relocations), otherwise those are }
  211. { not relocated properly on e.g. linux/ppc64. g++ generates there for a }
  212. { vtable for a class called Window: }
  213. { .section .data.rel.ro._ZTV6Window,"awG",@progbits,_ZTV6Window,comdat }
  214. { TODO: .data.ro not yet working}
  215. {$if defined(arm) or defined(powerpc)}
  216. '.rodata',
  217. {$else arm}
  218. '.data',
  219. {$endif arm}
  220. {$if defined(m68k)} { Amiga/m68k GNU AS doesn't seem to like .rodata (KB) }
  221. '.data',
  222. {$else}
  223. '.rodata',
  224. {$endif}
  225. '.bss',
  226. '.threadvar',
  227. '.pdata',
  228. '', { stubs }
  229. '__DATA,__nl_symbol_ptr',
  230. '__DATA,__la_symbol_ptr',
  231. '__DATA,__mod_init_func',
  232. '__DATA,__mod_term_func',
  233. '.stab',
  234. '.stabstr',
  235. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  236. '.eh_frame',
  237. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  238. '.fpc',
  239. '.toc',
  240. '.init',
  241. '.fini',
  242. '.objc_class',
  243. '.objc_meta_class',
  244. '.objc_cat_cls_meth',
  245. '.objc_cat_inst_meth',
  246. '.objc_protocol',
  247. '.objc_string_object',
  248. '.objc_cls_meth',
  249. '.objc_inst_meth',
  250. '.objc_cls_refs',
  251. '.objc_message_refs',
  252. '.objc_symbols',
  253. '.objc_category',
  254. '.objc_class_vars',
  255. '.objc_instance_vars',
  256. '.objc_module_info',
  257. '.objc_class_names',
  258. '.objc_meth_var_types',
  259. '.objc_meth_var_names',
  260. '.objc_selector_strs',
  261. '.objc_protocol_ext',
  262. '.objc_class_ext',
  263. '.objc_property',
  264. '.objc_image_info',
  265. '.objc_cstring_object',
  266. '.objc_sel_fixup',
  267. '__DATA,__objc_data',
  268. '__DATA,__objc_const',
  269. '.objc_superrefs',
  270. '__DATA, __datacoal_nt,coalesced',
  271. '.objc_classlist',
  272. '.objc_nlclasslist',
  273. '.objc_catlist',
  274. '.obcj_nlcatlist',
  275. '.objc_protolist'
  276. );
  277. secnames_pic : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('','',
  278. '.text',
  279. '.data.rel',
  280. '.data.rel',
  281. '.data.rel',
  282. '.bss',
  283. '.threadvar',
  284. '.pdata',
  285. '', { stubs }
  286. '__DATA,__nl_symbol_ptr',
  287. '__DATA,__la_symbol_ptr',
  288. '__DATA,__mod_init_func',
  289. '__DATA,__mod_term_func',
  290. '.stab',
  291. '.stabstr',
  292. '.idata$2','.idata$4','.idata$5','.idata$6','.idata$7','.edata',
  293. '.eh_frame',
  294. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  295. '.fpc',
  296. '.toc',
  297. '.init',
  298. '.fini',
  299. '.objc_class',
  300. '.objc_meta_class',
  301. '.objc_cat_cls_meth',
  302. '.objc_cat_inst_meth',
  303. '.objc_protocol',
  304. '.objc_string_object',
  305. '.objc_cls_meth',
  306. '.objc_inst_meth',
  307. '.objc_cls_refs',
  308. '.objc_message_refs',
  309. '.objc_symbols',
  310. '.objc_category',
  311. '.objc_class_vars',
  312. '.objc_instance_vars',
  313. '.objc_module_info',
  314. '.objc_class_names',
  315. '.objc_meth_var_types',
  316. '.objc_meth_var_names',
  317. '.objc_selector_strs',
  318. '.objc_protocol_ext',
  319. '.objc_class_ext',
  320. '.objc_property',
  321. '.objc_image_info',
  322. '.objc_cstring_object',
  323. '.objc_sel_fixup',
  324. '__DATA, __objc_data',
  325. '__DATA, __objc_const',
  326. '.objc_superrefs',
  327. '__DATA, __datacoal_nt,coalesced',
  328. '.objc_classlist',
  329. '.objc_nlclasslist',
  330. '.objc_catlist',
  331. '.obcj_nlcatlist',
  332. '.objc_protolist'
  333. );
  334. var
  335. sep : string[3];
  336. secname : string;
  337. begin
  338. if (cs_create_pic in current_settings.moduleswitches) and
  339. not(target_info.system in systems_darwin) then
  340. secname:=secnames_pic[atype]
  341. else
  342. secname:=secnames[atype];
  343. {$ifdef m68k}
  344. { old Amiga GNU AS doesn't support .section .fpc }
  345. if (atype=sec_fpc) and (target_info.system = system_m68k_amiga) then
  346. secname:=secnames[sec_data];
  347. {$endif}
  348. if (atype=sec_fpc) and (Copy(aname,1,3)='res') then
  349. begin
  350. result:=secname+'.'+aname;
  351. exit;
  352. end;
  353. if (atype=sec_threadvar) and
  354. (target_info.system=system_i386_win32) then
  355. secname:='.tls';
  356. { go32v2 stub only loads .text and .data sections, and allocates space for .bss.
  357. Thus, data which normally goes into .rodata and .rodata_norel sections must
  358. end up in .data section }
  359. if (atype in [sec_rodata,sec_rodata_norel]) and
  360. (target_info.system=system_i386_go32v2) then
  361. secname:='.data';
  362. { section type user gives the user full controll on the section name }
  363. if atype=sec_user then
  364. secname:=aname;
  365. if is_smart_section(atype) and (aname<>'') then
  366. begin
  367. case aorder of
  368. secorder_begin :
  369. sep:='.b_';
  370. secorder_end :
  371. sep:='.z_';
  372. else
  373. sep:='.n_';
  374. end;
  375. result:=secname+sep+aname
  376. end
  377. else
  378. result:=secname;
  379. end;
  380. function TGNUAssembler.sectionattrs_coff(atype:TAsmSectiontype):string;
  381. begin
  382. case atype of
  383. sec_code, sec_init, sec_fini, sec_stub:
  384. result:='x';
  385. { TODO: must be individual for each section }
  386. sec_user:
  387. result:='d';
  388. sec_data, sec_data_lazy, sec_data_nonlazy, sec_fpc,
  389. sec_idata2, sec_idata4, sec_idata5, sec_idata6, sec_idata7:
  390. result:='d';
  391. { TODO: these need a fix to become read-only }
  392. sec_rodata, sec_rodata_norel:
  393. result:='d';
  394. sec_bss:
  395. result:='b';
  396. { TODO: Somewhat questionable. FPC does not allow initialized threadvars,
  397. so no sense to mark it as containing data. But Windows allows it to
  398. contain data, and Linux even has .tdata and .tbss }
  399. sec_threadvar:
  400. result:='b';
  401. sec_pdata, sec_edata, sec_eh_frame, sec_toc:
  402. result:='r';
  403. sec_stab,sec_stabstr,
  404. sec_debug_frame,sec_debug_info,sec_debug_line,sec_debug_abbrev:
  405. result:='n';
  406. else
  407. result:=''; { defaults to data+load }
  408. end;
  409. end;
  410. procedure TGNUAssembler.WriteSection(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder);
  411. var
  412. s : string;
  413. begin
  414. AsmLn;
  415. case target_info.system of
  416. system_i386_OS2,
  417. system_i386_EMX,
  418. system_m68k_amiga, { amiga has old GNU AS (2.14), which blews up from .section (KB) }
  419. system_m68k_linux: ;
  420. system_powerpc_darwin,
  421. system_i386_darwin,
  422. system_i386_iphonesim,
  423. system_powerpc64_darwin,
  424. system_x86_64_darwin,
  425. system_arm_darwin:
  426. begin
  427. if (atype in [sec_stub,sec_objc_data,sec_objc_const,sec_data_coalesced]) then
  428. AsmWrite('.section ');
  429. end
  430. else
  431. AsmWrite('.section ');
  432. end;
  433. s:=sectionname(atype,aname,aorder);
  434. AsmWrite(s);
  435. case atype of
  436. sec_fpc :
  437. if aname = 'resptrs' then
  438. AsmWrite(', "a", @progbits');
  439. sec_stub :
  440. begin
  441. case target_info.system of
  442. { there are processor-independent shortcuts available }
  443. { for this, namely .symbol_stub and .picsymbol_stub, but }
  444. { they don't work and gcc doesn't use them either... }
  445. system_powerpc_darwin,
  446. system_powerpc64_darwin:
  447. if (cs_create_pic in current_settings.moduleswitches) then
  448. AsmWriteln('__TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32')
  449. else
  450. AsmWriteln('__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,16');
  451. system_i386_darwin,
  452. system_i386_iphonesim:
  453. AsmWriteln('__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5');
  454. system_arm_darwin:
  455. if (cs_create_pic in current_settings.moduleswitches) then
  456. AsmWriteln('.section __TEXT,__picsymbolstub4,symbol_stubs,none,16')
  457. else
  458. AsmWriteln('.section __TEXT,__symbol_stub4,symbol_stubs,none,12')
  459. { darwin/x86-64 uses RIP-based GOT addressing, no symbol stubs }
  460. else
  461. internalerror(2006031101);
  462. end;
  463. end;
  464. else
  465. { GNU AS won't recognize '.text.n_something' section name as belonging
  466. to '.text' and assigns default attributes to it, which is not
  467. always correct. We have to fix it.
  468. TODO: This likely applies to all systems which smartlink without
  469. creating libraries }
  470. if (target_info.system in [system_i386_win32,system_x86_64_win64]) and
  471. is_smart_section(atype) and (aname<>'') then
  472. begin
  473. s:=sectionattrs_coff(atype);
  474. if (s<>'') then
  475. AsmWrite(',"'+s+'"');
  476. end;
  477. end;
  478. AsmLn;
  479. LastSecType:=atype;
  480. end;
  481. procedure TGNUAssembler.WriteDecodedUleb128(a: qword);
  482. var
  483. i,len : longint;
  484. buf : array[0..63] of byte;
  485. begin
  486. len:=EncodeUleb128(a,buf);
  487. for i:=0 to len-1 do
  488. begin
  489. if (i > 0) then
  490. AsmWrite(',');
  491. AsmWrite(tostr(buf[i]));
  492. end;
  493. end;
  494. procedure TGNUAssembler.WriteDecodedSleb128(a: int64);
  495. var
  496. i,len : longint;
  497. buf : array[0..255] of byte;
  498. begin
  499. len:=EncodeSleb128(a,buf);
  500. for i:=0 to len-1 do
  501. begin
  502. if (i > 0) then
  503. AsmWrite(',');
  504. AsmWrite(tostr(buf[i]));
  505. end;
  506. end;
  507. procedure TGNUAssembler.WriteTree(p:TAsmList);
  508. function needsObject(hp : tai_symbol) : boolean;
  509. begin
  510. needsObject :=
  511. (
  512. assigned(hp.next) and
  513. (tai(hp.next).typ in [ait_const,ait_datablock,
  514. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit])
  515. ) or
  516. (hp.sym.typ=AT_DATA);
  517. end;
  518. procedure doalign(alignment: byte; use_op: boolean; fillop: byte; out last_align: longint);
  519. var
  520. i: longint;
  521. begin
  522. last_align:=alignment;
  523. if alignment>1 then
  524. begin
  525. if not(target_info.system in systems_darwin) then
  526. begin
  527. AsmWrite(#9'.balign '+tostr(alignment));
  528. if use_op then
  529. AsmWrite(','+tostr(fillop))
  530. {$ifdef x86}
  531. { force NOP as alignment op code }
  532. else if LastSecType=sec_code then
  533. AsmWrite(',0x90');
  534. {$endif x86}
  535. end
  536. else
  537. begin
  538. { darwin as only supports .align }
  539. if not ispowerof2(alignment,i) then
  540. internalerror(2003010305);
  541. AsmWrite(#9'.align '+tostr(i));
  542. last_align:=i;
  543. end;
  544. AsmLn;
  545. end;
  546. end;
  547. var
  548. ch : char;
  549. hp : tai;
  550. constdef : taiconst_type;
  551. s,t : string;
  552. i,pos,l : longint;
  553. InlineLevel : longint;
  554. last_align : longint;
  555. co : comp;
  556. sin : single;
  557. d : double;
  558. {$ifdef cpuextended}
  559. e : extended;
  560. {$endif cpuextended}
  561. do_line : boolean;
  562. sepChar : char;
  563. begin
  564. if not assigned(p) then
  565. exit;
  566. last_align := 2;
  567. InlineLevel:=0;
  568. { lineinfo is only needed for al_procedures (PFV) }
  569. do_line:=(cs_asm_source in current_settings.globalswitches) or
  570. ((cs_lineinfo in current_settings.moduleswitches)
  571. and (p=current_asmdata.asmlists[al_procedures]));
  572. hp:=tai(p.first);
  573. while assigned(hp) do
  574. begin
  575. prefetch(pointer(hp.next)^);
  576. if not(hp.typ in SkipLineInfo) then
  577. begin
  578. current_filepos:=tailineinfo(hp).fileinfo;
  579. { no line info for inlined code }
  580. if do_line and (inlinelevel=0) then
  581. WriteSourceLine(hp as tailineinfo);
  582. end;
  583. case hp.typ of
  584. ait_comment :
  585. Begin
  586. AsmWrite(target_asm.comment);
  587. AsmWritePChar(tai_comment(hp).str);
  588. AsmLn;
  589. End;
  590. ait_regalloc :
  591. begin
  592. if (cs_asm_regalloc in current_settings.globalswitches) then
  593. begin
  594. AsmWrite(#9+target_asm.comment+'Register ');
  595. repeat
  596. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  597. if (hp.next=nil) or
  598. (tai(hp.next).typ<>ait_regalloc) or
  599. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  600. break;
  601. hp:=tai(hp.next);
  602. AsmWrite(',');
  603. until false;
  604. AsmWrite(' ');
  605. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  606. end;
  607. end;
  608. ait_tempalloc :
  609. begin
  610. if (cs_asm_tempalloc in current_settings.globalswitches) then
  611. WriteTempalloc(tai_tempalloc(hp));
  612. end;
  613. ait_align :
  614. begin
  615. doalign(tai_align_abstract(hp).aligntype,tai_align_abstract(hp).use_op,tai_align_abstract(hp).fillop,last_align);
  616. end;
  617. ait_section :
  618. begin
  619. if tai_section(hp).sectype<>sec_none then
  620. WriteSection(tai_section(hp).sectype,tai_section(hp).name^,tai_section(hp).secorder)
  621. else
  622. begin
  623. {$ifdef EXTDEBUG}
  624. AsmWrite(target_asm.comment);
  625. AsmWriteln(' sec_none');
  626. {$endif EXTDEBUG}
  627. end;
  628. end;
  629. ait_datablock :
  630. begin
  631. if (target_info.system in systems_darwin) then
  632. begin
  633. { On Mac OS X you can't have common symbols in a shared library
  634. since those are in the TEXT section and the text section is
  635. read-only in shared libraries (so it can be shared among different
  636. processes). The alternate code creates some kind of common symbols
  637. in the data segment.
  638. }
  639. if tai_datablock(hp).is_global then
  640. begin
  641. asmwrite('.globl ');
  642. asmwriteln(tai_datablock(hp).sym.name);
  643. asmwriteln('.data');
  644. asmwrite('.zerofill __DATA, __common, ');
  645. asmwrite(tai_datablock(hp).sym.name);
  646. asmwriteln(', '+tostr(tai_datablock(hp).size)+','+tostr(last_align));
  647. if not(LastSecType in [sec_data,sec_none]) then
  648. writesection(LastSecType,'',secorder_default);
  649. end
  650. else
  651. begin
  652. asmwrite(#9'.lcomm'#9);
  653. asmwrite(tai_datablock(hp).sym.name);
  654. asmwrite(','+tostr(tai_datablock(hp).size));
  655. asmwrite(','+tostr(last_align));
  656. asmln;
  657. end;
  658. end
  659. else
  660. begin
  661. {$ifdef USE_COMM_IN_BSS}
  662. if writingpackages then
  663. begin
  664. { The .comm is required for COMMON symbols. These are used
  665. in the shared library loading. All the symbols declared in
  666. the .so file need to resolve to the data allocated in the main
  667. program (PFV) }
  668. if tai_datablock(hp).is_global then
  669. begin
  670. asmwrite(#9'.comm'#9);
  671. asmwrite(tai_datablock(hp).sym.name);
  672. asmwrite(','+tostr(tai_datablock(hp).size));
  673. asmwrite(','+tostr(last_align));
  674. asmln;
  675. end
  676. else
  677. begin
  678. asmwrite(#9'.lcomm'#9);
  679. asmwrite(tai_datablock(hp).sym.name);
  680. asmwrite(','+tostr(tai_datablock(hp).size));
  681. asmwrite(','+tostr(last_align));
  682. asmln;
  683. end
  684. end
  685. else
  686. {$endif USE_COMM_IN_BSS}
  687. begin
  688. if Tai_datablock(hp).is_global then
  689. begin
  690. asmwrite(#9'.globl ');
  691. asmwriteln(Tai_datablock(hp).sym.name);
  692. end;
  693. if (target_info.system <> system_arm_linux) then
  694. sepChar := '@'
  695. else
  696. sepChar := '%';
  697. if (tf_needs_symbol_type in target_info.flags) then
  698. asmwriteln(#9'.type '+Tai_datablock(hp).sym.name+','+sepChar+'object');
  699. if (tf_needs_symbol_size in target_info.flags) and (tai_datablock(hp).size > 0) then
  700. asmwriteln(#9'.size '+Tai_datablock(hp).sym.name+','+tostr(Tai_datablock(hp).size));
  701. asmwrite(Tai_datablock(hp).sym.name);
  702. asmwriteln(':');
  703. asmwriteln(#9'.zero '+tostr(Tai_datablock(hp).size));
  704. end;
  705. end;
  706. end;
  707. ait_const:
  708. begin
  709. constdef:=tai_const(hp).consttype;
  710. case constdef of
  711. {$ifndef cpu64bitaddr}
  712. aitconst_128bit :
  713. begin
  714. internalerror(200404291);
  715. end;
  716. aitconst_64bit :
  717. begin
  718. if assigned(tai_const(hp).sym) then
  719. internalerror(200404292);
  720. AsmWrite(ait_const2str[aitconst_32bit]);
  721. if target_info.endian = endian_little then
  722. begin
  723. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  724. AsmWrite(',');
  725. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  726. end
  727. else
  728. begin
  729. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  730. AsmWrite(',');
  731. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  732. end;
  733. AsmLn;
  734. end;
  735. {$endif cpu64bitaddr}
  736. aitconst_uleb128bit,
  737. aitconst_sleb128bit,
  738. {$ifdef cpu64bitaddr}
  739. aitconst_128bit,
  740. aitconst_64bit,
  741. {$endif cpu64bitaddr}
  742. aitconst_32bit,
  743. aitconst_16bit,
  744. aitconst_8bit,
  745. aitconst_rva_symbol,
  746. aitconst_secrel32_symbol,
  747. aitconst_darwin_dwarf_delta32,
  748. aitconst_darwin_dwarf_delta64:
  749. begin
  750. if (target_info.system in systems_darwin) and
  751. (constdef in [aitconst_uleb128bit,aitconst_sleb128bit]) then
  752. begin
  753. AsmWrite(ait_const2str[aitconst_8bit]);
  754. case tai_const(hp).consttype of
  755. aitconst_uleb128bit:
  756. WriteDecodedUleb128(qword(tai_const(hp).value));
  757. aitconst_sleb128bit:
  758. WriteDecodedSleb128(int64(tai_const(hp).value));
  759. end
  760. end
  761. else
  762. begin
  763. AsmWrite(ait_const2str[constdef]);
  764. l:=0;
  765. t := '';
  766. repeat
  767. if assigned(tai_const(hp).sym) then
  768. begin
  769. if assigned(tai_const(hp).endsym) then
  770. begin
  771. if (constdef in [aitconst_darwin_dwarf_delta32,aitconst_darwin_dwarf_delta64]) then
  772. begin
  773. s := NextSetLabel;
  774. t := #9'.set '+s+','+tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name;
  775. end
  776. else
  777. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  778. end
  779. else
  780. s:=tai_const(hp).sym.name;
  781. {$ifdef avr}
  782. s:=ReplaceForbiddenChars(s);
  783. {$endif avr}
  784. if tai_const(hp).value<>0 then
  785. s:=s+tostr_with_plus(tai_const(hp).value);
  786. end
  787. else
  788. {$ifdef cpu64bitaddr}
  789. s:=tostr(tai_const(hp).value);
  790. {$else cpu64bitaddr}
  791. { 64 bit constants are already handled above in this case }
  792. s:=tostr(longint(tai_const(hp).value));
  793. {$endif cpu64bitaddr}
  794. AsmWrite(s);
  795. inc(l,length(s));
  796. { Values with symbols are written on a single line to improve
  797. reading of the .s file (PFV) }
  798. if assigned(tai_const(hp).sym) or
  799. not(LastSecType in [sec_data,sec_rodata,sec_rodata_norel]) or
  800. (l>line_length) or
  801. (hp.next=nil) or
  802. (tai(hp.next).typ<>ait_const) or
  803. (tai_const(hp.next).consttype<>constdef) or
  804. assigned(tai_const(hp.next).sym) then
  805. break;
  806. hp:=tai(hp.next);
  807. AsmWrite(',');
  808. until false;
  809. if (t <> '') then
  810. begin
  811. AsmLn;
  812. AsmWrite(t);
  813. end;
  814. end;
  815. AsmLn;
  816. end;
  817. else
  818. internalerror(200704251);
  819. end;
  820. end;
  821. { the "and defined(FPC_HAS_TYPE_EXTENDED)" isn't optimal but currently the only solution
  822. it prevents proper cross compilation to i386 though
  823. }
  824. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  825. ait_real_80bit :
  826. begin
  827. if do_line then
  828. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  829. { Make sure e is a extended type, bestreal could be
  830. a different type (bestreal) !! (PFV) }
  831. e:=tai_real_80bit(hp).value;
  832. AsmWrite(#9'.byte'#9);
  833. for i:=0 to 9 do
  834. begin
  835. if i<>0 then
  836. AsmWrite(',');
  837. AsmWrite(tostr(t80bitarray(e)[i]));
  838. end;
  839. for i:=11 to tai_real_80bit(hp).savesize do
  840. AsmWrite(',0');
  841. AsmLn;
  842. end;
  843. {$endif cpuextended}
  844. ait_real_64bit :
  845. begin
  846. if do_line then
  847. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  848. d:=tai_real_64bit(hp).value;
  849. { swap the values to correct endian if required }
  850. if source_info.endian <> target_info.endian then
  851. swap64bitarray(t64bitarray(d));
  852. AsmWrite(#9'.byte'#9);
  853. {$ifdef arm}
  854. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  855. begin
  856. for i:=4 to 7 do
  857. begin
  858. if i<>4 then
  859. AsmWrite(',');
  860. AsmWrite(tostr(t64bitarray(d)[i]));
  861. end;
  862. for i:=0 to 3 do
  863. begin
  864. AsmWrite(',');
  865. AsmWrite(tostr(t64bitarray(d)[i]));
  866. end;
  867. end
  868. else
  869. {$endif arm}
  870. begin
  871. for i:=0 to 7 do
  872. begin
  873. if i<>0 then
  874. AsmWrite(',');
  875. AsmWrite(tostr(t64bitarray(d)[i]));
  876. end;
  877. end;
  878. AsmLn;
  879. end;
  880. ait_real_32bit :
  881. begin
  882. if do_line then
  883. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  884. sin:=tai_real_32bit(hp).value;
  885. { swap the values to correct endian if required }
  886. if source_info.endian <> target_info.endian then
  887. swap32bitarray(t32bitarray(sin));
  888. AsmWrite(#9'.byte'#9);
  889. for i:=0 to 3 do
  890. begin
  891. if i<>0 then
  892. AsmWrite(',');
  893. AsmWrite(tostr(t32bitarray(sin)[i]));
  894. end;
  895. AsmLn;
  896. end;
  897. ait_comp_64bit :
  898. begin
  899. if do_line then
  900. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  901. AsmWrite(#9'.byte'#9);
  902. co:=comp(tai_comp_64bit(hp).value);
  903. { swap the values to correct endian if required }
  904. if source_info.endian <> target_info.endian then
  905. swap64bitarray(t64bitarray(co));
  906. for i:=0 to 7 do
  907. begin
  908. if i<>0 then
  909. AsmWrite(',');
  910. AsmWrite(tostr(t64bitarray(co)[i]));
  911. end;
  912. AsmLn;
  913. end;
  914. ait_string :
  915. begin
  916. pos:=0;
  917. for i:=1 to tai_string(hp).len do
  918. begin
  919. if pos=0 then
  920. begin
  921. AsmWrite(#9'.ascii'#9'"');
  922. pos:=20;
  923. end;
  924. ch:=tai_string(hp).str[i-1];
  925. case ch of
  926. #0, {This can't be done by range, because a bug in FPC}
  927. #1..#31,
  928. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  929. '"' : s:='\"';
  930. '\' : s:='\\';
  931. else
  932. s:=ch;
  933. end;
  934. AsmWrite(s);
  935. inc(pos,length(s));
  936. if (pos>line_length) or (i=tai_string(hp).len) then
  937. begin
  938. AsmWriteLn('"');
  939. pos:=0;
  940. end;
  941. end;
  942. end;
  943. ait_label :
  944. begin
  945. if (tai_label(hp).labsym.is_used) then
  946. begin
  947. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  948. begin
  949. AsmWrite(#9'.private_extern ');
  950. AsmWriteln(tai_label(hp).labsym.name);
  951. end;
  952. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  953. begin
  954. AsmWrite('.globl'#9);
  955. {$ifdef avr}
  956. AsmWriteLn(ReplaceForbiddenChars(tai_label(hp).labsym.name));
  957. {$else avr}
  958. AsmWriteLn(tai_label(hp).labsym.name);
  959. {$endif avr}
  960. end;
  961. {$ifdef avr}
  962. AsmWrite(ReplaceForbiddenChars(tai_label(hp).labsym.name));
  963. {$else avr}
  964. AsmWrite(tai_label(hp).labsym.name);
  965. {$endif avr}
  966. AsmWriteLn(':');
  967. end;
  968. end;
  969. ait_symbol :
  970. begin
  971. if (tai_symbol(hp).sym.bind=AB_PRIVATE_EXTERN) then
  972. begin
  973. AsmWrite(#9'.private_extern ');
  974. {$ifdef avr}
  975. AsmWriteln(ReplaceForbiddenChars(tai_symbol(hp).sym.name));
  976. {$else avr}
  977. AsmWriteln(tai_symbol(hp).sym.name);
  978. {$endif avr}
  979. end;
  980. if (target_info.system = system_powerpc64_linux) and
  981. (tai_symbol(hp).sym.typ = AT_FUNCTION) and (cs_profile in current_settings.moduleswitches) then
  982. AsmWriteLn('.globl _mcount');
  983. if tai_symbol(hp).is_global then
  984. begin
  985. AsmWrite('.globl'#9);
  986. {$ifdef avr}
  987. AsmWriteln(ReplaceForbiddenChars(tai_symbol(hp).sym.name));
  988. {$else avr}
  989. AsmWriteln(tai_symbol(hp).sym.name);
  990. {$endif avr}
  991. end;
  992. if (target_info.system = system_powerpc64_linux) and
  993. (tai_symbol(hp).sym.typ = AT_FUNCTION) then
  994. begin
  995. AsmWriteLn('.section ".opd", "aw"');
  996. AsmWriteLn('.align 3');
  997. AsmWriteLn(tai_symbol(hp).sym.name + ':');
  998. AsmWriteLn('.quad .' + tai_symbol(hp).sym.name + ', .TOC.@tocbase, 0');
  999. AsmWriteLn('.previous');
  1000. AsmWriteLn('.size ' + tai_symbol(hp).sym.name + ', 24');
  1001. if (tai_symbol(hp).is_global) then
  1002. AsmWriteLn('.globl .' + tai_symbol(hp).sym.name);
  1003. AsmWriteLn('.type .' + tai_symbol(hp).sym.name + ', @function');
  1004. { the dotted name is the name of the actual function entry }
  1005. AsmWrite('.');
  1006. end
  1007. else
  1008. begin
  1009. if (target_info.system <> system_arm_linux) then
  1010. sepChar := '@'
  1011. else
  1012. sepChar := '#';
  1013. if (tf_needs_symbol_type in target_info.flags) then
  1014. begin
  1015. AsmWrite(#9'.type'#9 + tai_symbol(hp).sym.name);
  1016. if (needsObject(tai_symbol(hp))) then
  1017. AsmWriteLn(',' + sepChar + 'object')
  1018. else
  1019. AsmWriteLn(',' + sepChar + 'function');
  1020. end;
  1021. end;
  1022. {$ifdef avr}
  1023. if not(tai_symbol(hp).has_value) then
  1024. AsmWriteLn(ReplaceForbiddenChars(tai_symbol(hp).sym.name + ':'))
  1025. else
  1026. AsmWriteLn(ReplaceForbiddenChars(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value)));
  1027. {$else avr}
  1028. if not(tai_symbol(hp).has_value) then
  1029. AsmWriteLn(tai_symbol(hp).sym.name + ':')
  1030. else
  1031. AsmWriteLn(tai_symbol(hp).sym.name + '=' + tostr(tai_symbol(hp).value));
  1032. {$endif avr}
  1033. end;
  1034. {$ifdef arm}
  1035. ait_thumb_func:
  1036. begin
  1037. AsmWriteLn(#9'.thumb_func');
  1038. end;
  1039. {$endif arm}
  1040. ait_symbol_end :
  1041. begin
  1042. if tf_needs_symbol_size in target_info.flags then
  1043. begin
  1044. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  1045. inc(symendcount);
  1046. AsmWriteLn(s+':');
  1047. AsmWrite(#9'.size'#9);
  1048. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  1049. AsmWrite('.');
  1050. {$ifdef avr}
  1051. AsmWrite(ReplaceForbiddenChars(tai_symbol_end(hp).sym.name));
  1052. {$else avr}
  1053. AsmWrite(tai_symbol_end(hp).sym.name);
  1054. {$endif avr}
  1055. AsmWrite(', '+s+' - ');
  1056. if (target_info.system = system_powerpc64_linux) and (tai_symbol_end(hp).sym.typ = AT_FUNCTION) then
  1057. AsmWrite('.');
  1058. {$ifdef avr}
  1059. AsmWriteLn(ReplaceForbiddenChars(tai_symbol_end(hp).sym.name));
  1060. {$else avr}
  1061. AsmWriteLn(tai_symbol_end(hp).sym.name);
  1062. {$endif avr}
  1063. end;
  1064. end;
  1065. ait_instruction :
  1066. begin
  1067. WriteInstruction(hp);
  1068. end;
  1069. ait_stab :
  1070. begin
  1071. if assigned(tai_stab(hp).str) then
  1072. begin
  1073. AsmWrite(#9'.'+stabtypestr[tai_stab(hp).stabtype]+' ');
  1074. AsmWritePChar(tai_stab(hp).str);
  1075. AsmLn;
  1076. end;
  1077. end;
  1078. ait_force_line,
  1079. ait_function_name :
  1080. ;
  1081. ait_cutobject :
  1082. begin
  1083. if SmartAsm then
  1084. begin
  1085. { only reset buffer if nothing has changed }
  1086. if AsmSize=AsmStartSize then
  1087. AsmClear
  1088. else
  1089. begin
  1090. AsmClose;
  1091. DoAssemble;
  1092. AsmCreate(tai_cutobject(hp).place);
  1093. end;
  1094. { avoid empty files }
  1095. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  1096. begin
  1097. if tai(hp.next).typ=ait_section then
  1098. LastSecType:=tai_section(hp.next).sectype;
  1099. hp:=tai(hp.next);
  1100. end;
  1101. if LastSecType<>sec_none then
  1102. WriteSection(LastSecType,'',secorder_default);
  1103. AsmStartSize:=AsmSize;
  1104. end;
  1105. end;
  1106. ait_marker :
  1107. if tai_marker(hp).kind=mark_NoLineInfoStart then
  1108. inc(InlineLevel)
  1109. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  1110. dec(InlineLevel);
  1111. ait_directive :
  1112. begin
  1113. AsmWrite('.'+directivestr[tai_directive(hp).directive]+' ');
  1114. if assigned(tai_directive(hp).name) then
  1115. AsmWrite(tai_directive(hp).name^);
  1116. AsmLn;
  1117. end;
  1118. else
  1119. internalerror(2006012201);
  1120. end;
  1121. hp:=tai(hp.next);
  1122. end;
  1123. end;
  1124. procedure TGNUAssembler.WriteExtraHeader;
  1125. begin
  1126. end;
  1127. procedure TGNUAssembler.WriteInstruction(hp: tai);
  1128. begin
  1129. InstrWriter.WriteInstruction(hp);
  1130. end;
  1131. procedure TGNUAssembler.WriteWeakSymbolDef(s: tasmsymbol);
  1132. begin
  1133. AsmWriteLn(#9'.weak '+s.name);
  1134. end;
  1135. procedure TGNUAssembler.WriteAsmList;
  1136. var
  1137. n : string;
  1138. hal : tasmlisttype;
  1139. i: longint;
  1140. begin
  1141. {$ifdef EXTDEBUG}
  1142. if assigned(current_module.mainsource) then
  1143. Comment(V_Debug,'Start writing gas-styled assembler output for '+current_module.mainsource^);
  1144. {$endif}
  1145. if assigned(current_module.mainsource) then
  1146. n:=ExtractFileName(current_module.mainsource^)
  1147. else
  1148. n:=InputFileName;
  1149. { gcc does not add it either for Darwin (and AIX). Grep for
  1150. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  1151. }
  1152. if not(target_info.system in systems_darwin) then
  1153. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  1154. WriteExtraHeader;
  1155. AsmStartSize:=AsmSize;
  1156. symendcount:=0;
  1157. for hal:=low(TasmlistType) to high(TasmlistType) do
  1158. begin
  1159. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1160. writetree(current_asmdata.asmlists[hal]);
  1161. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1162. end;
  1163. { add weak symbol markers }
  1164. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  1165. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  1166. writeweaksymboldef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  1167. if create_smartlink_sections and
  1168. (target_info.system in systems_darwin) then
  1169. AsmWriteLn(#9'.subsections_via_symbols');
  1170. { "no executable stack" marker for Linux }
  1171. if (target_info.system in systems_linux) and
  1172. not(cs_executable_stack in current_settings.moduleswitches) then
  1173. begin
  1174. AsmWriteLn('.section .note.GNU-stack,"",%progbits');
  1175. end;
  1176. AsmLn;
  1177. {$ifdef EXTDEBUG}
  1178. if assigned(current_module.mainsource) then
  1179. Comment(V_Debug,'Done writing gas-styled assembler output for '+current_module.mainsource^);
  1180. {$endif EXTDEBUG}
  1181. end;
  1182. {****************************************************************************}
  1183. { Apple/GNU Assembler writer }
  1184. {****************************************************************************}
  1185. function TAppleGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1186. begin
  1187. if (target_info.system in systems_darwin) then
  1188. case atype of
  1189. sec_bss:
  1190. { all bss (lcomm) symbols are automatically put in the right }
  1191. { place by using the lcomm assembler directive }
  1192. atype := sec_none;
  1193. sec_debug_frame,
  1194. sec_eh_frame:
  1195. begin
  1196. result := '.section __DWARF,__debug_info,regular,debug';
  1197. exit;
  1198. end;
  1199. sec_debug_line:
  1200. begin
  1201. result := '.section __DWARF,__debug_line,regular,debug';
  1202. exit;
  1203. end;
  1204. sec_debug_info:
  1205. begin
  1206. result := '.section __DWARF,__debug_info,regular,debug';
  1207. exit;
  1208. end;
  1209. sec_debug_abbrev:
  1210. begin
  1211. result := '.section __DWARF,__debug_abbrev,regular,debug';
  1212. exit;
  1213. end;
  1214. sec_rodata:
  1215. begin
  1216. result := '.const_data';
  1217. exit;
  1218. end;
  1219. sec_rodata_norel:
  1220. begin
  1221. result := '.const';
  1222. exit;
  1223. end;
  1224. sec_fpc:
  1225. begin
  1226. result := '.section __TEXT, .fpc, regular, no_dead_strip';
  1227. exit;
  1228. end;
  1229. sec_code:
  1230. begin
  1231. if (aname='fpc_geteipasebx') or
  1232. (aname='fpc_geteipasecx') then
  1233. begin
  1234. result:='.section __TEXT,__textcoal_nt,coalesced,pure_instructions'#10'.weak_definition '+aname+
  1235. #10'.private_extern '+aname;
  1236. exit;
  1237. end;
  1238. end;
  1239. sec_data_nonlazy:
  1240. begin
  1241. result:='.section __DATA, __nl_symbol_ptr,non_lazy_symbol_pointers';
  1242. exit;
  1243. end;
  1244. sec_data_lazy:
  1245. begin
  1246. result:='.section __DATA, __la_symbol_ptr,lazy_symbol_pointers';
  1247. exit;
  1248. end;
  1249. sec_init_func:
  1250. begin
  1251. result:='.section __DATA, __mod_init_func, mod_init_funcs';
  1252. exit;
  1253. end;
  1254. sec_term_func:
  1255. begin
  1256. result:='.section __DATA, __mod_term_func, mod_term_funcs';
  1257. exit;
  1258. end;
  1259. sec_objc_protocol_ext:
  1260. begin
  1261. result:='.section __OBJC, __protocol_ext, regular, no_dead_strip';
  1262. exit;
  1263. end;
  1264. sec_objc_class_ext:
  1265. begin
  1266. result:='.section __OBJC, __class_ext, regular, no_dead_strip';
  1267. exit;
  1268. end;
  1269. sec_objc_property:
  1270. begin
  1271. result:='.section __OBJC, __property, regular, no_dead_strip';
  1272. exit;
  1273. end;
  1274. sec_objc_image_info:
  1275. begin
  1276. result:='.section __OBJC, __image_info, regular, no_dead_strip';
  1277. exit;
  1278. end;
  1279. sec_objc_cstring_object:
  1280. begin
  1281. result:='.section __OBJC, __cstring_object, regular, no_dead_strip';
  1282. exit;
  1283. end;
  1284. sec_objc_sel_fixup:
  1285. begin
  1286. result:='.section __OBJC, __sel_fixup, regular, no_dead_strip';
  1287. exit;
  1288. end;
  1289. sec_objc_message_refs:
  1290. begin
  1291. if (target_info.system in systems_objc_nfabi) then
  1292. begin
  1293. result:='.section __DATA, __objc_selrefs, literal_pointers, no_dead_strip';
  1294. exit;
  1295. end;
  1296. end;
  1297. sec_objc_cls_refs:
  1298. begin
  1299. if (target_info.system in systems_objc_nfabi) then
  1300. begin
  1301. result:='.section __DATA, __objc_clsrefs, regular, no_dead_strip';
  1302. exit;
  1303. end;
  1304. end;
  1305. sec_objc_meth_var_names,
  1306. sec_objc_class_names:
  1307. begin
  1308. if (target_info.system in systems_objc_nfabi) then
  1309. begin
  1310. result:='.cstring';
  1311. exit
  1312. end;
  1313. end;
  1314. sec_objc_inst_meth,
  1315. sec_objc_cls_meth,
  1316. sec_objc_cat_inst_meth,
  1317. sec_objc_cat_cls_meth:
  1318. begin
  1319. if (target_info.system in systems_objc_nfabi) then
  1320. begin
  1321. result:='.section __DATA, __objc_const';
  1322. exit;
  1323. end;
  1324. end;
  1325. sec_objc_meta_class,
  1326. sec_objc_class:
  1327. begin
  1328. if (target_info.system in systems_objc_nfabi) then
  1329. begin
  1330. result:='.section __DATA, __objc_data';
  1331. exit;
  1332. end;
  1333. end;
  1334. sec_objc_sup_refs:
  1335. begin
  1336. result:='.section __DATA, __objc_superrefs, regular, no_dead_strip';
  1337. exit
  1338. end;
  1339. sec_objc_classlist:
  1340. begin
  1341. result:='.section __DATA, __objc_classlist, regular, no_dead_strip';
  1342. exit
  1343. end;
  1344. sec_objc_nlclasslist:
  1345. begin
  1346. result:='.section __DATA, __objc_nlclasslist, regular, no_dead_strip';
  1347. exit
  1348. end;
  1349. sec_objc_catlist:
  1350. begin
  1351. result:='.section __DATA, __objc_catlist, regular, no_dead_strip';
  1352. exit
  1353. end;
  1354. sec_objc_nlcatlist:
  1355. begin
  1356. result:='.section __DATA, __objc_nlcatlist, regular, no_dead_strip';
  1357. exit
  1358. end;
  1359. sec_objc_protolist:
  1360. begin
  1361. result:='.section __DATA, __objc_protolist, coalesced, no_dead_strip';
  1362. exit;
  1363. end;
  1364. end;
  1365. result := inherited sectionname(atype,aname,aorder);
  1366. end;
  1367. procedure TAppleGNUAssembler.WriteWeakSymbolDef(s: tasmsymbol);
  1368. begin
  1369. AsmWriteLn(#9'.weak_reference '+s.name);
  1370. end;
  1371. {****************************************************************************}
  1372. { a.out/GNU Assembler writer }
  1373. {****************************************************************************}
  1374. function TAoutGNUAssembler.sectionname(atype:TAsmSectiontype;const aname:string;aorder:TAsmSectionOrder):string;
  1375. const
  1376. (* Translation table - replace unsupported section types with basic ones. *)
  1377. SecXTable: array[TAsmSectionType] of TAsmSectionType = (
  1378. sec_none,
  1379. sec_none,
  1380. sec_code,
  1381. sec_data,
  1382. sec_data (* sec_rodata *),
  1383. sec_data (* sec_rodata_norel *),
  1384. sec_bss,
  1385. sec_data (* sec_threadvar *),
  1386. { used for wince exception handling }
  1387. sec_code (* sec_pdata *),
  1388. { used for darwin import stubs }
  1389. sec_code (* sec_stub *),
  1390. sec_data,(* sec_data_nonlazy *)
  1391. sec_data,(* sec_data_lazy *)
  1392. sec_data,(* sec_init_func *)
  1393. sec_data,(* sec_term_func *)
  1394. { stabs }
  1395. sec_stab,sec_stabstr,
  1396. { win32 }
  1397. sec_data (* sec_idata2 *),
  1398. sec_data (* sec_idata4 *),
  1399. sec_data (* sec_idata5 *),
  1400. sec_data (* sec_idata6 *),
  1401. sec_data (* sec_idata7 *),
  1402. sec_data (* sec_edata *),
  1403. { C++ exception handling unwinding (uses dwarf) }
  1404. sec_eh_frame,
  1405. { dwarf }
  1406. sec_debug_frame,
  1407. sec_debug_info,
  1408. sec_debug_line,
  1409. sec_debug_abbrev,
  1410. { ELF resources (+ references to stabs debug information sections) }
  1411. sec_code (* sec_fpc *),
  1412. { Table of contents section }
  1413. sec_code (* sec_toc *),
  1414. sec_code (* sec_init *),
  1415. sec_code (* sec_fini *),
  1416. sec_none (* sec_objc_class *),
  1417. sec_none (* sec_objc_meta_class *),
  1418. sec_none (* sec_objc_cat_cls_meth *),
  1419. sec_none (* sec_objc_cat_inst_meth *),
  1420. sec_none (* sec_objc_protocol *),
  1421. sec_none (* sec_objc_string_object *),
  1422. sec_none (* sec_objc_cls_meth *),
  1423. sec_none (* sec_objc_inst_meth *),
  1424. sec_none (* sec_objc_cls_refs *),
  1425. sec_none (* sec_objc_message_refs *),
  1426. sec_none (* sec_objc_symbols *),
  1427. sec_none (* sec_objc_category *),
  1428. sec_none (* sec_objc_class_vars *),
  1429. sec_none (* sec_objc_instance_vars *),
  1430. sec_none (* sec_objc_module_info *),
  1431. sec_none (* sec_objc_class_names *),
  1432. sec_none (* sec_objc_meth_var_types *),
  1433. sec_none (* sec_objc_meth_var_names *),
  1434. sec_none (* sec_objc_selector_strs *),
  1435. sec_none (* sec_objc_protocol_ext *),
  1436. sec_none (* sec_objc_class_ext *),
  1437. sec_none (* sec_objc_property *),
  1438. sec_none (* sec_objc_image_info *),
  1439. sec_none (* sec_objc_cstring_object *),
  1440. sec_none (* sec_objc_sel_fixup *),
  1441. sec_none (* sec_objc_data *),
  1442. sec_none (* sec_objc_const *),
  1443. sec_none (* sec_objc_sup_refs *),
  1444. sec_none (* sec_data_coalesced *),
  1445. sec_none (* sec_objc_classlist *),
  1446. sec_none (* sec_objc_nlclasslist *),
  1447. sec_none (* sec_objc_catlist *),
  1448. sec_none (* sec_objc_nlcatlist *),
  1449. sec_none (* sec_objc_protlist *)
  1450. );
  1451. begin
  1452. Result := inherited SectionName (SecXTable [AType], AName, AOrder);
  1453. end;
  1454. {****************************************************************************}
  1455. { Abstract Instruction Writer }
  1456. {****************************************************************************}
  1457. constructor TCPUInstrWriter.create(_owner: TGNUAssembler);
  1458. begin
  1459. inherited create;
  1460. owner := _owner;
  1461. end;
  1462. end.