Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 8d4343e

Browse files
committed
gold-plugin: "Upgrade" debug info and handle its warnings.
The gold plugin never calls MaterializeModule, so any old debug info was not deleted and could cause crashes. Now that it is being "upgraded", the plugin also has to handle warnings and create Modules with a nice id (it shows in the warning). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230655 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent adaebc8 commit 8d4343e

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

test/tools/gold/Inputs/drop-debug.bc

1.13 KB
Binary file not shown.

test/tools/gold/drop-debug.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \
2+
; RUN: --plugin-opt=emit-llvm -shared %p/Inputs/drop-debug.bc \
3+
; RUN: -o t2.bc 2>&1 | FileCheck %s
4+
5+
; drop-debug.bc was created from "void f(void) {}" with clang 3.5 and
6+
; -gline-tables-only, so it contains old debug info.
7+
8+
; CHECK: warning: LLVM gold plugin: ignoring debug info with an invalid version (1) in {{.*}}/Inputs/drop-debug.bc

tools/gold/gold-plugin.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Bitcode/ReaderWriter.h"
2121
#include "llvm/CodeGen/Analysis.h"
2222
#include "llvm/CodeGen/CommandFlags.h"
23+
#include "llvm/IR/AutoUpgrade.h"
2324
#include "llvm/IR/Constants.h"
2425
#include "llvm/IR/DiagnosticInfo.h"
2526
#include "llvm/IR/DiagnosticPrinter.h"
@@ -273,20 +274,33 @@ static bool shouldSkip(uint32_t Symflags) {
273274
}
274275

275276
static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
276-
assert(DI.getSeverity() == DS_Error && "Only expecting errors");
277-
const auto &BDI = cast<BitcodeDiagnosticInfo>(DI);
278-
std::error_code EC = BDI.getError();
279-
if (EC == BitcodeError::InvalidBitcodeSignature)
280-
return;
277+
if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
278+
std::error_code EC = BDI->getError();
279+
if (EC == BitcodeError::InvalidBitcodeSignature)
280+
return;
281+
}
281282

282283
std::string ErrStorage;
283284
{
284285
raw_string_ostream OS(ErrStorage);
285286
DiagnosticPrinterRawOStream DP(OS);
286287
DI.print(DP);
287288
}
288-
message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
289-
ErrStorage.c_str());
289+
ld_plugin_level Level;
290+
switch (DI.getSeverity()) {
291+
case DS_Error:
292+
message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
293+
ErrStorage.c_str());
294+
llvm_unreachable("Fatal doesn't return.");
295+
case DS_Warning:
296+
Level = LDPL_WARNING;
297+
break;
298+
case DS_Remark:
299+
case DS_Note:
300+
Level = LDPL_INFO;
301+
break;
302+
}
303+
message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
290304
}
291305

292306
/// Called by gold to see whether this file is one that our plugin can handle.
@@ -561,7 +575,7 @@ static void freeSymName(ld_plugin_symbol &Sym) {
561575

562576
static std::unique_ptr<Module>
563577
getModuleForFile(LLVMContext &Context, claimed_file &F,
564-
off_t Filesize, raw_fd_ostream *ApiFile,
578+
ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
565579
StringSet<> &Internalize, StringSet<> &Maybe) {
566580

567581
if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
@@ -571,7 +585,8 @@ getModuleForFile(LLVMContext &Context, claimed_file &F,
571585
if (get_view(F.handle, &View) != LDPS_OK)
572586
message(LDPL_FATAL, "Failed to get a view of file");
573587

574-
MemoryBufferRef BufferRef(StringRef((const char *)View, Filesize), "");
588+
MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
589+
Info.name);
575590
ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
576591
object::IRObjectFile::create(BufferRef, Context);
577592

@@ -583,6 +598,8 @@ getModuleForFile(LLVMContext &Context, claimed_file &F,
583598

584599
Module &M = Obj.getModule();
585600

601+
UpgradeDebugInfo(M);
602+
586603
SmallPtrSet<GlobalValue *, 8> Used;
587604
collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
588605

@@ -792,6 +809,8 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
792809
return LDPS_OK;
793810

794811
LLVMContext Context;
812+
Context.setDiagnosticHandler(diagnosticHandler);
813+
795814
std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
796815
Linker L(Combined.get());
797816

@@ -804,8 +823,7 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
804823
if (get_input_file(F.handle, &File) != LDPS_OK)
805824
message(LDPL_FATAL, "Failed to get file information");
806825
std::unique_ptr<Module> M =
807-
getModuleForFile(Context, F, File.filesize, ApiFile,
808-
Internalize, Maybe);
826+
getModuleForFile(Context, F, File, ApiFile, Internalize, Maybe);
809827
if (!options::triple.empty())
810828
M->setTargetTriple(options::triple.c_str());
811829
else if (M->getTargetTriple().empty()) {

0 commit comments

Comments
 (0)