dwarf.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. {
  2. $Id$
  3. Copyright (c) 2003-2004 by Peter Vreman and Florian Klaempfl
  4. This units contains special support for DWARF debug info
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit dwarf;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,
  24. cgbase,cpubase,
  25. aasmbase,aasmtai;
  26. const
  27. maxdwarfops = 2;
  28. type
  29. tdwarfoperenc=(doe_uleb,doe_sleb,doe_ptr,doe_32bit,doe_16bit,doe_8bit);
  30. tdwarfopertype=(dop_reg,dop_const,dop_sym,dop_reloffset);
  31. tdwarfoper=record
  32. enc : tdwarfoperenc;
  33. case typ:tdwarfopertype of
  34. dop_reg : (register:tregister);
  35. dop_const : (value:int64);
  36. dop_sym : (sym:tasmsymbol);
  37. dop_reloffset : (beginsym,endsym:tasmsymbol);
  38. end;
  39. tdwarfitem=class(TLinkedListItem)
  40. op : byte;
  41. ops : byte;
  42. oper : array[0..maxdwarfops-1] of tdwarfoper;
  43. constructor create(aop:longint);
  44. constructor create_reg(aop:longint;enc1:tdwarfoperenc;reg:tregister);
  45. constructor create_const(aop:longint;enc1:tdwarfoperenc;val:int64);
  46. constructor create_reloffset(aop:longint;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  47. constructor create_reg_const(aop:longint;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  48. procedure generate_code(list:taasmoutput);
  49. end;
  50. tdwarf=class
  51. private
  52. FDwarfList : TLinkedList;
  53. public
  54. constructor create;
  55. destructor destroy;override;
  56. property DwarfList:TlinkedList read FDwarfList;
  57. end;
  58. tdwarfcfi=class(tdwarf)
  59. private
  60. FFrameStartLabel,
  61. FFrameEndLabel,
  62. FLastloclabel : tasmlabel;
  63. procedure cfa_advance_loc(list:taasmoutput);
  64. protected
  65. code_alignment_factor,
  66. data_alignment_factor : shortint;
  67. public
  68. constructor create;
  69. procedure generate_code(list:taasmoutput);
  70. { operations }
  71. procedure start_frame(list:taasmoutput);
  72. procedure end_frame(list:taasmoutput);
  73. procedure cfa_offset(list:taasmoutput;reg:tregister;ofs:longint);
  74. procedure cfa_restore(list:taasmoutput;reg:tregister);
  75. procedure cfa_def_cfa_register(list:taasmoutput;reg:tregister);
  76. procedure cfa_def_cfa_offset(list:taasmoutput;ofs:longint);
  77. end;
  78. var
  79. dwarfcfi : tdwarfcfi;
  80. function dwarf_reg(r:tregister):longint;
  81. implementation
  82. uses
  83. verbose;
  84. const
  85. { Call frame information }
  86. DW_CFA_set_loc = $01;
  87. DW_CFA_advance_loc1 = $02;
  88. DW_CFA_advance_loc2 = $03;
  89. DW_CFA_advance_loc4 = $04;
  90. DW_CFA_offset_extended = $05;
  91. DW_CFA_restore_extended = $06;
  92. DW_CFA_def_cfa = $0c;
  93. DW_CFA_def_cfa_register = $0d;
  94. DW_CFA_def_cfa_offset = $0e;
  95. { Own additions }
  96. DW_CFA_start_frame = $f0;
  97. DW_CFA_end_frame = $f1;
  98. {****************************************************************************
  99. Helpers
  100. ****************************************************************************}
  101. function dwarf_reg(r:tregister):longint;
  102. begin
  103. result:=regdwarf_table[findreg_by_number(r)];
  104. end;
  105. {****************************************************************************
  106. TDWARF
  107. ****************************************************************************}
  108. constructor tdwarf.create;
  109. begin
  110. FDwarfList:=TLinkedList.Create;
  111. end;
  112. destructor tdwarf.destroy;
  113. begin
  114. FDwarfList.Free;
  115. end;
  116. {****************************************************************************
  117. TDWARFITEM
  118. ****************************************************************************}
  119. constructor tdwarfitem.create(aop:longint);
  120. begin
  121. inherited create;
  122. op:=aop;
  123. ops:=0;
  124. end;
  125. constructor tdwarfitem.create_reg(aop:longint;enc1:tdwarfoperenc;reg:tregister);
  126. begin
  127. inherited create;
  128. op:=aop;
  129. ops:=1;
  130. oper[0].typ:=dop_reg;
  131. oper[0].enc:=enc1;
  132. oper[0].register:=reg;
  133. end;
  134. constructor tdwarfitem.create_const(aop:longint;enc1:tdwarfoperenc;val:int64);
  135. begin
  136. inherited create;
  137. op:=aop;
  138. ops:=1;
  139. oper[0].typ:=dop_const;
  140. oper[0].enc:=enc1;
  141. oper[0].value:=val;
  142. end;
  143. constructor tdwarfitem.create_reloffset(aop:longint;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  144. begin
  145. inherited create;
  146. op:=aop;
  147. ops:=1;
  148. { relative offsets are passed }
  149. oper[0].typ:=dop_reloffset;
  150. oper[0].enc:=enc1;
  151. oper[0].beginsym:=beginlab;
  152. oper[0].endsym:=endlab;
  153. end;
  154. constructor tdwarfitem.create_reg_const(aop:longint;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  155. begin
  156. inherited create;
  157. op:=aop;
  158. ops:=2;
  159. oper[0].typ:=dop_reg;
  160. oper[0].enc:=enc1;
  161. oper[0].register:=reg;
  162. oper[1].typ:=dop_const;
  163. oper[1].enc:=enc2;
  164. oper[1].value:=val;
  165. end;
  166. procedure tdwarfitem.generate_code(list:taasmoutput);
  167. const
  168. enc2ait_const : array[tdwarfoperenc] of taitype = (
  169. ait_const_uleb128bit,ait_const_sleb128bit,ait_const_ptr,
  170. ait_const_32bit,ait_const_16bit,ait_const_8bit
  171. );
  172. var
  173. i : integer;
  174. v : int64;
  175. begin
  176. list.concat(tai_const.create_8bit(op));
  177. for i:=0 to ops-1 do
  178. begin
  179. case oper[i].typ of
  180. dop_const :
  181. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  182. dop_sym :
  183. begin
  184. if oper[i].enc<>doe_ptr then
  185. internalerror(200404127);
  186. list.concat(tai_const.create_sym(oper[i].sym));
  187. end;
  188. dop_reloffset :
  189. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  190. dop_reg :
  191. list.concat(tai_const.create(enc2ait_const[oper[i].enc],regdwarf_table[findreg_by_number(oper[i].register)]));
  192. else
  193. internalerror(200404128);
  194. end;
  195. end;
  196. end;
  197. {****************************************************************************
  198. TDWARFCFI
  199. ****************************************************************************}
  200. constructor tdwarfcfi.create;
  201. begin
  202. inherited create;
  203. FFrameStartLabel:=nil;
  204. FFrameEndLabel:=nil;
  205. FLastLocLabel:=nil;
  206. code_alignment_factor:=1;
  207. data_alignment_factor:=-1;
  208. end;
  209. procedure tdwarfcfi.generate_code(list:taasmoutput);
  210. var
  211. hp : tdwarfitem;
  212. cielabel,
  213. lenstartlabel,
  214. lenendlabel : tasmlabel;
  215. tc : tai_const;
  216. begin
  217. new_section(list,sec_debug_frame,'',0);
  218. { CIE
  219. DWORD length
  220. DWORD CIE_Id = 0xffffffff
  221. BYTE version = 1
  222. STRING augmentation = "" = BYTE 0
  223. ULEB128 code alignment factor = 1
  224. ULEB128 data alignment factor = -1
  225. BYTE return address register
  226. <...> start sequence
  227. }
  228. objectlibrary.getlabel(cielabel);
  229. list.concat(tai_label.create(cielabel));
  230. objectlibrary.getlabel(lenstartlabel);
  231. objectlibrary.getlabel(lenendlabel);
  232. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  233. list.concat(tai_label.create(lenstartlabel));
  234. list.concat(tai_const.create_32bit(longint($ffffffff)));
  235. list.concat(tai_const.create_8bit(1));
  236. list.concat(tai_const.create_8bit(0)); { empty string }
  237. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  238. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  239. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  240. { Generate standard code
  241. def_cfa(stackpointer,sizeof(aint))
  242. cfa_offset_extended(returnaddres,-sizeof(aint))
  243. }
  244. {$warning TODO This needs to be target dependent}
  245. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  246. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  247. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  248. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  249. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  250. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  251. list.concat(cai_align.create_zeros(4));
  252. list.concat(tai_label.create(lenendlabel));
  253. lenstartlabel:=nil;
  254. lenendlabel:=nil;
  255. hp:=TDwarfItem(Dwarflist.first);
  256. while assigned(hp) do
  257. begin
  258. case hp.op of
  259. DW_CFA_Start_Frame :
  260. begin
  261. if assigned(lenstartlabel) then
  262. internalerror(200404125);
  263. if (hp.ops<>1) or
  264. (hp.oper[0].typ<>dop_reloffset) then
  265. internalerror(200404126);
  266. objectlibrary.getlabel(lenstartlabel);
  267. objectlibrary.getlabel(lenendlabel);
  268. { FDE
  269. DWORD length
  270. DWORD CIE-pointer = cielabel
  271. PTRSIZE initial location = oper[0]
  272. PTRSIZE function size = oper[1]
  273. }
  274. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  275. list.concat(tai_label.create(lenstartlabel));
  276. { force label offset to 32bit }
  277. tc:=tai_const.create_sym(cielabel);
  278. tc.typ:=ait_const_32bit;
  279. list.concat(tc);
  280. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  281. list.concat(tai_const.create_rel_sym(ait_const_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  282. end;
  283. DW_CFA_End_Frame :
  284. begin
  285. list.concat(cai_align.create_zeros(4));
  286. list.concat(tai_label.create(lenendlabel));
  287. lenstartlabel:=nil;
  288. lenendlabel:=nil;
  289. end;
  290. else
  291. hp.generate_code(list);
  292. end;
  293. hp:=TDwarfItem(hp.next);
  294. end;
  295. { Check for open frames }
  296. if assigned(lenstartlabel) then
  297. internalerror(2004041210);
  298. { Dwarflist is processed, remove items }
  299. DwarfList.Clear;
  300. end;
  301. procedure tdwarfcfi.start_frame(list:taasmoutput);
  302. begin
  303. if assigned(FFrameStartLabel) then
  304. internalerror(200404129);
  305. objectlibrary.getlabel(FFrameStartLabel);
  306. objectlibrary.getlabel(FFrameEndLabel);
  307. FLastloclabel:=FFrameStartLabel;
  308. list.concat(tai_label.create(FFrameStartLabel));
  309. dwarflist.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  310. end;
  311. procedure tdwarfcfi.end_frame(list:taasmoutput);
  312. begin
  313. if not assigned(FFrameStartLabel) then
  314. internalerror(2004041213);
  315. dwarflist.concat(tdwarfitem.create(DW_CFA_end_frame));
  316. list.concat(tai_label.create(FFrameEndLabel));
  317. FFrameStartLabel:=nil;
  318. FFrameEndLabel:=nil;
  319. FLastLocLabel:=nil;
  320. end;
  321. procedure tdwarfcfi.cfa_advance_loc(list:taasmoutput);
  322. var
  323. currloclabel : tasmlabel;
  324. begin
  325. if FLastloclabel=nil then
  326. internalerror(200404082);
  327. objectlibrary.getlabel(currloclabel);
  328. list.concat(tai_label.create(currloclabel));
  329. dwarflist.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  330. FLastloclabel:=currloclabel;
  331. end;
  332. procedure tdwarfcfi.cfa_offset(list:taasmoutput;reg:tregister;ofs:longint);
  333. begin
  334. cfa_advance_loc(list);
  335. {$warning TODO check if ref is a temp}
  336. { offset must be positive }
  337. dwarflist.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  338. end;
  339. procedure tdwarfcfi.cfa_restore(list:taasmoutput;reg:tregister);
  340. begin
  341. cfa_advance_loc(list);
  342. dwarflist.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  343. end;
  344. procedure tdwarfcfi.cfa_def_cfa_register(list:taasmoutput;reg:tregister);
  345. begin
  346. cfa_advance_loc(list);
  347. dwarflist.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  348. end;
  349. procedure tdwarfcfi.cfa_def_cfa_offset(list:taasmoutput;ofs:longint);
  350. begin
  351. cfa_advance_loc(list);
  352. dwarflist.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  353. end;
  354. begin
  355. {$warning TODO Maybe initialize per module}
  356. dwarfcfi:=tdwarfcfi.create;
  357. end.
  358. {
  359. $Log$
  360. Revision 1.4 2004-10-31 21:45:02 peter
  361. * generic tlocation
  362. * move tlocation to cgutils
  363. Revision 1.3 2004/06/20 08:55:29 florian
  364. * logs truncated
  365. Revision 1.2 2004/06/16 20:07:07 florian
  366. * dwarf branch merged
  367. Revision 1.1.2.6 2004/05/01 16:02:09 peter
  368. * POINTER_SIZE replaced with sizeof(aint)
  369. * aint,aword,tconst*int moved to globtype
  370. Revision 1.1.2.5 2004/04/27 18:18:25 peter
  371. * aword -> aint
  372. Revision 1.1.2.4 2004/04/20 16:35:58 peter
  373. * generate dwarf for stackframe entry
  374. Revision 1.1.2.3 2004/04/12 19:34:45 peter
  375. * basic framework for dwarf CFI
  376. }