From 99a11d63918a781b0d4d9df24e3ee73772a5ca74 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Tue, 4 Jun 2024 10:55:44 +0200 Subject: [PATCH] Plug the test reources build into gradle scripts --- ddprof-lib/gtest/build.gradle | 20 +++++++++++ ddprof-lib/src/main/cpp/symbols_linux.cpp | 7 +--- ddprof-lib/src/test/cpp/elfparser_ut.cpp | 3 +- .../resources/unresolved-functions/Makefile | 5 +-- .../resources/unresolved-functions/linker.ld | 36 +++++++++++++++++-- 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/ddprof-lib/gtest/build.gradle b/ddprof-lib/gtest/build.gradle index b38a6db9..9735cac1 100644 --- a/ddprof-lib/gtest/build.gradle +++ b/ddprof-lib/gtest/build.gradle @@ -36,6 +36,22 @@ tasks.withType(StripSymbols).configureEach { task -> } } +def buildResourcesTask = tasks.register("buildResources", Exec) { + group = 'build' + description = "Build the resources for the Google Tests" + + onlyIf { + hasGtest && !project.hasProperty('skip-native') && os().isLinux() + } + + def targetDir = project(':ddprof-lib').file('build/test/resources/unresolved-functions') + + commandLine "sh", "-c", "cd ${project(':ddprof-lib').projectDir}/src/test/resources/unresolved-functions && make TARGET_DIR=${targetDir}" + + inputs.files project(':ddprof-lib').files('src/test/resources/unresolved-functions') + outputs.file "${targetDir}/main" +} + def gtestAll = tasks.register("gtest") { onlyIf { hasGtest && !project.hasProperty('skip-native') @@ -155,6 +171,10 @@ tasks.whenTaskAdded { task -> gtestLinkTask.dependsOn compileTask } gtestTask.get().dependsOn gtestExecuteTask.get() + if (os().isLinux()) { + // custom binaries for tests are built only on linux + gtestExecuteTask.get().dependsOn buildResourcesTask + } gtestAll.get().dependsOn gtestExecuteTask.get() } } diff --git a/ddprof-lib/src/main/cpp/symbols_linux.cpp b/ddprof-lib/src/main/cpp/symbols_linux.cpp index 319c36a9..b3ea4045 100644 --- a/ddprof-lib/src/main/cpp/symbols_linux.cpp +++ b/ddprof-lib/src/main/cpp/symbols_linux.cpp @@ -76,7 +76,6 @@ bool ElfParser::parseFile(CodeCache* cc, const char* base, const char* file_name } size_t length = (size_t)lseek64(fd, 0, SEEK_END); - fprintf(stdout, "===> Parsing file: %s, length=%lu\n", file_name, length); void* addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); @@ -85,7 +84,6 @@ bool ElfParser::parseFile(CodeCache* cc, const char* base, const char* file_name } else { ElfParser elf(cc, base != nullptr ? base : (const char*)addr, addr, file_name, length, false); if (elf.validHeader()) { - fprintf(stdout, "===> Loading symbols for: %s (%lu)\n", file_name, length); elf.loadSymbols(use_debug); } munmap(addr, length); @@ -333,17 +331,15 @@ bool ElfParser::loadSymbolsUsingDebugLink() { } void ElfParser::loadSymbolTable(const char* symbols, size_t total_size, size_t ent_size, const char* strings) { - fprintf(stdout, "===> Loading symbol table at %p, base=%p, size=%lu, entry_size=%lu\n", symbols, _base, total_size, ent_size); for (const char* symbols_end = symbols + total_size; symbols < symbols_end; symbols += ent_size) { ElfSymbol* sym = (ElfSymbol*)symbols; if (sym->st_name != 0 && sym->st_value != 0) { + // sanity check the offsets not to exceed the file size if (_length == 0 || (sym->st_name < _length && sym->st_value < _length)) { // Skip special AArch64 mapping symbols: $x and $d if (sym->st_size != 0 || sym->st_info != 0 || strings[sym->st_name] != '$') { _cc->add(_base + sym->st_value, (int)sym->st_size, strings + sym->st_name); } - } else { - fprintf(stdout, "===> Skipping symbol: value=%lu, name=%u, stt=%d\n", sym->st_value, sym->st_name, sym->st_info & 0xf); } } } @@ -450,7 +446,6 @@ void Symbols::parseLibraries(CodeCacheArray* array, bool kernel_symbols) { while ((len = getline(&str, &str_size, f)) > 0) { str[len - 1] = 0; - fprintf(stdout, "===> Parsing library: %s\n", str); MemoryMapDesc map(str); if (!map.isReadable() || map.file() == NULL || map.file()[0] == 0) { continue; diff --git a/ddprof-lib/src/test/cpp/elfparser_ut.cpp b/ddprof-lib/src/test/cpp/elfparser_ut.cpp index 9db37ce3..6bff98fa 100644 --- a/ddprof-lib/src/test/cpp/elfparser_ut.cpp +++ b/ddprof-lib/src/test/cpp/elfparser_ut.cpp @@ -13,9 +13,8 @@ TEST(Elf, readSymTable) { if (getcwd(cwd, sizeof(cwd)) == nullptr) { exit(1); } - fprintf(stdout, "Current working directory: %s\n", cwd); char path[PATH_MAX]; - snprintf(path, sizeof(path) - 1, "%s/../src/test/resources/libj9jit.so", cwd); + snprintf(path, sizeof(path) - 1, "%s/../build/test/resources/unresolved-functions/main", cwd); if (access(path, R_OK) != 0) { fprintf(stdout, "Missing test resource %s. Skipping the test\n", path); exit(0); diff --git a/ddprof-lib/src/test/resources/unresolved-functions/Makefile b/ddprof-lib/src/test/resources/unresolved-functions/Makefile index d1590d49..2f3f66f1 100644 --- a/ddprof-lib/src/test/resources/unresolved-functions/Makefile +++ b/ddprof-lib/src/test/resources/unresolved-functions/Makefile @@ -1,3 +1,4 @@ +TARGET_DIR = ../build/test/resources/unresolved-functions all: - gcc -c main.c -o main.o - gcc -o main main.o -T linker.ld \ No newline at end of file + gcc -c main.c -o $(TARGET_DIR)/main.o + gcc -o $(TARGET_DIR)/main $(TARGET_DIR)/main.o -T linker.ld \ No newline at end of file diff --git a/ddprof-lib/src/test/resources/unresolved-functions/linker.ld b/ddprof-lib/src/test/resources/unresolved-functions/linker.ld index 6f0701d7..0e930a39 100644 --- a/ddprof-lib/src/test/resources/unresolved-functions/linker.ld +++ b/ddprof-lib/src/test/resources/unresolved-functions/linker.ld @@ -1,13 +1,43 @@ +PHDRS +{ + headers PT_PHDR PHDRS ; + interp PT_INTERP ; + text PT_LOAD FILEHDR PHDRS ; + data PT_LOAD ; +} + SECTIONS { . = 0x10000; - .text : { *(.text) } + .text : { + *(.text) + } :text + . = 0x20000; - .data : { *(.data) } - .bss : { *(.bss) } + .data : { + *(.data) + } :data + + .bss : { + *(.bss) + } + . = 0x30000; unresolved_symbol = .; . = 0xffffffffffffffff; unresolved_function = .; + /* Add the .init_array section */ + .init_array : { + __init_array_start = .; + KEEP(*(.init_array)) + __init_array_end = .; + } + + /* Add the .fini_array section */ + .fini_array : { + __fini_array_start = .; + KEEP(*(.fini_array)) + __fini_array_end = .; + } }