Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * genelf_debug.c
0004  * Copyright (C) 2015, Google, Inc
0005  *
0006  * Contributed by:
0007  *  Stephane Eranian <eranian@google.com>
0008  *
0009  * based on GPLv2 source code from Oprofile
0010  * @remark Copyright 2007 OProfile authors
0011  * @author Philippe Elie
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     // Not counting this field
0104     uword total_length;
0105     // version number (2 currently)
0106     uhalf version;
0107     // relative offset from next field to
0108     // program statement
0109     uword prolog_length;
0110     ubyte minimum_instruction_length;
0111     ubyte default_is_stmt;
0112     // line_base - see DWARF 2 specs
0113     sbyte line_base;
0114     // line_range - see DWARF 2 specs
0115     ubyte line_range;
0116     // number of opcode + 1
0117     ubyte opcode_base;
0118     /* follow the array of opcode args nr: ubytes [nr_opcode_base] */
0119     /* follow the search directories index, zero terminated string
0120      * terminated by an empty string.
0121      */
0122     /* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
0123      * the directory index entry, 0 means current directory, then mtime
0124      * and filesize, last entry is followed by en empty string.
0125      */
0126     /* follow the first program statement */
0127 } __packed;
0128 
0129 /* DWARF 2 spec talk only about one possible compilation unit header while
0130  * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
0131  * related to the used arch, an ELF 32 can hold more than 4 Go of debug
0132  * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
0133  * become a problem if we generate more than 4GB of debug information.
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 /* field filled at run time are marked with -1 */
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,    /* could be better when min instruction size != 1 */
0150     .default_is_stmt = 1,   /* we don't take care about basic block */
0151     .line_base = -5,    /* sensible value for line base ... */
0152     .line_range = -14,     /* ... and line range are guessed statically */
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 /* field filled at run time are marked with -1 */
0172 static struct compilation_unit_header default_comp_unit_header = {
0173     .total_length = -1,
0174     .version = 2,
0175     .debug_abbrev_offset = 0,     /* we reuse the same abbrev entries for all comp unit */
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     /* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
0276     emit_unsigned_LEB128(be, strlen(filename) + 5);
0277     emit_opcode(be, DW_LNE_define_file);
0278     emit_string(be, filename);
0279     /* directory index 0=do not know */
0280         emit_unsigned_LEB128(be, 0);
0281     /* last modification date on file 0=do not know */
0282         emit_unsigned_LEB128(be, 0);
0283     /* filesize 0=do not know */
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      * delta from line_base
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      * delta of addresses
0310      */
0311     delta_addr = (ent->addr - last_vma) / default_debug_line_header.minimum_instruction_length;
0312 
0313     /* This is not sufficient to ensure opcode will be in [0-256] but
0314      * sufficient to ensure when summing with the delta lineno we will
0315      * not overflow the unsigned long opcode */
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      * Machine state at start of a statement program
0335      * address = 0
0336      * file    = 1
0337      * line    = 1
0338      * column  = 0
0339      * is_stmt = default_is_stmt as given in the debug_line_header
0340      * basic block = 0
0341      * end sequence = 0
0342      */
0343 
0344     /* start state of the state machine we take care of */
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          * check if filename changed, if so add it
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              * lines differ, emit line delta
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              * addresses differ, emit address delta
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              * add new row to matrix
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     // empty directory entry
0412     buffer_ext_add(be, (void *)"", 1);
0413 
0414     // empty filename directory
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      * setup .debug_line section
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; /* .debug_line */
0528     shdr->sh_type = SHT_PROGBITS;
0529     shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
0530     shdr->sh_flags = 0;
0531     shdr->sh_entsize = 0;
0532 
0533     /*
0534      * setup .debug_info section
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; /* .debug_info */
0562     shdr->sh_type = SHT_PROGBITS;
0563     shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
0564     shdr->sh_flags = 0;
0565     shdr->sh_entsize = 0;
0566 
0567     /*
0568      * setup .debug_abbrev section
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; /* .debug_info */
0596     shdr->sh_type = SHT_PROGBITS;
0597     shdr->sh_addr = 0; /* must be zero or == sh_offset -> dynamic object */
0598     shdr->sh_flags = 0;
0599     shdr->sh_entsize = 0;
0600 
0601     /*
0602      * now we update the ELF image with all the sections
0603      */
0604     if (elf_update(e, ELF_C_WRITE) < 0) {
0605         warnx("elf_update debug failed");
0606         return -1;
0607     }
0608     return 0;
0609 }