MachOUniversal.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- MachOUniversal.h - Mach-O universal binaries -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares Mach-O fat/universal binaries.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
  14. #define LLVM_OBJECT_MACHOUNIVERSAL_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/ADT/iterator_range.h"
  18. #include "llvm/Object/Archive.h"
  19. #include "llvm/Object/Binary.h"
  20. #include "llvm/Object/MachO.h"
  21. #include "llvm/Support/ErrorOr.h"
  22. #include "llvm/Support/MachO.h"
  23. namespace llvm {
  24. namespace object {
  25. class MachOUniversalBinary : public Binary {
  26. virtual void anchor();
  27. uint32_t NumberOfObjects;
  28. public:
  29. class ObjectForArch {
  30. const MachOUniversalBinary *Parent;
  31. /// \brief Index of object in the universal binary.
  32. uint32_t Index;
  33. /// \brief Descriptor of the object.
  34. MachO::fat_arch Header;
  35. public:
  36. ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
  37. void clear() {
  38. Parent = nullptr;
  39. Index = 0;
  40. }
  41. bool operator==(const ObjectForArch &Other) const {
  42. return (Parent == Other.Parent) && (Index == Other.Index);
  43. }
  44. ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
  45. uint32_t getCPUType() const { return Header.cputype; }
  46. uint32_t getCPUSubType() const { return Header.cpusubtype; }
  47. uint32_t getOffset() const { return Header.offset; }
  48. uint32_t getSize() const { return Header.size; }
  49. uint32_t getAlign() const { return Header.align; }
  50. std::string getArchTypeName() const {
  51. Triple T = MachOObjectFile::getArch(Header.cputype, Header.cpusubtype);
  52. return T.getArchName();
  53. }
  54. ErrorOr<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;
  55. ErrorOr<std::unique_ptr<Archive>> getAsArchive() const;
  56. };
  57. class object_iterator {
  58. ObjectForArch Obj;
  59. public:
  60. object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
  61. const ObjectForArch *operator->() const { return &Obj; }
  62. const ObjectForArch &operator*() const { return Obj; }
  63. bool operator==(const object_iterator &Other) const {
  64. return Obj == Other.Obj;
  65. }
  66. bool operator!=(const object_iterator &Other) const {
  67. return !(*this == Other);
  68. }
  69. object_iterator& operator++() { // Preincrement
  70. Obj = Obj.getNext();
  71. return *this;
  72. }
  73. };
  74. MachOUniversalBinary(MemoryBufferRef Souce, std::error_code &EC);
  75. static ErrorOr<std::unique_ptr<MachOUniversalBinary>>
  76. create(MemoryBufferRef Source);
  77. object_iterator begin_objects() const {
  78. return ObjectForArch(this, 0);
  79. }
  80. object_iterator end_objects() const {
  81. return ObjectForArch(nullptr, 0);
  82. }
  83. iterator_range<object_iterator> objects() const {
  84. return make_range(begin_objects(), end_objects());
  85. }
  86. uint32_t getNumberOfObjects() const { return NumberOfObjects; }
  87. // Cast methods.
  88. static inline bool classof(Binary const *V) {
  89. return V->isMachOUniversalBinary();
  90. }
  91. ErrorOr<std::unique_ptr<MachOObjectFile>>
  92. getObjectForArch(StringRef ArchName) const;
  93. };
  94. }
  95. }
  96. #endif