cpp_build: disable LTO to preserve metadata in .data section#116
Open
okhsunrog wants to merge 1 commit intomystor:masterfrom
Open
cpp_build: disable LTO to preserve metadata in .data section#116okhsunrog wants to merge 1 commit intomystor:masterfrom
okhsunrog wants to merge 1 commit intomystor:masterfrom
Conversation
The cpp crate communicates type metadata (sizes, alignments, trait flags) from the build script to the proc-macro by embedding a struct with a magic byte pattern in the .data section of the compiled static library. The proc-macro then scans the .a file for this pattern using aho-corasick. When LTO is enabled via environment CXXFLAGS (e.g. -flto=auto, as is the default on Arch Linux's makepkg), the C++ compiler emits GIMPLE intermediate representation instead of native machine code. This leaves the .data and .text sections empty — all content lives in .gnu.lto_* sections as serialized IR. The magic bytes are no longer visible to the byte-level scan, causing the proc-macro to panic with: "Struct metadata not present in target library file." Fix this by passing -fno-lto when compiling the generated C++ library. This ensures the metadata struct is always present as raw bytes in .data, regardless of the environment's LTO settings. Fixes builds on distributions that enable LTO by default (Arch Linux, Fedora, Gentoo with LTO profiles).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem
The
cppcrate communicates type metadata (sizes, alignments, trait flags) fromcpp_buildtocpp_macrosby embedding a struct with a magic byte pattern in the.datasection of the compiled static library. The proc-macro scans the.afile for this pattern usingaho_corasick.When LTO is enabled via environment
CXXFLAGS(e.g.-flto=auto), the C++ compiler emits GIMPLE intermediate representation instead of native machine code. The.datasection is empty — all content lives in.gnu.lto_*sections. The magic bytes become invisible to the byte-level scan, causing the proc-macro to panic:This affects distributions that enable LTO by default in their build systems:
makepkgsets-flto=autoviaCXXFLAGS)Currently broken downstream packages include LibrePCB (via
qttypes/Slint) and anything else using thecppcrate.Fix
Pass
-fno-ltowhen compiling the generated C++ library, ensuring the metadata struct is always present as raw bytes in.dataregardless of environment LTO settings.Test plan
-fno-ltoonly affects the small generated metadata library, not the final binary linkage