Skip to content

Commit 27f2b81

Browse files
committedSep 11, 2019
[llvm-objcopy] Simplify --prefix-alloc-sections
Handle --prefix-alloc-sections after --rename-sections so that --prefix-alloc-sections code does not have to check if renaming has been performed. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D66931 llvm-svn: 371591
1 parent 1d68ed8 commit 27f2b81

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed
 

‎llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

+43-53
Original file line numberDiff line numberDiff line change
@@ -613,68 +613,58 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
613613
if (Error E = updateAndRemoveSymbols(Config, Obj))
614614
return E;
615615

616-
if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) {
617-
DenseSet<SectionBase *> PrefixedSections;
618-
for (auto &Sec : Obj.sections()) {
616+
if (!Config.SectionsToRename.empty()) {
617+
for (SectionBase &Sec : Obj.sections()) {
619618
const auto Iter = Config.SectionsToRename.find(Sec.Name);
620619
if (Iter != Config.SectionsToRename.end()) {
621620
const SectionRename &SR = Iter->second;
622621
Sec.Name = SR.NewName;
623622
if (SR.NewFlags.hasValue())
624623
setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
625624
}
625+
}
626+
}
626627

627-
// Add a prefix to allocated sections and their relocation sections. This
628-
// should be done after renaming the section by Config.SectionToRename to
629-
// imitate the GNU objcopy behavior.
630-
if (!Config.AllocSectionsPrefix.empty()) {
631-
if (Sec.Flags & SHF_ALLOC) {
632-
Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
633-
PrefixedSections.insert(&Sec);
634-
635-
// Rename relocation sections associated to the allocated sections.
636-
// For example, if we rename .text to .prefix.text, we also rename
637-
// .rel.text to .rel.prefix.text.
638-
//
639-
// Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
640-
// above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
641-
// .rela.prefix.plt since GNU objcopy does so.
642-
} else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
643-
auto *TargetSec = RelocSec->getSection();
644-
if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
645-
StringRef prefix;
646-
switch (Sec.Type) {
647-
case SHT_REL:
648-
prefix = ".rel";
649-
break;
650-
case SHT_RELA:
651-
prefix = ".rela";
652-
break;
653-
default:
654-
continue;
655-
}
656-
657-
// If the relocation section comes *after* the target section, we
658-
// don't add Config.AllocSectionsPrefix because we've already added
659-
// the prefix to TargetSec->Name. Otherwise, if the relocation
660-
// section comes *before* the target section, we add the prefix.
661-
if (PrefixedSections.count(TargetSec)) {
662-
Sec.Name = (prefix + TargetSec->Name).str();
663-
} else {
664-
const auto Iter = Config.SectionsToRename.find(TargetSec->Name);
665-
if (Iter != Config.SectionsToRename.end()) {
666-
// Both `--rename-section` and `--prefix-alloc-sections` are
667-
// given but the target section is not yet renamed.
668-
Sec.Name =
669-
(prefix + Config.AllocSectionsPrefix + Iter->second.NewName)
670-
.str();
671-
} else {
672-
Sec.Name =
673-
(prefix + Config.AllocSectionsPrefix + TargetSec->Name)
674-
.str();
675-
}
676-
}
628+
// Add a prefix to allocated sections and their relocation sections. This
629+
// should be done after renaming the section by Config.SectionToRename to
630+
// imitate the GNU objcopy behavior.
631+
if (!Config.AllocSectionsPrefix.empty()) {
632+
DenseSet<SectionBase *> PrefixedSections;
633+
for (SectionBase &Sec : Obj.sections()) {
634+
if (Sec.Flags & SHF_ALLOC) {
635+
Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
636+
PrefixedSections.insert(&Sec);
637+
} else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
638+
// Rename relocation sections associated to the allocated sections.
639+
// For example, if we rename .text to .prefix.text, we also rename
640+
// .rel.text to .rel.prefix.text.
641+
//
642+
// Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
643+
// above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
644+
// .rela.prefix.plt since GNU objcopy does so.
645+
const SectionBase *TargetSec = RelocSec->getSection();
646+
if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
647+
StringRef prefix;
648+
switch (Sec.Type) {
649+
case SHT_REL:
650+
prefix = ".rel";
651+
break;
652+
case SHT_RELA:
653+
prefix = ".rela";
654+
break;
655+
default:
656+
llvm_unreachable("not a relocation section");
677657
}
658+
659+
// If the relocation section comes *after* the target section, we
660+
// don't add Config.AllocSectionsPrefix because we've already added
661+
// the prefix to TargetSec->Name. Otherwise, if the relocation
662+
// section comes *before* the target section, we add the prefix.
663+
if (PrefixedSections.count(TargetSec))
664+
Sec.Name = (prefix + TargetSec->Name).str();
665+
else
666+
Sec.Name =
667+
(prefix + Config.AllocSectionsPrefix + TargetSec->Name).str();
678668
}
679669
}
680670
}

0 commit comments

Comments
 (0)
Please sign in to comment.