Coverage Report

Created: 2019-02-20 07:29

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