dwarf2-dbgfmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * DWARF2 debugging format
  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. struct dwarf2_head {
  31. yasm_bytecode *start_prevbc;
  32. yasm_bytecode *end_prevbc;
  33. /*@null@*/ yasm_section *debug_ptr;
  34. int with_address;
  35. int with_segment;
  36. };
  37. /* Bytecode callback function prototypes */
  38. static void dwarf2_head_bc_destroy(void *contents);
  39. static void dwarf2_head_bc_print(const void *contents, FILE *f,
  40. int indent_level);
  41. static int dwarf2_head_bc_calc_len
  42. (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data);
  43. static int dwarf2_head_bc_tobytes
  44. (yasm_bytecode *bc, unsigned char **bufp, unsigned char *bufstart, void *d,
  45. yasm_output_value_func output_value,
  46. /*@null@*/ yasm_output_reloc_func output_reloc);
  47. /* Bytecode callback structures */
  48. static const yasm_bytecode_callback dwarf2_head_bc_callback = {
  49. dwarf2_head_bc_destroy,
  50. dwarf2_head_bc_print,
  51. yasm_bc_finalize_common,
  52. NULL,
  53. dwarf2_head_bc_calc_len,
  54. yasm_bc_expand_common,
  55. dwarf2_head_bc_tobytes,
  56. 0
  57. };
  58. /* Section data callback function prototypes */
  59. static void dwarf2_section_data_destroy(void *data);
  60. static void dwarf2_section_data_print(void *data, FILE *f, int indent_level);
  61. /* Section data callback */
  62. const yasm_assoc_data_callback yasm_dwarf2__section_data_cb = {
  63. dwarf2_section_data_destroy,
  64. dwarf2_section_data_print
  65. };
  66. yasm_dbgfmt_module yasm_dwarf2_LTX_dbgfmt;
  67. static /*@null@*/ /*@only@*/ yasm_dbgfmt *
  68. dwarf2_dbgfmt_create(yasm_object *object)
  69. {
  70. yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 =
  71. yasm_xmalloc(sizeof(yasm_dbgfmt_dwarf2));
  72. size_t i;
  73. dbgfmt_dwarf2->dbgfmt.module = &yasm_dwarf2_LTX_dbgfmt;
  74. dbgfmt_dwarf2->dirs_allocated = 32;
  75. dbgfmt_dwarf2->dirs_size = 0;
  76. dbgfmt_dwarf2->dirs =
  77. yasm_xmalloc(sizeof(char *)*dbgfmt_dwarf2->dirs_allocated);
  78. dbgfmt_dwarf2->filenames_allocated = 32;
  79. dbgfmt_dwarf2->filenames_size = 0;
  80. dbgfmt_dwarf2->filenames =
  81. yasm_xmalloc(sizeof(dwarf2_filename)*dbgfmt_dwarf2->filenames_allocated);
  82. for (i=0; i<dbgfmt_dwarf2->filenames_allocated; i++) {
  83. dbgfmt_dwarf2->filenames[i].pathname = NULL;
  84. dbgfmt_dwarf2->filenames[i].filename = NULL;
  85. dbgfmt_dwarf2->filenames[i].dir = 0;
  86. }
  87. dbgfmt_dwarf2->format = DWARF2_FORMAT_32BIT; /* TODO: flexible? */
  88. dbgfmt_dwarf2->sizeof_address = yasm_arch_get_address_size(object->arch)/8;
  89. switch (dbgfmt_dwarf2->format) {
  90. case DWARF2_FORMAT_32BIT:
  91. dbgfmt_dwarf2->sizeof_offset = 4;
  92. break;
  93. case DWARF2_FORMAT_64BIT:
  94. dbgfmt_dwarf2->sizeof_offset = 8;
  95. break;
  96. }
  97. dbgfmt_dwarf2->min_insn_len = yasm_arch_min_insn_len(object->arch);
  98. return (yasm_dbgfmt *)dbgfmt_dwarf2;
  99. }
  100. static void
  101. dwarf2_dbgfmt_destroy(/*@only@*/ yasm_dbgfmt *dbgfmt)
  102. {
  103. yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)dbgfmt;
  104. size_t i;
  105. for (i=0; i<dbgfmt_dwarf2->dirs_size; i++)
  106. if (dbgfmt_dwarf2->dirs[i])
  107. yasm_xfree(dbgfmt_dwarf2->dirs[i]);
  108. yasm_xfree(dbgfmt_dwarf2->dirs);
  109. for (i=0; i<dbgfmt_dwarf2->filenames_size; i++) {
  110. if (dbgfmt_dwarf2->filenames[i].pathname)
  111. yasm_xfree(dbgfmt_dwarf2->filenames[i].pathname);
  112. if (dbgfmt_dwarf2->filenames[i].filename)
  113. yasm_xfree(dbgfmt_dwarf2->filenames[i].filename);
  114. }
  115. yasm_xfree(dbgfmt_dwarf2->filenames);
  116. yasm_xfree(dbgfmt);
  117. }
  118. /* Add a bytecode to a section, updating offset on insertion;
  119. * no optimization necessary.
  120. */
  121. yasm_bytecode *
  122. yasm_dwarf2__append_bc(yasm_section *sect, yasm_bytecode *bc)
  123. {
  124. yasm_bytecode *precbc = yasm_section_bcs_last(sect);
  125. bc->offset = yasm_bc_next_offset(precbc);
  126. yasm_section_bcs_append(sect, bc);
  127. return precbc;
  128. }
  129. static void
  130. dwarf2_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap,
  131. yasm_errwarns *errwarns)
  132. {
  133. yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt;
  134. size_t num_line_sections;
  135. /*@null@*/ yasm_section *debug_info, *debug_line, *main_code;
  136. /* If we don't have any .file directives, generate line information
  137. * based on the asm source.
  138. */
  139. debug_line = yasm_dwarf2__generate_line(object, linemap, errwarns,
  140. dbgfmt_dwarf2->filenames_size == 0,
  141. &main_code, &num_line_sections);
  142. /* If we don't have a .debug_info (or it's empty), generate the minimal
  143. * set of .debug_info, .debug_aranges, and .debug_abbrev so that the
  144. * .debug_line we're generating is actually useful.
  145. */
  146. debug_info = yasm_object_find_general(object, ".debug_info");
  147. if (num_line_sections > 0 &&
  148. (!debug_info || yasm_section_bcs_first(debug_info)
  149. == yasm_section_bcs_last(debug_info))) {
  150. debug_info = yasm_dwarf2__generate_info(object, debug_line, main_code);
  151. yasm_dwarf2__generate_aranges(object, debug_info);
  152. /*yasm_dwarf2__generate_pubnames(object, debug_info);*/
  153. }
  154. }
  155. yasm_symrec *
  156. yasm_dwarf2__bc_sym(yasm_symtab *symtab, yasm_bytecode *bc)
  157. {
  158. /*@dependent@*/ yasm_symrec *sym;
  159. if (bc->symrecs && bc->symrecs[0])
  160. sym = bc->symrecs[0];
  161. else
  162. sym = yasm_symtab_define_label(symtab, ".bcsym", bc, 0, 0);
  163. return sym;
  164. }
  165. dwarf2_head *
  166. yasm_dwarf2__add_head
  167. (yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2, yasm_section *sect,
  168. /*@null@*/ yasm_section *debug_ptr, int with_address, int with_segment)
  169. {
  170. dwarf2_head *head;
  171. yasm_bytecode *bc;
  172. head = yasm_xmalloc(sizeof(dwarf2_head));
  173. head->start_prevbc = yasm_section_bcs_last(sect);
  174. bc = yasm_bc_create_common(&dwarf2_head_bc_callback, head, 0);
  175. bc->len = dbgfmt_dwarf2->sizeof_offset + 2;
  176. if (dbgfmt_dwarf2->format == DWARF2_FORMAT_64BIT)
  177. bc->len += 4;
  178. if (debug_ptr) {
  179. head->debug_ptr = debug_ptr;
  180. bc->len += dbgfmt_dwarf2->sizeof_offset;
  181. } else
  182. head->debug_ptr = NULL;
  183. head->with_address = with_address;
  184. head->with_segment = with_segment;
  185. if (with_address)
  186. bc->len++;
  187. if (with_segment)
  188. bc->len++;
  189. head->end_prevbc = bc;
  190. yasm_dwarf2__append_bc(sect, bc);
  191. return head;
  192. }
  193. void
  194. yasm_dwarf2__set_head_end(dwarf2_head *head, yasm_bytecode *end_prevbc)
  195. {
  196. head->end_prevbc = end_prevbc;
  197. }
  198. static void
  199. dwarf2_head_bc_destroy(void *contents)
  200. {
  201. yasm_xfree(contents);
  202. }
  203. static void
  204. dwarf2_head_bc_print(const void *contents, FILE *f, int indent_level)
  205. {
  206. /* TODO */
  207. }
  208. static int
  209. dwarf2_head_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  210. void *add_span_data)
  211. {
  212. yasm_internal_error(N_("tried to calc_len a dwarf2 head bytecode"));
  213. /*@notreached@*/
  214. return 0;
  215. }
  216. static int
  217. dwarf2_head_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  218. unsigned char *bufstart, void *d,
  219. yasm_output_value_func output_value,
  220. yasm_output_reloc_func output_reloc)
  221. {
  222. yasm_object *object = yasm_section_get_object(bc->section);
  223. yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt;
  224. dwarf2_head *head = (dwarf2_head *)bc->contents;
  225. unsigned char *buf = *bufp;
  226. yasm_intnum *intn, *cval;
  227. if (dbgfmt_dwarf2->format == DWARF2_FORMAT_64BIT) {
  228. YASM_WRITE_8(buf, 0xff);
  229. YASM_WRITE_8(buf, 0xff);
  230. YASM_WRITE_8(buf, 0xff);
  231. YASM_WRITE_8(buf, 0xff);
  232. }
  233. /* Total length of aranges info (following this field) */
  234. cval = yasm_intnum_create_uint(dbgfmt_dwarf2->sizeof_offset);
  235. intn = yasm_calc_bc_dist(head->start_prevbc, head->end_prevbc);
  236. yasm_intnum_calc(intn, YASM_EXPR_SUB, cval);
  237. yasm_arch_intnum_tobytes(object->arch, intn, buf,
  238. dbgfmt_dwarf2->sizeof_offset,
  239. dbgfmt_dwarf2->sizeof_offset*8, 0, bc, 0);
  240. buf += dbgfmt_dwarf2->sizeof_offset;
  241. yasm_intnum_destroy(intn);
  242. /* DWARF version */
  243. yasm_intnum_set_uint(cval, 2);
  244. yasm_arch_intnum_tobytes(object->arch, cval, buf, 2, 16, 0, bc, 0);
  245. buf += 2;
  246. /* Pointer to another debug section */
  247. if (head->debug_ptr) {
  248. yasm_value value;
  249. yasm_value_init_sym(&value,
  250. yasm_dwarf2__bc_sym(object->symtab,
  251. yasm_section_bcs_first(head->debug_ptr)),
  252. dbgfmt_dwarf2->sizeof_offset*8);
  253. output_value(&value, buf, dbgfmt_dwarf2->sizeof_offset,
  254. (unsigned long)(buf-bufstart), bc, 0, d);
  255. buf += dbgfmt_dwarf2->sizeof_offset;
  256. }
  257. /* Size of the offset portion of the address */
  258. if (head->with_address)
  259. YASM_WRITE_8(buf, dbgfmt_dwarf2->sizeof_address);
  260. /* Size of a segment descriptor. 0 = flat address space */
  261. if (head->with_segment)
  262. YASM_WRITE_8(buf, 0);
  263. *bufp = buf;
  264. yasm_intnum_destroy(cval);
  265. return 0;
  266. }
  267. static void
  268. dwarf2_section_data_destroy(void *data)
  269. {
  270. dwarf2_section_data *dsd = data;
  271. dwarf2_loc *n1, *n2;
  272. /* Delete locations */
  273. n1 = STAILQ_FIRST(&dsd->locs);
  274. while (n1) {
  275. n2 = STAILQ_NEXT(n1, link);
  276. yasm_xfree(n1);
  277. n1 = n2;
  278. }
  279. yasm_xfree(data);
  280. }
  281. static void
  282. dwarf2_section_data_print(void *data, FILE *f, int indent_level)
  283. {
  284. /* TODO */
  285. }
  286. static const yasm_directive dwarf2_directives[] = {
  287. { ".loc", "gas", yasm_dwarf2__dir_loc, YASM_DIR_ARG_REQUIRED },
  288. { ".file", "gas", yasm_dwarf2__dir_file, YASM_DIR_ARG_REQUIRED },
  289. { "loc", "nasm", yasm_dwarf2__dir_loc, YASM_DIR_ARG_REQUIRED },
  290. { "file", "nasm", yasm_dwarf2__dir_file, YASM_DIR_ARG_REQUIRED },
  291. { NULL, NULL, NULL, 0 }
  292. };
  293. /* Define dbgfmt structure -- see dbgfmt.h for details */
  294. yasm_dbgfmt_module yasm_dwarf2_LTX_dbgfmt = {
  295. "DWARF2 debugging format",
  296. "dwarf2",
  297. dwarf2_directives,
  298. dwarf2_dbgfmt_create,
  299. dwarf2_dbgfmt_destroy,
  300. dwarf2_dbgfmt_generate
  301. };