Dispose modules early and only create codegen when the plugin is being
authorRafael Espindola <rafael.espindola@gmail.com>
Sun, 20 Feb 2011 18:28:29 +0000 (18:28 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Sun, 20 Feb 2011 18:28:29 +0000 (18:28 +0000)
used by the linker and not by nm or ar.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126089 91177308-0d34-0410-b5e6-96231b3b80d8

tools/gold/gold-plugin.cpp

index ad2774a64ef40fc447c458809ae8c7c8f3d54a3e..a9e01781a1bcbd2d8e76d791e1b15007f1714ea2 100644 (file)
@@ -64,7 +64,7 @@ namespace {
   std::string output_name = "";
   std::list<claimed_file> Modules;
   std::vector<sys::Path> Cleanup;
-  lto_code_gen_t code_gen;
+  lto_code_gen_t code_gen = NULL;
 }
 
 namespace options {
@@ -196,6 +196,8 @@ ld_plugin_status onload(ld_plugin_tv *tv) {
 
         if ((*callback)(all_symbols_read_hook) != LDPS_OK)
           return LDPS_ERR;
+
+        code_gen = lto_codegen_create();
       } break;
       case LDPT_REGISTER_CLEANUP_HOOK: {
         ld_plugin_register_cleanup callback;
@@ -236,8 +238,6 @@ ld_plugin_status onload(ld_plugin_tv *tv) {
     return LDPS_ERR;
   }
 
-  code_gen = lto_codegen_create();
-
   return LDPS_OK;
 }
 
@@ -322,6 +322,7 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
     cf.syms.push_back(ld_plugin_symbol());
     ld_plugin_symbol &sym = cf.syms.back();
     sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
+    sym.name = strdup(sym.name);
     sym.version = NULL;
 
     int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
@@ -379,7 +380,11 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
     }
   }
 
-  lto_codegen_add_module(code_gen, M);
+  if (code_gen)
+    lto_codegen_add_module(code_gen, M);
+
+  lto_module_dispose(M);
+
   return LDPS_OK;
 }
 
@@ -389,6 +394,8 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
 /// codegen.
 static ld_plugin_status all_symbols_read_hook(void) {
   std::ofstream api_file;
+  assert(code_gen);
+
   if (options::generate_api_file) {
     api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
     if (!api_file.is_open()) {
@@ -469,10 +476,10 @@ static ld_plugin_status all_symbols_read_hook(void) {
   std::string ErrMsg;
 
   const char *objPath;
+  sys::Path uniqueObjPath("/tmp/llvmgold.o");
   if (!options::obj_path.empty()) {
     objPath = options::obj_path.c_str();
   } else {
-    sys::Path uniqueObjPath("/tmp/llvmgold.o");
     if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
       (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
       return LDPS_ERR;
@@ -497,6 +504,13 @@ static ld_plugin_status all_symbols_read_hook(void) {
   objFile.keep();
 
   lto_codegen_dispose(code_gen);
+  for (std::list<claimed_file>::iterator I = Modules.begin(),
+         E = Modules.end(); I != E; ++I) {
+    for (unsigned i = 0; i != I->syms.size(); ++i) {
+      ld_plugin_symbol &sym = I->syms[i];
+      free(sym.name);
+    }
+  }
 
   if ((*add_input_file)(objPath) != LDPS_OK) {
     (*message)(LDPL_ERROR, "Unable to add .o file to the link.");