Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Error.h"
18
#include "Memory.h"
19
#include "lld/Config/Version.h"
20
#include "lld/Core/Reproduce.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.77k
ELFOptTable::ELFOptTable() : OptTable(OptInfo) {}
53
54
// Parse -color-diagnostics={auto,always,never} or -no-color-diagnostics.
55
1.77k
static bool getColorDiagnostics(opt::InputArgList &Args) {
56
1.77k
  auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
57
1.77k
                              OPT_no_color_diagnostics);
58
1.77k
  if (!Arg)
59
1.77k
    return ErrorOS->has_colors();
60
4
  
if (4
Arg->getOption().getID() == OPT_color_diagnostics4
)
61
1
    return true;
62
3
  
if (3
Arg->getOption().getID() == OPT_no_color_diagnostics3
)
63
1
    return false;
64
2
65
2
  StringRef S = Arg->getValue();
66
2
  if (S == "auto")
67
0
    return ErrorOS->has_colors();
68
2
  
if (2
S == "always"2
)
69
1
    return true;
70
1
  
if (1
S != "never"1
)
71
0
    error("unknown option: -color-diagnostics=" + S);
72
1.77k
  return false;
73
1.77k
}
74
75
1.77k
static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) {
76
1.77k
  if (auto *
Arg1.77k
= Args.getLastArg(OPT_rsp_quoting)) {
77
3
    StringRef S = Arg->getValue();
78
3
    if (
S != "windows" && 3
S != "posix"2
)
79
1
      error("invalid response file quoting: " + S);
80
3
    if (S == "windows")
81
1
      return cl::TokenizeWindowsCommandLine;
82
2
    return cl::TokenizeGNUCommandLine;
83
2
  }
84
1.77k
  
if (1.77k
Triple(sys::getProcessTriple()).getOS() == Triple::Win321.77k
)
85
0
    return cl::TokenizeWindowsCommandLine;
86
1.77k
  return cl::TokenizeGNUCommandLine;
87
1.77k
}
88
89
// Parses a given list of options.
90
1.77k
opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> Argv) {
91
1.77k
  // Make InputArgList from string vectors.
92
1.77k
  unsigned MissingIndex;
93
1.77k
  unsigned MissingCount;
94
1.77k
  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
95
1.77k
96
1.77k
  // We need to get the quoting style for response files before parsing all
97
1.77k
  // options so we parse here before and ignore all the options but
98
1.77k
  // --rsp-quoting.
99
1.77k
  opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
100
1.77k
101
1.77k
  // Expand response files (arguments in the form of @<filename>)
102
1.77k
  // and then parse the argument again.
103
1.77k
  cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec);
104
1.77k
  Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
105
1.77k
106
1.77k
  // Interpret -color-diagnostics early so that error messages
107
1.77k
  // for unknown flags are colored.
108
1.77k
  Config->ColorDiagnostics = getColorDiagnostics(Args);
109
1.77k
  if (MissingCount)
110
2
    error(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
111
1.77k
112
1.77k
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
113
5
    error("unknown argument: " + Arg->getSpelling());
114
1.77k
  return Args;
115
1.77k
}
116
117
2
void elf::printHelp(const char *Argv0) {
118
2
  ELFOptTable Table;
119
2
  Table.PrintHelp(outs(), Argv0, "lld", false /*ShowHidden*/,
120
2
                  true /*ShowAllAliases*/);
121
2
  outs() << "\n";
122
2
123
2
  // Scripts generated by Libtool versions up to at least 2.4.6 (the most
124
2
  // recent version as of March 2017) expect /: supported targets:.* elf/
125
2
  // in a message for the -help option. If it doesn't match, the scripts
126
2
  // assume that the linker doesn't support very basic features such as
127
2
  // shared libraries. Therefore, we need to print out at least "elf".
128
2
  // Here, we print out all the targets that we support.
129
2
  outs() << Argv0 << ": supported targets: "
130
2
         << "elf32-i386 elf32-iamcu elf32-littlearm elf32-ntradbigmips "
131
2
         << "elf32-ntradlittlemips elf32-powerpc elf32-tradbigmips "
132
2
         << "elf32-tradlittlemips elf32-x86-64 "
133
2
         << "elf64-amdgpu elf64-littleaarch64 elf64-powerpc elf64-tradbigmips "
134
2
         << "elf64-tradlittlemips elf64-x86-64\n";
135
2
}
136
137
// Reconstructs command line arguments so that so that you can re-run
138
// the same command with the same inputs. This is for --reproduce.
139
10
std::string elf::createResponseFile(const opt::InputArgList &Args) {
140
10
  SmallString<0> Data;
141
10
  raw_svector_ostream OS(Data);
142
10
  OS << "--chroot .\n";
143
10
144
10
  // Copy the command line to the output while rewriting paths.
145
57
  for (auto *Arg : Args) {
146
57
    switch (Arg->getOption().getUnaliasedOption().getID()) {
147
9
    case OPT_reproduce:
148
9
      break;
149
10
    case OPT_INPUT:
150
10
      OS << quote(rewritePath(Arg->getValue())) << "\n";
151
10
      break;
152
7
    case OPT_o:
153
7
      // If -o path contains directories, "lld @response.txt" will likely
154
7
      // fail because the archive we are creating doesn't contain empty
155
7
      // directories for the output path (-o doesn't create directories).
156
7
      // Strip directories to prevent the issue.
157
7
      OS << "-o " << quote(sys::path::filename(Arg->getValue())) << "\n";
158
7
      break;
159
8
    case OPT_dynamic_list:
160
8
    case OPT_library_path:
161
8
    case OPT_rpath:
162
8
    case OPT_script:
163
8
    case OPT_symbol_ordering_file:
164
8
    case OPT_version_script:
165
8
      OS << Arg->getSpelling() << " " << quote(rewritePath(Arg->getValue()))
166
8
         << "\n";
167
8
      break;
168
23
    default:
169
23
      OS << toString(Arg) << "\n";
170
57
    }
171
57
  }
172
10
  return Data.str();
173
10
}
174
175
// Find a file by concatenating given paths. If a resulting path
176
// starts with "=", the character is replaced with a --sysroot value.
177
34
static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
178
34
  SmallString<128> S;
179
34
  if (Path1.startswith("="))
180
5
    path::append(S, Config->Sysroot, Path1.substr(1), Path2);
181
34
  else
182
29
    path::append(S, Path1, Path2);
183
34
184
34
  if (fs::exists(S))
185
28
    return S.str().str();
186
6
  return None;
187
6
}
188
189
17
Optional<std::string> elf::findFromSearchPaths(StringRef Path) {
190
17
  for (StringRef Dir : Config->SearchPaths)
191
13
    
if (Optional<std::string> 13
S13
= findFile(Dir, Path))
192
10
      return S;
193
7
  return None;
194
7
}
195
196
// This is for -lfoo. We'll look for libfoo.so or libfoo.a from
197
// search paths.
198
33
Optional<std::string> elf::searchLibrary(StringRef Name) {
199
33
  if (Name.startswith(":"))
200
10
    return findFromSearchPaths(Name.substr(1));
201
23
202
23
  
for (StringRef Dir : Config->SearchPaths) 23
{
203
19
    if (!Config->Static)
204
13
      
if (Optional<std::string> 13
S13
= findFile(Dir, "lib" + Name + ".so"))
205
11
        return S;
206
8
    
if (Optional<std::string> 8
S8
= findFile(Dir, "lib" + Name + ".a"))
207
7
      return S;
208
5
  }
209
5
  return None;
210
5
}