Add compiler-generated global variable per source file [Issue #24] #118
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.
Description
This pull request addresses Issue #24 by adding a compiler-generated global variable for each source file compiled using LLVM.
A global variable of type
const char*is created during the compilation of each source file.The variable name is constructed by prefixing
__cli_to the source file name, where non-alphanumeric characters such as.and/are replaced with underscores.This ensures uniqueness of the variable name across all translation units in a multi-file project.
These variables act as metadata anchors, preserving the origin filename through all stages of the build: IR generation, object file creation, and final linking.
Example:
If compiling
test4.c, the following global will be emitted in the IR:If compiling
test4.o, the following global will be emitted in the object file is :0000000000000000 R __cli_test4_cIf compiling
a.out, the following global will be emitted in the executable file is :Commands to Reproduce the Global Variable Injection:
Generate LLVM IR from test4.c:
./build/bin/clang -emit-llvm -S test4.c -o test4.ll cat test4.ll | grep __cli_Output:
Compile to object file and inspect symbol:
Output:
0000000000000000 R __cli_test4_cLink to executable and inspect symbol:
Output:
0000000000000204 R __cli_test4_cInspect final binary with readelf:
Output: