dwarf.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. {****************************************************************************
  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. Fal_dwarf:=TLinkedList.Create;
  111. end;
  112. destructor tdwarf.destroy;
  113. begin
  114. Fal_dwarf.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. begin
  175. list.concat(tai_const.create_8bit(op));
  176. for i:=0 to ops-1 do
  177. begin
  178. case oper[i].typ of
  179. dop_const :
  180. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  181. dop_sym :
  182. begin
  183. if oper[i].enc<>doe_ptr then
  184. internalerror(200404127);
  185. list.concat(tai_const.create_sym(oper[i].sym));
  186. end;
  187. dop_reloffset :
  188. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  189. dop_reg :
  190. list.concat(tai_const.create(enc2ait_const[oper[i].enc],regdwarf_table[findreg_by_number(oper[i].register)]));
  191. else
  192. internalerror(200404128);
  193. end;
  194. end;
  195. end;
  196. {****************************************************************************
  197. TDWARFCFI
  198. ****************************************************************************}
  199. constructor tdwarfcfi.create;
  200. begin
  201. inherited create;
  202. FFrameStartLabel:=nil;
  203. FFrameEndLabel:=nil;
  204. FLastLocLabel:=nil;
  205. code_alignment_factor:=1;
  206. data_alignment_factor:=-4;
  207. end;
  208. {$ifdef i386}
  209. { if more cpu dependend stuff is implemented, this needs more refactoring }
  210. procedure tdwarfcfi.generate_initial_instructions(list:taasmoutput);
  211. begin
  212. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  213. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  214. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  215. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  216. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  217. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  218. end;
  219. {$else i386}
  220. { if more cpu dependend stuff is implemented, this needs more refactoring }
  221. procedure tdwarfcfi.generate_initial_instructions(list:taasmoutput);
  222. begin
  223. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  224. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  225. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  226. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  227. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  228. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  229. end;
  230. {$endif i386}
  231. procedure tdwarfcfi.generate_code(list:taasmoutput);
  232. var
  233. hp : tdwarfitem;
  234. cielabel,
  235. lenstartlabel,
  236. lenendlabel : tasmlabel;
  237. tc : tai_const;
  238. begin
  239. new_section(list,sec_debug_frame,'',0);
  240. { CIE
  241. DWORD length
  242. DWORD CIE_Id = 0xffffffff
  243. BYTE version = 1
  244. STRING augmentation = "" = BYTE 0
  245. ULEB128 code alignment factor = 1
  246. ULEB128 data alignment factor = -1
  247. BYTE return address register
  248. <...> start sequence
  249. }
  250. objectlibrary.getjumplabel(cielabel);
  251. list.concat(tai_label.create(cielabel));
  252. objectlibrary.getjumplabel(lenstartlabel);
  253. objectlibrary.getjumplabel(lenendlabel);
  254. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  255. list.concat(tai_label.create(lenstartlabel));
  256. list.concat(tai_const.create_32bit(longint($ffffffff)));
  257. list.concat(tai_const.create_8bit(1));
  258. list.concat(tai_const.create_8bit(0)); { empty string }
  259. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  260. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  261. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  262. { Generate standard code
  263. def_cfa(stackpointer,sizeof(aint))
  264. cfa_offset_extended(returnaddres,-sizeof(aint))
  265. }
  266. generate_initial_instructions(list);
  267. list.concat(cai_align.create_zeros(4));
  268. list.concat(tai_label.create(lenendlabel));
  269. lenstartlabel:=nil;
  270. lenendlabel:=nil;
  271. hp:=TDwarfItem(al_dwarf.first);
  272. while assigned(hp) do
  273. begin
  274. case hp.op of
  275. DW_CFA_Start_Frame :
  276. begin
  277. if assigned(lenstartlabel) then
  278. internalerror(200404125);
  279. if (hp.ops<>1) or
  280. (hp.oper[0].typ<>dop_reloffset) then
  281. internalerror(200404126);
  282. objectlibrary.getjumplabel(lenstartlabel);
  283. objectlibrary.getjumplabel(lenendlabel);
  284. { FDE
  285. DWORD length
  286. DWORD CIE-pointer = cielabel
  287. PTRSIZE initial location = oper[0]
  288. PTRSIZE function size = oper[1]
  289. }
  290. list.concat(tai_const.create_rel_sym(ait_const_32bit,lenstartlabel,lenendlabel));
  291. list.concat(tai_label.create(lenstartlabel));
  292. { force label offset to 32bit }
  293. tc:=tai_const.create_sym(cielabel);
  294. tc.typ:=ait_const_32bit;
  295. list.concat(tc);
  296. list.concat(tai_const.create_sym(hp.oper[0].beginsym));
  297. list.concat(tai_const.create_rel_sym(ait_const_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  298. end;
  299. DW_CFA_End_Frame :
  300. begin
  301. list.concat(cai_align.create_zeros(4));
  302. list.concat(tai_label.create(lenendlabel));
  303. lenstartlabel:=nil;
  304. lenendlabel:=nil;
  305. end;
  306. else
  307. hp.generate_code(list);
  308. end;
  309. hp:=TDwarfItem(hp.next);
  310. end;
  311. { Check for open frames }
  312. if assigned(lenstartlabel) then
  313. internalerror(2004041210);
  314. { al_dwarf is processed, remove items }
  315. al_dwarf.Clear;
  316. end;
  317. procedure tdwarfcfi.start_frame(list:taasmoutput);
  318. begin
  319. if assigned(FFrameStartLabel) then
  320. internalerror(200404129);
  321. objectlibrary.getjumplabel(FFrameStartLabel);
  322. objectlibrary.getjumplabel(FFrameEndLabel);
  323. FLastloclabel:=FFrameStartLabel;
  324. list.concat(tai_label.create(FFrameStartLabel));
  325. al_dwarf.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,FFrameStartLabel,FFrameEndLabel));
  326. end;
  327. procedure tdwarfcfi.end_frame(list:taasmoutput);
  328. begin
  329. if not assigned(FFrameStartLabel) then
  330. internalerror(2004041213);
  331. al_dwarf.concat(tdwarfitem.create(DW_CFA_end_frame));
  332. list.concat(tai_label.create(FFrameEndLabel));
  333. FFrameStartLabel:=nil;
  334. FFrameEndLabel:=nil;
  335. FLastLocLabel:=nil;
  336. end;
  337. procedure tdwarfcfi.cfa_advance_loc(list:taasmoutput);
  338. var
  339. currloclabel : tasmlabel;
  340. begin
  341. if FLastloclabel=nil then
  342. internalerror(200404082);
  343. objectlibrary.getjumplabel(currloclabel);
  344. list.concat(tai_label.create(currloclabel));
  345. al_dwarf.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  346. FLastloclabel:=currloclabel;
  347. end;
  348. procedure tdwarfcfi.cfa_offset(list:taasmoutput;reg:tregister;ofs:longint);
  349. begin
  350. cfa_advance_loc(list);
  351. {$warning TODO check if ref is a temp}
  352. { offset must be positive }
  353. al_dwarf.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  354. end;
  355. procedure tdwarfcfi.cfa_restore(list:taasmoutput;reg:tregister);
  356. begin
  357. cfa_advance_loc(list);
  358. al_dwarf.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  359. end;
  360. procedure tdwarfcfi.cfa_def_cfa_register(list:taasmoutput;reg:tregister);
  361. begin
  362. cfa_advance_loc(list);
  363. al_dwarf.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  364. end;
  365. procedure tdwarfcfi.cfa_def_cfa_offset(list:taasmoutput;ofs:longint);
  366. begin
  367. cfa_advance_loc(list);
  368. al_dwarf.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  369. end;
  370. begin
  371. {$warning TODO Maybe initialize per module}
  372. dwarfcfi:=tdwarfcfi.create;
  373. end.