I've add some support of DWARF debug info from nvidia nvcc to my dwarfdump. As everyone knows dwarf is over-complicated, fat and just disgusting - however, nvidia was able to take his nausea to a new level
четверг, 26 марта 2026 г.
dwarf from nvcc
пятница, 1 ноября 2024 г.
perl module for DWARF debug info parsing
четверг, 20 июня 2024 г.
frame sizes in dwarfdump
Add today dumping of stack frame sizes to my dwarfdump (well, where they are exists). Format of .debug_frame section obviously was invented by martian misantrophes so patch is huge and ugly
Sample of output for some random function from mips kernel:
// Addr 0x183D27C .text
// Frame Size 18
// FileName: drivers/char/random.c
// LocalVars:
// LVar0, tag 6965A71
// int ret
// LVar1, tag 6965A8F
// bool branch
ssize_t random_read_iter(struct kiocb* kiocb,struct iov_iter* iter);
and the same in JSON format:
"110516780":{"type":"function","file":"drivers/char/random.c","type_id":"110427157","name":"random_read_iter","addr":"25416316","section":".text","frame_size":"24","params":[{"name":"kiocb","id":"110516807","type_id":"110466611"},{"name":"iter","id":"110516828","type_id":"110470510"}],"lvars":["110516849":{"type":"var","file":"drivers/char/random.c","owner":"110516780","type_id":"110426600","name":"ret"},
"110516879":{"type":"var","file":"drivers/char/random.c","owner":"110516780","type_id":"110427073","name":"branch"}]}
Unfortunately dwarfdump can't work with kernel modules bcs they are actually just object files and for this sad reason they have relocations even for debug sections. So to properly deal with this files I need to apply relocations first and this is arch-specific action (which I prefer to avoid)
Binutils has 2 solution of this problem:
- objcopy calls bfd_simple_get_relocated_section_contents from libbfd.so and this means that tool should have dependency from it
- readelf has it's own relocation code in apply_relocations and this is huge pile of code
And I really don’t like both of the above approaches
вторник, 18 июня 2024 г.
function stack size in GCC
DW_AT_frame_base block, right?NO
- flag_callgraph_info is set - corresponding to option -fcallgraph-info
- flag_stack_usage_info is set - corresponding to options -fstack-usage
There should probably be heartbreaking conclusion about quality of opensource in general and gcc and in particular...
суббота, 15 июня 2024 г.
stack frames size in DWARF
As you might suspect, the stack size in the kernel is quite meager so it's very important to know how much of stack occupy your driver. So I conducted inhumane experiments on my own driver lkcd to check if stack frame size can be extracted from DWARF debug info. Biggest function in my driver is lkcd_ioctl so lets explore it
mips
addiu sp,sp,-688понедельник, 11 сентября 2023 г.
location lists from dwarf5
locx 53e
4FB5DF - 4FB5F4: DW_OP_piece 0x8, DW_OP_reg0 RAX, DW_OP_piece 0x8, DW_OP_breg0 RAX+0, DW_OP_lit3, DW_OP_lit8, DW_OP_mul, DW_OP_plus, DW_OP_stack_value, DW_OP_piece 0x8
4FB5F4 - 4FB60F: DW_OP_piece 0x8, DW_OP_reg2 RCX, DW_OP_piece 0x8, DW_OP_breg0 RAX+0, DW_OP_lit3, DW_OP_lit8, DW_OP_mul, DW_OP_plus, DW_OP_stack_value, DW_OP_piece 0x8
DW_OP_mul just pops from stack couple of values and put back result of their multiplication (see evaluation logic in method execute_stack_op), so this sub-expression can be rewritten as just DW_OP_lit24
Also it`s curious to check what other compilers support subj:
gcc
golang
openwatcom v2
No - judging by the funny comments
WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE DESCRIBE IT HERE!
воскресенье, 27 августа 2023 г.
dwarf5 from clang 14
It seems that clang in version 14 utilize more advanced features from DWARF5, so I add their support to my dwarfdump. IMHO most exciting features are:
Section .debug_line_str
Section .debug_str_offsets
<0><c>: Abbrev Number: 1 (DW_TAG_compile_unit)
<d> DW_AT_producer : (indexed string: 0): clang version 14.0.6 (git@github.com:github/semmle-code 5c87e7737f331823ed8ed280883888566f08cdea)
<e> DW_AT_language : 33 (C++14)
<10> DW_AT_name : (indexed string: 0x1): c/extractor/src/extractor.cpp
<11> DW_AT_str_offsets_base: 0x8
As you can see here 2 tags have names before we have value of string base. Much harder to parse in one pass now
New locations format
Offset Entry 2077
This cryptic message means that starting from address 0x4fb3c5 (note - most tools like objdump or llvm-dwarfdump cannot correctly show this new locations, in this case objdump showed address in bad format) some local variable located in register rdi until next address range. Seems that both IDA Pro and Binary Ninja cannot use this debug information:
0024ef56 00000000000006b4 (index into .debug_addr) 004fb3c500000000 (base address)
0024ef59 0000000000000000 000000000000001c DW_OP_reg5 (rdi).text:00004FB3C5 mov rdi, cs:compilation_tf
Global var compilation_tf has type a_trap_file_ptr - pointer to a_trap_file. IDA Pro has that types information from debug info but anyway cannot show access to field of a_trap_file at offset 0xC for next instruction
.text:00004FB3CC cmp dword ptr [rdi+0Ch], 0
суббота, 15 апреля 2023 г.
custom attributes in gcc and dwarf
Lets check if we can add our own attributes (if Google can afford it, then why is it forbidden to mere mortals?). For example I want to have in gcc and dwarf flag about functions/methods parameters direction - is some param IN or OUT. I chose the value of this dwarf attribure 0x28ff
It`s pretty obviously that we can add our own custom attribute in gcc - they even have example how to do this. But what about dwarf producer? Long story short - seems that you cannot do it from plugin. The only dwarf related pass for plugins is pass_dwarf2_frame. So we need to patch gcc. But before this we need to
build gcc from sources
At moment of writing latest stable version of gcc was 12.0 so run
git clone --branch releases/gcc-12 https://github.com/gcc-mirror/gcc.git
patch gcc
Lets see how gcc produces dwarf output. All symbol table formatters implement gcc_debug_hooks and currently gcc has 3 (btw there are patches for mingw to produce PDB, so in theory you could have vmlinux.pdb):
- dwarf2out.cc - this is our target
- godump.c
- vmsdbgout.c
bool add_param_direction(tree decl, dw_die_ref parm_die)
{
bool pa1 = lookup_attribute ("param_in", DECL_ATTRIBUTES (decl));
bool pa2 = lookup_attribute ("param_out", DECL_ATTRIBUTES (decl));
if ( !(pa1 ^ pa2) )
return false;
unsigned char pa_value = 0;
// seems that you can`t have flag with value 1 - see gcc_assert at line 9599
if ( pa1 )
pa_value = 2;
if ( pa2 )
pa_value = 3;
add_AT_flag(parm_die, (dwarf_attribute)0x28ff, pa_value);
return true;
}tree handle_param_in_attribute (tree *node, tree name, tree ARG_UNUSED (args),
int ARG_UNUSED(flags), bool *no_add_attrs)
{
if ( !DECL_P (*node) )
{
warning (OPT_Wattributes, "%qE attribute can apply to params declarations only", name);
*no_add_attrs = true;
return NULL_TREE;
}
tree decl = *node;
if (TREE_CODE (decl) != PARM_DECL)
{
warning (OPT_Wattributes, "%qE attribute can apply to params only", name);
*no_add_attrs = true;
} else {
// check presense of param_out
if ( lookup_attribute ("param_out", DECL_ATTRIBUTES (decl)) )
{
warning (OPT_Wattributes, "%qE attribute useless when param_out was used", name);
*no_add_attrs = true;
DECL_ATTRIBUTES (decl) = remove_attribute("param_out", DECL_ATTRIBUTES (decl));
}
}
return NULL_TREE;
}
handle_param_in_attribute checks that this attribute linked with function/method parameter. Then it checks that the same parameter don`t have attribute param_out - in this case it just removes both results
понедельник, 10 апреля 2023 г.
custom dwarf attributes in golang
0x2900
0x2901
0x2902
- kindChan
- kindMap
- kindSlice
0x2903
0x2904
0x2905
0x2906
index of the dictionary entry describing the real type of this type shape
пятница, 31 марта 2023 г.
DWARF size overhead
I made today simple script to estimate size overhead due types duplication. This is hard task for C++ - bcs some types can have specialized (or partially specialized) template parameters and sure this types should be considered as different. But for plain C we can safely get all high-level types and assume that types with the same name and declared at the same line and column are equal
Next I ran this script on objdump -g dump for linux kernel. Script gave me digit 252741370
Lets find size of .debug_info section
objdump -h vmlinux | grep debug_info
Size is 0x118205ec = 293733868
35 .debug_info 118205ec 0000000000000000 0000000000000000 03037230 2**0
And finally lets calculate share of unnecessary info: 252741370 / 293733868 = 0,8604
I am shocked - 86%!!! Looks like hd manufacturers conspiracy
Update: for C++ I made another version of this script to support namespaces and got following results:
- cc1 from gcc 111858917 / 130272407 = 0,8587
- gdb 105034993 / 139899598 = 0,7508
- llvm-dwarfdump 194031570 / 263053224 = 0,7376
четверг, 30 марта 2023 г.
dwarfdump
I made pale analog of world famous pdbdump to dump types and functions from DWARF. Before introducing my tool I have several words about DWARF - it is excess, compiler-specific, inconsistent and dangerous
Redudancy
ls -l llvm-dwarfdump llvm-dwarfdump.stripped
-rwxrwxr-x 1 redp redp 471241104 mar 29 00:52 llvm-dwarfdump
-rwxrwxr-x 1 redp redp 22170696 mar 29 17:49 llvm-dwarfdump.stripped
grep console_printk vm.g | wc -l
2883
compiler-specific
<0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
<c> DW_AT_name : internal/cpu
<19> DW_AT_language : 22 (Go)
<1a> DW_AT_stmt_list : 0x0
<1e> DW_AT_low_pc : 0x401000
<26> DW_AT_ranges : 0x0
<2a> DW_AT_comp_dir : .
<2c> DW_AT_producer : Go cmd/compile go1.13.8
<44> Unknown AT value: 2905: cpu
Inconsistency
- order of tags, so you can have mix of formal parameters with types at the same nesting level
- which attributes are mandatory for tags - I saw lots of missed DW_AT_sibling for example
- when locations info should be placed in separate section .debug_loc - seems that this happens for inlined subroutines only
- encoding of addresses. You have DW_AT_low_pc for functions address. But also there is DW_AT_abstract_origin (and DW_AT_specification). The same function can have different addresses even in plain C via this attributes:
<1><191cde>: Abbrev Number: 194 (DW_TAG_subprogram)
<191ce0> DW_AT_external : 1
<191ce0> DW_AT_name : (indirect string, offset: 0x24d2f): perf_events_lapic_init
<191ce4> DW_AT_decl_file : 1
<191ce5> DW_AT_decl_line : 1719
<191ce7> DW_AT_decl_column : 6
<191ce8> DW_AT_prototyped : 1
<191ce8> DW_AT_inline : 1 (inlined)
<1><19a945>: Abbrev Number: 96 (DW_TAG_subprogram)
<19a946> DW_AT_abstract_origin: <0x191cde>
<19a94a> DW_AT_low_pc : 0xffffffff81004dc0
<1><19b3c7>: Abbrev Number: 96 (DW_TAG_subprogram)
<19b3c8> DW_AT_abstract_origin: <0x191cde>
<19b3cc> DW_AT_low_pc : 0xffffffff81007930
