From 34be2209437e11a4844b209f4128766a88c70cea Mon Sep 17 00:00:00 2001 From: Krystian Chmielewski Date: Tue, 3 Aug 2021 13:54:24 +0000 Subject: [PATCH] Enable relocation from instruction to data seg Related-To: NEO-5833 Signed-off-by: Krystian Chmielewski --- shared/source/compiler_interface/linker.cpp | 18 +++++--------- .../compiler_interface/linker_tests.cpp | 24 +++++++++++++++---- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/shared/source/compiler_interface/linker.cpp b/shared/source/compiler_interface/linker.cpp index bdb8664132..3989d81d8e 100644 --- a/shared/source/compiler_interface/linker.cpp +++ b/shared/source/compiler_interface/linker.cpp @@ -136,7 +136,6 @@ bool LinkerInput::decodeRelocationTable(const void *data, uint32_t numEntries, u void LinkerInput::addDataRelocationInfo(const RelocationInfo &relocationInfo) { DEBUG_BREAK_IF((relocationInfo.relocationSegment != SegmentType::GlobalConstants) && (relocationInfo.relocationSegment != SegmentType::GlobalVariables)); - DEBUG_BREAK_IF((relocationInfo.symbolSegment != SegmentType::GlobalConstants) && (relocationInfo.symbolSegment != SegmentType::GlobalVariables)); DEBUG_BREAK_IF(relocationInfo.type == LinkerInput::RelocationInfo::Type::AddressHigh); this->traits.requiresPatchingOfGlobalVariablesBuffer |= (relocationInfo.relocationSegment == SegmentType::GlobalVariables); this->traits.requiresPatchingOfGlobalConstantsBuffer |= (relocationInfo.relocationSegment == SegmentType::GlobalConstants); @@ -190,17 +189,12 @@ void LinkerInput::decodeElfSymbolTableAndRelocations(Elf::Elf } else if (nameRef.startsWith(NEO::Elf::SpecialSectionNames::data.data())) { auto symbolSectionName = elf.getSectionName(reloc.symbolSectionIndex); auto symbolSegment = getSegmentForSection(symbolSectionName); - - if (symbolSegment == NEO::SegmentType::GlobalConstants || symbolSegment == NEO::SegmentType::GlobalVariables) { - relocationInfo.symbolSegment = symbolSegment; - auto relocationSegment = getSegmentForSection(nameRef); - - if (relocationSegment == NEO::SegmentType::GlobalConstants || relocationSegment == NEO::SegmentType::GlobalVariables) { - relocationInfo.relocationSegment = relocationSegment; - this->addDataRelocationInfo(relocationInfo); - } - } else { - DEBUG_BREAK_IF(true); + relocationInfo.symbolSegment = symbolSegment; + auto relocationSegment = getSegmentForSection(nameRef); + if (symbolSegment != NEO::SegmentType::Unknown && + (relocationSegment == NEO::SegmentType::GlobalConstants || relocationSegment == NEO::SegmentType::GlobalVariables)) { + relocationInfo.relocationSegment = relocationSegment; + this->addDataRelocationInfo(relocationInfo); } } } diff --git a/shared/test/unit_test/compiler_interface/linker_tests.cpp b/shared/test/unit_test/compiler_interface/linker_tests.cpp index e0e1bfc7b2..8431ee0272 100644 --- a/shared/test/unit_test/compiler_interface/linker_tests.cpp +++ b/shared/test/unit_test/compiler_interface/linker_tests.cpp @@ -604,11 +604,21 @@ TEST(LinkerInputTests, WhenDecodingElfGlobalDataRelocationsThenCorrectRelocation reloc2.relocType = static_cast(Elf::RELOCATION_X8664_TYPE::R_X8664_64); reloc2.symbolName = "symbol2"; reloc2.symbolSectionIndex = 2; - reloc2.symbolTableIndex = 0; + reloc2.symbolTableIndex = 1; reloc2.targetSectionIndex = 1; elf64.relocations.emplace_back(reloc2); + NEO::Elf::Elf::RelocationInfo reloc3; + reloc3.offset = 0; + reloc3.relocType = static_cast(Elf::RELOCATION_X8664_TYPE::R_X8664_64); + reloc3.symbolName = "symbol3"; + reloc3.symbolSectionIndex = 0; + reloc3.symbolTableIndex = 2; + reloc3.targetSectionIndex = 1; + + elf64.relocations.emplace_back(reloc3); + NEO::LinkerInput::SectionNameToSegmentIdMap nameToKernelId; nameToKernelId["abc"] = 0; @@ -616,7 +626,7 @@ TEST(LinkerInputTests, WhenDecodingElfGlobalDataRelocationsThenCorrectRelocation linkerInput.decodeElfSymbolTableAndRelocations(elf64, nameToKernelId); auto relocations = linkerInput.getDataRelocations(); - ASSERT_EQ(2u, relocations.size()); + ASSERT_EQ(3u, relocations.size()); EXPECT_EQ(64u, relocations[0].offset); EXPECT_EQ(NEO::SegmentType::GlobalVariables, relocations[0].relocationSegment); @@ -629,6 +639,12 @@ TEST(LinkerInputTests, WhenDecodingElfGlobalDataRelocationsThenCorrectRelocation EXPECT_EQ("symbol2", relocations[1].symbolName); EXPECT_EQ(NEO::SegmentType::GlobalConstants, relocations[1].symbolSegment); EXPECT_EQ(NEO::LinkerInput::RelocationInfo::Type::Address, relocations[1].type); + + EXPECT_EQ(0u, relocations[2].offset); + EXPECT_EQ(NEO::SegmentType::GlobalVariables, relocations[2].relocationSegment); + EXPECT_EQ("symbol3", relocations[2].symbolName); + EXPECT_EQ(NEO::SegmentType::Instructions, relocations[2].symbolSegment); + EXPECT_EQ(NEO::LinkerInput::RelocationInfo::Type::Address, relocations[2].type); } TEST(LinkerInputTests, WhenDecodingElfConstantDataRelocationsThenCorrectRelocationsAreAdded) { @@ -719,14 +735,14 @@ TEST(LinkerInputTests, GivenUnsupportedDataSegmentWhenDecodingElfDataRelocationT ASSERT_EQ(0u, relocations.size()); } -TEST(LinkerInputTests, GivenUnsupportedSymbolSegmentWhenDecodingElfDataRelocationThenNoRelocationsAreAddedAndDebugBreakIsCalled) { +TEST(LinkerInputTests, GivenUnsupportedSymbolSegmentWhenDecodingElfDataRelocationThenNoRelocationsAreAdded) { NEO::LinkerInput linkerInput = {}; NEO::Elf::ElfFileHeader header; MockElf elf64; elf64.elfFileHeader = &header; std::unordered_map sectionNames; - sectionNames[0] = ".text.abc"; + sectionNames[0] = ".unknown_section"; sectionNames[1] = ".data"; sectionNames[2] = ".data.const";