From 2758da98cdb45fde687969ff40487757c46276a2 Mon Sep 17 00:00:00 2001 From: raghavmedicherla Date: Thu, 8 Dec 2022 16:50:43 -0500 Subject: [PATCH] [hsa-runtime] Add support to hsa-runtime to find symbols from ".dynsym" section. Earlier, hsa-runtime was unable to find symbols from a stripped ELF-image becasue no support to find symbols from ".dynsym" section. Looking for symbols in .dynsym is enabled by LOADER_USE_DYNSYM=1 environment variable Change-Id: I4f0e8dd0eb053a6066d4d49b670c52e51149531a [ROCm/ROCR-Runtime commit: 4142a7737518254fc1ce4af9a3b8bc93c7ca6817] --- .../hsa-runtime/core/inc/amd_elf_image.hpp | 4 ++ .../libamdhsacode/amd_elf_image.cpp | 40 ++++++++++++++++++- .../libamdhsacode/amd_hsa_code.cpp | 4 +- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_elf_image.hpp b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_elf_image.hpp index 0da61f5cf8..177465c05e 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_elf_image.hpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/inc/amd_elf_image.hpp @@ -183,6 +183,10 @@ namespace elf { virtual StringTable* strtab() = 0; virtual SymbolTable* symtab() = 0; virtual SymbolTable* getSymtab(uint16_t index) = 0; + virtual SymbolTable* dynsym() = 0; + virtual SymbolTable* getDynsym(uint16_t index) = 0; + virtual SymbolTable* getSymbolTable() = 0; + virtual SymbolTable* getSymbolTable(uint16_t index) = 0; virtual StringTable* addStringTable(const std::string& name) = 0; virtual StringTable* getStringTable(uint16_t index) = 0; diff --git a/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp b/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp index 33871dd2b9..8062af901b 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp @@ -713,7 +713,24 @@ namespace elf { GElfStringTable* strtab() override; GElfSymbolTable* getSymtab(uint16_t index) override { - return static_cast(section(index)); + if (section(index)->type() == SHT_SYMTAB) + return static_cast(section(index)); + return nullptr; + } + GElfSymbolTable* getDynsym(uint16_t index) override + { + if (section(index)->type() == SHT_DYNSYM) + return static_cast(section(index)); + return nullptr; + } + + GElfSymbolTable* getSymbolTable() override; + GElfSymbolTable* getSymbolTable(uint16_t index) override + { + const char *UseDynsym = getenv("LOADER_USE_DYNSYM"); + if (UseDynsym && std::strncmp(UseDynsym, "0", 1) != 0) + return getDynsym(index); + return getSymtab(index); } GElfStringTable* addStringTable(const std::string& name) override; @@ -721,6 +738,7 @@ namespace elf { GElfSymbolTable* addSymbolTable(const std::string& name, StringTable* stab = 0) override; GElfSymbolTable* symtab() override; + GElfSymbolTable* dynsym() override; GElfSegment* segment(size_t i) override { return segments[i].get(); } Segment* segmentByVAddr(uint64_t vaddr) override; @@ -759,6 +777,7 @@ namespace elf { GElfStringTable* shstrtabSection; GElfStringTable* strtabSection; GElfSymbolTable* symtabSection; + GElfSymbolTable* dynsymSection; GElfNoteSection* noteSection; std::vector> segments; std::vector> sections; @@ -1261,6 +1280,7 @@ namespace elf { e(0), shstrtabSection(0), strtabSection(0), symtabSection(0), + dynsymSection(0), noteSection(0) { if (EV_NONE == elf_version(EV_CURRENT)) { @@ -1436,6 +1456,7 @@ namespace elf { if (section->type() == SHT_STRTAB) { strtabSection = static_cast(section.get()); } if (section->type() == SHT_SYMTAB) { symtabSection = static_cast(section.get()); } if (section->type() == SHT_NOTE) { noteSection = static_cast(section.get()); } + if (section->type() == SHT_DYNSYM) { dynsymSection = static_cast(section.get()); } } size_t phnum; @@ -1553,7 +1574,7 @@ namespace elf { } } - GElfStringTable* GElfImage::addStringTable(const std::string& name) + GElfStringTable* GElfImage::addStringTable(const std::string& name) { GElfStringTable* stab = new GElfStringTable(this); sections.push_back(std::unique_ptr(stab)); @@ -1597,6 +1618,21 @@ namespace elf { return symtabSection; } + GElfSymbolTable* GElfImage::dynsym() + { + if (!dynsymSection) { + dynsymSection = addSymbolTable(".dynsym", strtab()); + } + return dynsymSection; + } + + GElfSymbolTable* GElfImage::getSymbolTable() + { + const char *UseDynsym = getenv("LOADER_USE_DYNSYM"); + if (UseDynsym && std::strncmp(UseDynsym, "0", 1) != 0) + return dynsym(); + return symtab(); + } GElfNoteSection* GElfImage::note() { diff --git a/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_hsa_code.cpp b/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_hsa_code.cpp index e58b6e2ae4..da0a6532e3 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_hsa_code.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/libamdhsacode/amd_hsa_code.cpp @@ -1765,8 +1765,8 @@ namespace code { hsatext = sec; } } - for (size_t i = 0; i < img->symtab()->symbolCount(); ++i) { - amd::elf::Symbol* elfsym = img->symtab()->symbol(i); + for (size_t i = 0; i < img->getSymbolTable()->symbolCount(); ++i) { + amd::elf::Symbol* elfsym = img->getSymbolTable()->symbol(i); Symbol* sym = 0; switch (elfsym->type()) { case STT_AMDGPU_HSA_KERNEL: {