summaryrefslogtreecommitdiffstats
path: root/lld/lib/Driver/LinkerInvocation.cpp
blob: 21662774bd943105add06f400214c8091b6a1e48 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//===- 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/PassManager.h"
#include "lld/Core/Resolver.h"
#include "lld/ReaderWriter/ELFTargetInfo.h"
#include "lld/ReaderWriter/Reader.h"
#include "lld/ReaderWriter/Writer.h"

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"

using namespace lld;

namespace {
std::unique_ptr<TargetInfo> createTargetInfo(const LinkerOptions &lo) {
  return ELFTargetInfo::create(lo);
}
}

void LinkerInvocation::operator()() {
  // Honor -mllvm
  if (!_options._llvmArgs.empty()) {
    unsigned NumArgs = _options._llvmArgs.size();
    const char **Args = new const char*[NumArgs + 2];
    Args[0] = "lld (LLVM option parsing)";
    for (unsigned i = 0; i != NumArgs; ++i)
      Args[i + 1] = _options._llvmArgs[i].c_str();
    Args[NumArgs + 1] = 0;
    llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
  }

  // Create target.
  std::unique_ptr<TargetInfo> targetInfo(createTargetInfo(_options));

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

  // Read inputs
  InputFiles inputs;
  for (const auto &input : _options._input) {
    auto reader = targetInfo->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->parseFile(std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(buffer->getBuffer(), buffer->getBufferIdentifier())), files)) {
      llvm::errs() << "Failed to read file: " << input.getPath() << ": "
                    << ec.message() << "\n";
      return;
    }
    inputs.appendFiles(files);
  }
  inputs.assignFileOrdinals();

  auto writer = targetInfo->getWriter();

  // Give writer a chance to add files
  writer->addFiles(inputs);

  Resolver resolver(*targetInfo, inputs);
  resolver.resolve();
  MutableFile &merged = resolver.resultFile();

  PassManager pm;
  targetInfo->addPasses(pm);
  pm.runOnFile(merged);

  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