1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2015, Google, Inc
7 * Stephane Eranian <eranian@google.com>
9 * based on GPLv2 source code from Oprofile
10 * @remark Copyright 2007 OProfile authors
11 * @author Philippe Elie
13 #include <linux/compiler.h>
14 #include <linux/zalloc.h>
15 #include <sys/types.h>
29 #include "../util/jitdump.h"
31 #define BUFFER_EXT_DFL_SIZE (4 * 1024)
33 typedef uint32_t uword
;
34 typedef uint16_t uhalf
;
35 typedef int32_t sword
;
36 typedef int16_t shalf
;
37 typedef uint8_t ubyte
;
47 buffer_ext_dump(struct buffer_ext
*be
, const char *msg
)
50 warnx("DUMP for %s", msg
);
51 for (i
= 0 ; i
< be
->cur_pos
; i
++)
52 warnx("%4zu 0x%02x", i
, (((char *)be
->data
)[i
]) & 0xff);
56 buffer_ext_add(struct buffer_ext
*be
, void *addr
, size_t sz
)
59 size_t be_sz
= be
->max_sz
;
62 if ((be
->cur_pos
+ sz
) < be_sz
) {
63 memcpy(be
->data
+ be
->cur_pos
, addr
, sz
);
69 be_sz
= BUFFER_EXT_DFL_SIZE
;
73 tmp
= realloc(be
->data
, be_sz
);
84 buffer_ext_init(struct buffer_ext
*be
)
92 buffer_ext_exit(struct buffer_ext
*be
)
98 buffer_ext_size(struct buffer_ext
*be
)
104 buffer_ext_addr(struct buffer_ext
*be
)
109 struct debug_line_header
{
110 // Not counting this field
112 // version number (2 currently)
114 // relative offset from next field to
117 ubyte minimum_instruction_length
;
118 ubyte default_is_stmt
;
119 // line_base - see DWARF 2 specs
121 // line_range - see DWARF 2 specs
123 // number of opcode + 1
125 /* follow the array of opcode args nr: ubytes [nr_opcode_base] */
126 /* follow the search directories index, zero terminated string
127 * terminated by an empty string.
129 /* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
130 * the directory index entry, 0 means current directory, then mtime
131 * and filesize, last entry is followed by en empty string.
133 /* follow the first program statement */
136 /* DWARF 2 spec talk only about one possible compilation unit header while
137 * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
138 * related to the used arch, an ELF 32 can hold more than 4 Go of debug
139 * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
140 * become a problem if we generate more than 4GB of debug information.
142 struct compilation_unit_header
{
145 uword debug_abbrev_offset
;
149 #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
151 /* field filled at run time are marked with -1 */
152 static struct debug_line_header
const default_debug_line_header
= {
156 .minimum_instruction_length
= 1, /* could be better when min instruction size != 1 */
157 .default_is_stmt
= 1, /* we don't take care about basic block */
158 .line_base
= -5, /* sensible value for line base ... */
159 .line_range
= -14, /* ... and line range are guessed statically */
160 .opcode_base
= DW_LNS_num_opcode
163 static ubyte standard_opcode_length
[] =
165 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
169 [DW_LNS_advance_pc
] = 1,
170 [DW_LNS_advance_line
] = 1,
171 [DW_LNS_set_file
] = 1,
172 [DW_LNS_set_column
] = 1,
173 [DW_LNS_fixed_advance_pc
] = 1,
174 [DW_LNS_set_isa
] = 1,
178 /* field filled at run time are marked with -1 */
179 static struct compilation_unit_header default_comp_unit_header
= {
182 .debug_abbrev_offset
= 0, /* we reuse the same abbrev entries for all comp unit */
183 .pointer_size
= sizeof(void *)
186 static void emit_uword(struct buffer_ext
*be
, uword data
)
188 buffer_ext_add(be
, &data
, sizeof(uword
));
191 static void emit_string(struct buffer_ext
*be
, const char *s
)
193 buffer_ext_add(be
, (void *)s
, strlen(s
) + 1);
196 static void emit_unsigned_LEB128(struct buffer_ext
*be
,
200 ubyte cur
= data
& 0x7F;
204 buffer_ext_add(be
, &cur
, 1);
208 static void emit_signed_LEB128(struct buffer_ext
*be
, long data
)
211 int negative
= data
< 0;
212 int size
= sizeof(long) * CHAR_BIT
;
214 ubyte cur
= data
& 0x7F;
217 data
|= - (1 << (size
- 7));
218 if ((data
== 0 && !(cur
& 0x40)) ||
219 (data
== -1l && (cur
& 0x40)))
223 buffer_ext_add(be
, &cur
, 1);
227 static void emit_extended_opcode(struct buffer_ext
*be
, ubyte opcode
,
228 void *data
, size_t data_len
)
230 buffer_ext_add(be
, (char *)"", 1);
232 emit_unsigned_LEB128(be
, data_len
+ 1);
234 buffer_ext_add(be
, &opcode
, 1);
235 buffer_ext_add(be
, data
, data_len
);
238 static void emit_opcode(struct buffer_ext
*be
, ubyte opcode
)
240 buffer_ext_add(be
, &opcode
, 1);
243 static void emit_opcode_signed(struct buffer_ext
*be
,
244 ubyte opcode
, long data
)
246 buffer_ext_add(be
, &opcode
, 1);
247 emit_signed_LEB128(be
, data
);
250 static void emit_opcode_unsigned(struct buffer_ext
*be
, ubyte opcode
,
253 buffer_ext_add(be
, &opcode
, 1);
254 emit_unsigned_LEB128(be
, data
);
257 static void emit_advance_pc(struct buffer_ext
*be
, unsigned long delta_pc
)
259 emit_opcode_unsigned(be
, DW_LNS_advance_pc
, delta_pc
);
262 static void emit_advance_lineno(struct buffer_ext
*be
, long delta_lineno
)
264 emit_opcode_signed(be
, DW_LNS_advance_line
, delta_lineno
);
267 static void emit_lne_end_of_sequence(struct buffer_ext
*be
)
269 emit_extended_opcode(be
, DW_LNE_end_sequence
, NULL
, 0);
272 static void emit_set_file(struct buffer_ext
*be
, unsigned long idx
)
274 emit_opcode_unsigned(be
, DW_LNS_set_file
, idx
);
277 static void emit_lne_define_filename(struct buffer_ext
*be
,
278 const char *filename
)
280 buffer_ext_add(be
, (void *)"", 1);
282 /* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
283 emit_unsigned_LEB128(be
, strlen(filename
) + 5);
284 emit_opcode(be
, DW_LNE_define_file
);
285 emit_string(be
, filename
);
286 /* directory index 0=do not know */
287 emit_unsigned_LEB128(be
, 0);
288 /* last modification date on file 0=do not know */
289 emit_unsigned_LEB128(be
, 0);
290 /* filesize 0=do not know */
291 emit_unsigned_LEB128(be
, 0);
294 static void emit_lne_set_address(struct buffer_ext
*be
,
297 emit_extended_opcode(be
, DW_LNE_set_address
, &address
, sizeof(unsigned long));
300 static ubyte
get_special_opcode(struct debug_entry
*ent
,
301 unsigned int last_line
,
302 unsigned long last_vma
)
305 unsigned long delta_addr
;
308 * delta from line_base
310 temp
= (ent
->lineno
- last_line
) - default_debug_line_header
.line_base
;
312 if (temp
>= default_debug_line_header
.line_range
)
318 delta_addr
= (ent
->addr
- last_vma
) / default_debug_line_header
.minimum_instruction_length
;
320 /* This is not sufficient to ensure opcode will be in [0-256] but
321 * sufficient to ensure when summing with the delta lineno we will
322 * not overflow the unsigned long opcode */
324 if (delta_addr
<= 256 / default_debug_line_header
.line_range
) {
325 unsigned long opcode
= temp
+
326 (delta_addr
* default_debug_line_header
.line_range
) +
327 default_debug_line_header
.opcode_base
;
329 return opcode
<= 255 ? opcode
: 0;
334 static void emit_lineno_info(struct buffer_ext
*be
,
335 struct debug_entry
*ent
, size_t nr_entry
,
336 unsigned long code_addr
)
340 /* as described in the jitdump format */
341 const char repeated_name_marker
[] = {'\xff', '\0'};
344 * Machine state at start of a statement program
349 * is_stmt = default_is_stmt as given in the debug_line_header
354 /* start state of the state machine we take care of */
355 unsigned long last_vma
= 0;
356 char const *cur_filename
= NULL
;
357 unsigned long cur_file_idx
= 0;
360 emit_lne_set_address(be
, (void *)code_addr
);
362 for (i
= 0; i
< nr_entry
; i
++, ent
= debug_entry_next(ent
)) {
364 ubyte special_opcode
;
367 * check if filename changed, if so add it
369 if ((!cur_filename
|| strcmp(cur_filename
, ent
->name
)) &&
370 strcmp(repeated_name_marker
, ent
->name
)) {
371 emit_lne_define_filename(be
, ent
->name
);
372 cur_filename
= ent
->name
;
373 emit_set_file(be
, ++cur_file_idx
);
377 special_opcode
= get_special_opcode(ent
, last_line
, last_vma
);
378 if (special_opcode
!= 0) {
379 last_line
= ent
->lineno
;
380 last_vma
= ent
->addr
;
381 emit_opcode(be
, special_opcode
);
384 * lines differ, emit line delta
386 if (last_line
!= ent
->lineno
) {
387 emit_advance_lineno(be
, ent
->lineno
- last_line
);
388 last_line
= ent
->lineno
;
392 * addresses differ, emit address delta
394 if (last_vma
!= ent
->addr
) {
395 emit_advance_pc(be
, ent
->addr
- last_vma
);
396 last_vma
= ent
->addr
;
400 * add new row to matrix
403 emit_opcode(be
, DW_LNS_copy
);
408 static void add_debug_line(struct buffer_ext
*be
,
409 struct debug_entry
*ent
, size_t nr_entry
,
410 unsigned long code_addr
)
412 struct debug_line_header
* dbg_header
;
415 old_size
= buffer_ext_size(be
);
417 buffer_ext_add(be
, (void *)&default_debug_line_header
,
418 sizeof(default_debug_line_header
));
420 buffer_ext_add(be
, &standard_opcode_length
, sizeof(standard_opcode_length
));
422 // empty directory entry
423 buffer_ext_add(be
, (void *)"", 1);
425 // empty filename directory
426 buffer_ext_add(be
, (void *)"", 1);
428 dbg_header
= buffer_ext_addr(be
) + old_size
;
429 dbg_header
->prolog_length
= (buffer_ext_size(be
) - old_size
) -
430 offsetof(struct debug_line_header
, minimum_instruction_length
);
432 emit_lineno_info(be
, ent
, nr_entry
, code_addr
);
434 emit_lne_end_of_sequence(be
);
436 dbg_header
= buffer_ext_addr(be
) + old_size
;
437 dbg_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
438 offsetof(struct debug_line_header
, version
);
442 add_debug_abbrev(struct buffer_ext
*be
)
444 emit_unsigned_LEB128(be
, 1);
445 emit_unsigned_LEB128(be
, DW_TAG_compile_unit
);
446 emit_unsigned_LEB128(be
, DW_CHILDREN_yes
);
447 emit_unsigned_LEB128(be
, DW_AT_stmt_list
);
448 emit_unsigned_LEB128(be
, DW_FORM_data4
);
449 emit_unsigned_LEB128(be
, 0);
450 emit_unsigned_LEB128(be
, 0);
451 emit_unsigned_LEB128(be
, 0);
455 add_compilation_unit(struct buffer_ext
*be
,
456 size_t offset_debug_line
)
458 struct compilation_unit_header
*comp_unit_header
;
459 size_t old_size
= buffer_ext_size(be
);
461 buffer_ext_add(be
, &default_comp_unit_header
,
462 sizeof(default_comp_unit_header
));
464 emit_unsigned_LEB128(be
, 1);
465 emit_uword(be
, offset_debug_line
);
467 comp_unit_header
= buffer_ext_addr(be
) + old_size
;
468 comp_unit_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
469 offsetof(struct compilation_unit_header
, version
);
473 jit_process_debug_info(uint64_t code_addr
,
474 void *debug
, int nr_debug_entries
,
475 struct buffer_ext
*dl
,
476 struct buffer_ext
*da
,
477 struct buffer_ext
*di
)
479 struct debug_entry
*ent
= debug
;
482 for (i
= 0; i
< nr_debug_entries
; i
++) {
483 ent
->addr
= ent
->addr
- code_addr
;
484 ent
= debug_entry_next(ent
);
486 add_compilation_unit(di
, buffer_ext_size(dl
));
487 add_debug_line(dl
, debug
, nr_debug_entries
, GEN_ELF_TEXT_OFFSET
);
488 add_debug_abbrev(da
);
489 if (0) buffer_ext_dump(da
, "abbrev");
495 jit_add_debug_info(Elf
*e
, uint64_t code_addr
, void *debug
, int nr_debug_entries
)
500 struct buffer_ext dl
, di
, da
;
503 buffer_ext_init(&dl
);
504 buffer_ext_init(&di
);
505 buffer_ext_init(&da
);
507 if (jit_process_debug_info(code_addr
, debug
, nr_debug_entries
, &dl
, &da
, &di
))
511 * setup .debug_line section
515 warnx("cannot create section");
519 d
= elf_newdata(scn
);
521 warnx("cannot get new data");
527 d
->d_buf
= buffer_ext_addr(&dl
);
528 d
->d_type
= ELF_T_BYTE
;
529 d
->d_size
= buffer_ext_size(&dl
);
530 d
->d_version
= EV_CURRENT
;
532 shdr
= elf_getshdr(scn
);
534 warnx("cannot get section header");
538 shdr
->sh_name
= 52; /* .debug_line */
539 shdr
->sh_type
= SHT_PROGBITS
;
540 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
542 shdr
->sh_entsize
= 0;
545 * setup .debug_info section
549 warnx("cannot create section");
553 d
= elf_newdata(scn
);
555 warnx("cannot get new data");
561 d
->d_buf
= buffer_ext_addr(&di
);
562 d
->d_type
= ELF_T_BYTE
;
563 d
->d_size
= buffer_ext_size(&di
);
564 d
->d_version
= EV_CURRENT
;
566 shdr
= elf_getshdr(scn
);
568 warnx("cannot get section header");
572 shdr
->sh_name
= 64; /* .debug_info */
573 shdr
->sh_type
= SHT_PROGBITS
;
574 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
576 shdr
->sh_entsize
= 0;
579 * setup .debug_abbrev section
583 warnx("cannot create section");
587 d
= elf_newdata(scn
);
589 warnx("cannot get new data");
595 d
->d_buf
= buffer_ext_addr(&da
);
596 d
->d_type
= ELF_T_BYTE
;
597 d
->d_size
= buffer_ext_size(&da
);
598 d
->d_version
= EV_CURRENT
;
600 shdr
= elf_getshdr(scn
);
602 warnx("cannot get section header");
606 shdr
->sh_name
= 76; /* .debug_info */
607 shdr
->sh_type
= SHT_PROGBITS
;
608 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
610 shdr
->sh_entsize
= 0;
613 * now we update the ELF image with all the sections
615 if (elf_update(e
, ELF_C_WRITE
) < 0)
616 warnx("elf_update debug failed");
621 buffer_ext_exit(&dl
);
622 buffer_ext_exit(&di
);
623 buffer_ext_exit(&da
);