cfidwarf.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. {
  2. Copyright (c) 2003-2004 by Peter Vreman and Florian Klaempfl
  3. This units contains special support for DWARF debug info
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cfidwarf;
  18. {$i fpcdefs.inc}
  19. { $define debug_eh}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,
  24. cgbase,cpubase,
  25. aasmbase,aasmtai,aasmdata;
  26. const
  27. maxdwarfops = 2;
  28. type
  29. tdwarfoperenc=(doe_uleb,doe_sleb,doe_ptr,doe_32bit,doe_16bit,doe_8bit);
  30. tdwarfopertype=(dop_reg,dop_const,dop_sym,dop_reloffset);
  31. tdwarfoper=record
  32. enc : tdwarfoperenc;
  33. case typ:tdwarfopertype of
  34. dop_reg : (register:tregister);
  35. dop_const : (value:int64);
  36. dop_sym : (sym:tasmsymbol);
  37. dop_reloffset : (beginsym,endsym:tasmsymbol);
  38. end;
  39. tdwarfitem=class(TLinkedListItem)
  40. op : byte;
  41. ops : byte;
  42. oper : array[0..maxdwarfops-1] of tdwarfoper;
  43. constructor create(aop:byte);
  44. constructor create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  45. constructor create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  46. constructor create_sym(aop: byte; enc1: tdwarfoperenc; sym: TAsmSymbol);
  47. constructor create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  48. constructor create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  49. procedure generate_code(list:TAsmList);
  50. end;
  51. TDwarfAsmCFI=class(TAsmCFI)
  52. private
  53. FDwarfList : TLinkedList;
  54. FFrameStartLabel,
  55. FFrameEndLabel,
  56. FLastloclabel : tasmlabel;
  57. procedure cfa_advance_loc(list:TAsmList);
  58. procedure generate_initial_instructions(list:TAsmList);virtual;
  59. protected
  60. code_alignment_factor,
  61. data_alignment_factor : shortint;
  62. property DwarfList:TlinkedList read FDwarfList;
  63. public
  64. LSDALabel : TAsmLabel;
  65. use_eh_frame : boolean;
  66. constructor create;override;
  67. destructor destroy;override;
  68. procedure generate_code(list:TAsmList);override;
  69. function get_frame_start: TAsmLabel;
  70. { operations }
  71. procedure start_frame(list:TAsmList);override;
  72. procedure end_frame(list:TAsmList);override;
  73. procedure outmost_frame(list: TAsmList);override;
  74. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);override;
  75. procedure cfa_restore(list:TAsmList;reg:tregister);override;
  76. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);override;
  77. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);override;
  78. end;
  79. implementation
  80. uses
  81. systems,
  82. cutils,
  83. verbose,
  84. dwarfbase;
  85. {****************************************************************************
  86. TDWARFITEM
  87. ****************************************************************************}
  88. constructor tdwarfitem.create(aop:byte);
  89. begin
  90. inherited create;
  91. op:=aop;
  92. ops:=0;
  93. end;
  94. constructor tdwarfitem.create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  95. begin
  96. inherited create;
  97. op:=aop;
  98. ops:=1;
  99. oper[0].typ:=dop_reg;
  100. oper[0].enc:=enc1;
  101. oper[0].register:=reg;
  102. end;
  103. constructor tdwarfitem.create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  104. begin
  105. inherited create;
  106. op:=aop;
  107. ops:=1;
  108. oper[0].typ:=dop_const;
  109. oper[0].enc:=enc1;
  110. oper[0].value:=val;
  111. end;
  112. constructor tdwarfitem.create_sym(aop:byte;enc1:tdwarfoperenc;sym:TAsmSymbol);
  113. begin
  114. inherited create;
  115. op:=aop;
  116. ops:=1;
  117. oper[0].typ:=dop_sym;
  118. oper[0].enc:=enc1;
  119. oper[0].sym:=sym;
  120. end;
  121. constructor tdwarfitem.create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  122. begin
  123. inherited create;
  124. op:=aop;
  125. ops:=1;
  126. { relative offsets are passed }
  127. oper[0].typ:=dop_reloffset;
  128. oper[0].enc:=enc1;
  129. oper[0].beginsym:=beginlab;
  130. oper[0].endsym:=endlab;
  131. end;
  132. constructor tdwarfitem.create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  133. begin
  134. inherited create;
  135. op:=aop;
  136. ops:=2;
  137. oper[0].typ:=dop_reg;
  138. oper[0].enc:=enc1;
  139. oper[0].register:=reg;
  140. oper[1].typ:=dop_const;
  141. oper[1].enc:=enc2;
  142. oper[1].value:=val;
  143. end;
  144. procedure tdwarfitem.generate_code(list:TAsmList);
  145. const
  146. enc2ait_const : array[tdwarfoperenc] of taiconst_type = (
  147. aitconst_uleb128bit,aitconst_sleb128bit,aitconst_ptr,
  148. aitconst_32bit,aitconst_16bit,aitconst_8bit
  149. );
  150. var
  151. i : integer;
  152. begin
  153. list.concat(tai_const.create_8bit(op));
  154. for i:=0 to ops-1 do
  155. begin
  156. case oper[i].typ of
  157. dop_const :
  158. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  159. dop_sym :
  160. begin
  161. if oper[i].enc<>doe_ptr then
  162. internalerror(200404127);
  163. list.concat(tai_const.create_sym(oper[i].sym));
  164. end;
  165. dop_reloffset :
  166. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  167. dop_reg :
  168. list.concat(tai_const.create(enc2ait_const[oper[i].enc],dwarf_reg(oper[i].register)));
  169. else
  170. internalerror(200404128);
  171. end;
  172. end;
  173. end;
  174. {****************************************************************************
  175. TDwarfAsmCFI
  176. ****************************************************************************}
  177. constructor TDwarfAsmCFI.create;
  178. begin
  179. inherited create;
  180. FFrameStartLabel:=nil;
  181. FFrameEndLabel:=nil;
  182. FLastLocLabel:=nil;
  183. code_alignment_factor:=1;
  184. data_alignment_factor:=-4;
  185. FDwarfList:=TLinkedList.Create;
  186. if tf_use_psabieh in target_info.flags then
  187. use_eh_frame:=true;
  188. end;
  189. destructor TDwarfAsmCFI.destroy;
  190. begin
  191. FDwarfList.Free;
  192. end;
  193. {$ifdef i386}
  194. { if more cpu dependend stuff is implemented, this needs more refactoring }
  195. procedure TDwarfAsmCFI.generate_initial_instructions(list:TAsmList);
  196. begin
  197. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  198. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  199. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  200. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  201. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  202. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  203. end;
  204. {$else i386}
  205. { if more cpu dependend stuff is implemented, this needs more refactoring }
  206. procedure TDwarfAsmCFI.generate_initial_instructions(list:TAsmList);
  207. begin
  208. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  209. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  210. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  211. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  212. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  213. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  214. end;
  215. {$endif i386}
  216. procedure TDwarfAsmCFI.generate_code(list:TAsmList);
  217. var
  218. hp : tdwarfitem;
  219. CurrentLSDALabel,
  220. cielabel,
  221. lenstartlabel,
  222. lenendlabel,
  223. augendlabel,
  224. augstartlabel,
  225. fdeofslabel, curpos: tasmlabel;
  226. tc : tai_const;
  227. begin
  228. CurrentLSDALabel:=nil;
  229. if use_eh_frame then
  230. new_section(list,sec_eh_frame,'',0)
  231. else
  232. new_section(list,sec_debug_frame,'',0);
  233. { debug_frame:
  234. CIE
  235. DWORD length
  236. DWORD CIE_Id = 0xffffffff
  237. BYTE version = 1
  238. STRING augmentation = "" = BYTE 0
  239. ULEB128 code alignment factor = 1
  240. ULEB128 data alignment factor = -1
  241. BYTE return address register
  242. <...> augmentation
  243. <...> start sequence
  244. eh_frame:
  245. CIE
  246. DWORD length
  247. DWORD CIE_Id = 0
  248. BYTE version = 1
  249. STRING augmentation = 'zPLR'#0
  250. ULEB128 code alignment factor = 1
  251. ULEB128 data alignment factor = -1
  252. BYTE return address register
  253. <...> start sequence
  254. }
  255. current_asmdata.getlabel(cielabel,alt_dbgframe);
  256. list.concat(tai_label.create(cielabel));
  257. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  258. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  259. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  260. list.concat(tai_label.create(lenstartlabel));
  261. if use_eh_frame then
  262. begin
  263. list.concat(tai_const.create_32bit(0));
  264. list.concat(tai_const.create_8bit(1));
  265. list.concat(tai_const.create_8bit(ord('z')));
  266. list.concat(tai_const.create_8bit(ord('P')));
  267. list.concat(tai_const.create_8bit(ord('L')));
  268. list.concat(tai_const.create_8bit(ord('R')));
  269. list.concat(tai_const.create_8bit(0));
  270. end
  271. else
  272. begin
  273. list.concat(tai_const.create_32bit(longint($ffffffff)));
  274. list.concat(tai_const.create_8bit(1));
  275. list.concat(tai_const.create_8bit(0)); { empty string }
  276. end;
  277. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  278. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  279. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  280. { augmentation data }
  281. if use_eh_frame then
  282. begin
  283. current_asmdata.getlabel(augstartlabel,alt_dbgframe);
  284. current_asmdata.getlabel(augendlabel,alt_dbgframe);
  285. { size of augmentation data ('z') }
  286. list.concat(tai_const.create_rel_sym(aitconst_uleb128bit,augstartlabel,augendlabel));
  287. list.concat(tai_label.create(augstartlabel));
  288. { personality function ('P') }
  289. { encoding }
  290. list.concat(tai_const.create_8bit({DW_EH_PE_indirect or DW_EH_PE_pcrel or} DW_EH_PE_sdata4));
  291. { address of personality function }
  292. list.concat(tai_const.Createname('_fpc_psabieh_personality_v0',AT_FUNCTION,0));
  293. { LSDA encoding ('L')}
  294. list.concat(tai_const.create_8bit({DW_EH_PE_pcrel or }DW_EH_PE_sdata4));
  295. { FDE encoding ('R') }
  296. list.concat(tai_const.create_8bit({DW_EH_PE_pcrel or }DW_EH_PE_sdata4));
  297. list.concat(tai_label.create(augendlabel));
  298. end;
  299. { Generate standard code
  300. def_cfa(stackpointer,sizeof(aint))
  301. cfa_offset_extended(returnaddres,-sizeof(aint))
  302. }
  303. generate_initial_instructions(list);
  304. list.concat(cai_align.create_zeros(4));
  305. list.concat(tai_label.create(lenendlabel));
  306. lenstartlabel:=nil;
  307. lenendlabel:=nil;
  308. hp:=TDwarfItem(DwarfList.first);
  309. while assigned(hp) do
  310. begin
  311. case hp.op of
  312. DW_CFA_Start_Frame :
  313. begin
  314. if assigned(lenstartlabel) then
  315. internalerror(200404125);
  316. if (hp.ops<>1) or
  317. (hp.oper[0].typ<>dop_reloffset) then
  318. internalerror(200404126);
  319. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  320. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  321. { FDE
  322. DWORD length
  323. DWORD CIE-pointer = cielabel relative to section start
  324. PTRSIZE initial location = oper[0]
  325. PTRSIZE function size = oper[1]
  326. }
  327. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  328. list.concat(tai_label.create(lenstartlabel));
  329. if use_eh_frame then
  330. begin
  331. { relative offset to the CIE }
  332. current_asmdata.getlabel(fdeofslabel,alt_dbgframe);
  333. list.concat(tai_label.create(fdeofslabel));
  334. list.concat(tai_const.create_rel_sym(aitconst_32bit,cielabel,fdeofslabel));
  335. end
  336. else
  337. begin
  338. tc:=tai_const.create_sym(cielabel);
  339. { force label offset to secrel32 for windows systems }
  340. if (target_info.system in systems_windows+systems_wince) then
  341. tc.consttype:=aitconst_secrel32_symbol;
  342. list.concat(tc);
  343. end;
  344. current_asmdata.getlabel(curpos,alt_dbgframe);
  345. list.concat(tai_label.create(curpos));
  346. list.concat(tai_const.Create_sym(hp.oper[0].beginsym));
  347. list.concat(tai_const.create_rel_sym(aitconst_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  348. { we wrote a 'z' into the CIE augmentation data }
  349. if use_eh_frame then
  350. begin
  351. { size of augmentation }
  352. list.concat(tai_const.create_8bit(4));
  353. {$ifdef debug_eh}
  354. list.concat(tai_comment.Create(strpnew('LSDA')));
  355. {$endif debug_eh}
  356. { address of LSDA}
  357. list.concat(tai_const.Create_sym(CurrentLSDALabel));
  358. { do not reuse LSDA label }
  359. CurrentLSDALabel:=nil;
  360. end;
  361. end;
  362. DW_CFA_End_Frame :
  363. begin
  364. list.concat(cai_align.create_zeros(4));
  365. list.concat(tai_label.create(lenendlabel));
  366. lenstartlabel:=nil;
  367. lenendlabel:=nil;
  368. end;
  369. DW_Set_LSDALabel:
  370. CurrentLSDALabel:=hp.oper[0].sym as TAsmLabel;
  371. else
  372. hp.generate_code(list);
  373. end;
  374. hp:=TDwarfItem(hp.next);
  375. end;
  376. { Check for open frames }
  377. if assigned(lenstartlabel) then
  378. internalerror(2004041210);
  379. { DwarfList is processed, remove items }
  380. DwarfList.Clear;
  381. end;
  382. procedure TDwarfAsmCFI.start_frame(list:TAsmList);
  383. begin
  384. current_asmdata.getlabel(FFrameEndLabel,alt_dbgframe);
  385. FLastloclabel:=get_frame_start;
  386. list.concat(tai_label.create(get_frame_start));
  387. if assigned(LSDALabel) then
  388. DwarfList.concat(tdwarfitem.create_sym(DW_Set_LSDALabel,doe_32bit,LSDALabel));
  389. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,get_frame_start,FFrameEndLabel));
  390. end;
  391. function TDwarfAsmCFI.get_frame_start : TAsmLabel;
  392. begin
  393. if not(assigned(FFrameStartLabel)) then
  394. current_asmdata.getlabel(FFrameStartLabel,alt_dbgframe);
  395. Result:=FFrameStartLabel;
  396. end;
  397. procedure TDwarfAsmCFI.outmost_frame(list: TAsmList);
  398. begin
  399. cfa_advance_loc(list);
  400. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_undefined,doe_uleb,NR_RETURN_ADDRESS_REG));
  401. end;
  402. procedure TDwarfAsmCFI.end_frame(list:TAsmList);
  403. begin
  404. if not assigned(FFrameStartLabel) then
  405. internalerror(2004041213);
  406. DwarfList.concat(tdwarfitem.create(DW_CFA_end_frame));
  407. list.concat(tai_label.create(FFrameEndLabel));
  408. FFrameStartLabel:=nil;
  409. FFrameEndLabel:=nil;
  410. FLastLocLabel:=nil;
  411. end;
  412. procedure TDwarfAsmCFI.cfa_advance_loc(list:TAsmList);
  413. var
  414. currloclabel : tasmlabel;
  415. begin
  416. if FLastloclabel=nil then
  417. internalerror(200404082);
  418. current_asmdata.getlabel(currloclabel,alt_dbgframe);
  419. list.concat(tai_label.create(currloclabel));
  420. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  421. FLastloclabel:=currloclabel;
  422. end;
  423. procedure TDwarfAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  424. begin
  425. cfa_advance_loc(list);
  426. { TODO: check if ref is a temp}
  427. { offset must be positive }
  428. DwarfList.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  429. end;
  430. procedure TDwarfAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  431. begin
  432. cfa_advance_loc(list);
  433. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  434. end;
  435. procedure TDwarfAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  436. begin
  437. cfa_advance_loc(list);
  438. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  439. end;
  440. procedure TDwarfAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  441. begin
  442. cfa_advance_loc(list);
  443. DwarfList.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  444. end;
  445. begin
  446. CAsmCFI:=TDwarfAsmCFI;
  447. end.