cfidwarf.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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,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. private
  51. FDwarfList : TLinkedList;
  52. FFrameStartLabel,
  53. FFrameEndLabel,
  54. FLastloclabel : tasmlabel;
  55. procedure cfa_advance_loc(list:TAsmList);
  56. procedure generate_initial_instructions(list:TAsmList);virtual;
  57. protected
  58. code_alignment_factor,
  59. data_alignment_factor : shortint;
  60. property DwarfList:TlinkedList read FDwarfList;
  61. public
  62. constructor create;override;
  63. destructor destroy;override;
  64. procedure generate_code(list:TAsmList);override;
  65. { operations }
  66. procedure start_frame(list:TAsmList);override;
  67. procedure end_frame(list:TAsmList);override;
  68. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);override;
  69. procedure cfa_restore(list:TAsmList;reg:tregister);override;
  70. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);override;
  71. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);override;
  72. end;
  73. implementation
  74. uses
  75. verbose;
  76. const
  77. { Call frame information }
  78. DW_CFA_set_loc = $01;
  79. DW_CFA_advance_loc1 = $02;
  80. DW_CFA_advance_loc2 = $03;
  81. DW_CFA_advance_loc4 = $04;
  82. DW_CFA_offset_extended = $05;
  83. DW_CFA_restore_extended = $06;
  84. DW_CFA_def_cfa = $0c;
  85. DW_CFA_def_cfa_register = $0d;
  86. DW_CFA_def_cfa_offset = $0e;
  87. { Own additions }
  88. DW_CFA_start_frame = $f0;
  89. DW_CFA_end_frame = $f1;
  90. DW_LNS_copy = $01;
  91. DW_LNS_advance_pc = $02;
  92. DW_LNS_advance_line = $03;
  93. DW_LNS_set_file = $04;
  94. DW_LNS_set_column = $05;
  95. DW_LNS_negate_stmt = $06;
  96. DW_LNS_set_basic_block = $07;
  97. DW_LNS_const_add_pc = $08;
  98. DW_LNS_fixed_advance_pc = $09;
  99. DW_LNS_set_prologue_end = $0a;
  100. DW_LNS_set_epilogue_begin = $0b;
  101. DW_LNS_set_isa = $0c;
  102. DW_LNE_end_sequence = $01;
  103. DW_LNE_set_address = $02;
  104. DW_LNE_define_file = $03;
  105. DW_LNE_lo_user = $80;
  106. DW_LNE_hi_user = $ff;
  107. {****************************************************************************
  108. TDWARFITEM
  109. ****************************************************************************}
  110. constructor tdwarfitem.create(aop:byte);
  111. begin
  112. inherited create;
  113. op:=aop;
  114. ops:=0;
  115. end;
  116. constructor tdwarfitem.create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  117. begin
  118. inherited create;
  119. op:=aop;
  120. ops:=1;
  121. oper[0].typ:=dop_reg;
  122. oper[0].enc:=enc1;
  123. oper[0].register:=reg;
  124. end;
  125. constructor tdwarfitem.create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  126. begin
  127. inherited create;
  128. op:=aop;
  129. ops:=1;
  130. oper[0].typ:=dop_const;
  131. oper[0].enc:=enc1;
  132. oper[0].value:=val;
  133. end;
  134. constructor tdwarfitem.create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  135. begin
  136. inherited create;
  137. op:=aop;
  138. ops:=1;
  139. { relative offsets are passed }
  140. oper[0].typ:=dop_reloffset;
  141. oper[0].enc:=enc1;
  142. oper[0].beginsym:=beginlab;
  143. oper[0].endsym:=endlab;
  144. end;
  145. constructor tdwarfitem.create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  146. begin
  147. inherited create;
  148. op:=aop;
  149. ops:=2;
  150. oper[0].typ:=dop_reg;
  151. oper[0].enc:=enc1;
  152. oper[0].register:=reg;
  153. oper[1].typ:=dop_const;
  154. oper[1].enc:=enc2;
  155. oper[1].value:=val;
  156. end;
  157. procedure tdwarfitem.generate_code(list:TAsmList);
  158. const
  159. enc2ait_const : array[tdwarfoperenc] of taiconst_type = (
  160. aitconst_uleb128bit,aitconst_sleb128bit,aitconst_ptr,
  161. aitconst_32bit,aitconst_16bit,aitconst_8bit
  162. );
  163. var
  164. i : integer;
  165. begin
  166. list.concat(tai_const.create_8bit(op));
  167. for i:=0 to ops-1 do
  168. begin
  169. case oper[i].typ of
  170. dop_const :
  171. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  172. dop_sym :
  173. begin
  174. if oper[i].enc<>doe_ptr then
  175. internalerror(200404127);
  176. list.concat(tai_const.create_sym(oper[i].sym));
  177. end;
  178. dop_reloffset :
  179. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  180. dop_reg :
  181. list.concat(tai_const.create(enc2ait_const[oper[i].enc],dwarf_reg(oper[i].register)));
  182. else
  183. internalerror(200404128);
  184. end;
  185. end;
  186. end;
  187. {****************************************************************************
  188. TDwarfAsmCFI
  189. ****************************************************************************}
  190. constructor TDwarfAsmCFI.create;
  191. begin
  192. inherited create;
  193. FFrameStartLabel:=nil;
  194. FFrameEndLabel:=nil;
  195. FLastLocLabel:=nil;
  196. code_alignment_factor:=1;
  197. data_alignment_factor:=-4;
  198. FDwarfList:=TLinkedList.Create;
  199. end;
  200. destructor TDwarfAsmCFI.destroy;
  201. begin
  202. FDwarfList.Free;
  203. end;
  204. {$ifdef 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. {$else i386}
  216. { if more cpu dependend stuff is implemented, this needs more refactoring }
  217. procedure TDwarfAsmCFI.generate_initial_instructions(list:TAsmList);
  218. begin
  219. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  220. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  221. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  222. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  223. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  224. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  225. end;
  226. {$endif i386}
  227. procedure TDwarfAsmCFI.generate_code(list:TAsmList);
  228. var
  229. hp : tdwarfitem;
  230. cielabel,
  231. lenstartlabel,
  232. lenendlabel : tasmlabel;
  233. tc : tai_const;
  234. begin
  235. new_section(list,sec_debug_frame,'',0);
  236. { CIE
  237. DWORD length
  238. DWORD CIE_Id = 0xffffffff
  239. BYTE version = 1
  240. STRING augmentation = "" = BYTE 0
  241. ULEB128 code alignment factor = 1
  242. ULEB128 data alignment factor = -1
  243. BYTE return address register
  244. <...> start sequence
  245. }
  246. current_asmdata.getlabel(cielabel,alt_dbgframe);
  247. list.concat(tai_label.create(cielabel));
  248. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  249. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  250. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  251. list.concat(tai_label.create(lenstartlabel));
  252. list.concat(tai_const.create_32bit(longint($ffffffff)));
  253. list.concat(tai_const.create_8bit(1));
  254. list.concat(tai_const.create_8bit(0)); { empty string }
  255. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  256. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  257. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  258. { Generate standard code
  259. def_cfa(stackpointer,sizeof(aint))
  260. cfa_offset_extended(returnaddres,-sizeof(aint))
  261. }
  262. generate_initial_instructions(list);
  263. list.concat(cai_align.create_zeros(4));
  264. list.concat(tai_label.create(lenendlabel));
  265. lenstartlabel:=nil;
  266. lenendlabel:=nil;
  267. hp:=TDwarfItem(DwarfList.first);
  268. while assigned(hp) do
  269. begin
  270. case hp.op of
  271. DW_CFA_Start_Frame :
  272. begin
  273. if assigned(lenstartlabel) then
  274. internalerror(200404125);
  275. if (hp.ops<>1) or
  276. (hp.oper[0].typ<>dop_reloffset) then
  277. internalerror(200404126);
  278. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  279. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  280. { FDE
  281. DWORD length
  282. DWORD CIE-pointer = cielabel
  283. PTRSIZE initial location = oper[0]
  284. PTRSIZE function size = oper[1]
  285. }
  286. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  287. list.concat(tai_label.create(lenstartlabel));
  288. { force label offset to 32bit }
  289. tc:=tai_const.create_sym(cielabel);
  290. tc.consttype:=aitconst_32bit;
  291. list.concat(tc);
  292. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  293. list.concat(tai_const.create_rel_sym(aitconst_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  294. end;
  295. DW_CFA_End_Frame :
  296. begin
  297. list.concat(cai_align.create_zeros(4));
  298. list.concat(tai_label.create(lenendlabel));
  299. lenstartlabel:=nil;
  300. lenendlabel:=nil;
  301. end;
  302. else
  303. hp.generate_code(list);
  304. end;
  305. hp:=TDwarfItem(hp.next);
  306. end;
  307. { Check for open frames }
  308. if assigned(lenstartlabel) then
  309. internalerror(2004041210);
  310. { DwarfList is processed, remove items }
  311. DwarfList.Clear;
  312. end;
  313. procedure TDwarfAsmCFI.start_frame(list:TAsmList);
  314. begin
  315. if assigned(FFrameStartLabel) then
  316. internalerror(200404129);
  317. current_asmdata.getlabel(FFrameStartLabel,alt_dbgframe);
  318. current_asmdata.getlabel(FFrameEndLabel,alt_dbgframe);
  319. FLastloclabel:=FFrameStartLabel;
  320. list.concat(tai_label.create(FFrameStartLabel));
  321. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  322. end;
  323. procedure TDwarfAsmCFI.end_frame(list:TAsmList);
  324. begin
  325. if not assigned(FFrameStartLabel) then
  326. internalerror(2004041213);
  327. DwarfList.concat(tdwarfitem.create(DW_CFA_end_frame));
  328. list.concat(tai_label.create(FFrameEndLabel));
  329. FFrameStartLabel:=nil;
  330. FFrameEndLabel:=nil;
  331. FLastLocLabel:=nil;
  332. end;
  333. procedure TDwarfAsmCFI.cfa_advance_loc(list:TAsmList);
  334. var
  335. currloclabel : tasmlabel;
  336. begin
  337. if FLastloclabel=nil then
  338. internalerror(200404082);
  339. current_asmdata.getlabel(currloclabel,alt_dbgframe);
  340. list.concat(tai_label.create(currloclabel));
  341. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  342. FLastloclabel:=currloclabel;
  343. end;
  344. procedure TDwarfAsmCFI.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  345. begin
  346. cfa_advance_loc(list);
  347. {$warning TODO check if ref is a temp}
  348. { offset must be positive }
  349. DwarfList.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  350. end;
  351. procedure TDwarfAsmCFI.cfa_restore(list:TAsmList;reg:tregister);
  352. begin
  353. cfa_advance_loc(list);
  354. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  355. end;
  356. procedure TDwarfAsmCFI.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  357. begin
  358. cfa_advance_loc(list);
  359. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  360. end;
  361. procedure TDwarfAsmCFI.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  362. begin
  363. cfa_advance_loc(list);
  364. DwarfList.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  365. end;
  366. begin
  367. CAsmCFI:=TDwarfAsmCFI;
  368. end.