dwarf.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. cpuinfo;
  85. const
  86. { Call frame information }
  87. DW_CFA_set_loc = $01;
  88. DW_CFA_advance_loc1 = $02;
  89. DW_CFA_advance_loc2 = $03;
  90. DW_CFA_advance_loc4 = $04;
  91. DW_CFA_offset_extended = $05;
  92. DW_CFA_restore_extended = $06;
  93. DW_CFA_def_cfa = $0c;
  94. DW_CFA_def_cfa_register = $0d;
  95. DW_CFA_def_cfa_offset = $0e;
  96. { Own additions }
  97. DW_CFA_start_frame = $f0;
  98. DW_CFA_end_frame = $f1;
  99. {****************************************************************************
  100. Helpers
  101. ****************************************************************************}
  102. function dwarf_reg(r:tregister):longint;
  103. begin
  104. result:=regdwarf_table[findreg_by_number(r)];
  105. end;
  106. {****************************************************************************
  107. TDWARF
  108. ****************************************************************************}
  109. constructor tdwarf.create;
  110. begin
  111. FDwarfList:=TLinkedList.Create;
  112. end;
  113. destructor tdwarf.destroy;
  114. begin
  115. FDwarfList.Free;
  116. end;
  117. {****************************************************************************
  118. TDWARFITEM
  119. ****************************************************************************}
  120. constructor tdwarfitem.create(aop:longint);
  121. begin
  122. inherited create;
  123. op:=aop;
  124. ops:=0;
  125. end;
  126. constructor tdwarfitem.create_reg(aop:longint;enc1:tdwarfoperenc;reg:tregister);
  127. begin
  128. inherited create;
  129. op:=aop;
  130. ops:=1;
  131. oper[0].typ:=dop_reg;
  132. oper[0].enc:=enc1;
  133. oper[0].register:=reg;
  134. end;
  135. constructor tdwarfitem.create_const(aop:longint;enc1:tdwarfoperenc;val:int64);
  136. begin
  137. inherited create;
  138. op:=aop;
  139. ops:=1;
  140. oper[0].typ:=dop_const;
  141. oper[0].enc:=enc1;
  142. oper[0].value:=val;
  143. end;
  144. constructor tdwarfitem.create_reloffset(aop:longint;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  145. begin
  146. inherited create;
  147. op:=aop;
  148. ops:=1;
  149. { relative offsets are passed }
  150. oper[0].typ:=dop_reloffset;
  151. oper[0].enc:=enc1;
  152. oper[0].beginsym:=beginlab;
  153. oper[0].endsym:=endlab;
  154. end;
  155. constructor tdwarfitem.create_reg_const(aop:longint;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  156. begin
  157. inherited create;
  158. op:=aop;
  159. ops:=2;
  160. oper[0].typ:=dop_reg;
  161. oper[0].enc:=enc1;
  162. oper[0].register:=reg;
  163. oper[1].typ:=dop_const;
  164. oper[1].enc:=enc2;
  165. oper[1].value:=val;
  166. end;
  167. procedure tdwarfitem.generate_code(list:taasmoutput);
  168. const
  169. enc2ait_const : array[tdwarfoperenc] of taitype = (
  170. ait_const_uleb128bit,ait_const_sleb128bit,ait_const_ptr,
  171. ait_const_32bit,ait_const_16bit,ait_const_8bit
  172. );
  173. var
  174. i : integer;
  175. v : int64;
  176. begin
  177. list.concat(tai_const.create_8bit(op));
  178. for i:=0 to ops-1 do
  179. begin
  180. case oper[i].typ of
  181. dop_const :
  182. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  183. dop_sym :
  184. begin
  185. if oper[i].enc<>doe_ptr then
  186. internalerror(200404127);
  187. list.concat(tai_const.create_sym(oper[i].sym));
  188. end;
  189. dop_reloffset :
  190. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  191. dop_reg :
  192. list.concat(tai_const.create(enc2ait_const[oper[i].enc],regdwarf_table[findreg_by_number(oper[i].register)]));
  193. else
  194. internalerror(200404128);
  195. end;
  196. end;
  197. end;
  198. {****************************************************************************
  199. TDWARFCFI
  200. ****************************************************************************}
  201. constructor tdwarfcfi.create;
  202. begin
  203. inherited create;
  204. FFrameStartLabel:=nil;
  205. FFrameEndLabel:=nil;
  206. FLastLocLabel:=nil;
  207. code_alignment_factor:=1;
  208. data_alignment_factor:=-1;
  209. end;
  210. procedure tdwarfcfi.generate_code(list:taasmoutput);
  211. var
  212. hp : tdwarfitem;
  213. cielabel,
  214. lenstartlabel,
  215. lenendlabel : tasmlabel;
  216. tc : tai_const;
  217. begin
  218. new_section(list,sec_debug_frame,'',0);
  219. { CIE
  220. DWORD length
  221. DWORD CIE_Id = 0xffffffff
  222. BYTE version = 1
  223. STRING augmentation = "" = BYTE 0
  224. ULEB128 code alignment factor = 1
  225. ULEB128 data alignment factor = -1
  226. BYTE return address register
  227. <...> start sequence
  228. }
  229. objectlibrary.getlabel(cielabel);
  230. list.concat(tai_label.create(cielabel));
  231. objectlibrary.getlabel(lenstartlabel);
  232. objectlibrary.getlabel(lenendlabel);
  233. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  234. list.concat(tai_label.create(lenstartlabel));
  235. list.concat(tai_const.create_32bit(longint($ffffffff)));
  236. list.concat(tai_const.create_8bit(1));
  237. list.concat(tai_const.create_8bit(0)); { empty string }
  238. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  239. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  240. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  241. { Generate standard code
  242. def_cfa(stackpointer,sizeof(aint))
  243. cfa_offset_extended(returnaddres,-sizeof(aint))
  244. }
  245. {$warning TODO This needs to be target dependent}
  246. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  247. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  248. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  249. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  250. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  251. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  252. list.concat(cai_align.create_zeros(4));
  253. list.concat(tai_label.create(lenendlabel));
  254. lenstartlabel:=nil;
  255. lenendlabel:=nil;
  256. hp:=TDwarfItem(Dwarflist.first);
  257. while assigned(hp) do
  258. begin
  259. case hp.op of
  260. DW_CFA_Start_Frame :
  261. begin
  262. if assigned(lenstartlabel) then
  263. internalerror(200404125);
  264. if (hp.ops<>1) or
  265. (hp.oper[0].typ<>dop_reloffset) then
  266. internalerror(200404126);
  267. objectlibrary.getlabel(lenstartlabel);
  268. objectlibrary.getlabel(lenendlabel);
  269. { FDE
  270. DWORD length
  271. DWORD CIE-pointer = cielabel
  272. PTRSIZE initial location = oper[0]
  273. PTRSIZE function size = oper[1]
  274. }
  275. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  276. list.concat(tai_label.create(lenstartlabel));
  277. { force label offset to 32bit }
  278. tc:=tai_const.create_sym(cielabel);
  279. tc.typ:=ait_const_32bit;
  280. list.concat(tc);
  281. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  282. list.concat(tai_const.create_rel_sym(ait_const_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  283. end;
  284. DW_CFA_End_Frame :
  285. begin
  286. list.concat(cai_align.create_zeros(4));
  287. list.concat(tai_label.create(lenendlabel));
  288. lenstartlabel:=nil;
  289. lenendlabel:=nil;
  290. end;
  291. else
  292. hp.generate_code(list);
  293. end;
  294. hp:=TDwarfItem(hp.next);
  295. end;
  296. { Check for open frames }
  297. if assigned(lenstartlabel) then
  298. internalerror(2004041210);
  299. { Dwarflist is processed, remove items }
  300. DwarfList.Clear;
  301. end;
  302. procedure tdwarfcfi.start_frame(list:taasmoutput);
  303. begin
  304. if assigned(FFrameStartLabel) then
  305. internalerror(200404129);
  306. objectlibrary.getlabel(FFrameStartLabel);
  307. objectlibrary.getlabel(FFrameEndLabel);
  308. FLastloclabel:=FFrameStartLabel;
  309. list.concat(tai_label.create(FFrameStartLabel));
  310. dwarflist.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  311. end;
  312. procedure tdwarfcfi.end_frame(list:taasmoutput);
  313. begin
  314. if not assigned(FFrameStartLabel) then
  315. internalerror(2004041213);
  316. dwarflist.concat(tdwarfitem.create(DW_CFA_end_frame));
  317. list.concat(tai_label.create(FFrameEndLabel));
  318. FFrameStartLabel:=nil;
  319. FFrameEndLabel:=nil;
  320. FLastLocLabel:=nil;
  321. end;
  322. procedure tdwarfcfi.cfa_advance_loc(list:taasmoutput);
  323. var
  324. currloclabel : tasmlabel;
  325. begin
  326. if FLastloclabel=nil then
  327. internalerror(200404082);
  328. objectlibrary.getlabel(currloclabel);
  329. list.concat(tai_label.create(currloclabel));
  330. dwarflist.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  331. FLastloclabel:=currloclabel;
  332. end;
  333. procedure tdwarfcfi.cfa_offset(list:taasmoutput;reg:tregister;ofs:longint);
  334. begin
  335. cfa_advance_loc(list);
  336. {$warning TODO check if ref is a temp}
  337. { offset must be positive }
  338. dwarflist.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  339. end;
  340. procedure tdwarfcfi.cfa_restore(list:taasmoutput;reg:tregister);
  341. begin
  342. cfa_advance_loc(list);
  343. dwarflist.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  344. end;
  345. procedure tdwarfcfi.cfa_def_cfa_register(list:taasmoutput;reg:tregister);
  346. begin
  347. cfa_advance_loc(list);
  348. dwarflist.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  349. end;
  350. procedure tdwarfcfi.cfa_def_cfa_offset(list:taasmoutput;ofs:longint);
  351. begin
  352. cfa_advance_loc(list);
  353. dwarflist.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  354. end;
  355. begin
  356. {$warning TODO Maybe initialize per module}
  357. dwarfcfi:=tdwarfcfi.create;
  358. end.
  359. {
  360. $Log$
  361. Revision 1.3 2004-06-20 08:55:29 florian
  362. * logs truncated
  363. Revision 1.2 2004/06/16 20:07:07 florian
  364. * dwarf branch merged
  365. Revision 1.1.2.6 2004/05/01 16:02:09 peter
  366. * POINTER_SIZE replaced with sizeof(aint)
  367. * aint,aword,tconst*int moved to globtype
  368. Revision 1.1.2.5 2004/04/27 18:18:25 peter
  369. * aword -> aint
  370. Revision 1.1.2.4 2004/04/20 16:35:58 peter
  371. * generate dwarf for stackframe entry
  372. Revision 1.1.2.3 2004/04/12 19:34:45 peter
  373. * basic framework for dwarf CFI
  374. }