dwarf2-info.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * DWARF2 debugging format - info and abbreviation tables
  3. *
  4. * Copyright (C) 2006-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <libyasm.h>
  29. #include "dwarf2-dbgfmt.h"
  30. #define DW_LANG_Mips_Assembler 0x8001
  31. /* Tag encodings */
  32. typedef enum {
  33. DW_TAG_padding = 0x00,
  34. DW_TAG_array_type = 0x01,
  35. DW_TAG_class_type = 0x02,
  36. DW_TAG_entry_point = 0x03,
  37. DW_TAG_enumeration_type = 0x04,
  38. DW_TAG_formal_parameter = 0x05,
  39. DW_TAG_imported_declaration = 0x08,
  40. DW_TAG_label = 0x0a,
  41. DW_TAG_lexical_block = 0x0b,
  42. DW_TAG_member = 0x0d,
  43. DW_TAG_pointer_type = 0x0f,
  44. DW_TAG_reference_type = 0x10,
  45. DW_TAG_compile_unit = 0x11,
  46. DW_TAG_string_type = 0x12,
  47. DW_TAG_structure_type = 0x13,
  48. DW_TAG_subroutine_type = 0x15,
  49. DW_TAG_typedef = 0x16,
  50. DW_TAG_union_type = 0x17,
  51. DW_TAG_unspecified_parameters = 0x18,
  52. DW_TAG_variant = 0x19,
  53. DW_TAG_common_block = 0x1a,
  54. DW_TAG_common_inclusion = 0x1b,
  55. DW_TAG_inheritance = 0x1c,
  56. DW_TAG_inlined_subroutine = 0x1d,
  57. DW_TAG_module = 0x1e,
  58. DW_TAG_ptr_to_member_type = 0x1f,
  59. DW_TAG_set_type = 0x20,
  60. DW_TAG_subrange_type = 0x21,
  61. DW_TAG_with_stmt = 0x22,
  62. DW_TAG_access_declaration = 0x23,
  63. DW_TAG_base_type = 0x24,
  64. DW_TAG_catch_block = 0x25,
  65. DW_TAG_const_type = 0x26,
  66. DW_TAG_constant = 0x27,
  67. DW_TAG_enumerator = 0x28,
  68. DW_TAG_file_type = 0x29,
  69. DW_TAG_friend = 0x2a,
  70. DW_TAG_namelist = 0x2b,
  71. DW_TAG_namelist_item = 0x2c,
  72. DW_TAG_packed_type = 0x2d,
  73. DW_TAG_subprogram = 0x2e,
  74. DW_TAG_template_type_param = 0x2f,
  75. DW_TAG_template_value_param = 0x30,
  76. DW_TAG_thrown_type = 0x31,
  77. DW_TAG_try_block = 0x32,
  78. DW_TAG_variant_part = 0x33,
  79. DW_TAG_variable = 0x34,
  80. DW_TAG_volatile_type = 0x35
  81. } dwarf_tag;
  82. /* Attribute form encodings */
  83. typedef enum {
  84. DW_FORM_addr = 0x01,
  85. DW_FORM_block2 = 0x03,
  86. DW_FORM_block4 = 0x04,
  87. DW_FORM_data2 = 0x05,
  88. DW_FORM_data4 = 0x06,
  89. DW_FORM_data8 = 0x07,
  90. DW_FORM_string = 0x08,
  91. DW_FORM_block = 0x09,
  92. DW_FORM_block1 = 0x0a,
  93. DW_FORM_data1 = 0x0b,
  94. DW_FORM_flag = 0x0c,
  95. DW_FORM_sdata = 0x0d,
  96. DW_FORM_strp = 0x0e,
  97. DW_FORM_udata = 0x0f,
  98. DW_FORM_ref_addr = 0x10,
  99. DW_FORM_ref1 = 0x11,
  100. DW_FORM_ref2 = 0x12,
  101. DW_FORM_ref4 = 0x13,
  102. DW_FORM_ref8 = 0x14,
  103. DW_FORM_ref_udata = 0x15,
  104. DW_FORM_indirect = 0x16
  105. } dwarf_form;
  106. /* Attribute encodings */
  107. typedef enum {
  108. DW_AT_sibling = 0x01,
  109. DW_AT_location = 0x02,
  110. DW_AT_name = 0x03,
  111. DW_AT_ordering = 0x09,
  112. DW_AT_subscr_data = 0x0a,
  113. DW_AT_byte_size = 0x0b,
  114. DW_AT_bit_offset = 0x0c,
  115. DW_AT_bit_size = 0x0d,
  116. DW_AT_element_list = 0x0f,
  117. DW_AT_stmt_list = 0x10,
  118. DW_AT_low_pc = 0x11,
  119. DW_AT_high_pc = 0x12,
  120. DW_AT_language = 0x13,
  121. DW_AT_member = 0x14,
  122. DW_AT_discr = 0x15,
  123. DW_AT_discr_value = 0x16,
  124. DW_AT_visibility = 0x17,
  125. DW_AT_import = 0x18,
  126. DW_AT_string_length = 0x19,
  127. DW_AT_common_reference = 0x1a,
  128. DW_AT_comp_dir = 0x1b,
  129. DW_AT_const_value = 0x1c,
  130. DW_AT_containing_type = 0x1d,
  131. DW_AT_default_value = 0x1e,
  132. DW_AT_inline = 0x20,
  133. DW_AT_is_optional = 0x21,
  134. DW_AT_lower_bound = 0x22,
  135. DW_AT_producer = 0x25,
  136. DW_AT_prototyped = 0x27,
  137. DW_AT_return_addr = 0x2a,
  138. DW_AT_start_scope = 0x2c,
  139. DW_AT_stride_size = 0x2e,
  140. DW_AT_upper_bound = 0x2f,
  141. DW_AT_abstract_origin = 0x31,
  142. DW_AT_accessibility = 0x32,
  143. DW_AT_address_class = 0x33,
  144. DW_AT_artificial = 0x34,
  145. DW_AT_base_types = 0x35,
  146. DW_AT_calling_convention = 0x36,
  147. DW_AT_count = 0x37,
  148. DW_AT_data_member_location = 0x38,
  149. DW_AT_decl_column = 0x39,
  150. DW_AT_decl_file = 0x3a,
  151. DW_AT_decl_line = 0x3b,
  152. DW_AT_declaration = 0x3c,
  153. DW_AT_discr_list = 0x3d,
  154. DW_AT_encoding = 0x3e,
  155. DW_AT_external = 0x3f,
  156. DW_AT_frame_base = 0x40,
  157. DW_AT_friend = 0x41,
  158. DW_AT_identifier_case = 0x42,
  159. DW_AT_macro_info = 0x43,
  160. DW_AT_namelist_items = 0x44,
  161. DW_AT_priority = 0x45,
  162. DW_AT_segment = 0x46,
  163. DW_AT_specification = 0x47,
  164. DW_AT_static_link = 0x48,
  165. DW_AT_type = 0x49,
  166. DW_AT_use_location = 0x4a,
  167. DW_AT_variable_parameter = 0x4b,
  168. DW_AT_virtuality = 0x4c,
  169. DW_AT_vtable_elem_location = 0x4d
  170. } dwarf_attribute;
  171. typedef struct dwarf2_abbrev_attr {
  172. STAILQ_ENTRY(dwarf2_abbrev_attr) link;
  173. dwarf_attribute name;
  174. dwarf_form form;
  175. } dwarf2_abbrev_attr;
  176. typedef struct dwarf2_abbrev {
  177. unsigned long id;
  178. dwarf_tag tag;
  179. int has_children;
  180. STAILQ_HEAD(dwarf2_abbrev_attrhead, dwarf2_abbrev_attr) attrs;
  181. } dwarf2_abbrev;
  182. /* Bytecode callback function prototypes */
  183. static void dwarf2_abbrev_bc_destroy(void *contents);
  184. static void dwarf2_abbrev_bc_print(const void *contents, FILE *f,
  185. int indent_level);
  186. static int dwarf2_abbrev_bc_calc_len
  187. (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data);
  188. static int dwarf2_abbrev_bc_tobytes
  189. (yasm_bytecode *bc, unsigned char **bufp, unsigned char *bufstart, void *d,
  190. yasm_output_value_func output_value,
  191. /*@null@*/ yasm_output_reloc_func output_reloc);
  192. /* Bytecode callback structures */
  193. static const yasm_bytecode_callback dwarf2_abbrev_bc_callback = {
  194. dwarf2_abbrev_bc_destroy,
  195. dwarf2_abbrev_bc_print,
  196. yasm_bc_finalize_common,
  197. NULL,
  198. dwarf2_abbrev_bc_calc_len,
  199. yasm_bc_expand_common,
  200. dwarf2_abbrev_bc_tobytes,
  201. 0
  202. };
  203. static unsigned long
  204. dwarf2_add_abbrev_attr(dwarf2_abbrev *abbrev, dwarf_attribute name,
  205. dwarf_form form)
  206. {
  207. dwarf2_abbrev_attr *attr = yasm_xmalloc(sizeof(dwarf2_abbrev_attr));
  208. attr->name = name;
  209. attr->form = form;
  210. STAILQ_INSERT_TAIL(&abbrev->attrs, attr, link);
  211. return yasm_size_uleb128(name) + yasm_size_uleb128(form);
  212. }
  213. static void
  214. dwarf2_append_expr(yasm_section *sect, /*@only@*/ yasm_expr *expr,
  215. unsigned int size, int leb)
  216. {
  217. yasm_datavalhead dvs;
  218. yasm_bytecode *bc;
  219. yasm_dvs_initialize(&dvs);
  220. yasm_dvs_append(&dvs, yasm_dv_create_expr(expr));
  221. if (leb == 0)
  222. bc = yasm_bc_create_data(&dvs, size, 0, NULL, 0);
  223. else
  224. bc = yasm_bc_create_leb128(&dvs, leb<0, 0);
  225. yasm_bc_finalize(bc, yasm_dwarf2__append_bc(sect, bc));
  226. yasm_bc_calc_len(bc, NULL, NULL);
  227. }
  228. static void
  229. dwarf2_append_str(yasm_section *sect, const char *str)
  230. {
  231. yasm_datavalhead dvs;
  232. yasm_bytecode *bc;
  233. yasm_dvs_initialize(&dvs);
  234. yasm_dvs_append(&dvs, yasm_dv_create_string(yasm__xstrdup(str),
  235. strlen(str)));
  236. bc = yasm_bc_create_data(&dvs, 1, 1, NULL, 0);
  237. yasm_bc_finalize(bc, yasm_dwarf2__append_bc(sect, bc));
  238. yasm_bc_calc_len(bc, NULL, NULL);
  239. }
  240. yasm_section *
  241. yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line,
  242. yasm_section *main_code)
  243. {
  244. yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt;
  245. int new;
  246. yasm_bytecode *abc;
  247. dwarf2_abbrev *abbrev;
  248. dwarf2_head *head;
  249. char *buf;
  250. yasm_section *debug_abbrev =
  251. yasm_object_get_general(object, ".debug_abbrev", 4, 0, 0, &new, 0);
  252. yasm_section *debug_info =
  253. yasm_object_get_general(object, ".debug_info", 4, 0, 0, &new, 0);
  254. yasm_section_set_align(debug_abbrev, 0, 0);
  255. yasm_section_set_align(debug_info, 0, 0);
  256. /* Create abbreviation table entry for compilation unit */
  257. abbrev = yasm_xmalloc(sizeof(dwarf2_abbrev));
  258. abc = yasm_bc_create_common(&dwarf2_abbrev_bc_callback, abbrev, 0);
  259. abbrev->id = 1;
  260. abbrev->tag = DW_TAG_compile_unit;
  261. abbrev->has_children = 0;
  262. abc->len = yasm_size_uleb128(abbrev->id) + yasm_size_uleb128(abbrev->tag)
  263. + 3;
  264. STAILQ_INIT(&abbrev->attrs);
  265. yasm_dwarf2__append_bc(debug_abbrev, abc);
  266. /* info header */
  267. head = yasm_dwarf2__add_head(dbgfmt_dwarf2, debug_info, debug_abbrev, 1, 0);
  268. /* Generate abbreviations at the same time as info (since they're linked
  269. * and we're only generating one piece of info).
  270. */
  271. /* generating info using abbrev 1 */
  272. dwarf2_append_expr(debug_info,
  273. yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(1)), 0),
  274. 0, 1);
  275. /* statement list (line numbers) */
  276. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_stmt_list, DW_FORM_data4);
  277. dwarf2_append_expr(debug_info,
  278. yasm_expr_create_ident(yasm_expr_sym(
  279. yasm_dwarf2__bc_sym(object->symtab,
  280. yasm_section_bcs_first(debug_line))), 0),
  281. dbgfmt_dwarf2->sizeof_offset, 0);
  282. if (main_code) {
  283. yasm_symrec *first;
  284. first = yasm_dwarf2__bc_sym(object->symtab,
  285. yasm_section_bcs_first(main_code));
  286. /* All code is contiguous in one section */
  287. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_low_pc, DW_FORM_addr);
  288. dwarf2_append_expr(debug_info,
  289. yasm_expr_create_ident(yasm_expr_sym(first), 0),
  290. dbgfmt_dwarf2->sizeof_address, 0);
  291. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_high_pc, DW_FORM_addr);
  292. dwarf2_append_expr(debug_info,
  293. yasm_expr_create(YASM_EXPR_ADD, yasm_expr_sym(first),
  294. yasm_expr_int(yasm_calc_bc_dist(
  295. yasm_section_bcs_first(main_code),
  296. yasm_section_bcs_last(main_code))), 0),
  297. dbgfmt_dwarf2->sizeof_address, 0);
  298. }
  299. /* input filename */
  300. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_name, DW_FORM_string);
  301. if (!object->deb_filename) {
  302. object->deb_filename = yasm_replace_path(
  303. dbgfmt_dwarf2->dbgfmt.module->replace_map, dbgfmt_dwarf2->dbgfmt.module->replace_map_size,
  304. object->src_filename, strlen(object->src_filename));
  305. }
  306. dwarf2_append_str(debug_info, object->deb_filename);
  307. /* compile directory (current working directory) */
  308. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_comp_dir, DW_FORM_string);
  309. buf = yasm__getcwd();
  310. char * new_cwd_name = yasm_replace_path(
  311. dbgfmt_dwarf2->dbgfmt.module->replace_map, dbgfmt_dwarf2->dbgfmt.module->replace_map_size,
  312. buf, strlen(buf));
  313. dwarf2_append_str(debug_info, new_cwd_name);
  314. yasm_xfree(new_cwd_name);
  315. yasm_xfree(buf);
  316. /* producer - assembler name */
  317. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_producer, DW_FORM_string);
  318. if (getenv("YASM_TEST_SUITE"))
  319. dwarf2_append_str(debug_info, "yasm HEAD");
  320. else
  321. dwarf2_append_str(debug_info, PACKAGE_STRING);
  322. /* language - no standard code for assembler, use MIPS as a substitute */
  323. abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_language, DW_FORM_data2);
  324. dwarf2_append_expr(debug_info,
  325. yasm_expr_create_ident(yasm_expr_int(
  326. yasm_intnum_create_uint(DW_LANG_Mips_Assembler)), 0), 2, 0);
  327. /* Terminate list of abbreviations */
  328. abbrev = yasm_xmalloc(sizeof(dwarf2_abbrev));
  329. abc = yasm_bc_create_common(&dwarf2_abbrev_bc_callback, abbrev, 0);
  330. abbrev->id = 0;
  331. abbrev->tag = 0;
  332. abbrev->has_children = 0;
  333. STAILQ_INIT(&abbrev->attrs);
  334. abc->len = 1;
  335. yasm_dwarf2__append_bc(debug_abbrev, abc);
  336. /* mark end of info */
  337. yasm_dwarf2__set_head_end(head, yasm_section_bcs_last(debug_info));
  338. return debug_info;
  339. }
  340. static void
  341. dwarf2_abbrev_bc_destroy(void *contents)
  342. {
  343. dwarf2_abbrev *abbrev = (dwarf2_abbrev *)contents;
  344. dwarf2_abbrev_attr *n1, *n2;
  345. /* Delete attributes */
  346. n1 = STAILQ_FIRST(&abbrev->attrs);
  347. while (n1) {
  348. n2 = STAILQ_NEXT(n1, link);
  349. yasm_xfree(n1);
  350. n1 = n2;
  351. }
  352. yasm_xfree(contents);
  353. }
  354. static void
  355. dwarf2_abbrev_bc_print(const void *contents, FILE *f, int indent_level)
  356. {
  357. /* TODO */
  358. }
  359. static int
  360. dwarf2_abbrev_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  361. void *add_span_data)
  362. {
  363. yasm_internal_error(N_("tried to calc_len a dwarf2 aranges head bytecode"));
  364. /*@notreached@*/
  365. return 0;
  366. }
  367. static int
  368. dwarf2_abbrev_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  369. unsigned char *bufstart, void *d,
  370. yasm_output_value_func output_value,
  371. yasm_output_reloc_func output_reloc)
  372. {
  373. dwarf2_abbrev *abbrev = (dwarf2_abbrev *)bc->contents;
  374. unsigned char *buf = *bufp;
  375. dwarf2_abbrev_attr *attr;
  376. if (abbrev->id == 0) {
  377. YASM_WRITE_8(buf, 0);
  378. *bufp = buf;
  379. return 0;
  380. }
  381. buf += yasm_get_uleb128(abbrev->id, buf);
  382. buf += yasm_get_uleb128(abbrev->tag, buf);
  383. YASM_WRITE_8(buf, abbrev->has_children);
  384. STAILQ_FOREACH(attr, &abbrev->attrs, link) {
  385. buf += yasm_get_uleb128(attr->name, buf);
  386. buf += yasm_get_uleb128(attr->form, buf);
  387. }
  388. YASM_WRITE_8(buf, 0);
  389. YASM_WRITE_8(buf, 0);
  390. *bufp = buf;
  391. return 0;
  392. }