dwarf.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 dwarf;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,
  23. cgbase,cpubase,
  24. aasmbase,aasmtai;
  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:longint);
  43. constructor create_reg(aop:longint;enc1:tdwarfoperenc;reg:tregister);
  44. constructor create_const(aop:longint;enc1:tdwarfoperenc;val:int64);
  45. constructor create_reloffset(aop:longint;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  46. constructor create_reg_const(aop:longint;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  47. procedure generate_code(list:taasmoutput);
  48. end;
  49. tdwarf=class
  50. private
  51. Fal_dwarf : TLinkedList;
  52. public
  53. constructor create;
  54. destructor destroy;override;
  55. property al_dwarf:TlinkedList read Fal_dwarf;
  56. end;
  57. tdwarfcfi=class(tdwarf)
  58. private
  59. FFrameStartLabel,
  60. FFrameEndLabel,
  61. FLastloclabel : tasmlabel;
  62. procedure cfa_advance_loc(list:taasmoutput);
  63. protected
  64. code_alignment_factor,
  65. data_alignment_factor : shortint;
  66. public
  67. constructor create;
  68. procedure generate_code(list:taasmoutput);
  69. { operations }
  70. procedure start_frame(list:taasmoutput);
  71. procedure end_frame(list:taasmoutput);
  72. procedure cfa_offset(list:taasmoutput;reg:tregister;ofs:longint);
  73. procedure cfa_restore(list:taasmoutput;reg:tregister);
  74. procedure cfa_def_cfa_register(list:taasmoutput;reg:tregister);
  75. procedure cfa_def_cfa_offset(list:taasmoutput;ofs:longint);
  76. end;
  77. var
  78. dwarfcfi : tdwarfcfi;
  79. function dwarf_reg(r:tregister):longint;
  80. implementation
  81. uses
  82. verbose;
  83. const
  84. { Call frame information }
  85. DW_CFA_set_loc = $01;
  86. DW_CFA_advance_loc1 = $02;
  87. DW_CFA_advance_loc2 = $03;
  88. DW_CFA_advance_loc4 = $04;
  89. DW_CFA_offset_extended = $05;
  90. DW_CFA_restore_extended = $06;
  91. DW_CFA_def_cfa = $0c;
  92. DW_CFA_def_cfa_register = $0d;
  93. DW_CFA_def_cfa_offset = $0e;
  94. { Own additions }
  95. DW_CFA_start_frame = $f0;
  96. DW_CFA_end_frame = $f1;
  97. {****************************************************************************
  98. Helpers
  99. ****************************************************************************}
  100. function dwarf_reg(r:tregister):longint;
  101. begin
  102. result:=regdwarf_table[findreg_by_number(r)];
  103. end;
  104. {****************************************************************************
  105. TDWARF
  106. ****************************************************************************}
  107. constructor tdwarf.create;
  108. begin
  109. Fal_dwarf:=TLinkedList.Create;
  110. end;
  111. destructor tdwarf.destroy;
  112. begin
  113. Fal_dwarf.Free;
  114. end;
  115. {****************************************************************************
  116. TDWARFITEM
  117. ****************************************************************************}
  118. constructor tdwarfitem.create(aop:longint);
  119. begin
  120. inherited create;
  121. op:=aop;
  122. ops:=0;
  123. end;
  124. constructor tdwarfitem.create_reg(aop:longint;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:longint;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:longint;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:longint;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:taasmoutput);
  166. const
  167. enc2ait_const : array[tdwarfoperenc] of taitype = (
  168. ait_const_uleb128bit,ait_const_sleb128bit,ait_const_ptr,
  169. ait_const_32bit,ait_const_16bit,ait_const_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. TDWARFCFI
  197. ****************************************************************************}
  198. constructor tdwarfcfi.create;
  199. begin
  200. inherited create;
  201. FFrameStartLabel:=nil;
  202. FFrameEndLabel:=nil;
  203. FLastLocLabel:=nil;
  204. code_alignment_factor:=1;
  205. data_alignment_factor:=-1;
  206. end;
  207. procedure tdwarfcfi.generate_code(list:taasmoutput);
  208. var
  209. hp : tdwarfitem;
  210. cielabel,
  211. lenstartlabel,
  212. lenendlabel : tasmlabel;
  213. tc : tai_const;
  214. begin
  215. new_section(list,sec_debug_frame,'',0);
  216. { CIE
  217. DWORD length
  218. DWORD CIE_Id = 0xffffffff
  219. BYTE version = 1
  220. STRING augmentation = "" = BYTE 0
  221. ULEB128 code alignment factor = 1
  222. ULEB128 data alignment factor = -1
  223. BYTE return address register
  224. <...> start sequence
  225. }
  226. objectlibrary.getjumplabel(cielabel);
  227. list.concat(tai_label.create(cielabel));
  228. objectlibrary.getjumplabel(lenstartlabel);
  229. objectlibrary.getjumplabel(lenendlabel);
  230. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  231. list.concat(tai_label.create(lenstartlabel));
  232. list.concat(tai_const.create_32bit(longint($ffffffff)));
  233. list.concat(tai_const.create_8bit(1));
  234. list.concat(tai_const.create_8bit(0)); { empty string }
  235. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  236. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  237. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  238. { Generate standard code
  239. def_cfa(stackpointer,sizeof(aint))
  240. cfa_offset_extended(returnaddres,-sizeof(aint))
  241. }
  242. {$warning TODO This needs to be target dependent}
  243. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  244. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  245. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  246. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  247. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  248. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  249. list.concat(cai_align.create_zeros(4));
  250. list.concat(tai_label.create(lenendlabel));
  251. lenstartlabel:=nil;
  252. lenendlabel:=nil;
  253. hp:=TDwarfItem(al_dwarf.first);
  254. while assigned(hp) do
  255. begin
  256. case hp.op of
  257. DW_CFA_Start_Frame :
  258. begin
  259. if assigned(lenstartlabel) then
  260. internalerror(200404125);
  261. if (hp.ops<>1) or
  262. (hp.oper[0].typ<>dop_reloffset) then
  263. internalerror(200404126);
  264. objectlibrary.getjumplabel(lenstartlabel);
  265. objectlibrary.getjumplabel(lenendlabel);
  266. { FDE
  267. DWORD length
  268. DWORD CIE-pointer = cielabel
  269. PTRSIZE initial location = oper[0]
  270. PTRSIZE function size = oper[1]
  271. }
  272. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  273. list.concat(tai_label.create(lenstartlabel));
  274. { force label offset to 32bit }
  275. tc:=tai_const.create_sym(cielabel);
  276. tc.typ:=ait_const_32bit;
  277. list.concat(tc);
  278. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  279. list.concat(tai_const.create_rel_sym(ait_const_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  280. end;
  281. DW_CFA_End_Frame :
  282. begin
  283. list.concat(cai_align.create_zeros(4));
  284. list.concat(tai_label.create(lenendlabel));
  285. lenstartlabel:=nil;
  286. lenendlabel:=nil;
  287. end;
  288. else
  289. hp.generate_code(list);
  290. end;
  291. hp:=TDwarfItem(hp.next);
  292. end;
  293. { Check for open frames }
  294. if assigned(lenstartlabel) then
  295. internalerror(2004041210);
  296. { al_dwarf is processed, remove items }
  297. al_dwarf.Clear;
  298. end;
  299. procedure tdwarfcfi.start_frame(list:taasmoutput);
  300. begin
  301. if assigned(FFrameStartLabel) then
  302. internalerror(200404129);
  303. objectlibrary.getjumplabel(FFrameStartLabel);
  304. objectlibrary.getjumplabel(FFrameEndLabel);
  305. FLastloclabel:=FFrameStartLabel;
  306. list.concat(tai_label.create(FFrameStartLabel));
  307. al_dwarf.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  308. end;
  309. procedure tdwarfcfi.end_frame(list:taasmoutput);
  310. begin
  311. if not assigned(FFrameStartLabel) then
  312. internalerror(2004041213);
  313. al_dwarf.concat(tdwarfitem.create(DW_CFA_end_frame));
  314. list.concat(tai_label.create(FFrameEndLabel));
  315. FFrameStartLabel:=nil;
  316. FFrameEndLabel:=nil;
  317. FLastLocLabel:=nil;
  318. end;
  319. procedure tdwarfcfi.cfa_advance_loc(list:taasmoutput);
  320. var
  321. currloclabel : tasmlabel;
  322. begin
  323. if FLastloclabel=nil then
  324. internalerror(200404082);
  325. objectlibrary.getjumplabel(currloclabel);
  326. list.concat(tai_label.create(currloclabel));
  327. al_dwarf.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  328. FLastloclabel:=currloclabel;
  329. end;
  330. procedure tdwarfcfi.cfa_offset(list:taasmoutput;reg:tregister;ofs:longint);
  331. begin
  332. cfa_advance_loc(list);
  333. {$warning TODO check if ref is a temp}
  334. { offset must be positive }
  335. al_dwarf.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  336. end;
  337. procedure tdwarfcfi.cfa_restore(list:taasmoutput;reg:tregister);
  338. begin
  339. cfa_advance_loc(list);
  340. al_dwarf.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  341. end;
  342. procedure tdwarfcfi.cfa_def_cfa_register(list:taasmoutput;reg:tregister);
  343. begin
  344. cfa_advance_loc(list);
  345. al_dwarf.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  346. end;
  347. procedure tdwarfcfi.cfa_def_cfa_offset(list:taasmoutput;ofs:longint);
  348. begin
  349. cfa_advance_loc(list);
  350. al_dwarf.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  351. end;
  352. begin
  353. {$warning TODO Maybe initialize per module}
  354. dwarfcfi:=tdwarfcfi.create;
  355. end.