MachOUniversalWriter.h 3.4 KB

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