blob: 87fd72478312a56fcd7be52cd42b2b5b3726bb58 [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 {
101 AU.addRequiredID(llvm::LoopSimplifyID);
102
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700103 AU.addPreserved<llvm::DominatorTree>();
TDYa127f15b0ab2012-05-11 21:01:36 -0700104 AU.addPreserved<llvm::LoopInfo>();
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700105 AU.addPreservedID(llvm::LoopSimplifyID);
TDYa127f15b0ab2012-05-11 21:01:36 -0700106 AU.addPreserved<llvm::ScalarEvolution>();
107 AU.addPreservedID(llvm::BreakCriticalEdgesID);
108 }
109
110 virtual bool runOnLoop(llvm::Loop *loop, llvm::LPPassManager &lpm) {
111 CHECK_EQ(loop->getNumBackEdges(), 1U) << "Loop must be simplified!";
112 llvm::BasicBlock* bb = loop->getLoopLatch();
113 CHECK_NE(bb, static_cast<void*>(NULL)) << "A single loop latch must exist.";
114
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700115 irb_->SetInsertPoint(bb->getTerminator());
TDYa127f15b0ab2012-05-11 21:01:36 -0700116
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700117 using art::compiler_llvm::runtime_support::TestSuspend;
118 llvm::Value* runtime_func = irb_->GetRuntime(TestSuspend);
119 irb_->CreateCall(runtime_func, irb_->getJNull());
TDYa127f15b0ab2012-05-11 21:01:36 -0700120
121 return true;
122 }
123
124 private:
125 art::compiler_llvm::IRBuilder* irb_;
126};
127
128char AddSuspendCheckToLoopLatchPass::ID = 0;
129
130llvm::RegisterPass<AddSuspendCheckToLoopLatchPass> reg_add_suspend_check_to_loop_latch_pass_(
131 "add-suspend-check-to-loop-latch", "Add suspend check to loop latch pass", false, false);
132
133
Logan Chien110bcba2012-04-16 19:11:28 +0800134} // end anonymous namespace
135
Logan Chien8b977d32012-02-21 19:14:55 +0800136namespace art {
137namespace compiler_llvm {
138
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700139#ifdef ART_USE_DEXLANG_FRONTEND
140llvm::FunctionPass*
141CreateGBCExpanderPass(const greenland::IntrinsicHelper& intrinsic_helper,
142 IRBuilder& irb);
143#endif
144
Logan Chien8b977d32012-02-21 19:14:55 +0800145llvm::Module* makeLLVMModuleContents(llvm::Module* module);
146
147
Logan Chien971bf3f2012-05-01 15:47:55 +0800148CompilationUnit::CompilationUnit(const CompilerLLVM* compiler_llvm,
149 size_t cunit_idx)
150: compiler_llvm_(compiler_llvm), cunit_idx_(cunit_idx),
151 context_(new llvm::LLVMContext()) {
Logan Chien8b977d32012-02-21 19:14:55 +0800152
153 // Create the module and include the runtime function declaration
154 module_ = new llvm::Module("art", *context_);
155 makeLLVMModuleContents(module_);
156
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700157#ifdef ART_USE_DEXLANG_FRONTEND
158 dex_lang_ctx_ = new greenland::DexLang::Context(*module_);
159#endif
160
Logan Chien8b977d32012-02-21 19:14:55 +0800161 // Create IRBuilder
162 irb_.reset(new IRBuilder(*context_, *module_));
TDYa127d668a062012-04-13 12:36:57 -0700163
164 // We always need a switch case, so just use a normal function.
Logan Chien971bf3f2012-05-01 15:47:55 +0800165 switch(GetInstructionSet()) {
TDYa127b08ed122012-06-05 23:51:19 -0700166 default:
167 runtime_support_.reset(new RuntimeSupportBuilder(*context_, *module_, *irb_));
168 break;
TDYa127d668a062012-04-13 12:36:57 -0700169 case kArm:
TDYa127d668a062012-04-13 12:36:57 -0700170 runtime_support_.reset(new RuntimeSupportBuilderARM(*context_, *module_, *irb_));
171 break;
TDYa127b08ed122012-06-05 23:51:19 -0700172 case kThumb2:
173 runtime_support_.reset(new RuntimeSupportBuilderThumb2(*context_, *module_, *irb_));
174 break;
TDYa127d668a062012-04-13 12:36:57 -0700175 case kX86:
176 runtime_support_.reset(new RuntimeSupportBuilderX86(*context_, *module_, *irb_));
177 break;
178 }
179
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700180 runtime_support_->OptimizeRuntimeSupport();
181
TDYa127d668a062012-04-13 12:36:57 -0700182 irb_->SetRuntimeSupport(runtime_support_.get());
Logan Chien8b977d32012-02-21 19:14:55 +0800183}
184
185
186CompilationUnit::~CompilationUnit() {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700187#ifdef ART_USE_DEXLANG_FRONTEND
188 delete dex_lang_ctx_;
189#endif
Logan Chien8b977d32012-02-21 19:14:55 +0800190}
191
192
Logan Chien971bf3f2012-05-01 15:47:55 +0800193InstructionSet CompilationUnit::GetInstructionSet() const {
194 return compiler_llvm_->GetInstructionSet();
Logan Chien8b977d32012-02-21 19:14:55 +0800195}
196
197
Logan Chien971bf3f2012-05-01 15:47:55 +0800198bool CompilationUnit::Materialize() {
199 std::string elf_image;
Logan Chien110bcba2012-04-16 19:11:28 +0800200
Logan Chien971bf3f2012-05-01 15:47:55 +0800201 // Compile and prelink llvm::Module
202 if (!MaterializeToString(elf_image)) {
203 LOG(ERROR) << "Failed to materialize compilation unit " << cunit_idx_;
204 DeleteResources();
205 return false;
Logan Chien110bcba2012-04-16 19:11:28 +0800206 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800207
208#if 0
209 // Dump the ELF image for debugging
210 std::string filename(StringPrintf("%s/Art%zu.elf",
211 GetArtCacheOrDie(GetAndroidData()).c_str(),
212 cunit_idx_));
213 UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
214 output->WriteFully(elf_image.data(), elf_image.size());
215#endif
216
217 // Extract the .text section and prelink the code
218 if (!ExtractCodeAndPrelink(elf_image)) {
219 LOG(ERROR) << "Failed to extract code from compilation unit " << cunit_idx_;
220 DeleteResources();
221 return false;
222 }
223
224 DeleteResources();
225 return true;
Logan Chien110bcba2012-04-16 19:11:28 +0800226}
227
Logan Chien971bf3f2012-05-01 15:47:55 +0800228
229bool CompilationUnit::MaterializeToString(std::string& str_buffer) {
230 llvm::raw_string_ostream str_os(str_buffer);
231 return MaterializeToRawOStream(str_os);
232}
233
234
235bool CompilationUnit::MaterializeToRawOStream(llvm::raw_ostream& out_stream) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700236 // Lookup the LLVM target
Logan Chien12584172012-07-10 04:07:28 -0700237 const char* target_triple = NULL;
238 const char* target_cpu = "";
239 const char* target_attr = NULL;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700240
Logan Chien971bf3f2012-05-01 15:47:55 +0800241 InstructionSet insn_set = GetInstructionSet();
242 switch (insn_set) {
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700243 case kThumb2:
244 target_triple = "thumb-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700245 target_cpu = "cortex-a9";
TDYa127b08ed122012-06-05 23:51:19 -0700246 target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700247 break;
248
249 case kArm:
250 target_triple = "armv7-none-linux-gnueabi";
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700251 // TODO: Fix for Nexus S.
252 target_cpu = "cortex-a9";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700253 // TODO: Fix for Xoom.
TDYa127b08ed122012-06-05 23:51:19 -0700254 target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700255 break;
256
257 case kX86:
258 target_triple = "i386-pc-linux-gnu";
259 target_attr = "";
260 break;
261
262 case kMips:
263 target_triple = "mipsel-unknown-linux";
264 target_attr = "mips32r2";
265 break;
266
267 default:
Logan Chien971bf3f2012-05-01 15:47:55 +0800268 LOG(FATAL) << "Unknown instruction set: " << insn_set;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700269 }
270
271 std::string errmsg;
Logan Chien12584172012-07-10 04:07:28 -0700272 const llvm::Target* target =
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700273 llvm::TargetRegistry::lookupTarget(target_triple, errmsg);
274
275 CHECK(target != NULL) << errmsg;
276
277 // Target options
278 llvm::TargetOptions target_options;
279 target_options.FloatABIType = llvm::FloatABI::Soft;
280 target_options.NoFramePointerElim = true;
281 target_options.NoFramePointerElimNonLeaf = true;
282 target_options.UseSoftFloat = false;
TDYa1273978da52012-05-19 07:45:39 -0700283 target_options.EnableFastISel = false;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700284
285 // Create the llvm::TargetMachine
Logan Chienb6bed0b2012-05-04 15:03:56 +0800286 llvm::OwningPtr<llvm::TargetMachine> target_machine(
Shih-wei Liao53519bf2012-06-17 03:45:00 -0700287 target->createTargetMachine(target_triple, target_cpu, target_attr, target_options,
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700288 llvm::Reloc::Static, llvm::CodeModel::Small,
Shih-wei Liaodac5eb22012-06-03 14:06:04 -0700289 llvm::CodeGenOpt::Aggressive));
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700290
Logan Chienb6bed0b2012-05-04 15:03:56 +0800291 CHECK(target_machine.get() != NULL) << "Failed to create target machine";
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700292
293 // Add target data
Logan Chien12584172012-07-10 04:07:28 -0700294 const llvm::TargetData* target_data = target_machine->getTargetData();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700295
296 // PassManager for code generation passes
297 llvm::PassManager pm;
298 pm.add(new llvm::TargetData(*target_data));
299
300 // FunctionPassManager for optimization pass
Logan Chien799ef4f2012-04-23 00:17:47 +0800301 llvm::FunctionPassManager fpm(module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700302 fpm.add(new llvm::TargetData(*target_data));
303
TDYa127f15b0ab2012-05-11 21:01:36 -0700304 if (bitcode_filename_.empty()) {
305 // If we don't need write the bitcode to file, add the AddSuspendCheckToLoopLatchPass to the
306 // regular FunctionPass.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700307#ifdef ART_USE_DEXLANG_FRONTEND
308 fpm.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get()));
309#endif
TDYa127f15b0ab2012-05-11 21:01:36 -0700310 fpm.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
311 } else {
312 // Run AddSuspendCheckToLoopLatchPass before we write the bitcode to file.
313 llvm::FunctionPassManager fpm2(module_);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700314#ifdef ART_USE_DEXLANG_FRONTEND
315 fpm2.add(CreateGBCExpanderPass(dex_lang_ctx_->GetIntrinsicHelper(), *irb_.get()));
316#endif
TDYa127f15b0ab2012-05-11 21:01:36 -0700317 fpm2.add(new ::AddSuspendCheckToLoopLatchPass(irb_.get()));
318 fpm2.doInitialization();
319 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
320 F != E; ++F) {
321 fpm2.run(*F);
322 }
323 fpm2.doFinalization();
324
325
326 // Write bitcode to file
327 std::string errmsg;
328
329 llvm::OwningPtr<llvm::tool_output_file> out_file(
330 new llvm::tool_output_file(bitcode_filename_.c_str(), errmsg,
331 llvm::raw_fd_ostream::F_Binary));
332
333
334 if (!errmsg.empty()) {
335 LOG(ERROR) << "Failed to create bitcode output file: " << errmsg;
336 return false;
337 }
338
339 llvm::WriteBitcodeToFile(module_, out_file->os());
340 out_file->keep();
341 }
342
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700343 // Add optimization pass
344 llvm::PassManagerBuilder pm_builder;
Shih-wei Liaoe0e40242012-05-08 01:04:03 -0700345 //pm_builder.Inliner = llvm::createFunctionInliningPass();
Shih-wei Liaoba67d7d2012-06-23 18:48:04 -0700346 pm_builder.Inliner = llvm::createAlwaysInlinerPass();
Shih-wei Liao415576b2012-04-23 15:28:53 -0700347 //pm_builder.Inliner = llvm::createPartialInliningPass();
348 pm_builder.OptLevel = 3;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700349 pm_builder.DisableSimplifyLibCalls = 1;
TDYa127e4c2ccc2012-05-13 21:10:36 -0700350 pm_builder.DisableUnitAtATime = 1;
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700351 pm_builder.populateFunctionPassManager(fpm);
TDYa127ce9c3172012-05-15 06:09:27 -0700352 pm_builder.populateModulePassManager(pm);
353 pm.add(llvm::createStripDeadPrototypesPass());
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700354
355 // Add passes to emit ELF image
356 {
Logan Chien08e1ba32012-05-08 15:08:51 +0800357 llvm::formatted_raw_ostream formatted_os(out_stream, false);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700358
359 // Ask the target to add backend passes as necessary.
360 if (target_machine->addPassesToEmitFile(pm,
361 formatted_os,
362 llvm::TargetMachine::CGFT_ObjectFile,
363 true)) {
364 LOG(FATAL) << "Unable to generate ELF for this target";
365 return false;
366 }
367
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700368 // Run the per-function optimization
369 fpm.doInitialization();
Logan Chien799ef4f2012-04-23 00:17:47 +0800370 for (llvm::Module::iterator F = module_->begin(), E = module_->end();
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700371 F != E; ++F) {
372 fpm.run(*F);
373 }
374 fpm.doFinalization();
375
376 // Run the code generation passes
Logan Chien799ef4f2012-04-23 00:17:47 +0800377 pm.run(*module_);
Shih-wei Liaod7726e42012-04-20 15:23:36 -0700378 }
379
380 return true;
381}
Logan Chien110bcba2012-04-16 19:11:28 +0800382
Logan Chien971bf3f2012-05-01 15:47:55 +0800383
384bool CompilationUnit::ExtractCodeAndPrelink(const std::string& elf_image) {
Shih-wei Liao02a1e352012-06-30 00:42:07 -0700385 if (GetInstructionSet() == kX86) {
386 compiled_code_.push_back(0xccU);
387 compiled_code_.push_back(0xccU);
388 compiled_code_.push_back(0xccU);
389 compiled_code_.push_back(0xccU);
390 return true;
391 }
392
Logan Chien971bf3f2012-05-01 15:47:55 +0800393 llvm::OwningPtr<llvm::MemoryBuffer> elf_image_buff(
394 llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(elf_image.data(),
395 elf_image.size())));
396
397 llvm::OwningPtr<llvm::object::ObjectFile> elf_file(
398 llvm::object::ObjectFile::createELFObjectFile(elf_image_buff.take()));
399
400 llvm::error_code ec;
401
402 const ProcedureLinkageTable& plt = compiler_llvm_->GetProcedureLinkageTable();
403
404 for (llvm::object::section_iterator
405 sec_iter = elf_file->begin_sections(),
406 sec_end = elf_file->end_sections();
407 sec_iter != sec_end; sec_iter.increment(ec)) {
408
409 CHECK(ec == 0) << "Failed to read section because " << ec.message();
410
411 // Read the section information
412 llvm::StringRef name;
413 uint64_t alignment = 0u;
414 uint64_t size = 0u;
415
416 CHECK(sec_iter->getName(name) == 0);
417 CHECK(sec_iter->getSize(size) == 0);
418 CHECK(sec_iter->getAlignment(alignment) == 0);
419
420 if (name == ".data" || name == ".bss" || name == ".rodata") {
421 if (size > 0) {
422 LOG(FATAL) << "Compilation unit " << cunit_idx_ << " has non-empty "
423 << name.str() << " section";
424 }
425
426 } else if (name == "" || name == ".rel.text" ||
427 name == ".ARM.attributes" || name == ".symtab" ||
428 name == ".strtab" || name == ".shstrtab") {
429 // We can ignore these sections. We don't have to copy them into
430 // the result Oat file.
431
432 } else if (name == ".text") {
433 // Ensure the alignment requirement is less than or equal to
434 // kArchAlignment
435 CheckCodeAlign(alignment);
436
437 // Copy the compiled code
438 llvm::StringRef contents;
439 CHECK(sec_iter->getContents(contents) == 0);
440
441 copy(contents.data(),
442 contents.data() + contents.size(),
443 back_inserter(compiled_code_));
444
445 // Prelink the compiled code
446 for (llvm::object::relocation_iterator
447 rel_iter = sec_iter->begin_relocations(),
448 rel_end = sec_iter->end_relocations(); rel_iter != rel_end;
449 rel_iter.increment(ec)) {
450
451 CHECK(ec == 0) << "Failed to read relocation because " << ec.message();
452
453 // Read the relocation information
454 llvm::object::SymbolRef sym_ref;
455 uint64_t rel_offset = 0;
456 uint64_t rel_type = 0;
457 int64_t rel_addend = 0;
458
459 CHECK(rel_iter->getSymbol(sym_ref) == 0);
460 CHECK(rel_iter->getOffset(rel_offset) == 0);
461 CHECK(rel_iter->getType(rel_type) == 0);
462 CHECK(rel_iter->getAdditionalInfo(rel_addend) == 0);
463
464 // Read the symbol related to this relocation fixup
465 llvm::StringRef sym_name;
466 CHECK(sym_ref.getName(sym_name) == 0);
467
468 // Relocate the fixup.
469 // TODO: Support more relocation type.
470 CHECK(rel_type == llvm::ELF::R_ARM_ABS32);
471 CHECK_LE(rel_offset + 4, compiled_code_.size());
472
473 uintptr_t dest_addr = plt.GetEntryAddress(sym_name.str().c_str());
474 uintptr_t final_addr = dest_addr + rel_addend;
475 compiled_code_[rel_offset] = final_addr & 0xff;
476 compiled_code_[rel_offset + 1] = (final_addr >> 8) & 0xff;
477 compiled_code_[rel_offset + 2] = (final_addr >> 16) & 0xff;
478 compiled_code_[rel_offset + 3] = (final_addr >> 24) & 0xff;
479 }
480
481 } else {
482 LOG(WARNING) << "Unexpected section: " << name.str();
483 }
484 }
485
486 return true;
487}
488
489
490// Check whether the align is less than or equal to the code alignment of
491// that architecture. Since the Oat writer only guarantee that the compiled
492// method being aligned to kArchAlignment, we have no way to align the ELf
493// section if the section alignment is greater than kArchAlignment.
494void CompilationUnit::CheckCodeAlign(uint32_t align) const {
495 InstructionSet insn_set = GetInstructionSet();
496 switch (insn_set) {
497 case kThumb2:
498 case kArm:
499 CHECK_LE(align, static_cast<uint32_t>(kArmAlignment));
500 break;
501
502 case kX86:
503 CHECK_LE(align, static_cast<uint32_t>(kX86Alignment));
504 break;
505
506 case kMips:
507 CHECK_LE(align, static_cast<uint32_t>(kMipsAlignment));
508 break;
509
510 default:
511 LOG(FATAL) << "Unknown instruction set: " << insn_set;
512 }
513}
514
515
Logan Chien8b977d32012-02-21 19:14:55 +0800516} // namespace compiler_llvm
517} // namespace art