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 <sys/types.h>
28 #include "../util/jitdump.h"
30 #define BUFFER_EXT_DFL_SIZE (4 * 1024)
32 typedef uint32_t uword
;
33 typedef uint16_t uhalf
;
34 typedef int32_t sword
;
35 typedef int16_t shalf
;
36 typedef uint8_t ubyte
;
46 buffer_ext_dump(struct buffer_ext
*be
, const char *msg
)
49 warnx("DUMP for %s", msg
);
50 for (i
= 0 ; i
< be
->cur_pos
; i
++)
51 warnx("%4zu 0x%02x", i
, (((char *)be
->data
)[i
]) & 0xff);
55 buffer_ext_add(struct buffer_ext
*be
, void *addr
, size_t sz
)
58 size_t be_sz
= be
->max_sz
;
61 if ((be
->cur_pos
+ sz
) < be_sz
) {
62 memcpy(be
->data
+ be
->cur_pos
, addr
, sz
);
68 be_sz
= BUFFER_EXT_DFL_SIZE
;
72 tmp
= realloc(be
->data
, be_sz
);
83 buffer_ext_init(struct buffer_ext
*be
)
91 buffer_ext_size(struct buffer_ext
*be
)
97 buffer_ext_addr(struct buffer_ext
*be
)
102 struct debug_line_header
{
103 // Not counting this field
105 // version number (2 currently)
107 // relative offset from next field to
110 ubyte minimum_instruction_length
;
111 ubyte default_is_stmt
;
112 // line_base - see DWARF 2 specs
114 // line_range - see DWARF 2 specs
116 // number of opcode + 1
118 /* follow the array of opcode args nr: ubytes [nr_opcode_base] */
119 /* follow the search directories index, zero terminated string
120 * terminated by an empty string.
122 /* follow an array of { filename, LEB128, LEB128, LEB128 }, first is
123 * the directory index entry, 0 means current directory, then mtime
124 * and filesize, last entry is followed by en empty string.
126 /* follow the first program statement */
129 /* DWARF 2 spec talk only about one possible compilation unit header while
130 * binutils can handle two flavours of dwarf 2, 32 and 64 bits, this is not
131 * related to the used arch, an ELF 32 can hold more than 4 Go of debug
132 * information. For now we handle only DWARF 2 32 bits comp unit. It'll only
133 * become a problem if we generate more than 4GB of debug information.
135 struct compilation_unit_header
{
138 uword debug_abbrev_offset
;
142 #define DW_LNS_num_opcode (DW_LNS_set_isa + 1)
144 /* field filled at run time are marked with -1 */
145 static struct debug_line_header
const default_debug_line_header
= {
149 .minimum_instruction_length
= 1, /* could be better when min instruction size != 1 */
150 .default_is_stmt
= 1, /* we don't take care about basic block */
151 .line_base
= -5, /* sensible value for line base ... */
152 .line_range
= -14, /* ... and line range are guessed statically */
153 .opcode_base
= DW_LNS_num_opcode
156 static ubyte standard_opcode_length
[] =
158 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1
162 [DW_LNS_advance_pc
] = 1,
163 [DW_LNS_advance_line
] = 1,
164 [DW_LNS_set_file
] = 1,
165 [DW_LNS_set_column
] = 1,
166 [DW_LNS_fixed_advance_pc
] = 1,
167 [DW_LNS_set_isa
] = 1,
171 /* field filled at run time are marked with -1 */
172 static struct compilation_unit_header default_comp_unit_header
= {
175 .debug_abbrev_offset
= 0, /* we reuse the same abbrev entries for all comp unit */
176 .pointer_size
= sizeof(void *)
179 static void emit_uword(struct buffer_ext
*be
, uword data
)
181 buffer_ext_add(be
, &data
, sizeof(uword
));
184 static void emit_string(struct buffer_ext
*be
, const char *s
)
186 buffer_ext_add(be
, (void *)s
, strlen(s
) + 1);
189 static void emit_unsigned_LEB128(struct buffer_ext
*be
,
193 ubyte cur
= data
& 0x7F;
197 buffer_ext_add(be
, &cur
, 1);
201 static void emit_signed_LEB128(struct buffer_ext
*be
, long data
)
204 int negative
= data
< 0;
205 int size
= sizeof(long) * CHAR_BIT
;
207 ubyte cur
= data
& 0x7F;
210 data
|= - (1 << (size
- 7));
211 if ((data
== 0 && !(cur
& 0x40)) ||
212 (data
== -1l && (cur
& 0x40)))
216 buffer_ext_add(be
, &cur
, 1);
220 static void emit_extended_opcode(struct buffer_ext
*be
, ubyte opcode
,
221 void *data
, size_t data_len
)
223 buffer_ext_add(be
, (char *)"", 1);
225 emit_unsigned_LEB128(be
, data_len
+ 1);
227 buffer_ext_add(be
, &opcode
, 1);
228 buffer_ext_add(be
, data
, data_len
);
231 static void emit_opcode(struct buffer_ext
*be
, ubyte opcode
)
233 buffer_ext_add(be
, &opcode
, 1);
236 static void emit_opcode_signed(struct buffer_ext
*be
,
237 ubyte opcode
, long data
)
239 buffer_ext_add(be
, &opcode
, 1);
240 emit_signed_LEB128(be
, data
);
243 static void emit_opcode_unsigned(struct buffer_ext
*be
, ubyte opcode
,
246 buffer_ext_add(be
, &opcode
, 1);
247 emit_unsigned_LEB128(be
, data
);
250 static void emit_advance_pc(struct buffer_ext
*be
, unsigned long delta_pc
)
252 emit_opcode_unsigned(be
, DW_LNS_advance_pc
, delta_pc
);
255 static void emit_advance_lineno(struct buffer_ext
*be
, long delta_lineno
)
257 emit_opcode_signed(be
, DW_LNS_advance_line
, delta_lineno
);
260 static void emit_lne_end_of_sequence(struct buffer_ext
*be
)
262 emit_extended_opcode(be
, DW_LNE_end_sequence
, NULL
, 0);
265 static void emit_set_file(struct buffer_ext
*be
, unsigned long idx
)
267 emit_opcode_unsigned(be
, DW_LNS_set_file
, idx
);
270 static void emit_lne_define_filename(struct buffer_ext
*be
,
271 const char *filename
)
273 buffer_ext_add(be
, (void *)"", 1);
275 /* LNE field, strlen(filename) + zero termination, 3 bytes for: the dir entry, timestamp, filesize */
276 emit_unsigned_LEB128(be
, strlen(filename
) + 5);
277 emit_opcode(be
, DW_LNE_define_file
);
278 emit_string(be
, filename
);
279 /* directory index 0=do not know */
280 emit_unsigned_LEB128(be
, 0);
281 /* last modification date on file 0=do not know */
282 emit_unsigned_LEB128(be
, 0);
283 /* filesize 0=do not know */
284 emit_unsigned_LEB128(be
, 0);
287 static void emit_lne_set_address(struct buffer_ext
*be
,
290 emit_extended_opcode(be
, DW_LNE_set_address
, &address
, sizeof(unsigned long));
293 static ubyte
get_special_opcode(struct debug_entry
*ent
,
294 unsigned int last_line
,
295 unsigned long last_vma
)
298 unsigned long delta_addr
;
301 * delta from line_base
303 temp
= (ent
->lineno
- last_line
) - default_debug_line_header
.line_base
;
305 if (temp
>= default_debug_line_header
.line_range
)
311 delta_addr
= (ent
->addr
- last_vma
) / default_debug_line_header
.minimum_instruction_length
;
313 /* This is not sufficient to ensure opcode will be in [0-256] but
314 * sufficient to ensure when summing with the delta lineno we will
315 * not overflow the unsigned long opcode */
317 if (delta_addr
<= 256 / default_debug_line_header
.line_range
) {
318 unsigned long opcode
= temp
+
319 (delta_addr
* default_debug_line_header
.line_range
) +
320 default_debug_line_header
.opcode_base
;
322 return opcode
<= 255 ? opcode
: 0;
327 static void emit_lineno_info(struct buffer_ext
*be
,
328 struct debug_entry
*ent
, size_t nr_entry
,
329 unsigned long code_addr
)
334 * Machine state at start of a statement program
339 * is_stmt = default_is_stmt as given in the debug_line_header
344 /* start state of the state machine we take care of */
345 unsigned long last_vma
= 0;
346 char const *cur_filename
= NULL
;
347 unsigned long cur_file_idx
= 0;
350 emit_lne_set_address(be
, (void *)code_addr
);
352 for (i
= 0; i
< nr_entry
; i
++, ent
= debug_entry_next(ent
)) {
354 ubyte special_opcode
;
357 * check if filename changed, if so add it
359 if (!cur_filename
|| strcmp(cur_filename
, ent
->name
)) {
360 emit_lne_define_filename(be
, ent
->name
);
361 cur_filename
= ent
->name
;
362 emit_set_file(be
, ++cur_file_idx
);
366 special_opcode
= get_special_opcode(ent
, last_line
, last_vma
);
367 if (special_opcode
!= 0) {
368 last_line
= ent
->lineno
;
369 last_vma
= ent
->addr
;
370 emit_opcode(be
, special_opcode
);
373 * lines differ, emit line delta
375 if (last_line
!= ent
->lineno
) {
376 emit_advance_lineno(be
, ent
->lineno
- last_line
);
377 last_line
= ent
->lineno
;
381 * addresses differ, emit address delta
383 if (last_vma
!= ent
->addr
) {
384 emit_advance_pc(be
, ent
->addr
- last_vma
);
385 last_vma
= ent
->addr
;
389 * add new row to matrix
392 emit_opcode(be
, DW_LNS_copy
);
397 static void add_debug_line(struct buffer_ext
*be
,
398 struct debug_entry
*ent
, size_t nr_entry
,
399 unsigned long code_addr
)
401 struct debug_line_header
* dbg_header
;
404 old_size
= buffer_ext_size(be
);
406 buffer_ext_add(be
, (void *)&default_debug_line_header
,
407 sizeof(default_debug_line_header
));
409 buffer_ext_add(be
, &standard_opcode_length
, sizeof(standard_opcode_length
));
411 // empty directory entry
412 buffer_ext_add(be
, (void *)"", 1);
414 // empty filename directory
415 buffer_ext_add(be
, (void *)"", 1);
417 dbg_header
= buffer_ext_addr(be
) + old_size
;
418 dbg_header
->prolog_length
= (buffer_ext_size(be
) - old_size
) -
419 offsetof(struct debug_line_header
, minimum_instruction_length
);
421 emit_lineno_info(be
, ent
, nr_entry
, code_addr
);
423 emit_lne_end_of_sequence(be
);
425 dbg_header
= buffer_ext_addr(be
) + old_size
;
426 dbg_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
427 offsetof(struct debug_line_header
, version
);
431 add_debug_abbrev(struct buffer_ext
*be
)
433 emit_unsigned_LEB128(be
, 1);
434 emit_unsigned_LEB128(be
, DW_TAG_compile_unit
);
435 emit_unsigned_LEB128(be
, DW_CHILDREN_yes
);
436 emit_unsigned_LEB128(be
, DW_AT_stmt_list
);
437 emit_unsigned_LEB128(be
, DW_FORM_data4
);
438 emit_unsigned_LEB128(be
, 0);
439 emit_unsigned_LEB128(be
, 0);
440 emit_unsigned_LEB128(be
, 0);
444 add_compilation_unit(struct buffer_ext
*be
,
445 size_t offset_debug_line
)
447 struct compilation_unit_header
*comp_unit_header
;
448 size_t old_size
= buffer_ext_size(be
);
450 buffer_ext_add(be
, &default_comp_unit_header
,
451 sizeof(default_comp_unit_header
));
453 emit_unsigned_LEB128(be
, 1);
454 emit_uword(be
, offset_debug_line
);
456 comp_unit_header
= buffer_ext_addr(be
) + old_size
;
457 comp_unit_header
->total_length
= (buffer_ext_size(be
) - old_size
) -
458 offsetof(struct compilation_unit_header
, version
);
462 jit_process_debug_info(uint64_t code_addr
,
463 void *debug
, int nr_debug_entries
,
464 struct buffer_ext
*dl
,
465 struct buffer_ext
*da
,
466 struct buffer_ext
*di
)
468 struct debug_entry
*ent
= debug
;
471 for (i
= 0; i
< nr_debug_entries
; i
++) {
472 ent
->addr
= ent
->addr
- code_addr
;
473 ent
= debug_entry_next(ent
);
475 add_compilation_unit(di
, buffer_ext_size(dl
));
476 add_debug_line(dl
, debug
, nr_debug_entries
, GEN_ELF_TEXT_OFFSET
);
477 add_debug_abbrev(da
);
478 if (0) buffer_ext_dump(da
, "abbrev");
484 jit_add_debug_info(Elf
*e
, uint64_t code_addr
, void *debug
, int nr_debug_entries
)
489 struct buffer_ext dl
, di
, da
;
492 buffer_ext_init(&dl
);
493 buffer_ext_init(&di
);
494 buffer_ext_init(&da
);
496 ret
= jit_process_debug_info(code_addr
, debug
, nr_debug_entries
, &dl
, &da
, &di
);
500 * setup .debug_line section
504 warnx("cannot create section");
508 d
= elf_newdata(scn
);
510 warnx("cannot get new data");
516 d
->d_buf
= buffer_ext_addr(&dl
);
517 d
->d_type
= ELF_T_BYTE
;
518 d
->d_size
= buffer_ext_size(&dl
);
519 d
->d_version
= EV_CURRENT
;
521 shdr
= elf_getshdr(scn
);
523 warnx("cannot get section header");
527 shdr
->sh_name
= 52; /* .debug_line */
528 shdr
->sh_type
= SHT_PROGBITS
;
529 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
531 shdr
->sh_entsize
= 0;
534 * setup .debug_info section
538 warnx("cannot create section");
542 d
= elf_newdata(scn
);
544 warnx("cannot get new data");
550 d
->d_buf
= buffer_ext_addr(&di
);
551 d
->d_type
= ELF_T_BYTE
;
552 d
->d_size
= buffer_ext_size(&di
);
553 d
->d_version
= EV_CURRENT
;
555 shdr
= elf_getshdr(scn
);
557 warnx("cannot get section header");
561 shdr
->sh_name
= 64; /* .debug_info */
562 shdr
->sh_type
= SHT_PROGBITS
;
563 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
565 shdr
->sh_entsize
= 0;
568 * setup .debug_abbrev section
572 warnx("cannot create section");
576 d
= elf_newdata(scn
);
578 warnx("cannot get new data");
584 d
->d_buf
= buffer_ext_addr(&da
);
585 d
->d_type
= ELF_T_BYTE
;
586 d
->d_size
= buffer_ext_size(&da
);
587 d
->d_version
= EV_CURRENT
;
589 shdr
= elf_getshdr(scn
);
591 warnx("cannot get section header");
595 shdr
->sh_name
= 76; /* .debug_info */
596 shdr
->sh_type
= SHT_PROGBITS
;
597 shdr
->sh_addr
= 0; /* must be zero or == sh_offset -> dynamic object */
599 shdr
->sh_entsize
= 0;
602 * now we update the ELF image with all the sections
604 if (elf_update(e
, ELF_C_WRITE
) < 0) {
605 warnx("elf_update debug failed");