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 <krystian.chmielewski@intel.com>
This commit is contained in:
Krystian Chmielewski
2022-04-19 14:15:46 +00:00
committed by Compute-Runtime-Automation
parent 06fa316a75
commit 5c120f5f2d
3 changed files with 31 additions and 6 deletions

View File

@@ -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_TYPE_ZEBIN>(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<uint64_t>(debugZebin.data()) + elf.getSectionOffset(reloc.targetSectionIndex) + reloc.offset;
auto type = static_cast<RELOC_TYPE_ZEBIN>(reloc.relocType);
applyRelocation(relocAddr, relocVal, type);
applyRelocation(relocAddr, relocVal, relocType);
}
}
}

View File

@@ -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<uint8_t> debugZebin;
const Segments &segments;

View File

@@ -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<NEO::Elf::ELF_IDENTIFIER_CLASS::EI_CLASS_64> SymbolEntry;
typedef NEO::Elf::ElfRela<NEO::Elf::ELF_IDENTIFIER_CLASS::EI_CLASS_64> 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<decltype(SymbolEntry::shndx)>(kernelSectionIndex);
@@ -74,7 +74,12 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
symbols[5].shndx = static_cast<decltype(SymbolEntry::shndx)>(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<decltype(SymbolEntry::shndx)>(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<const uint8_t>(reinterpret_cast<uint8_t *>(symbols), sizeof(symbols)));
auto &relaHeader = elfEncoder.appendSection(NEO::Elf::SHT_RELA, NEO::Elf::SpecialSectionNames::relaPrefix.str() + NEO::Elf::SectionsNamesZebin::debugInfo.str(), ArrayRef<const uint8_t>(reinterpret_cast<uint8_t *>(debugRelocations), sizeof(debugRelocations)));
relaHeader.info = debugInfoSectionIndex;
@@ -177,11 +187,15 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
*reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[3].offset));
// if symbols points to other sections relocation is skipped - not text, data, debug
EXPECT_EQ(0U, *reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[4].offset));
EXPECT_EQ(*reinterpret_cast<uint64_t *>(debugInfo + debugRelocations[4].offset),
*reinterpret_cast<const uint64_t *>(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<const uint64_t *>(ptrDebugInfo + debugRelocations[5].offset));
EXPECT_EQ(*reinterpret_cast<uint64_t *>(debugInfo + debugRelocations[6].offset),
*reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[6].offset));
} else {
EXPECT_EQ(zebin.sectionHeaders[i].header->size, sectionHeader->size);
if (sectionHeader->size > 0U) {