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 <linux/compiler.h>
15 #include <sys/types.h>
30 #include "../util/jitdump.h"
32 #define BUFFER_EXT_DFL_SIZE (4 * 1024)
34 typedef uint32_t uword
;
35 typedef uint16_t uhalf
;
36 typedef int32_t sword
;
37 typedef int16_t shalf
;
38 typedef uint8_t ubyte
;
48 buffer_ext_dump(struct buffer_ext
*be
, const char *msg
)
51 warnx("DUMP for %s", msg
);
52 for (i
= 0 ; i
< be
->cur_pos
; i
++)
53 warnx("%4zu 0x%02x", i
, (((char *)be
->data
)[i
]) & 0xff);
57 buffer_ext_add(struct buffer_ext
*be
, void *addr
, size_t sz
)
60 size_t be_sz
= be
->max_sz
;
63 if ((be
->cur_pos
+ sz
) < be_sz
) {
64 memcpy(be
->data
+ be
->cur_pos
, addr
, sz
);
70 be_sz
= BUFFER_EXT_DFL_SIZE
;
74 tmp
= realloc(be
->data
, be_sz
);
85 buffer_ext_init(struct buffer_ext
*be
)
93 buffer_ext_size(struct buffer_ext
*be
)
99 buffer_ext_addr(struct buffer_ext
*be
)
104 struct debug_line_header
{
105 // Not counting this field
107 // version number (2 currently)
109 // relative offset from next field to
112 ubyte minimum_instruction_length
;
113 ubyte default_is_stmt
;
114 // line_base - see DWARF 2 specs
116 // line_range - see DWARF 2 specs
118 // number of opcode + 1
120 /* follow the array of opcode args nr: ubytes [nr_opcode_base] */
121 /* follow the search directories index, zero terminated string
122 * terminated by an empty string.
124 /* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
125 * the directory index entry, 0 means current directory, then mtime
126 * and filesize, last entry is followed by en empty string.
128 /* follow the first program statement */
131 /* DWARF 2 spec talk only about one possible compilation unit header while
132 * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
133 * related to the used arch, an ELF 32 can hold more than 4 Go of debug
134 * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
135 * become a problem if we generate more than 4GB of debug information.
137 struct compilation_unit_header
{
140 uword debug_abbrev_offset
;
144 #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
146 /* field filled at run time are marked with -1 */
147 static struct debug_line_header
const default_debug_line_header
= {
151 .minimum_instruction_length
= 1, /* could be better when min instruction size != 1 */
152 .default_is_stmt
= 1, /* we don't take care about basic block */
153 .line_base
= -5, /* sensible value for line base ... */
154 .line_range
= -14, /* ... and line range are guessed statically */
155 .opcode_base
= DW_LNS_num_opcode
158 static ubyte standard_opcode_length
[] =
160 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
164 [DW_LNS_advance_pc
] = 1,
165 [DW_LNS_advance_line
] = 1,
166 [DW_LNS_set_file
] = 1,
167 [DW_LNS_set_column
] = 1,
168 [DW_LNS_fixed_advance_pc
] = 1,
169 [DW_LNS_set_isa
] = 1,
173 /* field filled at run time are marked with -1 */
174 static struct compilation_unit_header default_comp_unit_header
= {
177 .debug_abbrev_offset
= 0, /* we reuse the same abbrev entries for all comp unit */
178 .pointer_size
= sizeof(void *)
181 static void emit_uword(struct buffer_ext
*be
, uword data
)
183 buffer_ext_add(be
, &data
, sizeof(uword
));
186 static void emit_string(struct buffer_ext
*be
, const char *s
)
188 buffer_ext_add(be
, (void *)s
, strlen(s
) + 1);
191 static void emit_unsigned_LEB128(struct buffer_ext
*be
,
195 ubyte cur
= data
& 0x7F;
199 buffer_ext_add(be
, &cur
, 1);
203 static void emit_signed_LEB128(struct buffer_ext
*be
, long data
)
206 int negative
= data
< 0;
207 int size
= sizeof(long) * CHAR_BIT
;
209 ubyte cur
= data
& 0x7F;
212 data
|= - (1 << (size
- 7));
213 if ((data
== 0 && !(cur
& 0x40)) ||
214 (data
== -1l && (cur
& 0x40)))
218 buffer_ext_add(be
, &cur
, 1);
222 static void emit_extended_opcode(struct buffer_ext
*be
, ubyte opcode
,
223 void *data
, size_t data_len
)
225 buffer_ext_add(be
, (char *)"", 1);
227 emit_unsigned_LEB128(be
, data_len
+ 1);
229 buffer_ext_add(be
, &opcode
, 1);
230 buffer_ext_add(be
, data
, data_len
);
233 static void emit_opcode(struct buffer_ext
*be
, ubyte opcode
)
235 buffer_ext_add(be
, &opcode
, 1);
238 static void emit_opcode_signed(struct buffer_ext
*be
,
239 ubyte opcode
, long data
)
241 buffer_ext_add(be
, &opcode
, 1);
242 emit_signed_LEB128(be
, data
);
245 static void emit_opcode_unsigned(struct buffer_ext
*be
, ubyte opcode
,
248 buffer_ext_add(be
, &opcode
, 1);
249 emit_unsigned_LEB128(be
, data
);
252 static void emit_advance_pc(struct buffer_ext
*be
, unsigned long delta_pc
)
254 emit_opcode_unsigned(be
, DW_LNS_advance_pc
, delta_pc
);
257 static void emit_advance_lineno(struct buffer_ext
*be
, long delta_lineno
)
259 emit_opcode_signed(be
, DW_LNS_advance_line
, delta_lineno
);
262 static void emit_lne_end_of_sequence(struct buffer_ext
*be
)
264 emit_extended_opcode(be
, DW_LNE_end_sequence
, NULL
, 0);
267 static void emit_set_file(struct buffer_ext
*be
, unsigned long idx
)
269 emit_opcode_unsigned(be
, DW_LNS_set_file
, idx
);
272 static void emit_lne_define_filename(struct buffer_ext
*be
,
273 const char *filename
)
275 buffer_ext_add(be
, (void *)"", 1);
277 /* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
278 emit_unsigned_LEB128(be
, strlen(filename
) + 5);
279 emit_opcode(be
, DW_LNE_define_file
);
280 emit_string(be
, filename
);
281 /* directory index 0=do not know */
282 emit_unsigned_LEB128(be
, 0);
283 /* last modification date on file 0=do not know */
284 emit_unsigned_LEB128(be
, 0);
285 /* filesize 0=do not know */
286 emit_unsigned_LEB128(be
, 0);
289 static void emit_lne_set_address(struct buffer_ext
*be
,
292 emit_extended_opcode(be
, DW_LNE_set_address
, &address
, sizeof(unsigned long));
295 static ubyte
get_special_opcode(struct debug_entry
*ent
,
296 unsigned int last_line
,
297 unsigned long last_vma
)
300 unsigned long delta_addr
;
303 * delta from line_base
305 temp
= (ent
->lineno
- last_line
) - default_debug_line_header
.line_base
;
307 if (temp
>= default_debug_line_header
.line_range
)
313 delta_addr
= (ent
->addr
- last_vma
) / default_debug_line_header
.minimum_instruction_length
;
315 /* This is not sufficient to ensure opcode will be in [0-256] but
316 * sufficient to ensure when summing with the delta lineno we will
317 * not overflow the unsigned long opcode */
319 if (delta_addr
<= 256 / default_debug_line_header
.line_range
) {
320 unsigned long opcode
= temp
+
321 (delta_addr
* default_debug_line_header
.line_range
) +
322 default_debug_line_header
.opcode_base
;
324 return opcode
<= 255 ? opcode
: 0;
329 static void emit_lineno_info(struct buffer_ext
*be
,
330 struct debug_entry
*ent
, size_t nr_entry
,
331 unsigned long code_addr
)
336 * Machine state at start of a statement program
341 * is_stmt = default_is_stmt as given in the debug_line_header
346 /* start state of the state machine we take care of */
347 unsigned long last_vma
= code_addr
;
348 char const *cur_filename
= NULL
;
349 unsigned long cur_file_idx
= 0;
352 emit_lne_set_address(be
, (void *)code_addr
);
354 for (i
= 0; i
< nr_entry
; i
++, ent
= debug_entry_next(ent
)) {
356 ubyte special_opcode
;
359 * check if filename changed, if so add it
361 if (!cur_filename
|| strcmp(cur_filename
, ent
->name
)) {
362 emit_lne_define_filename(be
, ent
->name
);
363 cur_filename
= ent
->name
;
364 emit_set_file(be
, ++cur_file_idx
);
368 special_opcode
= get_special_opcode(ent
, last_line
, last_vma
);
369 if (special_opcode
!= 0) {
370 last_line
= ent
->lineno
;
371 last_vma
= ent
->addr
;
372 emit_opcode(be
, special_opcode
);
375 * lines differ, emit line delta
377 if (last_line
!= ent
->lineno
) {
378 emit_advance_lineno(be
, ent
->lineno
- last_line
);
379 last_line
= ent
->lineno
;
383 * addresses differ, emit address delta
385 if (last_vma
!= ent
->addr
) {
386 emit_advance_pc(be
, ent
->addr
- last_vma
);
387 last_vma
= ent
->addr
;
391 * add new row to matrix
394 emit_opcode(be
, DW_LNS_copy
);
399 static void add_debug_line(struct buffer_ext
*be
,
400 struct debug_entry
*ent
, size_t nr_entry
,
401 unsigned long code_addr
)
403 struct debug_line_header
* dbg_header
;
406 old_size
= buffer_ext_size(be
);
408 buffer_ext_add(be
, (void *)&default_debug_line_header
,
409 sizeof(default_debug_line_header
));
411 buffer_ext_add(be
, &standard_opcode_length
, sizeof(standard_opcode_length
));
413 // empty directory entry
414 buffer_ext_add(be
, (void *)"", 1);
416 // empty filename directory
417 buffer_ext_add(be
, (void *)"", 1);
419 dbg_header
= buffer_ext_addr(be
) + old_size
;
420 dbg_header
->prolog_length
= (buffer_ext_size(be
) - old_size
) -
421 offsetof(struct debug_line_header
, minimum_instruction_length
);
423 emit_lineno_info(be
, ent
, nr_entry
, code_addr
);
425 emit_lne_end_of_sequence(be
);
427 dbg_header
= buffer_ext_addr(be
) + old_size
;
428 dbg_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
429 offsetof(struct debug_line_header
, version
);
433 add_debug_abbrev(struct buffer_ext
*be
)
435 emit_unsigned_LEB128(be
, 1);
436 emit_unsigned_LEB128(be
, DW_TAG_compile_unit
);
437 emit_unsigned_LEB128(be
, DW_CHILDREN_yes
);
438 emit_unsigned_LEB128(be
, DW_AT_stmt_list
);
439 emit_unsigned_LEB128(be
, DW_FORM_data4
);
440 emit_unsigned_LEB128(be
, 0);
441 emit_unsigned_LEB128(be
, 0);
442 emit_unsigned_LEB128(be
, 0);
446 add_compilation_unit(struct buffer_ext
*be
,
447 size_t offset_debug_line
)
449 struct compilation_unit_header
*comp_unit_header
;
450 size_t old_size
= buffer_ext_size(be
);
452 buffer_ext_add(be
, &default_comp_unit_header
,
453 sizeof(default_comp_unit_header
));
455 emit_unsigned_LEB128(be
, 1);
456 emit_uword(be
, offset_debug_line
);
458 comp_unit_header
= buffer_ext_addr(be
) + old_size
;
459 comp_unit_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
460 offsetof(struct compilation_unit_header
, version
);
464 jit_process_debug_info(uint64_t code_addr
,
465 void *debug
, int nr_debug_entries
,
466 struct buffer_ext
*dl
,
467 struct buffer_ext
*da
,
468 struct buffer_ext
*di
)
470 struct debug_entry
*ent
= debug
;
473 for (i
= 0; i
< nr_debug_entries
; i
++) {
474 ent
->addr
= ent
->addr
- code_addr
;
475 ent
= debug_entry_next(ent
);
477 add_compilation_unit(di
, buffer_ext_size(dl
));
478 add_debug_line(dl
, debug
, nr_debug_entries
, 0);
479 add_debug_abbrev(da
);
480 if (0) buffer_ext_dump(da
, "abbrev");
486 jit_add_debug_info(Elf
*e
, uint64_t code_addr
, void *debug
, int nr_debug_entries
)
491 struct buffer_ext dl
, di
, da
;
494 buffer_ext_init(&dl
);
495 buffer_ext_init(&di
);
496 buffer_ext_init(&da
);
498 ret
= jit_process_debug_info(code_addr
, debug
, nr_debug_entries
, &dl
, &da
, &di
);
502 * setup .debug_line section
506 warnx("cannot create section");
510 d
= elf_newdata(scn
);
512 warnx("cannot get new data");
518 d
->d_buf
= buffer_ext_addr(&dl
);
519 d
->d_type
= ELF_T_BYTE
;
520 d
->d_size
= buffer_ext_size(&dl
);
521 d
->d_version
= EV_CURRENT
;
523 shdr
= elf_getshdr(scn
);
525 warnx("cannot get section header");
529 shdr
->sh_name
= 52; /* .debug_line */
530 shdr
->sh_type
= SHT_PROGBITS
;
531 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
533 shdr
->sh_entsize
= 0;
536 * setup .debug_info section
540 warnx("cannot create section");
544 d
= elf_newdata(scn
);
546 warnx("cannot get new data");
552 d
->d_buf
= buffer_ext_addr(&di
);
553 d
->d_type
= ELF_T_BYTE
;
554 d
->d_size
= buffer_ext_size(&di
);
555 d
->d_version
= EV_CURRENT
;
557 shdr
= elf_getshdr(scn
);
559 warnx("cannot get section header");
563 shdr
->sh_name
= 64; /* .debug_info */
564 shdr
->sh_type
= SHT_PROGBITS
;
565 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
567 shdr
->sh_entsize
= 0;
570 * setup .debug_abbrev section
574 warnx("cannot create section");
578 d
= elf_newdata(scn
);
580 warnx("cannot get new data");
586 d
->d_buf
= buffer_ext_addr(&da
);
587 d
->d_type
= ELF_T_BYTE
;
588 d
->d_size
= buffer_ext_size(&da
);
589 d
->d_version
= EV_CURRENT
;
591 shdr
= elf_getshdr(scn
);
593 warnx("cannot get section header");
597 shdr
->sh_name
= 76; /* .debug_info */
598 shdr
->sh_type
= SHT_PROGBITS
;
599 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
601 shdr
->sh_entsize
= 0;
604 * now we update the ELF image with all the sections
606 if (elf_update(e
, ELF_C_WRITE
) < 0) {
607 warnx("elf_update debug failed");