0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/compiler.h>
0014 #include <sys/types.h>
0015 #include <stdio.h>
0016 #include <getopt.h>
0017 #include <stddef.h>
0018 #include <libelf.h>
0019 #include <string.h>
0020 #include <stdlib.h>
0021 #include <inttypes.h>
0022 #include <limits.h>
0023 #include <fcntl.h>
0024 #include <err.h>
0025 #include <dwarf.h>
0026
0027 #include "genelf.h"
0028 #include "../util/jitdump.h"
0029
0030 #define BUFFER_EXT_DFL_SIZE (4 * 1024)
0031
0032 typedef uint32_t uword;
0033 typedef uint16_t uhalf;
0034 typedef int32_t sword;
0035 typedef int16_t shalf;
0036 typedef uint8_t ubyte;
0037 typedef int8_t sbyte;
0038
0039 struct buffer_ext {
0040 size_t cur_pos;
0041 size_t max_sz;
0042 void *data;
0043 };
0044
0045 static void
0046 buffer_ext_dump(struct buffer_ext *be, const char *msg)
0047 {
0048 size_t i;
0049 warnx("DUMP for %s", msg);
0050 for (i = 0 ; i < be->cur_pos; i++)
0051 warnx("%4zu 0x%02x", i, (((char *)be->data)[i]) & 0xff);
0052 }
0053
0054 static inline int
0055 buffer_ext_add(struct buffer_ext *be, void *addr, size_t sz)
0056 {
0057 void *tmp;
0058 size_t be_sz = be->max_sz;
0059
0060 retry:
0061 if ((be->cur_pos + sz) < be_sz) {
0062 memcpy(be->data + be->cur_pos, addr, sz);
0063 be->cur_pos += sz;
0064 return 0;
0065 }
0066
0067 if (!be_sz)
0068 be_sz = BUFFER_EXT_DFL_SIZE;
0069 else
0070 be_sz <<= 1;
0071
0072 tmp = realloc(be->data, be_sz);
0073 if (!tmp)
0074 return -1;
0075
0076 be->data = tmp;
0077 be->max_sz = be_sz;
0078
0079 goto retry;
0080 }
0081
0082 static void
0083 buffer_ext_init(struct buffer_ext *be)
0084 {
0085 be->data = NULL;
0086 be->cur_pos = 0;
0087 be->max_sz = 0;
0088 }
0089
0090 static inline size_t
0091 buffer_ext_size(struct buffer_ext *be)
0092 {
0093 return be->cur_pos;
0094 }
0095
0096 static inline void *
0097 buffer_ext_addr(struct buffer_ext *be)
0098 {
0099 return be->data;
0100 }
0101
0102 struct debug_line_header {
0103
0104 uword total_length;
0105
0106 uhalf version;
0107
0108
0109 uword prolog_length;
0110 ubyte minimum_instruction_length;
0111 ubyte default_is_stmt;
0112
0113 sbyte line_base;
0114
0115 ubyte line_range;
0116
0117 ubyte opcode_base;
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127 } __packed;
0128
0129
0130
0131
0132
0133
0134
0135 struct compilation_unit_header {
0136 uword total_length;
0137 uhalf version;
0138 uword debug_abbrev_offset;
0139 ubyte pointer_size;
0140 } __packed;
0141
0142 #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
0143
0144
0145 static struct debug_line_header const default_debug_line_header = {
0146 .total_length = -1,
0147 .version = 2,
0148 .prolog_length = -1,
0149 .minimum_instruction_length = 1,
0150 .default_is_stmt = 1,
0151 .line_base = -5,
0152 .line_range = -14,
0153 .opcode_base = DW_LNS_num_opcode
0154 };
0155
0156 static ubyte standard_opcode_length[] =
0157 {
0158 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
0159 };
0160 #if 0
0161 {
0162 [DW_LNS_advance_pc] = 1,
0163 [DW_LNS_advance_line] = 1,
0164 [DW_LNS_set_file] = 1,
0165 [DW_LNS_set_column] = 1,
0166 [DW_LNS_fixed_advance_pc] = 1,
0167 [DW_LNS_set_isa] = 1,
0168 };
0169 #endif
0170
0171
0172 static struct compilation_unit_header default_comp_unit_header = {
0173 .total_length = -1,
0174 .version = 2,
0175 .debug_abbrev_offset = 0,
0176 .pointer_size = sizeof(void *)
0177 };
0178
0179 static void emit_uword(struct buffer_ext *be, uword data)
0180 {
0181 buffer_ext_add(be, &data, sizeof(uword));
0182 }
0183
0184 static void emit_string(struct buffer_ext *be, const char *s)
0185 {
0186 buffer_ext_add(be, (void *)s, strlen(s) + 1);
0187 }
0188
0189 static void emit_unsigned_LEB128(struct buffer_ext *be,
0190 unsigned long data)
0191 {
0192 do {
0193 ubyte cur = data & 0x7F;
0194 data >>= 7;
0195 if (data)
0196 cur |= 0x80;
0197 buffer_ext_add(be, &cur, 1);
0198 } while (data);
0199 }
0200
0201 static void emit_signed_LEB128(struct buffer_ext *be, long data)
0202 {
0203 int more = 1;
0204 int negative = data < 0;
0205 int size = sizeof(long) * CHAR_BIT;
0206 while (more) {
0207 ubyte cur = data & 0x7F;
0208 data >>= 7;
0209 if (negative)
0210 data |= - (1 << (size - 7));
0211 if ((data == 0 && !(cur & 0x40)) ||
0212 (data == -1l && (cur & 0x40)))
0213 more = 0;
0214 else
0215 cur |= 0x80;
0216 buffer_ext_add(be, &cur, 1);
0217 }
0218 }
0219
0220 static void emit_extended_opcode(struct buffer_ext *be, ubyte opcode,
0221 void *data, size_t data_len)
0222 {
0223 buffer_ext_add(be, (char *)"", 1);
0224
0225 emit_unsigned_LEB128(be, data_len + 1);
0226
0227 buffer_ext_add(be, &opcode, 1);
0228 buffer_ext_add(be, data, data_len);
0229 }
0230
0231 static void emit_opcode(struct buffer_ext *be, ubyte opcode)
0232 {
0233 buffer_ext_add(be, &opcode, 1);
0234 }
0235
0236 static void emit_opcode_signed(struct buffer_ext *be,
0237 ubyte opcode, long data)
0238 {
0239 buffer_ext_add(be, &opcode, 1);
0240 emit_signed_LEB128(be, data);
0241 }
0242
0243 static void emit_opcode_unsigned(struct buffer_ext *be, ubyte opcode,
0244 unsigned long data)
0245 {
0246 buffer_ext_add(be, &opcode, 1);
0247 emit_unsigned_LEB128(be, data);
0248 }
0249
0250 static void emit_advance_pc(struct buffer_ext *be, unsigned long delta_pc)
0251 {
0252 emit_opcode_unsigned(be, DW_LNS_advance_pc, delta_pc);
0253 }
0254
0255 static void emit_advance_lineno(struct buffer_ext *be, long delta_lineno)
0256 {
0257 emit_opcode_signed(be, DW_LNS_advance_line, delta_lineno);
0258 }
0259
0260 static void emit_lne_end_of_sequence(struct buffer_ext *be)
0261 {
0262 emit_extended_opcode(be, DW_LNE_end_sequence, NULL, 0);
0263 }
0264
0265 static void emit_set_file(struct buffer_ext *be, unsigned long idx)
0266 {
0267 emit_opcode_unsigned(be, DW_LNS_set_file, idx);
0268 }
0269
0270 static void emit_lne_define_filename(struct buffer_ext *be,
0271 const char *filename)
0272 {
0273 buffer_ext_add(be, (void *)"", 1);
0274
0275
0276 emit_unsigned_LEB128(be, strlen(filename) + 5);
0277 emit_opcode(be, DW_LNE_define_file);
0278 emit_string(be, filename);
0279
0280 emit_unsigned_LEB128(be, 0);
0281
0282 emit_unsigned_LEB128(be, 0);
0283
0284 emit_unsigned_LEB128(be, 0);
0285 }
0286
0287 static void emit_lne_set_address(struct buffer_ext *be,
0288 void *address)
0289 {
0290 emit_extended_opcode(be, DW_LNE_set_address, &address, sizeof(unsigned long));
0291 }
0292
0293 static ubyte get_special_opcode(struct debug_entry *ent,
0294 unsigned int last_line,
0295 unsigned long last_vma)
0296 {
0297 unsigned int temp;
0298 unsigned long delta_addr;
0299
0300
0301
0302
0303 temp = (ent->lineno - last_line) - default_debug_line_header.line_base;
0304
0305 if (temp >= default_debug_line_header.line_range)
0306 return 0;
0307
0308
0309
0310
0311 delta_addr = (ent->addr - last_vma) / default_debug_line_header.minimum_instruction_length;
0312
0313
0314
0315
0316
0317 if (delta_addr <= 256 / default_debug_line_header.line_range) {
0318 unsigned long opcode = temp +
0319 (delta_addr * default_debug_line_header.line_range) +
0320 default_debug_line_header.opcode_base;
0321
0322 return opcode <= 255 ? opcode : 0;
0323 }
0324 return 0;
0325 }
0326
0327 static void emit_lineno_info(struct buffer_ext *be,
0328 struct debug_entry *ent, size_t nr_entry,
0329 unsigned long code_addr)
0330 {
0331 size_t i;
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345 unsigned long last_vma = 0;
0346 char const *cur_filename = NULL;
0347 unsigned long cur_file_idx = 0;
0348 int last_line = 1;
0349
0350 emit_lne_set_address(be, (void *)code_addr);
0351
0352 for (i = 0; i < nr_entry; i++, ent = debug_entry_next(ent)) {
0353 int need_copy = 0;
0354 ubyte special_opcode;
0355
0356
0357
0358
0359 if (!cur_filename || strcmp(cur_filename, ent->name)) {
0360 emit_lne_define_filename(be, ent->name);
0361 cur_filename = ent->name;
0362 emit_set_file(be, ++cur_file_idx);
0363 need_copy = 1;
0364 }
0365
0366 special_opcode = get_special_opcode(ent, last_line, last_vma);
0367 if (special_opcode != 0) {
0368 last_line = ent->lineno;
0369 last_vma = ent->addr;
0370 emit_opcode(be, special_opcode);
0371 } else {
0372
0373
0374
0375 if (last_line != ent->lineno) {
0376 emit_advance_lineno(be, ent->lineno - last_line);
0377 last_line = ent->lineno;
0378 need_copy = 1;
0379 }
0380
0381
0382
0383 if (last_vma != ent->addr) {
0384 emit_advance_pc(be, ent->addr - last_vma);
0385 last_vma = ent->addr;
0386 need_copy = 1;
0387 }
0388
0389
0390
0391 if (need_copy)
0392 emit_opcode(be, DW_LNS_copy);
0393 }
0394 }
0395 }
0396
0397 static void add_debug_line(struct buffer_ext *be,
0398 struct debug_entry *ent, size_t nr_entry,
0399 unsigned long code_addr)
0400 {
0401 struct debug_line_header * dbg_header;
0402 size_t old_size;
0403
0404 old_size = buffer_ext_size(be);
0405
0406 buffer_ext_add(be, (void *)&default_debug_line_header,
0407 sizeof(default_debug_line_header));
0408
0409 buffer_ext_add(be, &standard_opcode_length, sizeof(standard_opcode_length));
0410
0411
0412 buffer_ext_add(be, (void *)"", 1);
0413
0414
0415 buffer_ext_add(be, (void *)"", 1);
0416
0417 dbg_header = buffer_ext_addr(be) + old_size;
0418 dbg_header->prolog_length = (buffer_ext_size(be) - old_size) -
0419 offsetof(struct debug_line_header, minimum_instruction_length);
0420
0421 emit_lineno_info(be, ent, nr_entry, code_addr);
0422
0423 emit_lne_end_of_sequence(be);
0424
0425 dbg_header = buffer_ext_addr(be) + old_size;
0426 dbg_header->total_length = (buffer_ext_size(be) - old_size) -
0427 offsetof(struct debug_line_header, version);
0428 }
0429
0430 static void
0431 add_debug_abbrev(struct buffer_ext *be)
0432 {
0433 emit_unsigned_LEB128(be, 1);
0434 emit_unsigned_LEB128(be, DW_TAG_compile_unit);
0435 emit_unsigned_LEB128(be, DW_CHILDREN_yes);
0436 emit_unsigned_LEB128(be, DW_AT_stmt_list);
0437 emit_unsigned_LEB128(be, DW_FORM_data4);
0438 emit_unsigned_LEB128(be, 0);
0439 emit_unsigned_LEB128(be, 0);
0440 emit_unsigned_LEB128(be, 0);
0441 }
0442
0443 static void
0444 add_compilation_unit(struct buffer_ext *be,
0445 size_t offset_debug_line)
0446 {
0447 struct compilation_unit_header *comp_unit_header;
0448 size_t old_size = buffer_ext_size(be);
0449
0450 buffer_ext_add(be, &default_comp_unit_header,
0451 sizeof(default_comp_unit_header));
0452
0453 emit_unsigned_LEB128(be, 1);
0454 emit_uword(be, offset_debug_line);
0455
0456 comp_unit_header = buffer_ext_addr(be) + old_size;
0457 comp_unit_header->total_length = (buffer_ext_size(be) - old_size) -
0458 offsetof(struct compilation_unit_header, version);
0459 }
0460
0461 static int
0462 jit_process_debug_info(uint64_t code_addr,
0463 void *debug, int nr_debug_entries,
0464 struct buffer_ext *dl,
0465 struct buffer_ext *da,
0466 struct buffer_ext *di)
0467 {
0468 struct debug_entry *ent = debug;
0469 int i;
0470
0471 for (i = 0; i < nr_debug_entries; i++) {
0472 ent->addr = ent->addr - code_addr;
0473 ent = debug_entry_next(ent);
0474 }
0475 add_compilation_unit(di, buffer_ext_size(dl));
0476 add_debug_line(dl, debug, nr_debug_entries, GEN_ELF_TEXT_OFFSET);
0477 add_debug_abbrev(da);
0478 if (0) buffer_ext_dump(da, "abbrev");
0479
0480 return 0;
0481 }
0482
0483 int
0484 jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries)
0485 {
0486 Elf_Data *d;
0487 Elf_Scn *scn;
0488 Elf_Shdr *shdr;
0489 struct buffer_ext dl, di, da;
0490 int ret;
0491
0492 buffer_ext_init(&dl);
0493 buffer_ext_init(&di);
0494 buffer_ext_init(&da);
0495
0496 ret = jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di);
0497 if (ret)
0498 return -1;
0499
0500
0501
0502 scn = elf_newscn(e);
0503 if (!scn) {
0504 warnx("cannot create section");
0505 return -1;
0506 }
0507
0508 d = elf_newdata(scn);
0509 if (!d) {
0510 warnx("cannot get new data");
0511 return -1;
0512 }
0513
0514 d->d_align = 1;
0515 d->d_off = 0LL;
0516 d->d_buf = buffer_ext_addr(&dl);
0517 d->d_type = ELF_T_BYTE;
0518 d->d_size = buffer_ext_size(&dl);
0519 d->d_version = EV_CURRENT;
0520
0521 shdr = elf_getshdr(scn);
0522 if (!shdr) {
0523 warnx("cannot get section header");
0524 return -1;
0525 }
0526
0527 shdr->sh_name = 52;
0528 shdr->sh_type = SHT_PROGBITS;
0529 shdr->sh_addr = 0;
0530 shdr->sh_flags = 0;
0531 shdr->sh_entsize = 0;
0532
0533
0534
0535
0536 scn = elf_newscn(e);
0537 if (!scn) {
0538 warnx("cannot create section");
0539 return -1;
0540 }
0541
0542 d = elf_newdata(scn);
0543 if (!d) {
0544 warnx("cannot get new data");
0545 return -1;
0546 }
0547
0548 d->d_align = 1;
0549 d->d_off = 0LL;
0550 d->d_buf = buffer_ext_addr(&di);
0551 d->d_type = ELF_T_BYTE;
0552 d->d_size = buffer_ext_size(&di);
0553 d->d_version = EV_CURRENT;
0554
0555 shdr = elf_getshdr(scn);
0556 if (!shdr) {
0557 warnx("cannot get section header");
0558 return -1;
0559 }
0560
0561 shdr->sh_name = 64;
0562 shdr->sh_type = SHT_PROGBITS;
0563 shdr->sh_addr = 0;
0564 shdr->sh_flags = 0;
0565 shdr->sh_entsize = 0;
0566
0567
0568
0569
0570 scn = elf_newscn(e);
0571 if (!scn) {
0572 warnx("cannot create section");
0573 return -1;
0574 }
0575
0576 d = elf_newdata(scn);
0577 if (!d) {
0578 warnx("cannot get new data");
0579 return -1;
0580 }
0581
0582 d->d_align = 1;
0583 d->d_off = 0LL;
0584 d->d_buf = buffer_ext_addr(&da);
0585 d->d_type = ELF_T_BYTE;
0586 d->d_size = buffer_ext_size(&da);
0587 d->d_version = EV_CURRENT;
0588
0589 shdr = elf_getshdr(scn);
0590 if (!shdr) {
0591 warnx("cannot get section header");
0592 return -1;
0593 }
0594
0595 shdr->sh_name = 76;
0596 shdr->sh_type = SHT_PROGBITS;
0597 shdr->sh_addr = 0;
0598 shdr->sh_flags = 0;
0599 shdr->sh_entsize = 0;
0600
0601
0602
0603
0604 if (elf_update(e, ELF_C_WRITE) < 0) {
0605 warnx("elf_update debug failed");
0606 return -1;
0607 }
0608 return 0;
0609 }