aggas.pas 54 KB

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