[llvm-objcopy][NFC] Add a helper method RelocationSectionBase::getNamePrefix()

Refactor handleArgs() to use that method.

Differential Revision: https://reviews.llvm.org/D110350
This commit is contained in:
Igor Kudrin 2021-09-24 22:02:36 +07:00
parent 1376ae9094
commit 6dda6c49ce
3 changed files with 17 additions and 15 deletions

View File

@ -618,27 +618,16 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
// .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();
}
}
}

View File

@ -893,6 +893,17 @@ Error SymbolTableSection::accept(MutableSectionVisitor &Visitor) {
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<bool(const SectionBase *)> ToRemove) {
if (ToRemove(Symbols)) {

View File

@ -745,6 +745,8 @@ public:
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;
}