Skip to content

Commit 5c120f5

Browse files
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 <[email protected]>
1 parent 06fa316 commit 5c120f5

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

shared/source/device_binary_format/debug_zebin.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,24 @@ void DebugZebinCreator::applyRelocation(uint64_t addr, uint64_t value, RELOC_TYP
7575
}
7676
}
7777

78+
bool DebugZebinCreator::isRelocTypeSupported(NEO::Elf::RELOC_TYPE_ZEBIN type) {
79+
return type == NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR ||
80+
type == NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR_32 ||
81+
type == NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR_32_HI;
82+
}
83+
7884
void DebugZebinCreator::applyRelocations() {
7985
std::string errors, warnings;
8086
auto elf = decodeElf(debugZebin, errors, warnings);
8187

8288
for (const auto &relocations : {elf.getDebugInfoRelocations(), elf.getRelocations()}) {
8389
for (const auto &reloc : relocations) {
8490

91+
auto relocType = static_cast<RELOC_TYPE_ZEBIN>(reloc.relocType);
92+
if (isRelocTypeSupported(relocType) == false) {
93+
continue;
94+
}
95+
8596
uint64_t symbolOffset = 0U;
8697
auto symbolSectionName = elf.getSectionName(reloc.symbolSectionIndex);
8798
if (auto segment = getSegmentByName(symbolSectionName)) {
@@ -97,8 +108,7 @@ void DebugZebinCreator::applyRelocations() {
97108

98109
uint64_t relocVal = symbolOffset + elf.getSymbolValue(reloc.symbolTableIndex) + reloc.addend;
99110
auto relocAddr = reinterpret_cast<uint64_t>(debugZebin.data()) + elf.getSectionOffset(reloc.targetSectionIndex) + reloc.offset;
100-
auto type = static_cast<RELOC_TYPE_ZEBIN>(reloc.relocType);
101-
applyRelocation(relocAddr, relocVal, type);
111+
applyRelocation(relocAddr, relocVal, relocType);
102112
}
103113
}
104114
}

shared/source/device_binary_format/debug_zebin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class DebugZebinCreator {
4949
void applyRelocation(uint64_t addr, uint64_t value, NEO::Elf::RELOC_TYPE_ZEBIN type);
5050
const Segments::Segment *getSegmentByName(ConstStringRef sectionName);
5151
const Segments::Segment *getTextSegmentByName(ConstStringRef sectionName);
52+
bool isRelocTypeSupported(NEO::Elf::RELOC_TYPE_ZEBIN type);
5253

5354
std::vector<uint8_t> debugZebin;
5455
const Segments &segments;

shared/test/unit_test/device_binary_format/zebin_debug_binary_tests.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
2020
uint8_t kernelISA[8] = {0x3};
2121
uint8_t stringData[8] = {0x4};
2222

23-
uint8_t debugInfo[0x28] = {0x0};
23+
uint8_t debugInfo[0x30] = {0x22};
2424
uint8_t debugAbbrev[8] = {0x0};
2525

2626
using Segment = NEO::Debug::Segments::Segment;
@@ -43,7 +43,7 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
4343
typedef NEO::Elf::ElfSymbolEntry<NEO::Elf::ELF_IDENTIFIER_CLASS::EI_CLASS_64> SymbolEntry;
4444
typedef NEO::Elf::ElfRela<NEO::Elf::ELF_IDENTIFIER_CLASS::EI_CLASS_64> Relocation;
4545

46-
SymbolEntry symbols[6]{};
46+
SymbolEntry symbols[7]{};
4747
symbols[0].name = elfEncoder.appendSectionName("kernel");
4848
symbols[0].info = NEO::Elf::SYMBOL_TABLE_TYPE::STT_SECTION | NEO::Elf::SYMBOL_TABLE_BIND::STB_LOCAL << 4;
4949
symbols[0].shndx = static_cast<decltype(SymbolEntry::shndx)>(kernelSectionIndex);
@@ -74,7 +74,12 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
7474
symbols[5].shndx = static_cast<decltype(SymbolEntry::shndx)>(debugInfoSectionIndex);
7575
symbols[5].value = 0U;
7676

77-
Relocation debugRelocations[6]{};
77+
symbols[6].name = elfEncoder.appendSectionName("kernel_payload_offset");
78+
symbols[6].info = NEO::Elf::SYMBOL_TABLE_TYPE::STT_SECTION | NEO::Elf::SYMBOL_TABLE_BIND::STB_LOCAL << 4;
79+
symbols[6].shndx = static_cast<decltype(SymbolEntry::shndx)>(kernelSectionIndex);
80+
symbols[6].value = 0x10U;
81+
82+
Relocation debugRelocations[7]{};
7883
debugRelocations[0].addend = 0xabc;
7984
debugRelocations[0].offset = 0x0;
8085
debugRelocations[0].info = (uint64_t(0) << 32) | NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR;
@@ -100,6 +105,11 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
100105
debugRelocations[5].offset = 0x20U;
101106
debugRelocations[5].info = (uint64_t(5) << 32) | NEO::Elf::RELOC_TYPE_ZEBIN::R_ZE_SYM_ADDR;
102107

108+
// Will be ignored due to reloc type
109+
debugRelocations[6].addend = 0x0;
110+
debugRelocations[6].offset = 0x28;
111+
debugRelocations[6].info = (uint64_t(6) << 32) | NEO::Elf::RELOC_TYPE_ZEBIN::R_PER_THREAD_PAYLOAD_OFFSET;
112+
103113
elfEncoder.appendSection(NEO::Elf::SHT_SYMTAB, NEO::Elf::SectionsNamesZebin::symtab, ArrayRef<const uint8_t>(reinterpret_cast<uint8_t *>(symbols), sizeof(symbols)));
104114
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)));
105115
relaHeader.info = debugInfoSectionIndex;
@@ -177,11 +187,15 @@ TEST(DebugZebinTest, givenValidZebinThenDebugZebinIsGenerated) {
177187
*reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[3].offset));
178188

179189
// if symbols points to other sections relocation is skipped - not text, data, debug
180-
EXPECT_EQ(0U, *reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[4].offset));
190+
EXPECT_EQ(*reinterpret_cast<uint64_t *>(debugInfo + debugRelocations[4].offset),
191+
*reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[4].offset));
181192

182193
// debug symbols with text segment name are offseted by corresponding segment's address
183194
EXPECT_EQ(segments.nameToSegMap["kernel"].address,
184195
*reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[5].offset));
196+
197+
EXPECT_EQ(*reinterpret_cast<uint64_t *>(debugInfo + debugRelocations[6].offset),
198+
*reinterpret_cast<const uint64_t *>(ptrDebugInfo + debugRelocations[6].offset));
185199
} else {
186200
EXPECT_EQ(zebin.sectionHeaders[i].header->size, sectionHeader->size);
187201
if (sectionHeader->size > 0U) {

0 commit comments

Comments
 (0)