Path: blob/main/contrib/llvm-project/llvm/lib/ObjCopy/MachO/MachOLayoutBuilder.h
35267 views
//===- MachOLayoutBuilder.h -------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_OBJCOPY_MACHO_MACHOLAYOUTBUILDER_H9#define LLVM_LIB_OBJCOPY_MACHO_MACHOLAYOUTBUILDER_H1011#include "MachOObject.h"12#include "llvm/ObjCopy/MachO/MachOObjcopy.h"1314namespace llvm {15namespace objcopy {16namespace macho {1718/// When MachO binaries include a LC_CODE_SIGNATURE load command,19/// the __LINKEDIT data segment will include a section corresponding20/// to the LC_CODE_SIGNATURE load command. This section serves as a signature21/// for the binary. Included in the CodeSignature section is a header followed22/// by a hash of the binary. If present, the CodeSignature section is the23/// last component of the binary.24struct CodeSignatureInfo {25// NOTE: These values are to be kept in sync with those in26// LLD's CodeSignatureSection class.2728static constexpr uint32_t Align = 16;29static constexpr uint8_t BlockSizeShift = 12;30// The binary is read in blocks of the following size.31static constexpr size_t BlockSize = (1 << BlockSizeShift); // 4 KiB32// For each block, a SHA256 hash (256 bits, 32 bytes) is written to33// the CodeSignature section.34static constexpr size_t HashSize = 256 / 8;35static constexpr size_t BlobHeadersSize = llvm::alignTo<8>(36sizeof(llvm::MachO::CS_SuperBlob) + sizeof(llvm::MachO::CS_BlobIndex));37// The size of the entire header depends upon the filename the binary is being38// written to, but the rest of the header is fixed in size.39static constexpr uint32_t FixedHeadersSize =40BlobHeadersSize + sizeof(llvm::MachO::CS_CodeDirectory);4142// The offset relative to the start of the binary where43// the CodeSignature section should begin.44uint32_t StartOffset;45// The size of the entire header, output file name size included.46uint32_t AllHeadersSize;47// The number of blocks required to hash the binary.48uint32_t BlockCount;49StringRef OutputFileName;50// The size of the entire CodeSignature section, including both the header and51// hashes.52uint32_t Size;53};5455class MachOLayoutBuilder {56Object &O;57bool Is64Bit;58StringRef OutputFileName;59uint64_t PageSize;60CodeSignatureInfo CodeSignature;6162// Points to the __LINKEDIT segment if it exists.63MachO::macho_load_command *LinkEditLoadCommand = nullptr;64StringTableBuilder StrTableBuilder;6566uint32_t computeSizeOfCmds() const;67void constructStringTable();68void updateSymbolIndexes();69void updateDySymTab(MachO::macho_load_command &MLC);70uint64_t layoutSegments();71uint64_t layoutRelocations(uint64_t Offset);72Error layoutTail(uint64_t Offset);7374static StringTableBuilder::Kind getStringTableBuilderKind(const Object &O,75bool Is64Bit);7677public:78MachOLayoutBuilder(Object &O, bool Is64Bit, StringRef OutputFileName,79uint64_t PageSize)80: O(O), Is64Bit(Is64Bit), OutputFileName(OutputFileName),81PageSize(PageSize),82StrTableBuilder(getStringTableBuilderKind(O, Is64Bit)) {}8384// Recomputes and updates fields in the given object such as file offsets.85Error layout();8687StringTableBuilder &getStringTableBuilder() { return StrTableBuilder; }8889const CodeSignatureInfo &getCodeSignature() const { return CodeSignature; }90};9192} // end namespace macho93} // end namespace objcopy94} // end namespace llvm9596#endif // LLVM_LIB_OBJCOPY_MACHO_MACHOLAYOUTBUILDER_H979899