diff options
| author | James Henderson <jh7370@my.bristol.ac.uk> | 2019-05-08 09:49:35 +0000 |
|---|---|---|
| committer | James Henderson <jh7370@my.bristol.ac.uk> | 2019-05-08 09:49:35 +0000 |
| commit | fa11fb33ad6eecfe081a9875ed27b98a1077e404 (patch) | |
| tree | 98cbc44058252a00260779637347f428901aa287 /llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp | |
| parent | 3c696b3e7c21815662247e2af0346a596a4d0668 (diff) | |
| download | bcm5719-llvm-fa11fb33ad6eecfe081a9875ed27b98a1077e404.tar.gz bcm5719-llvm-fa11fb33ad6eecfe081a9875ed27b98a1077e404.zip | |
[llvm-objcopy] Add --prefix-alloc-sections
This patch adds support for --prefix-alloc-sections, which adds a prefix
to every allocated section names.
It adds a prefix after renaming section names by --rename-section as GNU
objcopy does.
Fixes PR41266: https://bugs.llvm.org/show_bug.cgi?id=41266
Differential Revision: https://reviews.llvm.org/D60042
Patch by Seiya Nuta.
llvm-svn: 360233
Diffstat (limited to 'llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp')
| -rw-r--r-- | llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp index 9b21190000e..bed2414559f 100644 --- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -13,6 +13,7 @@ #include "llvm-objcopy.h" #include "llvm/ADT/BitmaskEnum.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" @@ -580,7 +581,8 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj, if (Error E = updateAndRemoveSymbols(Config, Obj)) return E; - if (!Config.SectionsToRename.empty()) { + if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) { + DenseSet<SectionBase *> PrefixedSections; for (auto &Sec : Obj.sections()) { const auto Iter = Config.SectionsToRename.find(Sec.Name); if (Iter != Config.SectionsToRename.end()) { @@ -589,6 +591,60 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj, if (SR.NewFlags.hasValue()) setSectionFlagsAndType(Sec, SR.NewFlags.getValue()); } + + // Add a prefix to allocated sections and their relocation sections. This + // should be done after renaming the section by Config.SectionToRename to + // imitate the GNU objcopy behavior. + if (!Config.AllocSectionsPrefix.empty()) { + if (Sec.Flags & SHF_ALLOC) { + Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str(); + PrefixedSections.insert(&Sec); + + // Rename relocation sections associated to the allocated sections. + // For example, if we rename .text to .prefix.text, we also rename + // .rel.text to .rel.prefix.text. + // + // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled + // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not + // .rela.prefix.plt since GNU objcopy does so. + } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) { + auto *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: + continue; + } + + // 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(); + } else { + const auto Iter = Config.SectionsToRename.find(TargetSec->Name); + if (Iter != Config.SectionsToRename.end()) { + // Both `--rename-section` and `--prefix-alloc-sections` are + // given but the target section is not yet renamed. + Sec.Name = + (prefix + Config.AllocSectionsPrefix + Iter->second.NewName) + .str(); + } else { + Sec.Name = + (prefix + Config.AllocSectionsPrefix + TargetSec->Name) + .str(); + } + } + } + } + } } } |

