Coverage Report

Created: 2019-07-24 05:18

/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
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "LTO.h"
10
#include "Config.h"
11
#include "InputFiles.h"
12
#include "LinkerScript.h"
13
#include "SymbolTable.h"
14
#include "Symbols.h"
15
#include "lld/Common/Args.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
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
51
  std::error_code ec;
52
  auto ret =
53
      llvm::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::F_None);
54
  if (ec) {
55
    error("cannot open " + file + ": " + ec.message());
56
    return nullptr;
57
  }
58
  return ret;
59
}
60
61
static std::string getThinLTOOutputFile(StringRef modulePath) {
62
  return lto::getThinLTOOutputFile(modulePath,
63
                                   config->thinLTOPrefixReplace.first,
64
                                   config->thinLTOPrefixReplace.second);
65
}
66
67
static lto::Config createConfig() {
68
  lto::Config c;
69
70
  // LLD supports the new relocations and address-significance tables.
71
  c.Options = initTargetOptionsFromCodeGenFlags();
72
  c.Options.RelaxELFRelocations = true;
73
  c.Options.EmitAddrsig = true;
74
75
  // Always emit a section per function/datum with LTO.
76
  c.Options.FunctionSections = true;
77
  c.Options.DataSections = true;
78
79
  if (auto relocModel = getRelocModelFromCMModel())
80
    c.RelocModel = *relocModel;
81
  else if (config->relocatable)
82
    c.RelocModel = None;
83
  else if (config->isPic)
84
    c.RelocModel = Reloc::PIC_;
85
  else
86
    c.RelocModel = Reloc::Static;
87
88
  c.CodeModel = getCodeModelFromCMModel();
89
  c.DisableVerify = config->disableVerify;
90
  c.DiagHandler = diagnosticHandler;
91
  c.OptLevel = config->ltoo;
92
  c.CPU = getCPUStr();
93
  c.MAttrs = getMAttrs();
94
  c.CGOptLevel = args::getCGOptLevel(config->ltoo);
95
96
  // Set up a custom pipeline if we've been asked to.
97
  c.OptPipeline = config->ltoNewPmPasses;
98
  c.AAPipeline = config->ltoAAPipeline;
99
100
  // Set up optimization remarks if we've been asked to.
101
  c.RemarksFilename = config->optRemarksFilename;
102
  c.RemarksPasses = config->optRemarksPasses;
103
  c.RemarksWithHotness = config->optRemarksWithHotness;
104
  c.RemarksFormat = config->optRemarksFormat;
105
106
  c.SampleProfile = config->ltoSampleProfile;
107
  c.UseNewPM = config->ltoNewPassManager;
108
  c.DebugPassManager = config->ltoDebugPassManager;
109
  c.DwoDir = config->dwoDir;
110
111
  c.CSIRProfile = config->ltoCSProfileFile;
112
  c.RunCSIRInstr = config->ltoCSProfileGenerate;
113
114
  if (config->emitLLVM) {
115
2
    c.PostInternalizeModuleHook = [](size_t task, const Module &m) {
116
2
      if (std::unique_ptr<raw_fd_ostream> os = openFile(config->outputFile))
117
2
        WriteBitcodeToFile(m, *os, false);
118
2
      return false;
119
2
    };
120
  }
121
122
  if (config->saveTemps)
123
    checkError(c.addSaveTemps(config->outputFile.str() + ".",
124
                              /*UseInputModulePath*/ true));
125
  return c;
126
}
127
128
2.78k
BitcodeCompiler::BitcodeCompiler() {
129
2.78k
  // Initialize indexFile.
130
2.78k
  if (!config->thinLTOIndexOnlyArg.empty())
131
1
    indexFile = openFile(config->thinLTOIndexOnlyArg);
132
2.78k
133
2.78k
  // Initialize ltoObj.
134
2.78k
  lto::ThinBackend backend;
135
2.78k
  if (config->thinLTOIndexOnly) {
136
21
    auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
137
14
    backend = lto::createWriteIndexesThinBackend(
138
14
        config->thinLTOPrefixReplace.first, config->thinLTOPrefixReplace.second,
139
14
        config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
140
2.76k
  } else if (config->thinLTOJobs != -1U) {
141
2
    backend = lto::createInProcessThinBackend(config->thinLTOJobs);
142
2
  }
143
2.78k
144
2.78k
  ltoObj = llvm::make_unique<lto::LTO>(createConfig(), backend,
145
2.78k
                                       config->ltoPartitions);
146
2.78k
147
2.78k
  // Initialize usedStartStop.
148
17.0k
  symtab->forEachSymbol([&](Symbol *sym) {
149
17.0k
    StringRef s = sym->getName();
150
17.0k
    for (StringRef prefix : {"__start_", "__stop_"})
151
34.1k
      if (s.startswith(prefix))
152
34
        usedStartStop.insert(s.substr(prefix.size()));
153
17.0k
  });
154
2.78k
}
155
156
2.78k
BitcodeCompiler::~BitcodeCompiler() = default;
157
158
253
void BitcodeCompiler::add(BitcodeFile &f) {
159
253
  lto::InputFile &obj = *f.obj;
160
253
  bool isExec = !config->shared && 
!config->relocatable109
;
161
253
162
253
  if (config->thinLTOIndexOnly)
163
25
    thinIndices.insert(obj.getName());
164
253
165
253
  ArrayRef<Symbol *> syms = f.getSymbols();
166
253
  ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
167
253
  std::vector<lto::SymbolResolution> resols(syms.size());
168
253
169
253
  // Provide a resolution to the LTO API for each symbol.
170
655
  for (size_t i = 0, e = syms.size(); i != e; 
++i402
) {
171
402
    Symbol *sym = syms[i];
172
402
    const lto::InputFile::Symbol &objSym = objSyms[i];
173
402
    lto::SymbolResolution &r = resols[i];
174
402
175
402
    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
176
402
    // reports two symbols for module ASM defined. Without this check, lld
177
402
    // flags an undefined in IR with a definition in ASM as prevailing.
178
402
    // Once IRObjectFile is fixed to report only one symbol this hack can
179
402
    // be removed.
180
402
    r.Prevailing = !objSym.isUndefined() && 
sym->file == &f299
;
181
402
182
402
    // We ask LTO to preserve following global symbols:
183
402
    // 1) All symbols when doing relocatable link, so that them can be used
184
402
    //    for doing final link.
185
402
    // 2) Symbols that are used in regular objects.
186
402
    // 3) C named sections if we have corresponding __start_/__stop_ symbol.
187
402
    // 4) Symbols that are defined in bitcode files and used for dynamic linking.
188
402
    r.VisibleToRegularObj = config->relocatable || 
sym->isUsedInRegularObj392
||
189
402
                            
(275
r.Prevailing275
&&
sym->includeInDynsym()185
) ||
190
402
                            
usedStartStop.count(objSym.getSectionName())156
;
191
402
    const auto *dr = dyn_cast<Defined>(sym);
192
402
    r.FinalDefinitionInLinkageUnit =
193
402
        (isExec || 
sym->visibility != STV_DEFAULT236
) &&
dr210
&&
194
402
        // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
195
402
        // will be generated by for them, triggering linker errors.
196
402
        // Symbol section is always null for bitcode symbols, hence the check
197
402
        // for isElf(). Skip linker script defined symbols as well: they have
198
402
        // no File defined.
199
402
        
!(187
dr->section == nullptr187
&&
(184
!sym->file184
||
sym->file->isElf()176
));
200
402
201
402
    if (r.Prevailing)
202
280
      sym->replace(Undefined{nullptr, sym->getName(), STB_GLOBAL, STV_DEFAULT,
203
280
                             sym->type});
204
402
205
402
    // We tell LTO to not apply interprocedural optimization for wrapped
206
402
    // (with --wrap) symbols because otherwise LTO would inline them while
207
402
    // their values are still not final.
208
402
    r.LinkerRedefined = !sym->canInline;
209
402
  }
210
253
  checkError(ltoObj->add(std::move(f.obj), resols));
211
253
}
212
213
// If LazyObjFile has not been added to link, emit empty index files.
214
// This is needed because this is what GNU gold plugin does and we have a
215
// distributed build system that depends on that behavior.
216
14
static void thinLTOCreateEmptyIndexFiles() {
217
14
  for (LazyObjFile *f : lazyObjFiles) {
218
3
    if (!isBitcode(f->mb))
219
1
      continue;
220
2
    std::string path = replaceThinLTOSuffix(getThinLTOOutputFile(f->getName()));
221
2
    std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
222
2
    if (!os)
223
0
      continue;
224
2
225
2
    ModuleSummaryIndex m(/*HaveGVs*/ false);
226
2
    m.setSkipModuleByDistributedBackend();
227
2
    WriteIndexToFile(m, *os);
228
2
    if (config->thinLTOEmitImportsFiles)
229
1
      openFile(path + ".imports");
230
2
  }
231
14
}
232
233
// Merge all the bitcode files we have seen, codegen the result
234
// and return the resulting ObjectFile(s).
235
2.78k
std::vector<InputFile *> BitcodeCompiler::compile() {
236
2.78k
  unsigned maxTasks = ltoObj->getMaxTasks();
237
2.78k
  buf.resize(maxTasks);
238
2.78k
  files.resize(maxTasks);
239
2.78k
240
2.78k
  // The --thinlto-cache-dir option specifies the path to a directory in which
241
2.78k
  // to cache native object files for ThinLTO incremental builds. If a path was
242
2.78k
  // specified, configure LTO to use it as the cache directory.
243
2.78k
  lto::NativeObjectCache cache;
244
2.78k
  if (!config->thinLTOCacheDir.empty())
245
6
    cache = check(
246
6
        lto::localCache(config->thinLTOCacheDir,
247
11
                        [&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
248
11
                          files[task] = std::move(mb);
249
11
                        }));
250
2.78k
251
2.78k
  if (!bitcodeFiles.empty())
252
178
    checkError(ltoObj->run(
253
215
        [&](size_t task) {
254
215
          return llvm::make_unique<lto::NativeObjectStream>(
255
215
              llvm::make_unique<raw_svector_ostream>(buf[task]));
256
215
        },
257
178
        cache));
258
2.78k
259
2.78k
  // Emit empty index files for non-indexed files
260
2.78k
  for (StringRef s : thinIndices) {
261
4
    std::string path = getThinLTOOutputFile(s);
262
4
    openFile(path + ".thinlto.bc");
263
4
    if (config->thinLTOEmitImportsFiles)
264
1
      openFile(path + ".imports");
265
4
  }
266
2.78k
267
2.78k
  if (config->thinLTOIndexOnly) {
268
14
    thinLTOCreateEmptyIndexFiles();
269
14
270
14
    if (!config->ltoObjPath.empty())
271
1
      saveBuffer(buf[0], config->ltoObjPath);
272
14
273
14
    // ThinLTO with index only option is required to generate only the index
274
14
    // files. After that, we exit from linker and ThinLTO backend runs in a
275
14
    // distributed environment.
276
14
    if (indexFile)
277
1
      indexFile->close();
278
14
    return {};
279
14
  }
280
2.76k
281
2.76k
  if (!config->thinLTOCacheDir.empty())
282
6
    pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy);
283
2.76k
284
2.76k
  if (!config->ltoObjPath.empty()) {
285
1
    saveBuffer(buf[0], config->ltoObjPath);
286
3
    for (unsigned i = 1; i != maxTasks; 
++i2
)
287
2
      saveBuffer(buf[i], config->ltoObjPath + Twine(i));
288
1
  }
289
2.76k
290
2.76k
  if (config->saveTemps) {
291
55
    saveBuffer(buf[0], config->outputFile + ".lto.o");
292
64
    for (unsigned i = 1; i != maxTasks; 
++i9
)
293
9
      saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
294
55
  }
295
2.76k
296
2.76k
  std::vector<InputFile *> ret;
297
5.58k
  for (unsigned i = 0; i != maxTasks; 
++i2.81k
)
298
2.81k
    if (!buf[i].empty())
299
202
      ret.push_back(createObjectFile(MemoryBufferRef(buf[i], "lto.tmp")));
300
2.76k
301
2.76k
  for (std::unique_ptr<MemoryBuffer> &file : files)
302
2.81k
    if (file)
303
12
      ret.push_back(createObjectFile(*file));
304
2.76k
  return ret;
305
2.76k
}