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 | |
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')
9 files changed, 248 insertions, 248 deletions
diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp index 849a1e4ea4..8e2bf36238 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.cpp @@ -15,14 +15,14 @@ namespace llvm { namespace objcopy { namespace macho { -StringTableBuilder::Kind -MachOLayoutBuilder::getStringTableBuilderKind(const Object &O, bool Is64Bit) { - if (O.Header.FileType == MachO::HeaderFileType::MH_OBJECT) - return Is64Bit ? StringTableBuilder::MachO64 : StringTableBuilder::MachO; - return Is64Bit ? StringTableBuilder::MachO64Linked - : StringTableBuilder::MachOLinked; -} - +StringTableBuilder::Kind +MachOLayoutBuilder::getStringTableBuilderKind(const Object &O, bool Is64Bit) { + if (O.Header.FileType == MachO::HeaderFileType::MH_OBJECT) + return Is64Bit ? StringTableBuilder::MachO64 : StringTableBuilder::MachO; + return Is64Bit ? StringTableBuilder::MachO64Linked + : StringTableBuilder::MachOLinked; +} + uint32_t MachOLayoutBuilder::computeSizeOfCmds() const { uint32_t Size = 0; for (const LoadCommand &LC : O.LoadCommands) { @@ -156,7 +156,7 @@ uint64_t MachOLayoutBuilder::layoutSegments() { "Section's address cannot be smaller than Segment's one"); uint32_t SectOffset = Sec->Addr - SegmentVmAddr; if (IsObjectFile) { - if (!Sec->hasValidOffset()) { + if (!Sec->hasValidOffset()) { Sec->Offset = 0; } else { uint64_t PaddingSize = @@ -166,7 +166,7 @@ uint64_t MachOLayoutBuilder::layoutSegments() { SegFileSize += PaddingSize + Sec->Size; } } else { - if (!Sec->hasValidOffset()) { + if (!Sec->hasValidOffset()) { Sec->Offset = 0; } else { Sec->Offset = SegOffset + SectOffset; @@ -260,8 +260,8 @@ Error MachOLayoutBuilder::layoutTail(uint64_t Offset) { sizeof(uint32_t) * O.IndirectSymTable.Symbols.size(); uint64_t StartOfCodeSignature = StartOfSymbolStrings + StrTableBuilder.getSize(); - if (O.CodeSignatureCommandIndex) - StartOfCodeSignature = alignTo(StartOfCodeSignature, 16); + if (O.CodeSignatureCommandIndex) + StartOfCodeSignature = alignTo(StartOfCodeSignature, 16); uint64_t LinkEditSize = (StartOfCodeSignature + O.CodeSignature.Data.size()) - StartOfLinkEdit; diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.h b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.h index 3b8d81cdf3..5fe6683e27 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.h +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOLayoutBuilder.h @@ -23,7 +23,7 @@ class MachOLayoutBuilder { // Points to the __LINKEDIT segment if it exists. MachO::macho_load_command *LinkEditLoadCommand = nullptr; - StringTableBuilder StrTableBuilder; + StringTableBuilder StrTableBuilder; uint32_t computeSizeOfCmds() const; void constructStringTable(); @@ -33,13 +33,13 @@ class MachOLayoutBuilder { uint64_t layoutRelocations(uint64_t Offset); Error layoutTail(uint64_t Offset); - static StringTableBuilder::Kind getStringTableBuilderKind(const Object &O, - bool Is64Bit); - + static StringTableBuilder::Kind getStringTableBuilderKind(const Object &O, + bool Is64Bit); + public: MachOLayoutBuilder(Object &O, bool Is64Bit, uint64_t PageSize) - : O(O), Is64Bit(Is64Bit), PageSize(PageSize), - StrTableBuilder(getStringTableBuilderKind(O, Is64Bit)) {} + : O(O), Is64Bit(Is64Bit), PageSize(PageSize), + StrTableBuilder(getStringTableBuilderKind(O, Is64Bit)) {} // Recomputes and updates fields in the given object such as file offsets. Error layout(); 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 diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.h b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.h index e79db34e8b..c3f5391f79 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.h +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOObjcopy.h @@ -24,10 +24,10 @@ class Buffer; namespace macho { Error executeObjcopyOnBinary(const CopyConfig &Config, object::MachOObjectFile &In, Buffer &Out); - -Error executeObjcopyOnMachOUniversalBinary( - CopyConfig &Config, const object::MachOUniversalBinary &In, Buffer &Out); - + +Error executeObjcopyOnMachOUniversalBinary( + CopyConfig &Config, const object::MachOUniversalBinary &In, Buffer &Out); + } // end namespace macho } // end namespace objcopy } // end namespace llvm diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.cpp b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.cpp index 495d54046f..548a85bd49 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.cpp +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.cpp @@ -10,7 +10,7 @@ #include "Object.h" #include "llvm/BinaryFormat/MachO.h" #include "llvm/Object/MachO.h" -#include "llvm/Support/Errc.h" +#include "llvm/Support/Errc.h" #include <memory> namespace llvm { @@ -35,7 +35,7 @@ Section constructSectionCommon(SectionType Sec, uint32_t Index) { S.Index = Index; S.Addr = Sec.addr; S.Size = Sec.size; - S.OriginalOffset = Sec.offset; + S.OriginalOffset = Sec.offset; S.Align = Sec.align; S.RelOff = Sec.reloff; S.NReloc = Sec.nreloc; @@ -60,7 +60,7 @@ template <> Section constructSection(MachO::section_64 Sec, uint32_t Index) { } template <typename SectionType, typename SegmentType> -Expected<std::vector<std::unique_ptr<Section>>> +Expected<std::vector<std::unique_ptr<Section>>> extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd, const object::MachOObjectFile &MachOObj, uint32_t &NextSectionIndex) { @@ -85,16 +85,16 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd, Expected<object::SectionRef> SecRef = MachOObj.getSection(NextSectionIndex++); if (!SecRef) - return SecRef.takeError(); + return SecRef.takeError(); - Expected<ArrayRef<uint8_t>> Data = - MachOObj.getSectionContents(SecRef->getRawDataRefImpl()); - if (!Data) - return Data.takeError(); + Expected<ArrayRef<uint8_t>> Data = + MachOObj.getSectionContents(SecRef->getRawDataRefImpl()); + if (!Data) + return Data.takeError(); + + S.Content = + StringRef(reinterpret_cast<const char *>(Data->data()), Data->size()); - S.Content = - StringRef(reinterpret_cast<const char *>(Data->data()), Data->size()); - S.Relocations.reserve(S.NReloc); for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()), RE = MachOObj.section_rel_end(SecRef->getRawDataRefImpl()); @@ -110,10 +110,10 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd, assert(S.NReloc == S.Relocations.size() && "Incorrect number of relocations"); } - return std::move(Sections); + return std::move(Sections); } -Error MachOReader::readLoadCommands(Object &O) const { +Error MachOReader::readLoadCommands(Object &O) const { // For MachO sections indices start from 1. uint32_t NextSectionIndex = 1; for (auto LoadCmd : MachOObj.load_commands()) { @@ -123,20 +123,20 @@ Error MachOReader::readLoadCommands(Object &O) const { O.CodeSignatureCommandIndex = O.LoadCommands.size(); break; case MachO::LC_SEGMENT: - if (Expected<std::vector<std::unique_ptr<Section>>> Sections = - extractSections<MachO::section, MachO::segment_command>( - LoadCmd, MachOObj, NextSectionIndex)) - LC.Sections = std::move(*Sections); - else - return Sections.takeError(); + if (Expected<std::vector<std::unique_ptr<Section>>> Sections = + extractSections<MachO::section, MachO::segment_command>( + LoadCmd, MachOObj, NextSectionIndex)) + LC.Sections = std::move(*Sections); + else + return Sections.takeError(); break; case MachO::LC_SEGMENT_64: - if (Expected<std::vector<std::unique_ptr<Section>>> Sections = - extractSections<MachO::section_64, MachO::segment_command_64>( - LoadCmd, MachOObj, NextSectionIndex)) - LC.Sections = std::move(*Sections); - else - return Sections.takeError(); + if (Expected<std::vector<std::unique_ptr<Section>>> Sections = + extractSections<MachO::section_64, MachO::segment_command_64>( + LoadCmd, MachOObj, NextSectionIndex)) + LC.Sections = std::move(*Sections); + else + return Sections.takeError(); break; case MachO::LC_SYMTAB: O.SymTabCommandIndex = O.LoadCommands.size(); @@ -184,7 +184,7 @@ Error MachOReader::readLoadCommands(Object &O) const { } O.LoadCommands.push_back(std::move(LC)); } - return Error::success(); + return Error::success(); } template <typename nlist_t> @@ -316,11 +316,11 @@ void MachOReader::readSwiftVersion(Object &O) const { } } -Expected<std::unique_ptr<Object>> MachOReader::create() const { +Expected<std::unique_ptr<Object>> MachOReader::create() const { auto Obj = std::make_unique<Object>(); readHeader(*Obj); - if (Error E = readLoadCommands(*Obj)) - return std::move(E); + if (Error E = readLoadCommands(*Obj)) + return std::move(E); readSymbolTable(*Obj); setSymbolInRelocationInfo(*Obj); readRebaseInfo(*Obj); @@ -333,7 +333,7 @@ Expected<std::unique_ptr<Object>> MachOReader::create() const { readFunctionStartsData(*Obj); readIndirectSymbolTable(*Obj); readSwiftVersion(*Obj); - return std::move(Obj); + return std::move(Obj); } } // end namespace macho diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.h b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.h index c42e58d0da..b446e02865 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.h +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOReader.h @@ -21,14 +21,14 @@ namespace macho { class Reader { public: virtual ~Reader(){}; - virtual Expected<std::unique_ptr<Object>> create() const = 0; + virtual Expected<std::unique_ptr<Object>> create() const = 0; }; class MachOReader : public Reader { const object::MachOObjectFile &MachOObj; void readHeader(Object &O) const; - Error readLoadCommands(Object &O) const; + Error readLoadCommands(Object &O) const; void readSymbolTable(Object &O) const; void setSymbolInRelocationInfo(Object &O) const; void readRebaseInfo(Object &O) const; @@ -46,7 +46,7 @@ class MachOReader : public Reader { public: explicit MachOReader(const object::MachOObjectFile &Obj) : MachOObj(Obj) {} - Expected<std::unique_ptr<Object>> create() const override; + Expected<std::unique_ptr<Object>> create() const override; }; } // end namespace macho diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOWriter.cpp b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOWriter.cpp index 7ff05ed9e0..56dd08df3b 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOWriter.cpp +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/MachOWriter.cpp @@ -121,14 +121,14 @@ size_t MachOWriter::totalSize() const { // Otherwise, use the last section / reloction. for (const LoadCommand &LC : O.LoadCommands) for (const std::unique_ptr<Section> &S : LC.Sections) { - if (!S->hasValidOffset()) { - assert((S->Offset == 0) && "Skipped section's offset must be zero"); - assert((S->isVirtualSection() || S->Size == 0) && - "Non-zero-fill sections with zero offset must have zero size"); - continue; - } - assert((S->Offset != 0) && - "Non-zero-fill section's offset cannot be zero"); + if (!S->hasValidOffset()) { + assert((S->Offset == 0) && "Skipped section's offset must be zero"); + assert((S->isVirtualSection() || S->Size == 0) && + "Non-zero-fill sections with zero offset must have zero size"); + continue; + } + assert((S->Offset != 0) && + "Non-zero-fill section's offset cannot be zero"); Ends.push_back(S->Offset + S->Size); if (S->RelOff) Ends.push_back(S->RelOff + @@ -248,12 +248,12 @@ void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) { void MachOWriter::writeSections() { for (const LoadCommand &LC : O.LoadCommands) for (const std::unique_ptr<Section> &Sec : LC.Sections) { - if (!Sec->hasValidOffset()) { - assert((Sec->Offset == 0) && "Skipped section's offset must be zero"); - assert((Sec->isVirtualSection() || Sec->Size == 0) && - "Non-zero-fill sections with zero offset must have zero size"); + if (!Sec->hasValidOffset()) { + assert((Sec->Offset == 0) && "Skipped section's offset must be zero"); + assert((Sec->isVirtualSection() || Sec->Size == 0) && + "Non-zero-fill sections with zero offset must have zero size"); continue; - } + } assert(Sec->Offset && "Section offset can not be zero"); assert((Sec->Size == Sec->Content.size()) && "Incorrect section size"); diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.cpp b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.cpp index 1f7e04c14e..cdb97531fb 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.cpp +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.cpp @@ -26,7 +26,7 @@ SymbolEntry *SymbolTable::getSymbolByIndex(uint32_t Index) { void SymbolTable::removeSymbols( function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove) { - llvm::erase_if(Symbols, ToRemove); + llvm::erase_if(Symbols, ToRemove); } void Object::updateLoadCommandIndexes() { @@ -108,54 +108,54 @@ Error Object::removeSections( return Error::success(); } -uint64_t Object::nextAvailableSegmentAddress() const { - uint64_t HeaderSize = - is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); - uint64_t Addr = HeaderSize + Header.SizeOfCmds; - for (const LoadCommand &LC : LoadCommands) { - const MachO::macho_load_command &MLC = LC.MachOLoadCommand; - switch (MLC.load_command_data.cmd) { - case MachO::LC_SEGMENT: - Addr = std::max(Addr, - static_cast<uint64_t>(MLC.segment_command_data.vmaddr) + - MLC.segment_command_data.vmsize); - break; - case MachO::LC_SEGMENT_64: - Addr = std::max(Addr, MLC.segment_command_64_data.vmaddr + - MLC.segment_command_64_data.vmsize); - break; - default: - continue; - } - } - return Addr; +uint64_t Object::nextAvailableSegmentAddress() const { + uint64_t HeaderSize = + is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); + uint64_t Addr = HeaderSize + Header.SizeOfCmds; + for (const LoadCommand &LC : LoadCommands) { + const MachO::macho_load_command &MLC = LC.MachOLoadCommand; + switch (MLC.load_command_data.cmd) { + case MachO::LC_SEGMENT: + Addr = std::max(Addr, + static_cast<uint64_t>(MLC.segment_command_data.vmaddr) + + MLC.segment_command_data.vmsize); + break; + case MachO::LC_SEGMENT_64: + Addr = std::max(Addr, MLC.segment_command_64_data.vmaddr + + MLC.segment_command_64_data.vmsize); + break; + default: + continue; + } + } + return Addr; } template <typename SegmentType> -static void -constructSegment(SegmentType &Seg, llvm::MachO::LoadCommandType CmdType, - StringRef SegName, uint64_t SegVMAddr, uint64_t SegVMSize) { +static void +constructSegment(SegmentType &Seg, llvm::MachO::LoadCommandType CmdType, + StringRef SegName, uint64_t SegVMAddr, uint64_t SegVMSize) { assert(SegName.size() <= sizeof(Seg.segname) && "too long segment name"); memset(&Seg, 0, sizeof(SegmentType)); Seg.cmd = CmdType; strncpy(Seg.segname, SegName.data(), SegName.size()); - Seg.maxprot |= - (MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE); - Seg.initprot |= - (MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE); - Seg.vmaddr = SegVMAddr; - Seg.vmsize = SegVMSize; + Seg.maxprot |= + (MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE); + Seg.initprot |= + (MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE); + Seg.vmaddr = SegVMAddr; + Seg.vmsize = SegVMSize; } -LoadCommand &Object::addSegment(StringRef SegName, uint64_t SegVMSize) { +LoadCommand &Object::addSegment(StringRef SegName, uint64_t SegVMSize) { LoadCommand LC; - const uint64_t SegVMAddr = nextAvailableSegmentAddress(); + const uint64_t SegVMAddr = nextAvailableSegmentAddress(); if (is64Bit()) constructSegment(LC.MachOLoadCommand.segment_command_64_data, - MachO::LC_SEGMENT_64, SegName, SegVMAddr, SegVMSize); + MachO::LC_SEGMENT_64, SegName, SegVMAddr, SegVMSize); else constructSegment(LC.MachOLoadCommand.segment_command_data, - MachO::LC_SEGMENT, SegName, SegVMAddr, SegVMSize); + MachO::LC_SEGMENT, SegName, SegVMAddr, SegVMSize); LoadCommands.push_back(std::move(LC)); return LoadCommands.back(); @@ -179,18 +179,18 @@ Optional<StringRef> LoadCommand::getSegmentName() const { } } -Optional<uint64_t> LoadCommand::getSegmentVMAddr() const { - const MachO::macho_load_command &MLC = MachOLoadCommand; - switch (MLC.load_command_data.cmd) { - case MachO::LC_SEGMENT: - return MLC.segment_command_data.vmaddr; - case MachO::LC_SEGMENT_64: - return MLC.segment_command_64_data.vmaddr; - default: - return None; - } -} - +Optional<uint64_t> LoadCommand::getSegmentVMAddr() const { + const MachO::macho_load_command &MLC = MachOLoadCommand; + switch (MLC.load_command_data.cmd) { + case MachO::LC_SEGMENT: + return MLC.segment_command_data.vmaddr; + case MachO::LC_SEGMENT_64: + return MLC.segment_command_64_data.vmaddr; + default: + return None; + } +} + } // end namespace macho } // end namespace objcopy } // end namespace llvm diff --git a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.h b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.h index 111023017d..0bb4b344b2 100644 --- a/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.h +++ b/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.h @@ -44,8 +44,8 @@ struct Section { std::string CanonicalName; uint64_t Addr = 0; uint64_t Size = 0; - // Offset in the input file. - Optional<uint32_t> OriginalOffset; + // Offset in the input file. + Optional<uint32_t> OriginalOffset; uint32_t Offset = 0; uint32_t Align = 0; uint32_t RelOff = 0; @@ -75,10 +75,10 @@ struct Section { getType() == MachO::S_GB_ZEROFILL || getType() == MachO::S_THREAD_LOCAL_ZEROFILL); } - - bool hasValidOffset() const { - return !(isVirtualSection() || (OriginalOffset && *OriginalOffset == 0)); - } + + bool hasValidOffset() const { + return !(isVirtualSection() || (OriginalOffset && *OriginalOffset == 0)); + } }; struct LoadCommand { @@ -100,9 +100,9 @@ struct LoadCommand { // Returns the segment name if the load command is a segment command. Optional<StringRef> getSegmentName() const; - - // Returns the segment vm address if the load command is a segment command. - Optional<uint64_t> getSegmentVMAddr() const; + + // Returns the segment vm address if the load command is a segment command. + Optional<uint64_t> getSegmentVMAddr() const; }; // A symbol information. Fields which starts with "n_" are same as them in the @@ -343,14 +343,14 @@ struct Object { /// Creates a new segment load command in the object and returns a reference /// to the newly created load command. The caller should verify that SegName /// is not too long (SegName.size() should be less than or equal to 16). - LoadCommand &addSegment(StringRef SegName, uint64_t SegVMSize); + LoadCommand &addSegment(StringRef SegName, uint64_t SegVMSize); bool is64Bit() const { return Header.Magic == MachO::MH_MAGIC_64 || Header.Magic == MachO::MH_CIGAM_64; } - - uint64_t nextAvailableSegmentAddress() const; + + uint64_t nextAvailableSegmentAddress() const; }; } // end namespace macho |