diff --git a/llvm/lib/ProfileData/RawMemProfReader.cpp b/llvm/lib/ProfileData/RawMemProfReader.cpp --- a/llvm/lib/ProfileData/RawMemProfReader.cpp +++ b/llvm/lib/ProfileData/RawMemProfReader.cpp @@ -261,6 +261,24 @@ FileName); } + // Check whether the profiled binary was built with position independent code + // (PIC). For now we provide a error message until symbolization support + // is added for pic. + auto* Elf64LEObject = llvm::cast(ElfObject); + const llvm::object::ELF64LEFile& ElfFile = Elf64LEObject->getELFFile(); + auto PHdrsOr = ElfFile.program_headers(); + if(!PHdrsOr) + return report(make_error(Twine("Could not read program headers: "), + inconvertibleErrorCode()), + FileName); + auto FirstLoadHeader = PHdrsOr->begin(); + while (FirstLoadHeader->p_type != llvm::ELF::PT_LOAD) + ++FirstLoadHeader; + if(FirstLoadHeader->p_vaddr == 0) + return report(make_error(Twine("Unsupported position independent code"), + inconvertibleErrorCode()), + FileName); + auto Triple = ElfObject->makeTriple(); if (!Triple.isX86()) return report(make_error(Twine("Unsupported target: ") + diff --git a/llvm/test/tools/llvm-profdata/Inputs/pic.memprofexe b/llvm/test/tools/llvm-profdata/Inputs/pic.memprofexe new file mode 100755 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@ +#include +int main(int argc, char **argv) { + char *x = (char *)malloc(10); + memset(x, 0, 10); + free(x); + x = (char *)malloc(10); + memset(x, 0, 10); + free(x); + return 0; +} +``` + +The following commands were used to compile the source to a memprof instrumented +executable and collect a raw binary format profile. Since the profile contains +virtual addresses for the callstack, we do not expect the raw binary profile to +be deterministic. The summary should be deterministic apart from changes to +the shared libraries linked in which could change the number of segments +recorded. + +``` +clang -fuse-ld=lld -Wl,--no-rosegment -gmlt -fdebug-info-for-profiling \ + -fmemory-profile -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer \ + -fno-optimize-sibling-calls -m64 -Wl,-build-id -pie \ + source.c -o pic.memprofexe + +env MEMPROF_OPTIONS=log_path=stdout ./pic.memprofexe > pic.memprofraw +``` + +RUN: not llvm-profdata show --memory %p/Inputs/pic.memprofraw --profiled-binary %p/Inputs/pic.memprofexe -o - |& FileCheck %s +CHECK: Unsupported position independent code