llvm.org GIT mirror llvm / 3fd7e37
Rewrite the gold plugin to fix pr19901. There is a fundamental difference between how the gold API and lib/LTO view the LTO process. The gold API talks about a particular symbol in a particular file. The lib/LTO API talks about a symbol in the merged module. The merged module is then defined in terms of the IR semantics. In particular, a linkonce_odr GV is only copied if it is used, since it is valid to drop unused linkonce_odr GVs. In the testcase in pr19901 both properties collide. What happens is that gold asks us to keep a particular linkonce_odr symbol, but the IR linker doesn't copy it to the merged module and we never have a chance to ask lib/LTO to keep it. This patch fixes it by having a more direct implementation of the gold API. If it asks us to keep a symbol, we change the linkage so it is not linkonce. If it says we can drop a symbol, we do so. All of this before we even send the module to lib/Linker. Since now we don't have to produce LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN, during symbol resolution we can use a temporary LLVMContext and do lazy module loading. This allows us to keep the minimum possible amount of allocated memory around. This should also allow as much parallelism as we want, since there is no shared context. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216215 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
9 changed file(s) with 462 addition(s) and 170 deletion(s). Raw diff Collapse all Expand all
0 target triple = "x86_64-unknown-linux-gnu"
1 define linkonce_odr hidden void @f() {
2 ret void
3 }
0 ; RUN: llvm-as %s -o %t.o
1 ; RUN: llvm-as %p/Inputs/alias-1.ll -o %t2.o
2 ; RUN: ld -shared -o %t3.o -plugin %llvmshlibdir/LLVMgold.so %t2.o %t.o \
3 ; RUN: -plugin-opt=emit-llvm
4 ; RUN: llvm-dis %t3.o -o - | FileCheck %s
5
6 ; CHECK: @a = global i32 42
7 ; CHECK: @b = global i32 1
8
9 @a = weak alias i32* @b
10 @b = global i32 1
5454 @g6 = global void()* @f6
5555
5656
57 ; API: f3
58 ; API: f5
59 ; API: g5
60 ; API: g6
57 ; API: f1 PREVAILING_DEF_IRONLY
58 ; API: f2 PREVAILING_DEF_IRONLY
59 ; API: f3 PREVAILING_DEF_IRONLY_EXP
60 ; API: f4 PREVAILING_DEF_IRONLY_EXP
61 ; API: f5 PREVAILING_DEF_IRONLY_EXP
62 ; API: f6 PREVAILING_DEF_IRONLY_EXP
63 ; API: g5 PREVAILING_DEF_IRONLY_EXP
64 ; API: g6 PREVAILING_DEF_IRONLY_EXP
0 ; RUN: llvm-as %s -o %t.o
11 ; RUN: ld -plugin %llvmshlibdir/LLVMgold.so -m elf32ppc \
22 ; RUN: -plugin-opt=mtriple=powerpc-linux-gnu \
3 ; RUN: -shared %t.o -o %t2.o
4 ; RUN: llvm-readobj %t2.o | FileCheck %s
3 ; RUN: -plugin-opt=obj-path=%t3.o \
4 ; RUN: -shared %t.o -o %t2
5 ; RUN: llvm-readobj --file-headers %t2 | FileCheck --check-prefix=DSO %s
6 ; RUN: llvm-readobj --file-headers %t3.o | FileCheck --check-prefix=REL %s
57
6 ; CHECK: Format: ELF32-ppc
8 ; REL: Type: Relocatable
9 ; REL-NEXT: Machine: EM_PPC
10
11 ; DSO: Type: SharedObject
12 ; DSO-NEXT: Machine: EM_PPC
0 ; RUN: llc %s -o %t.o -filetype=obj -relocation-model=pic
1 ; RUN: llvm-as %p/Inputs/pr19901-1.ll -o %t2.o
2 ; RUN: ld -shared -o %t.so -plugin %llvmshlibdir/LLVMgold.so %t2.o %t.o
3 ; RUN: llvm-objdump -d -symbolize %t.so | FileCheck %s
4
5 ; CHECK: g:
6 ; CHECK-NEXT: push
7 ; CHECK-NEXT: callq f
8
9 target triple = "x86_64-unknown-linux-gnu"
10 define i32 @g() {
11 call void @f()
12 ret i32 0
13 }
14 define linkonce_odr hidden void @f() {
15 ret void
16 }
1515
1616 set(LLVM_LINK_COMPONENTS
1717 ${LLVM_TARGETS_TO_BUILD}
18 LTO
18 Linker
19 BitWriter
20 IPO
1921 )
2022
2123 add_llvm_loadable_module(LLVMgold
1919 # early so we can set up LINK_COMPONENTS before including Makefile.rules
2020 include $(LEVEL)/Makefile.config
2121
22 LINK_COMPONENTS := $(TARGETS_TO_BUILD) LTO
22 LINK_COMPONENTS := $(TARGETS_TO_BUILD) Linker BitWriter IPO
2323
2424 # Because off_t is used in the public API, the largefile parts are required for
2525 # ABI compatibility.
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
15 #include "llvm-c/lto.h"
1615 #include "llvm/ADT/StringSet.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/CodeGen/Analysis.h"
1718 #include "llvm/CodeGen/CommandFlags.h"
18 #include "llvm/LTO/LTOCodeGenerator.h"
19 #include "llvm/LTO/LTOModule.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/Linker/Linker.h"
23 #include "llvm/MC/SubtargetFeature.h"
24 #include "llvm/Object/IRObjectFile.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/Host.h"
2028 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/TargetRegistry.h"
2130 #include "llvm/Support/TargetSelect.h"
31 #include "llvm/Target/TargetLibraryInfo.h"
32 #include "llvm/Transforms/IPO.h"
33 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
34 #include "llvm/Transforms/Utils/GlobalStatus.h"
35 #include "llvm/Transforms/Utils/ModuleUtils.h"
2236 #include
2337 #include
2438 #include
4559 abort();
4660 }
4761
62 static ld_plugin_get_input_file get_input_file = nullptr;
63 static ld_plugin_release_input_file release_input_file = nullptr;
4864 static ld_plugin_add_symbols add_symbols = nullptr;
4965 static ld_plugin_get_symbols get_symbols = nullptr;
5066 static ld_plugin_add_input_file add_input_file = nullptr;
5167 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
5268 static ld_plugin_get_view get_view = nullptr;
5369 static ld_plugin_message message = discard_message;
54 static lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
70 static Reloc::Model RelocationModel = Reloc::Default;
5571 static std::string output_name = "";
5672 static std::list Modules;
5773 static std::vector Cleanup;
58 static LTOCodeGenerator *CodeGen = nullptr;
59 static StringSet<> CannotBeHidden;
6074 static llvm::TargetOptions TargetOpts;
6175
6276 namespace options {
107121 }
108122 } else {
109123 // Save this option to pass to the code generator.
124 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
125 // add that.
126 if (extra.empty())
127 extra.push_back("LLVMgold");
128
110129 extra.push_back(opt_);
111130 }
112131 }
144163 case LDPO_REL: // .o
145164 case LDPO_DYN: // .so
146165 case LDPO_PIE: // position independent executable
147 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
166 RelocationModel = Reloc::PIC_;
148167 break;
149168 case LDPO_EXEC: // .exe
150 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
169 RelocationModel = Reloc::Static;
151170 break;
152171 default:
153172 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
182201 if (callback(cleanup_hook) != LDPS_OK)
183202 return LDPS_ERR;
184203 } break;
204 case LDPT_GET_INPUT_FILE:
205 get_input_file = tv->tv_u.tv_get_input_file;
206 break;
207 case LDPT_RELEASE_INPUT_FILE:
208 release_input_file = tv->tv_u.tv_release_input_file;
209 break;
185210 case LDPT_ADD_SYMBOLS:
186211 add_symbols = tv->tv_u.tv_add_symbols;
187212 break;
217242 if (!RegisteredAllSymbolsRead)
218243 return LDPS_OK;
219244
220 CodeGen = new LTOCodeGenerator();
221
222 // Pass through extra options to the code generator.
223 if (!options::extra.empty()) {
224 for (const char *Opt : options::extra)
225 CodeGen->setCodeGenDebugOptions(Opt);
226 }
227
228 CodeGen->parseCodeGenDebugOptions();
229 if (MAttrs.size()) {
230 std::string Attrs;
231 for (unsigned I = 0; I < MAttrs.size(); ++I) {
232 if (I > 0)
233 Attrs.append(",");
234 Attrs.append(MAttrs[I]);
235 }
236 CodeGen->setAttr(Attrs.c_str());
237 }
238
239 TargetOpts = InitTargetOptionsFromCodeGenFlags();
240 CodeGen->setTargetOptions(TargetOpts);
245 if (!get_input_file) {
246 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
247 return LDPS_ERR;
248 }
249 if (!release_input_file) {
250 message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
251 return LDPS_ERR;
252 }
241253
242254 return LDPS_OK;
243255 }
247259 /// possible.
248260 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
249261 int *claimed) {
250 const void *view;
262 LLVMContext Context;
251263 std::unique_ptr buffer;
252264 if (get_view) {
265 const void *view;
253266 if (get_view(file->handle, &view) != LDPS_OK) {
254267 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
255268 return LDPS_ERR;
256269 }
270 buffer.reset(MemoryBuffer::getMemBuffer(
271 StringRef((char *)view, file->filesize), "", false));
257272 } else {
258273 int64_t offset = 0;
259274 // Gold has found what might be IR part-way inside of a file, such as
269284 return LDPS_ERR;
270285 }
271286 buffer = std::move(BufferOrErr.get());
272 view = buffer->getBufferStart();
273 }
274
275 if (!LTOModule::isBitcodeFile(view, file->filesize))
287 }
288
289 ErrorOr ObjOrErr =
290 object::IRObjectFile::createIRObjectFile(buffer->getMemBufferRef(),
291 Context);
292 std::error_code EC = ObjOrErr.getError();
293 if (EC == BitcodeError::InvalidBitcodeSignature)
276294 return LDPS_OK;
277295
278296 *claimed = 1;
279297
280 std::string Error;
281 LTOModule *M =
282 LTOModule::createFromBuffer(view, file->filesize, TargetOpts, Error);
283 if (!M) {
298 if (EC) {
284299 message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
285 Error.c_str());
300 EC.message().c_str());
286301 return LDPS_ERR;
287302 }
303 std::unique_ptr Obj(ObjOrErr.get());
288304
289305 Modules.resize(Modules.size() + 1);
290306 claimed_file &cf = Modules.back();
291307
292 if (!options::triple.empty())
293 M->setTargetTriple(options::triple.c_str());
294
295308 cf.handle = file->handle;
296 unsigned sym_count = M->getSymbolCount();
297 cf.syms.reserve(sym_count);
298
299 for (unsigned i = 0; i != sym_count; ++i) {
300 lto_symbol_attributes attrs = M->getSymbolAttributes(i);
301 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
309
310 for (auto &Sym : Obj->symbols()) {
311 uint32_t Symflags = Sym.getFlags();
312 if (!(Symflags & object::BasicSymbolRef::SF_Global))
313 continue;
314
315 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
302316 continue;
303317
304318 cf.syms.push_back(ld_plugin_symbol());
305319 ld_plugin_symbol &sym = cf.syms.back();
306 sym.name = strdup(M->getSymbolName(i));
307320 sym.version = nullptr;
308321
309 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
310 bool CanBeHidden = scope == LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
311 if (!CanBeHidden)
312 CannotBeHidden.insert(sym.name);
313 switch (scope) {
314 case LTO_SYMBOL_SCOPE_HIDDEN:
322 SmallString<64> Name;
323 {
324 raw_svector_ostream OS(Name);
325 Sym.printName(OS);
326 }
327 sym.name = strdup(Name.c_str());
328
329 const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
330
331 sym.visibility = LDPV_DEFAULT;
332 if (GV) {
333 switch (GV->getVisibility()) {
334 case GlobalValue::DefaultVisibility:
335 sym.visibility = LDPV_DEFAULT;
336 break;
337 case GlobalValue::HiddenVisibility:
315338 sym.visibility = LDPV_HIDDEN;
316339 break;
317 case LTO_SYMBOL_SCOPE_PROTECTED:
340 case GlobalValue::ProtectedVisibility:
318341 sym.visibility = LDPV_PROTECTED;
319342 break;
320 case 0: // extern
321 case LTO_SYMBOL_SCOPE_DEFAULT:
322 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN:
323 sym.visibility = LDPV_DEFAULT;
324 break;
325 default:
326 message(LDPL_ERROR, "Unknown scope attribute: %d", scope);
327 return LDPS_ERR;
328 }
329
330 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
343 }
344 }
345
346 if (Symflags & object::BasicSymbolRef::SF_Undefined) {
347 sym.def = LDPK_UNDEF;
348 if (GV && GV->hasExternalWeakLinkage())
349 sym.def = LDPK_WEAKUNDEF;
350 } else {
351 sym.def = LDPK_DEF;
352 if (GV) {
353 assert(!GV->hasExternalWeakLinkage() &&
354 !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
355 if (GV->hasCommonLinkage())
356 sym.def = LDPK_COMMON;
357 else if (GV->isWeakForLinker())
358 sym.def = LDPK_WEAKDEF;
359 }
360 }
361
362 sym.size = 0;
331363 sym.comdat_key = nullptr;
332 switch (definition) {
333 case LTO_SYMBOL_DEFINITION_REGULAR:
334 sym.def = LDPK_DEF;
335 break;
336 case LTO_SYMBOL_DEFINITION_UNDEFINED:
337 sym.def = LDPK_UNDEF;
338 break;
339 case LTO_SYMBOL_DEFINITION_TENTATIVE:
340 sym.def = LDPK_COMMON;
341 break;
342 case LTO_SYMBOL_DEFINITION_WEAK:
343 sym.comdat_key = sym.name;
344 sym.def = LDPK_WEAKDEF;
345 break;
346 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
347 sym.def = LDPK_WEAKUNDEF;
348 break;
349 default:
350 message(LDPL_ERROR, "Unknown definition attribute: %d", definition);
351 return LDPS_ERR;
352 }
353
354 sym.size = 0;
364 if (GV && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()))
365 sym.comdat_key = sym.name;
355366
356367 sym.resolution = LDPR_UNKNOWN;
357368 }
358
359 cf.syms.reserve(cf.syms.size());
360369
361370 if (!cf.syms.empty()) {
362371 if (add_symbols(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
365374 }
366375 }
367376
368 if (CodeGen) {
369 std::string Error;
370 if (!CodeGen->addModule(M, Error)) {
371 message(LDPL_ERROR, "Error linking module: %s", Error.c_str());
372 return LDPS_ERR;
373 }
374 }
375
376 delete M;
377
378377 return LDPS_OK;
379378 }
380379
381 static bool mustPreserve(ld_plugin_symbol &Sym) {
382 if (Sym.resolution == LDPR_PREVAILING_DEF)
380 static void keepGlobalValue(GlobalValue &GV) {
381 assert(!GV.hasLocalLinkage());
382
383 switch (GV.getLinkage()) {
384 default:
385 break;
386 case GlobalValue::LinkOnceAnyLinkage:
387 GV.setLinkage(GlobalValue::WeakAnyLinkage);
388 break;
389 case GlobalValue::LinkOnceODRLinkage:
390 GV.setLinkage(GlobalValue::WeakODRLinkage);
391 break;
392 }
393
394 assert(!GV.isDiscardableIfUnused());
395 }
396
397 static bool isDeclaration(const GlobalValue &V) {
398 if (V.hasAvailableExternallyLinkage())
383399 return true;
384 if (Sym.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
385 return CannotBeHidden.count(Sym.name);
386 return false;
400
401 if (V.isMaterializable())
402 return false;
403
404 return V.isDeclaration();
405 }
406
407 static void internalize(GlobalValue &GV) {
408 if (isDeclaration(GV))
409 return; // We get here if there is a matching asm definition.
410 if (!GV.hasLocalLinkage())
411 GV.setLinkage(GlobalValue::InternalLinkage);
412 }
413
414 static void drop(GlobalValue &GV) {
415 if (auto *F = dyn_cast(&GV)) {
416 F->deleteBody();
417 return;
418 }
419
420 if (auto *Var = dyn_cast(&GV)) {
421 Var->setInitializer(nullptr);
422 Var->setLinkage(GlobalValue::ExternalLinkage);
423 return;
424 }
425
426 auto &Alias = cast(GV);
427 Module &M = *Alias.getParent();
428 PointerType &Ty = *cast(Alias.getType());
429 GlobalValue::LinkageTypes L = Alias.getLinkage();
430 auto *Var =
431 new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, L,
432 /*Initializer*/ nullptr);
433 Var->takeName(&Alias);
434 Alias.replaceAllUsesWith(Var);
435 }
436
437 static const char *getResolutionName(ld_plugin_symbol_resolution R) {
438 switch (R) {
439 case LDPR_UNKNOWN:
440 return "UNKNOWN";
441 case LDPR_UNDEF:
442 return "UNDEF";
443 case LDPR_PREVAILING_DEF:
444 return "PREVAILING_DEF";
445 case LDPR_PREVAILING_DEF_IRONLY:
446 return "PREVAILING_DEF_IRONLY";
447 case LDPR_PREEMPTED_REG:
448 return "PREEMPTED_REG";
449 case LDPR_PREEMPTED_IR:
450 return "PREEMPTED_IR";
451 case LDPR_RESOLVED_IR:
452 return "RESOLVED_IR";
453 case LDPR_RESOLVED_EXEC:
454 return "RESOLVED_EXEC";
455 case LDPR_RESOLVED_DYN:
456 return "RESOLVED_DYN";
457 case LDPR_PREVAILING_DEF_IRONLY_EXP:
458 return "PREVAILING_DEF_IRONLY_EXP";
459 }
460 }
461
462 static std::unique_ptr
463 getModuleForFile(LLVMContext &Context, claimed_file &F, raw_fd_ostream *ApiFile,
464 StringSet<> &Internalize, StringSet<> &Maybe) {
465 ld_plugin_input_file File;
466 if (get_input_file(F.handle, &File) != LDPS_OK)
467 message(LDPL_FATAL, "Failed to get file information");
468
469 if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
470 message(LDPL_FATAL, "Failed to get symbol information");
471
472 const void *View;
473 if (get_view(F.handle, &View) != LDPS_OK)
474 message(LDPL_FATAL, "Failed to get a view of file");
475
476 std::unique_ptr Buffer(MemoryBuffer::getMemBuffer(
477 StringRef((char *)View, File.filesize), "", false));
478
479 if (release_input_file(F.handle) != LDPS_OK)
480 message(LDPL_FATAL, "Failed to release file information");
481
482 ErrorOr MOrErr = getLazyBitcodeModule(Buffer.get(), Context);
483
484 if (std::error_code EC = MOrErr.getError())
485 message(LDPL_FATAL, "Could not read bitcode from file : %s",
486 EC.message().c_str());
487 Buffer.release();
488
489 std::unique_ptr M(MOrErr.get());
490
491 SmallPtrSet Used;
492 collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false);
493
494 std::vector Drop;
495 for (ld_plugin_symbol &Sym : F.syms) {
496 ld_plugin_symbol_resolution Resolution =
497 (ld_plugin_symbol_resolution)Sym.resolution;
498
499 if (options::generate_api_file)
500 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
501
502 GlobalValue *GV = M->getNamedValue(Sym.name);
503 if (!GV)
504 continue; // Asm symbol.
505
506 switch (Resolution) {
507 case LDPR_UNKNOWN:
508 llvm_unreachable("Unexpected resolution");
509
510 case LDPR_RESOLVED_IR:
511 case LDPR_RESOLVED_EXEC:
512 case LDPR_RESOLVED_DYN:
513 case LDPR_UNDEF:
514 assert(isDeclaration(*GV));
515 break;
516
517 case LDPR_PREVAILING_DEF_IRONLY: {
518 if (!Used.count(GV)) {
519 // Since we use the regular lib/Linker, we cannot just internalize GV
520 // now or it will not be copied to the merged module. Instead we force
521 // it to be copied and then internalize it.
522 keepGlobalValue(*GV);
523 Internalize.insert(Sym.name);
524 }
525 break;
526 }
527
528 case LDPR_PREVAILING_DEF:
529 keepGlobalValue(*GV);
530 break;
531
532 case LDPR_PREEMPTED_REG:
533 case LDPR_PREEMPTED_IR:
534 Drop.push_back(GV);
535 break;
536
537 case LDPR_PREVAILING_DEF_IRONLY_EXP: {
538 // We can only check for address uses after we merge the modules. The
539 // reason is that this GV might have a copy in another module
540 // and in that module the address might be significant, but that
541 // copy will be LDPR_PREEMPTED_IR.
542 if (GV->hasLinkOnceODRLinkage())
543 Maybe.insert(Sym.name);
544 keepGlobalValue(*GV);
545 break;
546 }
547 }
548
549 free(Sym.name);
550 Sym.name = nullptr;
551 Sym.comdat_key = nullptr;
552 }
553
554 if (!Drop.empty()) {
555 // This is horrible. Given how lazy loading is implemented, dropping
556 // the body while there is a materializer present doesn't work, the
557 // linker will just read the body back.
558 M->materializeAllPermanently();
559 for (auto *GV : Drop)
560 drop(*GV);
561 }
562
563 return M;
564 }
565
566 static void runLTOPasses(Module &M, TargetMachine &TM) {
567 PassManager passes;
568 PassManagerBuilder PMB;
569 PMB.LibraryInfo = new TargetLibraryInfo(Triple(TM.getTargetTriple()));
570 PMB.Inliner = createFunctionInliningPass();
571 PMB.VerifyInput = true;
572 PMB.VerifyOutput = true;
573 PMB.populateLTOPassManager(passes, &TM);
574 passes.run(M);
575 }
576
577 static void codegen(Module &M) {
578 const std::string &TripleStr = M.getTargetTriple();
579 Triple TheTriple(TripleStr);
580
581 std::string ErrMsg;
582 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
583 if (!TheTarget)
584 message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
585
586 if (unsigned NumOpts = options::extra.size())
587 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
588
589 SubtargetFeatures Features;
590 Features.getDefaultSubtargetFeatures(TheTriple);
591 for (const std::string &A : MAttrs)
592 Features.AddFeature(A);
593
594 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
595 std::unique_ptr TM(TheTarget->createTargetMachine(
596 TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
597 CodeModel::Default, CodeGenOpt::Aggressive));
598
599 runLTOPasses(M, *TM);
600
601 PassManager CodeGenPasses;
602 CodeGenPasses.add(new DataLayoutPass(&M));
603
604 SmallString<128> Filename;
605 int FD;
606 if (options::obj_path.empty()) {
607 std::error_code EC =
608 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
609 if (EC)
610 message(LDPL_FATAL, "Could not create temorary file: %s",
611 EC.message().c_str());
612 } else {
613 Filename = options::obj_path;
614 std::error_code EC =
615 sys::fs::openFileForWrite(Filename.c_str(), FD, sys::fs::F_None);
616 if (EC)
617 message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
618 }
619
620 {
621 raw_fd_ostream OS(FD, true);
622 formatted_raw_ostream FOS(OS);
623
624 if (TM->addPassesToEmitFile(CodeGenPasses, FOS,
625 TargetMachine::CGFT_ObjectFile))
626 message(LDPL_FATAL, "Failed to setup codegen");
627 CodeGenPasses.run(M);
628 }
629
630 if (add_input_file(Filename.c_str()) != LDPS_OK)
631 message(LDPL_FATAL,
632 "Unable to add .o file to the link. File left behind in: %s",
633 Filename.c_str());
634
635 if (options::obj_path.empty())
636 Cleanup.push_back(Filename.c_str());
387637 }
388638
389639 /// gold informs us that all symbols have been read. At this point, we use
390640 /// get_symbols to see if any of our definitions have been overridden by a
391641 /// native object file. Then, perform optimization and codegen.
392 static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *apiFile) {
393 assert(CodeGen);
394
642 static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
643 if (Modules.empty())
644 return LDPS_OK;
645
646 LLVMContext Context;
647 std::unique_ptr Combined(new Module("ld-temp.o", Context));
648 Linker L(Combined.get());
649
650 std::string DefaultTriple = sys::getDefaultTargetTriple();
651
652 StringSet<> Internalize;
653 StringSet<> Maybe;
395654 for (claimed_file &F : Modules) {
396 if (F.syms.empty())
655 std::unique_ptr M =
656 getModuleForFile(Context, F, ApiFile, Internalize, Maybe);
657 if (!options::triple.empty())
658 M->setTargetTriple(options::triple.c_str());
659 else if (M->getTargetTriple().empty()) {
660 M->setTargetTriple(DefaultTriple);
661 }
662
663 std::string ErrMsg;
664 if (L.linkInModule(M.get(), &ErrMsg))
665 message(LDPL_FATAL, "Failed to link module: %s", ErrMsg.c_str());
666 }
667
668 for (const auto &Name : Internalize) {
669 GlobalValue *GV = Combined->getNamedValue(Name.first());
670 if (GV)
671 internalize(*GV);
672 }
673
674 for (const auto &Name : Maybe) {
675 GlobalValue *GV = Combined->getNamedValue(Name.first());
676 if (!GV)
397677 continue;
398 get_symbols(F.handle, F.syms.size(), &F.syms[0]);
399 for (ld_plugin_symbol &Sym : F.syms) {
400 if (mustPreserve(Sym)) {
401 CodeGen->addMustPreserveSymbol(Sym.name);
402
403 if (options::generate_api_file)
404 (*apiFile) << Sym.name << "\n";
405 }
406 }
407 }
408
409 CodeGen->setCodePICModel(output_type);
410 CodeGen->setDebugInfo(LTO_DEBUG_MODEL_DWARF);
411 if (!options::mcpu.empty())
412 CodeGen->setCpu(options::mcpu.c_str());
678 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
679 if (canBeOmittedFromSymbolTable(GV))
680 internalize(*GV);
681 }
413682
414683 if (options::generate_bc_file != options::BC_NO) {
415684 std::string path;
419688 path = options::bc_path;
420689 else
421690 path = output_name + ".bc";
422 std::string Error;
423 if (!CodeGen->writeMergedModules(path.c_str(), Error))
424 message(LDPL_FATAL, "Failed to write the output file.");
691 {
692 std::string Error;
693 raw_fd_ostream OS(path.c_str(), Error, sys::fs::OpenFlags::F_None);
694 if (!Error.empty())
695 message(LDPL_FATAL, "Failed to write the output file.");
696 WriteBitcodeToFile(L.getModule(), OS);
697 }
425698 if (options::generate_bc_file == options::BC_ONLY)
426699 return LDPS_OK;
427700 }
428701
429 std::string ObjPath;
430 {
431 const char *Temp;
432 std::string Error;
433 if (!CodeGen->compile_to_file(&Temp, /*DisableOpt*/ false, /*DisableInline*/
434 false, /*DisableGVNLoadPRE*/ false, Error))
435 message(LDPL_ERROR, "Could not produce a combined object file\n");
436 ObjPath = Temp;
437 }
438
439 for (claimed_file &F : Modules) {
440 for (ld_plugin_symbol &Sym : F.syms)
441 free(Sym.name);
442 }
443
444 if (add_input_file(ObjPath.c_str()) != LDPS_OK) {
445 message(LDPL_ERROR, "Unable to add .o file to the link.");
446 message(LDPL_ERROR, "File left behind in: %s", ObjPath.c_str());
447 return LDPS_ERR;
448 }
702 codegen(*L.getModule());
449703
450704 if (!options::extra_library_path.empty() &&
451 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
452 message(LDPL_ERROR, "Unable to set the extra library path.");
453 return LDPS_ERR;
454 }
455
456 if (options::obj_path.empty())
457 Cleanup.push_back(ObjPath);
705 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
706 message(LDPL_FATAL, "Unable to set the extra library path.");
458707
459708 return LDPS_OK;
460709 }
465714 Ret = allSymbolsReadHook(nullptr);
466715 } else {
467716 std::string Error;
468 raw_fd_ostream apiFile("apifile.txt", Error, sys::fs::F_None);
717 raw_fd_ostream ApiFile("apifile.txt", Error, sys::fs::F_None);
469718 if (!Error.empty())
470719 message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
471720 Error.c_str());
472 Ret = allSymbolsReadHook(&apiFile);
473 }
474
475 delete CodeGen;
721 Ret = allSymbolsReadHook(&ApiFile);
722 }
476723
477724 if (options::generate_bc_file == options::BC_ONLY)
478725 exit(0);