diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp | |
parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
download | ydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp')
-rw-r--r-- | contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp | 242 |
1 files changed, 121 insertions, 121 deletions
diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp index 2af5b116d1..fef4a0ae55 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.cpp @@ -8,13 +8,13 @@ #include "MachOObjcopy.h" #include "../CopyConfig.h" -#include "../llvm-objcopy.h" +#include "../llvm-objcopy.h" #include "MachOReader.h" #include "MachOWriter.h" #include "llvm/ADT/DenseSet.h" -#include "llvm/Object/ArchiveWriter.h" -#include "llvm/Object/MachOUniversal.h" -#include "llvm/Object/MachOUniversalWriter.h" +#include "llvm/Object/ArchiveWriter.h" +#include "llvm/Object/MachOUniversal.h" +#include "llvm/Object/MachOUniversalWriter.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" @@ -137,14 +137,14 @@ static Error processLoadCommands(const CopyConfig &Config, Object &Obj) { DenseSet<StringRef> RPathsToRemove(Config.RPathsToRemove.begin(), Config.RPathsToRemove.end()); - LoadCommandPred RemovePred = [&RPathsToRemove, - &Config](const LoadCommand &LC) { + LoadCommandPred RemovePred = [&RPathsToRemove, + &Config](const LoadCommand &LC) { if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH) { - // When removing all RPaths we don't need to care - // about what it contains - if (Config.RemoveAllRpaths) - return true; - + // When removing all RPaths we don't need to care + // about what it contains + if (Config.RemoveAllRpaths) + return true; + StringRef RPath = getPayloadString(LC); if (RPathsToRemove.count(RPath)) { RPathsToRemove.erase(RPath); @@ -178,22 +178,22 @@ static Error processLoadCommands(const CopyConfig &Config, Object &Obj) { for (const auto &OldNew : Config.RPathsToUpdate) { StringRef Old = OldNew.getFirst(); StringRef New = OldNew.getSecond(); - if (!RPaths.contains(Old)) + if (!RPaths.contains(Old)) return createStringError(errc::invalid_argument, "no LC_RPATH load command with path: " + Old); - if (RPaths.contains(New)) + if (RPaths.contains(New)) return createStringError(errc::invalid_argument, - "rpath '" + New + - "' would create a duplicate load command"); + "rpath '" + New + + "' would create a duplicate load command"); } // Update load commands. for (LoadCommand &LC : Obj.LoadCommands) { switch (LC.MachOLoadCommand.load_command_data.cmd) { case MachO::LC_ID_DYLIB: - if (Config.SharedLibId) - updateLoadCommandPayloadString<MachO::dylib_command>( - LC, *Config.SharedLibId); + if (Config.SharedLibId) + updateLoadCommandPayloadString<MachO::dylib_command>( + LC, *Config.SharedLibId); break; case MachO::LC_RPATH: { @@ -220,30 +220,30 @@ static Error processLoadCommands(const CopyConfig &Config, Object &Obj) { // Add new RPaths. for (StringRef RPath : Config.RPathToAdd) { - if (RPaths.contains(RPath)) + if (RPaths.contains(RPath)) + return createStringError(errc::invalid_argument, + "rpath '" + RPath + + "' would create a duplicate load command"); + RPaths.insert(RPath); + Obj.LoadCommands.push_back(buildRPathLoadCommand(RPath)); + } + + for (StringRef RPath : Config.RPathToPrepend) { + if (RPaths.contains(RPath)) return createStringError(errc::invalid_argument, - "rpath '" + RPath + - "' would create a duplicate load command"); + "rpath '" + RPath + + "' would create a duplicate load command"); + RPaths.insert(RPath); - Obj.LoadCommands.push_back(buildRPathLoadCommand(RPath)); + Obj.LoadCommands.insert(Obj.LoadCommands.begin(), + buildRPathLoadCommand(RPath)); } - for (StringRef RPath : Config.RPathToPrepend) { - if (RPaths.contains(RPath)) - return createStringError(errc::invalid_argument, - "rpath '" + RPath + - "' would create a duplicate load command"); - - RPaths.insert(RPath); - Obj.LoadCommands.insert(Obj.LoadCommands.begin(), - buildRPathLoadCommand(RPath)); - } - - // Unlike appending rpaths, the indexes of subsequent load commands must - // be recalculated after prepending one. - if (!Config.RPathToPrepend.empty()) - Obj.updateLoadCommandIndexes(); - + // Unlike appending rpaths, the indexes of subsequent load commands must + // be recalculated after prepending one. + if (!Config.RPathToPrepend.empty()) + Obj.updateLoadCommandIndexes(); + return Error::success(); } @@ -280,34 +280,34 @@ static Error addSection(StringRef SecName, StringRef Filename, Object &Obj) { StringRef TargetSegName = Pair.first; Section Sec(TargetSegName, Pair.second); Sec.Content = Obj.NewSectionsContents.save(Buf->getBuffer()); - Sec.Size = Sec.Content.size(); + Sec.Size = Sec.Content.size(); // Add the a section into an existing segment. for (LoadCommand &LC : Obj.LoadCommands) { Optional<StringRef> SegName = LC.getSegmentName(); if (SegName && SegName == TargetSegName) { - uint64_t Addr = *LC.getSegmentVMAddr(); - for (const std::unique_ptr<Section> &S : LC.Sections) - Addr = std::max(Addr, S->Addr + S->Size); + uint64_t Addr = *LC.getSegmentVMAddr(); + for (const std::unique_ptr<Section> &S : LC.Sections) + Addr = std::max(Addr, S->Addr + S->Size); LC.Sections.push_back(std::make_unique<Section>(Sec)); - LC.Sections.back()->Addr = Addr; + LC.Sections.back()->Addr = Addr; return Error::success(); } } // There's no segment named TargetSegName. Create a new load command and // Insert a new section into it. - LoadCommand &NewSegment = - Obj.addSegment(TargetSegName, alignTo(Sec.Size, 16384)); + LoadCommand &NewSegment = + Obj.addSegment(TargetSegName, alignTo(Sec.Size, 16384)); NewSegment.Sections.push_back(std::make_unique<Section>(Sec)); - NewSegment.Sections.back()->Addr = *NewSegment.getSegmentVMAddr(); + NewSegment.Sections.back()->Addr = *NewSegment.getSegmentVMAddr(); return Error::success(); } // isValidMachOCannonicalName returns success if Name is a MachO cannonical name // ("<segment>,<section>") and lengths of both segment and section names are // valid. -static Error isValidMachOCannonicalName(StringRef Name) { +static Error isValidMachOCannonicalName(StringRef Name) { if (Name.count(',') != 1) return createStringError(errc::invalid_argument, "invalid section name '%s' (should be formatted " @@ -340,7 +340,7 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) { Config.ExtractDWO || Config.LocalizeHidden || Config.PreserveDates || Config.StripAllGNU || Config.StripDWO || Config.StripNonAlloc || Config.StripSections || Config.Weaken || Config.DecompressDebugSections || - Config.StripUnneeded || Config.DiscardMode == DiscardType::Locals || + Config.StripUnneeded || Config.DiscardMode == DiscardType::Locals || !Config.SymbolsToAdd.empty() || Config.EntryExpr) { return createStringError(llvm::errc::invalid_argument, "option not supported by llvm-objcopy for MachO"); @@ -388,11 +388,11 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) { Error executeObjcopyOnBinary(const CopyConfig &Config, object::MachOObjectFile &In, Buffer &Out) { MachOReader Reader(In); - Expected<std::unique_ptr<Object>> O = Reader.create(); + Expected<std::unique_ptr<Object>> O = Reader.create(); if (!O) - return createFileError(Config.InputFilename, O.takeError()); + return createFileError(Config.InputFilename, O.takeError()); - if (Error E = handleArgs(Config, **O)) + if (Error E = handleArgs(Config, **O)) return createFileError(Config.InputFilename, std::move(E)); // Page size used for alignment of segment sizes in Mach-O executables and @@ -408,81 +408,81 @@ Error executeObjcopyOnBinary(const CopyConfig &Config, PageSize = 4096; } - MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(), PageSize, Out); + MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(), PageSize, Out); if (auto E = Writer.finalize()) return E; return Writer.write(); } -Error executeObjcopyOnMachOUniversalBinary(CopyConfig &Config, - const MachOUniversalBinary &In, - Buffer &Out) { - SmallVector<OwningBinary<Binary>, 2> Binaries; - SmallVector<Slice, 2> Slices; - for (const auto &O : In.objects()) { - Expected<std::unique_ptr<Archive>> ArOrErr = O.getAsArchive(); - if (ArOrErr) { - Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr = - createNewArchiveMembers(Config, **ArOrErr); - if (!NewArchiveMembersOrErr) - return NewArchiveMembersOrErr.takeError(); - Expected<std::unique_ptr<MemoryBuffer>> OutputBufferOrErr = - writeArchiveToBuffer(*NewArchiveMembersOrErr, - (*ArOrErr)->hasSymbolTable(), (*ArOrErr)->kind(), - Config.DeterministicArchives, - (*ArOrErr)->isThin()); - if (!OutputBufferOrErr) - return OutputBufferOrErr.takeError(); - Expected<std::unique_ptr<Binary>> BinaryOrErr = - object::createBinary(**OutputBufferOrErr); - if (!BinaryOrErr) - return BinaryOrErr.takeError(); - Binaries.emplace_back(std::move(*BinaryOrErr), - std::move(*OutputBufferOrErr)); - Slices.emplace_back(*cast<Archive>(Binaries.back().getBinary()), - O.getCPUType(), O.getCPUSubType(), - O.getArchFlagName(), O.getAlign()); - continue; - } - // The methods getAsArchive, getAsObjectFile, getAsIRObject of the class - // ObjectForArch return an Error in case of the type mismatch. We need to - // check each in turn to see what kind of slice this is, so ignore errors - // produced along the way. - consumeError(ArOrErr.takeError()); - - Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = O.getAsObjectFile(); - if (!ObjOrErr) { - consumeError(ObjOrErr.takeError()); - return createStringError(std::errc::invalid_argument, - "slice for '%s' of the universal Mach-O binary " - "'%s' is not a Mach-O object or an archive", - O.getArchFlagName().c_str(), - Config.InputFilename.str().c_str()); - } - std::string ArchFlagName = O.getArchFlagName(); - MemBuffer MB(ArchFlagName); - if (Error E = executeObjcopyOnBinary(Config, **ObjOrErr, MB)) - return E; - std::unique_ptr<WritableMemoryBuffer> OutputBuffer = - MB.releaseMemoryBuffer(); - Expected<std::unique_ptr<Binary>> BinaryOrErr = - object::createBinary(*OutputBuffer); - if (!BinaryOrErr) - return BinaryOrErr.takeError(); - Binaries.emplace_back(std::move(*BinaryOrErr), std::move(OutputBuffer)); - Slices.emplace_back(*cast<MachOObjectFile>(Binaries.back().getBinary()), - O.getAlign()); - } - Expected<std::unique_ptr<MemoryBuffer>> B = - writeUniversalBinaryToBuffer(Slices); - if (!B) - return B.takeError(); - if (Error E = Out.allocate((*B)->getBufferSize())) - return E; - memcpy(Out.getBufferStart(), (*B)->getBufferStart(), (*B)->getBufferSize()); - return Out.commit(); -} - +Error executeObjcopyOnMachOUniversalBinary(CopyConfig &Config, + const MachOUniversalBinary &In, + Buffer &Out) { + SmallVector<OwningBinary<Binary>, 2> Binaries; + SmallVector<Slice, 2> Slices; + for (const auto &O : In.objects()) { + Expected<std::unique_ptr<Archive>> ArOrErr = O.getAsArchive(); + if (ArOrErr) { + Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr = + createNewArchiveMembers(Config, **ArOrErr); + if (!NewArchiveMembersOrErr) + return NewArchiveMembersOrErr.takeError(); + Expected<std::unique_ptr<MemoryBuffer>> OutputBufferOrErr = + writeArchiveToBuffer(*NewArchiveMembersOrErr, + (*ArOrErr)->hasSymbolTable(), (*ArOrErr)->kind(), + Config.DeterministicArchives, + (*ArOrErr)->isThin()); + if (!OutputBufferOrErr) + return OutputBufferOrErr.takeError(); + Expected<std::unique_ptr<Binary>> BinaryOrErr = + object::createBinary(**OutputBufferOrErr); + if (!BinaryOrErr) + return BinaryOrErr.takeError(); + Binaries.emplace_back(std::move(*BinaryOrErr), + std::move(*OutputBufferOrErr)); + Slices.emplace_back(*cast<Archive>(Binaries.back().getBinary()), + O.getCPUType(), O.getCPUSubType(), + O.getArchFlagName(), O.getAlign()); + continue; + } + // The methods getAsArchive, getAsObjectFile, getAsIRObject of the class + // ObjectForArch return an Error in case of the type mismatch. We need to + // check each in turn to see what kind of slice this is, so ignore errors + // produced along the way. + consumeError(ArOrErr.takeError()); + + Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = O.getAsObjectFile(); + if (!ObjOrErr) { + consumeError(ObjOrErr.takeError()); + return createStringError(std::errc::invalid_argument, + "slice for '%s' of the universal Mach-O binary " + "'%s' is not a Mach-O object or an archive", + O.getArchFlagName().c_str(), + Config.InputFilename.str().c_str()); + } + std::string ArchFlagName = O.getArchFlagName(); + MemBuffer MB(ArchFlagName); + if (Error E = executeObjcopyOnBinary(Config, **ObjOrErr, MB)) + return E; + std::unique_ptr<WritableMemoryBuffer> OutputBuffer = + MB.releaseMemoryBuffer(); + Expected<std::unique_ptr<Binary>> BinaryOrErr = + object::createBinary(*OutputBuffer); + if (!BinaryOrErr) + return BinaryOrErr.takeError(); + Binaries.emplace_back(std::move(*BinaryOrErr), std::move(OutputBuffer)); + Slices.emplace_back(*cast<MachOObjectFile>(Binaries.back().getBinary()), + O.getAlign()); + } + Expected<std::unique_ptr<MemoryBuffer>> B = + writeUniversalBinaryToBuffer(Slices); + if (!B) + return B.takeError(); + if (Error E = Out.allocate((*B)->getBufferSize())) + return E; + memcpy(Out.getBufferStart(), (*B)->getBufferStart(), (*B)->getBufferSize()); + return Out.commit(); +} + } // end namespace macho } // end namespace objcopy } // end namespace llvm |