Coverage Report

Created: 2018-01-17 17:22

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/DriverUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DriverUtils.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
// This file contains utility functions for the driver. Because there
11
// are so many small functions, we created this separate file to make
12
// Driver.cpp less cluttered.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "Driver.h"
17
#include "lld/Common/ErrorHandler.h"
18
#include "lld/Common/Memory.h"
19
#include "lld/Common/Reproduce.h"
20
#include "lld/Common/Version.h"
21
#include "llvm/ADT/Optional.h"
22
#include "llvm/ADT/STLExtras.h"
23
#include "llvm/ADT/Triple.h"
24
#include "llvm/Option/Option.h"
25
#include "llvm/Support/CommandLine.h"
26
#include "llvm/Support/FileSystem.h"
27
#include "llvm/Support/Path.h"
28
#include "llvm/Support/Process.h"
29
30
using namespace llvm;
31
using namespace llvm::sys;
32
33
using namespace lld;
34
using namespace lld::elf;
35
36
// Create OptTable
37
38
// Create prefix string literals used in Options.td
39
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
40
#include "Options.inc"
41
#undef PREFIX
42
43
// Create table mapping all options defined in Options.td
44
static const opt::OptTable::Info OptInfo[] = {
45
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
46
  {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
47
   X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
48
#include "Options.inc"
49
#undef OPTION
50
};
51
52
1.98k
ELFOptTable::ELFOptTable() : OptTable(OptInfo) {}
53
54
// Set color diagnostics according to -color-diagnostics={auto,always,never}
55
// or -no-color-diagnostics flags.
56
1.98k
static void handleColorDiagnostics(opt::InputArgList &Args) {
57
1.98k
  auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
58
1.98k
                              OPT_no_color_diagnostics);
59
1.98k
  if (!Arg)
60
1.98k
    return;
61
4
  else if (Arg->getOption().getID() == OPT_color_diagnostics)
62
1
    errorHandler().ColorDiagnostics = true;
63
3
  else if (Arg->getOption().getID() == OPT_no_color_diagnostics)
64
1
    errorHandler().ColorDiagnostics = false;
65
2
  else {
66
2
    StringRef S = Arg->getValue();
67
2
    if (S == "always")
68
1
      errorHandler().ColorDiagnostics = true;
69
1
    else if (S == "never")
70
1
      errorHandler().ColorDiagnostics = false;
71
0
    else if (S != "auto")
72
0
      error("unknown option: -color-diagnostics=" + S);
73
2
  }
74
1.98k
}
75
76
1.98k
static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) {
77
1.98k
  if (auto *Arg = Args.getLastArg(OPT_rsp_quoting)) {
78
3
    StringRef S = Arg->getValue();
79
3
    if (S != "windows" && 
S != "posix"2
)
80
1
      error("invalid response file quoting: " + S);
81
3
    if (S == "windows")
82
1
      return cl::TokenizeWindowsCommandLine;
83
2
    return cl::TokenizeGNUCommandLine;
84
2
  }
85
1.98k
  if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32)
86
0
    return cl::TokenizeWindowsCommandLine;
87
1.98k
  return cl::TokenizeGNUCommandLine;
88
1.98k
}
89
90
// Parses a given list of options.
91
1.98k
opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) {
92
1.98k
  // Make InputArgList from string vectors.
93
1.98k
  unsigned MissingIndex;
94
1.98k
  unsigned MissingCount;
95
1.98k
  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
96
1.98k
97
1.98k
  // We need to get the quoting style for response files before parsing all
98
1.98k
  // options so we parse here before and ignore all the options but
99
1.98k
  // --rsp-quoting.
100
1.98k
  opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
101
1.98k
102
1.98k
  // Expand response files (arguments in the form of @<filename>)
103
1.98k
  // and then parse the argument again.
104
1.98k
  cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec);
105
1.98k
  Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
106
1.98k
107
1.98k
  handleColorDiagnostics(Args);
108
1.98k
  if (MissingCount)
109
2
    error(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
110
1.98k
111
1.98k
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
112
6
    error("unknown argument: " + Arg->getSpelling());
113
1.98k
  return Args;
114
1.98k
}
115
116
2
void elf::printHelp(const char *Argv0) {
117
2
  ELFOptTable().PrintHelp(outs(), Argv0, "lld", false /*ShowHidden*/,
118
2
                          true /*ShowAllAliases*/);
119
2
  outs() << "\n";
120
2
121
2
  // Scripts generated by Libtool versions up to at least 2.4.6 (the most
122
2
  // recent version as of March 2017) expect /: supported targets:.* elf/
123
2
  // in a message for the -help option. If it doesn't match, the scripts
124
2
  // assume that the linker doesn't support very basic features such as
125
2
  // shared libraries. Therefore, we need to print out at least "elf".
126
2
  // Here, we print out all the targets that we support.
127
2
  outs() << Argv0 << ": supported targets: "
128
2
         << "elf32-i386 elf32-iamcu elf32-littlearm elf32-ntradbigmips "
129
2
         << "elf32-ntradlittlemips elf32-powerpc elf32-tradbigmips "
130
2
         << "elf32-tradlittlemips elf32-x86-64 "
131
2
         << "elf64-amdgpu elf64-littleaarch64 elf64-powerpc elf64-tradbigmips "
132
2
         << "elf64-tradlittlemips elf64-x86-64\n";
133
2
}
134
135
// Reconstructs command line arguments so that so that you can re-run
136
// the same command with the same inputs. This is for --reproduce.
137
10
std::string elf::createResponseFile(const opt::InputArgList &Args) {
138
10
  SmallString<0> Data;
139
10
  raw_svector_ostream OS(Data);
140
10
  OS << "--chroot .\n";
141
10
142
10
  // Copy the command line to the output while rewriting paths.
143
59
  for (auto *Arg : Args) {
144
59
    switch (Arg->getOption().getUnaliasedOption().getID()) {
145
59
    case OPT_reproduce:
146
9
      break;
147
59
    case OPT_INPUT:
148
10
      OS << quote(rewritePath(Arg->getValue())) << "\n";
149
10
      break;
150
59
    case OPT_o:
151
7
      // If -o path contains directories, "lld @response.txt" will likely
152
7
      // fail because the archive we are creating doesn't contain empty
153
7
      // directories for the output path (-o doesn't create directories).
154
7
      // Strip directories to prevent the issue.
155
7
      OS << "-o " << quote(sys::path::filename(Arg->getValue())) << "\n";
156
7
      break;
157
59
    case OPT_dynamic_list:
158
10
    case OPT_library_path:
159
10
    case OPT_rpath:
160
10
    case OPT_script:
161
10
    case OPT_symbol_ordering_file:
162
10
    case OPT_sysroot:
163
10
    case OPT_version_script:
164
10
      OS << Arg->getSpelling() << " " << quote(rewritePath(Arg->getValue()))
165
10
         << "\n";
166
10
      break;
167
23
    default:
168
23
      OS << toString(*Arg) << "\n";
169
59
    }
170
59
  }
171
10
  return Data.str();
172
10
}
173
174
// Find a file by concatenating given paths. If a resulting path
175
// starts with "=", the character is replaced with a --sysroot value.
176
35
static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
177
35
  SmallString<128> S;
178
35
  if (Path1.startswith("="))
179
5
    path::append(S, Config->Sysroot, Path1.substr(1), Path2);
180
30
  else
181
30
    path::append(S, Path1, Path2);
182
35
183
35
  if (fs::exists(S))
184
29
    return S.str().str();
185
6
  return None;
186
6
}
187
188
19
Optional<std::string> elf::findFromSearchPaths(StringRef Path) {
189
19
  for (StringRef Dir : Config->SearchPaths)
190
14
    if (Optional<std::string> S = findFile(Dir, Path))
191
11
      return S;
192
19
  
return None8
;
193
19
}
194
195
// This is for -lfoo. We'll look for libfoo.so or libfoo.a from
196
// search paths.
197
33
Optional<std::string> elf::searchLibrary(StringRef Name) {
198
33
  if (Name.startswith(":"))
199
10
    return findFromSearchPaths(Name.substr(1));
200
23
201
23
  for (StringRef Dir : Config->SearchPaths) {
202
19
    if (!Config->Static)
203
13
      if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".so"))
204
11
        return S;
205
8
    if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a"))
206
7
      return S;
207
8
  }
208
23
  
return None5
;
209
23
}
210
211
// If a linker script doesn't exist in the current directory, we also look for
212
// the script in the '-L' search paths. This matches the behaviour of both '-T'
213
// and linker script INPUT() directives in ld.bfd.
214
418
Optional<std::string> elf::searchLinkerScript(StringRef Name) {
215
418
  if (fs::exists(Name))
216
413
    return Name.str();
217
5
  return findFromSearchPaths(Name);
218
5
}