Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/lib/ReaderWriter/FileArchive.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/ReaderWriter/FileArchive.cpp -----------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "lld/Core/ArchiveLibraryFile.h"
11
#include "lld/Core/File.h"
12
#include "lld/Core/LLVM.h"
13
#include "lld/Core/Reader.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include "llvm/ADT/StringRef.h"
16
#include "llvm/BinaryFormat/Magic.h"
17
#include "llvm/Object/Archive.h"
18
#include "llvm/Object/Error.h"
19
#include "llvm/Support/Debug.h"
20
#include "llvm/Support/ErrorOr.h"
21
#include "llvm/Support/FileSystem.h"
22
#include "llvm/Support/Format.h"
23
#include "llvm/Support/MemoryBuffer.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include <memory>
26
#include <set>
27
#include <string>
28
#include <system_error>
29
#include <unordered_map>
30
#include <utility>
31
#include <vector>
32
33
using llvm::object::Archive;
34
using llvm::file_magic;
35
using llvm::identify_magic;
36
37
namespace lld {
38
39
namespace {
40
41
/// \brief The FileArchive class represents an Archive Library file
42
class FileArchive : public lld::ArchiveLibraryFile {
43
public:
44
  FileArchive(std::unique_ptr<MemoryBuffer> mb, const Registry &reg,
45
              StringRef path, bool logLoading)
46
      : ArchiveLibraryFile(path), _mb(std::shared_ptr<MemoryBuffer>(mb.release())),
47
10
        _registry(reg), _logLoading(logLoading) {}
48
49
  /// \brief Check if any member of the archive contains an Atom with the
50
  /// specified name and return the File object for that member, or nullptr.
51
11
  File *find(StringRef name) override {
52
11
    auto member = _symbolMemberMap.find(name);
53
11
    if (member == _symbolMemberMap.end())
54
6
      return nullptr;
55
5
    Archive::Child c = member->second;
56
5
57
5
    // Don't return a member already returned
58
5
    Expected<StringRef> buf = c.getBuffer();
59
5
    if (
!buf5
) {
60
0
      // TODO: Actually report errors helpfully.
61
0
      consumeError(buf.takeError());
62
0
      return nullptr;
63
0
    }
64
5
    const char *memberStart = buf->data();
65
5
    if (_membersInstantiated.count(memberStart))
66
0
      return nullptr;
67
5
    _membersInstantiated.insert(memberStart);
68
5
69
5
    std::unique_ptr<File> result;
70
5
    if (instantiateMember(c, result))
71
0
      return nullptr;
72
5
73
5
    File *file = result.get();
74
5
    _filesReturned.push_back(std::move(result));
75
5
76
5
    // Give up the file pointer. It was stored and will be destroyed with destruction of FileArchive
77
5
    return file;
78
5
  }
79
80
  /// \brief parse each member
81
  std::error_code
82
2
  parseAllMembers(std::vector<std::unique_ptr<File>> &result) override {
83
2
    if (std::error_code ec = parse())
84
0
      return ec;
85
2
    llvm::Error err = llvm::Error::success();
86
2
    for (auto mf = _archive->child_begin(err), me = _archive->child_end();
87
4
         
mf != me4
;
++mf2
) {
88
2
      std::unique_ptr<File> file;
89
2
      if (std::error_code 
ec2
= instantiateMember(*mf, file)) {
90
0
        // err is Success (or we wouldn't be in the loop body) but we can't
91
0
        // return without testing or consuming it.
92
0
        consumeError(std::move(err));
93
0
        return ec;
94
0
      }
95
2
      result.push_back(std::move(file));
96
2
    }
97
2
    
if (2
err2
)
98
0
      return errorToErrorCode(std::move(err));
99
2
    return std::error_code();
100
2
  }
101
102
0
  const AtomRange<DefinedAtom> defined() const override {
103
0
    return _noDefinedAtoms;
104
0
  }
105
106
0
  const AtomRange<UndefinedAtom> undefined() const override {
107
0
    return _noUndefinedAtoms;
108
0
  }
109
110
0
  const AtomRange<SharedLibraryAtom> sharedLibrary() const override {
111
0
    return _noSharedLibraryAtoms;
112
0
  }
113
114
0
  const AtomRange<AbsoluteAtom> absolute() const override {
115
0
    return _noAbsoluteAtoms;
116
0
  }
117
118
8
  void clearAtoms() override {
119
8
    _noDefinedAtoms.clear();
120
8
    _noUndefinedAtoms.clear();
121
8
    _noSharedLibraryAtoms.clear();
122
8
    _noAbsoluteAtoms.clear();
123
8
  }
124
125
protected:
126
10
  std::error_code doParse() override {
127
10
    // Make Archive object which will be owned by FileArchive object.
128
10
    llvm::Error Err = llvm::Error::success();
129
10
    _archive.reset(new Archive(_mb->getMemBufferRef(), Err));
130
10
    if (Err)
131
0
      return errorToErrorCode(std::move(Err));
132
10
    std::error_code ec;
133
10
    if ((ec = buildTableOfContents()))
134
0
      return ec;
135
10
    return std::error_code();
136
10
  }
137
138
private:
139
  std::error_code instantiateMember(Archive::Child member,
140
7
                                    std::unique_ptr<File> &result) const {
141
7
    Expected<llvm::MemoryBufferRef> mbOrErr = member.getMemoryBufferRef();
142
7
    if (!mbOrErr)
143
0
      return errorToErrorCode(mbOrErr.takeError());
144
7
    llvm::MemoryBufferRef mb = mbOrErr.get();
145
7
    std::string memberPath = (_archive->getFileName() + "("
146
7
                           + mb.getBufferIdentifier() + ")").str();
147
7
148
7
    if (_logLoading)
149
0
      llvm::errs() << memberPath << "\n";
150
7
151
7
    std::unique_ptr<MemoryBuffer> memberMB(MemoryBuffer::getMemBuffer(
152
7
        mb.getBuffer(), mb.getBufferIdentifier(), false));
153
7
154
7
    ErrorOr<std::unique_ptr<File>> fileOrErr =
155
7
        _registry.loadFile(std::move(memberMB));
156
7
    if (std::error_code ec = fileOrErr.getError())
157
0
      return ec;
158
7
    result = std::move(fileOrErr.get());
159
7
    if (std::error_code ec = result->parse())
160
0
      return ec;
161
7
    result->setArchivePath(_archive->getFileName());
162
7
163
7
    // The memory buffer is co-owned by the archive file and the children,
164
7
    // so that the bufffer is deallocated when all the members are destructed.
165
7
    result->setSharedMemoryBuffer(_mb);
166
7
    return std::error_code();
167
7
  }
168
169
10
  std::error_code buildTableOfContents() {
170
10
    DEBUG_WITH_TYPE("FileArchive", llvm::dbgs()
171
10
                                       << "Table of contents for archive '"
172
10
                                       << _archive->getFileName() << "':\n");
173
10
    for (const Archive::Symbol &sym : _archive->symbols()) {
174
10
      StringRef name = sym.getName();
175
10
      Expected<Archive::Child> memberOrErr = sym.getMember();
176
10
      if (!memberOrErr)
177
0
        return errorToErrorCode(memberOrErr.takeError());
178
10
      Archive::Child member = memberOrErr.get();
179
10
      DEBUG_WITH_TYPE("FileArchive",
180
10
                      llvm::dbgs()
181
10
                          << llvm::format("0x%08llX ",
182
10
                                          member.getBuffer()->data())
183
10
                          << "'" << name << "'\n");
184
10
      _symbolMemberMap.insert(std::make_pair(name, member));
185
10
    }
186
10
    return std::error_code();
187
10
  }
188
189
  typedef std::unordered_map<StringRef, Archive::Child> MemberMap;
190
  typedef std::set<const char *> InstantiatedSet;
191
192
  std::shared_ptr<MemoryBuffer> _mb;
193
  const Registry &_registry;
194
  std::unique_ptr<Archive> _archive;
195
  MemberMap _symbolMemberMap;
196
  InstantiatedSet _membersInstantiated;
197
  bool _logLoading;
198
  std::vector<std::unique_ptr<MemoryBuffer>> _memberBuffers;
199
  std::vector<std::unique_ptr<File>> _filesReturned;
200
};
201
202
class ArchiveReader : public Reader {
203
public:
204
223
  ArchiveReader(bool logLoading) : _logLoading(logLoading) {}
205
206
291
  bool canParse(file_magic magic, MemoryBufferRef) const override {
207
291
    return magic == file_magic::archive;
208
291
  }
209
210
  ErrorOr<std::unique_ptr<File>> loadFile(std::unique_ptr<MemoryBuffer> mb,
211
10
                                          const Registry &reg) const override {
212
10
    StringRef path = mb->getBufferIdentifier();
213
10
    std::unique_ptr<File> ret =
214
10
        llvm::make_unique<FileArchive>(std::move(mb), reg, path, _logLoading);
215
10
    return std::move(ret);
216
10
  }
217
218
private:
219
  bool _logLoading;
220
};
221
222
} // anonymous namespace
223
224
223
void Registry::addSupportArchives(bool logLoading) {
225
223
  add(std::unique_ptr<Reader>(new ArchiveReader(logLoading)));
226
223
}
227
228
} // namespace lld