MachOUniversalWriter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MachOUniversalWriter.h - MachO universal binary writer----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Declares the Slice class and writeUniversalBinary function for writing a
  15. // MachO universal binary file.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_OBJECT_MACHOUNIVERSALWRITER_H
  19. #define LLVM_OBJECT_MACHOUNIVERSALWRITER_H
  20. #include "llvm/Object/Archive.h"
  21. #include "llvm/Object/Binary.h"
  22. #include "llvm/Object/MachO.h"
  23. namespace llvm {
  24. class LLVMContext;
  25. namespace object {
  26. class IRObjectFile;
  27. class Slice {
  28. const Binary *B;
  29. uint32_t CPUType;
  30. uint32_t CPUSubType;
  31. std::string ArchName;
  32. // P2Alignment field stores slice alignment values from universal
  33. // binaries. This is also needed to order the slices so the total
  34. // file size can be calculated before creating the output buffer.
  35. uint32_t P2Alignment;
  36. Slice(const IRObjectFile &IRO, uint32_t CPUType, uint32_t CPUSubType,
  37. std::string ArchName, uint32_t Align);
  38. public:
  39. explicit Slice(const MachOObjectFile &O);
  40. Slice(const MachOObjectFile &O, uint32_t Align);
  41. /// This constructor takes pre-specified \param CPUType , \param CPUSubType ,
  42. /// \param ArchName , \param Align instead of inferring them from the archive
  43. /// members.
  44. Slice(const Archive &A, uint32_t CPUType, uint32_t CPUSubType,
  45. std::string ArchName, uint32_t Align);
  46. static Expected<Slice> create(const Archive &A,
  47. LLVMContext *LLVMCtx = nullptr);
  48. static Expected<Slice> create(const IRObjectFile &IRO, uint32_t Align);
  49. void setP2Alignment(uint32_t Align) { P2Alignment = Align; }
  50. const Binary *getBinary() const { return B; }
  51. uint32_t getCPUType() const { return CPUType; }
  52. uint32_t getCPUSubType() const { return CPUSubType; }
  53. uint32_t getP2Alignment() const { return P2Alignment; }
  54. uint64_t getCPUID() const {
  55. return static_cast<uint64_t>(CPUType) << 32 | CPUSubType;
  56. }
  57. std::string getArchString() const {
  58. if (!ArchName.empty())
  59. return ArchName;
  60. return ("unknown(" + Twine(CPUType) + "," +
  61. Twine(CPUSubType & ~MachO::CPU_SUBTYPE_MASK) + ")")
  62. .str();
  63. }
  64. friend bool operator<(const Slice &Lhs, const Slice &Rhs) {
  65. if (Lhs.CPUType == Rhs.CPUType)
  66. return Lhs.CPUSubType < Rhs.CPUSubType;
  67. // force arm64-family to follow after all other slices for
  68. // compatibility with cctools lipo
  69. if (Lhs.CPUType == MachO::CPU_TYPE_ARM64)
  70. return false;
  71. if (Rhs.CPUType == MachO::CPU_TYPE_ARM64)
  72. return true;
  73. // Sort by alignment to minimize file size
  74. return Lhs.P2Alignment < Rhs.P2Alignment;
  75. }
  76. };
  77. Error writeUniversalBinary(ArrayRef<Slice> Slices, StringRef OutputFileName);
  78. Expected<std::unique_ptr<MemoryBuffer>>
  79. writeUniversalBinaryToBuffer(ArrayRef<Slice> Slices);
  80. } // end namespace object
  81. } // end namespace llvm
  82. #endif // LLVM_OBJECT_MACHOUNIVERSALWRITER_H
  83. #ifdef __GNUC__
  84. #pragma GCC diagnostic pop
  85. #endif