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