2
0

dwarf.pas 15 KB

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