MachOLayoutBuilder.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- MachOLayoutBuilder.h -------------------------------------*- C++ -*-===//
  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. #ifndef LLVM_LIB_OBJCOPY_MACHO_MACHOLAYOUTBUILDER_H
  9. #define LLVM_LIB_OBJCOPY_MACHO_MACHOLAYOUTBUILDER_H
  10. #include "MachOObject.h"
  11. #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
  12. namespace llvm {
  13. namespace objcopy {
  14. namespace macho {
  15. /// When MachO binaries include a LC_CODE_SIGNATURE load command,
  16. /// the __LINKEDIT data segment will include a section corresponding
  17. /// to the LC_CODE_SIGNATURE load command. This section serves as a signature
  18. /// for the binary. Included in the CodeSignature section is a header followed
  19. /// by a hash of the binary. If present, the CodeSignature section is the
  20. /// last component of the binary.
  21. struct CodeSignatureInfo {
  22. // NOTE: These values are to be kept in sync with those in
  23. // LLD's CodeSignatureSection class.
  24. static constexpr uint32_t Align = 16;
  25. static constexpr uint8_t BlockSizeShift = 12;
  26. // The binary is read in blocks of the following size.
  27. static constexpr size_t BlockSize = (1 << BlockSizeShift); // 4 KiB
  28. // For each block, a SHA256 hash (256 bits, 32 bytes) is written to
  29. // the CodeSignature section.
  30. static constexpr size_t HashSize = 256 / 8;
  31. static constexpr size_t BlobHeadersSize = llvm::alignTo<8>(
  32. sizeof(llvm::MachO::CS_SuperBlob) + sizeof(llvm::MachO::CS_BlobIndex));
  33. // The size of the entire header depends upon the filename the binary is being
  34. // written to, but the rest of the header is fixed in size.
  35. static constexpr uint32_t FixedHeadersSize =
  36. BlobHeadersSize + sizeof(llvm::MachO::CS_CodeDirectory);
  37. // The offset relative to the start of the binary where
  38. // the CodeSignature section should begin.
  39. uint32_t StartOffset;
  40. // The size of the entire header, output file name size included.
  41. uint32_t AllHeadersSize;
  42. // The number of blocks required to hash the binary.
  43. uint32_t BlockCount;
  44. StringRef OutputFileName;
  45. // The size of the entire CodeSignature section, including both the header and
  46. // hashes.
  47. uint32_t Size;
  48. };
  49. class MachOLayoutBuilder {
  50. Object &O;
  51. bool Is64Bit;
  52. StringRef OutputFileName;
  53. uint64_t PageSize;
  54. CodeSignatureInfo CodeSignature;
  55. // Points to the __LINKEDIT segment if it exists.
  56. MachO::macho_load_command *LinkEditLoadCommand = nullptr;
  57. StringTableBuilder StrTableBuilder;
  58. uint32_t computeSizeOfCmds() const;
  59. void constructStringTable();
  60. void updateSymbolIndexes();
  61. void updateDySymTab(MachO::macho_load_command &MLC);
  62. uint64_t layoutSegments();
  63. uint64_t layoutRelocations(uint64_t Offset);
  64. Error layoutTail(uint64_t Offset);
  65. static StringTableBuilder::Kind getStringTableBuilderKind(const Object &O,
  66. bool Is64Bit);
  67. public:
  68. MachOLayoutBuilder(Object &O, bool Is64Bit, StringRef OutputFileName,
  69. uint64_t PageSize)
  70. : O(O), Is64Bit(Is64Bit), OutputFileName(OutputFileName),
  71. PageSize(PageSize),
  72. StrTableBuilder(getStringTableBuilderKind(O, Is64Bit)) {}
  73. // Recomputes and updates fields in the given object such as file offsets.
  74. Error layout();
  75. StringTableBuilder &getStringTableBuilder() { return StrTableBuilder; }
  76. const CodeSignatureInfo &getCodeSignature() const { return CodeSignature; }
  77. };
  78. } // end namespace macho
  79. } // end namespace objcopy
  80. } // end namespace llvm
  81. #endif // LLVM_LIB_OBJCOPY_MACHO_MACHOLAYOUTBUILDER_H