Is it possible to link some functions in a regular binary?

I just have an interesting idea. I used objdump to dump a simple binary and I see many functions in the binary. Is it possible to create another C program that communicates with these functions? Assuming I know the input and output options.

Additional information: file1: test.c

#include <stdio.h> int add(int x,int y) { return x+y; } int main(int argc, const char *argv[]) { printf("%d\n",add(3,4)); return 0; } 

file2: test1.c

 #include <stdio.h> int main(int argc, const char *argv[]) { printf("%d\n",add(8,8)); return 0; } gcc test.c -o test.exe gcc test1.c test.exe -o test1.exe 

Output:

 ld: in test.exe, can't link with a main executable collect2: ld returned 1 exit status 
+4
source share
3 answers

From a practical point of view, there is a slight difference between the object (.o) file and the executable. An object file may contain unrelated characters where the executable cannot. The executable file must contain an entry point at which the object file does not have this restriction. The executable file has a more complete header. The executable file also performs all jump offsets, as was the result of the connection resolution step. Some features may have been permanently postponed.

So, theoretically, you can create an executable file that calls functions from another executable file, but not only with a normal communication line. The main problem is that the second executable cannot have an entry point - the main function - and is still associated with the original (as the names will collide).

If your goal is to simply call the original functions, I suggest using a different method from the direct link that you seem to be suggesting. If you create a shared library and put it in the LD_PRELOAD environment variable and then call the original executable file, you can use your library to efficiently connect the program record (possibly using the _main character) and then call an alternative program procedure. Since this library loads with the source binary, you can call all the source functions ...

But the easiest way to name functions from binary is to simply link to the object files instead of the executable.

+1
source

I'm afraid not.

The compiled binary is processed through the transfer phase by the linker, which links each symbol reference in the code to the run-time address.

You can do a simple experiment to find out the differences, here is a program that displays "Hello World":

 // main.c #include <stdio.h> int main() { printf("Hello World!"); return 0; } 

Using gcc -c , you can compile the source code into a roaming object:

$ gcc -c main.o

 $ readelf -s main.o Symbol table '.symtab' contains 10 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 FILE LOCAL DEFAULT ABS main.c 2: 00000000 0 SECTION LOCAL DEFAULT 1 3: 00000000 0 SECTION LOCAL DEFAULT 3 4: 00000000 0 SECTION LOCAL DEFAULT 4 5: 00000000 0 SECTION LOCAL DEFAULT 5 6: 00000000 0 SECTION LOCAL DEFAULT 7 7: 00000000 0 SECTION LOCAL DEFAULT 6 8: 00000000 29 FUNC GLOBAL DEFAULT 1 main 9: 00000000 0 NOTYPE GLOBAL DEFAULT UND printf 

You can see that the value of the main function is 0x0, which means that it has not yet been moved and can be associated with others.

But when you compile the file using the gcc command to generate the executable:

 $ gcc main.c $ readelf -s a.out | grep main 2: 00000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC _2.0 (2) 39: 00000000 0 FILE LOCAL DEFAULT ABS main.c 51: 00000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@ @GLIBC_ 62: 080483c4 29 FUNC GLOBAL DEFAULT 13 main 

Now you can see that the address of the main function has been moved to 0x80483c4, which is the address of the runtime of the function code. Generating a.out can no longer be linked to others, as there may be a violation of the runtime address for this.

Generally speaking, the movement phase cannot be canceled because some information about the symbol is lost after the phase.

For more information, I suggest you read the chapter “Binding” in the book “Computer System: A Programmer's Perspective” , which talks a lot about linking and moving.

+2
source

Of course, just write a header file that provides declarations for the functions you want to use with the correct function signatures, and then include that header file in your C code where you call the functions. Then compile and link to another object file to create the final executable.

The assumption, however, is that the functions in the file of the object you dropped follow the ABI and the calling conventions for the platform / compiler you are working with (I know this seems obvious) and it cannot include its own entry point ( i.e. function main() ). As for the second point, the object file should be basically a “library” of autonomous functions. This means that you cannot reference the executable file.

+1
source

Source: https://habr.com/ru/post/1391070/


All Articles