cfidwarf.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. {$if defined(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. {$elseif defined(avr)}
  238. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  239. begin
  240. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  241. list.concat(tai_const.create_uleb128bit(32));
  242. list.concat(tai_const.create_uleb128bit(2));
  243. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  244. list.concat(tai_const.create_uleb128bit(36));
  245. list.concat(tai_const.create_uleb128bit((-1) div data_alignment_factor));
  246. end;
  247. {$else}
  248. { if more cpu dependend stuff is implemented, this needs more refactoring }
  249. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  250. begin
  251. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  252. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  253. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  254. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  255. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  256. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  257. end;
  258. {$endif i386}
  259. procedure TDwarfAsmCFILowLevel.generate_code(list:TAsmList);
  260. var
  261. hp : tdwarfitem;
  262. cielabel,
  263. lenstartlabel,
  264. lenendlabel : tasmlabel;
  265. tc : tai_const;
  266. begin
  267. new_section(list,sec_debug_frame,'',0);
  268. { CIE
  269. DWORD length
  270. DWORD CIE_Id = 0xffffffff
  271. BYTE version = 1
  272. STRING augmentation = "" = BYTE 0
  273. ULEB128 code alignment factor = 1
  274. ULEB128 data alignment factor = -1
  275. BYTE return address register
  276. <...> start sequence
  277. }
  278. current_asmdata.getlabel(cielabel,alt_dbgframe);
  279. list.concat(tai_label.create(cielabel));
  280. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  281. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  282. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  283. list.concat(tai_label.create(lenstartlabel));
  284. list.concat(tai_const.create_32bit(longint($ffffffff)));
  285. list.concat(tai_const.create_8bit(1));
  286. list.concat(tai_const.create_8bit(0)); { empty string }
  287. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  288. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  289. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  290. { Generate standard code
  291. def_cfa(stackpointer,sizeof(aint))
  292. cfa_offset_extended(returnaddres,-sizeof(aint))
  293. }
  294. generate_initial_instructions(list);
  295. list.concat(cai_align.create_zeros(sizeof(pint)));
  296. list.concat(tai_label.create(lenendlabel));
  297. lenstartlabel:=nil;
  298. lenendlabel:=nil;
  299. hp:=TDwarfItem(DwarfList.first);
  300. while assigned(hp) do
  301. begin
  302. case hp.op of
  303. DW_CFA_Start_Frame :
  304. begin
  305. if assigned(lenstartlabel) then
  306. internalerror(200404125);
  307. if (hp.ops<>1) or
  308. (hp.oper[0].typ<>dop_reloffset) then
  309. internalerror(200404126);
  310. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  311. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  312. { FDE
  313. DWORD length
  314. DWORD CIE-pointer = cielabel relative to section start
  315. PTRSIZE initial location = oper[0]
  316. PTRSIZE function size = oper[1]
  317. }
  318. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  319. list.concat(tai_label.create(lenstartlabel));
  320. tc:=tai_const.create_sym(cielabel);
  321. { force label offset to secrel32 for windows systems }
  322. if (target_info.system in systems_windows+systems_wince) then
  323. tc.consttype:=aitconst_secrel32_symbol;
  324. list.concat(tc);
  325. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  326. list.concat(tai_const.create_rel_sym(aitconst_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  327. end;
  328. DW_CFA_End_Frame :
  329. begin
  330. list.concat(cai_align.create_zeros(sizeof(pint)));
  331. list.concat(tai_label.create(lenendlabel));
  332. lenstartlabel:=nil;
  333. lenendlabel:=nil;
  334. end;
  335. else
  336. hp.generate_code(list);
  337. end;
  338. hp:=TDwarfItem(hp.next);
  339. end;
  340. { Check for open frames }
  341. if assigned(lenstartlabel) then
  342. internalerror(2004041210);
  343. { DwarfList is processed, remove items }
  344. DwarfList.Clear;
  345. end;
  346. procedure TDwarfAsmCFILowLevel.start_frame(list:TAsmList);
  347. begin
  348. if assigned(FFrameStartLabel) then
  349. internalerror(200404129);
  350. current_asmdata.getlabel(FFrameStartLabel,alt_dbgframe);
  351. current_asmdata.getlabel(FFrameEndLabel,alt_dbgframe);
  352. FLastloclabel:=FFrameStartLabel;
  353. list.concat(tai_label.create(FFrameStartLabel));
  354. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  355. end;
  356. procedure TDwarfAsmCFILowLevel.end_frame(list:TAsmList);
  357. begin
  358. if not assigned(FFrameStartLabel) then
  359. internalerror(2004041213);
  360. DwarfList.concat(tdwarfitem.create(DW_CFA_end_frame));
  361. list.concat(tai_label.create(FFrameEndLabel));
  362. FFrameStartLabel:=nil;
  363. FFrameEndLabel:=nil;
  364. FLastLocLabel:=nil;
  365. end;
  366. procedure TDwarfAsmCFILowLevel.cfa_advance_loc(list:TAsmList);
  367. var
  368. currloclabel : tasmlabel;
  369. begin
  370. if FLastloclabel=nil then
  371. internalerror(200404082);
  372. current_asmdata.getlabel(currloclabel,alt_dbgframe);
  373. list.concat(tai_label.create(currloclabel));
  374. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  375. FLastloclabel:=currloclabel;
  376. end;
  377. procedure TDwarfAsmCFILowLevel.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  378. begin
  379. cfa_advance_loc(list);
  380. { TODO: check if ref is a temp}
  381. { offset must be positive }
  382. DwarfList.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  383. end;
  384. procedure TDwarfAsmCFILowLevel.cfa_restore(list:TAsmList;reg:tregister);
  385. begin
  386. cfa_advance_loc(list);
  387. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  388. end;
  389. procedure TDwarfAsmCFILowLevel.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  390. begin
  391. cfa_advance_loc(list);
  392. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  393. end;
  394. procedure TDwarfAsmCFILowLevel.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  395. begin
  396. cfa_advance_loc(list);
  397. DwarfList.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  398. end;
  399. {****************************************************************************
  400. TDwarfAsmCFILowLevel
  401. ****************************************************************************}
  402. procedure TDwarfAsmCFIHighLevel.generate_code(list: TAsmList);
  403. begin
  404. if not(tf_use_hlcfi in target_info.flags) then
  405. begin
  406. inherited;
  407. exit;
  408. end;
  409. end;
  410. procedure TDwarfAsmCFIHighLevel.start_frame(list: TAsmList);
  411. begin
  412. if not(tf_use_hlcfi in target_info.flags) then
  413. begin
  414. inherited;
  415. exit;
  416. end;
  417. list.concat(tai_cfi_op_none.create(cfi_startproc));
  418. end;
  419. procedure TDwarfAsmCFIHighLevel.end_frame(list: TAsmList);
  420. begin
  421. if not(tf_use_hlcfi in target_info.flags) then
  422. begin
  423. inherited;
  424. exit;
  425. end;
  426. list.concat(tai_cfi_op_none.create(cfi_endproc));
  427. end;
  428. procedure TDwarfAsmCFIHighLevel.cfa_offset(list: TAsmList; reg: tregister; ofs: longint);
  429. begin
  430. if not(tf_use_hlcfi in target_info.flags) then
  431. begin
  432. inherited;
  433. exit;
  434. end;
  435. list.concat(tai_cfi_op_reg_val.create(cfi_offset,reg,ofs));
  436. end;
  437. procedure TDwarfAsmCFIHighLevel.cfa_restore(list: TAsmList; reg: tregister);
  438. begin
  439. if not(tf_use_hlcfi in target_info.flags) then
  440. begin
  441. inherited;
  442. exit;
  443. end;
  444. list.concat(tai_cfi_op_reg.create(cfi_restore,reg));
  445. end;
  446. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa_register(list: TAsmList; reg: tregister);
  447. begin
  448. if not(tf_use_hlcfi in target_info.flags) then
  449. begin
  450. inherited;
  451. exit;
  452. end;
  453. list.concat(tai_cfi_op_reg.create(cfi_def_cfa_register,reg));
  454. end;
  455. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa_offset(list: TAsmList; ofs: longint);
  456. begin
  457. if not(tf_use_hlcfi in target_info.flags) then
  458. begin
  459. inherited;
  460. exit;
  461. end;
  462. list.concat(tai_cfi_op_val.create(cfi_def_cfa_offset,ofs));
  463. end;
  464. begin
  465. CAsmCFI:=TDwarfAsmCFIHighLevel;
  466. end.