this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Adding wrap_elf() CMake function.

=====
USAGE EXAMPLE:
=====

include(wrap_elf)
include(darling_exe)

wrap_elf(asound libasound.so)

add_darling_executable(pcm_min pcm_min.c)
target_link_libraries(pcm_min system asound)

Where pcm_min.c is a sample application taken from http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html

+41 -12
+22
cmake/wrap_elf.cmake
··· 1 + include(darling_lib) 2 + #include(CMakeParseArguments) 3 + 4 + function(wrap_elf name elfname) 5 + add_custom_command( 6 + OUTPUT 7 + ${CMAKE_CURRENT_BINARY_DIR}/${name}.c 8 + COMMAND 9 + ${CMAKE_BINARY_DIR}/src/dyld/wrapgen 10 + ${elfname} 11 + ${CMAKE_CURRENT_BINARY_DIR}/${name}.c 12 + DEPENDS 13 + wrapgen 14 + ) 15 + 16 + set(DYLIB_INSTALL_NAME "/usr/lib/native/lib${name}.dylib") 17 + include_directories(${CMAKE_SOURCE_DIR}/src/dyld) 18 + add_darling_library(${name} SHARED ${CMAKE_CURRENT_BINARY_DIR}/${name}.c) 19 + target_link_libraries(${name} PRIVATE system) 20 + install(TARGETS ${name} DESTINATION libexec/darling/usr/lib/native) 21 + endfunction(wrap_elf) 22 +
+4 -3
src/dyld/CMakeLists.txt
··· 9 9 10 10 enable_language(C ASM) 11 11 12 - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -ggdb") 12 + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") 13 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 13 14 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Ttext-segment,0x400000 -Wl,-Tbss,0x410000 -Wl,-Tdata,0x420000") 14 15 add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}" -D_GNU_SOURCE -DMLDR_BUILD) 15 16 ··· 27 28 add_executable(mldr ${mldr_sources}) 28 29 target_link_libraries(mldr pthread dl) 29 30 30 - add_executable(stubgen64 stubgen/stubgen64.c) 31 - target_link_libraries(stubgen64 dl) 31 + add_executable(wrapgen wrapgen/wrapgen.cpp) 32 + target_link_libraries(wrapgen dl) 32 33 33 34 install(TARGETS mldr DESTINATION libexec/darling/bin) 34 35 install(TARGETS darling DESTINATION bin
+15 -9
src/dyld/wrapgen/wrapgen.cpp
··· 10 10 #include <sys/mman.h> 11 11 #include <sstream> 12 12 #include <stdexcept> 13 + #include <fstream> 13 14 14 15 #ifndef PATH_MAX 15 16 # define PATH_MAX 4096 ··· 21 22 // TODO: use wrapgen32 to generate 32-bit wrappers. 22 23 23 24 void parse_elf(const char* elf, std::string& soname_out, std::set<std::string>& symbols_out); 24 - void generate_wrapper(const char* soname, const std::set<std::string>& symbols); 25 + void generate_wrapper(std::ofstream& output, const char* soname, const std::set<std::string>& symbols); 25 26 26 27 int main(int argc, const char** argv) 27 28 { 28 29 std::string elfLibrary; 29 30 std::set<std::string> symbols; 30 31 std::string soname; 32 + std::ofstream output; 31 33 32 - if (argc != 2) 34 + if (argc != 3) 33 35 { 34 - std::cerr << "Usage: " << argv[0] << " <library-name>\n"; 36 + std::cerr << "Usage: " << argv[0] << " <library-name> <output-file>\n"; 35 37 return 1; 36 38 } 37 39 38 40 elfLibrary = argv[1]; 41 + output.open(argv[2]); 39 42 40 43 try 41 44 { 45 + if (!output.is_open()) 46 + throw std::runtime_error("Cannot open output file"); 47 + 42 48 if (access(elfLibrary.c_str(), R_OK) == -1) 43 49 { 44 50 // Try loading the library and then ask the loader where it found the library. ··· 70 76 } 71 77 72 78 parse_elf(elfLibrary.c_str(), soname, symbols); 73 - generate_wrapper(soname.c_str(), symbols); 79 + generate_wrapper(output, soname.c_str(), symbols); 74 80 } 75 81 catch (const std::exception& e) 76 82 { ··· 230 236 munmap((void*) ehdr, length); 231 237 } 232 238 233 - void generate_wrapper(const char* soname, const std::set<std::string>& symbols) 239 + void generate_wrapper(std::ofstream& output, const char* soname, const std::set<std::string>& symbols) 234 240 { 235 - std::cout << "#include <elfcalls.h>\n" 241 + output << "#include <elfcalls.h>\n" 236 242 "extern struct elf_calls* _elfcalls;\n\n"; 237 243 238 - std::cout << "static void* lib_handle;\n" 244 + output << "static void* lib_handle;\n" 239 245 "__attribute__((constructor)) static void initializer() {\n" 240 246 "\tlib_handle = _elfcalls->dlopen_fatal(\"" << soname << "\");\n" 241 247 "}\n\n"; 242 248 243 - std::cout << "__attribute__((destructor)) static void destructor() {\n" 249 + output << "__attribute__((destructor)) static void destructor() {\n" 244 250 "\t_elfcalls->dlclose_fatal(lib_handle);\n" 245 251 "}\n\n"; 246 252 247 253 for (const std::string& sym : symbols) 248 254 { 249 - std::cout << "void* " << sym << "() {\n" 255 + output << "void* " << sym << "() {\n" 250 256 "\t__asm__(\".symbol_resolver _" << sym << "\");\n" 251 257 "\treturn _elfcalls->dlsym_fatal(lib_handle, \"" << sym << "\");\n" 252 258 "}\n\n";