Skip to content

Instantly share code, notes, and snippets.

@danieldaeschle
Created December 10, 2020 23:28
Show Gist options
  • Save danieldaeschle/63bbec027eedca2bbb925b7aa95ac1d0 to your computer and use it in GitHub Desktop.
Save danieldaeschle/63bbec027eedca2bbb925b7aa95ac1d0 to your computer and use it in GitHub Desktop.
Rename function in object code
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void handler(int sig) {
void *array[10];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 10);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
// will be renamed to v.foo
void v_foo() {
int *foo = (int*)-1; // make a bad pointer
printf("%d\n", *foo); // causes segfault
}
void bar() { v_foo(); }
void foo() { bar(); }
int main(int argc, char* argv[]) {
signal(SIGSEGV, handler); // install our handler
foo(); // this will call foo, bar, and baz. baz segfaults.
return 0;
}
// gcc -g -O -c main.
// v run main.v
// gcc -g -rdynamic main.o -o main
// ./main => f_foo is v.foo in error
import os
fn main() {
to_replace := 'v_foo'
replace_to := 'v.foo'
mut elf := os.read_bytes('main.o')?
mut new_elf := []byte{}
for i := 0; i < elf.len; i++ {
c := elf[i]
if c == to_replace[0] {
mut found := true
if i + to_replace.len > elf.len {
break
}
for j in 0 .. to_replace.len {
c2 := elf[i + j]
cr := to_replace[j] // current character which should be replaced
if c2 != cr {
found = false
break
}
}
if found {
// insert new ones
new_elf << replace_to.bytes()
i += to_replace.len - 1
} else {
new_elf << c
}
} else {
new_elf << c
}
}
os.write_file_array('main.o', new_elf)?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment