diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp --- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -618,27 +618,16 @@ // .rela.prefix.plt since GNU objcopy does so. const SectionBase *TargetSec = RelocSec->getSection(); if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) { - StringRef prefix; - switch (Sec.Type) { - case SHT_REL: - prefix = ".rel"; - break; - case SHT_RELA: - prefix = ".rela"; - break; - default: - llvm_unreachable("not a relocation section"); - } - // If the relocation section comes *after* the target section, we // don't add Config.AllocSectionsPrefix because we've already added // the prefix to TargetSec->Name. Otherwise, if the relocation // section comes *before* the target section, we add the prefix. if (PrefixedSections.count(TargetSec)) - Sec.Name = (prefix + TargetSec->Name).str(); + Sec.Name = (RelocSec->getNamePrefix() + TargetSec->Name).str(); else - Sec.Name = - (prefix + Config.AllocSectionsPrefix + TargetSec->Name).str(); + Sec.Name = (RelocSec->getNamePrefix() + Config.AllocSectionsPrefix + + TargetSec->Name) + .str(); } } } diff --git a/llvm/tools/llvm-objcopy/ELF/Object.h b/llvm/tools/llvm-objcopy/ELF/Object.h --- a/llvm/tools/llvm-objcopy/ELF/Object.h +++ b/llvm/tools/llvm-objcopy/ELF/Object.h @@ -745,6 +745,8 @@ const SectionBase *getSection() const { return SecToApplyRel; } void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } + StringRef getNamePrefix() const; + static bool classof(const SectionBase *S) { return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA; } diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp --- a/llvm/tools/llvm-objcopy/ELF/Object.cpp +++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -893,6 +893,17 @@ return Visitor.visit(*this); } +StringRef RelocationSectionBase::getNamePrefix() const { + switch (Type) { + case SHT_REL: + return ".rel"; + case SHT_RELA: + return ".rela"; + default: + llvm_unreachable("not a relocation section"); + } +} + Error RelocationSection::removeSectionReferences( bool AllowBrokenLinks, function_ref ToRemove) { if (ToRemove(Symbols)) {