cfidwarf.pas 15 KB

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