cfidwarf.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. interface
  20. uses
  21. cclasses,
  22. globtype,
  23. cgbase,cpubase,
  24. aasmbase,aasmcfi,aasmtai,aasmdata;
  25. const
  26. maxdwarfops = 2;
  27. type
  28. tdwarfoperenc=(doe_uleb,doe_sleb,doe_ptr,doe_32bit,doe_16bit,doe_8bit);
  29. tdwarfopertype=(dop_reg,dop_const,dop_sym,dop_reloffset);
  30. tdwarfoper=record
  31. enc : tdwarfoperenc;
  32. case typ:tdwarfopertype of
  33. dop_reg : (register:tregister);
  34. dop_const : (value:int64);
  35. dop_sym : (sym:tasmsymbol);
  36. dop_reloffset : (beginsym,endsym:tasmsymbol);
  37. end;
  38. tdwarfitem=class(TLinkedListItem)
  39. op : byte;
  40. ops : byte;
  41. oper : array[0..maxdwarfops-1] of tdwarfoper;
  42. constructor create(aop:byte);
  43. constructor create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  44. constructor create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  45. constructor create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  46. constructor create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  47. procedure generate_code(list:TAsmList);
  48. end;
  49. TDwarfAsmCFI=class(TAsmCFI)
  50. constructor create;override;
  51. end;
  52. TDwarfAsmCFILowLevel=class(TDwarfAsmCFI)
  53. private
  54. FDwarfList : TLinkedList;
  55. FFrameStartLabel,
  56. FFrameEndLabel,
  57. FLastloclabel : tasmlabel;
  58. procedure cfa_advance_loc(list:TAsmList);
  59. procedure generate_initial_instructions(list:TAsmList);virtual;
  60. protected
  61. code_alignment_factor,
  62. data_alignment_factor : shortint;
  63. property DwarfList:TlinkedList read FDwarfList;
  64. public
  65. constructor create;override;
  66. destructor destroy;override;
  67. procedure generate_code(list:TAsmList);override;
  68. { operations }
  69. procedure start_frame(list:TAsmList);override;
  70. procedure end_frame(list:TAsmList);override;
  71. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);override;
  72. procedure cfa_restore(list:TAsmList;reg:tregister);override;
  73. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);override;
  74. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);override;
  75. end;
  76. TDwarfAsmCFIHighLevel=class(TDwarfAsmCFILowLevel)
  77. public
  78. procedure generate_code(list:TAsmList);override;
  79. { operations }
  80. procedure start_frame(list:TAsmList);override;
  81. procedure end_frame(list:TAsmList);override;
  82. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);override;
  83. procedure cfa_restore(list:TAsmList;reg:tregister);override;
  84. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);override;
  85. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);override;
  86. end;
  87. implementation
  88. uses
  89. systems,
  90. verbose;
  91. const
  92. { Call frame information }
  93. DW_CFA_set_loc = $01;
  94. DW_CFA_advance_loc1 = $02;
  95. DW_CFA_advance_loc2 = $03;
  96. DW_CFA_advance_loc4 = $04;
  97. DW_CFA_offset_extended = $05;
  98. DW_CFA_restore_extended = $06;
  99. DW_CFA_def_cfa = $0c;
  100. DW_CFA_def_cfa_register = $0d;
  101. DW_CFA_def_cfa_offset = $0e;
  102. { Own additions }
  103. DW_CFA_start_frame = $f0;
  104. DW_CFA_end_frame = $f1;
  105. DW_LNS_copy = $01;
  106. DW_LNS_advance_pc = $02;
  107. DW_LNS_advance_line = $03;
  108. DW_LNS_set_file = $04;
  109. DW_LNS_set_column = $05;
  110. DW_LNS_negate_stmt = $06;
  111. DW_LNS_set_basic_block = $07;
  112. DW_LNS_const_add_pc = $08;
  113. DW_LNS_fixed_advance_pc = $09;
  114. DW_LNS_set_prologue_end = $0a;
  115. DW_LNS_set_epilogue_begin = $0b;
  116. DW_LNS_set_isa = $0c;
  117. DW_LNE_end_sequence = $01;
  118. DW_LNE_set_address = $02;
  119. DW_LNE_define_file = $03;
  120. DW_LNE_lo_user = $80;
  121. DW_LNE_hi_user = $ff;
  122. {****************************************************************************
  123. TDWARFITEM
  124. ****************************************************************************}
  125. constructor tdwarfitem.create(aop:byte);
  126. begin
  127. inherited create;
  128. op:=aop;
  129. ops:=0;
  130. end;
  131. constructor tdwarfitem.create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  132. begin
  133. inherited create;
  134. op:=aop;
  135. ops:=1;
  136. oper[0].typ:=dop_reg;
  137. oper[0].enc:=enc1;
  138. oper[0].register:=reg;
  139. end;
  140. constructor tdwarfitem.create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  141. begin
  142. inherited create;
  143. op:=aop;
  144. ops:=1;
  145. oper[0].typ:=dop_const;
  146. oper[0].enc:=enc1;
  147. oper[0].value:=val;
  148. end;
  149. constructor tdwarfitem.create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  150. begin
  151. inherited create;
  152. op:=aop;
  153. ops:=1;
  154. { relative offsets are passed }
  155. oper[0].typ:=dop_reloffset;
  156. oper[0].enc:=enc1;
  157. oper[0].beginsym:=beginlab;
  158. oper[0].endsym:=endlab;
  159. end;
  160. constructor tdwarfitem.create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  161. begin
  162. inherited create;
  163. op:=aop;
  164. ops:=2;
  165. oper[0].typ:=dop_reg;
  166. oper[0].enc:=enc1;
  167. oper[0].register:=reg;
  168. oper[1].typ:=dop_const;
  169. oper[1].enc:=enc2;
  170. oper[1].value:=val;
  171. end;
  172. procedure tdwarfitem.generate_code(list:TAsmList);
  173. const
  174. enc2ait_const : array[tdwarfoperenc] of taiconst_type = (
  175. aitconst_uleb128bit,aitconst_sleb128bit,aitconst_ptr,
  176. aitconst_32bit,aitconst_16bit,aitconst_8bit
  177. );
  178. var
  179. i : integer;
  180. begin
  181. list.concat(tai_const.create_8bit(op));
  182. for i:=0 to ops-1 do
  183. begin
  184. case oper[i].typ of
  185. dop_const :
  186. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  187. dop_sym :
  188. begin
  189. if oper[i].enc<>doe_ptr then
  190. internalerror(200404127);
  191. list.concat(tai_const.create_sym(oper[i].sym));
  192. end;
  193. dop_reloffset :
  194. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  195. dop_reg :
  196. list.concat(tai_const.create(enc2ait_const[oper[i].enc],dwarf_reg(oper[i].register)));
  197. else
  198. internalerror(200404128);
  199. end;
  200. end;
  201. end;
  202. {****************************************************************************
  203. TDwarfAsmCFI
  204. ****************************************************************************}
  205. constructor TDwarfAsmCFI.create;
  206. begin
  207. inherited;
  208. end;
  209. {****************************************************************************
  210. TDwarfAsmCFILowLevel
  211. ****************************************************************************}
  212. constructor TDwarfAsmCFILowLevel.create;
  213. begin
  214. inherited create;
  215. FFrameStartLabel:=nil;
  216. FFrameEndLabel:=nil;
  217. FLastLocLabel:=nil;
  218. code_alignment_factor:=1;
  219. data_alignment_factor:=-4;
  220. FDwarfList:=TLinkedList.Create;
  221. end;
  222. destructor TDwarfAsmCFILowLevel.destroy;
  223. begin
  224. FDwarfList.Free;
  225. end;
  226. {$ifdef i386}
  227. { if more cpu dependend stuff is implemented, this needs more refactoring }
  228. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  229. begin
  230. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  231. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  232. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  233. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  234. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  235. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  236. end;
  237. {$else i386}
  238. { if more cpu dependend stuff is implemented, this needs more refactoring }
  239. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  240. begin
  241. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  242. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  243. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  244. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  245. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  246. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  247. end;
  248. {$endif i386}
  249. procedure TDwarfAsmCFILowLevel.generate_code(list:TAsmList);
  250. var
  251. hp : tdwarfitem;
  252. cielabel,
  253. lenstartlabel,
  254. lenendlabel : tasmlabel;
  255. tc : tai_const;
  256. begin
  257. new_section(list,sec_debug_frame,'',0);
  258. { CIE
  259. DWORD length
  260. DWORD CIE_Id = 0xffffffff
  261. BYTE version = 1
  262. STRING augmentation = "" = BYTE 0
  263. ULEB128 code alignment factor = 1
  264. ULEB128 data alignment factor = -1
  265. BYTE return address register
  266. <...> start sequence
  267. }
  268. current_asmdata.getlabel(cielabel,alt_dbgframe);
  269. list.concat(tai_label.create(cielabel));
  270. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  271. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  272. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  273. list.concat(tai_label.create(lenstartlabel));
  274. list.concat(tai_const.create_32bit(longint($ffffffff)));
  275. list.concat(tai_const.create_8bit(1));
  276. list.concat(tai_const.create_8bit(0)); { empty string }
  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. { Generate standard code
  281. def_cfa(stackpointer,sizeof(aint))
  282. cfa_offset_extended(returnaddres,-sizeof(aint))
  283. }
  284. generate_initial_instructions(list);
  285. list.concat(cai_align.create_zeros(4));
  286. list.concat(tai_label.create(lenendlabel));
  287. lenstartlabel:=nil;
  288. lenendlabel:=nil;
  289. hp:=TDwarfItem(DwarfList.first);
  290. while assigned(hp) do
  291. begin
  292. case hp.op of
  293. DW_CFA_Start_Frame :
  294. begin
  295. if assigned(lenstartlabel) then
  296. internalerror(200404125);
  297. if (hp.ops<>1) or
  298. (hp.oper[0].typ<>dop_reloffset) then
  299. internalerror(200404126);
  300. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  301. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  302. { FDE
  303. DWORD length
  304. DWORD CIE-pointer = cielabel relative to section start
  305. PTRSIZE initial location = oper[0]
  306. PTRSIZE function size = oper[1]
  307. }
  308. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  309. list.concat(tai_label.create(lenstartlabel));
  310. tc:=tai_const.create_sym(cielabel);
  311. { force label offset to secrel32 for windows systems }
  312. if (target_info.system in systems_windows+systems_wince) then
  313. tc.consttype:=aitconst_secrel32_symbol;
  314. list.concat(tc);
  315. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  316. list.concat(tai_const.create_rel_sym(aitconst_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  317. end;
  318. DW_CFA_End_Frame :
  319. begin
  320. list.concat(cai_align.create_zeros(4));
  321. list.concat(tai_label.create(lenendlabel));
  322. lenstartlabel:=nil;
  323. lenendlabel:=nil;
  324. end;
  325. else
  326. hp.generate_code(list);
  327. end;
  328. hp:=TDwarfItem(hp.next);
  329. end;
  330. { Check for open frames }
  331. if assigned(lenstartlabel) then
  332. internalerror(2004041210);
  333. { DwarfList is processed, remove items }
  334. DwarfList.Clear;
  335. end;
  336. procedure TDwarfAsmCFILowLevel.start_frame(list:TAsmList);
  337. begin
  338. if assigned(FFrameStartLabel) then
  339. internalerror(200404129);
  340. current_asmdata.getlabel(FFrameStartLabel,alt_dbgframe);
  341. current_asmdata.getlabel(FFrameEndLabel,alt_dbgframe);
  342. FLastloclabel:=FFrameStartLabel;
  343. list.concat(tai_label.create(FFrameStartLabel));
  344. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  345. end;
  346. procedure TDwarfAsmCFILowLevel.end_frame(list:TAsmList);
  347. begin
  348. if not assigned(FFrameStartLabel) then
  349. internalerror(2004041213);
  350. DwarfList.concat(tdwarfitem.create(DW_CFA_end_frame));
  351. list.concat(tai_label.create(FFrameEndLabel));
  352. FFrameStartLabel:=nil;
  353. FFrameEndLabel:=nil;
  354. FLastLocLabel:=nil;
  355. end;
  356. procedure TDwarfAsmCFILowLevel.cfa_advance_loc(list:TAsmList);
  357. var
  358. currloclabel : tasmlabel;
  359. begin
  360. if FLastloclabel=nil then
  361. internalerror(200404082);
  362. current_asmdata.getlabel(currloclabel,alt_dbgframe);
  363. list.concat(tai_label.create(currloclabel));
  364. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  365. FLastloclabel:=currloclabel;
  366. end;
  367. procedure TDwarfAsmCFILowLevel.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  368. begin
  369. cfa_advance_loc(list);
  370. { TODO: check if ref is a temp}
  371. { offset must be positive }
  372. DwarfList.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  373. end;
  374. procedure TDwarfAsmCFILowLevel.cfa_restore(list:TAsmList;reg:tregister);
  375. begin
  376. cfa_advance_loc(list);
  377. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  378. end;
  379. procedure TDwarfAsmCFILowLevel.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  380. begin
  381. cfa_advance_loc(list);
  382. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  383. end;
  384. procedure TDwarfAsmCFILowLevel.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  385. begin
  386. cfa_advance_loc(list);
  387. DwarfList.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  388. end;
  389. {****************************************************************************
  390. TDwarfAsmCFILowLevel
  391. ****************************************************************************}
  392. procedure TDwarfAsmCFIHighLevel.generate_code(list: TAsmList);
  393. begin
  394. if not(tf_use_hlcfi in target_info.flags) then
  395. begin
  396. inherited;
  397. exit;
  398. end;
  399. end;
  400. procedure TDwarfAsmCFIHighLevel.start_frame(list: TAsmList);
  401. begin
  402. if not(tf_use_hlcfi in target_info.flags) then
  403. begin
  404. inherited;
  405. exit;
  406. end;
  407. list.concat(tai_cfi_op_none.create(cfi_startproc));
  408. end;
  409. procedure TDwarfAsmCFIHighLevel.end_frame(list: TAsmList);
  410. begin
  411. if not(tf_use_hlcfi in target_info.flags) then
  412. begin
  413. inherited;
  414. exit;
  415. end;
  416. list.concat(tai_cfi_op_none.create(cfi_endproc));
  417. end;
  418. procedure TDwarfAsmCFIHighLevel.cfa_offset(list: TAsmList; reg: tregister; ofs: longint);
  419. begin
  420. if not(tf_use_hlcfi in target_info.flags) then
  421. begin
  422. inherited;
  423. exit;
  424. end;
  425. list.concat(tai_cfi_op_reg_val.create(cfi_offset,reg,ofs));
  426. end;
  427. procedure TDwarfAsmCFIHighLevel.cfa_restore(list: TAsmList; reg: tregister);
  428. begin
  429. if not(tf_use_hlcfi in target_info.flags) then
  430. begin
  431. inherited;
  432. exit;
  433. end;
  434. list.concat(tai_cfi_op_reg.create(cfi_restore,reg));
  435. end;
  436. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa_register(list: TAsmList; reg: tregister);
  437. begin
  438. if not(tf_use_hlcfi in target_info.flags) then
  439. begin
  440. inherited;
  441. exit;
  442. end;
  443. list.concat(tai_cfi_op_reg.create(cfi_def_cfa_register,reg));
  444. end;
  445. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa_offset(list: TAsmList; ofs: longint);
  446. begin
  447. if not(tf_use_hlcfi in target_info.flags) then
  448. begin
  449. inherited;
  450. exit;
  451. end;
  452. list.concat(tai_cfi_op_val.create(cfi_def_cfa_offset,ofs));
  453. end;
  454. begin
  455. CAsmCFI:=TDwarfAsmCFIHighLevel;
  456. end.