Coverage Report

Created: 2019-01-18 03:29

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