From 5c120f5f2d9ac858927690c5202a9885dd7daef6 Mon Sep 17 00:00:00 2001 From: Krystian Chmielewski Date: Tue, 19 Apr 2022 14:15:46 +0000 Subject: [PATCH] fix: Ignore unsupported relocs in debug zebin Do not apply relocations with types different than {1, 2, 3}, when creating debug zebin. Signed-off-by: Krystian Chmielewski --- .../device_binary_format/debug_zebin.cpp | 14 ++++++++++-- .../source/device_binary_format/debug_zebin.h | 1 + .../zebin_debug_binary_tests.cpp | 22 +++++++++++++++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/shared/source/device_binary_format/debug_zebin.cpp b/shared/source/device_binary_format/debug_zebin.cpp index 46b5ca869d..1dabb48c01 100644 --- a/shared/source/device_binary_format/debug_zebin.cpp +++ b/shared/source/device_binary_format/debug_zebin.cpp @@ -75,6 +75,12 @@ void DebugZebinCreator::applyRelocation(uint64_t addr, uint64_t value, RELOC_TYP } } +bool DebugZebinCreator::isRelocTypeSupported(NEO::Elf::RELOC_TYPE_ZEBIN type) { + return type == NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR || + type == NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR_32 || + type == NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR_32_HI; +} + void DebugZebinCreator::applyRelocations() { std::string errors, warnings; auto elf = decodeElf(debugZebin, errors, warnings); @@ -82,6 +88,11 @@ void DebugZebinCreator::applyRelocations() { for (const auto &relocations : {elf.getDebugInfoRelocations(), elf.getRelocations()}) { for (const auto &reloc : relocations) { + auto relocType = static_cast(reloc.relocType); + if (isRelocTypeSupported(relocType) == false) { + continue; + } + uint64_t symbolOffset = 0U; auto symbolSectionName = elf.getSectionName(reloc.symbolSectionIndex); if (auto segment = getSegmentByName(symbolSectionName)) { @@ -97,8 +108,7 @@ void DebugZebinCreator::applyRelocations() { uint64_t relocVal = symbolOffset + elf.getSymbolValue(reloc.symbolTableIndex) + reloc.addend; auto relocAddr = reinterpret_cast(debugZebin.data()) + elf.getSectionOffset(reloc.targetSectionIndex) + reloc.offset; - auto type = static_cast(reloc.relocType); - applyRelocation(relocAddr, relocVal, type); + applyRelocation(relocAddr, relocVal, relocType); } } } diff --git a/shared/source/device_binary_format/debug_zebin.h b/shared/source/device_binary_format/debug_zebin.h index e87cc609a4..691b8ffbe3 100644 --- a/shared/source/device_binary_format/debug_zebin.h +++ b/shared/source/device_binary_format/debug_zebin.h @@ -49,6 +49,7 @@ class DebugZebinCreator { void applyRelocation(uint64_t addr, uint64_t value, NEO::Elf::RELOC_TYPE_ZEBIN type); const Segments::Segment *getSegmentByName(ConstStringRef sectionName); const Segments::Segment *getTextSegmentByName(ConstStringRef sectionName); + bool isRelocTypeSupported(NEO::Elf::RELOC_TYPE_ZEBIN type); std::vector debugZebin; const Segments &segments; diff --git a/shared/test/unit_test/device_binary_format/zebin_debug_binary_tests.cpp b/shared/test/unit_test/device_binary_format/zebin_debug_binary_tests.cpp index 8beaf6d881..3a4ccc8011 100644 --- a/shared/test/unit_test/device_binary_format/zebin_debug_binary_tests.cpp +++ b/shared/test/unit_test/device_binary_format/zebin_debug_binary_tests.cpp @@ -20,7 +20,7 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) { uint8_t kernelISA[8] = {0x3}; uint8_t stringData[8] = {0x4}; - uint8_t debugInfo[0x28] = {0x0}; + uint8_t debugInfo[0x30] = {0x22}; uint8_t debugAbbrev[8] = {0x0}; using Segment = NEO::Debug::Segments::Segment; @@ -43,7 +43,7 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) { typedef NEO::Elf::ElfSymbolEntry SymbolEntry; typedef NEO::Elf::ElfRela Relocation; - SymbolEntry symbols[6]{}; + SymbolEntry symbols[7]{}; symbols[0].name = elfEncoder.appendSectionName("kernel"); symbols[0].info = NEO::Elf::SYMBOL_TABLE_TYPE::STT_SECTION | NEO::Elf::SYMBOL_TABLE_BIND::STB_LOCAL << 4; symbols[0].shndx = static_cast(kernelSectionIndex); @@ -74,7 +74,12 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) { symbols[5].shndx = static_cast(debugInfoSectionIndex); symbols[5].value = 0U; - Relocation debugRelocations[6]{}; + symbols[6].name = elfEncoder.appendSectionName("kernel_payload_offset"); + symbols[6].info = NEO::Elf::SYMBOL_TABLE_TYPE::STT_SECTION | NEO::Elf::SYMBOL_TABLE_BIND::STB_LOCAL << 4; + symbols[6].shndx = static_cast(kernelSectionIndex); + symbols[6].value = 0x10U; + + Relocation debugRelocations[7]{}; debugRelocations[0].addend = 0xabc; debugRelocations[0].offset = 0x0; debugRelocations[0].info = (uint64_t(0) << 32) | NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR; @@ -100,6 +105,11 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) { debugRelocations[5].offset = 0x20U; debugRelocations[5].info = (uint64_t(5) << 32) | NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR; + // Will be ignored due to reloc type + debugRelocations[6].addend = 0x0; + debugRelocations[6].offset = 0x28; + debugRelocations[6].info = (uint64_t(6) << 32) | NEO::Elf::RELOC_TYPE_ZEBIN::R_PER_THREAD_PAYLOAD_OFFSET; + elfEncoder.appendSection(NEO::Elf::SHT_SYMTAB, NEO::Elf::SectionsNamesZebin::symtab, ArrayRef(reinterpret_cast(symbols), sizeof(symbols))); auto &relaHeader = elfEncoder.appendSection(NEO::Elf::SHT_RELA, NEO::Elf::SpecialSectionNames::relaPrefix.str() + NEO::Elf::SectionsNamesZebin::debugInfo.str(), ArrayRef(reinterpret_cast(debugRelocations), sizeof(debugRelocations))); relaHeader.info = debugInfoSectionIndex; @@ -177,11 +187,15 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) { *reinterpret_cast(ptrDebugInfo + debugRelocations[3].offset)); // if symbols points to other sections relocation is skipped - not text, data, debug - EXPECT_EQ(0U, *reinterpret_cast(ptrDebugInfo + debugRelocations[4].offset)); + EXPECT_EQ(*reinterpret_cast(debugInfo + debugRelocations[4].offset), + *reinterpret_cast(ptrDebugInfo + debugRelocations[4].offset)); // debug symbols with text segment name are offseted by corresponding segment's address EXPECT_EQ(segments.nameToSegMap["kernel"].address, *reinterpret_cast(ptrDebugInfo + debugRelocations[5].offset)); + + EXPECT_EQ(*reinterpret_cast(debugInfo + debugRelocations[6].offset), + *reinterpret_cast(ptrDebugInfo + debugRelocations[6].offset)); } else { EXPECT_EQ(zebin.sectionHeaders[i].header->size, sectionHeader->size); if (sectionHeader->size > 0U) {