Home | History | Annotate | Line # | Download | only in ELF
      1 //===- ELFObjcopy.cpp -----------------------------------------------------===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 
      9 #include "ELFObjcopy.h"
     10 #include "CommonConfig.h"
     11 #include "ELFConfig.h"
     12 #include "Object.h"
     13 #include "llvm-objcopy.h"
     14 #include "llvm/ADT/BitmaskEnum.h"
     15 #include "llvm/ADT/DenseSet.h"
     16 #include "llvm/ADT/Optional.h"
     17 #include "llvm/ADT/STLExtras.h"
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/ADT/StringRef.h"
     20 #include "llvm/ADT/Twine.h"
     21 #include "llvm/BinaryFormat/ELF.h"
     22 #include "llvm/MC/MCTargetOptions.h"
     23 #include "llvm/Object/Binary.h"
     24 #include "llvm/Object/ELFObjectFile.h"
     25 #include "llvm/Object/ELFTypes.h"
     26 #include "llvm/Object/Error.h"
     27 #include "llvm/Option/Option.h"
     28 #include "llvm/Support/Casting.h"
     29 #include "llvm/Support/Compression.h"
     30 #include "llvm/Support/Errc.h"
     31 #include "llvm/Support/Error.h"
     32 #include "llvm/Support/ErrorHandling.h"
     33 #include "llvm/Support/ErrorOr.h"
     34 #include "llvm/Support/FileSystem.h"
     35 #include "llvm/Support/Memory.h"
     36 #include "llvm/Support/Path.h"
     37 #include "llvm/Support/raw_ostream.h"
     38 #include <algorithm>
     39 #include <cassert>
     40 #include <cstdlib>
     41 #include <functional>
     42 #include <iterator>
     43 #include <memory>
     44 #include <string>
     45 #include <system_error>
     46 #include <utility>
     47 
     48 namespace llvm {
     49 namespace objcopy {
     50 namespace elf {
     51 
     52 using namespace object;
     53 using namespace ELF;
     54 using SectionPred = std::function<bool(const SectionBase &Sec)>;
     55 
     56 static bool isDebugSection(const SectionBase &Sec) {
     57   return StringRef(Sec.Name).startswith(".debug") ||
     58          StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
     59 }
     60 
     61 static bool isDWOSection(const SectionBase &Sec) {
     62   return StringRef(Sec.Name).endswith(".dwo");
     63 }
     64 
     65 static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
     66   // We can't remove the section header string table.
     67   if (&Sec == Obj.SectionNames)
     68     return false;
     69   // Short of keeping the string table we want to keep everything that is a DWO
     70   // section and remove everything else.
     71   return !isDWOSection(Sec);
     72 }
     73 
     74 uint64_t getNewShfFlags(SectionFlag AllFlags) {
     75   uint64_t NewFlags = 0;
     76   if (AllFlags & SectionFlag::SecAlloc)
     77     NewFlags |= ELF::SHF_ALLOC;
     78   if (!(AllFlags & SectionFlag::SecReadonly))
     79     NewFlags |= ELF::SHF_WRITE;
     80   if (AllFlags & SectionFlag::SecCode)
     81     NewFlags |= ELF::SHF_EXECINSTR;
     82   if (AllFlags & SectionFlag::SecMerge)
     83     NewFlags |= ELF::SHF_MERGE;
     84   if (AllFlags & SectionFlag::SecStrings)
     85     NewFlags |= ELF::SHF_STRINGS;
     86   if (AllFlags & SectionFlag::SecExclude)
     87     NewFlags |= ELF::SHF_EXCLUDE;
     88   return NewFlags;
     89 }
     90 
     91 static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags,
     92                                             uint64_t NewFlags) {
     93   // Preserve some flags which should not be dropped when setting flags.
     94   // Also, preserve anything OS/processor dependant.
     95   const uint64_t PreserveMask =
     96       (ELF::SHF_COMPRESSED | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
     97        ELF::SHF_MASKOS | ELF::SHF_MASKPROC | ELF::SHF_TLS |
     98        ELF::SHF_INFO_LINK) &
     99       ~ELF::SHF_EXCLUDE;
    100   return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
    101 }
    102 
    103 static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) {
    104   Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(Flags));
    105 
    106   // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule
    107   // may promote more non-ALLOC sections than GNU objcopy, but it is fine as
    108   // non-ALLOC SHT_NOBITS sections do not make much sense.
    109   if (Sec.Type == SHT_NOBITS &&
    110       (!(Sec.Flags & ELF::SHF_ALLOC) ||
    111        Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
    112     Sec.Type = SHT_PROGBITS;
    113 }
    114 
    115 static ElfType getOutputElfType(const Binary &Bin) {
    116   // Infer output ELF type from the input ELF object
    117   if (isa<ELFObjectFile<ELF32LE>>(Bin))
    118     return ELFT_ELF32LE;
    119   if (isa<ELFObjectFile<ELF64LE>>(Bin))
    120     return ELFT_ELF64LE;
    121   if (isa<ELFObjectFile<ELF32BE>>(Bin))
    122     return ELFT_ELF32BE;
    123   if (isa<ELFObjectFile<ELF64BE>>(Bin))
    124     return ELFT_ELF64BE;
    125   llvm_unreachable("Invalid ELFType");
    126 }
    127 
    128 static ElfType getOutputElfType(const MachineInfo &MI) {
    129   // Infer output ELF type from the binary arch specified
    130   if (MI.Is64Bit)
    131     return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
    132   else
    133     return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
    134 }
    135 
    136 static std::unique_ptr<Writer> createELFWriter(const CommonConfig &Config,
    137                                                Object &Obj, raw_ostream &Out,
    138                                                ElfType OutputElfType) {
    139   // Depending on the initial ELFT and OutputFormat we need a different Writer.
    140   switch (OutputElfType) {
    141   case ELFT_ELF32LE:
    142     return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !Config.StripSections,
    143                                                 Config.OnlyKeepDebug);
    144   case ELFT_ELF64LE:
    145     return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !Config.StripSections,
    146                                                 Config.OnlyKeepDebug);
    147   case ELFT_ELF32BE:
    148     return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !Config.StripSections,
    149                                                 Config.OnlyKeepDebug);
    150   case ELFT_ELF64BE:
    151     return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !Config.StripSections,
    152                                                 Config.OnlyKeepDebug);
    153   }
    154   llvm_unreachable("Invalid output format");
    155 }
    156 
    157 static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
    158                                             Object &Obj, raw_ostream &Out,
    159                                             ElfType OutputElfType) {
    160   switch (Config.OutputFormat) {
    161   case FileFormat::Binary:
    162     return std::make_unique<BinaryWriter>(Obj, Out);
    163   case FileFormat::IHex:
    164     return std::make_unique<IHexWriter>(Obj, Out);
    165   default:
    166     return createELFWriter(Config, Obj, Out, OutputElfType);
    167   }
    168 }
    169 
    170 template <class... Ts>
    171 static Error makeStringError(std::error_code EC, const Twine &Msg,
    172                              Ts &&... Args) {
    173   std::string FullMsg = (EC.message() + ": " + Msg).str();
    174   return createStringError(EC, FullMsg.c_str(), std::forward<Ts>(Args)...);
    175 }
    176 
    177 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
    178                                Object &Obj) {
    179   for (auto &Sec : Obj.sections()) {
    180     if (Sec.Name == SecName) {
    181       if (Sec.Type == SHT_NOBITS)
    182         return createStringError(object_error::parse_failed,
    183                                  "cannot dump section '%s': it has no contents",
    184                                  SecName.str().c_str());
    185       Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
    186           FileOutputBuffer::create(Filename, Sec.OriginalData.size());
    187       if (!BufferOrErr)
    188         return BufferOrErr.takeError();
    189       std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
    190       std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
    191                 Buf->getBufferStart());
    192       if (Error E = Buf->commit())
    193         return E;
    194       return Error::success();
    195     }
    196   }
    197   return createStringError(object_error::parse_failed, "section '%s' not found",
    198                            SecName.str().c_str());
    199 }
    200 
    201 static bool isCompressable(const SectionBase &Sec) {
    202   return !(Sec.Flags & ELF::SHF_COMPRESSED) &&
    203          StringRef(Sec.Name).startswith(".debug");
    204 }
    205 
    206 static Error replaceDebugSections(
    207     Object &Obj, SectionPred &RemovePred,
    208     function_ref<bool(const SectionBase &)> ShouldReplace,
    209     function_ref<Expected<SectionBase *>(const SectionBase *)> AddSection) {
    210   // Build a list of the debug sections we are going to replace.
    211   // We can't call `AddSection` while iterating over sections,
    212   // because it would mutate the sections array.
    213   SmallVector<SectionBase *, 13> ToReplace;
    214   for (auto &Sec : Obj.sections())
    215     if (ShouldReplace(Sec))
    216       ToReplace.push_back(&Sec);
    217 
    218   // Build a mapping from original section to a new one.
    219   DenseMap<SectionBase *, SectionBase *> FromTo;
    220   for (SectionBase *S : ToReplace) {
    221     Expected<SectionBase *> NewSection = AddSection(S);
    222     if (!NewSection)
    223       return NewSection.takeError();
    224 
    225     FromTo[S] = *NewSection;
    226   }
    227 
    228   // Now we want to update the target sections of relocation
    229   // sections. Also we will update the relocations themselves
    230   // to update the symbol references.
    231   for (auto &Sec : Obj.sections())
    232     Sec.replaceSectionReferences(FromTo);
    233 
    234   RemovePred = [ShouldReplace, RemovePred](const SectionBase &Sec) {
    235     return ShouldReplace(Sec) || RemovePred(Sec);
    236   };
    237 
    238   return Error::success();
    239 }
    240 
    241 static bool isUnneededSymbol(const Symbol &Sym) {
    242   return !Sym.Referenced &&
    243          (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
    244          Sym.Type != STT_SECTION;
    245 }
    246 
    247 static Error updateAndRemoveSymbols(const CommonConfig &Config, Object &Obj) {
    248   // TODO: update or remove symbols only if there is an option that affects
    249   // them.
    250   if (!Obj.SymbolTable)
    251     return Error::success();
    252 
    253   Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
    254     // Common and undefined symbols don't make sense as local symbols, and can
    255     // even cause crashes if we localize those, so skip them.
    256     if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
    257         ((Config.LocalizeHidden &&
    258           (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
    259          Config.SymbolsToLocalize.matches(Sym.Name)))
    260       Sym.Binding = STB_LOCAL;
    261 
    262     // Note: these two globalize flags have very similar names but different
    263     // meanings:
    264     //
    265     // --globalize-symbol: promote a symbol to global
    266     // --keep-global-symbol: all symbols except for these should be made local
    267     //
    268     // If --globalize-symbol is specified for a given symbol, it will be
    269     // global in the output file even if it is not included via
    270     // --keep-global-symbol. Because of that, make sure to check
    271     // --globalize-symbol second.
    272     if (!Config.SymbolsToKeepGlobal.empty() &&
    273         !Config.SymbolsToKeepGlobal.matches(Sym.Name) &&
    274         Sym.getShndx() != SHN_UNDEF)
    275       Sym.Binding = STB_LOCAL;
    276 
    277     if (Config.SymbolsToGlobalize.matches(Sym.Name) &&
    278         Sym.getShndx() != SHN_UNDEF)
    279       Sym.Binding = STB_GLOBAL;
    280 
    281     if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding == STB_GLOBAL)
    282       Sym.Binding = STB_WEAK;
    283 
    284     if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
    285         Sym.getShndx() != SHN_UNDEF)
    286       Sym.Binding = STB_WEAK;
    287 
    288     const auto I = Config.SymbolsToRename.find(Sym.Name);
    289     if (I != Config.SymbolsToRename.end())
    290       Sym.Name = std::string(I->getValue());
    291 
    292     if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
    293       Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
    294   });
    295 
    296   // The purpose of this loop is to mark symbols referenced by sections
    297   // (like GroupSection or RelocationSection). This way, we know which
    298   // symbols are still 'needed' and which are not.
    299   if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
    300       !Config.OnlySection.empty()) {
    301     for (SectionBase &Sec : Obj.sections())
    302       Sec.markSymbols();
    303   }
    304 
    305   auto RemoveSymbolsPred = [&](const Symbol &Sym) {
    306     if (Config.SymbolsToKeep.matches(Sym.Name) ||
    307         (Config.KeepFileSymbols && Sym.Type == STT_FILE))
    308       return false;
    309 
    310     if ((Config.DiscardMode == DiscardType::All ||
    311          (Config.DiscardMode == DiscardType::Locals &&
    312           StringRef(Sym.Name).startswith(".L"))) &&
    313         Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
    314         Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
    315       return true;
    316 
    317     if (Config.StripAll || Config.StripAllGNU)
    318       return true;
    319 
    320     if (Config.StripDebug && Sym.Type == STT_FILE)
    321       return true;
    322 
    323     if (Config.SymbolsToRemove.matches(Sym.Name))
    324       return true;
    325 
    326     if ((Config.StripUnneeded ||
    327          Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
    328         (!Obj.isRelocatable() || isUnneededSymbol(Sym)))
    329       return true;
    330 
    331     // We want to remove undefined symbols if all references have been stripped.
    332     if (!Config.OnlySection.empty() && !Sym.Referenced &&
    333         Sym.getShndx() == SHN_UNDEF)
    334       return true;
    335 
    336     return false;
    337   };
    338 
    339   return Obj.removeSymbols(RemoveSymbolsPred);
    340 }
    341 
    342 static Error replaceAndRemoveSections(const CommonConfig &Config, Object &Obj) {
    343   SectionPred RemovePred = [](const SectionBase &) { return false; };
    344 
    345   // Removes:
    346   if (!Config.ToRemove.empty()) {
    347     RemovePred = [&Config](const SectionBase &Sec) {
    348       return Config.ToRemove.matches(Sec.Name);
    349     };
    350   }
    351 
    352   if (Config.StripDWO)
    353     RemovePred = [RemovePred](const SectionBase &Sec) {
    354       return isDWOSection(Sec) || RemovePred(Sec);
    355     };
    356 
    357   if (Config.ExtractDWO)
    358     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
    359       return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
    360     };
    361 
    362   if (Config.StripAllGNU)
    363     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
    364       if (RemovePred(Sec))
    365         return true;
    366       if ((Sec.Flags & SHF_ALLOC) != 0)
    367         return false;
    368       if (&Sec == Obj.SectionNames)
    369         return false;
    370       switch (Sec.Type) {
    371       case SHT_SYMTAB:
    372       case SHT_REL:
    373       case SHT_RELA:
    374       case SHT_STRTAB:
    375         return true;
    376       }
    377       return isDebugSection(Sec);
    378     };
    379 
    380   if (Config.StripSections) {
    381     RemovePred = [RemovePred](const SectionBase &Sec) {
    382       return RemovePred(Sec) || Sec.ParentSegment == nullptr;
    383     };
    384   }
    385 
    386   if (Config.StripDebug || Config.StripUnneeded) {
    387     RemovePred = [RemovePred](const SectionBase &Sec) {
    388       return RemovePred(Sec) || isDebugSection(Sec);
    389     };
    390   }
    391 
    392   if (Config.StripNonAlloc)
    393     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
    394       if (RemovePred(Sec))
    395         return true;
    396       if (&Sec == Obj.SectionNames)
    397         return false;
    398       return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
    399     };
    400 
    401   if (Config.StripAll)
    402     RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
    403       if (RemovePred(Sec))
    404         return true;
    405       if (&Sec == Obj.SectionNames)
    406         return false;
    407       if (StringRef(Sec.Name).startswith(".gnu.warning"))
    408         return false;
    409       // We keep the .ARM.attribute section to maintain compatibility
    410       // with Debian derived distributions. This is a bug in their
    411       // patchset as documented here:
    412       // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=943798
    413       if (Sec.Type == SHT_ARM_ATTRIBUTES)
    414         return false;
    415       if (Sec.ParentSegment != nullptr)
    416         return false;
    417       return (Sec.Flags & SHF_ALLOC) == 0;
    418     };
    419 
    420   if (Config.ExtractPartition || Config.ExtractMainPartition) {
    421     RemovePred = [RemovePred](const SectionBase &Sec) {
    422       if (RemovePred(Sec))
    423         return true;
    424       if (Sec.Type == SHT_LLVM_PART_EHDR || Sec.Type == SHT_LLVM_PART_PHDR)
    425         return true;
    426       return (Sec.Flags & SHF_ALLOC) != 0 && !Sec.ParentSegment;
    427     };
    428   }
    429 
    430   // Explicit copies:
    431   if (!Config.OnlySection.empty()) {
    432     RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
    433       // Explicitly keep these sections regardless of previous removes.
    434       if (Config.OnlySection.matches(Sec.Name))
    435         return false;
    436 
    437       // Allow all implicit removes.
    438       if (RemovePred(Sec))
    439         return true;
    440 
    441       // Keep special sections.
    442       if (Obj.SectionNames == &Sec)
    443         return false;
    444       if (Obj.SymbolTable == &Sec ||
    445           (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
    446         return false;
    447 
    448       // Remove everything else.
    449       return true;
    450     };
    451   }
    452 
    453   if (!Config.KeepSection.empty()) {
    454     RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
    455       // Explicitly keep these sections regardless of previous removes.
    456       if (Config.KeepSection.matches(Sec.Name))
    457         return false;
    458       // Otherwise defer to RemovePred.
    459       return RemovePred(Sec);
    460     };
    461   }
    462 
    463   // This has to be the last predicate assignment.
    464   // If the option --keep-symbol has been specified
    465   // and at least one of those symbols is present
    466   // (equivalently, the updated symbol table is not empty)
    467   // the symbol table and the string table should not be removed.
    468   if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
    469       Obj.SymbolTable && !Obj.SymbolTable->empty()) {
    470     RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
    471       if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
    472         return false;
    473       return RemovePred(Sec);
    474     };
    475   }
    476 
    477   if (Config.CompressionType != DebugCompressionType::None) {
    478     if (Error Err = replaceDebugSections(
    479             Obj, RemovePred, isCompressable,
    480             [&Config, &Obj](const SectionBase *S) -> Expected<SectionBase *> {
    481               Expected<CompressedSection> NewSection =
    482                   CompressedSection::create(*S, Config.CompressionType);
    483               if (!NewSection)
    484                 return NewSection.takeError();
    485 
    486               return &Obj.addSection<CompressedSection>(std::move(*NewSection));
    487             }))
    488       return Err;
    489   } else if (Config.DecompressDebugSections) {
    490     if (Error Err = replaceDebugSections(
    491             Obj, RemovePred,
    492             [](const SectionBase &S) { return isa<CompressedSection>(&S); },
    493             [&Obj](const SectionBase *S) {
    494               const CompressedSection *CS = cast<CompressedSection>(S);
    495               return &Obj.addSection<DecompressedSection>(*CS);
    496             }))
    497       return Err;
    498   }
    499 
    500   return Obj.removeSections(Config.AllowBrokenLinks, RemovePred);
    501 }
    502 
    503 // This function handles the high level operations of GNU objcopy including
    504 // handling command line options. It's important to outline certain properties
    505 // we expect to hold of the command line operations. Any operation that "keeps"
    506 // should keep regardless of a remove. Additionally any removal should respect
    507 // any previous removals. Lastly whether or not something is removed shouldn't
    508 // depend a) on the order the options occur in or b) on some opaque priority
    509 // system. The only priority is that keeps/copies overrule removes.
    510 static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
    511                         Object &Obj) {
    512   if (Config.OutputArch) {
    513     Obj.Machine = Config.OutputArch.getValue().EMachine;
    514     Obj.OSABI = Config.OutputArch.getValue().OSABI;
    515   }
    516 
    517   if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
    518     return Obj.removeSections(
    519         Config.AllowBrokenLinks,
    520         [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
    521   }
    522 
    523   // Dump sections before add/remove for compatibility with GNU objcopy.
    524   for (StringRef Flag : Config.DumpSection) {
    525     StringRef SectionName;
    526     StringRef FileName;
    527     std::tie(SectionName, FileName) = Flag.split('=');
    528     if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
    529       return E;
    530   }
    531 
    532   // It is important to remove the sections first. For example, we want to
    533   // remove the relocation sections before removing the symbols. That allows
    534   // us to avoid reporting the inappropriate errors about removing symbols
    535   // named in relocations.
    536   if (Error E = replaceAndRemoveSections(Config, Obj))
    537     return E;
    538 
    539   if (Error E = updateAndRemoveSymbols(Config, Obj))
    540     return E;
    541 
    542   if (!Config.SectionsToRename.empty()) {
    543     for (SectionBase &Sec : Obj.sections()) {
    544       const auto Iter = Config.SectionsToRename.find(Sec.Name);
    545       if (Iter != Config.SectionsToRename.end()) {
    546         const SectionRename &SR = Iter->second;
    547         Sec.Name = std::string(SR.NewName);
    548         if (SR.NewFlags.hasValue())
    549           setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
    550       }
    551     }
    552   }
    553 
    554   // Add a prefix to allocated sections and their relocation sections. This
    555   // should be done after renaming the section by Config.SectionToRename to
    556   // imitate the GNU objcopy behavior.
    557   if (!Config.AllocSectionsPrefix.empty()) {
    558     DenseSet<SectionBase *> PrefixedSections;
    559     for (SectionBase &Sec : Obj.sections()) {
    560       if (Sec.Flags & SHF_ALLOC) {
    561         Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
    562         PrefixedSections.insert(&Sec);
    563       } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
    564         // Rename relocation sections associated to the allocated sections.
    565         // For example, if we rename .text to .prefix.text, we also rename
    566         // .rel.text to .rel.prefix.text.
    567         //
    568         // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
    569         // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
    570         // .rela.prefix.plt since GNU objcopy does so.
    571         const SectionBase *TargetSec = RelocSec->getSection();
    572         if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
    573           StringRef prefix;
    574           switch (Sec.Type) {
    575           case SHT_REL:
    576             prefix = ".rel";
    577             break;
    578           case SHT_RELA:
    579             prefix = ".rela";
    580             break;
    581           default:
    582             llvm_unreachable("not a relocation section");
    583           }
    584 
    585           // If the relocation section comes *after* the target section, we
    586           // don't add Config.AllocSectionsPrefix because we've already added
    587           // the prefix to TargetSec->Name. Otherwise, if the relocation
    588           // section comes *before* the target section, we add the prefix.
    589           if (PrefixedSections.count(TargetSec))
    590             Sec.Name = (prefix + TargetSec->Name).str();
    591           else
    592             Sec.Name =
    593                 (prefix + Config.AllocSectionsPrefix + TargetSec->Name).str();
    594         }
    595       }
    596     }
    597   }
    598 
    599   if (!Config.SetSectionAlignment.empty()) {
    600     for (SectionBase &Sec : Obj.sections()) {
    601       auto I = Config.SetSectionAlignment.find(Sec.Name);
    602       if (I != Config.SetSectionAlignment.end())
    603         Sec.Align = I->second;
    604     }
    605   }
    606 
    607   if (Config.OnlyKeepDebug)
    608     for (auto &Sec : Obj.sections())
    609       if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
    610         Sec.Type = SHT_NOBITS;
    611 
    612   for (const auto &Flag : Config.AddSection) {
    613     std::pair<StringRef, StringRef> SecPair = Flag.split("=");
    614     StringRef SecName = SecPair.first;
    615     StringRef File = SecPair.second;
    616     ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
    617         MemoryBuffer::getFile(File);
    618     if (!BufOrErr)
    619       return createFileError(File, errorCodeToError(BufOrErr.getError()));
    620     std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
    621     ArrayRef<uint8_t> Data(
    622         reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
    623         Buf->getBufferSize());
    624     OwnedDataSection &NewSection =
    625         Obj.addSection<OwnedDataSection>(SecName, Data);
    626     if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
    627       NewSection.Type = SHT_NOTE;
    628   }
    629 
    630   if (!Config.AddGnuDebugLink.empty())
    631     Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink,
    632                                         Config.GnuDebugLinkCRC32);
    633 
    634   // If the symbol table was previously removed, we need to create a new one
    635   // before adding new symbols.
    636   if (!Obj.SymbolTable && !ELFConfig.SymbolsToAdd.empty())
    637     if (Error E = Obj.addNewSymbolTable())
    638       return E;
    639 
    640   for (const NewSymbolInfo &SI : ELFConfig.SymbolsToAdd) {
    641     SectionBase *Sec = Obj.findSection(SI.SectionName);
    642     uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value;
    643     Obj.SymbolTable->addSymbol(
    644         SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility,
    645         Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
    646   }
    647 
    648   // --set-section-flags works with sections added by --add-section.
    649   if (!Config.SetSectionFlags.empty()) {
    650     for (auto &Sec : Obj.sections()) {
    651       const auto Iter = Config.SetSectionFlags.find(Sec.Name);
    652       if (Iter != Config.SetSectionFlags.end()) {
    653         const SectionFlagsUpdate &SFU = Iter->second;
    654         setSectionFlagsAndType(Sec, SFU.NewFlags);
    655       }
    656     }
    657   }
    658 
    659   if (Config.EntryExpr)
    660     Obj.Entry = Config.EntryExpr(Obj.Entry);
    661   return Error::success();
    662 }
    663 
    664 static Error writeOutput(const CommonConfig &Config, Object &Obj,
    665                          raw_ostream &Out, ElfType OutputElfType) {
    666   std::unique_ptr<Writer> Writer =
    667       createWriter(Config, Obj, Out, OutputElfType);
    668   if (Error E = Writer->finalize())
    669     return E;
    670   return Writer->write();
    671 }
    672 
    673 Error executeObjcopyOnIHex(const CommonConfig &Config,
    674                            const ELFConfig &ELFConfig, MemoryBuffer &In,
    675                            raw_ostream &Out) {
    676   IHexReader Reader(&In);
    677   Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
    678   if (!Obj)
    679     return Obj.takeError();
    680 
    681   const ElfType OutputElfType =
    682       getOutputElfType(Config.OutputArch.getValueOr(MachineInfo()));
    683   if (Error E = handleArgs(Config, ELFConfig, **Obj))
    684     return E;
    685   return writeOutput(Config, **Obj, Out, OutputElfType);
    686 }
    687 
    688 Error executeObjcopyOnRawBinary(const CommonConfig &Config,
    689                                 const ELFConfig &ELFConfig, MemoryBuffer &In,
    690                                 raw_ostream &Out) {
    691   uint8_t NewSymbolVisibility =
    692       ELFConfig.NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT);
    693   BinaryReader Reader(&In, NewSymbolVisibility);
    694   Expected<std::unique_ptr<Object>> Obj = Reader.create(true);
    695   if (!Obj)
    696     return Obj.takeError();
    697 
    698   // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
    699   // (-B<arch>).
    700   const ElfType OutputElfType =
    701       getOutputElfType(Config.OutputArch.getValueOr(MachineInfo()));
    702   if (Error E = handleArgs(Config, ELFConfig, **Obj))
    703     return E;
    704   return writeOutput(Config, **Obj, Out, OutputElfType);
    705 }
    706 
    707 Error executeObjcopyOnBinary(const CommonConfig &Config,
    708                              const ELFConfig &ELFConfig,
    709                              object::ELFObjectFileBase &In, raw_ostream &Out) {
    710   ELFReader Reader(&In, Config.ExtractPartition);
    711   Expected<std::unique_ptr<Object>> Obj =
    712       Reader.create(!ELFConfig.SymbolsToAdd.empty());
    713   if (!Obj)
    714     return Obj.takeError();
    715   // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
    716   const ElfType OutputElfType =
    717       Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
    718                         : getOutputElfType(In);
    719 
    720   if (Error E = handleArgs(Config, ELFConfig, **Obj))
    721     return createFileError(Config.InputFilename, std::move(E));
    722 
    723   if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
    724     return createFileError(Config.InputFilename, std::move(E));
    725 
    726   return Error::success();
    727 }
    728 
    729 } // end namespace elf
    730 } // end namespace objcopy
    731 } // end namespace llvm
    732