'Replacing the symbol name in the symbol table to a new longer name in relocatable ELF object file
This is for a relocatable ELF object file, not fully-linked ELF or ELF shared library.
Currently if there is a program such as main.c:
int main() {
foo();
return 0;
}
and compile with gcc -c main.c it will generate a main.o.
I want to replace the call to foo with fool or food (a longer name) after the object file is already created.
Because currently the relocatable ELF will be broken if we extend past the length 3 of foo.
How can I do this?
Solution 1:[1]
I want to replace the call to
foowithfoolorfood(a longer name) after the object file is already created.
This is quite possible, but far from trivial.
First, you need to copy the .symtab section to the end of the file, and append the desired string food\0 to it.
Second, you need to update the section table and replace the offset and length of the original .symtab with the offset and size if the section added in previous step.
Last, you need to find the symbol (in the .symtab section) and update its st_name with the offset of the food string added in the first step.
And that's all there is to it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mkrieger1 |
