cfidwarf.pas 15 KB

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