genelf_debug.c 14 KB

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