Skip to contentSkip to conversation

dwarf_loader: Fix --lang_exclude on merged CUs

PATCH In review Alan Derk 2 participants 6 messages Oct 10 – Nov 14

Subsystems (2)
lore
Share & tools
lore

Alan Derk

author
#0 lore raw

v2 cover dwarf_loader: Fix --lang_exclude on merged CUs

Two fixes for --lang_exclude when CUs are merged, as for LTO objects.

Patch 1 takes the merged CU's language from a CU that is kept, so that excluding the language of the first CU no longer drops the whole object. With LTO, this is what makes BTF generation fail for kernel modules that mix Rust and C.

Patch 2 fixes a second bug from the same commit, which I found while testing userspace LTO objects after sending v1. With clang LTO, a type such as int is emitted once and shared through DW_FORM_ref_addr, so skipping the excluded CUs left references into them dangling: pahole crashed printing such members, and -J encoded them as void. Patch 2 loads the types that kept CUs use from the excluded CUs. Output without --lang_exclude does not change, and neither does the BTF of the LTO kernels I tested.

Patch 1 exposes this bug in one more link order. For a C++ object linked before a C one, pahole used to drop the C types without any error; with patch 1 alone it keeps them, and then crashes printing them, or encodes them as void with -J. Please apply the two together.

As far as I know, patch 1 removes the last thing that keeps the kernel's RUST option off when LTO and BTF are both on. Once these fixes are in a release, I'd like to propose relaxing that dependency to something like:

  depends on !DEBUG_INFO_BTF || (PAHOLE_HAS_LANG_EXCLUDE && (!LTO || PAHOLE_VERSION >= 133))

assuming they land in 1.33. The kernel tests in patch 1 ran with "&& !LTO" dropped from that line, hence the Cc to rust-for-linux.

Changes in v2:
- Add patch 2.
- Patch 1 is unchanged; its note about the Kconfig follow-up moved here.

v1: https://lore.kernel.org/all/[email protected]/

Alan Derk (2):
dwarf_loader: Take the merged CU language from a CU that is kept
dwarf_loader: Load types that kept CUs use from excluded CUs

 dwarf_loader.c                  | 129 +++++++++++++++++++++++++++-----
 tests/lang_exclude_lto_refs.sh  |  76 +++++++++++++++++++
 tests/lang_exclude_merged_cu.sh |  67 +++++++++++++++++
 3 files changed, 253 insertions(+), 19 deletions(-)
 create mode 100755 tests/lang_exclude_lto_refs.sh
 create mode 100755 tests/lang_exclude_merged_cu.sh

base-commit: 1e342ac44979748bcf5bb37f84ea055e7f4d7184

Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 1/2 dwarf_loader: Take the merged CU language from a CU that is kept

When the CUs of an object are merged, as for clang LTO objects, the merged CU takes the language of the first CU. Since commit b98565e7b17ec24d ("dwarf_loader: Honour --lang_exclude when merging LTO built CUs") the DIEs of excluded CUs are skipped while merging, but pahole_stealer() then applies the same filter to the merged CU. If the first CU is in an excluded language, the whole merged CU is dropped, including the CUs that were kept, and with -J pahole writes no BTF and still exits 0.

Kernel modules mixing Rust and C hit this in clang LTO builds with CONFIG_DEBUG_INFO_BTF_MODULES: kbuild passes --lang_exclude=rust, and the Rust CU comes first in the linked module even when the Rust object is listed last, as the C objects are bitcode compiled at link time. scripts/gen-btf.sh then fails, here for a Rust sample module:

FAILED: load BTF from samples/rust/rust_minimal.ko.BTF.1: No such file or directory

This is what is left of the problem behind kernel commit 5daa0c35a1f0 ("rust: Disallow BTF generation with Rust + LTO"), which keeps RUST off when LTO and BTF are both on. With pahole 1.32 a ThinLTO vmlinux with Rust built in gets clean BTF, but its Rust modules fail as above, so that restriction can't be relaxed yet. Distributions with LTO kernels, such as CachyOS's default kernel, keep BTF for sched_ext and BPF tools and go without Rust; out-of-tree modules that build their own Rust objects there hit this today.

Take the merged CU's language from the first CU the filter keeps, and its producer too, as producer_clang also steers how the merged DIEs are processed. Run the filter before checking for DIEs, as the unmerged path does, so that a first CU without DIEs, such as an assembler CU, still counts and the output without --lang_exclude does not change. If every CU is filtered out, the merged CU keeps the first CU's language and is dropped as before.

Before, with a C++ object linked before a C one:

  $ pahole -V --btf_features=force_cu_merging --lang_exclude=c++14 merged.o
  Filtering CU first.cpp written in c++14.
  Filtering CU  written in c++14.

After:

  $ pahole -V --btf_features=force_cu_merging --lang_exclude=c++14 merged.o
  Filtering CU first.cpp written in c++14.
  struct c_only {
  	int                        b;                    /*     0     4 */
  ...

Add tests/lang_exclude_merged_cu.sh for this.

Tested with clang 21 builds of 7.2.9 with RUST enabled: x86_64 with ThinLTO (DWARF 4 and 5), full LTO and distributed ThinLTO, and arm64 and riscv64 with ThinLTO. The Rust sample modules get BTF and load with MODULE_ALLOW_BTF_MISMATCH=n, and the BTF of vmlinux and of the C modules does not change.

Fixes b98565e7b17ec24d ("dwarf_loader: Honour --lang_exclude when merging LTO built CUs")
Assisted-by LLM

---
Part of the dwarf_loader.c diff is re-indentation; git show -w is easier to read.

 dwarf_loader.c                  | 34 +++++++++++------
 tests/lang_exclude_merged_cu.sh | 67 +++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 12 deletions(-)
 create mode 100755 tests/lang_exclude_merged_cu.sh
dwarf_loader.c +22 -12
index 01049e9..fbed36e 100644
@@ -5329,6 +5329,7 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 	struct dwarf_cu *dcu = NULL;
 	Dwarf_Off off = 0, noff;
 	struct cu *cu = NULL;
+	bool kept_cu = false;
 	size_t cuhl;
 
 	while (dwarf_nextcu(dw, off, &noff, &cuhl, NULL, &pointer_size,
@@ -5441,23 +5442,32 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 			cus__add(cus, cu);
 		}
 
-		Dwarf_Die child;
-		if (dwarf_child(cu_die, &child) == 0) {
-			bool filtered = false;
+		bool filtered = false;
 
-			if (conf->early_cu_filter) {
-				struct cu unmerged_cu = {
-					.name	  = attr_string(cu_die, DW_AT_name, conf),
-					.language = attr_numeric(cu_die, DW_AT_language),
-				};
+		if (conf->early_cu_filter) {
+			struct cu unmerged_cu = {
+				.name	  = attr_string(cu_die, DW_AT_name, conf),
+				.language = attr_numeric(cu_die, DW_AT_language),
+			};
 
-				filtered = conf->early_cu_filter(&unmerged_cu) == NULL;
-			}
+			filtered = conf->early_cu_filter(&unmerged_cu) == NULL;
+		}
 
-			if (!filtered && die__process_unit(&child, cu, conf, 0) != 0)
-				goto out_abort;
+		/*
+		 * pahole filters the merged CU by language again, so it must
+		 * not keep the language of a CU filtered out here.
+		 */
+		if (!filtered && !kept_cu) {
+			cu->language = attr_numeric(cu_die, DW_AT_language);
+			cu->producer_clang = attr_producer_clang(cu_die);
+			kept_cu = true;
 		}
 
+		Dwarf_Die child;
+		if (!filtered && dwarf_child(cu_die, &child) == 0 &&
+		    die__process_unit(&child, cu, conf, 0) != 0)
+			goto out_abort;
+
 		off = noff;
 	}
 
tests/lang_exclude_merged_cu.sh new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.55.0
author
#2 lore raw

PATCH v2 2/2 dwarf_loader: Load types that kept CUs use from excluded CUs

In reply to Alan Derk

With clang LTO, LLVM emits a type such as int once, in the first CU that uses it, and other CUs refer to it with DW_FORM_ref_addr. Since commit b98565e7b17ec24d ("dwarf_loader: Honour --lang_exclude when merging LTO built CUs") the DIEs of the CUs that --lang_exclude filters out are skipped when merging, so a kept CU that uses a type from one of them is left with a dangling reference. Members that use such a type end up typed void: pahole crashes printing them, and -J encodes them with type_id 0 and exits 0.

  $ head zero.c first.cpp second.c
  ==> zero.c <==
  char zero_c;

==> first.cpp <==
int cpp_int;

  ==> second.c <==
  struct c_only { int b; } c_var;
  $ clang -flto -g -c zero.c second.c; clang++ -flto -g -c first.cpp
  $ clang -flto -fuse-ld=lld -shared -o three.so zero.o first.o second.o
  $ pahole --lang_exclude=c++14 three.so
  namespace__recode_dwarf_types: couldn't find 0x57 type for 0x8a (member)!
  struct c_only {
  	<ERROR(__class__fprintf:1838): 0 not found!>
  Segmentation fault

Remember the offsets of the CUs that the filter skips, and when a type lookup misses an offset in one of them, load that DIE if it is a type. The types it refers to are looked up when it is recoded, so they are loaded too. As this adds types while recoding, cu__recode_dwarf_types() now repeats its passes until no new tags show up.

After:

  $ pahole --lang_exclude=c++14 three.so
  struct c_only {
  	int                        b;                    /*     0     4 */
  ...

Only types are loaded. References to functions and variables in excluded CUs, such as the abstract origin of a function inlined across CUs or the function that a C++ using declaration names, still print "couldn't find", as loading them would add functions in the excluded language to the output. A nested C++ type that a kept CU refers to directly is loaded as a top-level type.

Without --lang_exclude, or when no kept CU refers to an excluded one, nothing is loaded and the output does not change.

Add tests/lang_exclude_lto_refs.sh for this.

Tested with clang 21 full LTO and ThinLTO builds of a C++/C program in both link orders. Only base types (int, char, long) were loaded, and apart from the order of the types the output matches that of the other link order: text and BTF with C++ excluded, text with C excluded. Two C++ objects built with -std=c++11 and -std=c++17 that include <cstdio> give the same text with c++11 excluded in either order; there the C library types they share, such as FILE, are loaded too. ASan reports no errors on these apart from a leak with -J that predates this series. gcc 16 LTO, Rust+C ThinLTO and Debian debuginfo processed by dwz load nothing and do not change, and neither does the BTF of 7.2.9 x86_64 and arm64 ThinLTO kernels with Rust enabled.

Fixes b98565e7b17ec24d ("dwarf_loader: Honour --lang_exclude when merging LTO built CUs")
Assisted-by LLM
---
 dwarf_loader.c                 | 95 +++++++++++++++++++++++++++++++---
 tests/lang_exclude_lto_refs.sh | 76 +++++++++++++++++++++++++++
 2 files changed, 164 insertions(+), 7 deletions(-)
 create mode 100755 tests/lang_exclude_lto_refs.sh
dwarf_loader.c +88 -7
index fbed36e..10ab37a 100644
@@ -169,6 +169,11 @@ struct alt_pu {
 	bool	  hit;		/* referenced by main CUs? */
 };
 
+struct excluded_cu {
+	Dwarf_Off start;
+	Dwarf_Off end;
+};
+
 struct dwarf_cu {
 	struct hlist_head *hash_tags;	/* tags (functions, etc.) keyed by DWARF offset */
 	struct hlist_head *hash_types;	/* types keyed by DWARF offset */
@@ -183,6 +188,10 @@ struct dwarf_cu {
 	uint32_t  nr_alt_pus;
 	uint32_t  allocated_alt_pus;
 	bool	  processing_alt;	/* true while processing alt-file DIEs */
+	struct excluded_cu *excluded_cus;	/* skipped by early_cu_filter when merging */
+	uint32_t  nr_excluded_cus;
+	Dwarf	  *dw;
+	struct conf_load *conf;
 };
 
 static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu)
@@ -215,6 +224,8 @@ static int dwarf_cu__init(struct dwarf_cu *dcu, struct cu *cu)
 	dcu->alt_pus = NULL;
 	dcu->nr_alt_pus = 0;
 	dcu->allocated_alt_pus = 0;
+	dcu->excluded_cus = NULL;
+	dcu->nr_excluded_cus = 0;
 	/*
 	 * processing_alt is per-merged-CU state, not thread-safe.
 	 * The merged-CU path (cus__merge_and_process_cu) is
@@ -249,6 +260,7 @@ static void dwarf_cu__delete(struct cu *cu)
 
 	zfree(&dcu->imported_units);
 	zfree(&dcu->alt_pus);
+	zfree(&dcu->excluded_cus);
 	if (dcu->alt) {
 		cu__free(cu, dcu->alt->hash_tags);
 		cu__free(cu, dcu->alt->hash_types);
@@ -475,6 +487,8 @@ static struct dwarf_tag *__dwarf_cu__find_tag_by_ref(const struct dwarf_cu *cu,
 				    dtag->from_types_section.field, \
 				    dtag->from_alt.field)
 
+static struct dwarf_tag *dwarf_cu__load_excluded_type(struct dwarf_cu *dcu, Dwarf_Off ref);
+
 /* Same as __dwarf_cu__find_tag_by_ref but for type lookups (hash_types). */
 static struct dwarf_tag *__dwarf_cu__find_type_by_ref(struct dwarf_cu *dcu,
 						      const Dwarf_Off ref,
@@ -497,6 +511,8 @@ static struct dwarf_tag *__dwarf_cu__find_type_by_ref(struct dwarf_cu *dcu,
 
 	struct dwarf_tag *dtag = hashtags__find(dcu->hash_types, ref);
 
+	if (dtag == NULL)
+		dtag = dwarf_cu__load_excluded_type(dcu, ref);
 	if (dtag)
 		dcu->last_type_lookup = dtag;
 
@@ -3693,6 +3709,62 @@ static int die__process_unit(Dwarf_Die *die, struct cu *cu, struct conf_load *co
 	return 0;
 }
 
+static int dwarf_cu__add_excluded_cu(struct dwarf_cu *dcu, Dwarf_Off start, Dwarf_Off end)
+{
+	struct excluded_cu *cus = realloc(dcu->excluded_cus,
+					  (dcu->nr_excluded_cus + 1) * sizeof(*cus));
+
+	if (cus == NULL)
+		return -ENOMEM;
+
+	cus[dcu->nr_excluded_cus++] = (struct excluded_cu){ .start = start, .end = end };
+	dcu->excluded_cus = cus;
+	return 0;
+}
+
+static bool dwarf_cu__in_excluded_cu(const struct dwarf_cu *dcu, Dwarf_Off offset)
+{
+	for (uint32_t i = 0; i < dcu->nr_excluded_cus; i++)
+		if (offset >= dcu->excluded_cus[i].start && offset < dcu->excluded_cus[i].end)
+			return true;
+	return false;
+}
+
+/*
+ * With LTO, LLVM emits a type such as int once, in the first CU that uses it,
+ * and other CUs refer to it with DW_FORM_ref_addr. So the CUs kept when merging
+ * can refer to types in CUs that early_cu_filter excluded. Load those types
+ * when they are looked up, and nothing else from the excluded CUs.
+ */
+static struct dwarf_tag *dwarf_cu__load_excluded_type(struct dwarf_cu *dcu, Dwarf_Off ref)
+{
+	struct tag *tag, die_tag = {};
+	uint32_t id = 0;
+	Dwarf_Die die;
+
+	if (!dwarf_cu__in_excluded_cu(dcu, ref) || dwarf_offdie(dcu->dw, ref, &die) == NULL)
+		return NULL;
+
+	/*
+	 * A using declaration can name a function, and processing a function
+	 * adds its parameters to the CU's tables, so check the tag first.
+	 */
+	die_tag.tag = dwarf_tag(&die);
+	if (!tag__is_tag_type(&die_tag))
+		return NULL;
+
+	tag = die__process_tag(&die, dcu->cu, 1, dcu->conf);
+	if (tag == NULL || tag == &unsupported_tag)
+		return NULL;
+
+	/* As in die__process_unit(). */
+	if (tag->tag != DW_TAG_unspecified_type)
+		cu__add_tag(dcu->cu, tag, &id);
+	cu__hash(dcu->cu, tag);
+	tag__dwarf(tag)->small_id = id;
+	return tag__dwarf(tag);
+}
+
 static void ftype__recode_dwarf_types(struct tag *tag, struct cu *cu);
 
 static int namespace__recode_dwarf_types(struct tag *tag, struct cu *cu)
@@ -4497,10 +4569,10 @@ static int cu__resolve_func_ret_types_optimized(struct cu *cu, struct conf_load
 
 static int cu__recode_dwarf_types_table(struct cu *cu,
 					struct ptr_table *pt,
-					uint32_t i)
+					uint32_t *i)
 {
-	for (; i < pt->nr_entries; ++i) {
-		struct tag *tag = pt->entries[i];
+	for (; *i < pt->nr_entries; ++*i) {
+		struct tag *tag = pt->entries[*i];
 
 		if (tag != NULL) /* void, see cu__new */
 			if (tag__recode_dwarf_type(tag, cu))
@@ -4512,10 +4584,15 @@ static int cu__recode_dwarf_types_table(struct cu *cu,
 
 static int cu__recode_dwarf_types(struct cu *cu)
 {
-	if (cu__recode_dwarf_types_table(cu, &cu->types_table, 1) ||
-	    cu__recode_dwarf_types_table(cu, &cu->tags_table, 0) ||
-	    cu__recode_dwarf_types_table(cu, &cu->functions_table, 0))
-		return -1;
+	uint32_t types = 1, tags = 0, functions = 0;
+
+	/* Recoding can add tags, see dwarf_cu__load_excluded_type(). */
+	do {
+		if (cu__recode_dwarf_types_table(cu, &cu->types_table, &types) ||
+		    cu__recode_dwarf_types_table(cu, &cu->tags_table, &tags) ||
+		    cu__recode_dwarf_types_table(cu, &cu->functions_table, &functions))
+			return -1;
+	} while (types < cu->types_table.nr_entries || tags < cu->tags_table.nr_entries);
 	return 0;
 }
 
@@ -5366,6 +5443,8 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 
 			dcu->cu = cu;
 			dcu->type_unit = type_dcu;
+			dcu->dw = dw;
+			dcu->conf = conf;
 			cu->priv = dcu;
 			cu->dfops = &dwarf__ops;
 
@@ -5451,6 +5530,8 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,
 			};
 
 			filtered = conf->early_cu_filter(&unmerged_cu) == NULL;
+			if (filtered && dwarf_cu__add_excluded_cu(dcu, off, noff))
+				goto out_abort;
 		}
 
 		/*
tests/lang_exclude_lto_refs.sh new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
2.55.0
#3 lore raw
In reply to Alan Derk
Quoted 45 lines

From: Alan Derk <[email protected]>
To: Arnaldo Carvalho de Melo <[email protected]>,
Alan Maguire <[email protected]>
Cc: [email protected],
[email protected],
[email protected]
Subject: [PATCH dwarves v2 0/2] dwarf_loader: Fix --lang_exclude on merged CUs
Date: Sat, 10 Oct 2026 00:52:25 +0000
Message-ID: <[email protected]>
X-Mailer: git-send-email 2.55.0
Precedence: bulk
X-Mailing-List: [email protected]
List-Id: <dwarves.vger.kernel.org>
List-Subscribe: <mailto:[email protected]>
List-Unsubscribe: <mailto:[email protected]>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

Two fixes for --lang_exclude when CUs are merged, as for LTO objects.

Patch 1 takes the merged CU's language from a CU that is kept, so that excluding the language of the first CU no longer drops the whole object. With LTO, this is what makes BTF generation fail for kernel modules that mix Rust and C.

Patch 2 fixes a second bug from the same commit, which I found while testing userspace LTO objects after sending v1. With clang LTO, a type such as int is emitted once and shared through DW_FORM_ref_addr, so skipping the excluded CUs left references into them dangling: pahole crashed printing such members, and -J encoded them as void. Patch 2 loads the types that kept CUs use from the excluded CUs. Output without --lang_exclude does not change, and neither does the BTF of the LTO kernels I tested.

Patch 1 exposes this bug in one more link order. For a C++ object linked before a C one, pahole used to drop the C types without any error; with patch 1 alone it keeps them, and then crashes printing them, or encodes them as void with -J. Please apply the two together.

As far as I know, patch 1 removes the last thing that keeps the kernel's RUST option off when LTO and BTF are both on. Once these fixes are in a release, I'd like to propose relaxing that dependency to something like:

  depends on !DEBUG_INFO_BTF || (PAHOLE_HAS_LANG_EXCLUDE && (!LTO || PAHOLE_VERSION >= 133))
Quoted 8 lines

assuming they land in 1.33. The kernel tests in patch 1 ran with "&& !LTO" dropped from that line, hence the Cc to rust-for-linux.

Changes in v2:
- Add patch 2.
- Patch 1 is unchanged; its note about the Kconfig follow-up moved here.

v1: https://lore.kernel.org/all/[email protected]/

Thanks for doing this work! Sorry for the delay in getting to it, I'm looking at the patches now.

In terms of enabling kernel builds to support adding BTF data for Rust, that's a goal we're working towards too, but it turns out there are some complications. We discussed a bit of this in Linux Plumbers in the BPF track [1], but the main issue boils down to how bindgen-generated structures differ somewhat from the C-based types; enums aren't always faithfully represented for example. This has knock-on effects for BTF generation and we often end up with duplicate types for key kernel data structures; the bindgen and C versions of "struct sock" for example. This in turn can confuse tooling which expects a canonical type for the critical kernel structures.

We've been working on a solution; essentially, the idea is to detect such cases and fix up the type graph in pahole using a new "rust_bindgen_replace" feature so that the final BTF does not have to carry such duplicates. It will only run for Rust CUs, and it is implemented in a work-in-progress series at [2]; the bindgen-related changes are in [3].

The other commits in the series add BTF tags to allow tracers to use demangled names in function tracing descriptors, and add testing etc.

[1] https://lpc.events/event/20/contributions/2408/
[2] https://github.com/alan-maguire/dwarves/tree/pahole-next-btf-rust
[3] https://github.com/acmel/dwarves/commit/68954b9e0d1b4a191a6d7e3329875c2acc983c8c

author
#4 lore raw
Quoted 2 lines · Alan Maguire

Thanks for doing this work! Sorry for the delay in getting to it, I'm looking at the patches now.

Of course. Please take your time on the review. And thanks for the background on the Rust BTF work.

Quoted 4 lines

We've been working on a solution; essentially, the idea is to detect such cases and fix up the type graph in pahole using a new "rust_bindgen_replace" feature so that the final BTF does not have to carry such duplicates. It will only run for Rust CUs, [...]

Patches 1-2 are general --lang_exclude fixes (both reproducers are C++/C). The Kconfig follow-up keeps --lang_exclude=rust, so it is a
stopgap: Rust stays out of BTF, as it does without LTO, until rust_bindgen_replace brings Rust types in. I realize it may not be worth it, though it would -- if I understand correctly -- unblock RUST with LTO and BTF in the meantime.

To check how this series of patches fits with your branch, I applied it on top of pahole-next-btf-rust (b5e46f8; it applies cleanly) and tried both on the 7.2.9 kernels I tested the series on (x86_64 ThinLTO, full LTO and distributed ThinLTO, arm64 ThinLTO) and a non-LTO x86_64 build, booting each in qemu and loading rust_minimal, rust_print and kobject-example:

- With kbuild's flags (--lang_exclude=rust), the branch alone fails to build the Rust sample modules on the LTO kernels, as patch 1 describes. With the series on top, all five kernels build, boot and load the three modules with BTF.
- With rust_bindgen_replace and nothing excluded, the series changes
nothing: vmlinux and module BTF are byte-identical with and without it.

One thing I noticed along the way, since it's the merged-CU language that patch 1 deals with: under LTO the merged CU has one language, taken from its first CU (a C or asm file for vmlinux), so cu__is_rust() is false and rust_bindgen_replace doesn't run on vmlinux. Without LTO it replaced 564 types as expected.

Cheers,
Alan

#5 lore raw
Quoted 7 lines · Alan Derk
Quoted 2 lines · Alan Maguire

Thanks for doing this work! Sorry for the delay in getting to it, I'm looking at the patches now.

Of course. Please take your time on the review. And thanks for the background on the Rust BTF work.

Changes are applied, thanks for fixing these issues! I'm thinking we should probably add a CI job that builds an LTO vmlinux; I'll look at doing that shortly.

Quoted 27 lines
Quoted 4 lines

We've been working on a solution; essentially, the idea is to detect such cases and fix up the type graph in pahole using a new "rust_bindgen_replace" feature so that the final BTF does not have to carry such duplicates. It will only run for Rust CUs, [...]

Patches 1-2 are general --lang_exclude fixes (both reproducers are C++/C). The Kconfig follow-up keeps --lang_exclude=rust, so it is a
stopgap: Rust stays out of BTF, as it does without LTO, until rust_bindgen_replace brings Rust types in. I realize it may not be worth it, though it would -- if I understand correctly -- unblock RUST with LTO and BTF in the meantime.

To check how this series of patches fits with your branch, I applied it on top of pahole-next-btf-rust (b5e46f8; it applies cleanly) and tried both on the 7.2.9 kernels I tested the series on (x86_64 ThinLTO, full LTO and distributed ThinLTO, arm64 ThinLTO) and a non-LTO x86_64 build, booting each in qemu and loading rust_minimal, rust_print and kobject-example:

- With kbuild's flags (--lang_exclude=rust), the branch alone fails to build the Rust sample modules on the LTO kernels, as patch 1 describes. With the series on top, all five kernels build, boot and load the three modules with BTF.
- With rust_bindgen_replace and nothing excluded, the series changes
nothing: vmlinux and module BTF are byte-identical with and without it.

Quoted 6 lines

One thing I noticed along the way, since it's the merged-CU language that patch 1 deals with: under LTO the merged CU has one language, taken from its first CU (a C or asm file for vmlinux), so cu__is_rust() is false and rust_bindgen_replace doesn't run on vmlinux. Without LTO it replaced 564 types as expected.

Ah, that's a great point. May have to rethink that part. Thanks again for giving the changes a try!

Alan