Since, I could not achieve success with Windows based system, I switched back to Linux/Unix. This time I addressed the drawbacks of my previous implementation.
My previous implementation worked only for a dynamically linked executable. This meant that the executable would result in a segmentation fault in case the user tried to link it statically.
Therefore, I had to address the same with either another implementation (a ground’s up approach) or updating the my current implementation. The use of dlfcn library meant that I could not update it any further, so I continued on finding another solution. This is when I stumbled upon the -Wl,-wrap method of wrapping functions. This method worked with statically and dynamically linked executable. Thus, this was an apt replacement of my current implementation.
An example of -Wl,-wrap with wrapping __libc_start_main to change the entry point is given as follows:
Step 1: Create a file named wrap_libc.cpp with the following code in it:
With the -Wl,-wrap method, you need to prepend __wrap_ with the function name (so __libc_start_main converts to __wrap___libc_start_main).
Step 2: Compile the code to create an object file to link to another file. In your terminal type:
$ g++ -c -o wrap_libc.o wrap_libc.cpp
This should create an object file by the name of wrap_libc.o which you can later link with.
Step 3: It is now time to create a test file:
Step 4: Open up the terminal and type:
$ g++ -static test.cpp wrap_libc.o -o static_working_file
This should result in the creation of a statically linked executable. Run it to check if you get the expected output:
$ ./static_working_fileThis is our Custom Entry pointmain
If your output matches the above output, things have worked out. And so now, we can address the second drawback.
Now that I had made things working for statically linked executable, I had to handle another major drawback. The HPX Runtime system initializes it from the custom implemented Entry Point. This would mean, that any global scope initialization using HPX functionality will result in a segmentation fault.
This needed further investigation, which leads to another libc function known as __libc_csu_init which carries on the global scope initializations. So wrapping this function to initialize the runtime system should help (or result in what is known as Initialization Sequencing Fiasco). I’m currently working on testing this method. A simple implementation can be seen here (needs HPX installed). You will need to run the makefile to generate the executable.
My current goals are to test this implementation thoroughly before integrating it with the source code (Since it will require a lot of changes in the cmake structure). I will then switch back to Windows to complete the project.
Until next week, I bid you farewell