MachOUniversalWriter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- MachOUniversalWriter.h - MachO universal binary writer----*- 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. //
  9. // Declares the Slice class and writeUniversalBinary function for writing a
  10. // MachO universal binary file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_MACHOUNIVERSALWRITER_H
  14. #define LLVM_OBJECT_MACHOUNIVERSALWRITER_H
  15. #include "llvm/Object/Archive.h"
  16. #include "llvm/Object/Binary.h"
  17. #include "llvm/Object/MachO.h"
  18. namespace llvm {
  19. class LLVMContext;
  20. namespace object {
  21. class IRObjectFile;
  22. class Slice {
  23. const Binary *B;
  24. uint32_t CPUType;
  25. uint32_t CPUSubType;
  26. std::string ArchName;
  27. // P2Alignment field stores slice alignment values from universal
  28. // binaries. This is also needed to order the slices so the total
  29. // file size can be calculated before creating the output buffer.
  30. uint32_t P2Alignment;
  31. Slice(const IRObjectFile &IRO, uint32_t CPUType, uint32_t CPUSubType,
  32. std::string ArchName, uint32_t Align);
  33. public:
  34. explicit Slice(const MachOObjectFile &O);
  35. Slice(const MachOObjectFile &O, uint32_t Align);
  36. /// This constructor takes pre-specified \param CPUType , \param CPUSubType ,
  37. /// \param ArchName , \param Align instead of inferring them from the archive
  38. /// members.
  39. Slice(const Archive &A, uint32_t CPUType, uint32_t CPUSubType,
  40. std::string ArchName, uint32_t Align);
  41. static Expected<Slice> create(const Archive &A,
  42. LLVMContext *LLVMCtx = nullptr);
  43. static Expected<Slice> create(const IRObjectFile &IRO, uint32_t Align);
  44. void setP2Alignment(uint32_t Align) { P2Alignment = Align; }
  45. const Binary *getBinary() const { return B; }
  46. uint32_t getCPUType() const { return CPUType; }
  47. uint32_t getCPUSubType() const { return CPUSubType; }
  48. uint32_t getP2Alignment() const { return P2Alignment; }
  49. uint64_t getCPUID() const {
  50. return static_cast<uint64_t>(CPUType) << 32 | CPUSubType;
  51. }
  52. std::string getArchString() const {
  53. if (!ArchName.empty())
  54. return ArchName;
  55. return ("unknown(" + Twine(CPUType) + "," +
  56. Twine(CPUSubType & ~MachO::CPU_SUBTYPE_MASK) + ")")
  57. .str();
  58. }
  59. friend bool operator<(const Slice &Lhs, const Slice &Rhs) {
  60. if (Lhs.CPUType == Rhs.CPUType)
  61. return Lhs.CPUSubType < Rhs.CPUSubType;
  62. // force arm64-family to follow after all other slices for
  63. // compatibility with cctools lipo
  64. if (Lhs.CPUType == MachO::CPU_TYPE_ARM64)
  65. return false;
  66. if (Rhs.CPUType == MachO::CPU_TYPE_ARM64)
  67. return true;
  68. // Sort by alignment to minimize file size
  69. return Lhs.P2Alignment < Rhs.P2Alignment;
  70. }
  71. };
  72. Error writeUniversalBinary(ArrayRef<Slice> Slices, StringRef OutputFileName);
  73. Error writeUniversalBinaryToStream(ArrayRef<Slice> Slices, raw_ostream &Out);
  74. } // end namespace object
  75. } // end namespace llvm
  76. #endif // LLVM_OBJECT_MACHOUNIVERSALWRITER_H