Added support for R_X86_64_IRELATIVE reloc, along with a test for it (for #303)

This commit is contained in:
ptitSeb 2022-07-03 14:14:17 +02:00
parent 48b881f491
commit 672a41a79c
5 changed files with 37 additions and 1 deletions

View File

@ -736,7 +736,12 @@ add_test(backtrace ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX64}
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref19.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )
file(GLOB extension_tests "${CMAKE_SOURCE_DIR}/tests/extensions/*.c")
add_test(irelative_reloc ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX64}
-D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test20 -D TEST_OUTPUT=tmpfile20.txt
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref20.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )
file(GLOB extension_tests "${CMAKE_SOURCE_DIR}/tests/extensions/*.c")
foreach(file ${extension_tests})
get_filename_component(testname "${file}" NAME_WE)
add_test(NAME "${testname}" COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX64}

View File

@ -620,6 +620,14 @@ int RelocateElfRELA(lib_t *maplib, lib_t *local_maplib, int bindnow, elfheader_t
printf_dump(LOG_NEVER, "Apply %s R_X86_64_RELATIVE @%p (%p -> %p)\n", (bind==STB_LOCAL)?"Local":"Global", p, *(void**)p, (void*)(head->delta+ rela[i].r_addend));
*p = head->delta+ rela[i].r_addend;
break;
case R_X86_64_IRELATIVE:
{
x64emu_t* emu = thread_get_emu();
EmuCall(emu, head->delta+rela[i].r_addend);
printf_dump(LOG_NEVER, "Apply %s R_X86_64_IRELATIVE @%p (%p -> %p()=%p)\n", (bind==STB_LOCAL)?"Local":"Global", p, *(void**)p, (void*)(head->delta+ rela[i].r_addend), (void*)(R_RAX));
*p = R_RAX;
}
break;
case R_X86_64_COPY:
globoffs = offs;
globend = end;

2
tests/ref20.txt Normal file
View File

@ -0,0 +1,2 @@
Called function number 2

BIN
tests/test20 Executable file

Binary file not shown.

21
tests/test20.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
int myfunc1() { return 1; }
int myfunc2() { return 2; }
// Prototype for the common entry point
/*extern "C" */int myfunc();
__asm__ (".type myfunc, @gnu_indirect_function");
// Make the dispatcher function. This returns a pointer to the desired function version
typeof(myfunc) * myfunc_dispatch (void) __asm__ ("myfunc");
typeof(myfunc) * myfunc_dispatch (void) {
if (0)
return &myfunc1;
else
return &myfunc2;
}
int main() {
printf("\nCalled function number %i\n", myfunc());
return 0;
}