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