From b2d2f2835112470976027ef18b85c6e5c991dea8 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 11 Aug 2014 19:06:54 +0000 Subject: [PATCH] Fix using -plugin-opt=apiflie when also using -plugin-opt=emit-llvm. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215378 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/gold/emit-llvm.ll | 8 +++++++ tools/gold/gold-plugin.cpp | 43 +++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/test/tools/gold/emit-llvm.ll b/test/tools/gold/emit-llvm.ll index aeff138b484..7ea0e5e3e63 100644 --- a/test/tools/gold/emit-llvm.ll +++ b/test/tools/gold/emit-llvm.ll @@ -2,8 +2,10 @@ ; RUN: ld -plugin %llvmshlibdir/LLVMgold.so \ ; RUN: --plugin-opt=emit-llvm \ +; RUN: --plugin-opt=generate-api-file \ ; RUN: -shared %t.o -o %t2.o ; RUN: llvm-dis %t2.o -o - | FileCheck %s +; RUN: FileCheck --check-prefix=API %s < %T/../apifile.txt ; RUN: ld -plugin %llvmshlibdir/LLVMgold.so \ ; RUN: -m elf_x86_64 --plugin-opt=also-emit-llvm \ @@ -51,3 +53,9 @@ define linkonce_odr void @f6() unnamed_addr { ret void } @g6 = global void()* @f6 + + +; API: f3 +; API: f5 +; API: g5 +; API: g6 diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index 7b37e4613ff..afea2b14011 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -390,20 +390,9 @@ static bool mustPreserve(ld_plugin_symbol &Sym) { /// gold informs us that all symbols have been read. At this point, we use /// get_symbols to see if any of our definitions have been overridden by a /// native object file. Then, perform optimization and codegen. -static ld_plugin_status all_symbols_read_hook(void) { - // FIXME: raw_fd_ostream should be able to represent an unopened file. - std::unique_ptr api_file; - +static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *apiFile) { assert(CodeGen); - if (options::generate_api_file) { - std::string Error; - api_file.reset(new raw_fd_ostream("apifile.txt", Error, sys::fs::F_None)); - if (!Error.empty()) - message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s", - Error.c_str()); - } - for (claimed_file &F : Modules) { if (F.syms.empty()) continue; @@ -413,7 +402,7 @@ static ld_plugin_status all_symbols_read_hook(void) { CodeGen->addMustPreserveSymbol(Sym.name); if (options::generate_api_file) - (*api_file) << Sym.name << "\n"; + (*apiFile) << Sym.name << "\n"; } } } @@ -434,10 +423,8 @@ static ld_plugin_status all_symbols_read_hook(void) { std::string Error; if (!CodeGen->writeMergedModules(path.c_str(), Error)) message(LDPL_FATAL, "Failed to write the output file."); - if (options::generate_bc_file == options::BC_ONLY) { - delete CodeGen; - exit(0); - } + if (options::generate_bc_file == options::BC_ONLY) + return LDPS_OK; } std::string ObjPath; @@ -450,7 +437,6 @@ static ld_plugin_status all_symbols_read_hook(void) { ObjPath = Temp; } - delete CodeGen; for (claimed_file &F : Modules) { for (ld_plugin_symbol &Sym : F.syms) free(Sym.name); @@ -474,6 +460,27 @@ static ld_plugin_status all_symbols_read_hook(void) { return LDPS_OK; } +static ld_plugin_status all_symbols_read_hook(void) { + ld_plugin_status Ret; + if (!options::generate_api_file) { + Ret = allSymbolsReadHook(nullptr); + } else { + std::string Error; + raw_fd_ostream apiFile("apifile.txt", Error, sys::fs::F_None); + if (!Error.empty()) + message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s", + Error.c_str()); + Ret = allSymbolsReadHook(&apiFile); + } + + delete CodeGen; + + if (options::generate_bc_file == options::BC_ONLY) + exit(0); + + return Ret; +} + static ld_plugin_status cleanup_hook(void) { for (std::string &Name : Cleanup) { std::error_code EC = sys::fs::remove(Name); -- 2.34.1