From b9eaeeae2fb9785df112d8d9ccad6edfdb5926ec Mon Sep 17 00:00:00 2001 From: Logan Chien Date: Sat, 17 Mar 2012 19:45:01 +0800 Subject: Emit the ELF image into the memory buffer. Emit the ELF image to the memory buffer, so that we can copy and embed them into Oat file in the future. Currently, We are keeping writing the ELF image to the file, such as gtest-0.elf, for the unit test. However, we will remove them after the Oat file related commits are ready. (cherry picked from commit 9c1d4f58b54283d38796f3084a5b88ae43d42698) Change-Id: I912ac74a6c59d92554f8aeba5556f7a44e38d2a0 --- src/compiler_llvm/compilation_unit.cc | 56 +++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'src/compiler_llvm/compilation_unit.cc') diff --git a/src/compiler_llvm/compilation_unit.cc b/src/compiler_llvm/compilation_unit.cc index 2e01fe9c3c..f654e488b6 100644 --- a/src/compiler_llvm/compilation_unit.cc +++ b/src/compiler_llvm/compilation_unit.cc @@ -167,35 +167,41 @@ bool CompilationUnit::Materialize() { pm_builder.populateModulePassManager(pm); pm_builder.populateFunctionPassManager(fpm); - // Add passes to emit file + // Add passes to emit ELF image + { + llvm::formatted_raw_ostream formatted_os( + *(new llvm::raw_string_ostream(elf_image_)), true); + + // Ask the target to add backend passes as necessary. + if (target_machine->addPassesToEmitFile(pm, + formatted_os, + llvm::TargetMachine::CGFT_ObjectFile, + true)) { + LOG(FATAL) << "Unable to generate ELF for this target"; + return false; + } + + // Run the per-function optimization + fpm.doInitialization(); + for (llvm::Module::iterator F = module_->begin(), E = module_->end(); + F != E; ++F) { + fpm.run(*F); + } + fpm.doFinalization(); + + // Run the code generation passes + pm.run(*module_); + } + + // Write ELF image to file + // TODO: Remove this when we can embed the ELF image in the Oat file. + // We are keeping these code to run the unit test. llvm::OwningPtr out_file( new llvm::tool_output_file(elf_filename_.c_str(), errmsg, llvm::raw_fd_ostream::F_Binary)); - - llvm::formatted_raw_ostream formatted_os(out_file->os(), false); - - // Ask the target to add backend passes as necessary. - if (target_machine->addPassesToEmitFile(pm, - formatted_os, - llvm::TargetMachine::CGFT_ObjectFile, - true)) { - LOG(FATAL) << "Unable to generate ELF for this target"; - return false; - } - - // Run the per-function optimization - fpm.doInitialization(); - for (llvm::Module::iterator F = module_->begin(), E = module_->end(); - F != E; ++F) { - fpm.run(*F); - } - fpm.doFinalization(); - - // Run the code generation passes - pm.run(*module_); - - // Keep the generated executable + out_file->os().write(reinterpret_cast(GetElfImage()), GetElfSize()); out_file->keep(); + LOG(INFO) << "ELF: " << elf_filename_ << " (done)"; // Free the resources -- cgit v1.2.3-59-g8ed1b