From: Omar Sandoval <osan...@fb.com> Hello,
I recently encountered a bug that dwfl_addrmodule doesn't work correctly for Linux kernel modules. This is because each section of a kernel module is allocated independently, so sections from different kernel modules may be intermixed. For example: # cd /sys/modules # cat ext4/sections/.init.text 0xffffffffc0f0f000 # cat ext4/sections/.bss 0xffffffffc1303e80 # cat kvm/sections/.init.text 0xffffffffc0f06000 # cat kvm/sections/.bss 0xffffffffc10d2340 This confuses dwfl_addrmodule/dwfl_addrsegment, which builds a lookup table based on mod->low_addr and mod->high_addr. For relocatable files, we should be using the addresses of each section, instead. Patch 4 makes this change, but it needs some preparation. Patch 1 allows us to distinguish between unloaded sections and sections loaded at zero. This is necessary so that dwfl_addrmodule doesn't map, e.g., 0x123 to a module with an unloaded section of size 0x200. Because indexing every section creates many more lookup entries than we previously had, patch 3 separates the module lookup table from the dwfl_report_segment lookup table. Finally, patch 2 is the patch I sent yesterday, included in this series because it would conflict with the later patches. If these patches are the wrong way to go about this, please consider this a bug report. I'd be happy to test alternative fixes. Thanks! Omar Sandoval (4): libdwfl: return error from __libdwfl_relocate_value for unloaded sections libdwfl: remove broken coalescing logic in dwfl_report_segment libdwfl: store module lookup table separately from segments libdwfl: use sections of relocatable files for dwfl_addrmodule .gitignore | 1 + libdwfl/ChangeLog | 30 +++++ libdwfl/derelocate.c | 24 +--- libdwfl/dwfl_addrmodule.c | 106 ++++++++++++++- libdwfl/dwfl_getmodules.c | 14 +- libdwfl/dwfl_module.c | 11 +- libdwfl/dwfl_module_getsym.c | 3 +- libdwfl/libdwfl.h | 20 +-- libdwfl/libdwflP.h | 42 ++++-- libdwfl/link_map.c | 7 +- libdwfl/relocate.c | 9 +- libdwfl/segment.c | 178 +++---------------------- tests/ChangeLog | 5 + tests/Makefile.am | 6 +- tests/dwfl-report-segment-contiguous.c | 82 ++++++++++++ 15 files changed, 305 insertions(+), 233 deletions(-) create mode 100644 tests/dwfl-report-segment-contiguous.c -- 2.24.0