summaryrefslogtreecommitdiffstats
path: root/lld/lib/Driver/LinkerInvocation.cpp
blob: 08581655c76e10603df2ad40c6e5bba936098124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===- lib/Driver/LinkerInvocation.cpp - Linker Invocation ----------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lld/Driver/LinkerInvocation.h"

#include "lld/Core/InputFiles.h"
#include "lld/Core/Resolver.h"
#include "lld/Driver/Target.h"

#include "llvm/Support/raw_ostream.h"

using namespace lld;

void LinkerInvocation::operator()() {
    // Create target.
    std::unique_ptr<Target> target(Target::create(_options));

    if (!target) {
      llvm::errs() << "Failed to create target for " << _options._target
                   << "\n";
      return;
    }

    // Read inputs
    InputFiles inputs;
    for (const auto &input : _options._input) {
      auto reader = target->getReader(input);
      if (error_code ec = reader) {
        llvm::errs() << "Failed to get reader for: " << input.getPath() << ": "
                     << ec.message() << "\n";
        return;
      }

      auto buffer = input.getBuffer();
      if (error_code ec = buffer) {
        llvm::errs() << "Failed to read file: " << input.getPath() << ": "
                     << ec.message() << "\n";
        return;
      }

      std::vector<std::unique_ptr<File>> files;
      if (llvm::error_code ec = reader->readFile(
            buffer->getBufferIdentifier(), files)) {
        llvm::errs() << "Failed to read file: " << input.getPath() << ": "
                     << ec.message() << "\n";
        return;
      }
      inputs.appendFiles(files);
    }

    ResolverOptions ro;
    Resolver resolver(ro, inputs);
    resolver.resolve();
    File &merged = resolver.resultFile();

    auto writer = target->getWriter();
    if (error_code ec = writer) {
      llvm::errs() << "Failed to get writer: " << ec.message() << ".\n";
      return;
    }

    if (error_code ec = writer->writeFile(merged, _options._outputPath))
      llvm::errs() << "Failed to write file: " << ec.message() << "\n";
  }
OpenPOWER on IntegriCloud