blob: 0852da5644c9266a6773358dd2e9258d615bbbaa [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>
David Srbecky1baabf02015-06-16 17:12:34 +000020#ifndef __APPLE__
21#include <link.h> // for dl_iterate_phdr.
22#endif
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <unistd.h>
24
25#include <cstdlib>
26#include <cstring>
Richard Uhlere5fed032015-03-18 08:21:11 -070027#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include <type_traits>
Shubham Ajmerab22dea02017-10-04 18:36:41 -070029#include <sys/stat.h>
Richard Uhlere5fed032015-03-18 08:21:11 -070030
Andreas Gampefa8429b2015-04-07 18:34:42 -070031// dlopen_ext support from bionic.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010032#ifdef ART_TARGET_ANDROID
Andreas Gampefa8429b2015-04-07 18:34:42 -070033#include "android/dlext.h"
34#endif
35
Andreas Gampe46ee31b2016-12-14 10:11:49 -080036#include "android-base/stringprintf.h"
37
Andreas Gampec6ea7d02017-02-01 16:46:28 -080038#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070039#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070040#include "base/enums.h"
David Sehr891a50e2017-10-27 17:01:07 -070041#include "base/file_utils.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080042#include "base/logging.h" // For VLOG_IS_ON.
Elliott Hughes1aa246d2012-12-13 09:29:36 -080043#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080044#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080045#include "base/unix_file/fd_file.h"
David Sehr013fd802018-01-11 22:55:24 -080046#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/dex_file_loader.h"
48#include "dex/dex_file_types.h"
49#include "dex/standard_dex_file.h"
David Sehr0225f8e2018-01-31 08:52:24 +000050#include "dex/utf-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080051#include "elf_file.h"
Alex Light84d76052014-08-22 17:49:35 -070052#include "elf_utils.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000053#include "gc_root.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010054#include "gc/space/image_space.h"
David Srbecky1baabf02015-06-16 17:12:34 +000055#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070057#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070058#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010059#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070060#include "oat_file_manager.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070061#include "os.h"
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070062#include "runtime.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000063#include "type_lookup_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064#include "utils.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010065#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070066
67namespace art {
68
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069using android::base::StringPrintf;
70
Andreas Gampe049cff02015-12-01 23:27:12 -080071// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000072static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070073
Andreas Gampe049cff02015-12-01 23:27:12 -080074// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070075// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
76// multiple times). However, if/when we switch the above, we likely want to switch this, too,
77// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000078static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070079
80// For debugging, Open will print DlOpen error message if set to true.
81static constexpr bool kPrintDlOpenErrorMessage = false;
82
Andreas Gampe049cff02015-12-01 23:27:12 -080083// Note for OatFileBase and descendents:
84//
85// These are used in OatFile::Open to try all our loaders.
86//
87// The process is simple:
88//
89// 1) Allocate an instance through the standard constructor (location, executable)
90// 2) Load() to try to open the file.
91// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
92// 4) PreSetup() for any steps that should be done before the final setup.
93// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -070094
Andreas Gampe049cff02015-12-01 23:27:12 -080095class OatFileBase : public OatFile {
96 public:
97 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -070098
Andreas Gampe049cff02015-12-01 23:27:12 -080099 template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100100 static OatFileBase* OpenOatFile(const std::string& vdex_filename,
101 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700102 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800103 uint8_t* requested_base,
104 uint8_t* oat_file_begin,
105 bool writable,
106 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800107 bool low_4gb,
Richard Uhlere5fed032015-03-18 08:21:11 -0700108 const char* abs_dex_location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800109 std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800110
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700111 template <typename kOatFileBaseSubType>
112 static OatFileBase* OpenOatFile(int vdex_fd,
113 int oat_fd,
114 const std::string& vdex_filename,
115 const std::string& oat_filename,
116 uint8_t* requested_base,
117 uint8_t* oat_file_begin,
118 bool writable,
119 bool executable,
120 bool low_4gb,
121 const char* abs_dex_location,
122 std::string* error_msg);
123
Andreas Gampe049cff02015-12-01 23:27:12 -0800124 protected:
125 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700126
Andreas Gampe049cff02015-12-01 23:27:12 -0800127 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
128 std::string* error_msg) const = 0;
129
Andreas Gampe4075f832016-05-18 13:09:54 -0700130 virtual void PreLoad() = 0;
131
David Brazdil7b49e6c2016-09-01 11:06:18 +0100132 bool LoadVdex(const std::string& vdex_filename,
133 bool writable,
134 bool low_4gb,
135 std::string* error_msg);
136
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700137 bool LoadVdex(int vdex_fd,
138 const std::string& vdex_filename,
139 bool writable,
140 bool low_4gb,
141 std::string* error_msg);
142
Andreas Gampe049cff02015-12-01 23:27:12 -0800143 virtual bool Load(const std::string& elf_filename,
144 uint8_t* oat_file_begin,
145 bool writable,
146 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800147 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800148 std::string* error_msg) = 0;
149
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700150 virtual bool Load(int oat_fd,
151 uint8_t* oat_file_begin,
152 bool writable,
153 bool executable,
154 bool low_4gb,
155 std::string* error_msg) = 0;
156
Andreas Gampe049cff02015-12-01 23:27:12 -0800157 bool ComputeFields(uint8_t* requested_base,
158 const std::string& file_path,
159 std::string* error_msg);
160
161 virtual void PreSetup(const std::string& elf_filename) = 0;
162
163 bool Setup(const char* abs_dex_location, std::string* error_msg);
164
165 // Setters exposed for ElfOatFile.
166
167 void SetBegin(const uint8_t* begin) {
168 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700169 }
170
Andreas Gampe049cff02015-12-01 23:27:12 -0800171 void SetEnd(const uint8_t* end) {
172 end_ = end;
173 }
174
David Brazdilc93b3be2016-09-12 18:49:58 +0100175 void SetVdex(VdexFile* vdex) {
176 vdex_.reset(vdex);
177 }
178
Andreas Gampe049cff02015-12-01 23:27:12 -0800179 private:
180 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
181};
182
183template <typename kOatFileBaseSubType>
David Brazdil7b49e6c2016-09-01 11:06:18 +0100184OatFileBase* OatFileBase::OpenOatFile(const std::string& vdex_filename,
185 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800186 const std::string& location,
187 uint8_t* requested_base,
188 uint8_t* oat_file_begin,
189 bool writable,
190 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800191 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800192 const char* abs_dex_location,
193 std::string* error_msg) {
194 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700195
196 ret->PreLoad();
197
Andreas Gampe049cff02015-12-01 23:27:12 -0800198 if (!ret->Load(elf_filename,
199 oat_file_begin,
200 writable,
201 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800202 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800203 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800204 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800205 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800206
Andreas Gampe049cff02015-12-01 23:27:12 -0800207 if (!ret->ComputeFields(requested_base, elf_filename, error_msg)) {
208 return nullptr;
209 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700210
David Srbeckyec2cdf42017-12-08 16:21:25 +0000211 if (!ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
212 return nullptr;
213 }
214
Andreas Gampe049cff02015-12-01 23:27:12 -0800215 ret->PreSetup(elf_filename);
216
217 if (!ret->Setup(abs_dex_location, error_msg)) {
218 return nullptr;
219 }
220
Dave Allison69dfe512014-07-11 17:11:58 +0000221 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800222}
223
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700224template <typename kOatFileBaseSubType>
225OatFileBase* OatFileBase::OpenOatFile(int vdex_fd,
226 int oat_fd,
227 const std::string& vdex_location,
228 const std::string& oat_location,
229 uint8_t* requested_base,
230 uint8_t* oat_file_begin,
231 bool writable,
232 bool executable,
233 bool low_4gb,
234 const char* abs_dex_location,
235 std::string* error_msg) {
236 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable));
237
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700238 if (!ret->Load(oat_fd,
239 oat_file_begin,
240 writable,
241 executable,
242 low_4gb,
243 error_msg)) {
244 return nullptr;
245 }
246
247 if (!ret->ComputeFields(requested_base, oat_location, error_msg)) {
248 return nullptr;
249 }
250
David Srbeckyec2cdf42017-12-08 16:21:25 +0000251 if (!ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) {
252 return nullptr;
253 }
254
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700255 ret->PreSetup(oat_location);
256
257 if (!ret->Setup(abs_dex_location, error_msg)) {
258 return nullptr;
259 }
260
261 return ret.release();
262}
263
David Brazdil7b49e6c2016-09-01 11:06:18 +0100264bool OatFileBase::LoadVdex(const std::string& vdex_filename,
265 bool writable,
266 bool low_4gb,
267 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000268 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
269 vdex_end_ - vdex_begin_,
Andreas Gampec1fc4492018-01-10 14:19:36 -0800270 vdex_begin_ != nullptr /* mmap_reuse */,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000271 vdex_filename,
272 writable,
273 low_4gb,
274 /* unquicken*/ false,
275 error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100276 if (vdex_.get() == nullptr) {
277 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
278 vdex_filename.c_str(),
279 error_msg->c_str());
280 return false;
281 }
282 return true;
283}
284
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700285bool OatFileBase::LoadVdex(int vdex_fd,
286 const std::string& vdex_filename,
287 bool writable,
288 bool low_4gb,
289 std::string* error_msg) {
290 if (vdex_fd != -1) {
291 struct stat s;
292 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s));
293 if (rc == -1) {
294 PLOG(WARNING) << "Failed getting length of vdex file";
295 } else {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000296 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
297 vdex_end_ - vdex_begin_,
Andreas Gampec1fc4492018-01-10 14:19:36 -0800298 vdex_begin_ != nullptr /* mmap_reuse */,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000299 vdex_fd,
300 s.st_size,
301 vdex_filename,
302 writable,
303 low_4gb,
304 false /* unquicken */,
305 error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700306 if (vdex_.get() == nullptr) {
307 *error_msg = "Failed opening vdex file.";
308 return false;
309 }
310 }
311 }
312 return true;
313}
314
Andreas Gampe049cff02015-12-01 23:27:12 -0800315bool OatFileBase::ComputeFields(uint8_t* requested_base,
316 const std::string& file_path,
317 std::string* error_msg) {
318 std::string symbol_error_msg;
319 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700320 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800321 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
322 file_path.c_str(),
323 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700324 return false;
325 }
326 if (requested_base != nullptr && begin_ != requested_base) {
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000327 // Host can fail this check. Do not dump there to avoid polluting the output.
Andreas Gampe7ec09042016-04-01 17:20:49 -0700328 if (kIsTargetBuild && (kIsDebugBuild || VLOG_IS_ON(oat))) {
Andreas Gampe170331f2017-12-07 18:41:03 -0800329 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::WARNING);
Nicolas Geoffrayf97cf2a2016-03-09 15:36:23 +0000330 }
Andreas Gampefa8429b2015-04-07 18:34:42 -0700331 *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
Andreas Gampe049cff02015-12-01 23:27:12 -0800332 "oatdata=%p != expected=%p. See process maps in the log.",
333 begin_, requested_base);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700334 return false;
335 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800336 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700337 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800338 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
339 file_path.c_str(),
340 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700341 return false;
342 }
343 // Readjust to be non-inclusive upper bound.
344 end_ += sizeof(uint32_t);
345
Andreas Gampe049cff02015-12-01 23:27:12 -0800346 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700347 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800348 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700349 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700350 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800351 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700352 if (bss_end_ == nullptr) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000353 *error_msg = StringPrintf("Failed to find oatbsslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700354 return false;
355 }
356 // Readjust to be non-inclusive upper bound.
357 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100358 // Find bss methods if present.
359 bss_methods_ =
360 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000361 // Find bss roots if present.
362 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700363 }
364
David Srbeckyec2cdf42017-12-08 16:21:25 +0000365 vdex_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdex", &symbol_error_msg));
366 if (vdex_begin_ == nullptr) {
367 // No .vdex section.
368 vdex_end_ = nullptr;
369 } else {
370 vdex_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdexlastword", &symbol_error_msg));
371 if (vdex_end_ == nullptr) {
372 *error_msg = StringPrintf("Failed to find oatdexlastword symbol in '%s'", file_path.c_str());
373 return false;
374 }
375 // Readjust to be non-inclusive upper bound.
376 vdex_end_ += sizeof(uint32_t);
377 }
378
Andreas Gampe049cff02015-12-01 23:27:12 -0800379 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800380}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800381
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100382// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
383// position by the number of bytes read, i.e. sizeof(T).
384// Return true on success, false if the read would go beyond the end of the OatFile.
385template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100386inline static bool ReadOatDexFileData(const OatFile& oat_file,
387 /*inout*/const uint8_t** oat,
388 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100389 DCHECK(oat != nullptr);
390 DCHECK(value != nullptr);
391 DCHECK_LE(*oat, oat_file.End());
392 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
393 return false;
394 }
395 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
396 typedef __attribute__((__aligned__(1))) T unaligned_type;
397 *value = *reinterpret_cast<const unaligned_type*>(*oat);
398 *oat += sizeof(T);
399 return true;
400}
401
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100402static inline bool MapConstantTables(const gc::space::ImageSpace* space,
403 uint8_t* address) {
404 // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
405 // Note: The current approach relies on the filename still referencing the same inode.
406
407 File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
408 if (!file.IsOpened()) {
409 LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
410 return false;
411 }
412
413 uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
414 uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
415 std::string error_msg;
416 std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
417 size,
418 PROT_READ,
419 MAP_PRIVATE,
420 file.Fd(),
421 offset,
422 /* low_4gb */ false,
423 /* reuse */ true,
424 file.GetPath().c_str(),
425 &error_msg));
426 if (mem_map == nullptr) {
427 LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
428 return false;
429 }
430 return true;
431}
432
Vladimir Markof3c52b42017-11-17 17:32:12 +0000433static bool ReadIndexBssMapping(OatFile* oat_file,
434 /*inout*/const uint8_t** oat,
435 size_t dex_file_index,
436 const std::string& dex_file_location,
437 const char* tag,
438 /*out*/const IndexBssMapping** mapping,
439 std::string* error_msg) {
440 uint32_t index_bss_mapping_offset;
441 if (UNLIKELY(!ReadOatDexFileData(*oat_file, oat, &index_bss_mapping_offset))) {
442 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
443 "after %s bss mapping offset",
444 oat_file->GetLocation().c_str(),
445 dex_file_index,
446 dex_file_location.c_str(),
447 tag);
448 return false;
449 }
450 const bool readable_index_bss_mapping_size =
451 index_bss_mapping_offset != 0u &&
452 index_bss_mapping_offset <= oat_file->Size() &&
453 IsAligned<alignof(IndexBssMapping)>(index_bss_mapping_offset) &&
454 oat_file->Size() - index_bss_mapping_offset >= IndexBssMapping::ComputeSize(0);
455 const IndexBssMapping* index_bss_mapping = readable_index_bss_mapping_size
456 ? reinterpret_cast<const IndexBssMapping*>(oat_file->Begin() + index_bss_mapping_offset)
457 : nullptr;
458 if (index_bss_mapping_offset != 0u &&
459 (UNLIKELY(index_bss_mapping == nullptr) ||
460 UNLIKELY(index_bss_mapping->size() == 0u) ||
461 UNLIKELY(oat_file->Size() - index_bss_mapping_offset <
462 IndexBssMapping::ComputeSize(index_bss_mapping->size())))) {
463 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
464 " truncated %s bss mapping, offset %u of %zu, length %zu",
465 oat_file->GetLocation().c_str(),
466 dex_file_index,
467 dex_file_location.c_str(),
468 tag,
469 index_bss_mapping_offset,
470 oat_file->Size(),
471 index_bss_mapping != nullptr ? index_bss_mapping->size() : 0u);
472 return false;
473 }
474
475 *mapping = index_bss_mapping;
476 return true;
477}
478
479static void DCheckIndexToBssMapping(OatFile* oat_file,
480 uint32_t number_of_indexes,
481 size_t slot_size,
482 const IndexBssMapping* index_bss_mapping) {
483 if (kIsDebugBuild && index_bss_mapping != nullptr) {
484 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
485 const IndexBssMappingEntry* prev_entry = nullptr;
486 for (const IndexBssMappingEntry& entry : *index_bss_mapping) {
487 CHECK_ALIGNED_PARAM(entry.bss_offset, slot_size);
488 // When loading a non-executable ElfOatFile, .bss symbols are not even
489 // looked up, so we cannot verify the offset against BssSize().
490 if (oat_file->IsExecutable()) {
491 CHECK_LT(entry.bss_offset, oat_file->BssSize());
492 }
493 uint32_t mask = entry.GetMask(index_bits);
494 CHECK_LE(POPCOUNT(mask) * slot_size, entry.bss_offset);
495 size_t index_mask_span = (mask != 0u) ? 32u - index_bits - CTZ(mask) : 0u;
496 CHECK_LE(index_mask_span, entry.GetIndex(index_bits));
497 if (prev_entry != nullptr) {
498 CHECK_LT(prev_entry->GetIndex(index_bits), entry.GetIndex(index_bits) - index_mask_span);
499 }
500 prev_entry = &entry;
501 }
502 CHECK_LT(prev_entry->GetIndex(index_bits), number_of_indexes);
503 }
504}
505
Andreas Gampe049cff02015-12-01 23:27:12 -0800506bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700507 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800508 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100509 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
510 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800511 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700512 return false;
513 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100514 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
515 size_t key_value_store_size =
516 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
517 if (Size() < sizeof(OatHeader) + key_value_store_size) {
518 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
519 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100520 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100521 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100522 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100523 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700524 return false;
525 }
526
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100527 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
528 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
529 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
530 "%zu is not in [%zu, %zu]",
531 GetLocation().c_str(),
532 oat_dex_files_offset,
533 GetOatHeader().GetHeaderSize(),
534 Size());
535 return false;
536 }
537 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
538
539 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
540 if (!IsAligned<kPageSize>(bss_begin_) ||
541 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
542 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000543 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
544 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100545 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000546 GetLocation().c_str(),
547 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100548 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000549 bss_roots_,
550 bss_end_);
551 return false;
552 }
553
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100554 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
555 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
556 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
557 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100558 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000559 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000560 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100561 bss_methods_,
562 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000563 bss_end_);
564 return false;
565 }
566
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100567 uint8_t* after_tables =
568 (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_; // May be null.
569 uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
570 uint8_t* boot_image_tables_end =
571 (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
572 DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100573 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
574 oat_dex_files_storage_.reserve(dex_file_count);
575 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100576 uint32_t dex_file_location_size;
577 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
578 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
579 "location size",
580 GetLocation().c_str(),
581 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700582 return false;
583 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100584 if (UNLIKELY(dex_file_location_size == 0U)) {
585 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
586 GetLocation().c_str(),
587 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700588 return false;
589 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000590 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100591 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
592 "location",
593 GetLocation().c_str(),
594 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700595 return false;
596 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000597 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
598 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700599
Richard Uhlere5fed032015-03-18 08:21:11 -0700600 std::string dex_file_location = ResolveRelativeEncodedDexLocation(
601 abs_dex_location,
602 std::string(dex_file_location_data, dex_file_location_size));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700603
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100604 uint32_t dex_file_checksum;
605 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
606 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
607 "dex file checksum",
608 GetLocation().c_str(),
609 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700610 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700611 return false;
612 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700613
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100614 uint32_t dex_file_offset;
615 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
616 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
617 "after dex file offsets",
618 GetLocation().c_str(),
619 i,
620 dex_file_location.c_str());
621 return false;
622 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100623 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100624 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
625 "offset %u > %zu",
626 GetLocation().c_str(),
627 i,
628 dex_file_location.c_str(),
629 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100630 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700631 return false;
632 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000633 const uint8_t* dex_file_pointer = nullptr;
634 if (UNLIKELY(dex_file_offset == 0U)) {
635 if (uncompressed_dex_files_ == nullptr) {
Andreas Gampefc604a72018-02-08 15:43:37 -0800636 // Do not support mixed-mode oat files.
637 if (i > 0) {
638 *error_msg = StringPrintf("In oat file '%s', unsupported uncompressed-dex-file for dex "
639 "file %zu (%s)",
640 GetLocation().c_str(),
641 i,
642 dex_file_location.c_str());
643 return false;
644 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000645 uncompressed_dex_files_.reset(new std::vector<std::unique_ptr<const DexFile>>());
646 // No dex files, load it from location.
647 const ArtDexFileLoader dex_file_loader;
648 if (!dex_file_loader.Open(dex_file_location.c_str(),
649 dex_file_location,
650 /* verify */ false,
651 /* verify_checksum */ false,
652 error_msg,
653 uncompressed_dex_files_.get())) {
654 if (Runtime::Current() == nullptr) {
655 // If there's no runtime, we're running oatdump, so return
656 // a half constructed oat file that oatdump knows how to deal with.
657 LOG(WARNING) << "Could not find associated dex files of oat file. "
658 << "Oatdump will only dump the header.";
659 return true;
660 } else {
661 return false;
662 }
663 }
Andreas Gampefc604a72018-02-08 15:43:37 -0800664 // The oat file may be out of date wrt/ the dex-file location. We need to be defensive
665 // here and ensure that at least the number of dex files still matches.
666 // Note: actual checksum comparisons are the duty of the OatFileAssistant and will be
667 // done after loading the OatFile.
668 if (uncompressed_dex_files_->size() != dex_file_count) {
669 *error_msg = StringPrintf("In oat file '%s', expected %u uncompressed dex files, but "
670 "found %zu in '%s'",
671 GetLocation().c_str(),
672 dex_file_count,
673 uncompressed_dex_files_->size(),
674 dex_file_location.c_str());
675 return false;
676 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000677 }
678 dex_file_pointer = uncompressed_dex_files_.get()->at(i)->Begin();
679 } else {
Andreas Gampefc604a72018-02-08 15:43:37 -0800680 // Do not support mixed-mode oat files.
681 if (uncompressed_dex_files_ != nullptr) {
682 *error_msg = StringPrintf("In oat file '%s', unsupported embedded dex-file for dex file "
683 "%zu (%s)",
684 GetLocation().c_str(),
685 i,
686 dex_file_location.c_str());
687 return false;
688 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000689 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
690 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
691 "offset %u of %zu but the size of dex file header is %zu",
692 GetLocation().c_str(),
693 i,
694 dex_file_location.c_str(),
695 dex_file_offset,
696 DexSize(),
697 sizeof(DexFile::Header));
698 return false;
699 }
700 dex_file_pointer = DexBegin() + dex_file_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000701 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800702
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700703 const bool valid_magic = DexFileLoader::IsMagicValid(dex_file_pointer);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700704 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100705 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
706 "dex file magic '%s'",
707 GetLocation().c_str(),
708 i,
709 dex_file_location.c_str(),
710 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700711 return false;
712 }
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700713 if (UNLIKELY(!DexFileLoader::IsVersionAndMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100714 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
715 "dex file version '%s'",
716 GetLocation().c_str(),
717 i,
718 dex_file_location.c_str(),
719 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700720 return false;
721 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800722 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000723 if (dex_file_offset != 0 && (DexSize() - dex_file_offset < header->file_size_)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000724 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
725 "offset %u and size %u truncated at %zu",
726 GetLocation().c_str(),
727 i,
728 dex_file_location.c_str(),
729 dex_file_offset,
730 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100731 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000732 return false;
733 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300734
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000735 uint32_t class_offsets_offset;
736 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
737 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
738 "after class offsets offset",
739 GetLocation().c_str(),
740 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300741 dex_file_location.c_str());
742 return false;
743 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000744 if (UNLIKELY(class_offsets_offset > Size()) ||
745 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
746 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
747 "class offsets, offset %u of %zu, class defs %u",
748 GetLocation().c_str(),
749 i,
750 dex_file_location.c_str(),
751 class_offsets_offset,
752 Size(),
753 header->class_defs_size_);
754 return false;
755 }
756 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
757 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
758 "class offsets, offset %u",
759 GetLocation().c_str(),
760 i,
761 dex_file_location.c_str(),
762 class_offsets_offset);
763 return false;
764 }
765 const uint32_t* class_offsets_pointer =
766 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
767
768 uint32_t lookup_table_offset;
769 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
770 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
771 "after lookup table offset",
772 GetLocation().c_str(),
773 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300774 dex_file_location.c_str());
775 return false;
776 }
777 const uint8_t* lookup_table_data = lookup_table_offset != 0u
778 ? Begin() + lookup_table_offset
779 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000780 if (lookup_table_offset != 0u &&
781 (UNLIKELY(lookup_table_offset > Size()) ||
782 UNLIKELY(Size() - lookup_table_offset <
783 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100784 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000785 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100786 GetLocation().c_str(),
787 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000788 dex_file_location.c_str(),
789 lookup_table_offset,
790 Size(),
791 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700792 return false;
793 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700794
Mathieu Chartier120aa282017-08-05 16:03:03 -0700795 uint32_t dex_layout_sections_offset;
796 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
797 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
798 "after dex layout sections offset",
799 GetLocation().c_str(),
800 i,
801 dex_file_location.c_str());
802 return false;
803 }
804 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
805 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
806 : nullptr;
807
Vladimir Markof3c52b42017-11-17 17:32:12 +0000808 const IndexBssMapping* method_bss_mapping;
809 const IndexBssMapping* type_bss_mapping;
810 const IndexBssMapping* string_bss_mapping;
811 if (!ReadIndexBssMapping(
812 this, &oat, i, dex_file_location, "method", &method_bss_mapping, error_msg) ||
813 !ReadIndexBssMapping(
814 this, &oat, i, dex_file_location, "type", &type_bss_mapping, error_msg) ||
815 !ReadIndexBssMapping(
816 this, &oat, i, dex_file_location, "string", &string_bss_mapping, error_msg)) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100817 return false;
818 }
Vladimir Markof3c52b42017-11-17 17:32:12 +0000819 DCheckIndexToBssMapping(
820 this, header->method_ids_size_, static_cast<size_t>(pointer_size), method_bss_mapping);
821 DCheckIndexToBssMapping(
822 this, header->type_ids_size_, sizeof(GcRoot<mirror::Class>), type_bss_mapping);
823 DCheckIndexToBssMapping(
824 this, header->string_ids_size_, sizeof(GcRoot<mirror::String>), string_bss_mapping);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100825
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700826 std::string canonical_location =
827 DexFileLoader::GetDexCanonicalLocation(dex_file_location.c_str());
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100828
829 // Create the OatDexFile and add it to the owning container.
Vladimir Marko539690a2014-06-05 18:36:42 +0100830 OatDexFile* oat_dex_file = new OatDexFile(this,
831 dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100832 canonical_location,
Vladimir Marko539690a2014-06-05 18:36:42 +0100833 dex_file_checksum,
834 dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300835 lookup_table_data,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100836 method_bss_mapping,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000837 type_bss_mapping,
838 string_bss_mapping,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000839 class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -0700840 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100841 oat_dex_files_storage_.push_back(oat_dex_file);
842
843 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Marko3f5838d2014-08-07 18:07:18 +0100844 StringPiece key(oat_dex_file->GetDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100845 oat_dex_files_.Put(key, oat_dex_file);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100846 if (canonical_location != dex_file_location) {
847 StringPiece canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
848 oat_dex_files_.Put(canonical_key, oat_dex_file);
849 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100851
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100852 if (boot_image_tables != nullptr) {
Vladimir Marko667585a2017-09-29 10:42:31 +0100853 Runtime* runtime = Runtime::Current();
854 if (UNLIKELY(runtime == nullptr)) {
855 // This must be oatdump without boot image. Make sure the .bss is inaccessible.
Mathieu Chartier8d8de0c2017-10-04 09:35:30 -0700856 CheckedCall(mprotect, "protect bss", const_cast<uint8_t*>(BssBegin()), BssSize(), PROT_NONE);
Vladimir Marko667585a2017-09-29 10:42:31 +0100857 } else {
858 // Map boot image tables into the .bss. The reserved size must match size of the tables.
859 size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
860 size_t tables_size = 0u;
861 for (gc::space::ImageSpace* space : runtime->GetHeap()->GetBootImageSpaces()) {
862 tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
863 DCHECK_ALIGNED(tables_size, kPageSize);
864 }
865 if (tables_size != reserved_size) {
866 *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
867 " %zu bytes, should be %zu.",
868 GetLocation().c_str(),
869 reserved_size,
870 tables_size);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100871 return false;
872 }
Vladimir Marko667585a2017-09-29 10:42:31 +0100873 for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
874 uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
875 if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
876 return false;
877 }
878 boot_image_tables += current_tables_size;
879 }
880 DCHECK(boot_image_tables == boot_image_tables_end);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100881 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100882 }
Brian Carlstromf1b30302013-03-28 10:35:32 -0700883 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700884}
885
Andreas Gampe049cff02015-12-01 23:27:12 -0800886////////////////////////
887// OatFile via dlopen //
888////////////////////////
889
Andreas Gampe049cff02015-12-01 23:27:12 -0800890class DlOpenOatFile FINAL : public OatFileBase {
891 public:
892 DlOpenOatFile(const std::string& filename, bool executable)
893 : OatFileBase(filename, executable),
894 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700895 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800896 }
897
898 ~DlOpenOatFile() {
899 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700900 if (!kIsTargetBuild) {
901 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
902 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700903 dlclose(dlopen_handle_);
904 } else {
905 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700906 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800907 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800908 }
909
910 protected:
911 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
912 std::string* error_msg) const OVERRIDE {
913 const uint8_t* ptr =
914 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
915 if (ptr == nullptr) {
916 *error_msg = dlerror();
917 }
918 return ptr;
919 }
920
Andreas Gampe4075f832016-05-18 13:09:54 -0700921 void PreLoad() OVERRIDE;
922
Andreas Gampe049cff02015-12-01 23:27:12 -0800923 bool Load(const std::string& elf_filename,
924 uint8_t* oat_file_begin,
925 bool writable,
926 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800927 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800928 std::string* error_msg) OVERRIDE;
929
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700930 bool Load(int, uint8_t*, bool, bool, bool, std::string*) {
931 return false;
932 }
933
Andreas Gampe049cff02015-12-01 23:27:12 -0800934 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
935 void PreSetup(const std::string& elf_filename) OVERRIDE;
936
937 private:
938 bool Dlopen(const std::string& elf_filename,
939 uint8_t* oat_file_begin,
940 std::string* error_msg);
941
Richard Uhlera206c742016-05-24 15:04:22 -0700942 // On the host, if the same library is loaded again with dlopen the same
943 // file handle is returned. This differs from the behavior of dlopen on the
944 // target, where dlopen reloads the library at a different address every
945 // time you load it. The runtime relies on the target behavior to ensure
946 // each instance of the loaded library has a unique dex cache. To avoid
947 // problems, we fall back to our own linker in the case when the same
948 // library is opened multiple times on host. dlopen_handles_ is used to
949 // detect that case.
950 // Guarded by host_dlopen_handles_lock_;
951 static std::unordered_set<void*> host_dlopen_handles_;
952
Andreas Gampe049cff02015-12-01 23:27:12 -0800953 // dlopen handle during runtime.
954 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
955
956 // Dummy memory map objects corresponding to the regions mapped by dlopen.
957 std::vector<std::unique_ptr<MemMap>> dlopen_mmaps_;
958
Andreas Gampe4075f832016-05-18 13:09:54 -0700959 // The number of shared objects the linker told us about before loading. Used to
960 // (optimistically) optimize the PreSetup stage (see comment there).
961 size_t shared_objects_before_;
962
Andreas Gampe049cff02015-12-01 23:27:12 -0800963 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
964};
965
Richard Uhlera206c742016-05-24 15:04:22 -0700966std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
967
Andreas Gampe4075f832016-05-18 13:09:54 -0700968void DlOpenOatFile::PreLoad() {
969#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -0700970 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -0700971 LOG(FATAL) << "Should not reach here.";
972 UNREACHABLE();
973#else
974 // Count the entries in dl_iterate_phdr we get at this point in time.
975 struct dl_iterate_context {
976 static int callback(struct dl_phdr_info *info ATTRIBUTE_UNUSED,
977 size_t size ATTRIBUTE_UNUSED,
978 void *data) {
979 reinterpret_cast<dl_iterate_context*>(data)->count++;
980 return 0; // Continue iteration.
981 }
982 size_t count = 0;
983 } context;
984
985 dl_iterate_phdr(dl_iterate_context::callback, &context);
986 shared_objects_before_ = context.count;
987#endif
988}
989
Andreas Gampe049cff02015-12-01 23:27:12 -0800990bool DlOpenOatFile::Load(const std::string& elf_filename,
991 uint8_t* oat_file_begin,
992 bool writable,
993 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800994 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -0800995 std::string* error_msg) {
996 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
997 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
998 // !executable is a sign that we may want to patch), which may not be allowed for
999 // various reasons.
1000 if (!kUseDlopen) {
1001 *error_msg = "DlOpen is disabled.";
1002 return false;
1003 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001004 if (low_4gb) {
1005 *error_msg = "DlOpen does not support low 4gb loading.";
1006 return false;
1007 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001008 if (writable) {
1009 *error_msg = "DlOpen does not support writable loading.";
1010 return false;
1011 }
1012 if (!executable) {
1013 *error_msg = "DlOpen does not support non-executable loading.";
1014 return false;
1015 }
1016
1017 // dlopen always returns the same library if it is already opened on the host. For this reason
1018 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
1019 // the same library loaded multiple times at different addresses is required for class unloading
1020 // and for having dex caches arrays in the .bss section.
1021 if (!kIsTargetBuild) {
1022 if (!kUseDlopenOnHost) {
1023 *error_msg = "DlOpen disabled for host.";
1024 return false;
1025 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001026 }
1027
1028 bool success = Dlopen(elf_filename, oat_file_begin, error_msg);
1029 DCHECK(dlopen_handle_ != nullptr || !success);
1030
1031 return success;
1032}
1033
1034bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
1035 uint8_t* oat_file_begin,
1036 std::string* error_msg) {
1037#ifdef __APPLE__
1038 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
1039 // but let's fallback to the custom loading code for the time being.
1040 UNUSED(elf_filename, oat_file_begin);
1041 *error_msg = "Dlopen unsupported on Mac.";
1042 return false;
1043#else
1044 {
1045 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
1046 if (absolute_path == nullptr) {
1047 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
1048 return false;
1049 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001050#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +00001051 android_dlextinfo extinfo = {};
Andreas Gampe049cff02015-12-01 23:27:12 -08001052 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
1053 // (open oat files multiple
1054 // times).
1055 ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
1056 // (non-pic boot image).
Andreas Gampe226b91e2015-12-02 10:29:33 -08001057 if (oat_file_begin != nullptr) { //
1058 extinfo.flags |= ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; // Use the requested addr if
1059 extinfo.reserved_addr = oat_file_begin; // vaddr = 0.
1060 } // (pic boot image).
Andreas Gampe049cff02015-12-01 23:27:12 -08001061 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
1062#else
Andreas Gampe049cff02015-12-01 23:27:12 -08001063 UNUSED(oat_file_begin);
Julien Duraj1af0c4f2016-11-16 14:05:48 +00001064 static_assert(!kIsTargetBuild || kIsTargetLinux, "host_dlopen_handles_ will leak handles");
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -07001065 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -07001066 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
1067 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -07001068 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
1069 dlclose(dlopen_handle_);
1070 dlopen_handle_ = nullptr;
1071 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
1072 return false;
1073 }
1074 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001075#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -08001076 }
1077 if (dlopen_handle_ == nullptr) {
1078 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
1079 return false;
1080 }
1081 return true;
1082#endif
1083}
1084
1085void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -08001086#ifdef __APPLE__
1087 UNUSED(elf_filename);
1088 LOG(FATAL) << "Should not reach here.";
1089 UNREACHABLE();
1090#else
Andreas Gampe049cff02015-12-01 23:27:12 -08001091 struct dl_iterate_context {
1092 static int callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
1093 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Andreas Gampe4075f832016-05-18 13:09:54 -07001094 context->shared_objects_seen++;
1095 if (context->shared_objects_seen < context->shared_objects_before) {
1096 // We haven't been called yet for anything we haven't seen before. Just continue.
1097 // Note: this is aggressively optimistic. If another thread was unloading a library,
1098 // we may miss out here. However, this does not happen often in practice.
1099 return 0;
1100 }
1101
Andreas Gampe049cff02015-12-01 23:27:12 -08001102 // See whether this callback corresponds to the file which we have just loaded.
1103 bool contains_begin = false;
1104 for (int i = 0; i < info->dlpi_phnum; i++) {
1105 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1106 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1107 info->dlpi_phdr[i].p_vaddr);
1108 size_t memsz = info->dlpi_phdr[i].p_memsz;
1109 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
1110 contains_begin = true;
1111 break;
1112 }
1113 }
1114 }
1115 // Add dummy mmaps for this file.
1116 if (contains_begin) {
1117 for (int i = 0; i < info->dlpi_phnum; i++) {
1118 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1119 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1120 info->dlpi_phdr[i].p_vaddr);
1121 size_t memsz = info->dlpi_phdr[i].p_memsz;
1122 MemMap* mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
1123 context->dlopen_mmaps_->push_back(std::unique_ptr<MemMap>(mmap));
1124 }
1125 }
1126 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1127 }
1128 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1129 }
1130 const uint8_t* const begin_;
1131 std::vector<std::unique_ptr<MemMap>>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -07001132 const size_t shared_objects_before;
1133 size_t shared_objects_seen;
1134 };
1135 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -08001136
1137 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001138 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
1139 // before giving up. This should be unusual.
1140 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
1141 << shared_objects_before_;
1142 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
1143 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
1144 // OK, give up and print an error.
Andreas Gampe170331f2017-12-07 18:41:03 -08001145 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::WARNING);
Andreas Gampe4075f832016-05-18 13:09:54 -07001146 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
1147 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001148 }
Andreas Gampe74f07b52015-12-02 11:53:26 -08001149#endif
Andreas Gampe049cff02015-12-01 23:27:12 -08001150}
1151
1152////////////////////////////////////////////////
1153// OatFile via our own ElfFile implementation //
1154////////////////////////////////////////////////
1155
1156class ElfOatFile FINAL : public OatFileBase {
1157 public:
1158 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
1159
1160 static ElfOatFile* OpenElfFile(File* file,
1161 const std::string& location,
1162 uint8_t* requested_base,
1163 uint8_t* oat_file_begin, // Override base if not null
1164 bool writable,
1165 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001166 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001167 const char* abs_dex_location,
1168 std::string* error_msg);
1169
1170 bool InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001171 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001172 const char* abs_dex_location,
1173 std::string* error_msg);
1174
1175 protected:
1176 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
1177 std::string* error_msg) const OVERRIDE {
1178 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
1179 if (ptr == nullptr) {
1180 *error_msg = "(Internal implementation could not find symbol)";
1181 }
1182 return ptr;
1183 }
1184
Andreas Gampe4075f832016-05-18 13:09:54 -07001185 void PreLoad() OVERRIDE {
1186 }
1187
Andreas Gampe049cff02015-12-01 23:27:12 -08001188 bool Load(const std::string& elf_filename,
1189 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1190 bool writable,
1191 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001192 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001193 std::string* error_msg) OVERRIDE;
1194
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001195 bool Load(int oat_fd,
1196 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1197 bool writable,
1198 bool executable,
1199 bool low_4gb,
1200 std::string* error_msg) OVERRIDE;
1201
Andreas Gampe049cff02015-12-01 23:27:12 -08001202 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) OVERRIDE {
1203 }
1204
1205 private:
1206 bool ElfFileOpen(File* file,
1207 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1208 bool writable,
1209 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001210 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001211 std::string* error_msg);
1212
1213 private:
1214 // Backing memory map for oat file during cross compilation.
1215 std::unique_ptr<ElfFile> elf_file_;
1216
1217 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
1218};
1219
1220ElfOatFile* ElfOatFile::OpenElfFile(File* file,
1221 const std::string& location,
1222 uint8_t* requested_base,
1223 uint8_t* oat_file_begin, // Override base if not null
1224 bool writable,
1225 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001226 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001227 const char* abs_dex_location,
1228 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001229 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001230 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001231 bool success = oat_file->ElfFileOpen(file,
1232 oat_file_begin,
1233 writable,
1234 low_4gb,
1235 executable,
1236 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001237 if (!success) {
1238 CHECK(!error_msg->empty());
1239 return nullptr;
1240 }
1241
1242 // Complete the setup.
1243 if (!oat_file->ComputeFields(requested_base, file->GetPath(), error_msg)) {
1244 return nullptr;
1245 }
1246
1247 if (!oat_file->Setup(abs_dex_location, error_msg)) {
1248 return nullptr;
1249 }
1250
1251 return oat_file.release();
1252}
1253
1254bool ElfOatFile::InitializeFromElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001255 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001256 const char* abs_dex_location,
1257 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001258 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001259 if (IsExecutable()) {
1260 *error_msg = "Cannot initialize from elf file in executable mode.";
1261 return false;
1262 }
1263 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001264 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001265 uint64_t offset, size;
1266 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1267 CHECK(has_section);
1268 SetBegin(elf_file->Begin() + offset);
1269 SetEnd(elf_file->Begin() + size + offset);
1270 // Ignore the optional .bss section when opening non-executable.
1271 return Setup(abs_dex_location, error_msg);
1272}
1273
1274bool ElfOatFile::Load(const std::string& elf_filename,
1275 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1276 bool writable,
1277 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001278 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001279 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001280 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001281 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1282 if (file == nullptr) {
1283 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1284 return false;
1285 }
1286 return ElfOatFile::ElfFileOpen(file.get(),
1287 oat_file_begin,
1288 writable,
1289 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001290 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001291 error_msg);
1292}
1293
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001294bool ElfOatFile::Load(int oat_fd,
1295 uint8_t* oat_file_begin, // Override where the file is loaded to if not null
1296 bool writable,
1297 bool executable,
1298 bool low_4gb,
1299 std::string* error_msg) {
1300 ScopedTrace trace(__PRETTY_FUNCTION__);
1301 if (oat_fd != -1) {
1302 std::unique_ptr<File> file = std::make_unique<File>(oat_fd, false);
1303 file->DisableAutoClose();
1304 if (file == nullptr) {
1305 *error_msg = StringPrintf("Failed to open oat filename for reading: %s",
1306 strerror(errno));
1307 return false;
1308 }
1309 return ElfOatFile::ElfFileOpen(file.get(),
1310 oat_file_begin,
1311 writable,
1312 executable,
1313 low_4gb,
1314 error_msg);
1315 }
1316 return false;
1317}
1318
Andreas Gampe049cff02015-12-01 23:27:12 -08001319bool ElfOatFile::ElfFileOpen(File* file,
1320 uint8_t* oat_file_begin,
1321 bool writable,
1322 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001323 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001324 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001325 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001326 // TODO: rename requested_base to oat_data_begin
1327 elf_file_.reset(ElfFile::Open(file,
1328 writable,
1329 /*program_header_only*/true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001330 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001331 error_msg,
1332 oat_file_begin));
1333 if (elf_file_ == nullptr) {
1334 DCHECK(!error_msg->empty());
1335 return false;
1336 }
Brian Carlstromf5b0f2c2016-10-14 01:04:26 -07001337 bool loaded = elf_file_->Load(file, executable, low_4gb, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001338 DCHECK(loaded || !error_msg->empty());
1339 return loaded;
1340}
1341
1342//////////////////////////
1343// General OatFile code //
1344//////////////////////////
1345
1346std::string OatFile::ResolveRelativeEncodedDexLocation(
1347 const char* abs_dex_location, const std::string& rel_dex_location) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001348 // For host, we still do resolution as the rel_dex_location might be absolute
1349 // for a target dex (for example /system/foo/foo.apk).
1350 if (abs_dex_location != nullptr && (rel_dex_location[0] != '/' || !kIsTargetBuild)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001351 // Strip :classes<N>.dex used for secondary multidex files.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001352 std::string base = DexFileLoader::GetBaseLocation(rel_dex_location);
1353 std::string multidex_suffix = DexFileLoader::GetMultiDexSuffix(rel_dex_location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001354
1355 // Check if the base is a suffix of the provided abs_dex_location.
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001356 std::string target_suffix = ((rel_dex_location[0] != '/') ? "/" : "") + base;
Andreas Gampe049cff02015-12-01 23:27:12 -08001357 std::string abs_location(abs_dex_location);
1358 if (abs_location.size() > target_suffix.size()) {
1359 size_t pos = abs_location.size() - target_suffix.size();
1360 if (abs_location.compare(pos, std::string::npos, target_suffix) == 0) {
1361 return abs_location + multidex_suffix;
1362 }
1363 }
1364 }
1365 return rel_dex_location;
1366}
1367
1368static void CheckLocation(const std::string& location) {
1369 CHECK(!location.empty());
1370}
1371
1372OatFile* OatFile::OpenWithElfFile(ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001373 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001374 const std::string& location,
1375 const char* abs_dex_location,
1376 std::string* error_msg) {
1377 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, false /* executable */));
David Brazdilc93b3be2016-09-12 18:49:58 +01001378 return oat_file->InitializeFromElfFile(elf_file, vdex_file, abs_dex_location, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001379 ? oat_file.release()
1380 : nullptr;
1381}
1382
David Brazdil7b49e6c2016-09-01 11:06:18 +01001383OatFile* OatFile::Open(const std::string& oat_filename,
1384 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001385 uint8_t* requested_base,
1386 uint8_t* oat_file_begin,
1387 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001388 bool low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001389 const char* abs_dex_location,
1390 std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001391 ScopedTrace trace("Open oat file " + oat_location);
1392 CHECK(!oat_filename.empty()) << oat_location;
1393 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001394
Calin Juravle367b9d82017-05-15 18:18:39 -07001395 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001396
1397 // Check that the files even exist, fast-fail.
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001398 if (!OS::FileExists(vdex_filename.c_str())) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001399 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1400 return nullptr;
1401 } else if (!OS::FileExists(oat_filename.c_str())) {
1402 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001403 return nullptr;
1404 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001405
1406 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1407 // disabled.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001408 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(vdex_filename,
1409 oat_filename,
1410 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001411 requested_base,
1412 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001413 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001414 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001415 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001416 abs_dex_location,
1417 error_msg);
1418 if (with_dlopen != nullptr) {
1419 return with_dlopen;
1420 }
1421 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001422 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001423 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001424 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1425 //
1426 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1427 //
1428 // We use our own ELF loader for Quick to deal with legacy apps that
1429 // open a generated dex file by name, remove the file, then open
1430 // another generated dex file with the same name. http://b/10614658
1431 //
1432 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1433 //
1434 //
1435 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1436 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
David Brazdil7b49e6c2016-09-01 11:06:18 +01001437 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_filename,
1438 oat_filename,
1439 oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001440 requested_base,
1441 oat_file_begin,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001442 false /* writable */,
Andreas Gampe049cff02015-12-01 23:27:12 -08001443 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001444 low_4gb,
Andreas Gampe049cff02015-12-01 23:27:12 -08001445 abs_dex_location,
1446 error_msg);
1447 return with_internal;
1448}
1449
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001450OatFile* OatFile::Open(int vdex_fd,
1451 int oat_fd,
1452 const std::string& oat_location,
1453 uint8_t* requested_base,
1454 uint8_t* oat_file_begin,
1455 bool executable,
1456 bool low_4gb,
1457 const char* abs_dex_location,
1458 std::string* error_msg) {
1459 CHECK(!oat_location.empty()) << oat_location;
1460
1461 std::string vdex_location = GetVdexFilename(oat_location);
1462
1463 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(vdex_fd,
1464 oat_fd,
1465 vdex_location,
1466 oat_location,
1467 requested_base,
1468 oat_file_begin,
1469 false /* writable */,
1470 executable,
1471 low_4gb,
1472 abs_dex_location,
1473 error_msg);
1474 return with_internal;
1475}
1476
Andreas Gampe049cff02015-12-01 23:27:12 -08001477OatFile* OatFile::OpenWritable(File* file,
1478 const std::string& location,
1479 const char* abs_dex_location,
1480 std::string* error_msg) {
1481 CheckLocation(location);
1482 return ElfOatFile::OpenElfFile(file,
1483 location,
1484 nullptr,
1485 nullptr,
1486 true,
1487 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001488 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001489 abs_dex_location,
1490 error_msg);
1491}
1492
1493OatFile* OatFile::OpenReadable(File* file,
1494 const std::string& location,
1495 const char* abs_dex_location,
1496 std::string* error_msg) {
1497 CheckLocation(location);
1498 return ElfOatFile::OpenElfFile(file,
1499 location,
1500 nullptr,
1501 nullptr,
1502 false,
1503 false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001504 /*low_4gb*/false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001505 abs_dex_location,
1506 error_msg);
1507}
1508
1509OatFile::OatFile(const std::string& location, bool is_executable)
1510 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001511 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001512 begin_(nullptr),
1513 end_(nullptr),
1514 bss_begin_(nullptr),
1515 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001516 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001517 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001518 is_executable_(is_executable),
David Srbeckyec2cdf42017-12-08 16:21:25 +00001519 vdex_begin_(nullptr),
1520 vdex_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001521 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1522 CHECK(!location_.empty());
1523}
1524
1525OatFile::~OatFile() {
1526 STLDeleteElements(&oat_dex_files_storage_);
1527}
1528
Brian Carlstrome24fa612011-09-29 00:53:55 -07001529const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001530 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001531}
1532
Ian Rogers13735952014-10-08 12:43:28 -07001533const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001534 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001535 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001536}
1537
Ian Rogers13735952014-10-08 12:43:28 -07001538const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001539 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001540 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001541}
1542
Vladimir Marko5c42c292015-02-25 12:02:49 +00001543const uint8_t* OatFile::BssBegin() const {
1544 return bss_begin_;
1545}
1546
1547const uint8_t* OatFile::BssEnd() const {
1548 return bss_end_;
1549}
1550
David Srbeckyec2cdf42017-12-08 16:21:25 +00001551const uint8_t* OatFile::VdexBegin() const {
1552 return vdex_begin_;
1553}
1554
1555const uint8_t* OatFile::VdexEnd() const {
1556 return vdex_end_;
1557}
1558
David Brazdil7b49e6c2016-09-01 11:06:18 +01001559const uint8_t* OatFile::DexBegin() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001560 return vdex_->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001561}
1562
1563const uint8_t* OatFile::DexEnd() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001564 return vdex_->End();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001565}
1566
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001567ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1568 if (bss_methods_ != nullptr) {
1569 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1570 ArtMethod** methods_end =
1571 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1572 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1573 } else {
1574 return ArrayRef<ArtMethod*>();
1575 }
1576}
1577
Vladimir Markoaad75c62016-10-03 08:46:48 +00001578ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1579 if (bss_roots_ != nullptr) {
1580 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1581 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1582 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1583 } else {
1584 return ArrayRef<GcRoot<mirror::Object>>();
1585 }
1586}
1587
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001588const OatFile::OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1589 const uint32_t* dex_location_checksum,
Richard Uhler9a37efc2016-08-05 16:32:55 -07001590 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001591 // NOTE: We assume here that the canonical location for a given dex_location never
1592 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1593 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1594 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001595
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001596 // TODO: Additional analysis of usage patterns to see if this can be simplified
1597 // without any performance loss, for example by not doing the first lock-free lookup.
1598
1599 const OatFile::OatDexFile* oat_dex_file = nullptr;
1600 StringPiece key(dex_location);
1601 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1602 // directly mentioned in the oat file and doesn't require locking.
1603 auto primary_it = oat_dex_files_.find(key);
1604 if (primary_it != oat_dex_files_.end()) {
1605 oat_dex_file = primary_it->second;
1606 DCHECK(oat_dex_file != nullptr);
1607 } else {
1608 // This dex_location is not one of the dex locations directly mentioned in the
1609 // oat file. The correct lookup is via the canonical location but first see in
1610 // the secondary_oat_dex_files_ whether we've looked up this location before.
1611 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1612 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1613 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001614 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001615 } else {
1616 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001617 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001618 if (dex_canonical_location != dex_location) {
1619 StringPiece canonical_key(dex_canonical_location);
1620 auto canonical_it = oat_dex_files_.find(canonical_key);
1621 if (canonical_it != oat_dex_files_.end()) {
1622 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001623 } // else keep null.
1624 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001625
1626 // Copy the key to the string_cache_ and store the result in secondary map.
1627 string_cache_.emplace_back(key.data(), key.length());
1628 StringPiece key_copy(string_cache_.back());
1629 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001630 }
1631 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001632
1633 if (oat_dex_file == nullptr) {
1634 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001635 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001636 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1637 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1638 }
1639 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001640 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001641
Richard Uhler9a37efc2016-08-05 16:32:55 -07001642 if (dex_location_checksum != nullptr &&
1643 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1644 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001645 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001646 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1647 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1648 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1649 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1650 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001651 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001652 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001653 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001654 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001655}
1656
Brian Carlstrome24fa612011-09-29 00:53:55 -07001657OatFile::OatDexFile::OatDexFile(const OatFile* oat_file,
Elliott Hughesaa6a5882012-01-13 19:39:16 -08001658 const std::string& dex_file_location,
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001659 const std::string& canonical_dex_file_location,
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001660 uint32_t dex_file_location_checksum,
Ian Rogers13735952014-10-08 12:43:28 -07001661 const uint8_t* dex_file_pointer,
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001662 const uint8_t* lookup_table_data,
Vladimir Markof3c52b42017-11-17 17:32:12 +00001663 const IndexBssMapping* method_bss_mapping_data,
1664 const IndexBssMapping* type_bss_mapping_data,
1665 const IndexBssMapping* string_bss_mapping_data,
Vladimir Marko09d09432015-09-08 13:47:48 +01001666 const uint32_t* oat_class_offsets_pointer,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001667 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001668 : oat_file_(oat_file),
1669 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001670 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001671 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001672 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001673 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001674 method_bss_mapping_(method_bss_mapping_data),
Vladimir Markof3c52b42017-11-17 17:32:12 +00001675 type_bss_mapping_(type_bss_mapping_data),
1676 string_bss_mapping_(string_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001677 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001678 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001679 // Initialize TypeLookupTable.
1680 if (lookup_table_data_ != nullptr) {
1681 // Peek the number of classes from the DexFile.
1682 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1683 const uint32_t num_class_defs = dex_header->class_defs_size_;
1684 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1685 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1686 } else {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001687 const uint8_t* dex_data = dex_file_pointer_;
1688 // TODO: Clean this up to create the type lookup table after the dex file has been created?
1689 if (CompactDexFile::IsMagicValid(dex_header->magic_)) {
1690 dex_data += dex_header->data_off_;
1691 }
1692 lookup_table_ = TypeLookupTable::Open(dex_data, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001693 }
1694 }
1695}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001696
Mathieu Chartier1b868492016-11-16 16:22:37 -08001697OatFile::OatDexFile::OatDexFile(std::unique_ptr<TypeLookupTable>&& lookup_table)
1698 : lookup_table_(std::move(lookup_table)) {}
1699
Brian Carlstrome24fa612011-09-29 00:53:55 -07001700OatFile::OatDexFile::~OatDexFile() {}
1701
Ian Rogers05f28c62012-10-23 18:12:13 -07001702size_t OatFile::OatDexFile::FileSize() const {
1703 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1704}
1705
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001706std::unique_ptr<const DexFile> OatFile::OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001707 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001708 static constexpr bool kVerify = false;
1709 static constexpr bool kVerifyChecksum = false;
David Sehr013fd802018-01-11 22:55:24 -08001710 const ArtDexFileLoader dex_file_loader;
1711 return dex_file_loader.Open(dex_file_pointer_,
1712 FileSize(),
1713 dex_file_location_,
1714 dex_file_location_checksum_,
1715 this,
1716 kVerify,
1717 kVerifyChecksum,
1718 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001719}
1720
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001721uint32_t OatFile::OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
1722 return oat_class_offsets_pointer_[class_def_index];
1723}
1724
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001725OatFile::OatClass OatFile::OatDexFile::GetOatClass(uint16_t class_def_index) const {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001726 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001727
Ian Rogers13735952014-10-08 12:43:28 -07001728 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001729 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001730
Ian Rogers13735952014-10-08 12:43:28 -07001731 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001732 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
Vladimir Marko2c64a832018-01-04 11:31:56 +00001733 ClassStatus status = enum_cast<ClassStatus>(*reinterpret_cast<const int16_t*>(status_pointer));
1734 CHECK_LE(status, ClassStatus::kLast);
Brian Carlstromba150c32013-08-27 17:31:03 -07001735
Ian Rogers13735952014-10-08 12:43:28 -07001736 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001737 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1738 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1739 CHECK_LT(type, kOatClassMax);
1740
Ian Rogers13735952014-10-08 12:43:28 -07001741 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001742 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001743
Brian Carlstromcd937a92014-03-04 22:53:23 -08001744 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07001745 const uint8_t* bitmap_pointer = nullptr;
1746 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07001747 if (type != kOatClassNoneCompiled) {
1748 if (type == kOatClassSomeCompiled) {
1749 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
1750 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
1751 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
1752 methods_pointer = bitmap_pointer + bitmap_size;
1753 } else {
1754 methods_pointer = after_type_pointer;
1755 }
1756 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08001757 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001758
Richard Uhler07b3c232015-03-31 15:57:54 -07001759 return OatFile::OatClass(oat_file_,
1760 status,
1761 type,
1762 bitmap_size,
1763 reinterpret_cast<const uint32_t*>(bitmap_pointer),
1764 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001765}
1766
David Sehr9aa352e2016-09-15 18:13:52 -07001767const DexFile::ClassDef* OatFile::OatDexFile::FindClassDef(const DexFile& dex_file,
1768 const char* descriptor,
1769 size_t hash) {
1770 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1771 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001772 bool used_lookup_table = false;
1773 const DexFile::ClassDef* lookup_table_classdef = nullptr;
David Sehr9aa352e2016-09-15 18:13:52 -07001774 if (LIKELY((oat_dex_file != nullptr) && (oat_dex_file->GetTypeLookupTable() != nullptr))) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001775 used_lookup_table = true;
David Sehr9aa352e2016-09-15 18:13:52 -07001776 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable()->Lookup(descriptor, hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001777 lookup_table_classdef = (class_def_idx != dex::kDexNoIndex)
1778 ? &dex_file.GetClassDef(class_def_idx)
1779 : nullptr;
1780 if (!kIsDebugBuild) {
1781 return lookup_table_classdef;
1782 }
David Sehr9aa352e2016-09-15 18:13:52 -07001783 }
1784 // Fast path for rare no class defs case.
1785 const uint32_t num_class_defs = dex_file.NumClassDefs();
1786 if (num_class_defs == 0) {
1787 return nullptr;
1788 }
1789 const DexFile::TypeId* type_id = dex_file.FindTypeId(descriptor);
1790 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001791 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001792 const DexFile::ClassDef* found_class_def = dex_file.FindClassDef(type_idx);
1793 if (kIsDebugBuild && used_lookup_table) {
1794 DCHECK_EQ(found_class_def, lookup_table_classdef);
1795 }
1796 return found_class_def;
David Sehr9aa352e2016-09-15 18:13:52 -07001797 }
1798 return nullptr;
1799}
1800
Mathieu Chartier120aa282017-08-05 16:03:03 -07001801// Madvise the dex file based on the state we are moving to.
1802void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001803 Runtime* const runtime = Runtime::Current();
1804 const bool low_ram = runtime->GetHeap()->IsLowMemoryMode();
Mathieu Chartier150d25d2017-08-28 09:52:55 -07001805 // TODO: Also do madvise hints for non low ram devices.
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001806 if (!low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07001807 return;
1808 }
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07001809 if (state == MadviseState::kMadviseStateAtLoad && runtime->MAdviseRandomAccess()) {
1810 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
1811 // Other devices have enough page cache to get performance benefits from loading more pages
1812 // into the page cache.
1813 MadviseLargestPageAlignedRegion(dex_file.Begin(),
1814 dex_file.Begin() + dex_file.Size(),
1815 MADV_RANDOM);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001816 }
1817 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1818 if (oat_dex_file != nullptr) {
1819 // Should always be there.
1820 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
1821 CHECK(sections != nullptr);
1822 sections->Madvise(&dex_file, state);
1823 }
1824}
1825
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001826OatFile::OatClass::OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +00001827 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -07001828 OatClassType type,
1829 uint32_t bitmap_size,
1830 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001831 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07001832 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001833 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001834 switch (type_) {
1835 case kOatClassAllCompiled: {
1836 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001837 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001838 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001839 break;
1840 }
1841 case kOatClassSomeCompiled: {
1842 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001843 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001844 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001845 break;
1846 }
1847 case kOatClassNoneCompiled: {
1848 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001849 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07001850 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07001851 break;
1852 }
1853 case kOatClassMax: {
1854 LOG(FATAL) << "Invalid OatClassType " << type_;
1855 break;
1856 }
1857 }
1858}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001859
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001860uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
1861 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1862 if (oat_method_offsets == nullptr) {
1863 return 0u;
1864 }
1865 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
1866}
1867
1868const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001869 // 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 -07001870 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001871 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001872 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001873 }
1874 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001875 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07001876 CHECK_EQ(kOatClassAllCompiled, type_);
1877 methods_pointer_index = method_index;
1878 } else {
1879 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001880 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001881 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07001882 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001883 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
1884 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07001885 }
1886 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001887 return &oat_method_offsets;
1888}
1889
1890const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
1891 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
1892 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001893 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001894 }
1895 if (oat_file_->IsExecutable() ||
1896 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001897 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001898 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07001899 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001900 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
1901 // version.
1902 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001903}
1904
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905void OatFile::OatMethod::LinkMethod(ArtMethod* method) const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001906 CHECK(method != nullptr);
Ian Rogersef7d42f2014-01-06 12:55:46 -08001907 method->SetEntryPointFromQuickCompiledCode(GetQuickCode());
Brian Carlstromae826982011-11-09 01:33:42 -08001908}
1909
Andreas Gampe7ba64962014-10-23 11:37:40 -07001910bool OatFile::IsPic() const {
Igor Murashkin46774762014-10-22 11:37:02 -07001911 return GetOatHeader().IsPic();
Andreas Gampe7ba64962014-10-23 11:37:40 -07001912 // TODO: Check against oat_patches. b/18144996
1913}
1914
Sebastien Hertz0de11332015-05-13 12:14:05 +02001915bool OatFile::IsDebuggable() const {
1916 return GetOatHeader().IsDebuggable();
1917}
1918
Andreas Gampe29d38e72016-03-23 15:31:51 +00001919CompilerFilter::Filter OatFile::GetCompilerFilter() const {
1920 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00001921}
1922
Calin Juravle44e5efa2017-09-12 00:54:26 -07001923std::string OatFile::GetClassLoaderContext() const {
1924 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001925}
Calin Juravle44e5efa2017-09-12 00:54:26 -07001926
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001927const char* OatFile::GetCompilationReason() const {
1928 return GetOatHeader().GetStoreValueByKey(OatHeader::kCompilationReasonKey);
1929}
1930
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001931OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
1932 uint16_t class_def_idx,
1933 bool* found) {
1934 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
1935 const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08001936 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01001937 *found = false;
1938 return OatFile::OatClass::Invalid();
1939 }
1940 *found = true;
1941 return oat_dex_file->GetOatClass(class_def_idx);
1942}
1943
Mathieu Chartier1b868492016-11-16 16:22:37 -08001944void OatFile::OatDexFile::AssertAotCompiler() {
1945 CHECK(Runtime::Current()->IsAotCompiler());
1946}
1947
Brian Carlstrome24fa612011-09-29 00:53:55 -07001948} // namespace art