genelf_debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * genelf_debug.c
  3. * Copyright (C) 2015, Google, Inc
  4. *
  5. * Contributed by:
  6. * Stephane Eranian <eranian@google.com>
  7. *
  8. * Released under the GPL v2.
  9. *
  10. * based on GPLv2 source code from Oprofile
  11. * @remark Copyright 2007 OProfile authors
  12. * @author Philippe Elie
  13. */
  14. #include <sys/types.h>
  15. #include <stdio.h>
  16. #include <getopt.h>
  17. #include <stddef.h>
  18. #include <libelf.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <inttypes.h>
  22. #include <limits.h>
  23. #include <fcntl.h>
  24. #include <err.h>
  25. #include <dwarf.h>
  26. #include "perf.h"
  27. #include "genelf.h"
  28. #include "../util/jitdump.h"
  29. #define BUFFER_EXT_DFL_SIZE (4 * 1024)
  30. typedef uint32_t uword;
  31. typedef uint16_t uhalf;
  32. typedef int32_t sword;
  33. typedef int16_t shalf;
  34. typedef uint8_t ubyte;
  35. typedef int8_t sbyte;
  36. struct buffer_ext {
  37. size_t cur_pos;
  38. size_t max_sz;
  39. void *data;
  40. };
  41. static void
  42. buffer_ext_dump(struct buffer_ext *be, const char *msg)
  43. {
  44. size_t i;
  45. warnx("DUMP for %s", msg);
  46. for (i = 0 ; i < be->cur_pos; i++)
  47. warnx("%4zu 0x%02x", i, (((char *)be->data)[i]) & 0xff);
  48. }
  49. static inline int
  50. buffer_ext_add(struct buffer_ext *be, void *addr, size_t sz)
  51. {
  52. void *tmp;
  53. size_t be_sz = be->max_sz;
  54. retry:
  55. if ((be->cur_pos + sz) < be_sz) {
  56. memcpy(be->data + be->cur_pos, addr, sz);
  57. be->cur_pos += sz;
  58. return 0;
  59. }
  60. if (!be_sz)
  61. be_sz = BUFFER_EXT_DFL_SIZE;
  62. else
  63. be_sz <<= 1;
  64. tmp = realloc(be->data, be_sz);
  65. if (!tmp)
  66. return -1;
  67. be->data = tmp;
  68. be->max_sz = be_sz;
  69. goto retry;
  70. }
  71. static void
  72. buffer_ext_init(struct buffer_ext *be)
  73. {
  74. be->data = NULL;
  75. be->cur_pos = 0;
  76. be->max_sz = 0;
  77. }
  78. static inline size_t
  79. buffer_ext_size(struct buffer_ext *be)
  80. {
  81. return be->cur_pos;
  82. }
  83. static inline void *
  84. buffer_ext_addr(struct buffer_ext *be)
  85. {
  86. return be->data;
  87. }
  88. struct debug_line_header {
  89. // Not counting this field
  90. uword total_length;
  91. // version number (2 currently)
  92. uhalf version;
  93. // relative offset from next field to
  94. // program statement
  95. uword prolog_length;
  96. ubyte minimum_instruction_length;
  97. ubyte default_is_stmt;
  98. // line_base - see DWARF 2 specs
  99. sbyte line_base;
  100. // line_range - see DWARF 2 specs
  101. ubyte line_range;
  102. // number of opcode + 1
  103. ubyte opcode_base;
  104. /* follow the array of opcode args nr: ubytes [nr_opcode_base] */
  105. /* follow the search directories index, zero terminated string
  106. * terminated by an empty string.
  107. */
  108. /* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
  109. * the directory index entry, 0 means current directory, then mtime
  110. * and filesize, last entry is followed by en empty string.
  111. */
  112. /* follow the first program statement */
  113. } __attribute__((packed));
  114. /* DWARF 2 spec talk only about one possible compilation unit header while
  115. * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
  116. * related to the used arch, an ELF 32 can hold more than 4 Go of debug
  117. * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
  118. * become a problem if we generate more than 4GB of debug information.
  119. */
  120. struct compilation_unit_header {
  121. uword total_length;
  122. uhalf version;
  123. uword debug_abbrev_offset;
  124. ubyte pointer_size;
  125. } __attribute__((packed));
  126. #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
  127. /* field filled at run time are marked with -1 */
  128. static struct debug_line_header const default_debug_line_header = {
  129. .total_length = -1,
  130. .version = 2,
  131. .prolog_length = -1,
  132. .minimum_instruction_length = 1, /* could be better when min instruction size != 1 */
  133. .default_is_stmt = 1, /* we don't take care about basic block */
  134. .line_base = -5, /* sensible value for line base ... */
  135. .line_range = -14, /* ... and line range are guessed statically */
  136. .opcode_base = DW_LNS_num_opcode
  137. };
  138. static ubyte standard_opcode_length[] =
  139. {
  140. 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
  141. };
  142. #if 0
  143. {
  144. [DW_LNS_advance_pc] = 1,
  145. [DW_LNS_advance_line] = 1,
  146. [DW_LNS_set_file] = 1,
  147. [DW_LNS_set_column] = 1,
  148. [DW_LNS_fixed_advance_pc] = 1,
  149. [DW_LNS_set_isa] = 1,
  150. };
  151. #endif
  152. /* field filled at run time are marked with -1 */
  153. static struct compilation_unit_header default_comp_unit_header = {
  154. .total_length = -1,
  155. .version = 2,
  156. .debug_abbrev_offset = 0, /* we reuse the same abbrev entries for all comp unit */
  157. .pointer_size = sizeof(void *)
  158. };
  159. static void emit_uword(struct buffer_ext *be, uword data)
  160. {
  161. buffer_ext_add(be, &data, sizeof(uword));
  162. }
  163. static void emit_string(struct buffer_ext *be, const char *s)
  164. {
  165. buffer_ext_add(be, (void *)s, strlen(s) + 1);
  166. }
  167. static void emit_unsigned_LEB128(struct buffer_ext *be,
  168. unsigned long data)
  169. {
  170. do {
  171. ubyte cur = data & 0x7F;
  172. data >>= 7;
  173. if (data)
  174. cur |= 0x80;
  175. buffer_ext_add(be, &cur, 1);
  176. } while (data);
  177. }
  178. static void emit_signed_LEB128(struct buffer_ext *be, long data)
  179. {
  180. int more = 1;
  181. int negative = data < 0;
  182. int size = sizeof(long) * CHAR_BIT;
  183. while (more) {
  184. ubyte cur = data & 0x7F;
  185. data >>= 7;
  186. if (negative)
  187. data |= - (1 << (size - 7));
  188. if ((data == 0 && !(cur & 0x40)) ||
  189. (data == -1l && (cur & 0x40)))
  190. more = 0;
  191. else
  192. cur |= 0x80;
  193. buffer_ext_add(be, &cur, 1);
  194. }
  195. }
  196. static void emit_extended_opcode(struct buffer_ext *be, ubyte opcode,
  197. void *data, size_t data_len)
  198. {
  199. buffer_ext_add(be, (char *)"", 1);
  200. emit_unsigned_LEB128(be, data_len + 1);
  201. buffer_ext_add(be, &opcode, 1);
  202. buffer_ext_add(be, data, data_len);
  203. }
  204. static void emit_opcode(struct buffer_ext *be, ubyte opcode)
  205. {
  206. buffer_ext_add(be, &opcode, 1);
  207. }
  208. static void emit_opcode_signed(struct buffer_ext *be,
  209. ubyte opcode, long data)
  210. {
  211. buffer_ext_add(be, &opcode, 1);
  212. emit_signed_LEB128(be, data);
  213. }
  214. static void emit_opcode_unsigned(struct buffer_ext *be, ubyte opcode,
  215. unsigned long data)
  216. {
  217. buffer_ext_add(be, &opcode, 1);
  218. emit_unsigned_LEB128(be, data);
  219. }
  220. static void emit_advance_pc(struct buffer_ext *be, unsigned long delta_pc)
  221. {
  222. emit_opcode_unsigned(be, DW_LNS_advance_pc, delta_pc);
  223. }
  224. static void emit_advance_lineno(struct buffer_ext *be, long delta_lineno)
  225. {
  226. emit_opcode_signed(be, DW_LNS_advance_line, delta_lineno);
  227. }
  228. static void emit_lne_end_of_sequence(struct buffer_ext *be)
  229. {
  230. emit_extended_opcode(be, DW_LNE_end_sequence, NULL, 0);
  231. }
  232. static void emit_set_file(struct buffer_ext *be, unsigned long idx)
  233. {
  234. emit_opcode_unsigned(be, DW_LNS_set_file, idx);
  235. }
  236. static void emit_lne_define_filename(struct buffer_ext *be,
  237. const char *filename)
  238. {
  239. buffer_ext_add(be, (void *)"", 1);
  240. /* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
  241. emit_unsigned_LEB128(be, strlen(filename) + 5);
  242. emit_opcode(be, DW_LNE_define_file);
  243. emit_string(be, filename);
  244. /* directory index 0=do not know */
  245. emit_unsigned_LEB128(be, 0);
  246. /* last modification date on file 0=do not know */
  247. emit_unsigned_LEB128(be, 0);
  248. /* filesize 0=do not know */
  249. emit_unsigned_LEB128(be, 0);
  250. }
  251. static void emit_lne_set_address(struct buffer_ext *be,
  252. void *address)
  253. {
  254. emit_extended_opcode(be, DW_LNE_set_address, &address, sizeof(unsigned long));
  255. }
  256. static ubyte get_special_opcode(struct debug_entry *ent,
  257. unsigned int last_line,
  258. unsigned long last_vma)
  259. {
  260. unsigned int temp;
  261. unsigned long delta_addr;
  262. /*
  263. * delta from line_base
  264. */
  265. temp = (ent->lineno - last_line) - default_debug_line_header.line_base;
  266. if (temp >= default_debug_line_header.line_range)
  267. return 0;
  268. /*
  269. * delta of addresses
  270. */
  271. delta_addr = (ent->addr - last_vma) / default_debug_line_header.minimum_instruction_length;
  272. /* This is not sufficient to ensure opcode will be in [0-256] but
  273. * sufficient to ensure when summing with the delta lineno we will
  274. * not overflow the unsigned long opcode */
  275. if (delta_addr <= 256 / default_debug_line_header.line_range) {
  276. unsigned long opcode = temp +
  277. (delta_addr * default_debug_line_header.line_range) +
  278. default_debug_line_header.opcode_base;
  279. return opcode <= 255 ? opcode : 0;
  280. }
  281. return 0;
  282. }
  283. static void emit_lineno_info(struct buffer_ext *be,
  284. struct debug_entry *ent, size_t nr_entry,
  285. unsigned long code_addr)
  286. {
  287. size_t i;
  288. /*
  289. * Machine state at start of a statement program
  290. * address = 0
  291. * file = 1
  292. * line = 1
  293. * column = 0
  294. * is_stmt = default_is_stmt as given in the debug_line_header
  295. * basic block = 0
  296. * end sequence = 0
  297. */
  298. /* start state of the state machine we take care of */
  299. unsigned long last_vma = code_addr;
  300. char const *cur_filename = NULL;
  301. unsigned long cur_file_idx = 0;
  302. int last_line = 1;
  303. emit_lne_set_address(be, (void *)code_addr);
  304. for (i = 0; i < nr_entry; i++, ent = debug_entry_next(ent)) {
  305. int need_copy = 0;
  306. ubyte special_opcode;
  307. /*
  308. * check if filename changed, if so add it
  309. */
  310. if (!cur_filename || strcmp(cur_filename, ent->name)) {
  311. emit_lne_define_filename(be, ent->name);
  312. cur_filename = ent->name;
  313. emit_set_file(be, ++cur_file_idx);
  314. need_copy = 1;
  315. }
  316. special_opcode = get_special_opcode(ent, last_line, last_vma);
  317. if (special_opcode != 0) {
  318. last_line = ent->lineno;
  319. last_vma = ent->addr;
  320. emit_opcode(be, special_opcode);
  321. } else {
  322. /*
  323. * lines differ, emit line delta
  324. */
  325. if (last_line != ent->lineno) {
  326. emit_advance_lineno(be, ent->lineno - last_line);
  327. last_line = ent->lineno;
  328. need_copy = 1;
  329. }
  330. /*
  331. * addresses differ, emit address delta
  332. */
  333. if (last_vma != ent->addr) {
  334. emit_advance_pc(be, ent->addr - last_vma);
  335. last_vma = ent->addr;
  336. need_copy = 1;
  337. }
  338. /*
  339. * add new row to matrix
  340. */
  341. if (need_copy)
  342. emit_opcode(be, DW_LNS_copy);
  343. }
  344. }
  345. }
  346. static void add_debug_line(struct buffer_ext *be,
  347. struct debug_entry *ent, size_t nr_entry,
  348. unsigned long code_addr)
  349. {
  350. struct debug_line_header * dbg_header;
  351. size_t old_size;
  352. old_size = buffer_ext_size(be);
  353. buffer_ext_add(be, (void *)&default_debug_line_header,
  354. sizeof(default_debug_line_header));
  355. buffer_ext_add(be, &standard_opcode_length, sizeof(standard_opcode_length));
  356. // empty directory entry
  357. buffer_ext_add(be, (void *)"", 1);
  358. // empty filename directory
  359. buffer_ext_add(be, (void *)"", 1);
  360. dbg_header = buffer_ext_addr(be) + old_size;
  361. dbg_header->prolog_length = (buffer_ext_size(be) - old_size) -
  362. offsetof(struct debug_line_header, minimum_instruction_length);
  363. emit_lineno_info(be, ent, nr_entry, code_addr);
  364. emit_lne_end_of_sequence(be);
  365. dbg_header = buffer_ext_addr(be) + old_size;
  366. dbg_header->total_length = (buffer_ext_size(be) - old_size) -
  367. offsetof(struct debug_line_header, version);
  368. }
  369. static void
  370. add_debug_abbrev(struct buffer_ext *be)
  371. {
  372. emit_unsigned_LEB128(be, 1);
  373. emit_unsigned_LEB128(be, DW_TAG_compile_unit);
  374. emit_unsigned_LEB128(be, DW_CHILDREN_yes);
  375. emit_unsigned_LEB128(be, DW_AT_stmt_list);
  376. emit_unsigned_LEB128(be, DW_FORM_data4);
  377. emit_unsigned_LEB128(be, 0);
  378. emit_unsigned_LEB128(be, 0);
  379. emit_unsigned_LEB128(be, 0);
  380. }
  381. static void
  382. add_compilation_unit(struct buffer_ext *be,
  383. size_t offset_debug_line)
  384. {
  385. struct compilation_unit_header *comp_unit_header;
  386. size_t old_size = buffer_ext_size(be);
  387. buffer_ext_add(be, &default_comp_unit_header,
  388. sizeof(default_comp_unit_header));
  389. emit_unsigned_LEB128(be, 1);
  390. emit_uword(be, offset_debug_line);
  391. comp_unit_header = buffer_ext_addr(be) + old_size;
  392. comp_unit_header->total_length = (buffer_ext_size(be) - old_size) -
  393. offsetof(struct compilation_unit_header, version);
  394. }
  395. static int
  396. jit_process_debug_info(uint64_t code_addr,
  397. void *debug, int nr_debug_entries,
  398. struct buffer_ext *dl,
  399. struct buffer_ext *da,
  400. struct buffer_ext *di)
  401. {
  402. struct debug_entry *ent = debug;
  403. int i;
  404. for (i = 0; i < nr_debug_entries; i++) {
  405. ent->addr = ent->addr - code_addr;
  406. ent = debug_entry_next(ent);
  407. }
  408. add_compilation_unit(di, buffer_ext_size(dl));
  409. add_debug_line(dl, debug, nr_debug_entries, 0);
  410. add_debug_abbrev(da);
  411. if (0) buffer_ext_dump(da, "abbrev");
  412. return 0;
  413. }
  414. int
  415. jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries)
  416. {
  417. Elf_Data *d;
  418. Elf_Scn *scn;
  419. Elf_Shdr *shdr;
  420. struct buffer_ext dl, di, da;
  421. int ret;
  422. buffer_ext_init(&dl);
  423. buffer_ext_init(&di);
  424. buffer_ext_init(&da);
  425. ret = jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di);
  426. if (ret)
  427. return -1;
  428. /*
  429. * setup .debug_line section
  430. */
  431. scn = elf_newscn(e);
  432. if (!scn) {
  433. warnx("cannot create section");
  434. return -1;
  435. }
  436. d = elf_newdata(scn);
  437. if (!d) {
  438. warnx("cannot get new data");
  439. return -1;
  440. }
  441. d->d_align = 1;
  442. d->d_off = 0LL;
  443. d->d_buf = buffer_ext_addr(&dl);
  444. d->d_type = ELF_T_BYTE;
  445. d->d_size = buffer_ext_size(&dl);
  446. d->d_version = EV_CURRENT;
  447. shdr = elf_getshdr(scn);
  448. if (!shdr) {
  449. warnx("cannot get section header");
  450. return -1;
  451. }
  452. shdr->sh_name = 52; /* .debug_line */
  453. shdr->sh_type = SHT_PROGBITS;
  454. shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
  455. shdr->sh_flags = 0;
  456. shdr->sh_entsize = 0;
  457. /*
  458. * setup .debug_info section
  459. */
  460. scn = elf_newscn(e);
  461. if (!scn) {
  462. warnx("cannot create section");
  463. return -1;
  464. }
  465. d = elf_newdata(scn);
  466. if (!d) {
  467. warnx("cannot get new data");
  468. return -1;
  469. }
  470. d->d_align = 1;
  471. d->d_off = 0LL;
  472. d->d_buf = buffer_ext_addr(&di);
  473. d->d_type = ELF_T_BYTE;
  474. d->d_size = buffer_ext_size(&di);
  475. d->d_version = EV_CURRENT;
  476. shdr = elf_getshdr(scn);
  477. if (!shdr) {
  478. warnx("cannot get section header");
  479. return -1;
  480. }
  481. shdr->sh_name = 64; /* .debug_info */
  482. shdr->sh_type = SHT_PROGBITS;
  483. shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
  484. shdr->sh_flags = 0;
  485. shdr->sh_entsize = 0;
  486. /*
  487. * setup .debug_abbrev section
  488. */
  489. scn = elf_newscn(e);
  490. if (!scn) {
  491. warnx("cannot create section");
  492. return -1;
  493. }
  494. d = elf_newdata(scn);
  495. if (!d) {
  496. warnx("cannot get new data");
  497. return -1;
  498. }
  499. d->d_align = 1;
  500. d->d_off = 0LL;
  501. d->d_buf = buffer_ext_addr(&da);
  502. d->d_type = ELF_T_BYTE;
  503. d->d_size = buffer_ext_size(&da);
  504. d->d_version = EV_CURRENT;
  505. shdr = elf_getshdr(scn);
  506. if (!shdr) {
  507. warnx("cannot get section header");
  508. return -1;
  509. }
  510. shdr->sh_name = 76; /* .debug_info */
  511. shdr->sh_type = SHT_PROGBITS;
  512. shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
  513. shdr->sh_flags = 0;
  514. shdr->sh_entsize = 0;
  515. /*
  516. * now we update the ELF image with all the sections
  517. */
  518. if (elf_update(e, ELF_C_WRITE) < 0) {
  519. warnx("elf_update debug failed");
  520. return -1;
  521. }
  522. return 0;
  523. }