diff --git a/lib/comgr/src/comgr.cpp b/lib/comgr/src/comgr.cpp index 5c2c8b3..ded4f77 100644 --- a/lib/comgr/src/comgr.cpp +++ b/lib/comgr/src/comgr.cpp @@ -2005,17 +2005,31 @@ amd_comgr_populate_name_expression_map(amd_comgr_data_t Data, Elf_Shdr_Impl dynsymShdr, relaShdr, rodataShdr; for (auto Shdr : Sections) { - if (Shdr.sh_type == llvm::ELF::SHT_DYNSYM) + + if (Shdr.sh_type == ELF::SHT_DYNSYM) dynsymShdr = Shdr; - if (Shdr.sh_type == ELF::SHT_RELA) + // Check sh_info to differentiate .rela.dyn and not .rela + if (Shdr.sh_type == ELF::SHT_RELA && Shdr.sh_info == 0) relaShdr = Shdr; - if ((Shdr.sh_type == ELF::SHT_PROGBITS) && - !(Shdr.sh_flags & ELF::SHF_WRITE) && - !(Shdr.sh_flags & ELF::SHF_EXECINSTR) && - !(Shdr.sh_flags & ELF::SHF_MASKPROC) ) - rodataShdr = Shdr; + // We can't uniquely identify the .rodata section using the type and flag + // because other sections may use the exact same flags and type (i.e. + // .interp). For correctness, we can check the name instead + if (Shdr.sh_type == ELF::SHT_PROGBITS && + (Shdr.sh_flags & ELF::SHF_ALLOC)) { + + Expected SecNameOrError = ELFFile.getSectionName(Shdr); + if (!SecNameOrError) { + llvm::logAllUnhandledErrors(SecNameOrError.takeError(), + llvm::errs(), "ELFObj creation error: "); + return AMD_COMGR_STATUS_ERROR; + } + StringRef SecName = std::move(SecNameOrError.get()); + + if (SecName.equals(StringRef(".rodata"))) + rodataShdr = Shdr; + } } // .dynsym - Find name expressions with amdgcn_name_expr and store their @@ -2097,9 +2111,9 @@ amd_comgr_populate_name_expression_map(amd_comgr_data_t Data, // Collect an unmangled name for each name expression for (auto expData : nameExpDataVec) { - // TODO: If/when an accessor API becomes availble to get the starting + // TODO: If/when an accessor API becomes available to get the starting // address for the section, switch to that - int offset = expData->RodataOffset - rodataShdr.sh_offset; + size_t offset = expData->RodataOffset - rodataShdr.sh_offset; // Store from the offset up until the first '\0' const char *unmangled = diff --git a/lib/comgr/test/name_expression_map_test.c b/lib/comgr/test/name_expression_map_test.c index df062bb..0cd58ff 100644 --- a/lib/comgr/test/name_expression_map_test.c +++ b/lib/comgr/test/name_expression_map_test.c @@ -131,8 +131,10 @@ int main(int argc, char *argv[]) { exit(1); } - char *nameExpressions[] = {"my_kernel_BOO(2+1),float >"}; - char *symbolNames[] = {"_Z13my_kernel_BOOILi3EfEvPT0_"}; + char *nameExpressions[] = {"my_kernel_BOO(2+1),float >", + "my_kernel_FOO(2+1),float >"}; + char *symbolNames[] = {"_Z13my_kernel_BOOILi3EfEvPT0_", + "_Z13my_kernel_FOOILi3EfEvPT0_"}; for (size_t I = 0; I < numNames; ++I) { size_t Size;