Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/MachOUniversal.h
Line
Count
Source (jump to first uncovered line)
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
14
#ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
15
#define LLVM_OBJECT_MACHOUNIVERSAL_H
16
17
#include "llvm/ADT/Triple.h"
18
#include "llvm/ADT/iterator_range.h"
19
#include "llvm/BinaryFormat/MachO.h"
20
#include "llvm/Object/Archive.h"
21
#include "llvm/Object/Binary.h"
22
#include "llvm/Object/MachO.h"
23
24
namespace llvm {
25
class StringRef;
26
27
namespace object {
28
29
class MachOUniversalBinary : public Binary {
30
  virtual void anchor();
31
32
  uint32_t Magic;
33
  uint32_t NumberOfObjects;
34
public:
35
0
  class ObjectForArch {
36
    const MachOUniversalBinary *Parent;
37
    /// Index of object in the universal binary.
38
    uint32_t Index;
39
    /// Descriptor of the object.
40
    MachO::fat_arch Header;
41
    MachO::fat_arch_64 Header64;
42
43
  public:
44
    ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
45
46
168
    void clear() {
47
168
      Parent = nullptr;
48
168
      Index = 0;
49
168
    }
50
51
296
    bool operator==(const ObjectForArch &Other) const {
52
296
      return (Parent == Other.Parent) && 
(Index == Other.Index)72
;
53
296
    }
54
55
200
    ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
56
450
    uint32_t getCPUType() const {
57
450
      if (Parent->getMagic() == MachO::FAT_MAGIC)
58
441
        return Header.cputype;
59
9
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
60
9
        return Header64.cputype;
61
450
    }
62
144
    uint32_t getCPUSubType() const {
63
144
      if (Parent->getMagic() == MachO::FAT_MAGIC)
64
141
        return Header.cpusubtype;
65
3
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
66
3
        return Header64.cpusubtype;
67
144
    }
68
2.63k
    uint32_t getOffset() const {
69
2.63k
      if (Parent->getMagic() == MachO::FAT_MAGIC)
70
2.58k
        return Header.offset;
71
50
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
72
50
        return Header64.offset;
73
2.63k
    }
74
1.03k
    uint32_t getSize() const {
75
1.03k
      if (Parent->getMagic() == MachO::FAT_MAGIC)
76
1.01k
        return Header.size;
77
19
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
78
19
        return Header64.size;
79
1.03k
    }
80
509
    uint32_t getAlign() const {
81
509
      if (Parent->getMagic() == MachO::FAT_MAGIC)
82
491
        return Header.align;
83
18
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
84
18
        return Header64.align;
85
509
    }
86
    uint32_t getReserved() const {
87
      if (Parent->getMagic() == MachO::FAT_MAGIC)
88
        return 0;
89
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
90
        return Header64.reserved;
91
    }
92
121
    std::string getArchFlagName() const {
93
121
      const char *McpuDefault, *ArchFlag;
94
121
      if (Parent->getMagic() == MachO::FAT_MAGIC) {
95
114
        Triple T =
96
114
            MachOObjectFile::getArchTriple(Header.cputype, Header.cpusubtype,
97
114
                                           &McpuDefault, &ArchFlag);
98
114
      } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
99
7
        Triple T =
100
7
            MachOObjectFile::getArchTriple(Header64.cputype,
101
7
                                           Header64.cpusubtype,
102
7
                                           &McpuDefault, &ArchFlag);
103
7
      }
104
121
      if (ArchFlag) {
105
120
        std::string ArchFlagName(ArchFlag);
106
120
        return ArchFlagName;
107
120
      } else {
108
1
        std::string ArchFlagName("");
109
1
        return ArchFlagName;
110
1
      }
111
121
    }
112
113
    Expected<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;
114
115
    Expected<std::unique_ptr<Archive>> getAsArchive() const;
116
  };
117
118
  class object_iterator {
119
    ObjectForArch Obj;
120
  public:
121
192
    object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
122
    const ObjectForArch *operator->() const { return &Obj; }
123
64
    const ObjectForArch &operator*() const { return Obj; }
124
125
296
    bool operator==(const object_iterator &Other) const {
126
296
      return Obj == Other.Obj;
127
296
    }
128
296
    bool operator!=(const object_iterator &Other) const {
129
296
      return !(*this == Other);
130
296
    }
131
132
200
    object_iterator& operator++() {  // Preincrement
133
200
      Obj = Obj.getNext();
134
200
      return *this;
135
200
    }
136
  };
137
138
  MachOUniversalBinary(MemoryBufferRef Souce, Error &Err);
139
  static Expected<std::unique_ptr<MachOUniversalBinary>>
140
  create(MemoryBufferRef Source);
141
142
96
  object_iterator begin_objects() const {
143
96
    return ObjectForArch(this, 0);
144
96
  }
145
96
  object_iterator end_objects() const {
146
96
    return ObjectForArch(nullptr, 0);
147
96
  }
148
149
28
  iterator_range<object_iterator> objects() const {
150
28
    return make_range(begin_objects(), end_objects());
151
28
  }
152
153
5.98k
  uint32_t getMagic() const { return Magic; }
154
1.03k
  uint32_t getNumberOfObjects() const { return NumberOfObjects; }
155
156
  // Cast methods.
157
3.71k
  static bool classof(Binary const *V) {
158
3.71k
    return V->isMachOUniversalBinary();
159
3.71k
  }
160
161
  Expected<std::unique_ptr<MachOObjectFile>>
162
  getObjectForArch(StringRef ArchName) const;
163
};
164
165
}
166
}
167
168
#endif