diff options
Diffstat (limited to 'llvm/tools/llvm-objcopy/MachO/Object.h')
-rw-r--r-- | llvm/tools/llvm-objcopy/MachO/Object.h | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/llvm/tools/llvm-objcopy/MachO/Object.h b/llvm/tools/llvm-objcopy/MachO/Object.h index dc2606eefa4a2..e825d1867b09a 100644 --- a/llvm/tools/llvm-objcopy/MachO/Object.h +++ b/llvm/tools/llvm-objcopy/MachO/Object.h @@ -37,6 +37,7 @@ struct MachHeader { struct RelocationInfo; struct Section { + uint32_t Index; std::string Segname; std::string Sectname; // CanonicalName is a string formatted as “<Segname>,<Sectname>". @@ -55,11 +56,11 @@ struct Section { std::vector<RelocationInfo> Relocations; Section(StringRef SegName, StringRef SectName) - : Segname(SegName), Sectname(SectName), + : Segname(std::string(SegName)), Sectname(std::string(SectName)), CanonicalName((Twine(SegName) + Twine(',') + SectName).str()) {} Section(StringRef SegName, StringRef SectName, StringRef Content) - : Segname(SegName), Sectname(SectName), + : Segname(std::string(SegName)), Sectname(std::string(SectName)), CanonicalName((Twine(SegName) + Twine(',') + SectName).str()), Content(Content) {} @@ -83,13 +84,13 @@ struct LoadCommand { // The raw content of the payload of the load command (located right after the // corresponding struct). In some cases it is either empty or can be // copied-over without digging into its structure. - std::vector<uint8_t> Payload; + std::vector<uint8_t> Payload; // Some load commands can contain (inside the payload) an array of sections, // though the contents of the sections are stored separately. The struct // Section describes only sections' metadata and where to find the // corresponding content inside the binary. - std::vector<Section> Sections; + std::vector<std::unique_ptr<Section>> Sections; // Returns the segment name if the load command is a segment command. Optional<StringRef> getSegmentName() const; @@ -106,15 +107,22 @@ struct SymbolEntry { uint16_t n_desc; uint64_t n_value; - bool isExternalSymbol() const { - return n_type & ((MachO::N_EXT | MachO::N_PEXT)); - } + bool isExternalSymbol() const { return n_type & MachO::N_EXT; } bool isLocalSymbol() const { return !isExternalSymbol(); } bool isUndefinedSymbol() const { return (n_type & MachO::N_TYPE) == MachO::N_UNDF; } + + bool isSwiftSymbol() const { + return StringRef(Name).startswith("_$s") || + StringRef(Name).startswith("_$S"); + } + + Optional<uint32_t> section() const { + return n_sect == MachO::NO_SECT ? None : Optional<uint32_t>(n_sect); + } }; /// The location of the symbol table inside the binary is described by LC_SYMTAB @@ -157,10 +165,29 @@ struct StringTable { }; struct RelocationInfo { - const SymbolEntry *Symbol; + // The referenced symbol entry. Set if !Scattered && Extern. + Optional<const SymbolEntry *> Symbol; + // The referenced section. Set if !Scattered && !Extern. + Optional<const Section *> Sec; // True if Info is a scattered_relocation_info. bool Scattered; + // True if the r_symbolnum points to a section number (i.e. r_extern=0). + bool Extern; MachO::any_relocation_info Info; + + unsigned getPlainRelocationSymbolNum(bool IsLittleEndian) { + if (IsLittleEndian) + return Info.r_word1 & 0xffffff; + return Info.r_word1 >> 8; + } + + void setPlainRelocationSymbolNum(unsigned SymbolNum, bool IsLittleEndian) { + assert(SymbolNum < (1 << 24) && "SymbolNum out of range"); + if (IsLittleEndian) + Info.r_word1 = (Info.r_word1 & ~0x00ffffff) | SymbolNum; + else + Info.r_word1 = (Info.r_word1 & ~0xffffff00) | (SymbolNum << 8); + } }; /// The location of the rebase info inside the binary is described by @@ -275,7 +302,12 @@ struct Object { IndirectSymbolTable IndirectSymTable; LinkData DataInCode; LinkData FunctionStarts; + LinkData CodeSignature; + + Optional<uint32_t> SwiftVersion; + /// The index of LC_CODE_SIGNATURE load command if present. + Optional<size_t> CodeSignatureCommandIndex; /// The index of LC_SYMTAB load command if present. Optional<size_t> SymTabCommandIndex; /// The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present. @@ -292,7 +324,13 @@ struct Object { Object() : NewSectionsContents(Alloc) {} - void removeSections(function_ref<bool(const Section &)> ToRemove); + Error + removeSections(function_ref<bool(const std::unique_ptr<Section> &)> ToRemove); + + Error removeLoadCommands(function_ref<bool(const LoadCommand &)> ToRemove); + + void updateLoadCommandIndexes(); + void addLoadCommand(LoadCommand LC); /// Creates a new segment load command in the object and returns a reference |