Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/LTO.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LTO.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 "LTO.h"
11
#include "Config.h"
12
#include "InputFiles.h"
13
#include "LinkerScript.h"
14
#include "SymbolTable.h"
15
#include "Symbols.h"
16
#include "lld/Common/ErrorHandler.h"
17
#include "lld/Common/TargetOptionsCommandFlags.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/ADT/SmallString.h"
20
#include "llvm/ADT/StringRef.h"
21
#include "llvm/ADT/Twine.h"
22
#include "llvm/BinaryFormat/ELF.h"
23
#include "llvm/Bitcode/BitcodeReader.h"
24
#include "llvm/Bitcode/BitcodeWriter.h"
25
#include "llvm/IR/DiagnosticPrinter.h"
26
#include "llvm/LTO/Caching.h"
27
#include "llvm/LTO/Config.h"
28
#include "llvm/LTO/LTO.h"
29
#include "llvm/Object/SymbolicFile.h"
30
#include "llvm/Support/CodeGen.h"
31
#include "llvm/Support/Error.h"
32
#include "llvm/Support/FileSystem.h"
33
#include "llvm/Support/MemoryBuffer.h"
34
#include <algorithm>
35
#include <cstddef>
36
#include <memory>
37
#include <string>
38
#include <system_error>
39
#include <vector>
40
41
using namespace llvm;
42
using namespace llvm::object;
43
using namespace llvm::ELF;
44
45
using namespace lld;
46
using namespace lld::elf;
47
48
// Creates an empty file to store a list of object files for final
49
// linking of distributed ThinLTO.
50
6
static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {
51
6
  std::error_code EC;
52
6
  auto Ret =
53
6
      llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None);
54
6
  if (EC) {
55
2
    error("cannot open " + File + ": " + EC.message());
56
2
    return nullptr;
57
2
  }
58
4
  return Ret;
59
4
}
60
61
4
static std::string getThinLTOOutputFile(StringRef ModulePath) {
62
4
  return lto::getThinLTOOutputFile(ModulePath,
63
4
                                   Config->ThinLTOPrefixReplace.first,
64
4
                                   Config->ThinLTOPrefixReplace.second);
65
4
}
66
67
149
static lto::Config createConfig() {
68
149
  lto::Config C;
69
149
70
149
  // LLD supports the new relocations.
71
149
  C.Options = InitTargetOptionsFromCodeGenFlags();
72
149
  C.Options.RelaxELFRelocations = true;
73
149
74
149
  // Always emit a section per function/datum with LTO.
75
149
  C.Options.FunctionSections = true;
76
149
  C.Options.DataSections = true;
77
149
78
149
  if (Config->Relocatable)
79
3
    C.RelocModel = None;
80
146
  else if (Config->Pic)
81
88
    C.RelocModel = Reloc::PIC_;
82
58
  else
83
58
    C.RelocModel = Reloc::Static;
84
149
85
149
  C.CodeModel = GetCodeModelFromCMModel();
86
149
  C.DisableVerify = Config->DisableVerify;
87
149
  C.DiagHandler = diagnosticHandler;
88
149
  C.OptLevel = Config->LTOO;
89
149
  C.CPU = GetCPUStr();
90
149
91
149
  // Set up a custom pipeline if we've been asked to.
92
149
  C.OptPipeline = Config->LTONewPmPasses;
93
149
  C.AAPipeline = Config->LTOAAPipeline;
94
149
95
149
  // Set up optimization remarks if we've been asked to.
96
149
  C.RemarksFilename = Config->OptRemarksFilename;
97
149
  C.RemarksWithHotness = Config->OptRemarksWithHotness;
98
149
99
149
  C.SampleProfile = Config->LTOSampleProfile;
100
149
  C.UseNewPM = Config->LTONewPassManager;
101
149
  C.DebugPassManager = Config->LTODebugPassManager;
102
149
103
149
  if (Config->SaveTemps)
104
51
    checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
105
51
                              /*UseInputModulePath*/ true));
106
149
  return C;
107
149
}
108
109
149
BitcodeCompiler::BitcodeCompiler() {
110
149
  // Initialize LTOObj.
111
149
  lto::ThinBackend Backend;
112
149
113
149
  if (Config->ThinLTOIndexOnly) {
114
11
    StringRef Path = Config->ThinLTOIndexOnlyArg;
115
11
    if (!Path.empty())
116
1
      IndexFile = openFile(Path);
117
11
118
19
    auto OnIndexWrite = [&](const std::string &Identifier) {
119
19
      ObjectToIndexFileState[Identifier] = true;
120
19
    };
121
11
122
11
    Backend = lto::createWriteIndexesThinBackend(
123
11
        Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second,
124
11
        Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite);
125
138
  } else if (Config->ThinLTOJobs != -1U) {
126
2
    Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
127
2
  }
128
149
129
149
  LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend,
130
149
                                       Config->LTOPartitions);
131
149
132
149
  // Initialize UsedStartStop.
133
301
  for (Symbol *Sym : Symtab->getSymbols()) {
134
301
    StringRef Name = Sym->getName();
135
301
    for (StringRef Prefix : {"__start_", "__stop_"})
136
602
      if (Name.startswith(Prefix))
137
4
        UsedStartStop.insert(Name.substr(Prefix.size()));
138
301
  }
139
149
}
140
141
149
BitcodeCompiler::~BitcodeCompiler() = default;
142
143
244
static void undefine(Symbol *S) {
144
244
  replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT,
145
244
                           S->Type);
146
244
}
147
148
219
void BitcodeCompiler::add(BitcodeFile &F) {
149
219
  lto::InputFile &Obj = *F.Obj;
150
219
  bool IsExec = !Config->Shared && 
!Config->Relocatable90
;
151
219
152
219
  if (Config->ThinLTOIndexOnly)
153
23
    ObjectToIndexFileState.insert({Obj.getName(), false});
154
219
155
219
  ArrayRef<Symbol *> Syms = F.getSymbols();
156
219
  ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols();
157
219
  std::vector<lto::SymbolResolution> Resols(Syms.size());
158
219
159
219
  // Provide a resolution to the LTO API for each symbol.
160
575
  for (size_t I = 0, E = Syms.size(); I != E; 
++I356
) {
161
356
    Symbol *Sym = Syms[I];
162
356
    const lto::InputFile::Symbol &ObjSym = ObjSyms[I];
163
356
    lto::SymbolResolution &R = Resols[I];
164
356
165
356
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
166
356
    // reports two symbols for module ASM defined. Without this check, lld
167
356
    // flags an undefined in IR with a definition in ASM as prevailing.
168
356
    // Once IRObjectFile is fixed to report only one symbol this hack can
169
356
    // be removed.
170
356
    R.Prevailing = !ObjSym.isUndefined() && 
Sym->File == &F263
;
171
356
172
356
    // We ask LTO to preserve following global symbols:
173
356
    // 1) All symbols when doing relocatable link, so that them can be used
174
356
    //    for doing final link.
175
356
    // 2) Symbols that are used in regular objects.
176
356
    // 3) C named sections if we have corresponding __start_/__stop_ symbol.
177
356
    // 4) Symbols that are defined in bitcode files and used for dynamic linking.
178
356
    R.VisibleToRegularObj = Config->Relocatable || 
Sym->IsUsedInRegularObj351
||
179
356
                            
(248
R.Prevailing248
&&
Sym->includeInDynsym()165
) ||
180
356
                            
UsedStartStop.count(ObjSym.getSectionName())138
;
181
356
    const auto *DR = dyn_cast<Defined>(Sym);
182
356
    R.FinalDefinitionInLinkageUnit =
183
356
        (IsExec || 
Sym->Visibility != STV_DEFAULT210
) &&
DR190
&&
184
356
        // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
185
356
        // will be generated by for them, triggering linker errors.
186
356
        // Symbol section is always null for bitcode symbols, hence the check
187
356
        // for isElf(). Skip linker script defined symbols as well: they have
188
356
        // no File defined.
189
356
        
!(173
DR->Section == nullptr173
&&
(167
!Sym->File167
||
Sym->File->isElf()159
));
190
356
191
356
    if (R.Prevailing)
192
244
      undefine(Sym);
193
356
194
356
    // We tell LTO to not apply interprocedural optimization for wrapped
195
356
    // (with --wrap) symbols because otherwise LTO would inline them while
196
356
    // their values are still not final.
197
356
    R.LinkerRedefined = !Sym->CanInline;
198
356
  }
199
219
  checkError(LTOObj->add(std::move(F.Obj), Resols));
200
219
}
201
202
0
static void createEmptyIndex(StringRef ModulePath) {
203
0
  std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(ModulePath));
204
0
  if (Path.empty())
205
0
    return;
206
0
207
0
  std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
208
0
  if (!OS)
209
0
    return;
210
0
211
0
  ModuleSummaryIndex M(/*HaveGVs*/ false);
212
0
  M.setSkipModuleByDistributedBackend();
213
0
  WriteIndexToFile(M, *OS);
214
0
215
0
  if (Config->ThinLTOEmitImportsFiles)
216
0
    openFile(Path + ".imports");
217
0
}
218
219
// Merge all the bitcode files we have seen, codegen the result
220
// and return the resulting ObjectFile(s).
221
149
std::vector<InputFile *> BitcodeCompiler::compile() {
222
149
  unsigned MaxTasks = LTOObj->getMaxTasks();
223
149
  Buf.resize(MaxTasks);
224
149
  Files.resize(MaxTasks);
225
149
226
149
  // The --thinlto-cache-dir option specifies the path to a directory in which
227
149
  // to cache native object files for ThinLTO incremental builds. If a path was
228
149
  // specified, configure LTO to use it as the cache directory.
229
149
  lto::NativeObjectCache Cache;
230
149
  if (!Config->ThinLTOCacheDir.empty())
231
5
    Cache = check(
232
5
        lto::localCache(Config->ThinLTOCacheDir,
233
10
                        [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
234
10
                          Files[Task] = std::move(MB);
235
10
                        }));
236
149
237
149
  checkError(LTOObj->run(
238
185
      [&](size_t Task) {
239
185
        return llvm::make_unique<lto::NativeObjectStream>(
240
185
            llvm::make_unique<raw_svector_ostream>(Buf[Task]));
241
185
      },
242
149
      Cache));
243
149
244
149
  // Emit empty index files for non-indexed files
245
149
  if (Config->ThinLTOIndexOnly) {
246
11
    for (auto &Identifier : ObjectToIndexFileState)
247
23
      if (!Identifier.getValue()) {
248
4
        std::string Path = getThinLTOOutputFile(Identifier.getKey());
249
4
        openFile(Path + ".thinlto.bc");
250
4
251
4
        if (Config->ThinLTOEmitImportsFiles)
252
1
          openFile(Path + ".imports");
253
4
      }
254
11
  }
255
149
256
149
  // If LazyObjFile has not been added to link, emit empty index files.
257
149
  // This is needed because this is what GNU gold plugin does and we have a
258
149
  // distributed build system that depends on that behavior.
259
149
  if (Config->ThinLTOIndexOnly) {
260
11
    for (LazyObjFile *F : LazyObjFiles)
261
1
      if (!F->AddedToLink && 
isBitcode(F->MB)0
)
262
0
        createEmptyIndex(F->getName());
263
11
264
11
    if (!Config->LTOObjPath.empty())
265
1
      saveBuffer(Buf[0], Config->LTOObjPath);
266
11
267
11
    // ThinLTO with index only option is required to generate only the index
268
11
    // files. After that, we exit from linker and ThinLTO backend runs in a
269
11
    // distributed environment.
270
11
    if (IndexFile)
271
1
      IndexFile->close();
272
11
    return {};
273
11
  }
274
138
275
138
  if (!Config->ThinLTOCacheDir.empty())
276
5
    pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
277
138
278
138
  std::vector<InputFile *> Ret;
279
322
  for (unsigned I = 0; I != MaxTasks; 
++I184
) {
280
184
    if (Buf[I].empty())
281
10
      continue;
282
174
    if (Config->SaveTemps) {
283
60
      if (I == 0)
284
51
        saveBuffer(Buf[I], Config->OutputFile + ".lto.o");
285
9
      else
286
9
        saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o");
287
60
    }
288
174
    InputFile *Obj = createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp"));
289
174
    Ret.push_back(Obj);
290
174
  }
291
138
292
138
  for (std::unique_ptr<MemoryBuffer> &File : Files)
293
184
    if (File)
294
10
      Ret.push_back(createObjectFile(*File));
295
138
  return Ret;
296
138
}