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 Error ELFBuilder::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::Elf_Shdr_Range> Sections = + ElfFile.sections(); + if (!Sections) + return Sections.takeError(); + + for (const typename ELFFile::Elf_Shdr &Shdr : *Sections) { + if (Index == 0) { + ++Index; + continue; + } + Expected 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) { if (Expected> Data = ElfFile.getSectionContents(Shdr)) return Obj.addSection(*Data); else @@ -1900,8 +1914,6 @@ } template Error ELFBuilder::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);