3 * Copyright (C) 2015, Google, Inc
6 * Stephane Eranian <eranian@google.com>
8 * Released under the GPL v2.
10 * based on GPLv2 source code from Oprofile
11 * @remark Copyright 2007 OProfile authors
12 * @author Philippe Elie
14 #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_size(struct buffer_ext
*be
)
98 buffer_ext_addr(struct buffer_ext
*be
)
103 struct debug_line_header
{
104 // Not counting this field
106 // version number (2 currently)
108 // relative offset from next field to
111 ubyte minimum_instruction_length
;
112 ubyte default_is_stmt
;
113 // line_base - see DWARF 2 specs
115 // line_range - see DWARF 2 specs
117 // number of opcode + 1
119 /* follow the array of opcode args nr: ubytes [nr_opcode_base] */
120 /* follow the search directories index, zero terminated string
121 * terminated by an empty string.
123 /* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
124 * the directory index entry, 0 means current directory, then mtime
125 * and filesize, last entry is followed by en empty string.
127 /* follow the first program statement */
128 } __attribute__((packed
));
130 /* DWARF 2 spec talk only about one possible compilation unit header while
131 * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
132 * related to the used arch, an ELF 32 can hold more than 4 Go of debug
133 * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
134 * become a problem if we generate more than 4GB of debug information.
136 struct compilation_unit_header
{
139 uword debug_abbrev_offset
;
141 } __attribute__((packed
));
143 #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
145 /* field filled at run time are marked with -1 */
146 static struct debug_line_header
const default_debug_line_header
= {
150 .minimum_instruction_length
= 1, /* could be better when min instruction size != 1 */
151 .default_is_stmt
= 1, /* we don't take care about basic block */
152 .line_base
= -5, /* sensible value for line base ... */
153 .line_range
= -14, /* ... and line range are guessed statically */
154 .opcode_base
= DW_LNS_num_opcode
157 static ubyte standard_opcode_length
[] =
159 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
163 [DW_LNS_advance_pc
] = 1,
164 [DW_LNS_advance_line
] = 1,
165 [DW_LNS_set_file
] = 1,
166 [DW_LNS_set_column
] = 1,
167 [DW_LNS_fixed_advance_pc
] = 1,
168 [DW_LNS_set_isa
] = 1,
172 /* field filled at run time are marked with -1 */
173 static struct compilation_unit_header default_comp_unit_header
= {
176 .debug_abbrev_offset
= 0, /* we reuse the same abbrev entries for all comp unit */
177 .pointer_size
= sizeof(void *)
180 static void emit_uword(struct buffer_ext
*be
, uword data
)
182 buffer_ext_add(be
, &data
, sizeof(uword
));
185 static void emit_string(struct buffer_ext
*be
, const char *s
)
187 buffer_ext_add(be
, (void *)s
, strlen(s
) + 1);
190 static void emit_unsigned_LEB128(struct buffer_ext
*be
,
194 ubyte cur
= data
& 0x7F;
198 buffer_ext_add(be
, &cur
, 1);
202 static void emit_signed_LEB128(struct buffer_ext
*be
, long data
)
205 int negative
= data
< 0;
206 int size
= sizeof(long) * CHAR_BIT
;
208 ubyte cur
= data
& 0x7F;
211 data
|= - (1 << (size
- 7));
212 if ((data
== 0 && !(cur
& 0x40)) ||
213 (data
== -1l && (cur
& 0x40)))
217 buffer_ext_add(be
, &cur
, 1);
221 static void emit_extended_opcode(struct buffer_ext
*be
, ubyte opcode
,
222 void *data
, size_t data_len
)
224 buffer_ext_add(be
, (char *)"", 1);
226 emit_unsigned_LEB128(be
, data_len
+ 1);
228 buffer_ext_add(be
, &opcode
, 1);
229 buffer_ext_add(be
, data
, data_len
);
232 static void emit_opcode(struct buffer_ext
*be
, ubyte opcode
)
234 buffer_ext_add(be
, &opcode
, 1);
237 static void emit_opcode_signed(struct buffer_ext
*be
,
238 ubyte opcode
, long data
)
240 buffer_ext_add(be
, &opcode
, 1);
241 emit_signed_LEB128(be
, data
);
244 static void emit_opcode_unsigned(struct buffer_ext
*be
, ubyte opcode
,
247 buffer_ext_add(be
, &opcode
, 1);
248 emit_unsigned_LEB128(be
, data
);
251 static void emit_advance_pc(struct buffer_ext
*be
, unsigned long delta_pc
)
253 emit_opcode_unsigned(be
, DW_LNS_advance_pc
, delta_pc
);
256 static void emit_advance_lineno(struct buffer_ext
*be
, long delta_lineno
)
258 emit_opcode_signed(be
, DW_LNS_advance_line
, delta_lineno
);
261 static void emit_lne_end_of_sequence(struct buffer_ext
*be
)
263 emit_extended_opcode(be
, DW_LNE_end_sequence
, NULL
, 0);
266 static void emit_set_file(struct buffer_ext
*be
, unsigned long idx
)
268 emit_opcode_unsigned(be
, DW_LNS_set_file
, idx
);
271 static void emit_lne_define_filename(struct buffer_ext
*be
,
272 const char *filename
)
274 buffer_ext_add(be
, (void *)"", 1);
276 /* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
277 emit_unsigned_LEB128(be
, strlen(filename
) + 5);
278 emit_opcode(be
, DW_LNE_define_file
);
279 emit_string(be
, filename
);
280 /* directory index 0=do not know */
281 emit_unsigned_LEB128(be
, 0);
282 /* last modification date on file 0=do not know */
283 emit_unsigned_LEB128(be
, 0);
284 /* filesize 0=do not know */
285 emit_unsigned_LEB128(be
, 0);
288 static void emit_lne_set_address(struct buffer_ext
*be
,
291 emit_extended_opcode(be
, DW_LNE_set_address
, &address
, sizeof(unsigned long));
294 static ubyte
get_special_opcode(struct debug_entry
*ent
,
295 unsigned int last_line
,
296 unsigned long last_vma
)
299 unsigned long delta_addr
;
302 * delta from line_base
304 temp
= (ent
->lineno
- last_line
) - default_debug_line_header
.line_base
;
306 if (temp
>= default_debug_line_header
.line_range
)
312 delta_addr
= (ent
->addr
- last_vma
) / default_debug_line_header
.minimum_instruction_length
;
314 /* This is not sufficient to ensure opcode will be in [0-256] but
315 * sufficient to ensure when summing with the delta lineno we will
316 * not overflow the unsigned long opcode */
318 if (delta_addr
<= 256 / default_debug_line_header
.line_range
) {
319 unsigned long opcode
= temp
+
320 (delta_addr
* default_debug_line_header
.line_range
) +
321 default_debug_line_header
.opcode_base
;
323 return opcode
<= 255 ? opcode
: 0;
328 static void emit_lineno_info(struct buffer_ext
*be
,
329 struct debug_entry
*ent
, size_t nr_entry
,
330 unsigned long code_addr
)
335 * Machine state at start of a statement program
340 * is_stmt = default_is_stmt as given in the debug_line_header
345 /* start state of the state machine we take care of */
346 unsigned long last_vma
= code_addr
;
347 char const *cur_filename
= NULL
;
348 unsigned long cur_file_idx
= 0;
351 emit_lne_set_address(be
, (void *)code_addr
);
353 for (i
= 0; i
< nr_entry
; i
++, ent
= debug_entry_next(ent
)) {
355 ubyte special_opcode
;
358 * check if filename changed, if so add it
360 if (!cur_filename
|| strcmp(cur_filename
, ent
->name
)) {
361 emit_lne_define_filename(be
, ent
->name
);
362 cur_filename
= ent
->name
;
363 emit_set_file(be
, ++cur_file_idx
);
367 special_opcode
= get_special_opcode(ent
, last_line
, last_vma
);
368 if (special_opcode
!= 0) {
369 last_line
= ent
->lineno
;
370 last_vma
= ent
->addr
;
371 emit_opcode(be
, special_opcode
);
374 * lines differ, emit line delta
376 if (last_line
!= ent
->lineno
) {
377 emit_advance_lineno(be
, ent
->lineno
- last_line
);
378 last_line
= ent
->lineno
;
382 * addresses differ, emit address delta
384 if (last_vma
!= ent
->addr
) {
385 emit_advance_pc(be
, ent
->addr
- last_vma
);
386 last_vma
= ent
->addr
;
390 * add new row to matrix
393 emit_opcode(be
, DW_LNS_copy
);
398 static void add_debug_line(struct buffer_ext
*be
,
399 struct debug_entry
*ent
, size_t nr_entry
,
400 unsigned long code_addr
)
402 struct debug_line_header
* dbg_header
;
405 old_size
= buffer_ext_size(be
);
407 buffer_ext_add(be
, (void *)&default_debug_line_header
,
408 sizeof(default_debug_line_header
));
410 buffer_ext_add(be
, &standard_opcode_length
, sizeof(standard_opcode_length
));
412 // empty directory entry
413 buffer_ext_add(be
, (void *)"", 1);
415 // empty filename directory
416 buffer_ext_add(be
, (void *)"", 1);
418 dbg_header
= buffer_ext_addr(be
) + old_size
;
419 dbg_header
->prolog_length
= (buffer_ext_size(be
) - old_size
) -
420 offsetof(struct debug_line_header
, minimum_instruction_length
);
422 emit_lineno_info(be
, ent
, nr_entry
, code_addr
);
424 emit_lne_end_of_sequence(be
);
426 dbg_header
= buffer_ext_addr(be
) + old_size
;
427 dbg_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
428 offsetof(struct debug_line_header
, version
);
432 add_debug_abbrev(struct buffer_ext
*be
)
434 emit_unsigned_LEB128(be
, 1);
435 emit_unsigned_LEB128(be
, DW_TAG_compile_unit
);
436 emit_unsigned_LEB128(be
, DW_CHILDREN_yes
);
437 emit_unsigned_LEB128(be
, DW_AT_stmt_list
);
438 emit_unsigned_LEB128(be
, DW_FORM_data4
);
439 emit_unsigned_LEB128(be
, 0);
440 emit_unsigned_LEB128(be
, 0);
441 emit_unsigned_LEB128(be
, 0);
445 add_compilation_unit(struct buffer_ext
*be
,
446 size_t offset_debug_line
)
448 struct compilation_unit_header
*comp_unit_header
;
449 size_t old_size
= buffer_ext_size(be
);
451 buffer_ext_add(be
, &default_comp_unit_header
,
452 sizeof(default_comp_unit_header
));
454 emit_unsigned_LEB128(be
, 1);
455 emit_uword(be
, offset_debug_line
);
457 comp_unit_header
= buffer_ext_addr(be
) + old_size
;
458 comp_unit_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
459 offsetof(struct compilation_unit_header
, version
);
463 jit_process_debug_info(uint64_t code_addr
,
464 void *debug
, int nr_debug_entries
,
465 struct buffer_ext
*dl
,
466 struct buffer_ext
*da
,
467 struct buffer_ext
*di
)
469 struct debug_entry
*ent
= debug
;
472 for (i
= 0; i
< nr_debug_entries
; i
++) {
473 ent
->addr
= ent
->addr
- code_addr
;
474 ent
= debug_entry_next(ent
);
476 add_compilation_unit(di
, buffer_ext_size(dl
));
477 add_debug_line(dl
, debug
, nr_debug_entries
, 0);
478 add_debug_abbrev(da
);
479 if (0) buffer_ext_dump(da
, "abbrev");
485 jit_add_debug_info(Elf
*e
, uint64_t code_addr
, void *debug
, int nr_debug_entries
)
490 struct buffer_ext dl
, di
, da
;
493 buffer_ext_init(&dl
);
494 buffer_ext_init(&di
);
495 buffer_ext_init(&da
);
497 ret
= jit_process_debug_info(code_addr
, debug
, nr_debug_entries
, &dl
, &da
, &di
);
501 * setup .debug_line section
505 warnx("cannot create section");
509 d
= elf_newdata(scn
);
511 warnx("cannot get new data");
517 d
->d_buf
= buffer_ext_addr(&dl
);
518 d
->d_type
= ELF_T_BYTE
;
519 d
->d_size
= buffer_ext_size(&dl
);
520 d
->d_version
= EV_CURRENT
;
522 shdr
= elf_getshdr(scn
);
524 warnx("cannot get section header");
528 shdr
->sh_name
= 52; /* .debug_line */
529 shdr
->sh_type
= SHT_PROGBITS
;
530 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
532 shdr
->sh_entsize
= 0;
535 * setup .debug_info section
539 warnx("cannot create section");
543 d
= elf_newdata(scn
);
545 warnx("cannot get new data");
551 d
->d_buf
= buffer_ext_addr(&di
);
552 d
->d_type
= ELF_T_BYTE
;
553 d
->d_size
= buffer_ext_size(&di
);
554 d
->d_version
= EV_CURRENT
;
556 shdr
= elf_getshdr(scn
);
558 warnx("cannot get section header");
562 shdr
->sh_name
= 64; /* .debug_info */
563 shdr
->sh_type
= SHT_PROGBITS
;
564 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
566 shdr
->sh_entsize
= 0;
569 * setup .debug_abbrev section
573 warnx("cannot create section");
577 d
= elf_newdata(scn
);
579 warnx("cannot get new data");
585 d
->d_buf
= buffer_ext_addr(&da
);
586 d
->d_type
= ELF_T_BYTE
;
587 d
->d_size
= buffer_ext_size(&da
);
588 d
->d_version
= EV_CURRENT
;
590 shdr
= elf_getshdr(scn
);
592 warnx("cannot get section header");
596 shdr
->sh_name
= 76; /* .debug_info */
597 shdr
->sh_type
= SHT_PROGBITS
;
598 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
600 shdr
->sh_entsize
= 0;
603 * now we update the ELF image with all the sections
605 if (elf_update(e
, ELF_C_WRITE
) < 0) {
606 warnx("elf_update debug failed");