blob: b5ff67cfff21bdd788d883079bd0aa1bbe88bb24 [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
Logan Chien110bcba2012-04-16 19:11:28 +080081namespace {
82
TDYa127f15b0ab2012-05-11 21:01:36 -070083// TODO: We may need something to manage these passes.
84// TODO: We need high-level IR to analysis and do this at the IRBuilder level.
85class AddSuspendCheckToLoopLatchPass : public llvm::LoopPass {
86 public:
87 static char ID;
88
89 AddSuspendCheckToLoopLatchPass() : llvm::LoopPass(ID), irb_(NULL) {
90 LOG(FATAL) << "Unexpected instantiation of AddSuspendCheckToLoopLatchPass";
91 // NOTE: We have to declare this constructor for llvm::RegisterPass, but
92 // this constructor won't work because we have no information on
93 // IRBuilder. Thus, we should place a LOG(FATAL) here.
94 }
95
96 AddSuspendCheckToLoopLatchPass(art::compiler_llvm::IRBuilder* irb)
97 : llvm::LoopPass(ID), irb_(irb) {
98 }
99
100 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const {
TDYa1279a129452012-07-19 03:10:08 -0700101 AU.addRequired<llvm::LoopInfo>();
TDYa127f15b0ab2012-05-11 21:01:36 -0700102 AU.addRequiredID(llvm::LoopSimplifyID);
103
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700104 AU.addPreserved<llvm::DominatorTree>();
TDYa127f15b0ab2012-05-11 21:01:36 -0700105 AU.addPreserved<llvm::LoopInfo>();
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700106 AU.addPreservedID(llvm::LoopSimplifyID);
TDYa127f15b0ab2012-05-11 21:01:36 -0700107 AU.addPreserved<llvm::ScalarEvolution>();
108 AU.addPreservedID(llvm::BreakCriticalEdgesID);
109 }
110
111 virtual bool runOnLoop(llvm::Loop *loop, llvm::LPPassManager &lpm) {
TDYa1279a129452012-07-19 03:10:08 -0700112 llvm::LoopInfo* loop_info = &getAnalysis<llvm::LoopInfo>();
113
TDYa127f15b0ab2012-05-11 21:01:36 -0700114 CHECK_EQ(loop->getNumBackEdges(), 1U) << "Loop must be simplified!";
115 llvm::BasicBlock* bb = loop->getLoopLatch();
116 CHECK_NE(bb, static_cast<void*>(NULL)) << "A single loop latch must exist.";
117
TDYa1279a129452012-07-19 03:10:08 -0700118 llvm::BasicBlock* tb = bb->splitBasicBlock(bb->getTerminator(), "suspend_exit");
119 // Remove unconditional branch which is added by splitBasicBlock.
120 bb->getTerminator()->eraseFromParent();
TDYa127f15b0ab2012-05-11 21:01:36 -0700121
TDYa1279a129452012-07-19 03:10:08 -0700122 irb_->SetInsertPoint(bb);
123 irb_->Runtime().EmitTestSuspend();
124 irb_->CreateBr(tb);
125
126 loop->addBasicBlockToLoop(tb, loop_info->getBase());
127 // EmitTestSuspend() creates some basic blocks. We should add them to using
128 // addBasicBlockToLoop(...) as above.
129 for (llvm::succ_iterator succ_iter = llvm::succ_begin(bb), succ_end = llvm::succ_end(bb);
130 succ_iter != succ_end;
131 succ_iter++) {
132 loop->addBasicBlockToLoop(*succ_iter, loop_info->getBase());
133 }
TDYa127f15b0ab2012-05-11 21:01:36 -0700134
135 return true;
136 }
137
138 private:
139 art::compiler_llvm::IRBuilder* irb_;
140};
141
142char AddSuspendCheckToLoopLatchPass::ID = 0;
143
144llvm::RegisterPass<AddSuspendCheckToLoopLatchPass> reg_add_suspend_check_to_loop_latch_pass_(
145 "add-suspend-check-to-loop-latch", "Add suspend check to loop latch pass", false, false);
146
147
Logan Chien110bcba2012-04-16 19:11:28 +0800148} // end anonymous namespace
149
Logan Chien8b977d32012-02-21 19:14:55 +0800150namespace art {
151namespace compiler_llvm {
152
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700153#ifdef ART_USE_DEXLANG_FRONTEND
154llvm::FunctionPass*
155CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper,
156 IRBuilder& irb);
157#endif
158
Logan Chien8b977d32012-02-21 19:14:55 +0800159llvm::Module* makeLLVMModuleContents(llvm::Module* module);
160
161
Logan Chien971bf3f2012-05-01 15:47:55 +0800162CompilationUnit::CompilationUnit(const CompilerLLVM* compiler_llvm,
163 size_t cunit_idx)
164: compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx),
165 context_(new llvm::LLVMContext()) {
Logan Chien8b977d32012-02-21 19:14:55 +0800166
167 // Create the module and include the runtime function declaration
168 module_ = new llvm::Module("art", *context_);
169 makeLLVMModuleContents(module_);
170
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700171#ifdef ART_USE_DEXLANG_FRONTEND
172 dex_lang_ctx_ = new greenland::DexLang::Context(*module_);
173#endif
174
Logan Chien8b977d32012-02-21 19:14:55 +0800175 // Create IRBuilder
176 irb_.reset(new IRBuilder(*context_, *module_));
TDYa127d668a062012-04-13 12:36:57 -0700177
178 // We always need a switch case, so just use a normal function.
Logan Chien971bf3f2012-05-01 15:47:55 +0800179 switch(GetInstructionSet()) {
TDYa127b08ed122012-06-05 23:51:19 -0700180 default:
181 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
182 break;
TDYa127d668a062012-04-13 12:36:57 -0700183 case kArm:
TDYa127d668a062012-04-13 12:36:57 -0700184 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
185 break;
TDYa127b08ed122012-06-05 23:51:19 -0700186 case kThumb2:
187 runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_));
188 break;
TDYa127d668a062012-04-13 12:36:57 -0700189 case kX86:
190 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
191 break;
192 }
193
TDYa127d668a062012-04-13 12:36:57 -0700194 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800195}
196
197
198CompilationUnit::~CompilationUnit() {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700199#ifdef ART_USE_DEXLANG_FRONTEND
200 delete dex_lang_ctx_;
201#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800202}
203
204
Logan Chien971bf3f2012-05-01 15:47:55 +0800205InstructionSet CompilationUnit::GetInstructionSet() const {
206 return compiler_llvm_->GetInstructionSet();
Logan Chien8b977d32012-02-21 19:14:55 +0800207}
208
209
Logan Chien971bf3f2012-05-01 15:47:55 +0800210bool CompilationUnit::Materialize() {
211 std::string elf_image;
Logan Chien110bcba2012-04-16 19:11:28 +0800212
Logan Chien971bf3f2012-05-01 15:47:55 +0800213 // Compile and prelink llvm::Module
214 if (!MaterializeToString(elf_image)) {
215 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_idx_;
216 DeleteResources();
217 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800218 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800219
220#if 0
221 // Dump the ELF image for debugging
222 std::string filename(StringPrintf("%s/Art%zu.elf",
223 GetArtCacheOrDie(GetAndroidData()).c_str(),
224 cunit_idx_));
225 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
226 output->WriteFully(elf_image.data(), elf_image.size());
227#endif
228
229 // Extract the .text section and prelink the code
230 if (!ExtractCodeAndPrelink(elf_image)) {
231 LOG(ERROR) << "Failed to extract code from compilation unit " << cunit_idx_;
232 DeleteResources();
233 return false;
234 }
235
236 DeleteResources();
237 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800238}
239
Logan Chien971bf3f2012-05-01 15:47:55 +0800240
241bool CompilationUnit::MaterializeToString(std::string& str_buffer) {
242 llvm::raw_string_ostream str_os(str_buffer);
243 return MaterializeToRawOStream(str_os);
244}
245
246
247bool CompilationUnit::MaterializeToRawOStream(llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700248 // Lookup the LLVM target
Logan Chien12584172012-07-10 04:07:28 -0700249 const char* target_triple = NULL;
250 const char* target_cpu = "";
251 const char* target_attr = NULL;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700252
Logan Chien971bf3f2012-05-01 15:47:55 +0800253 InstructionSet insn_set = GetInstructionSet();
254 switch (insn_set) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700255 case kThumb2:
256 target_triple = "thumb-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700257 target_cpu = "cortex-a9";
TDYa127b08ed122012-06-05 23:51:19 -0700258 target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700259 break;
260
261 case kArm:
262 target_triple = "armv7-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700263 // TODO: Fix for Nexus S.
264 target_cpu = "cortex-a9";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700265 // TODO: Fix for Xoom.
TDYa127b08ed122012-06-05 23:51:19 -0700266 target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700267 break;
268
269 case kX86:
270 target_triple = "i386-pc-linux-gnu";
271 target_attr = "";
272 break;
273
274 case kMips:
275 target_triple = "mipsel-unknown-linux";
276 target_attr = "mips32r2";
277 break;
278
279 default:
Logan Chien971bf3f2012-05-01 15:47:55 +0800280 LOG(FATAL) << "Unknown instruction set: " << insn_set;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700281 }
282
283 std::string errmsg;
Logan Chien12584172012-07-10 04:07:28 -0700284 const llvm::Target* target =
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700285 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
286
287 CHECK(target != NULL) << errmsg;
288
289 // Target options
290 llvm::TargetOptions target_options;
291 target_options.FloatABIType = llvm::FloatABI::Soft;
292 target_options.NoFramePointerElim = true;
293 target_options.NoFramePointerElimNonLeaf = true;
294 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700295 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700296
297 // Create the llvm::TargetMachine
Logan Chienb6bed0b2012-05-04 15:03:56 +0800298 llvm::OwningPtr<llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700299 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700300 llvm::Reloc::Static, llvm::CodeModel::Small,
Shih-wei Liaodac5eb22012-06-03 14:06:04 -0700301 llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700302
Logan Chienb6bed0b2012-05-04 15:03:56 +0800303 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700304
305 // Add target data
Logan Chien12584172012-07-10 04:07:28 -0700306 const llvm::TargetData* target_data = target_machine->getTargetData();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700307
308 // PassManager for code generation passes
309 llvm::PassManager pm;
310 pm.add(new llvm::TargetData(*target_data));
311
312 // FunctionPassManager for optimization pass
Logan Chien799ef4f2012-04-23 00:17:47 +0800313 llvm::FunctionPassManager fpm(module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700314 fpm.add(new llvm::TargetData(*target_data));
315
TDYa127f15b0ab2012-05-11 21:01:36 -0700316 if (bitcode_filename_.empty()) {
317 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
318 // regular FunctionPass.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700319#ifdef ART_USE_DEXLANG_FRONTEND
320 fpm.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get()));
321#endif
TDYa127f15b0ab2012-05-11 21:01:36 -0700322 fpm.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
323 } else {
324 // Run AddSuspendCheckToLoopLatchPass before we write the bitcode to file.
325 llvm::FunctionPassManager fpm2(module_);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700326#ifdef ART_USE_DEXLANG_FRONTEND
327 fpm2.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get()));
328#endif
TDYa127f15b0ab2012-05-11 21:01:36 -0700329 fpm2.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
330 fpm2.doInitialization();
331 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
332 F != E; ++F) {
333 fpm2.run(*F);
334 }
335 fpm2.doFinalization();
336
337
338 // Write bitcode to file
339 std::string errmsg;
340
341 llvm::OwningPtr<llvm::tool_output_file> out_file(
342 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
343 llvm::raw_fd_ostream::F_Binary));
344
345
346 if (!errmsg.empty()) {
347 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
348 return false;
349 }
350
351 llvm::WriteBitcodeToFile(module_, out_file->os());
352 out_file->keep();
353 }
354
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700355 // Add optimization pass
356 llvm::PassManagerBuilder pm_builder;
TDYa1279a129452012-07-19 03:10:08 -0700357 // TODO: Use inliner after we can do IPO.
358 pm_builder.Inliner = NULL;
Shih-wei Liaoe0e40242012-05-08 01:04:03 -0700359 //pm_builder.Inliner = llvm::createFunctionInliningPass();
TDYa1279a129452012-07-19 03:10:08 -0700360 //pm_builder.Inliner = llvm::createAlwaysInlinerPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700361 //pm_builder.Inliner = llvm::createPartialInliningPass();
362 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700363 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700364 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700365 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700366 pm_builder.populateModulePassManager(pm);
367 pm.add(llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700368
369 // Add passes to emit ELF image
370 {
Logan Chien08e1ba32012-05-08 15:08:51 +0800371 llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700372
373 // Ask the target to add backend passes as necessary.
374 if (target_machine->addPassesToEmitFile(pm,
375 formatted_os,
376 llvm::TargetMachine::CGFT_ObjectFile,
377 true)) {
378 LOG(FATAL) << "Unable to generate ELF for this target";
379 return false;
380 }
381
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700382 // Run the per-function optimization
383 fpm.doInitialization();
Logan Chien799ef4f2012-04-23 00:17:47 +0800384 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700385 F != E; ++F) {
386 fpm.run(*F);
387 }
388 fpm.doFinalization();
389
390 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800391 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700392 }
393
394 return true;
395}
Logan Chien110bcba2012-04-16 19:11:28 +0800396
Logan Chien971bf3f2012-05-01 15:47:55 +0800397
398bool CompilationUnit::ExtractCodeAndPrelink(const std::string& elf_image) {
Shih-wei Liao02a1e352012-06-30 00:42:07 -0700399 if (GetInstructionSet() == kX86) {
400 compiled_code_.push_back(0xccU);
401 compiled_code_.push_back(0xccU);
402 compiled_code_.push_back(0xccU);
403 compiled_code_.push_back(0xccU);
404 return true;
405 }
406
Logan Chien971bf3f2012-05-01 15:47:55 +0800407 llvm::OwningPtr<llvm::MemoryBuffer> elf_image_buff(
408 llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(elf_image.data(),
409 elf_image.size())));
410
411 llvm::OwningPtr<llvm::object::ObjectFile> elf_file(
412 llvm::object::ObjectFile::createELFObjectFile(elf_image_buff.take()));
413
414 llvm::error_code ec;
415
416 const ProcedureLinkageTable& plt = compiler_llvm_->GetProcedureLinkageTable();
417
418 for (llvm::object::section_iterator
419 sec_iter = elf_file->begin_sections(),
420 sec_end = elf_file->end_sections();
421 sec_iter != sec_end; sec_iter.increment(ec)) {
422
423 CHECK(ec == 0) << "Failed to read section because " << ec.message();
424
425 // Read the section information
426 llvm::StringRef name;
427 uint64_t alignment = 0u;
428 uint64_t size = 0u;
429
430 CHECK(sec_iter->getName(name) == 0);
431 CHECK(sec_iter->getSize(size) == 0);
432 CHECK(sec_iter->getAlignment(alignment) == 0);
433
434 if (name == ".data" || name == ".bss" || name == ".rodata") {
435 if (size > 0) {
436 LOG(FATAL) << "Compilation unit " << cunit_idx_ << " has non-empty "
437 << name.str() << " section";
438 }
439
440 } else if (name == "" || name == ".rel.text" ||
441 name == ".ARM.attributes" || name == ".symtab" ||
442 name == ".strtab" || name == ".shstrtab") {
443 // We can ignore these sections. We don't have to copy them into
444 // the result Oat file.
445
446 } else if (name == ".text") {
447 // Ensure the alignment requirement is less than or equal to
448 // kArchAlignment
449 CheckCodeAlign(alignment);
450
451 // Copy the compiled code
452 llvm::StringRef contents;
453 CHECK(sec_iter->getContents(contents) == 0);
454
455 copy(contents.data(),
456 contents.data() + contents.size(),
457 back_inserter(compiled_code_));
458
459 // Prelink the compiled code
460 for (llvm::object::relocation_iterator
461 rel_iter = sec_iter->begin_relocations(),
462 rel_end = sec_iter->end_relocations(); rel_iter != rel_end;
463 rel_iter.increment(ec)) {
464
465 CHECK(ec == 0) << "Failed to read relocation because " << ec.message();
466
467 // Read the relocation information
468 llvm::object::SymbolRef sym_ref;
469 uint64_t rel_offset = 0;
470 uint64_t rel_type = 0;
471 int64_t rel_addend = 0;
472
473 CHECK(rel_iter->getSymbol(sym_ref) == 0);
474 CHECK(rel_iter->getOffset(rel_offset) == 0);
475 CHECK(rel_iter->getType(rel_type) == 0);
476 CHECK(rel_iter->getAdditionalInfo(rel_addend) == 0);
477
478 // Read the symbol related to this relocation fixup
479 llvm::StringRef sym_name;
480 CHECK(sym_ref.getName(sym_name) == 0);
481
482 // Relocate the fixup.
483 // TODO: Support more relocation type.
484 CHECK(rel_type == llvm::ELF::R_ARM_ABS32);
485 CHECK_LE(rel_offset + 4, compiled_code_.size());
486
487 uintptr_t dest_addr = plt.GetEntryAddress(sym_name.str().c_str());
488 uintptr_t final_addr = dest_addr + rel_addend;
489 compiled_code_[rel_offset] = final_addr & 0xff;
490 compiled_code_[rel_offset + 1] = (final_addr >> 8) & 0xff;
491 compiled_code_[rel_offset + 2] = (final_addr >> 16) & 0xff;
492 compiled_code_[rel_offset + 3] = (final_addr >> 24) & 0xff;
493 }
494
495 } else {
496 LOG(WARNING) << "Unexpected section: " << name.str();
497 }
498 }
499
500 return true;
501}
502
503
504// Check whether the align is less than or equal to the code alignment of
505// that architecture. Since the Oat writer only guarantee that the compiled
506// method being aligned to kArchAlignment, we have no way to align the ELf
507// section if the section alignment is greater than kArchAlignment.
508void CompilationUnit::CheckCodeAlign(uint32_t align) const {
509 InstructionSet insn_set = GetInstructionSet();
510 switch (insn_set) {
511 case kThumb2:
512 case kArm:
513 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
514 break;
515
516 case kX86:
517 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
518 break;
519
520 case kMips:
521 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
522 break;
523
524 default:
525 LOG(FATAL) << "Unknown instruction set: " << insn_set;
526 }
527}
528
529
Logan Chien8b977d32012-02-21 19:14:55 +0800530} // namespace compiler_llvm
531} // namespace art