blob: 80fc7fa836ac9862f17b0ee0c6f3b0e6d4e35f75 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
17#include "oat_file.h"
18
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019#include <dlfcn.h>
Calin Juravle4e1d5792014-07-15 23:56:47 +010020#include <string.h>
Andreas Gampedaab38c2014-09-12 18:38:24 -070021#include <unistd.h>
Brian Carlstrom700c8d32012-11-05 10:42:02 -080022
Andreas Gampe7848da42015-04-09 11:15:04 -070023#include <cstdlib>
David Srbecky1baabf02015-06-16 17:12:34 +000024#ifndef __APPLE__
25#include <link.h> // for dl_iterate_phdr.
26#endif
Richard Uhlere5fed032015-03-18 08:21:11 -070027#include <sstream>
28
Andreas Gampefa8429b2015-04-07 18:34:42 -070029// dlopen_ext support from bionic.
Andreas Gampec60e1b72015-07-30 08:57:50 -070030#ifdef __ANDROID__
Andreas Gampefa8429b2015-04-07 18:34:42 -070031#include "android/dlext.h"
32#endif
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Igor Murashkina315f5c2015-07-31 17:35:52 -070035#include "base/out.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070036#include "base/bit_vector.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080037#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080038#include "base/unix_file/fd_file.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080039#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070040#include "elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041#include "oat.h"
David Srbecky1baabf02015-06-16 17:12:34 +000042#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070044#include "mirror/object-inl.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010045#include "oat_file-inl.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070046#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070047#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utils.h"
Ian Rogers1809a722013-08-09 22:05:32 -070049#include "vmap_table.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070050
51namespace art {
52
Andreas Gampefa8429b2015-04-07 18:34:42 -070053// Whether OatFile::Open will try DlOpen() first. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000054static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070055
56// Whether OatFile::Open will try DlOpen() on the host. On the host we're not linking against
57// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
58// multiple times). However, if/when we switch the above, we likely want to switch this, too,
59// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000060static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070061
62// For debugging, Open will print DlOpen error message if set to true.
63static constexpr bool kPrintDlOpenErrorMessage = false;
64
Richard Uhlere5fed032015-03-18 08:21:11 -070065std::string OatFile::ResolveRelativeEncodedDexLocation(
66 const char* abs_dex_location, const std::string& rel_dex_location) {
67 if (abs_dex_location != nullptr && rel_dex_location[0] != '/') {
68 // Strip :classes<N>.dex used for secondary multidex files.
69 std::string base = DexFile::GetBaseLocation(rel_dex_location);
70 std::string multidex_suffix = DexFile::GetMultiDexSuffix(rel_dex_location);
71
72 // Check if the base is a suffix of the provided abs_dex_location.
73 std::string target_suffix = "/" + base;
74 std::string abs_location(abs_dex_location);
75 if (abs_location.size() > target_suffix.size()) {
76 size_t pos = abs_location.size() - target_suffix.size();
77 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
78 return abs_location + multidex_suffix;
79 }
80 }
81 }
82 return rel_dex_location;
83}
84
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085void OatFile::CheckLocation(const std::string& location) {
Brian Carlstrom7a967b32012-03-28 15:23:10 -070086 CHECK(!location.empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080087}
88
Alex Light84d76052014-08-22 17:49:35 -070089OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
90 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -070091 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -070092 out<std::string> error_msg) {
Alex Light84d76052014-08-22 17:49:35 -070093 std::unique_ptr<OatFile> oat_file(new OatFile(location, false));
94 oat_file->elf_file_.reset(elf_file);
Tong Shen62d1ca32014-09-03 17:24:56 -070095 uint64_t offset, size;
Igor Murashkina315f5c2015-07-31 17:35:52 -070096 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", outof(offset), outof(size));
Tong Shen62d1ca32014-09-03 17:24:56 -070097 CHECK(has_section);
98 oat_file->begin_ = elf_file->Begin() + offset;
99 oat_file->end_ = elf_file->Begin() + size + offset;
Vladimir Marko5c42c292015-02-25 12:02:49 +0000100 // Ignore the optional .bss section when opening non-executable.
Igor Murashkina315f5c2015-07-31 17:35:52 -0700101 return oat_file->Setup(abs_dex_location, outof_forward(error_msg)) ? oat_file.release() : nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800102}
103
104OatFile* OatFile::Open(const std::string& filename,
105 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700106 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700107 uint8_t* oat_file_begin,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700109 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700110 out<std::string> error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800111 CHECK(!filename.empty()) << location;
Andreas Gampe7ba64962014-10-23 11:37:40 -0700112 CheckLocation(location);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700113 std::unique_ptr<OatFile> ret;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700114
115 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
116 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
117 // !executable is a sign that we may want to patch), which may not be allowed for
118 // various reasons.
119 if (kUseDlopen && (kIsTargetBuild || kUseDlopenOnHost) && executable) {
120 // Try to use dlopen. This may fail for various reasons, outlined below. We try dlopen, as
121 // this will register the oat file with the linker and allows libunwind to find our info.
122 ret.reset(OpenDlopen(filename, location, requested_base, abs_dex_location, error_msg));
123 if (ret.get() != nullptr) {
124 return ret.release();
125 }
126 if (kPrintDlOpenErrorMessage) {
127 LOG(ERROR) << "Failed to dlopen: " << *error_msg;
128 }
129 }
130
Elliott Hughes956af0f2014-12-11 14:34:28 -0800131 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
132 //
133 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
134 //
135 // We use our own ELF loader for Quick to deal with legacy apps that
136 // open a generated dex file by name, remove the file, then open
137 // another generated dex file with the same name. http://b/10614658
138 //
139 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700140 //
141 //
142 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
143 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800144 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700145 if (file == nullptr) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800146 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
147 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800148 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800149 ret.reset(OpenElfFile(file.get(), location, requested_base, oat_file_begin, false, executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700150 abs_dex_location, error_msg));
Elliott Hughes956af0f2014-12-11 14:34:28 -0800151
152 // It would be nice to unlink here. But we might have opened the file created by the
153 // ScopedLock, which we better not delete to avoid races. TODO: Investigate how to fix the API
154 // to allow removal when we know the ELF must be borked.
Dave Allison69dfe512014-07-11 17:11:58 +0000155 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800156}
157
Igor Murashkina315f5c2015-07-31 17:35:52 -0700158OatFile* OatFile::OpenWritable(File* file,
159 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700160 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700161 out<std::string> error_msg) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800162 CheckLocation(location);
Igor Murashkina315f5c2015-07-31 17:35:52 -0700163 return OpenElfFile(file, location, nullptr, nullptr, true, false, abs_dex_location,
164 outof_forward(error_msg));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800165}
166
Igor Murashkina315f5c2015-07-31 17:35:52 -0700167OatFile* OatFile::OpenReadable(File* file,
168 const std::string& location,
Richard Uhlere5fed032015-03-18 08:21:11 -0700169 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700170 out<std::string> error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700171 CheckLocation(location);
Igor Murashkina315f5c2015-07-31 17:35:52 -0700172 return OpenElfFile(file, location, nullptr, nullptr, false, false, abs_dex_location,
173 outof_forward(error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700174}
175
Andreas Gampefa8429b2015-04-07 18:34:42 -0700176OatFile* OatFile::OpenDlopen(const std::string& elf_filename,
177 const std::string& location,
178 uint8_t* requested_base,
179 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700180 out<std::string> error_msg) {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700181 std::unique_ptr<OatFile> oat_file(new OatFile(location, true));
Igor Murashkina315f5c2015-07-31 17:35:52 -0700182 bool success = oat_file->Dlopen(elf_filename,
183 requested_base,
184 abs_dex_location,
185 outof_forward(error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700186 if (!success) {
187 return nullptr;
188 }
189 return oat_file.release();
190}
191
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800192OatFile* OatFile::OpenElfFile(File* file,
193 const std::string& location,
Ian Rogers13735952014-10-08 12:43:28 -0700194 uint8_t* requested_base,
Igor Murashkin46774762014-10-22 11:37:02 -0700195 uint8_t* oat_file_begin,
Brian Carlstromf1d34552013-07-12 20:22:23 -0700196 bool writable,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700197 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700198 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700199 out<std::string> error_msg) {
Alex Light9dcc4572014-08-14 14:16:26 -0700200 std::unique_ptr<OatFile> oat_file(new OatFile(location, executable));
Igor Murashkin46774762014-10-22 11:37:02 -0700201 bool success = oat_file->ElfFileOpen(file, requested_base, oat_file_begin, writable, executable,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700202 abs_dex_location, outof_forward(error_msg));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700203 if (!success) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700204 CHECK(!error_msg->empty());
205 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700206 }
207 return oat_file.release();
208}
209
Alex Light9dcc4572014-08-14 14:16:26 -0700210OatFile::OatFile(const std::string& location, bool is_executable)
Igor Murashkina315f5c2015-07-31 17:35:52 -0700211 : location_(location),
212 begin_(nullptr),
213 end_(nullptr),
214 bss_begin_(nullptr),
215 bss_end_(nullptr),
216 is_executable_(is_executable),
217 dlopen_handle_(nullptr),
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100218 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800219 CHECK(!location_.empty());
220}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700221
222OatFile::~OatFile() {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100223 STLDeleteElements(&oat_dex_files_storage_);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700224 if (dlopen_handle_ != nullptr) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800225 dlclose(dlopen_handle_);
226 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700227}
228
Igor Murashkina315f5c2015-07-31 17:35:52 -0700229bool OatFile::Dlopen(const std::string& elf_filename,
230 uint8_t* requested_base,
231 const char* abs_dex_location,
232 out<std::string> error_msg) {
David Srbecky1baabf02015-06-16 17:12:34 +0000233#ifdef __APPLE__
234 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
235 // but let's fallback to the custom loading code for the time being.
236 UNUSED(elf_filename);
237 UNUSED(requested_base);
238 UNUSED(abs_dex_location);
239 UNUSED(error_msg);
240 return false;
241#else
Andreas Gampefa8429b2015-04-07 18:34:42 -0700242 std::unique_ptr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
243 if (absolute_path == nullptr) {
244 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
245 return false;
246 }
Andreas Gampec60e1b72015-07-30 08:57:50 -0700247#ifdef __ANDROID__
Andreas Gampefa8429b2015-04-07 18:34:42 -0700248 android_dlextinfo extinfo;
David Srbecky1baabf02015-06-16 17:12:34 +0000249 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | ANDROID_DLEXT_FORCE_FIXED_VADDR;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700250 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
251#else
252 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
253#endif
254 if (dlopen_handle_ == nullptr) {
255 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
256 return false;
257 }
258 begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatdata"));
259 if (begin_ == nullptr) {
260 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s': %s", elf_filename.c_str(),
261 dlerror());
262 return false;
263 }
264 if (requested_base != nullptr && begin_ != requested_base) {
265 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
266 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
267 "oatdata=%p != expected=%p, %s. See process maps in the log.",
268 begin_, requested_base, elf_filename.c_str());
269 return false;
270 }
271 end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword"));
272 if (end_ == nullptr) {
273 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s': %s", elf_filename.c_str(),
274 dlerror());
275 return false;
276 }
277 // Readjust to be non-inclusive upper bound.
278 end_ += sizeof(uint32_t);
279
280 bss_begin_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbss"));
281 if (bss_begin_ == nullptr) {
282 // No .bss section. Clear dlerror().
283 bss_end_ = nullptr;
284 dlerror();
285 } else {
286 bss_end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatbsslastword"));
287 if (bss_end_ == nullptr) {
288 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
289 elf_filename.c_str());
290 return false;
291 }
292 // Readjust to be non-inclusive upper bound.
293 bss_end_ += sizeof(uint32_t);
294 }
295
David Srbecky1baabf02015-06-16 17:12:34 +0000296 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
297 struct dl_iterate_context {
298 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
299 auto* context = reinterpret_cast<dl_iterate_context*>(data);
David Srbeckyaa038702015-06-17 02:11:07 +0100300 // See whether this callback corresponds to the file which we have just loaded.
301 bool contains_begin = false;
302 for (int i = 0; i < info->dlpi_phnum; i++) {
303 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
304 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
305 info->dlpi_phdr[i].p_vaddr);
306 size_t memsz = info->dlpi_phdr[i].p_memsz;
307 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
308 contains_begin = true;
309 break;
David Srbecky1baabf02015-06-16 17:12:34 +0000310 }
311 }
312 }
David Srbeckyaa038702015-06-17 02:11:07 +0100313 // Add dummy mmaps for this file.
314 if (contains_begin) {
315 for (int i = 0; i < info->dlpi_phnum; i++) {
316 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
317 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
318 info->dlpi_phdr[i].p_vaddr);
319 size_t memsz = info->dlpi_phdr[i].p_memsz;
David Srbecky82e73dc2015-06-17 18:36:23 +0100320 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
321 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
David Srbeckyaa038702015-06-17 02:11:07 +0100322 }
323 }
324 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
325 }
326 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
David Srbecky1baabf02015-06-16 17:12:34 +0000327 }
David Srbecky82e73dc2015-06-17 18:36:23 +0100328 const uint8_t* const begin_;
329 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
330 } context = { begin_, &dlopen_mmaps_ };
David Srbecky1baabf02015-06-16 17:12:34 +0000331
David Srbeckyaa038702015-06-17 02:11:07 +0100332 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
David Srbecky5dedb802015-06-17 00:08:02 +0100333 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
334 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but can not find its mmaps.";
335 }
336
Igor Murashkina315f5c2015-07-31 17:35:52 -0700337 return Setup(abs_dex_location, outof_forward(error_msg));
David Srbecky1baabf02015-06-16 17:12:34 +0000338#endif // __APPLE__
Andreas Gampefa8429b2015-04-07 18:34:42 -0700339}
340
Igor Murashkina315f5c2015-07-31 17:35:52 -0700341bool OatFile::ElfFileOpen(File* file,
342 uint8_t* requested_base,
343 uint8_t* oat_file_begin,
344 bool writable,
345 bool executable,
Richard Uhlere5fed032015-03-18 08:21:11 -0700346 const char* abs_dex_location,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700347 out<std::string> error_msg) {
Igor Murashkin46774762014-10-22 11:37:02 -0700348 // TODO: rename requested_base to oat_data_begin
Igor Murashkina315f5c2015-07-31 17:35:52 -0700349 elf_file_.reset(ElfFile::Open(file,
350 writable,
351 /*program_header_only*/true,
352 outof_forward(error_msg),
Igor Murashkin46774762014-10-22 11:37:02 -0700353 oat_file_begin));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700354 if (elf_file_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700355 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800356 return false;
357 }
Igor Murashkina315f5c2015-07-31 17:35:52 -0700358 bool loaded = elf_file_->Load(executable, outof_forward(error_msg));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800359 if (!loaded) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700360 DCHECK(!error_msg->empty());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800361 return false;
362 }
363 begin_ = elf_file_->FindDynamicSymbolAddress("oatdata");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700364 if (begin_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700365 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800366 return false;
367 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700368 if (requested_base != nullptr && begin_ != requested_base) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800369 PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700370 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800371 "oatdata=%p != expected=%p. See process maps in the log.",
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700372 begin_, requested_base);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800373 return false;
374 }
375 end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700376 if (end_ == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700377 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s'", file->GetPath().c_str());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800378 return false;
379 }
380 // Readjust to be non-inclusive upper bound.
381 end_ += sizeof(uint32_t);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000382
383 bss_begin_ = elf_file_->FindDynamicSymbolAddress("oatbss");
384 if (bss_begin_ == nullptr) {
385 // No .bss section. Clear dlerror().
386 bss_end_ = nullptr;
387 dlerror();
388 } else {
389 bss_end_ = elf_file_->FindDynamicSymbolAddress("oatbsslastword");
390 if (bss_end_ == nullptr) {
391 *error_msg = StringPrintf("Failed to find oatbasslastword symbol in '%s'",
392 file->GetPath().c_str());
393 return false;
394 }
395 // Readjust to be non-inclusive upper bound.
396 bss_end_ += sizeof(uint32_t);
397 }
398
Igor Murashkina315f5c2015-07-31 17:35:52 -0700399 return Setup(abs_dex_location, outof_forward(error_msg));
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800400}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800401
Igor Murashkina315f5c2015-07-31 17:35:52 -0700402bool OatFile::Setup(const char* abs_dex_location, out<std::string> error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700403 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800404 std::string cause = GetOatHeader().GetValidationErrorMessage();
405 *error_msg = StringPrintf("Invalid oat header for '%s': %s", GetLocation().c_str(),
406 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700407 return false;
408 }
Ian Rogers13735952014-10-08 12:43:28 -0700409 const uint8_t* oat = Begin();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700410 oat += sizeof(OatHeader);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700411 if (oat > End()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700412 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader", GetLocation().c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700413 return false;
414 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700415
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700416 oat += GetOatHeader().GetKeyValueStoreSize();
Brian Carlstromfb331d72013-07-25 22:00:16 -0700417 if (oat > End()) {
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700418 *error_msg = StringPrintf("In oat file '%s' found truncated variable-size data: "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700419 "%p + %zd + %ud <= %p", GetLocation().c_str(),
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700420 Begin(), sizeof(OatHeader), GetOatHeader().GetKeyValueStoreSize(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700421 End());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700422 return false;
423 }
424
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100425 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
426 oat_dex_files_storage_.reserve(dex_file_count);
427 for (size_t i = 0; i < dex_file_count; i++) {
Dmitry Petrochenko83bef922014-02-03 12:34:59 +0700428 uint32_t dex_file_location_size = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700429 if (UNLIKELY(dex_file_location_size == 0U)) {
430 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with empty location name",
431 GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700432 return false;
433 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700434 oat += sizeof(dex_file_location_size);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700435 if (UNLIKELY(oat > End())) {
436 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd truncated after dex file "
437 "location size", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700438 return false;
439 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700440
441 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
442 oat += dex_file_location_size;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700443 if (UNLIKELY(oat > End())) {
444 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd with truncated dex file "
445 "location", GetLocation().c_str(), i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700446 return false;
447 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700448
Richard Uhlere5fed032015-03-18 08:21:11 -0700449 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
450 abs_dex_location,
451 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700452
453 uint32_t dex_file_checksum = *reinterpret_cast<const uint32_t*>(oat);
454 oat += sizeof(dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700455 if (UNLIKELY(oat > End())) {
456 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated after "
457 "dex file checksum", GetLocation().c_str(), i,
458 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700459 return false;
460 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461
Brian Carlstrom89521892011-12-07 22:05:07 -0800462 uint32_t dex_file_offset = *reinterpret_cast<const uint32_t*>(oat);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700463 if (UNLIKELY(dex_file_offset == 0U)) {
464 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with zero dex "
465 "file offset", GetLocation().c_str(), i, dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700466 return false;
467 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700468 if (UNLIKELY(dex_file_offset > Size())) {
469 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with dex file "
470 "offset %ud > %zd", GetLocation().c_str(), i,
471 dex_file_location.c_str(), dex_file_offset, Size());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700472 return false;
473 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800474 oat += sizeof(dex_file_offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700475 if (UNLIKELY(oat > End())) {
476 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700477 "after dex file offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700478 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700479 return false;
480 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800481
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800482 const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700483 if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
484 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700485 "dex file magic '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700486 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700487 return false;
488 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700489 if (UNLIKELY(!DexFile::IsVersionValid(dex_file_pointer))) {
490 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with invalid "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700491 "dex file version '%s'", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700492 dex_file_location.c_str(), dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700493 return false;
494 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800495 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
496 const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
Brian Carlstrom89521892011-12-07 22:05:07 -0800497
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800498 oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700499 if (UNLIKELY(oat > End())) {
500 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
Brian Carlstrom37628b72014-10-28 13:54:26 -0700501 "method offsets", GetLocation().c_str(), i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700502 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700503 return false;
504 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700505
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100506 std::string canonical_location = DexFile::GetDexCanonicalLocation(dex_file_location.c_str());
507
508 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100509 OatDexFile* oat_dex_file = new OatDexFile(this,
510 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100511 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100512 dex_file_checksum,
513 dex_file_pointer,
514 methods_offsets_pointer);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100515 oat_dex_files_storage_.push_back(oat_dex_file);
516
517 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100518 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100519 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100520 if (canonical_location != dex_file_location) {
521 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
522 oat_dex_files_.Put(canonical_key, oat_dex_file);
523 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700524 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700525 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700526}
527
528const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800529 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700530}
531
Ian Rogers13735952014-10-08 12:43:28 -0700532const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700533 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800534 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700535}
536
Ian Rogers13735952014-10-08 12:43:28 -0700537const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700538 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800539 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700540}
541
Vladimir Marko5c42c292015-02-25 12:02:49 +0000542const uint8_t* OatFile::BssBegin() const {
543 return bss_begin_;
544}
545
546const uint8_t* OatFile::BssEnd() const {
547 return bss_end_;
548}
549
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700550const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
551 const uint32_t* dex_location_checksum,
Ian Rogers7fe2c692011-12-06 16:35:59 -0800552 bool warn_if_not_found) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100553 // NOTE: We assume here that the canonical location for a given dex_location never
554 // changes. If it does (i.e. some symlink used by the filename changes) we may return
555 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
556 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +0100557
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100558 // TODO: Additional analysis of usage patterns to see if this can be simplified
559 // without any performance loss, for example by not doing the first lock-free lookup.
560
561 const OatFile::OatDexFile* oat_dex_file = nullptr;
562 StringPiece key(dex_location);
563 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
564 // directly mentioned in the oat file and doesn't require locking.
565 auto primary_it = oat_dex_files_.find(key);
566 if (primary_it != oat_dex_files_.end()) {
567 oat_dex_file = primary_it->second;
568 DCHECK(oat_dex_file != nullptr);
569 } else {
570 // This dex_location is not one of the dex locations directly mentioned in the
571 // oat file. The correct lookup is via the canonical location but first see in
572 // the secondary_oat_dex_files_ whether we've looked up this location before.
573 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
574 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
575 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700576 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100577 } else {
578 // We haven't seen this dex_location before, we must check the canonical location.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100579 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100580 if (dex_canonical_location != dex_location) {
581 StringPiece canonical_key(dex_canonical_location);
582 auto canonical_it = oat_dex_files_.find(canonical_key);
583 if (canonical_it != oat_dex_files_.end()) {
584 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700585 } // else keep null.
586 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100587
588 // Copy the key to the string_cache_ and store the result in secondary map.
589 string_cache_.emplace_back(key.data(), key.length());
590 StringPiece key_copy(string_cache_.back());
591 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700592 }
593 }
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100594 if (oat_dex_file != nullptr &&
595 (dex_location_checksum == nullptr ||
596 oat_dex_file->GetDexFileLocationChecksum() == *dex_location_checksum)) {
597 return oat_dex_file;
598 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700599
600 if (warn_if_not_found) {
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100601 std::string dex_canonical_location = DexFile::GetDexCanonicalLocation(dex_location);
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800602 std::string checksum("<unspecified>");
Andreas Gampefa8429b2015-04-07 18:34:42 -0700603 if (dex_location_checksum != nullptr) {
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800604 checksum = StringPrintf("0x%08x", *dex_location_checksum);
605 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700606 LOG(WARNING) << "Failed to find OatDexFile for DexFile " << dex_location
Calin Juravle4e1d5792014-07-15 23:56:47 +0100607 << " ( canonical path " << dex_canonical_location << ")"
Brian Carlstrom0d6adac2014-02-05 17:39:16 -0800608 << " with checksum " << checksum << " in OatFile " << GetLocation();
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700609 if (kIsDebugBuild) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100610 for (const OatDexFile* odf : oat_dex_files_storage_) {
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700611 LOG(WARNING) << "OatFile " << GetLocation()
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100612 << " contains OatDexFile " << odf->GetDexFileLocation()
613 << " (canonical path " << odf->GetCanonicalDexFileLocation() << ")"
614 << " with checksum 0x" << std::hex << odf->GetDexFileLocationChecksum();
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700615 }
Ian Rogers7fe2c692011-12-06 16:35:59 -0800616 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700617 }
Calin Juravle4e1d5792014-07-15 23:56:47 +0100618
Andreas Gampefa8429b2015-04-07 18:34:42 -0700619 return nullptr;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700620}
621
Brian Carlstrome24fa612011-09-29 00:53:55 -0700622OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800623 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100624 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800625 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -0700626 const uint8_t* dex_file_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800627 const uint32_t* oat_class_offsets_pointer)
Brian Carlstrome24fa612011-09-29 00:53:55 -0700628 : oat_file_(oat_file),
629 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100630 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800631 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -0800632 dex_file_pointer_(dex_file_pointer),
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800633 oat_class_offsets_pointer_(oat_class_offsets_pointer) {}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700634
635OatFile::OatDexFile::~OatDexFile() {}
636
Ian Rogers05f28c62012-10-23 18:12:13 -0700637size_t OatFile::OatDexFile::FileSize() const {
638 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
639}
640
Igor Murashkina315f5c2015-07-31 17:35:52 -0700641std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(out<std::string> error_msg) const {
Ian Rogers05f28c62012-10-23 18:12:13 -0700642 return DexFile::Open(dex_file_pointer_, FileSize(), dex_file_location_,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700643 dex_file_location_checksum_, this, outof_forward(error_msg));
Brian Carlstrom89521892011-12-07 22:05:07 -0800644}
645
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700646uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
647 return oat_class_offsets_pointer_[class_def_index];
648}
649
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100650OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700651 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800652
Ian Rogers13735952014-10-08 12:43:28 -0700653 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700654 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800655
Ian Rogers13735952014-10-08 12:43:28 -0700656 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -0700657 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
658 mirror::Class::Status status =
659 static_cast<mirror::Class::Status>(*reinterpret_cast<const int16_t*>(status_pointer));
660 CHECK_LT(status, mirror::Class::kStatusMax);
661
Ian Rogers13735952014-10-08 12:43:28 -0700662 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -0700663 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
664 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
665 CHECK_LT(type, kOatClassMax);
666
Ian Rogers13735952014-10-08 12:43:28 -0700667 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800668 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -0700669
Brian Carlstromcd937a92014-03-04 22:53:23 -0800670 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700671 const uint8_t* bitmap_pointer = nullptr;
672 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -0700673 if (type != kOatClassNoneCompiled) {
674 if (type == kOatClassSomeCompiled) {
675 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
676 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
677 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
678 methods_pointer = bitmap_pointer + bitmap_size;
679 } else {
680 methods_pointer = after_type_pointer;
681 }
682 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -0800683 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800684
Richard Uhler07b3c232015-03-31 15:57:54 -0700685 return OatFile::OatClass(oat_file_,
686 status,
687 type,
688 bitmap_size,
689 reinterpret_cast<const uint32_t*>(bitmap_pointer),
690 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700691}
692
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800693OatFile::OatClass::OatClass(const OatFile* oat_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694 mirror::Class::Status status,
Brian Carlstromba150c32013-08-27 17:31:03 -0700695 OatClassType type,
696 uint32_t bitmap_size,
697 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800698 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -0700699 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100700 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700701 switch (type_) {
702 case kOatClassAllCompiled: {
703 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800704 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700705 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700706 break;
707 }
708 case kOatClassSomeCompiled: {
709 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800710 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700711 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700712 break;
713 }
714 case kOatClassNoneCompiled: {
715 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -0800716 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -0700717 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -0700718 break;
719 }
720 case kOatClassMax: {
721 LOG(FATAL) << "Invalid OatClassType " << type_;
722 break;
723 }
724 }
725}
Brian Carlstrome24fa612011-09-29 00:53:55 -0700726
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700727uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
728 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
729 if (oat_method_offsets == nullptr) {
730 return 0u;
731 }
732 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
733}
734
735const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100736 // NOTE: We don't keep the number of methods and cannot do a bounds check for method_index.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700738 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700739 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700740 }
741 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -0700743 CHECK_EQ(kOatClassAllCompiled, type_);
744 methods_pointer_index = method_index;
745 } else {
746 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100747 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700748 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700749 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100750 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
751 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -0700752 }
753 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 return &oat_method_offsets;
755}
756
757const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
758 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
759 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800760 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700761 }
762 if (oat_file_->IsExecutable() ||
763 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800764 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800765 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -0700766 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800767 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
768 // version.
769 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700770}
771
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -0700773 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800774 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -0800775}
776
Andreas Gampe7ba64962014-10-23 11:37:40 -0700777bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -0700778 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -0700779 // TODO: Check against oat_patches. b/18144996
780}
781
Sebastien Hertz0de11332015-05-13 12:14:05 +0200782bool OatFile::IsDebuggable() const {
783 return GetOatHeader().IsDebuggable();
784}
785
Andreas Gampe7848da42015-04-09 11:15:04 -0700786static constexpr char kDexClassPathEncodingSeparator = '*';
787
788std::string OatFile::EncodeDexFileDependencies(const std::vector<const DexFile*>& dex_files) {
789 std::ostringstream out;
790
791 for (const DexFile* dex_file : dex_files) {
792 out << dex_file->GetLocation().c_str();
793 out << kDexClassPathEncodingSeparator;
794 out << dex_file->GetLocationChecksum();
795 out << kDexClassPathEncodingSeparator;
796 }
797
798 return out.str();
799}
800
Igor Murashkina315f5c2015-07-31 17:35:52 -0700801bool OatFile::CheckStaticDexFileDependencies(const char* dex_dependencies, out<std::string> msg) {
Andreas Gampe7848da42015-04-09 11:15:04 -0700802 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
803 // No dependencies.
804 return true;
805 }
806
807 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
808 // Split() instead of manual parsing of the combined char*.
809 std::vector<std::string> split;
Igor Murashkina315f5c2015-07-31 17:35:52 -0700810 Split(dex_dependencies, kDexClassPathEncodingSeparator, outof(split));
Andreas Gampe7848da42015-04-09 11:15:04 -0700811 if (split.size() % 2 != 0) {
812 // Expected pairs of location and checksum.
813 *msg = StringPrintf("Odd number of elements in dependency list %s", dex_dependencies);
814 return false;
815 }
816
817 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
818 std::string& location = *it;
819 std::string& checksum = *(it + 1);
820 int64_t converted = strtoll(checksum.c_str(), nullptr, 10);
821 if (converted == 0) {
822 // Conversion error.
823 *msg = StringPrintf("Conversion error for %s", checksum.c_str());
824 return false;
825 }
826
827 uint32_t dex_checksum;
828 std::string error_msg;
829 if (DexFile::GetChecksum(DexFile::GetDexCanonicalLocation(location.c_str()).c_str(),
Igor Murashkina315f5c2015-07-31 17:35:52 -0700830 outof(dex_checksum),
831 outof(error_msg))) {
Andreas Gampe7848da42015-04-09 11:15:04 -0700832 if (converted != dex_checksum) {
833 *msg = StringPrintf("Checksums don't match for %s: %" PRId64 " vs %u",
834 location.c_str(), converted, dex_checksum);
835 return false;
836 }
837 } else {
838 // Problem retrieving checksum.
839 // TODO: odex files?
840 *msg = StringPrintf("Could not retrieve checksum for %s: %s", location.c_str(),
841 error_msg.c_str());
842 return false;
843 }
844 }
845
846 return true;
847}
848
849bool OatFile::GetDexLocationsFromDependencies(const char* dex_dependencies,
Igor Murashkina315f5c2015-07-31 17:35:52 -0700850 out<std::vector<std::string>> locations) {
Andreas Gampe7848da42015-04-09 11:15:04 -0700851 if (dex_dependencies == nullptr || dex_dependencies[0] == 0) {
852 return true;
853 }
854
855 // Assumption: this is not performance-critical. So it's OK to do this with a std::string and
856 // Split() instead of manual parsing of the combined char*.
857 std::vector<std::string> split;
Igor Murashkina315f5c2015-07-31 17:35:52 -0700858 Split(dex_dependencies, kDexClassPathEncodingSeparator, outof(split));
Andreas Gampe7848da42015-04-09 11:15:04 -0700859 if (split.size() % 2 != 0) {
860 // Expected pairs of location and checksum.
861 return false;
862 }
863
864 for (auto it = split.begin(), end = split.end(); it != end; it += 2) {
865 locations->push_back(*it);
866 }
867
868 return true;
869}
870
Brian Carlstrome24fa612011-09-29 00:53:55 -0700871} // namespace art