From c046824c183da8f365f8f742537b21eb70b5628d Mon Sep 17 00:00:00 2001 From: Kacper Nowak Date: Mon, 25 Jul 2022 17:31:45 +0000 Subject: [PATCH] Linker: do not process relocations for symbols with unknown segment type In decodeElfSymbolTableAndRelocations, when symbol's section is of unknown type, then do not add it to linker's symbol table. Signed-off-by: Kacper Nowak --- shared/source/compiler_interface/linker.cpp | 4 ++- .../compiler_interface/linker_tests.cpp | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/shared/source/compiler_interface/linker.cpp b/shared/source/compiler_interface/linker.cpp index 7dbf4c7e43d6a..546abb557a379 100644 --- a/shared/source/compiler_interface/linker.cpp +++ b/shared/source/compiler_interface/linker.cpp @@ -177,7 +177,9 @@ void LinkerInput::decodeElfSymbolTableAndRelocations(Elf::Elf auto symbolSectionName = elf.getSectionName(symbol.shndx); auto symbolSegment = getSegmentForSection(symbolSectionName); - + if (NEO::SegmentType::Unknown == symbolSegment) { + continue; + } switch (type) { default: DEBUG_BREAK_IF(type != Elf::SYMBOL_TABLE_TYPE::STT_NOTYPE); diff --git a/shared/test/unit_test/compiler_interface/linker_tests.cpp b/shared/test/unit_test/compiler_interface/linker_tests.cpp index 596ad0a730366..0bfd625695619 100644 --- a/shared/test/unit_test/compiler_interface/linker_tests.cpp +++ b/shared/test/unit_test/compiler_interface/linker_tests.cpp @@ -514,6 +514,34 @@ TEST(LinkerInputTests, GivenInvalidTextSectionNameWhenDecodingElfTextRelocations ASSERT_EQ(0u, relocations.size()); } +TEST(LinkerInputTests, whenSymbolHasShndxReferringToSectionOfUnknownTypehenDoNotAddItToLinkerInput) { + NEO::LinkerInput linkerInput = {}; + NEO::Elf::ElfFileHeader header; + MockElf elf64; + elf64.elfFileHeader = &header; + + std::unordered_map sectionNames; + sectionNames[0] = ".invalid.aaa"; + elf64.setupSecionNames(std::move(sectionNames)); + elf64.overrideSymbolName = true; + + NEO::Elf::ElfSymbolEntry symbol; + symbol.info = NEO::Elf::SYMBOL_TABLE_TYPE::STT_OBJECT | NEO::Elf::SYMBOL_TABLE_BIND::STB_GLOBAL << 4; + symbol.name = 0x20; + symbol.other = 0; + symbol.shndx = 0; + symbol.size = 0x8; + symbol.value = 0x4000; + + elf64.symbolTable.emplace_back(symbol); + + NEO::LinkerInput::SectionNameToSegmentIdMap nameToKernelId; + linkerInput.decodeElfSymbolTableAndRelocations(elf64, nameToKernelId); + + auto symbols = linkerInput.getSymbols(); + ASSERT_EQ(0u, symbols.size()); +} + TEST(LinkerInputTests, GivenValidZebinRelocationTypesWhenDecodingElfTextRelocationsThenCorrectTypeIsSet) { NEO::LinkerInput linkerInput = {}; NEO::Elf::ElfFileHeader header;