Coverage Report

Created: 2018-07-19 03:59

/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
164
    void clear() {
47
164
      Parent = nullptr;
48
164
      Index = 0;
49
164
    }
50
51
289
    bool operator==(const ObjectForArch &Other) const {
52
289
      return (Parent == Other.Parent) && 
(Index == Other.Index)69
;
53
289
    }
54
55
194
    ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
56
446
    uint32_t getCPUType() const {
57
446
      if (Parent->getMagic() == MachO::FAT_MAGIC)
58
437
        return Header.cputype;
59
9
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
60
9
        return Header64.cputype;
61
446
    }
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.61k
    uint32_t getOffset() const {
69
2.61k
      if (Parent->getMagic() == MachO::FAT_MAGIC)
70
2.56k
        return Header.offset;
71
50
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
72
50
        return Header64.offset;
73
2.61k
    }
74
1.02k
    uint32_t getSize() const {
75
1.02k
      if (Parent->getMagic() == MachO::FAT_MAGIC)
76
1.00k
        return Header.size;
77
19
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
78
19
        return Header64.size;
79
1.02k
    }
80
503
    uint32_t getAlign() const {
81
503
      if (Parent->getMagic() == MachO::FAT_MAGIC)
82
485
        return Header.align;
83
18
      else // Parent->getMagic() == MachO::FAT_MAGIC_64
84
18
        return Header64.align;
85
503
    }
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
117
    std::string getArchFlagName() const {
93
117
      const char *McpuDefault, *ArchFlag;
94
117
      if (Parent->getMagic() == MachO::FAT_MAGIC) {
95
110
        Triple T =
96
110
            MachOObjectFile::getArchTriple(Header.cputype, Header.cpusubtype,
97
110
                                           &McpuDefault, &ArchFlag);
98
110
      } 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
117
      if (ArchFlag) {
105
116
        std::string ArchFlagName(ArchFlag);
106
116
        return ArchFlagName;
107
116
      } else {
108
1
        std::string ArchFlagName("");
109
1
        return ArchFlagName;
110
1
      }
111
117
    }
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
190
    object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
122
    const ObjectForArch *operator->() const { return &Obj; }
123
65
    const ObjectForArch &operator*() const { return Obj; }
124
125
289
    bool operator==(const object_iterator &Other) const {
126
289
      return Obj == Other.Obj;
127
289
    }
128
289
    bool operator!=(const object_iterator &Other) const {
129
289
      return !(*this == Other);
130
289
    }
131
132
194
    object_iterator& operator++() {  // Preincrement
133
194
      Obj = Obj.getNext();
134
194
      return *this;
135
194
    }
136
  };
137
138
  MachOUniversalBinary(MemoryBufferRef Souce, Error &Err);
139
  static Expected<std::unique_ptr<MachOUniversalBinary>>
140
  create(MemoryBufferRef Source);
141
142
95
  object_iterator begin_objects() const {
143
95
    return ObjectForArch(this, 0);
144
95
  }
145
95
  object_iterator end_objects() const {
146
95
    return ObjectForArch(nullptr, 0);
147
95
  }
148
149
29
  iterator_range<object_iterator> objects() const {
150
29
    return make_range(begin_objects(), end_objects());
151
29
  }
152
153
5.92k
  uint32_t getMagic() const { return Magic; }
154
1.01k
  uint32_t getNumberOfObjects() const { return NumberOfObjects; }
155
156
  // Cast methods.
157
3.42k
  static bool classof(Binary const *V) {
158
3.42k
    return V->isMachOUniversalBinary();
159
3.42k
  }
160
161
  Expected<std::unique_ptr<MachOObjectFile>>
162
  getObjectForArch(StringRef ArchName) const;
163
};
164
165
}
166
}
167
168
#endif