[PATCH 03/34] perf jit: Add address ranges to DWARF compilation units
From: James Clark
Date: Wed Nov 18 2026 - 11:30:01 EST
The JIT ELF writer emits a line table and a compilation unit containing
only DW_AT_stmt_list. LLVM locates the CU by address before looking up a
source line, but the CU address range isn't written into the DWARF.
Therefore it can't resolve these files even though GNU addr2line could
by reading the line table directly. This means that JIT src line lookups
when LLVM is used.
Pass the generated code size to the debug information writer and emit
DW_AT_low_pc and DW_AT_high_pc address ranges. Also mark the compilation
unit as childless to match its contents.
Add a test which writes JIT debug information at a nonzero runtime load
address and resolves an ELF-relative address through LLVM.
Fixes: c3f8644c21df ("perf report: Support LLVM for addr2line()")
Signed-off-by: James Clark <james.clark@xxxxxxxxxx>
---
tools/perf/tests/genelf.c | 70 +++++++++++++++++++++++++++++++++++++++++-
tools/perf/util/genelf.c | 2 +-
tools/perf/util/genelf.h | 3 +-
tools/perf/util/genelf_debug.c | 22 +++++++++----
4 files changed, 88 insertions(+), 9 deletions(-)
diff --git a/tools/perf/tests/genelf.c b/tools/perf/tests/genelf.c
index 95f3be1b683a..4d23778c1979 100644
--- a/tools/perf/tests/genelf.c
+++ b/tools/perf/tests/genelf.c
@@ -5,6 +5,8 @@
#include <string.h>
#include <unistd.h>
#include <linux/compiler.h>
+#include <linux/kernel.h>
+#include <linux/unaligned.h>
#include "debug.h"
#include "tests.h"
@@ -12,6 +14,8 @@
#ifdef HAVE_JITDUMP
#include <libelf.h>
#include "../util/genelf.h"
+#include "../util/jitdump.h"
+#include "../util/llvm.h"
#endif
#define TEMPL "/tmp/perf-test-XXXXXX"
@@ -50,4 +54,68 @@ static int test__jit_write_elf(struct test_suite *test __maybe_unused,
#endif
}
-DEFINE_SUITE("Test jit_write_elf", jit_write_elf);
+static int test__jit_write_elf_dwarf(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+#if defined(HAVE_JITDUMP) && defined(HAVE_LIBDW_SUPPORT) && defined(HAVE_LIBLLVM_SUPPORT)
+ const uint64_t load_addr = 0x12340000;
+ static const char filename[] = "jit.c";
+ unsigned char code[16] = { 0 };
+ unsigned char debug[2 * (sizeof(struct debug_entry) + sizeof(filename))];
+ struct debug_entry *entry = (struct debug_entry *)debug;
+ char path[] = TEMPL;
+ char *file = NULL;
+ unsigned int line = 0;
+ int fd, ret = TEST_FAIL;
+
+ /* The second row terminates the first row's source-line range. */
+ for (int i = 0; i < 2; i++) {
+ put_unaligned(load_addr + i * 8, &entry->addr);
+ put_unaligned(42 + i, &entry->lineno);
+ put_unaligned(0, &entry->discrim);
+ memcpy(entry + 1, filename, sizeof(filename));
+ entry = debug_entry_next(entry);
+ }
+
+ fd = mkstemp(path);
+ if (fd < 0) {
+ perror("mkstemp failed");
+ return TEST_FAIL;
+ }
+
+ if (jit_write_elf(fd, load_addr, "main", code, sizeof(code),
+ debug, 2, NULL, 0, 0))
+ goto out;
+
+ /* LLVM must find the CU from the ELF address before reading its lines. */
+ if (llvm__addr2line(path, GEN_ELF_TEXT_OFFSET + 4, &file, &line,
+ NULL, false, NULL, NULL) <= 0) {
+ pr_debug("LLVM could not resolve the JIT source line\n");
+ goto out;
+ }
+ if (!file || strcmp(file, filename) || line != 42) {
+ pr_debug("Unexpected JIT source line: %s:%u\n", file ?: "??", line);
+ goto out;
+ }
+ ret = TEST_OK;
+out:
+ free(file);
+ close(fd);
+ unlink(path);
+ return ret;
+#else
+ return TEST_SKIP;
+#endif
+}
+
+static struct test_case tests__jit_write_elf[] = {
+ TEST_CASE("Write JIT ELF", jit_write_elf),
+ TEST_CASE_REASON("Resolve JIT ELF source line with LLVM", jit_write_elf_dwarf,
+ "requires JIT dump, libdw and LLVM support"),
+ { .name = NULL, }
+};
+
+struct test_suite suite__jit_write_elf = {
+ .desc = "Test jit_write_elf",
+ .test_cases = tests__jit_write_elf,
+};
diff --git a/tools/perf/util/genelf.c b/tools/perf/util/genelf.c
index 14882def9704..fc145f9401d2 100644
--- a/tools/perf/util/genelf.c
+++ b/tools/perf/util/genelf.c
@@ -449,7 +449,7 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
#ifdef HAVE_LIBDW_SUPPORT
if (debug && nr_debug_entries) {
- retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
+ retval = jit_add_debug_info(e, load_addr, csize, debug, nr_debug_entries);
if (retval)
goto error;
} else
diff --git a/tools/perf/util/genelf.h b/tools/perf/util/genelf.h
index 9f0b875d6548..67309794239a 100644
--- a/tools/perf/util/genelf.h
+++ b/tools/perf/util/genelf.h
@@ -10,7 +10,8 @@ int jit_write_elf(int fd, uint64_t code_addr, const char *sym,
void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size);
#ifdef HAVE_LIBDW_SUPPORT
/* genelf_debug.c */
-int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries);
+int jit_add_debug_info(Elf *e, uint64_t code_addr, int code_size,
+ void *debug, int nr_debug_entries);
#endif
#if defined(__arm__)
diff --git a/tools/perf/util/genelf_debug.c b/tools/perf/util/genelf_debug.c
index 7d9ef43aa6ac..a124ab40f36b 100644
--- a/tools/perf/util/genelf_debug.c
+++ b/tools/perf/util/genelf_debug.c
@@ -449,9 +449,13 @@ add_debug_abbrev(struct buffer_ext *be)
{
emit_unsigned_LEB128(be, 1);
emit_unsigned_LEB128(be, DW_TAG_compile_unit);
- emit_unsigned_LEB128(be, DW_CHILDREN_yes);
+ emit_unsigned_LEB128(be, DW_CHILDREN_no);
emit_unsigned_LEB128(be, DW_AT_stmt_list);
emit_unsigned_LEB128(be, DW_FORM_data4);
+ emit_unsigned_LEB128(be, DW_AT_low_pc);
+ emit_unsigned_LEB128(be, DW_FORM_addr);
+ emit_unsigned_LEB128(be, DW_AT_high_pc);
+ emit_unsigned_LEB128(be, DW_FORM_addr);
emit_unsigned_LEB128(be, 0);
emit_unsigned_LEB128(be, 0);
emit_unsigned_LEB128(be, 0);
@@ -459,9 +463,11 @@ add_debug_abbrev(struct buffer_ext *be)
static void
add_compilation_unit(struct buffer_ext *be,
- size_t offset_debug_line)
+ size_t offset_debug_line, int code_size)
{
struct compilation_unit_header *comp_unit_header;
+ uintptr_t low_pc = GEN_ELF_TEXT_OFFSET;
+ uintptr_t high_pc = low_pc + code_size;
size_t old_size = buffer_ext_size(be);
buffer_ext_add(be, &default_comp_unit_header,
@@ -469,6 +475,9 @@ add_compilation_unit(struct buffer_ext *be,
emit_unsigned_LEB128(be, 1);
emit_uword(be, offset_debug_line);
+ /* DW_FORM_addr uses the address size declared in the CU header. */
+ buffer_ext_add(be, &low_pc, sizeof(low_pc));
+ buffer_ext_add(be, &high_pc, sizeof(high_pc));
comp_unit_header = buffer_ext_addr(be) + old_size;
comp_unit_header->total_length = (buffer_ext_size(be) - old_size) -
@@ -476,7 +485,7 @@ add_compilation_unit(struct buffer_ext *be,
}
static int
-jit_process_debug_info(uint64_t code_addr,
+jit_process_debug_info(uint64_t code_addr, int code_size,
void *debug, int nr_debug_entries,
struct buffer_ext *dl,
struct buffer_ext *da,
@@ -489,7 +498,7 @@ jit_process_debug_info(uint64_t code_addr,
put_unaligned(get_unaligned(&ent->addr) - code_addr, &ent->addr);
ent = debug_entry_next(ent);
}
- add_compilation_unit(di, buffer_ext_size(dl));
+ add_compilation_unit(di, buffer_ext_size(dl), code_size);
add_debug_line(dl, debug, nr_debug_entries, GEN_ELF_TEXT_OFFSET);
add_debug_abbrev(da);
if (0) buffer_ext_dump(da, "abbrev");
@@ -498,7 +507,8 @@ jit_process_debug_info(uint64_t code_addr,
}
int
-jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries)
+jit_add_debug_info(Elf *e, uint64_t code_addr, int code_size,
+ void *debug, int nr_debug_entries)
{
Elf_Data *d;
Elf_Scn *scn;
@@ -510,7 +520,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
buffer_ext_init(&di);
buffer_ext_init(&da);
- if (jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di))
+ if (jit_process_debug_info(code_addr, code_size, debug, nr_debug_entries, &dl, &da, &di))
goto out;
/*
--
2.43.0