[PATCH] D111181: llvm-objcopy][ELF] Don't assume RELA sections are dynamic if they carry the SHF_ALLOC flag

Ard Biesheuvel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 5 22:55:13 PDT 2021


ardb updated this revision to Diff 377432.
ardb added a comment.

Refine the check so that

- ET_REL file types are assumed to only have static relocation sections
- ET_DYN/ET_EXEC file types are permitted to have either, and the disambiguation is based on the SHF_ALLOC flag as before


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

https://reviews.llvm.org/D111181

Files:
  llvm/tools/llvm-objcopy/ELF/Object.cpp


Index: llvm/tools/llvm-objcopy/ELF/Object.cpp
===================================================================
--- llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -1415,12 +1415,26 @@
 }
 
 template <class ELFT> Error ELFBuilder<ELFT>::findEhdrOffset() {
+  uint32_t Index = 0;
   if (!ExtractPartition)
     return Error::success();
 
-  for (const SectionBase &Sec : Obj.sections()) {
-    if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) {
-      EhdrOffset = Sec.Offset;
+  Expected<typename ELFFile<ELFT>::Elf_Shdr_Range> Sections =
+      ElfFile.sections();
+  if (!Sections)
+    return Sections.takeError();
+
+  for (const typename ELFFile<ELFT>::Elf_Shdr &Shdr : *Sections) {
+    if (Index == 0) {
+      ++Index;
+      continue;
+    }
+    Expected<StringRef> SecName = ElfFile.getSectionName(Shdr);
+    if (!SecName)
+      return SecName.takeError();
+    if (Shdr.sh_type == SHT_LLVM_PART_EHDR &&
+        SecName->str() == *ExtractPartition) {
+      EhdrOffset = Shdr.sh_offset;
       return Error::success();
     }
   }
@@ -1692,7 +1706,7 @@
   switch (Shdr.sh_type) {
   case SHT_REL:
   case SHT_RELA:
-    if (Shdr.sh_flags & SHF_ALLOC) {
+    if (Obj.Type != ELF::ET_REL && (Shdr.sh_flags & SHF_ALLOC) ) {
       if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
         return Obj.addSection<DynamicRelocationSection>(*Data);
       else
@@ -1900,8 +1914,6 @@
 }
 
 template <class ELFT> Error ELFBuilder<ELFT>::build(bool EnsureSymtab) {
-  if (Error E = readSectionHeaders())
-    return E;
   if (Error E = findEhdrOffset())
     return E;
 
@@ -1922,6 +1934,8 @@
   Obj.Entry = Ehdr.e_entry;
   Obj.Flags = Ehdr.e_flags;
 
+  if (Error E = readSectionHeaders())
+    return E;
   if (Error E = readSections(EnsureSymtab))
     return E;
   return readProgramHeaders(*HeadersFile);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111181.377432.patch
Type: text/x-patch
Size: 1898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211006/6384386d/attachment.bin>


More information about the llvm-commits mailing list