Skip to content

Commit ca42862

Browse files
committed
Add an option to the gold plugin to make it emit a file with the public api
list that can in turn be passed to -internalize pass through -internalize-public-api-file. Pass gold -plugin-opt=generate-api-file to produce "apifile.txt" in the current directory. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65295 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent bc989d4 commit ca42862

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

tools/gold/gold-plugin.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cerrno>
2323
#include <cstdlib>
2424
#include <cstring>
25+
#include <fstream>
2526
#include <list>
2627
#include <vector>
2728

@@ -42,6 +43,8 @@ namespace {
4243
int api_version = 0;
4344
int gold_version = 0;
4445

46+
bool generate_api_file = false;
47+
4548
struct claimed_file {
4649
lto_module_t M;
4750
void *handle;
@@ -96,7 +99,11 @@ ld_plugin_status onload(ld_plugin_tv *tv) {
9699
//output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
97100
break;
98101
case LDPT_OPTION:
99-
(*message)(LDPL_WARNING, "Ignoring flag %s", tv->tv_u.tv_string);
102+
if (strcmp("generate-api-file", tv->tv_u.tv_string) == 0) {
103+
generate_api_file = true;
104+
} else {
105+
(*message)(LDPL_WARNING, "Ignoring flag %s", tv->tv_u.tv_string);
106+
}
100107
break;
101108
case LDPT_REGISTER_CLAIM_FILE_HOOK: {
102109
ld_plugin_register_claim_file callback;
@@ -285,6 +292,15 @@ ld_plugin_status all_symbols_read_hook(void) {
285292
E = Modules.end(); I != E; ++I)
286293
lto_codegen_add_module(cg, I->M);
287294

295+
std::ofstream api_file;
296+
if (generate_api_file) {
297+
api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
298+
if (!api_file.is_open()) {
299+
(*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
300+
abort();
301+
}
302+
}
303+
288304
// If we don't preserve any symbols, libLTO will assume that all symbols are
289305
// needed. Keep all symbols unless we're producing a final executable.
290306
if (output_type == LTO_CODEGEN_PIC_MODEL_STATIC) {
@@ -298,10 +314,16 @@ ld_plugin_status all_symbols_read_hook(void) {
298314
I->syms[i].resolution == LDPR_RESOLVED_IR)) {
299315
lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
300316
anySymbolsPreserved = true;
317+
318+
if (generate_api_file)
319+
api_file << I->syms[i].name << "\n";
301320
}
302321
}
303322
}
304323

324+
if (generate_api_file)
325+
api_file.close();
326+
305327
if (!anySymbolsPreserved) {
306328
// This entire file is unnecessary!
307329
lto_codegen_dispose(cg);

0 commit comments

Comments
 (0)