Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Last active March 6, 2020 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlabbe/9a9c9a4515c51291b09ec8e4eda6db6d to your computer and use it in GitHub Desktop.
Save mlabbe/9a9c9a4515c51291b09ec8e4eda6db6d to your computer and use it in GitHub Desktop.
Redefine a symbol in a compiled elf object file
#!/bin/sh -x
rm -f *.o redefine
CC=clang-9
LD=$CC
CFLAGS="-fno-inline-functions -O0"
$CC -c redefine.c -o redefine.o $CFLAGS
$CC -c redefine_b.c -o redefine_b.o $CFLAGS
$CC -c redefine_c.c -o redefine_c.o $CFLAGS
# rename the moveme function(symbol) to setaside
llvm-objcopy-9 --redefine-sym moveme=setaside redefine_c.o redefine_c.o
$LD redefine.o redefine_b.o redefine_c.o -o redefine && \
./redefine
$ ./redefine
success!
this function is called setaside() by the time linking happens
void moveme(void);
void setaside(void);
int main(void) {
moveme();
setaside();
return 0;
}
#include<stdio.h>
void moveme(void) {
puts("success!");
}
#include <stdio.h>
void moveme(void) {
puts("this function is called setaside() by the time linking happens");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment