kkocdko
kkocdko

Reputation: 416

How to redefine symbol in the object file created in clang llvm LTO? It seems be "LLVM IR bitcode" file

There's an old project which needs to redefine symbol in build progress. I want to enable -flto, but it seems unable to redefine symbol now.

echo "int foo(){return 0;}" > foo.c

clang -c foo.c
llvm-objcopy --redefine-sym foo=bar foo.o
file foo.o
#> foo.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

clang -flto -c foo.c
llvm-objcopy --redefine-sym foo=bar foo.o
#> llvm-objcopy: error: 'foo.o': The file was not recognized as a valid object file
file foo.o
#> foo.o: LLVM IR bitcode

Or, can I do something to solve the problem in other way? What I need is clang -o abc abc.c foo.o but let the function call in abc.c resolved to correct function in foo.o.

Environment info:

root@codespaces-251304:/bind# clang --version
Ubuntu clang version 19.1.0 (++20240815083225+4d4a4100f68d-1~exp1~20240815083357.22)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin

root@codespaces-251304:/bind# llvm-objcopy --version
llvm-objcopy, compatible with GNU objcopy
Ubuntu LLVM version 19.1.0
  Optimized build.

Upvotes: 0

Views: 86

Answers (1)

kkocdko
kkocdko

Reputation: 416

After hours I found a solution, use llvm-dis and llvm-as .

rename_function(){
  llvm-dis $3 -o - | sed -e "s|name: \"$1\"|name: \"$2\"|" -e "s|@$1(|@$2(|" | llvm-as -o $3.m.o
  mv $3.m.o $3
}
rename_function bar foo foo.o

Upvotes: 0

Related Questions