blob: 3524cddfbfab6263182ae255da7c7eb966ef9a59 [file] [log] [blame]
Logan Chien8b977d32012-02-21 19:14:55 +08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "compilation_unit.h"
18
Logan Chien110bcba2012-04-16 19:11:28 +080019#include "compiled_method.h"
Logan Chien971bf3f2012-05-01 15:47:55 +080020#include "compiler_llvm.h"
Shih-wei Liaod7726e42012-04-20 15:23:36 -070021#include "file.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070022#include "instruction_set.h"
Logan Chien8b977d32012-02-21 19:14:55 +080023#include "ir_builder.h"
24#include "logging.h"
Shih-wei Liaod7726e42012-04-20 15:23:36 -070025#include "os.h"
Logan Chien8b977d32012-02-21 19:14:55 +080026
TDYa127d668a062012-04-13 12:36:57 -070027#include "runtime_support_builder_arm.h"
TDYa127b08ed122012-06-05 23:51:19 -070028#include "runtime_support_builder_thumb2.h"
TDYa127d668a062012-04-13 12:36:57 -070029#include "runtime_support_builder_x86.h"
30
Logan Chien8b977d32012-02-21 19:14:55 +080031#include <llvm/ADT/OwningPtr.h>
32#include <llvm/ADT/StringSet.h>
33#include <llvm/ADT/Triple.h>
34#include <llvm/Analysis/CallGraph.h>
35#include <llvm/Analysis/DebugInfo.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070036#include <llvm/Analysis/Dominators.h>
37#include <llvm/Analysis/LoopInfo.h>
Logan Chien8b977d32012-02-21 19:14:55 +080038#include <llvm/Analysis/LoopPass.h>
39#include <llvm/Analysis/RegionPass.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070040#include <llvm/Analysis/ScalarEvolution.h>
Logan Chien8b977d32012-02-21 19:14:55 +080041#include <llvm/Analysis/Verifier.h>
42#include <llvm/Assembly/PrintModulePass.h>
43#include <llvm/Bitcode/ReaderWriter.h>
44#include <llvm/CallGraphSCCPass.h>
Logan Chien110bcba2012-04-16 19:11:28 +080045#include <llvm/CodeGen/MachineFrameInfo.h>
46#include <llvm/CodeGen/MachineFunction.h>
47#include <llvm/CodeGen/MachineFunctionPass.h>
Logan Chien8b977d32012-02-21 19:14:55 +080048#include <llvm/DerivedTypes.h>
49#include <llvm/LLVMContext.h>
Logan Chien8b977d32012-02-21 19:14:55 +080050#include <llvm/Module.h>
Logan Chien971bf3f2012-05-01 15:47:55 +080051#include <llvm/Object/ObjectFile.h>
Logan Chien8b977d32012-02-21 19:14:55 +080052#include <llvm/PassManager.h>
53#include <llvm/Support/Debug.h>
Logan Chien971bf3f2012-05-01 15:47:55 +080054#include <llvm/Support/ELF.h>
Logan Chien8b977d32012-02-21 19:14:55 +080055#include <llvm/Support/FormattedStream.h>
56#include <llvm/Support/ManagedStatic.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070057#include <llvm/Support/MemoryBuffer.h>
Logan Chien8b977d32012-02-21 19:14:55 +080058#include <llvm/Support/PassNameParser.h>
59#include <llvm/Support/PluginLoader.h>
60#include <llvm/Support/PrettyStackTrace.h>
61#include <llvm/Support/Signals.h>
62#include <llvm/Support/SystemUtils.h>
63#include <llvm/Support/TargetRegistry.h>
64#include <llvm/Support/TargetSelect.h>
65#include <llvm/Support/ToolOutputFile.h>
66#include <llvm/Support/raw_ostream.h>
Shih-wei Liaod7726e42012-04-20 15:23:36 -070067#include <llvm/Support/system_error.h>
Logan Chien8b977d32012-02-21 19:14:55 +080068#include <llvm/Target/TargetData.h>
69#include <llvm/Target/TargetLibraryInfo.h>
70#include <llvm/Target/TargetMachine.h>
Shih-wei Liaof1cb9a52012-04-20 01:49:18 -070071#include <llvm/Transforms/IPO.h>
Logan Chien8b977d32012-02-21 19:14:55 +080072#include <llvm/Transforms/IPO/PassManagerBuilder.h>
TDYa127f15b0ab2012-05-11 21:01:36 -070073#include <llvm/Transforms/Scalar.h>
Logan Chien8b977d32012-02-21 19:14:55 +080074
Shih-wei Liaod7726e42012-04-20 15:23:36 -070075#include <sys/types.h>
76#include <sys/wait.h>
77#include <unistd.h>
78
Logan Chien8b977d32012-02-21 19:14:55 +080079#include <string>
80
81namespace art {
82namespace compiler_llvm {
83
TDYa12755e5e6c2012-09-11 15:14:42 -070084#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -070085llvm::FunctionPass*
86CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper,
87 IRBuilder& irb);
buzbeec531cef2012-10-18 07:09:20 -070088#elif defined(ART_USE_PORTABLE_COMPILER)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -070089llvm::FunctionPass*
90CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
91 Compiler* compiler, OatCompilationUnit* oat_compilation_unit);
Shih-wei Liao21d28f52012-06-12 05:55:00 -070092#endif
93
Logan Chien8b977d32012-02-21 19:14:55 +080094llvm::Module* makeLLVMModuleContents(llvm::Module* module);
95
96
Logan Chien971bf3f2012-05-01 15:47:55 +080097CompilationUnit::CompilationUnit(const CompilerLLVM* compiler_llvm,
98 size_t cunit_idx)
TDYa12755e5e6c2012-09-11 15:14:42 -070099: compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx) {
buzbeec531cef2012-10-18 07:09:20 -0700100#if !defined(ART_USE_PORTABLE_COMPILER)
TDYa12755e5e6c2012-09-11 15:14:42 -0700101 context_.reset(new llvm::LLVMContext());
Logan Chien8b977d32012-02-21 19:14:55 +0800102 module_ = new llvm::Module("art", *context_);
TDYa12755e5e6c2012-09-11 15:14:42 -0700103#else
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700104 compiler_ = NULL;
105 oat_compilation_unit_ = NULL;
buzbee4df2bbd2012-10-11 14:46:06 -0700106 llvm_info_.reset(new LLVMInfo());
107 context_.reset(llvm_info_->GetLLVMContext());
108 module_ = llvm_info_->GetLLVMModule();
TDYa12755e5e6c2012-09-11 15:14:42 -0700109#endif
110
111 // Include the runtime function declaration
112 makeLLVMModuleContents(module_);
113
114#if defined(ART_USE_DEXLANG_FRONTEND)
115 dex_lang_ctx_ = new greenland::DexLang::Context(*module_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700116#endif
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700117
Logan Chien8b977d32012-02-21 19:14:55 +0800118 // Create IRBuilder
119 irb_.reset(new IRBuilder(*context_, *module_));
TDYa127d668a062012-04-13 12:36:57 -0700120
121 // We always need a switch case, so just use a normal function.
Logan Chien971bf3f2012-05-01 15:47:55 +0800122 switch(GetInstructionSet()) {
TDYa127b08ed122012-06-05 23:51:19 -0700123 default:
124 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
125 break;
TDYa127d668a062012-04-13 12:36:57 -0700126 case kArm:
TDYa127d668a062012-04-13 12:36:57 -0700127 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
128 break;
TDYa127b08ed122012-06-05 23:51:19 -0700129 case kThumb2:
130 runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_));
131 break;
TDYa127d668a062012-04-13 12:36:57 -0700132 case kX86:
133 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
134 break;
135 }
136
TDYa127d668a062012-04-13 12:36:57 -0700137 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800138}
139
140
141CompilationUnit::~CompilationUnit() {
TDYa12755e5e6c2012-09-11 15:14:42 -0700142#if defined(ART_USE_DEXLANG_FRONTEND)
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700143 delete dex_lang_ctx_;
buzbeec531cef2012-10-18 07:09:20 -0700144#elif defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -0700145 llvm::LLVMContext* llvm_context = context_.release(); // Managed by llvm_info_
TDYa12755e5e6c2012-09-11 15:14:42 -0700146 CHECK(llvm_context != NULL);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700147#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800148}
149
150
Logan Chien971bf3f2012-05-01 15:47:55 +0800151InstructionSet CompilationUnit::GetInstructionSet() const {
152 return compiler_llvm_->GetInstructionSet();
Logan Chien8b977d32012-02-21 19:14:55 +0800153}
154
155
Logan Chien971bf3f2012-05-01 15:47:55 +0800156bool CompilationUnit::Materialize() {
157 std::string elf_image;
Logan Chien110bcba2012-04-16 19:11:28 +0800158
Logan Chien971bf3f2012-05-01 15:47:55 +0800159 // Compile and prelink llvm::Module
160 if (!MaterializeToString(elf_image)) {
161 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_idx_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800162 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800163 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800164
165#if 0
166 // Dump the ELF image for debugging
167 std::string filename(StringPrintf("%s/Art%zu.elf",
168 GetArtCacheOrDie(GetAndroidData()).c_str(),
169 cunit_idx_));
170 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
171 output->WriteFully(elf_image.data(), elf_image.size());
172#endif
173
174 // Extract the .text section and prelink the code
175 if (!ExtractCodeAndPrelink(elf_image)) {
176 LOG(ERROR) << "Failed to extract code from compilation unit " << cunit_idx_;
Logan Chien971bf3f2012-05-01 15:47:55 +0800177 return false;
178 }
179
Logan Chien971bf3f2012-05-01 15:47:55 +0800180 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800181}
182
Logan Chien971bf3f2012-05-01 15:47:55 +0800183
184bool CompilationUnit::MaterializeToString(std::string& str_buffer) {
185 llvm::raw_string_ostream str_os(str_buffer);
186 return MaterializeToRawOStream(str_os);
187}
188
189
190bool CompilationUnit::MaterializeToRawOStream(llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700191 // Lookup the LLVM target
Logan Chien12584172012-07-10 04:07:28 -0700192 const char* target_triple = NULL;
193 const char* target_cpu = "";
194 const char* target_attr = NULL;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700195
Logan Chien971bf3f2012-05-01 15:47:55 +0800196 InstructionSet insn_set = GetInstructionSet();
197 switch (insn_set) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700198 case kThumb2:
199 target_triple = "thumb-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700200 target_cpu = "cortex-a9";
TDYa127b08ed122012-06-05 23:51:19 -0700201 target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700202 break;
203
204 case kArm:
205 target_triple = "armv7-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700206 // TODO: Fix for Nexus S.
207 target_cpu = "cortex-a9";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700208 // TODO: Fix for Xoom.
TDYa127b08ed122012-06-05 23:51:19 -0700209 target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700210 break;
211
212 case kX86:
213 target_triple = "i386-pc-linux-gnu";
214 target_attr = "";
215 break;
216
217 case kMips:
218 target_triple = "mipsel-unknown-linux";
219 target_attr = "mips32r2";
220 break;
221
222 default:
Logan Chien971bf3f2012-05-01 15:47:55 +0800223 LOG(FATAL) << "Unknown instruction set: " << insn_set;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700224 }
225
226 std::string errmsg;
Logan Chien12584172012-07-10 04:07:28 -0700227 const llvm::Target* target =
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700228 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
229
230 CHECK(target != NULL) << errmsg;
231
232 // Target options
233 llvm::TargetOptions target_options;
234 target_options.FloatABIType = llvm::FloatABI::Soft;
235 target_options.NoFramePointerElim = true;
236 target_options.NoFramePointerElimNonLeaf = true;
237 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700238 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700239
240 // Create the llvm::TargetMachine
Logan Chienb6bed0b2012-05-04 15:03:56 +0800241 llvm::OwningPtr<llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700242 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700243 llvm::Reloc::Static, llvm::CodeModel::Small,
Shih-wei Liaodac5eb22012-06-03 14:06:04 -0700244 llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700245
Logan Chienb6bed0b2012-05-04 15:03:56 +0800246 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700247
248 // Add target data
Logan Chien12584172012-07-10 04:07:28 -0700249 const llvm::TargetData* target_data = target_machine->getTargetData();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700250
251 // PassManager for code generation passes
252 llvm::PassManager pm;
253 pm.add(new llvm::TargetData(*target_data));
254
255 // FunctionPassManager for optimization pass
Logan Chien799ef4f2012-04-23 00:17:47 +0800256 llvm::FunctionPassManager fpm(module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700257 fpm.add(new llvm::TargetData(*target_data));
258
TDYa127f15b0ab2012-05-11 21:01:36 -0700259 if (bitcode_filename_.empty()) {
260 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
261 // regular FunctionPass.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800262#if defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -0700263 fpm.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700264 compiler_, oat_compilation_unit_));
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700265#endif
TDYa127f15b0ab2012-05-11 21:01:36 -0700266 } else {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800267#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127f15b0ab2012-05-11 21:01:36 -0700268 llvm::FunctionPassManager fpm2(module_);
buzbee4df2bbd2012-10-11 14:46:06 -0700269 fpm2.add(CreateGBCExpanderPass(*llvm_info_->GetIntrinsicHelper(), *irb_.get(),
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700270 compiler_, oat_compilation_unit_));
TDYa127f15b0ab2012-05-11 21:01:36 -0700271 fpm2.doInitialization();
272 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
273 F != E; ++F) {
274 fpm2.run(*F);
275 }
276 fpm2.doFinalization();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800277#endif
TDYa127f15b0ab2012-05-11 21:01:36 -0700278
279 // Write bitcode to file
280 std::string errmsg;
281
282 llvm::OwningPtr<llvm::tool_output_file> out_file(
283 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
284 llvm::raw_fd_ostream::F_Binary));
285
286
287 if (!errmsg.empty()) {
288 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
289 return false;
290 }
291
292 llvm::WriteBitcodeToFile(module_, out_file->os());
293 out_file->keep();
294 }
295
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700296 // Add optimization pass
297 llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700298 // TODO: Use inliner after we can do IPO.
299 pm_builder.Inliner = NULL;
Shih-wei Liaoe0e40242012-05-08 01:04:03 -0700300 //pm_builder.Inliner = llvm::createFunctionInliningPass();
TDYa1279a129452012-07-19 03:10:08 -0700301 //pm_builder.Inliner = llvm::createAlwaysInlinerPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700302 //pm_builder.Inliner = llvm::createPartialInliningPass();
303 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700304 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700305 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700306 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700307 pm_builder.populateModulePassManager(pm);
308 pm.add(llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700309
310 // Add passes to emit ELF image
311 {
Logan Chien08e1ba32012-05-08 15:08:51 +0800312 llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700313
314 // Ask the target to add backend passes as necessary.
315 if (target_machine->addPassesToEmitFile(pm,
316 formatted_os,
317 llvm::TargetMachine::CGFT_ObjectFile,
318 true)) {
319 LOG(FATAL) << "Unable to generate ELF for this target";
320 return false;
321 }
322
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700323 // Run the per-function optimization
324 fpm.doInitialization();
Logan Chien799ef4f2012-04-23 00:17:47 +0800325 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700326 F != E; ++F) {
327 fpm.run(*F);
328 }
329 fpm.doFinalization();
330
331 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800332 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700333 }
334
335 return true;
336}
Logan Chien110bcba2012-04-16 19:11:28 +0800337
Logan Chien971bf3f2012-05-01 15:47:55 +0800338
339bool CompilationUnit::ExtractCodeAndPrelink(const std::string& elf_image) {
Shih-wei Liao02a1e352012-06-30 00:42:07 -0700340 if (GetInstructionSet() == kX86) {
341 compiled_code_.push_back(0xccU);
342 compiled_code_.push_back(0xccU);
343 compiled_code_.push_back(0xccU);
344 compiled_code_.push_back(0xccU);
345 return true;
346 }
347
Logan Chien971bf3f2012-05-01 15:47:55 +0800348 llvm::OwningPtr<llvm::MemoryBuffer> elf_image_buff(
349 llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(elf_image.data(),
350 elf_image.size())));
351
352 llvm::OwningPtr<llvm::object::ObjectFile> elf_file(
353 llvm::object::ObjectFile::createELFObjectFile(elf_image_buff.take()));
354
355 llvm::error_code ec;
356
357 const ProcedureLinkageTable& plt = compiler_llvm_->GetProcedureLinkageTable();
358
359 for (llvm::object::section_iterator
360 sec_iter = elf_file->begin_sections(),
361 sec_end = elf_file->end_sections();
362 sec_iter != sec_end; sec_iter.increment(ec)) {
363
364 CHECK(ec == 0) << "Failed to read section because " << ec.message();
365
366 // Read the section information
367 llvm::StringRef name;
368 uint64_t alignment = 0u;
369 uint64_t size = 0u;
370
371 CHECK(sec_iter->getName(name) == 0);
372 CHECK(sec_iter->getSize(size) == 0);
373 CHECK(sec_iter->getAlignment(alignment) == 0);
374
375 if (name == ".data" || name == ".bss" || name == ".rodata") {
376 if (size > 0) {
377 LOG(FATAL) << "Compilation unit " << cunit_idx_ << " has non-empty "
378 << name.str() << " section";
379 }
380
381 } else if (name == "" || name == ".rel.text" ||
382 name == ".ARM.attributes" || name == ".symtab" ||
383 name == ".strtab" || name == ".shstrtab") {
384 // We can ignore these sections. We don't have to copy them into
385 // the result Oat file.
386
387 } else if (name == ".text") {
388 // Ensure the alignment requirement is less than or equal to
389 // kArchAlignment
390 CheckCodeAlign(alignment);
391
392 // Copy the compiled code
393 llvm::StringRef contents;
394 CHECK(sec_iter->getContents(contents) == 0);
395
396 copy(contents.data(),
397 contents.data() + contents.size(),
398 back_inserter(compiled_code_));
399
400 // Prelink the compiled code
401 for (llvm::object::relocation_iterator
402 rel_iter = sec_iter->begin_relocations(),
403 rel_end = sec_iter->end_relocations(); rel_iter != rel_end;
404 rel_iter.increment(ec)) {
405
406 CHECK(ec == 0) << "Failed to read relocation because " << ec.message();
407
408 // Read the relocation information
409 llvm::object::SymbolRef sym_ref;
410 uint64_t rel_offset = 0;
411 uint64_t rel_type = 0;
412 int64_t rel_addend = 0;
413
414 CHECK(rel_iter->getSymbol(sym_ref) == 0);
415 CHECK(rel_iter->getOffset(rel_offset) == 0);
416 CHECK(rel_iter->getType(rel_type) == 0);
417 CHECK(rel_iter->getAdditionalInfo(rel_addend) == 0);
418
419 // Read the symbol related to this relocation fixup
420 llvm::StringRef sym_name;
421 CHECK(sym_ref.getName(sym_name) == 0);
422
423 // Relocate the fixup.
424 // TODO: Support more relocation type.
425 CHECK(rel_type == llvm::ELF::R_ARM_ABS32);
426 CHECK_LE(rel_offset + 4, compiled_code_.size());
427
428 uintptr_t dest_addr = plt.GetEntryAddress(sym_name.str().c_str());
429 uintptr_t final_addr = dest_addr + rel_addend;
430 compiled_code_[rel_offset] = final_addr & 0xff;
431 compiled_code_[rel_offset + 1] = (final_addr >> 8) & 0xff;
432 compiled_code_[rel_offset + 2] = (final_addr >> 16) & 0xff;
433 compiled_code_[rel_offset + 3] = (final_addr >> 24) & 0xff;
434 }
435
436 } else {
437 LOG(WARNING) << "Unexpected section: " << name.str();
438 }
439 }
440
441 return true;
442}
443
444
445// Check whether the align is less than or equal to the code alignment of
446// that architecture. Since the Oat writer only guarantee that the compiled
447// method being aligned to kArchAlignment, we have no way to align the ELf
448// section if the section alignment is greater than kArchAlignment.
449void CompilationUnit::CheckCodeAlign(uint32_t align) const {
450 InstructionSet insn_set = GetInstructionSet();
451 switch (insn_set) {
452 case kThumb2:
453 case kArm:
454 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
455 break;
456
457 case kX86:
458 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
459 break;
460
461 case kMips:
462 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
463 break;
464
465 default:
466 LOG(FATAL) << "Unknown instruction set: " << insn_set;
467 }
468}
469
470
Logan Chien8b977d32012-02-21 19:14:55 +0800471} // namespace compiler_llvm
472} // namespace art