Skip to content

Conversation

@ningaraj44
Copy link

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:

@__cli_test4_c = constant [8 x i8] c"test4.c\00"

If compiling test4.o, the following global will be emitted in the object file is :

0000000000000000 R __cli_test4_c

If compiling a.out, the following global will be emitted in the executable file is :

25: 0000000000000204     8 OBJECT  GLOBAL DEFAULT   16 __cli_test4_c

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:

@__cli_test4_c = constant [8 x i8] c"test4.c\00"

Compile to object file and inspect symbol:

./build/bin/clang -c test4.c -o test4.o
nm test4.o | grep __cli_

Output:

0000000000000000 R __cli_test4_c

Link to executable and inspect symbol:

./build/bin/clang test4.o -o a.out
nm a.out | grep __cli_

Output:

0000000000000204 R __cli_test4_c

Inspect final binary with readelf:

readelf -s a.out | grep __cli_

Output:

25: 0000000000000204     8 OBJECT  GLOBAL DEFAULT   16 __cli_test4_c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant