[PATCH] D102296: [ELF] getRelocatedSection: remove the check for ET_REL object file

Amir Ayupov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 26 18:30:42 PDT 2021


Amir updated this revision to Diff 348140.
Amir added a comment.

Removed unneeded includes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102296/new/

https://reviews.llvm.org/D102296

Files:
  llvm/include/llvm/Object/ELFObjectFile.h
  llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
  llvm/unittests/Object/ELFObjectFileTest.cpp


Index: llvm/unittests/Object/ELFObjectFileTest.cpp
===================================================================
--- llvm/unittests/Object/ELFObjectFileTest.cpp
+++ llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -590,3 +590,63 @@
   DoCheck(OverLimitNumBlocks,
           "ULEB128 value at offset 0x8 exceeds UINT32_MAX (0x100000000)");
 }
+
+// Test for ObjectFile::getRelocatedSection: check that it returns relocated
+// section for executable file.
+TEST(ELFObjectFileTest, ExecutableWithRelocs) {
+  SmallString<0> Storage;
+  Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    Info:            .text
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .text
+      - Name:            .rela.text
+      - Name:            .symtab
+      - Name:            .strtab
+      - Name:            .shstrtab
+Symbols:
+  - Name:            .text
+    Type:            STT_SECTION
+    Section:         .text
+)");
+
+  ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
+  const ELFObjectFile<ELF64LE> &Obj = *ElfOrErr;
+
+  bool FoundText;
+  bool FoundRela;
+
+  for (SectionRef Sec : Obj.sections()) {
+    Expected<StringRef> SecNameOrErr = Sec.getName();
+    ASSERT_THAT_EXPECTED(SecNameOrErr, Succeeded());
+    StringRef SecName = *SecNameOrErr;
+    if (SecName != ".rela.text")
+      continue;
+    FoundRela = true;
+    Expected<section_iterator> RelSecOrErr = Sec.getRelocatedSection();
+    ASSERT_THAT_EXPECTED(RelSecOrErr, Succeeded());
+    section_iterator RelSec = *RelSecOrErr;
+    ASSERT_NE(RelSec, Obj.section_end());
+    ASSERT_TRUE(RelSec->isText());
+    Expected<StringRef> TextSecNameOrErr = RelSec->getName();
+    ASSERT_THAT_EXPECTED(TextSecNameOrErr, Succeeded());
+    StringRef TextSecName = *TextSecNameOrErr;
+    ASSERT_EQ(TextSecName, ".text");
+    FoundText = true;
+  }
+  ASSERT_TRUE(FoundText);
+  ASSERT_TRUE(FoundRela);
+}
Index: llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
===================================================================
--- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
+++ llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
@@ -1682,7 +1682,8 @@
       // Try to obtain an already relocated version of this section.
       // Else use the unrelocated section from the object file. We'll have to
       // apply relocations ourselves later.
-      section_iterator RelocatedSection = *SecOrErr;
+      section_iterator RelocatedSection =
+          Obj.isRelocatableObject() ? *SecOrErr : Obj.section_end();
       if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
         Expected<StringRef> E = Section.getContents();
         if (E)
Index: llvm/include/llvm/Object/ELFObjectFile.h
===================================================================
--- llvm/include/llvm/Object/ELFObjectFile.h
+++ llvm/include/llvm/Object/ELFObjectFile.h
@@ -968,9 +968,6 @@
 template <class ELFT>
 Expected<section_iterator>
 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
-  if (EF.getHeader().e_type != ELF::ET_REL)
-    return section_end();
-
   const Elf_Shdr *EShdr = getSection(Sec);
   uintX_t Type = EShdr->sh_type;
   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102296.348140.patch
Type: text/x-patch
Size: 3587 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210527/523446d7/attachment.bin>


More information about the llvm-commits mailing list