Skip to content
Snippets Groups Projects
Commit 4cb309fa authored by Alessandro Di Federico's avatar Alessandro Di Federico
Browse files

Feed libraries to the test executable through stdin

* Created a Python script to properly format the input libraries
* Updated the CMake script to use the Python script
* Drop generation of .h files from shared libraries
* Refactor test.c
* Set eld_{init,finish} as {con,de}structors
parent 5487e7bb
No related branches found
No related tags found
No related merge requests found
......@@ -44,24 +44,14 @@ include_directories("${PROJECT_BINARY_DIR}")
set(OR1K_SIM_PATH "or32-elf-sim" CACHE FILEPATH "Path to the OR1K simulator")
configure_file(sim.cfg sim.cfg COPYONLY)
# Generate .h files to embed shared objects
add_library(eld STATIC dl.c eld.c elf-object.c)
# libmy.so
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libmy.so.h
COMMAND xxd -i libmy.so > libmy.so.h
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libmy.so
)
add_custom_target(libmy_header
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libmy.so.h)
# Loader main executable
add_executable(loader test.c)
target_link_libraries(loader eld)
# Create a copy of the loader executable forcing it to a shared object
# so it can be dynamically linked against other shared libraries
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libyour.so.h
COMMAND xxd -i libyour.so > libyour.so.h
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libyour.so
)
add_custom_command(
TARGET loader
POST_BUILD
......@@ -70,34 +60,25 @@ add_custom_command(
COMMAND dd if=${CMAKE_CURRENT_SOURCE_DIR}/shared-library-id.bin
of=main seek=16 bs=1 conv=notrunc
)
add_custom_target(libyour_header
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libyour.so.h)
# Test dynamic libraries
add_library(your SHARED libyour.c)
add_library(my SHARED libmy.c)
target_link_libraries(my your)
add_library(eld STATIC dl.c eld.c elf-object.c)
# Loader main executable
add_executable(loader test.c)
target_link_libraries(loader eld)
# Link libmy against libyour and the main executable
target_link_libraries(my your ":main")
add_dependencies(my loader)
# Simulation targets
add_custom_target(loader_sim COMMAND ${OR1K_SIM_PATH} -f sim.cfg loader)
# Use a script to provide the shared objects to the loader on the stdin
set(FEED_SO ${CMAKE_CURRENT_SOURCE_DIR}/feed-so.py
${CMAKE_CURRENT_BINARY_DIR}/libyour.so
${CMAKE_CURRENT_BINARY_DIR}/libmy.so)
add_custom_target(loader_sim
COMMAND ${FEED_SO} | ${OR1K_SIM_PATH} -f sim.cfg loader)
add_custom_target(loader_sim_debug
COMMAND ${OR1K_SIM_PATH} -f sim.cfg --srv=9001 loader)
COMMAND ${FEED_SO} | ${OR1K_SIM_PATH} -f sim.cfg --srv=9001 loader)
# Dependencies
add_dependencies(loader_sim loader)
add_dependencies(loader_sim_debug loader)
add_dependencies(loader libmy_header)
add_dependencies(libmy_header my)
add_dependencies(loader libyour_header)
add_dependencies(libyour_header your)
add_dependencies(loader_sim loader my your)
add_dependencies(loader_sim_debug loader my your)
int eld_init();
int eld_finish();
__attribute__((constructor)) int eld_init();
__attribute__((destructor)) int eld_finish();
void *dlsym(void *handle, char *symbol);
void *dlopen(char *filename, int flag);
int dlclose(void *handle);
#include "dl.h"
#include "eld.h"
#include "support.h"
......
#!/usr/bin/python
import os
import struct
import sys
def main():
for so in sys.argv[1:]:
size = os.path.getsize(so)
sys.stdout.write(struct.pack("<L", size))
sys.stdout.write(open(so).read())
sys.stdout.write("\x00\x00\x00\x00")
if __name__ == "__main__":
main()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "support.h"
#include "dl.h"
// Test libraries
#include "libmy.so.h"
#include "libyour.so.h"
#define MAX_LIBS 32
#define MAX_LIB_SIZE (1024 * 1024)
int main() {
struct {
char *library;
void *handle;
} libs[MAX_LIBS] = {0};
unsigned int lib_count = 0;
// Simple test function to be used by test libraries
void print(int value) {
printf("%d\n", value);
}
// Reads from stdin a 4-byte little endian integer
int read_length() {
unsigned int i = 0, result = 0;
char input = 0;
for (; i < 4; i++) {
read(0, &input, 1);
result |= (input & 0xff) << i * 8;
}
return result;
}
int load_libs() {
int result = SUCCESS;
// Initialize the ELD library
RETURN_ON_ERROR(eld_init());
// Read libraries from stdin
unsigned int so_length = 0;
while ((so_length = read_length())) {
if (lib_count > MAX_LIBS) {
DBG_MSG("Too many libraries, maximum is %u", MAX_LIBS);
return -1;
} else if (so_length > MAX_LIB_SIZE) {
DBG_MSG("Library is too big (%u bytes), maximum is %d kB", so_length,
MAX_LIB_SIZE / 1024);
return -1;
}
// Reserve space for the library
libs[lib_count].library = (char *) malloc(so_length);
DBG_MSG("Reading %u bytes at %p...", so_length, libs[lib_count].library);
// Read the library from stdin
unsigned char input = 0;
for (unsigned int i = 0; i < so_length; i++) {
read(0, &input, 1);
libs[lib_count].library[i] = input;
}
// Open the library
libs[lib_count].handle = dlopen(libs[lib_count].library, 0);
RETURN_ON_NULL(libs[lib_count].handle);
// Load libraries
void *libyour_handle = NULL;
RETURN_ON_NULL(libyour_handle = dlopen((char *) libyour_so, 0));
lib_count++;
}
return result;
}
int unload_libs() {
int result = SUCCESS;
// Close and unload the loaded libraries
while (lib_count --> 0) {
RETURN_ON_ERROR(dlclose(libs[lib_count].handle));
free(libs[lib_count].library);
}
return result;
}
int main() {
int result = SUCCESS;
void *libmy_handle = NULL;
RETURN_ON_NULL(libmy_handle = dlopen((char *) libmy_so, 0));
RETURN_ON_ERROR(load_libs());
// Play around with the loaded libraries
typedef int (*myfunc_ptr)();
myfunc_ptr myfunc = NULL;
myfunc = dlsym(NULL, "my");
......@@ -28,12 +95,9 @@ int main() {
printf("myfunc() == %d\n", myfunc());
printf("your_variable == 0x%x\n", *libyour_variable);
// Close the loaded libraries
RETURN_ON_ERROR(dlclose(libmy_handle));
RETURN_ON_ERROR(dlclose(libyour_handle));
RETURN_ON_ERROR(unload_libs());
// Finalize the ELD library
RETURN_ON_ERROR(eld_finish());
DBG_MSG("Done");
return result;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment