blob: 81356c2f2d05161d3e1a5a8bfc121ce4ac6c77e6 [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 Gampe875b4f22018-11-19 12:59:15 -080036#include <android-base/logging.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080037#include "android-base/stringprintf.h"
38
David Brazdil7126c5b2019-03-05 00:02:51 +000039#include "arch/instruction_set_features.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080040#include "art_method.h"
Brian Carlstromba150c32013-08-27 17:31:03 -070041#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070042#include "base/enums.h"
David Sehr891a50e2017-10-27 17:01:07 -070043#include "base/file_utils.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080044#include "base/logging.h" // For VLOG_IS_ON.
David Sehr79e26072018-04-06 17:58:50 -070045#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080046#include "base/os.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080047#include "base/stl_util.h"
Vladimir Markob7bf8432019-12-03 13:18:50 +000048#include "base/string_view_cpp20.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080049#include "base/systrace.h"
Elliott Hughes76160052012-12-12 16:31:20 -080050#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080051#include "base/utils.h"
David Sehr013fd802018-01-11 22:55:24 -080052#include "dex/art_dex_file_loader.h"
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080053#include "dex/dex_file.h"
David Sehr9e734c72018-01-04 17:56:19 -080054#include "dex/dex_file_loader.h"
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080055#include "dex/dex_file_structs.h"
David Sehr9e734c72018-01-04 17:56:19 -080056#include "dex/dex_file_types.h"
57#include "dex/standard_dex_file.h"
David Sehr9c4a0152018-04-05 12:23:54 -070058#include "dex/type_lookup_table.h"
David Sehr0225f8e2018-01-31 08:52:24 +000059#include "dex/utf-inl.h"
David Srbecky50928112019-03-22 17:06:28 +000060#include "elf/elf_utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061#include "elf_file.h"
Vladimir Markoaad75c62016-10-03 08:46:48 +000062#include "gc_root.h"
Vladimir Marko1cedb4a2019-02-06 14:13:28 +000063#include "gc/heap.h"
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +010064#include "gc/space/image_space.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065#include "mirror/class.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070066#include "mirror/object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070067#include "oat.h"
Nicolas Geoffrayc04c8002015-07-14 11:37:54 +010068#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070069#include "oat_file_manager.h"
Vladimir Marko1cedb4a2019-02-06 14:13:28 +000070#include "runtime-inl.h"
Vladimir Marko97d7e1c2016-10-04 14:44:28 +010071#include "vdex_file.h"
David Brazdil7126c5b2019-03-05 00:02:51 +000072#include "verifier/verifier_deps.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070073
74namespace art {
75
Andreas Gampe46ee31b2016-12-14 10:11:49 -080076using android::base::StringPrintf;
77
Andreas Gampe049cff02015-12-01 23:27:12 -080078// Whether OatFile::Open will try dlopen. Fallback is our own ELF loader.
David Srbecky1baabf02015-06-16 17:12:34 +000079static constexpr bool kUseDlopen = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070080
Andreas Gampe049cff02015-12-01 23:27:12 -080081// Whether OatFile::Open will try dlopen on the host. On the host we're not linking against
Andreas Gampefa8429b2015-04-07 18:34:42 -070082// bionic, so cannot take advantage of the support for changed semantics (loading the same soname
83// multiple times). However, if/when we switch the above, we likely want to switch this, too,
84// to get test coverage of the code paths.
David Srbecky1baabf02015-06-16 17:12:34 +000085static constexpr bool kUseDlopenOnHost = true;
Andreas Gampefa8429b2015-04-07 18:34:42 -070086
87// For debugging, Open will print DlOpen error message if set to true.
88static constexpr bool kPrintDlOpenErrorMessage = false;
89
Andreas Gampe049cff02015-12-01 23:27:12 -080090// Note for OatFileBase and descendents:
91//
92// These are used in OatFile::Open to try all our loaders.
93//
94// The process is simple:
95//
96// 1) Allocate an instance through the standard constructor (location, executable)
97// 2) Load() to try to open the file.
98// 3) ComputeFields() to populate the OatFile fields like begin_, using FindDynamicSymbolAddress.
99// 4) PreSetup() for any steps that should be done before the final setup.
100// 5) Setup() to complete the procedure.
Richard Uhlere5fed032015-03-18 08:21:11 -0700101
Andreas Gampe049cff02015-12-01 23:27:12 -0800102class OatFileBase : public OatFile {
103 public:
104 virtual ~OatFileBase() {}
Richard Uhlere5fed032015-03-18 08:21:11 -0700105
Andreas Gampe049cff02015-12-01 23:27:12 -0800106 template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100107 static OatFileBase* OpenOatFile(int zip_fd,
108 const std::string& vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100109 const std::string& elf_filename,
Alex Light84d76052014-08-22 17:49:35 -0700110 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800111 bool writable,
112 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800113 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000114 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100115 /*inout*/MemMap* reservation, // Where to load if not null.
116 /*out*/std::string* error_msg);
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800117
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700118 template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100119 static OatFileBase* OpenOatFile(int zip_fd,
120 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700121 int oat_fd,
122 const std::string& vdex_filename,
123 const std::string& oat_filename,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700124 bool writable,
125 bool executable,
126 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000127 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100128 /*inout*/MemMap* reservation, // Where to load if not null.
129 /*out*/std::string* error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700130
Andreas Gampe049cff02015-12-01 23:27:12 -0800131 protected:
132 OatFileBase(const std::string& filename, bool executable) : OatFile(filename, executable) {}
Andreas Gampefa8429b2015-04-07 18:34:42 -0700133
Andreas Gampe049cff02015-12-01 23:27:12 -0800134 virtual const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
135 std::string* error_msg) const = 0;
136
Andreas Gampe4075f832016-05-18 13:09:54 -0700137 virtual void PreLoad() = 0;
138
David Brazdil7b49e6c2016-09-01 11:06:18 +0100139 bool LoadVdex(const std::string& vdex_filename,
140 bool writable,
141 bool low_4gb,
142 std::string* error_msg);
143
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700144 bool LoadVdex(int vdex_fd,
145 const std::string& vdex_filename,
146 bool writable,
147 bool low_4gb,
148 std::string* error_msg);
149
Andreas Gampe049cff02015-12-01 23:27:12 -0800150 virtual bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800151 bool writable,
152 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800153 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100154 /*inout*/MemMap* reservation, // Where to load if not null.
155 /*out*/std::string* error_msg) = 0;
Andreas Gampe049cff02015-12-01 23:27:12 -0800156
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700157 virtual bool Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700158 bool writable,
159 bool executable,
160 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100161 /*inout*/MemMap* reservation, // Where to load if not null.
162 /*out*/std::string* error_msg) = 0;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700163
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100164 bool ComputeFields(const std::string& file_path, std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -0800165
166 virtual void PreSetup(const std::string& elf_filename) = 0;
167
Vladimir Markob7bf8432019-12-03 13:18:50 +0000168 bool Setup(int zip_fd, ArrayRef<const std::string> dex_filenames, std::string* error_msg);
David Brazdil7126c5b2019-03-05 00:02:51 +0000169 bool Setup(const std::vector<const DexFile*>& dex_files);
Andreas Gampe049cff02015-12-01 23:27:12 -0800170
171 // Setters exposed for ElfOatFile.
172
173 void SetBegin(const uint8_t* begin) {
174 begin_ = begin;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700175 }
176
Andreas Gampe049cff02015-12-01 23:27:12 -0800177 void SetEnd(const uint8_t* end) {
178 end_ = end;
179 }
180
David Brazdilc93b3be2016-09-12 18:49:58 +0100181 void SetVdex(VdexFile* vdex) {
182 vdex_.reset(vdex);
183 }
184
Andreas Gampe049cff02015-12-01 23:27:12 -0800185 private:
Alex Lightabd8f052019-12-06 10:49:17 -0800186 // Returns true if we want to remove quickened opcodes before loading the VDEX file, false
187 // otherwise.
188 bool ShouldUnquickenVDex() const;
189
Andreas Gampe049cff02015-12-01 23:27:12 -0800190 DISALLOW_COPY_AND_ASSIGN(OatFileBase);
191};
192
193template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100194OatFileBase* OatFileBase::OpenOatFile(int zip_fd,
195 const std::string& vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100196 const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800197 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -0800198 bool writable,
199 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800200 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000201 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100202 /*inout*/MemMap* reservation,
203 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800204 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(location, executable));
Andreas Gampe4075f832016-05-18 13:09:54 -0700205
206 ret->PreLoad();
207
Andreas Gampe049cff02015-12-01 23:27:12 -0800208 if (!ret->Load(elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800209 writable,
210 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800211 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100212 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -0800213 error_msg)) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800214 return nullptr;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 }
Elliott Hughes956af0f2014-12-11 14:34:28 -0800216
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100217 if (!ret->ComputeFields(elf_filename, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800218 return nullptr;
219 }
Andreas Gampe4075f832016-05-18 13:09:54 -0700220
David Sehr2300b2d2018-05-10 14:20:10 -0700221 ret->PreSetup(elf_filename);
222
David Srbeckyec2cdf42017-12-08 16:21:25 +0000223 if (!ret->LoadVdex(vdex_filename, writable, low_4gb, error_msg)) {
224 return nullptr;
225 }
226
Vladimir Markob7bf8432019-12-03 13:18:50 +0000227 if (!ret->Setup(zip_fd, dex_filenames, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800228 return nullptr;
229 }
230
Dave Allison69dfe512014-07-11 17:11:58 +0000231 return ret.release();
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800232}
233
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700234template <typename kOatFileBaseSubType>
Nicolas Geoffray30025092018-04-19 14:43:29 +0100235OatFileBase* OatFileBase::OpenOatFile(int zip_fd,
236 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700237 int oat_fd,
238 const std::string& vdex_location,
239 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700240 bool writable,
241 bool executable,
242 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000243 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100244 /*inout*/MemMap* reservation,
245 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700246 std::unique_ptr<OatFileBase> ret(new kOatFileBaseSubType(oat_location, executable));
247
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700248 if (!ret->Load(oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700249 writable,
250 executable,
251 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100252 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700253 error_msg)) {
254 return nullptr;
255 }
256
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100257 if (!ret->ComputeFields(oat_location, error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700258 return nullptr;
259 }
260
David Sehr2300b2d2018-05-10 14:20:10 -0700261 ret->PreSetup(oat_location);
262
David Srbeckyec2cdf42017-12-08 16:21:25 +0000263 if (!ret->LoadVdex(vdex_fd, vdex_location, writable, low_4gb, error_msg)) {
264 return nullptr;
265 }
266
Vladimir Markob7bf8432019-12-03 13:18:50 +0000267 if (!ret->Setup(zip_fd, dex_filenames, error_msg)) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700268 return nullptr;
269 }
270
271 return ret.release();
272}
273
Alex Lightabd8f052019-12-06 10:49:17 -0800274bool OatFileBase::ShouldUnquickenVDex() const {
275 // We sometimes load oat files without a runtime (eg oatdump) and don't want to do anything in
276 // that case. If we are debuggable there are no -quick opcodes to unquicken. If the runtime is not
277 // debuggable we don't care whether there are -quick opcodes or not so no need to do anything.
278 return Runtime::Current() != nullptr && !IsDebuggable() && Runtime::Current()->IsJavaDebuggable();
279}
280
David Brazdil7b49e6c2016-09-01 11:06:18 +0100281bool OatFileBase::LoadVdex(const std::string& vdex_filename,
282 bool writable,
283 bool low_4gb,
284 std::string* error_msg) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000285 vdex_ = VdexFile::OpenAtAddress(vdex_begin_,
286 vdex_end_ - vdex_begin_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100287 /*mmap_reuse=*/ vdex_begin_ != nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000288 vdex_filename,
289 writable,
290 low_4gb,
Alex Lightabd8f052019-12-06 10:49:17 -0800291 ShouldUnquickenVDex(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000292 error_msg);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100293 if (vdex_.get() == nullptr) {
294 *error_msg = StringPrintf("Failed to load vdex file '%s' %s",
295 vdex_filename.c_str(),
296 error_msg->c_str());
297 return false;
298 }
299 return true;
300}
301
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700302bool OatFileBase::LoadVdex(int vdex_fd,
303 const std::string& vdex_filename,
304 bool writable,
305 bool low_4gb,
306 std::string* error_msg) {
307 if (vdex_fd != -1) {
308 struct stat s;
309 int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd, &s));
310 if (rc == -1) {
311 PLOG(WARNING) << "Failed getting length of vdex file";
312 } else {
Alex Lightabd8f052019-12-06 10:49:17 -0800313 vdex_ = VdexFile::OpenAtAddress(
314 vdex_begin_,
315 vdex_end_ - vdex_begin_,
316 /*mmap_reuse=*/ vdex_begin_ != nullptr,
317 vdex_fd,
318 s.st_size,
319 vdex_filename,
320 writable,
321 low_4gb,
322 ShouldUnquickenVDex(),
323 error_msg);
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700324 if (vdex_.get() == nullptr) {
325 *error_msg = "Failed opening vdex file.";
326 return false;
327 }
328 }
329 }
330 return true;
331}
332
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100333bool OatFileBase::ComputeFields(const std::string& file_path, std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800334 std::string symbol_error_msg;
335 begin_ = FindDynamicSymbolAddress("oatdata", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700336 if (begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800337 *error_msg = StringPrintf("Failed to find oatdata symbol in '%s' %s",
338 file_path.c_str(),
339 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700340 return false;
341 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800342 end_ = FindDynamicSymbolAddress("oatlastword", &symbol_error_msg);
Andreas Gampefa8429b2015-04-07 18:34:42 -0700343 if (end_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800344 *error_msg = StringPrintf("Failed to find oatlastword symbol in '%s' %s",
345 file_path.c_str(),
346 symbol_error_msg.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700347 return false;
348 }
349 // Readjust to be non-inclusive upper bound.
350 end_ += sizeof(uint32_t);
351
Vladimir Markob066d432018-01-03 13:14:37 +0000352 data_bimg_rel_ro_begin_ = FindDynamicSymbolAddress("oatdatabimgrelro", &symbol_error_msg);
353 if (data_bimg_rel_ro_begin_ != nullptr) {
354 data_bimg_rel_ro_end_ =
355 FindDynamicSymbolAddress("oatdatabimgrelrolastword", &symbol_error_msg);
356 if (data_bimg_rel_ro_end_ == nullptr) {
357 *error_msg =
358 StringPrintf("Failed to find oatdatabimgrelrolastword symbol in '%s'", file_path.c_str());
359 return false;
360 }
361 // Readjust to be non-inclusive upper bound.
362 data_bimg_rel_ro_end_ += sizeof(uint32_t);
363 }
364
Andreas Gampe049cff02015-12-01 23:27:12 -0800365 bss_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbss", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700366 if (bss_begin_ == nullptr) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800367 // No .bss section.
Andreas Gampefa8429b2015-04-07 18:34:42 -0700368 bss_end_ = nullptr;
Andreas Gampefa8429b2015-04-07 18:34:42 -0700369 } else {
Andreas Gampe049cff02015-12-01 23:27:12 -0800370 bss_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbsslastword", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700371 if (bss_end_ == nullptr) {
David Srbeckyec2cdf42017-12-08 16:21:25 +0000372 *error_msg = StringPrintf("Failed to find oatbsslastword symbol in '%s'", file_path.c_str());
Andreas Gampefa8429b2015-04-07 18:34:42 -0700373 return false;
374 }
375 // Readjust to be non-inclusive upper bound.
376 bss_end_ += sizeof(uint32_t);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100377 // Find bss methods if present.
378 bss_methods_ =
379 const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssmethods", &symbol_error_msg));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380 // Find bss roots if present.
381 bss_roots_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatbssroots", &symbol_error_msg));
Andreas Gampefa8429b2015-04-07 18:34:42 -0700382 }
383
David Srbeckyec2cdf42017-12-08 16:21:25 +0000384 vdex_begin_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdex", &symbol_error_msg));
385 if (vdex_begin_ == nullptr) {
386 // No .vdex section.
387 vdex_end_ = nullptr;
388 } else {
389 vdex_end_ = const_cast<uint8_t*>(FindDynamicSymbolAddress("oatdexlastword", &symbol_error_msg));
390 if (vdex_end_ == nullptr) {
391 *error_msg = StringPrintf("Failed to find oatdexlastword symbol in '%s'", file_path.c_str());
392 return false;
393 }
394 // Readjust to be non-inclusive upper bound.
395 vdex_end_ += sizeof(uint32_t);
396 }
397
Andreas Gampe049cff02015-12-01 23:27:12 -0800398 return true;
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800399}
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800400
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100401// Read an unaligned entry from the OatDexFile data in OatFile and advance the read
402// position by the number of bytes read, i.e. sizeof(T).
403// Return true on success, false if the read would go beyond the end of the OatFile.
404template <typename T>
Vladimir Marko722fa982015-10-19 18:18:27 +0100405inline static bool ReadOatDexFileData(const OatFile& oat_file,
406 /*inout*/const uint8_t** oat,
407 /*out*/T* value) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100408 DCHECK(oat != nullptr);
409 DCHECK(value != nullptr);
410 DCHECK_LE(*oat, oat_file.End());
411 if (UNLIKELY(static_cast<size_t>(oat_file.End() - *oat) < sizeof(T))) {
412 return false;
413 }
414 static_assert(std::is_trivial<T>::value, "T must be a trivial type");
Andreas Gampec55bb392018-09-21 00:02:02 +0000415 using unaligned_type __attribute__((__aligned__(1))) = T;
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100416 *value = *reinterpret_cast<const unaligned_type*>(*oat);
417 *oat += sizeof(T);
418 return true;
419}
420
Vladimir Markof3c52b42017-11-17 17:32:12 +0000421static bool ReadIndexBssMapping(OatFile* oat_file,
422 /*inout*/const uint8_t** oat,
423 size_t dex_file_index,
424 const std::string& dex_file_location,
425 const char* tag,
426 /*out*/const IndexBssMapping** mapping,
427 std::string* error_msg) {
428 uint32_t index_bss_mapping_offset;
429 if (UNLIKELY(!ReadOatDexFileData(*oat_file, oat, &index_bss_mapping_offset))) {
430 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
431 "after %s bss mapping offset",
432 oat_file->GetLocation().c_str(),
433 dex_file_index,
434 dex_file_location.c_str(),
435 tag);
436 return false;
437 }
438 const bool readable_index_bss_mapping_size =
439 index_bss_mapping_offset != 0u &&
440 index_bss_mapping_offset <= oat_file->Size() &&
441 IsAligned<alignof(IndexBssMapping)>(index_bss_mapping_offset) &&
442 oat_file->Size() - index_bss_mapping_offset >= IndexBssMapping::ComputeSize(0);
443 const IndexBssMapping* index_bss_mapping = readable_index_bss_mapping_size
444 ? reinterpret_cast<const IndexBssMapping*>(oat_file->Begin() + index_bss_mapping_offset)
445 : nullptr;
446 if (index_bss_mapping_offset != 0u &&
447 (UNLIKELY(index_bss_mapping == nullptr) ||
448 UNLIKELY(index_bss_mapping->size() == 0u) ||
449 UNLIKELY(oat_file->Size() - index_bss_mapping_offset <
450 IndexBssMapping::ComputeSize(index_bss_mapping->size())))) {
451 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned or "
452 " truncated %s bss mapping, offset %u of %zu, length %zu",
453 oat_file->GetLocation().c_str(),
454 dex_file_index,
455 dex_file_location.c_str(),
456 tag,
457 index_bss_mapping_offset,
458 oat_file->Size(),
459 index_bss_mapping != nullptr ? index_bss_mapping->size() : 0u);
460 return false;
461 }
462
463 *mapping = index_bss_mapping;
464 return true;
465}
466
David Brazdil7126c5b2019-03-05 00:02:51 +0000467bool OatFileBase::Setup(const std::vector<const DexFile*>& dex_files) {
468 for (const DexFile* dex_file : dex_files) {
469 std::string dex_location = dex_file->GetLocation();
470 std::string canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location.c_str());
471
472 // Create an OatDexFile and add it to the owning container.
473 OatDexFile* oat_dex_file = new OatDexFile(this, dex_file, dex_location, canonical_location);
474 oat_dex_files_storage_.push_back(oat_dex_file);
475
476 // Add the location and canonical location (if different) to the oat_dex_files_ table.
477 std::string_view key(oat_dex_file->GetDexFileLocation());
478 oat_dex_files_.Put(key, oat_dex_file);
479 if (canonical_location != dex_location) {
480 std::string_view canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
481 oat_dex_files_.Put(canonical_key, oat_dex_file);
482 }
483 }
484
485 return true;
486}
487
Vladimir Markob7bf8432019-12-03 13:18:50 +0000488bool OatFileBase::Setup(int zip_fd,
489 ArrayRef<const std::string> dex_filenames,
490 std::string* error_msg) {
Brian Carlstromf1b30302013-03-28 10:35:32 -0700491 if (!GetOatHeader().IsValid()) {
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800492 std::string cause = GetOatHeader().GetValidationErrorMessage();
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100493 *error_msg = StringPrintf("Invalid oat header for '%s': %s",
494 GetLocation().c_str(),
Andreas Gampe2bcb3b22014-12-12 15:25:14 -0800495 cause.c_str());
Brian Carlstromf1b30302013-03-28 10:35:32 -0700496 return false;
497 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100498 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
499 size_t key_value_store_size =
500 (Size() >= sizeof(OatHeader)) ? GetOatHeader().GetKeyValueStoreSize() : 0u;
501 if (Size() < sizeof(OatHeader) + key_value_store_size) {
502 *error_msg = StringPrintf("In oat file '%s' found truncated OatHeader, "
503 "size = %zu < %zu + %zu",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100504 GetLocation().c_str(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100505 Size(),
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100506 sizeof(OatHeader),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100507 key_value_store_size);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700508 return false;
509 }
510
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100511 size_t oat_dex_files_offset = GetOatHeader().GetOatDexFilesOffset();
512 if (oat_dex_files_offset < GetOatHeader().GetHeaderSize() || oat_dex_files_offset > Size()) {
513 *error_msg = StringPrintf("In oat file '%s' found invalid oat dex files offset: "
514 "%zu is not in [%zu, %zu]",
515 GetLocation().c_str(),
516 oat_dex_files_offset,
517 GetOatHeader().GetHeaderSize(),
518 Size());
519 return false;
520 }
521 const uint8_t* oat = Begin() + oat_dex_files_offset; // Jump to the OatDexFile records.
522
Vladimir Markob066d432018-01-03 13:14:37 +0000523 if (!IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_begin_) ||
524 !IsAligned<sizeof(uint32_t)>(data_bimg_rel_ro_end_) ||
525 data_bimg_rel_ro_begin_ > data_bimg_rel_ro_end_) {
526 *error_msg = StringPrintf("In oat file '%s' found unaligned or unordered databimgrelro "
527 "symbol(s): begin = %p, end = %p",
528 GetLocation().c_str(),
529 data_bimg_rel_ro_begin_,
530 data_bimg_rel_ro_end_);
531 return false;
532 }
533
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100534 DCHECK_GE(static_cast<size_t>(pointer_size), alignof(GcRoot<mirror::Object>));
535 if (!IsAligned<kPageSize>(bss_begin_) ||
536 !IsAlignedParam(bss_methods_, static_cast<size_t>(pointer_size)) ||
537 !IsAlignedParam(bss_roots_, static_cast<size_t>(pointer_size)) ||
Vladimir Markoaad75c62016-10-03 08:46:48 +0000538 !IsAligned<alignof(GcRoot<mirror::Object>)>(bss_end_)) {
539 *error_msg = StringPrintf("In oat file '%s' found unaligned bss symbol(s): "
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100540 "begin = %p, methods_ = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000541 GetLocation().c_str(),
542 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100543 bss_methods_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000544 bss_roots_,
545 bss_end_);
546 return false;
547 }
548
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100549 if ((bss_methods_ != nullptr && (bss_methods_ < bss_begin_ || bss_methods_ > bss_end_)) ||
550 (bss_roots_ != nullptr && (bss_roots_ < bss_begin_ || bss_roots_ > bss_end_)) ||
551 (bss_methods_ != nullptr && bss_roots_ != nullptr && bss_methods_ > bss_roots_)) {
552 *error_msg = StringPrintf("In oat file '%s' found bss symbol(s) outside .bss or unordered: "
Vladimir Marko0f3c7002017-09-07 14:15:56 +0100553 "begin = %p, methods = %p, roots = %p, end = %p",
Vladimir Markoaad75c62016-10-03 08:46:48 +0000554 GetLocation().c_str(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000555 bss_begin_,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100556 bss_methods_,
557 bss_roots_,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000558 bss_end_);
559 return false;
560 }
561
Vladimir Markoe47f60c2018-02-21 13:43:28 +0000562 if (bss_methods_ != nullptr && bss_methods_ != bss_begin_) {
563 *error_msg = StringPrintf("In oat file '%s' found unexpected .bss gap before 'oatbssmethods': "
564 "begin = %p, methods = %p",
565 GetLocation().c_str(),
566 bss_begin_,
567 bss_methods_);
568 return false;
569 }
570
Vladimir Markob7bf8432019-12-03 13:18:50 +0000571 std::string_view primary_location;
572 std::string_view primary_location_replacement;
573 size_t dex_filenames_pos = 0u;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100574 uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
575 oat_dex_files_storage_.reserve(dex_file_count);
576 for (size_t i = 0; i < dex_file_count; i++) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100577 uint32_t dex_file_location_size;
578 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_location_size))) {
579 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu truncated after dex file "
580 "location size",
581 GetLocation().c_str(),
582 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700583 return false;
584 }
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100585 if (UNLIKELY(dex_file_location_size == 0U)) {
586 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with empty location name",
587 GetLocation().c_str(),
588 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700589 return false;
590 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000591 if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100592 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
593 "location",
594 GetLocation().c_str(),
595 i);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700596 return false;
597 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000598 const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
599 oat += dex_file_location_size;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700600
Vladimir Markob7bf8432019-12-03 13:18:50 +0000601 // Location encoded in the oat file. We will use this for multidex naming.
602 std::string_view oat_dex_file_location(dex_file_location_data, dex_file_location_size);
603 std::string dex_file_location(oat_dex_file_location);
604 bool is_multidex = DexFileLoader::IsMultiDexLocation(dex_file_location.c_str());
605 if (!is_multidex) {
606 primary_location = oat_dex_file_location;
607 if (!dex_filenames.empty()) {
608 if (dex_filenames_pos == dex_filenames.size()) {
609 *error_msg = StringPrintf("In oat file '%s' found excessive primary location '%s'"
610 ", expected only %zu primary locations",
611 GetLocation().c_str(),
612 dex_file_location.c_str(),
613 dex_filenames.size());
614 return false;
615 }
616 primary_location_replacement = dex_filenames[dex_filenames_pos];
617 ++dex_filenames_pos;
618 }
619 }
620 if ((i == 0 && is_multidex) ||
621 (!external_dex_files_.empty() && (is_multidex != (i < external_dex_files_.size())))) {
622 *error_msg = StringPrintf("In oat file '%s' found unexpected %s location '%s'",
623 GetLocation().c_str(),
624 is_multidex ? "multi-dex" : "primary",
625 dex_file_location.c_str());
626 return false;
627 }
628 if (is_multidex &&
629 (!StartsWith(dex_file_location, primary_location) ||
630 dex_file_location[primary_location.size()] != DexFileLoader::kMultiDexSeparator)) {
631 *error_msg = StringPrintf("In oat file '%s' found unexpected multidex location '%s',"
632 " unrelated to '%s'",
633 GetLocation().c_str(),
634 dex_file_location.c_str(),
635 std::string(primary_location).c_str());
636 return false;
637 }
638 std::string dex_file_name = dex_file_location;
639 if (!dex_filenames.empty()) {
640 dex_file_name.replace(/*pos*/ 0u, primary_location.size(), primary_location_replacement);
641 // If the location does not contain path and matches the file name component,
642 // use the provided file name also as the location.
643 // TODO: Do we need this for anything other than tests?
644 if (dex_file_location.find('/') == std::string::npos &&
645 dex_file_name.size() > dex_file_location.size() &&
646 dex_file_name[dex_file_name.size() - dex_file_location.size() - 1u] == '/' &&
647 EndsWith(dex_file_name, dex_file_location)) {
648 dex_file_location = dex_file_name;
649 }
650 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700651
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100652 uint32_t dex_file_checksum;
653 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_checksum))) {
654 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated after "
655 "dex file checksum",
656 GetLocation().c_str(),
657 i,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700658 dex_file_location.c_str());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700659 return false;
660 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700661
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100662 uint32_t dex_file_offset;
663 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_file_offset))) {
664 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
665 "after dex file offsets",
666 GetLocation().c_str(),
667 i,
668 dex_file_location.c_str());
669 return false;
670 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100671 if (UNLIKELY(dex_file_offset > DexSize())) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100672 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
673 "offset %u > %zu",
674 GetLocation().c_str(),
675 i,
676 dex_file_location.c_str(),
677 dex_file_offset,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100678 DexSize());
Brian Carlstromfb331d72013-07-25 22:00:16 -0700679 return false;
680 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000681 const uint8_t* dex_file_pointer = nullptr;
682 if (UNLIKELY(dex_file_offset == 0U)) {
Vladimir Markob7bf8432019-12-03 13:18:50 +0000683 // Do not support mixed-mode oat files.
684 if (i != 0u && external_dex_files_.empty()) {
685 *error_msg = StringPrintf("In oat file '%s', unsupported uncompressed-dex-file for dex "
686 "file %zu (%s)",
687 GetLocation().c_str(),
688 i,
689 dex_file_location.c_str());
690 return false;
691 }
692 DCHECK_LE(i, external_dex_files_.size());
693 if (i == external_dex_files_.size()) {
694 std::vector<std::unique_ptr<const DexFile>> new_dex_files;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000695 // No dex files, load it from location.
696 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100697 bool loaded = false;
698 if (zip_fd != -1) {
699 loaded = dex_file_loader.OpenZip(zip_fd,
700 dex_file_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100701 /*verify=*/ false,
702 /*verify_checksum=*/ false,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100703 error_msg,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000704 &new_dex_files);
Nicolas Geoffray30025092018-04-19 14:43:29 +0100705 } else {
David Brazdil3e8aae02019-03-26 18:48:02 +0000706 loaded = dex_file_loader.Open(dex_file_name.c_str(),
Nicolas Geoffray30025092018-04-19 14:43:29 +0100707 dex_file_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100708 /*verify=*/ false,
709 /*verify_checksum=*/ false,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100710 error_msg,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000711 &new_dex_files);
Nicolas Geoffray30025092018-04-19 14:43:29 +0100712 }
713 if (!loaded) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000714 if (Runtime::Current() == nullptr) {
715 // If there's no runtime, we're running oatdump, so return
716 // a half constructed oat file that oatdump knows how to deal with.
717 LOG(WARNING) << "Could not find associated dex files of oat file. "
718 << "Oatdump will only dump the header.";
719 return true;
720 } else {
721 return false;
722 }
723 }
Andreas Gampefc604a72018-02-08 15:43:37 -0800724 // The oat file may be out of date wrt/ the dex-file location. We need to be defensive
725 // here and ensure that at least the number of dex files still matches.
Vladimir Markob7bf8432019-12-03 13:18:50 +0000726 // If we have a zip_fd, or reached the end of provided `dex_filenames`, we must
727 // load all dex files from that file, otherwise we may open multiple files.
Andreas Gampefc604a72018-02-08 15:43:37 -0800728 // Note: actual checksum comparisons are the duty of the OatFileAssistant and will be
729 // done after loading the OatFile.
Vladimir Markob7bf8432019-12-03 13:18:50 +0000730 size_t max_dex_files = dex_file_count - external_dex_files_.size();
731 bool expect_all =
732 (zip_fd != -1) || (!dex_filenames.empty() && dex_filenames_pos == dex_filenames.size());
733 if (expect_all ? new_dex_files.size() != max_dex_files
734 : new_dex_files.size() > max_dex_files) {
735 *error_msg = StringPrintf("In oat file '%s', expected %s%zu uncompressed dex files, but "
Andreas Gampefc604a72018-02-08 15:43:37 -0800736 "found %zu in '%s'",
737 GetLocation().c_str(),
Vladimir Markob7bf8432019-12-03 13:18:50 +0000738 (expect_all ? "" : "<="),
739 max_dex_files,
740 new_dex_files.size(),
Andreas Gampefc604a72018-02-08 15:43:37 -0800741 dex_file_location.c_str());
742 return false;
743 }
Vladimir Markob7bf8432019-12-03 13:18:50 +0000744 for (std::unique_ptr<const DexFile>& dex_file : new_dex_files) {
745 external_dex_files_.push_back(std::move(dex_file));
746 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000747 }
Vladimir Markob7bf8432019-12-03 13:18:50 +0000748 dex_file_pointer = external_dex_files_[i]->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000749 } else {
Andreas Gampefc604a72018-02-08 15:43:37 -0800750 // Do not support mixed-mode oat files.
Vladimir Markob7bf8432019-12-03 13:18:50 +0000751 if (!external_dex_files_.empty()) {
Andreas Gampefc604a72018-02-08 15:43:37 -0800752 *error_msg = StringPrintf("In oat file '%s', unsupported embedded dex-file for dex file "
753 "%zu (%s)",
754 GetLocation().c_str(),
755 i,
756 dex_file_location.c_str());
757 return false;
758 }
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000759 if (UNLIKELY(DexSize() - dex_file_offset < sizeof(DexFile::Header))) {
760 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
761 "offset %u of %zu but the size of dex file header is %zu",
762 GetLocation().c_str(),
763 i,
764 dex_file_location.c_str(),
765 dex_file_offset,
766 DexSize(),
767 sizeof(DexFile::Header));
768 return false;
769 }
770 dex_file_pointer = DexBegin() + dex_file_offset;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000771 }
Brian Carlstrom89521892011-12-07 22:05:07 -0800772
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700773 const bool valid_magic = DexFileLoader::IsMagicValid(dex_file_pointer);
Mathieu Chartier7b074bf2017-09-25 16:22:36 -0700774 if (UNLIKELY(!valid_magic)) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100775 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
776 "dex file magic '%s'",
777 GetLocation().c_str(),
778 i,
779 dex_file_location.c_str(),
780 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700781 return false;
782 }
Mathieu Chartiercf76bf82017-09-25 16:22:36 -0700783 if (UNLIKELY(!DexFileLoader::IsVersionAndMagicValid(dex_file_pointer))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100784 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with invalid "
785 "dex file version '%s'",
786 GetLocation().c_str(),
787 i,
788 dex_file_location.c_str(),
789 dex_file_pointer);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700790 return false;
791 }
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800792 const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000793 if (dex_file_offset != 0 && (DexSize() - dex_file_offset < header->file_size_)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000794 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
795 "offset %u and size %u truncated at %zu",
796 GetLocation().c_str(),
797 i,
798 dex_file_location.c_str(),
799 dex_file_offset,
800 header->file_size_,
David Brazdil7b49e6c2016-09-01 11:06:18 +0100801 DexSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000802 return false;
803 }
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300804
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000805 uint32_t class_offsets_offset;
806 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
807 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
808 "after class offsets offset",
809 GetLocation().c_str(),
810 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300811 dex_file_location.c_str());
812 return false;
813 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000814 if (UNLIKELY(class_offsets_offset > Size()) ||
815 UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
816 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
817 "class offsets, offset %u of %zu, class defs %u",
818 GetLocation().c_str(),
819 i,
820 dex_file_location.c_str(),
821 class_offsets_offset,
822 Size(),
823 header->class_defs_size_);
824 return false;
825 }
826 if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
827 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
828 "class offsets, offset %u",
829 GetLocation().c_str(),
830 i,
831 dex_file_location.c_str(),
832 class_offsets_offset);
833 return false;
834 }
835 const uint32_t* class_offsets_pointer =
836 reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
837
838 uint32_t lookup_table_offset;
839 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
840 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
841 "after lookup table offset",
842 GetLocation().c_str(),
843 i,
Artem Udovichenkod9786b02015-10-14 16:36:55 +0300844 dex_file_location.c_str());
845 return false;
846 }
847 const uint8_t* lookup_table_data = lookup_table_offset != 0u
848 ? Begin() + lookup_table_offset
849 : nullptr;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000850 if (lookup_table_offset != 0u &&
851 (UNLIKELY(lookup_table_offset > Size()) ||
852 UNLIKELY(Size() - lookup_table_offset <
853 TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100854 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000855 "type lookup table, offset %u of %zu, class defs %u",
Vladimir Marko06d7aaa2015-10-16 11:23:41 +0100856 GetLocation().c_str(),
857 i,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000858 dex_file_location.c_str(),
859 lookup_table_offset,
860 Size(),
861 header->class_defs_size_);
Brian Carlstromfb331d72013-07-25 22:00:16 -0700862 return false;
863 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700864
Mathieu Chartier120aa282017-08-05 16:03:03 -0700865 uint32_t dex_layout_sections_offset;
866 if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &dex_layout_sections_offset))) {
867 *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
868 "after dex layout sections offset",
869 GetLocation().c_str(),
870 i,
871 dex_file_location.c_str());
872 return false;
873 }
874 const DexLayoutSections* const dex_layout_sections = dex_layout_sections_offset != 0
875 ? reinterpret_cast<const DexLayoutSections*>(Begin() + dex_layout_sections_offset)
876 : nullptr;
877
Vladimir Markof3c52b42017-11-17 17:32:12 +0000878 const IndexBssMapping* method_bss_mapping;
879 const IndexBssMapping* type_bss_mapping;
880 const IndexBssMapping* string_bss_mapping;
881 if (!ReadIndexBssMapping(
882 this, &oat, i, dex_file_location, "method", &method_bss_mapping, error_msg) ||
883 !ReadIndexBssMapping(
884 this, &oat, i, dex_file_location, "type", &type_bss_mapping, error_msg) ||
885 !ReadIndexBssMapping(
886 this, &oat, i, dex_file_location, "string", &string_bss_mapping, error_msg)) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100887 return false;
888 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100889
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100890 // Create the OatDexFile and add it to the owning container.
David Brazdil3e8aae02019-03-26 18:48:02 +0000891 OatDexFile* oat_dex_file = new OatDexFile(
892 this,
893 dex_file_location,
894 DexFileLoader::GetDexCanonicalLocation(dex_file_name.c_str()),
895 dex_file_checksum,
896 dex_file_pointer,
897 lookup_table_data,
898 method_bss_mapping,
899 type_bss_mapping,
900 string_bss_mapping,
901 class_offsets_pointer,
902 dex_layout_sections);
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100903 oat_dex_files_storage_.push_back(oat_dex_file);
904
905 // Add the location and canonical location (if different) to the oat_dex_files_ table.
Vladimir Markob7bf8432019-12-03 13:18:50 +0000906 // Note: We do not add the non-canonical `dex_file_name`. If it is different from both
907 // the location and canonical location, GetOatDexFile() shall canonicalize it when
908 // requested and match the canonical path.
909 std::string_view key = oat_dex_file_location; // References oat file data.
David Brazdil3e8aae02019-03-26 18:48:02 +0000910 std::string_view canonical_key(oat_dex_file->GetCanonicalDexFileLocation());
Vladimir Marko539690a2014-06-05 18:36:42 +0100911 oat_dex_files_.Put(key, oat_dex_file);
David Brazdil3e8aae02019-03-26 18:48:02 +0000912 if (canonical_key != key) {
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100913 oat_dex_files_.Put(canonical_key, oat_dex_file);
914 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700915 }
Vladimir Markob7bf8432019-12-03 13:18:50 +0000916 if (!dex_filenames.empty() && dex_filenames_pos != dex_filenames.size()) {
917 *error_msg = StringPrintf("Oat file '%s' contains only %zu primary dex locations, expected %zu",
918 GetLocation().c_str(),
919 dex_filenames_pos,
920 dex_filenames.size());
921 return false;
922 }
Vladimir Marko09d09432015-09-08 13:47:48 +0100923
Vladimir Markob066d432018-01-03 13:14:37 +0000924 if (DataBimgRelRoBegin() != nullptr) {
Vladimir Markoe80ecf32019-08-01 15:20:58 +0100925 // Make .data.bimg.rel.ro read only. ClassLinker shall temporarily make it writable for
926 // relocation when we register a dex file from this oat file. We do not do the relocation
927 // here to avoid dirtying the pages if the code is never actually ready to be executed.
Vladimir Markob066d432018-01-03 13:14:37 +0000928 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin());
929 CheckedCall(mprotect, "protect relocations", reloc_begin, DataBimgRelRoSize(), PROT_READ);
Vladimir Markoe80ecf32019-08-01 15:20:58 +0100930 // Make sure the file lists a boot image dependency, otherwise the .data.bimg.rel.ro
931 // section is bogus. The full dependency is checked before the code is executed.
Vladimir Marko21910692019-11-06 13:27:03 +0000932 // We cannot do this check if we do not have a key-value store, i.e. for secondary
933 // oat files for boot image extensions.
934 if (GetOatHeader().GetKeyValueStoreSize() != 0u) {
935 const char* boot_class_path_checksum =
936 GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
937 if (boot_class_path_checksum == nullptr ||
938 boot_class_path_checksum[0] != gc::space::ImageSpace::kImageChecksumPrefix) {
939 *error_msg = StringPrintf("Oat file '%s' contains .data.bimg.rel.ro section "
940 "without boot image dependency.",
941 GetLocation().c_str());
942 return false;
943 }
Vladimir Markob066d432018-01-03 13:14:37 +0000944 }
945 }
946
Brian Carlstromf1b30302013-03-28 10:35:32 -0700947 return true;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700948}
949
Andreas Gampe049cff02015-12-01 23:27:12 -0800950////////////////////////
951// OatFile via dlopen //
952////////////////////////
953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100954class DlOpenOatFile final : public OatFileBase {
Andreas Gampe049cff02015-12-01 23:27:12 -0800955 public:
956 DlOpenOatFile(const std::string& filename, bool executable)
957 : OatFileBase(filename, executable),
958 dlopen_handle_(nullptr),
Richard Uhlera206c742016-05-24 15:04:22 -0700959 shared_objects_before_(0) {
Andreas Gampe049cff02015-12-01 23:27:12 -0800960 }
961
962 ~DlOpenOatFile() {
963 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -0700964 if (!kIsTargetBuild) {
965 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
966 host_dlopen_handles_.erase(dlopen_handle_);
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -0700967 dlclose(dlopen_handle_);
968 } else {
969 dlclose(dlopen_handle_);
Richard Uhlera206c742016-05-24 15:04:22 -0700970 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800971 }
Andreas Gampe049cff02015-12-01 23:27:12 -0800972 }
973
974 protected:
975 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100976 std::string* error_msg) const override {
Andreas Gampe049cff02015-12-01 23:27:12 -0800977 const uint8_t* ptr =
978 reinterpret_cast<const uint8_t*>(dlsym(dlopen_handle_, symbol_name.c_str()));
979 if (ptr == nullptr) {
980 *error_msg = dlerror();
981 }
982 return ptr;
983 }
984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100985 void PreLoad() override;
Andreas Gampe4075f832016-05-18 13:09:54 -0700986
Andreas Gampe049cff02015-12-01 23:27:12 -0800987 bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -0800988 bool writable,
989 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800990 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100991 /*inout*/MemMap* reservation, // Where to load if not null.
992 /*out*/std::string* error_msg) override;
Andreas Gampe049cff02015-12-01 23:27:12 -0800993
Vladimir Markoc09cd052018-08-23 16:36:36 +0100994 bool Load(int oat_fd ATTRIBUTE_UNUSED,
995 bool writable ATTRIBUTE_UNUSED,
996 bool executable ATTRIBUTE_UNUSED,
997 bool low_4gb ATTRIBUTE_UNUSED,
998 /*inout*/MemMap* reservation ATTRIBUTE_UNUSED,
999 /*out*/std::string* error_msg ATTRIBUTE_UNUSED) override {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001000 return false;
1001 }
1002
Andreas Gampe049cff02015-12-01 23:27:12 -08001003 // Ask the linker where it mmaped the file and notify our mmap wrapper of the regions.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001004 void PreSetup(const std::string& elf_filename) override;
Andreas Gampe049cff02015-12-01 23:27:12 -08001005
1006 private:
1007 bool Dlopen(const std::string& elf_filename,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001008 /*inout*/MemMap* reservation, // Where to load if not null.
1009 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001010
Richard Uhlera206c742016-05-24 15:04:22 -07001011 // On the host, if the same library is loaded again with dlopen the same
1012 // file handle is returned. This differs from the behavior of dlopen on the
1013 // target, where dlopen reloads the library at a different address every
1014 // time you load it. The runtime relies on the target behavior to ensure
1015 // each instance of the loaded library has a unique dex cache. To avoid
1016 // problems, we fall back to our own linker in the case when the same
1017 // library is opened multiple times on host. dlopen_handles_ is used to
1018 // detect that case.
1019 // Guarded by host_dlopen_handles_lock_;
1020 static std::unordered_set<void*> host_dlopen_handles_;
1021
Vladimir Markoc09cd052018-08-23 16:36:36 +01001022 // Reservation and dummy memory map objects corresponding to the regions mapped by dlopen.
1023 // Note: Must be destroyed after dlclose() as it can hold the owning reservation.
1024 std::vector<MemMap> dlopen_mmaps_;
1025
Andreas Gampe049cff02015-12-01 23:27:12 -08001026 // dlopen handle during runtime.
1027 void* dlopen_handle_; // TODO: Unique_ptr with custom deleter.
1028
Andreas Gampe4075f832016-05-18 13:09:54 -07001029 // The number of shared objects the linker told us about before loading. Used to
1030 // (optimistically) optimize the PreSetup stage (see comment there).
1031 size_t shared_objects_before_;
1032
Andreas Gampe049cff02015-12-01 23:27:12 -08001033 DISALLOW_COPY_AND_ASSIGN(DlOpenOatFile);
1034};
1035
Richard Uhlera206c742016-05-24 15:04:22 -07001036std::unordered_set<void*> DlOpenOatFile::host_dlopen_handles_;
1037
Andreas Gampe4075f832016-05-18 13:09:54 -07001038void DlOpenOatFile::PreLoad() {
1039#ifdef __APPLE__
Andreas Gampe39004a62016-05-18 21:27:00 -07001040 UNUSED(shared_objects_before_);
Andreas Gampe4075f832016-05-18 13:09:54 -07001041 LOG(FATAL) << "Should not reach here.";
1042 UNREACHABLE();
1043#else
1044 // Count the entries in dl_iterate_phdr we get at this point in time.
1045 struct dl_iterate_context {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001046 static int callback(dl_phdr_info* info ATTRIBUTE_UNUSED,
Andreas Gampe4075f832016-05-18 13:09:54 -07001047 size_t size ATTRIBUTE_UNUSED,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001048 void* data) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001049 reinterpret_cast<dl_iterate_context*>(data)->count++;
1050 return 0; // Continue iteration.
1051 }
1052 size_t count = 0;
1053 } context;
1054
1055 dl_iterate_phdr(dl_iterate_context::callback, &context);
1056 shared_objects_before_ = context.count;
1057#endif
1058}
1059
Andreas Gampe049cff02015-12-01 23:27:12 -08001060bool DlOpenOatFile::Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001061 bool writable,
1062 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001063 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001064 /*inout*/MemMap* reservation, // Where to load if not null.
1065 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001066 // Use dlopen only when flagged to do so, and when it's OK to load things executable.
1067 // TODO: Also try when not executable? The issue here could be re-mapping as writable (as
1068 // !executable is a sign that we may want to patch), which may not be allowed for
1069 // various reasons.
1070 if (!kUseDlopen) {
1071 *error_msg = "DlOpen is disabled.";
1072 return false;
1073 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001074 if (low_4gb) {
1075 *error_msg = "DlOpen does not support low 4gb loading.";
1076 return false;
1077 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001078 if (writable) {
1079 *error_msg = "DlOpen does not support writable loading.";
1080 return false;
1081 }
1082 if (!executable) {
1083 *error_msg = "DlOpen does not support non-executable loading.";
1084 return false;
1085 }
1086
1087 // dlopen always returns the same library if it is already opened on the host. For this reason
1088 // we only use dlopen if we are the target or we do not already have the dex file opened. Having
1089 // the same library loaded multiple times at different addresses is required for class unloading
1090 // and for having dex caches arrays in the .bss section.
1091 if (!kIsTargetBuild) {
1092 if (!kUseDlopenOnHost) {
1093 *error_msg = "DlOpen disabled for host.";
1094 return false;
1095 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001096 }
1097
Vladimir Markoc09cd052018-08-23 16:36:36 +01001098 bool success = Dlopen(elf_filename, reservation, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001099 DCHECK(dlopen_handle_ != nullptr || !success);
1100
1101 return success;
1102}
1103
1104bool DlOpenOatFile::Dlopen(const std::string& elf_filename,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001105 /*inout*/MemMap* reservation,
1106 /*out*/std::string* error_msg) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001107#ifdef __APPLE__
1108 // The dl_iterate_phdr syscall is missing. There is similar API on OSX,
1109 // but let's fallback to the custom loading code for the time being.
Vladimir Marko3ec8fb62018-08-31 17:47:38 +01001110 UNUSED(elf_filename, reservation);
Andreas Gampe049cff02015-12-01 23:27:12 -08001111 *error_msg = "Dlopen unsupported on Mac.";
1112 return false;
1113#else
1114 {
1115 UniqueCPtr<char> absolute_path(realpath(elf_filename.c_str(), nullptr));
1116 if (absolute_path == nullptr) {
1117 *error_msg = StringPrintf("Failed to find absolute path for '%s'", elf_filename.c_str());
1118 return false;
1119 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001120#ifdef ART_TARGET_ANDROID
Anton Kirilov3a2e78e2017-01-06 13:33:42 +00001121 android_dlextinfo extinfo = {};
Vladimir Markof6cfd002018-11-01 16:53:31 +00001122 extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; // Force-load, don't reuse handle
1123 // (open oat files multiple times).
Vladimir Markoc09cd052018-08-23 16:36:36 +01001124 if (reservation != nullptr) {
1125 if (!reservation->IsValid()) {
1126 *error_msg = StringPrintf("Invalid reservation for %s", elf_filename.c_str());
1127 return false;
1128 }
1129 extinfo.flags |= ANDROID_DLEXT_RESERVED_ADDRESS; // Use the reserved memory range.
1130 extinfo.reserved_addr = reservation->Begin();
1131 extinfo.reserved_size = reservation->Size();
1132 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001133 dlopen_handle_ = android_dlopen_ext(absolute_path.get(), RTLD_NOW, &extinfo);
Vladimir Markoc09cd052018-08-23 16:36:36 +01001134 if (reservation != nullptr && dlopen_handle_ != nullptr) {
1135 // Find used pages from the reservation.
1136 struct dl_iterate_context {
1137 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) {
1138 auto* context = reinterpret_cast<dl_iterate_context*>(data);
1139 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match");
1140 using Elf_Half = Elf64_Half;
1141
1142 // See whether this callback corresponds to the file which we have just loaded.
1143 uint8_t* reservation_begin = context->reservation->Begin();
1144 bool contained_in_reservation = false;
1145 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
1146 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1147 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1148 info->dlpi_phdr[i].p_vaddr);
1149 size_t memsz = info->dlpi_phdr[i].p_memsz;
1150 size_t offset = static_cast<size_t>(vaddr - reservation_begin);
1151 if (offset < context->reservation->Size()) {
1152 contained_in_reservation = true;
1153 DCHECK_LE(memsz, context->reservation->Size() - offset);
1154 } else if (vaddr < reservation_begin) {
1155 // Check that there's no overlap with the reservation.
1156 DCHECK_LE(memsz, static_cast<size_t>(reservation_begin - vaddr));
1157 }
1158 break; // It is sufficient to check the first PT_LOAD header.
1159 }
1160 }
1161
1162 if (contained_in_reservation) {
1163 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
1164 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1165 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1166 info->dlpi_phdr[i].p_vaddr);
1167 size_t memsz = info->dlpi_phdr[i].p_memsz;
1168 size_t offset = static_cast<size_t>(vaddr - reservation_begin);
1169 DCHECK_LT(offset, context->reservation->Size());
1170 DCHECK_LE(memsz, context->reservation->Size() - offset);
1171 context->max_size = std::max(context->max_size, offset + memsz);
1172 }
1173 }
1174
1175 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1176 }
1177 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1178 }
1179
1180 const MemMap* const reservation;
1181 size_t max_size = 0u;
1182 };
1183 dl_iterate_context context = { reservation };
1184
1185 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
1186 LOG(FATAL) << "Could not find the shared object mmapped to the reservation.";
1187 UNREACHABLE();
1188 }
1189
1190 // Take ownership of the memory used by the shared object. dlopen() does not assume
1191 // full ownership of this memory and dlclose() shall just remap it as zero pages with
1192 // PROT_NONE. We need to unmap the memory when destroying this oat file.
1193 dlopen_mmaps_.push_back(reservation->TakeReservedMemory(context.max_size));
1194 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001195#else
Steve Austin882ed6b2018-06-08 11:40:38 -07001196 static_assert(!kIsTargetBuild || kIsTargetLinux || kIsTargetFuchsia,
1197 "host_dlopen_handles_ will leak handles");
Vladimir Markoc09cd052018-08-23 16:36:36 +01001198 if (reservation != nullptr) {
1199 *error_msg = StringPrintf("dlopen() into reserved memory is unsupported on host for '%s'.",
1200 elf_filename.c_str());
1201 return false;
1202 }
Mathieu Chartierc7d3f4b2016-06-01 10:48:19 -07001203 MutexLock mu(Thread::Current(), *Locks::host_dlopen_handles_lock_);
Richard Uhlera206c742016-05-24 15:04:22 -07001204 dlopen_handle_ = dlopen(absolute_path.get(), RTLD_NOW);
1205 if (dlopen_handle_ != nullptr) {
Richard Uhlera206c742016-05-24 15:04:22 -07001206 if (!host_dlopen_handles_.insert(dlopen_handle_).second) {
1207 dlclose(dlopen_handle_);
1208 dlopen_handle_ = nullptr;
1209 *error_msg = StringPrintf("host dlopen re-opened '%s'", elf_filename.c_str());
1210 return false;
1211 }
1212 }
Bilyan Borisovbb661c02016-04-04 16:27:32 +01001213#endif // ART_TARGET_ANDROID
Andreas Gampe049cff02015-12-01 23:27:12 -08001214 }
1215 if (dlopen_handle_ == nullptr) {
1216 *error_msg = StringPrintf("Failed to dlopen '%s': %s", elf_filename.c_str(), dlerror());
1217 return false;
1218 }
1219 return true;
1220#endif
1221}
1222
1223void DlOpenOatFile::PreSetup(const std::string& elf_filename) {
Andreas Gampe74f07b52015-12-02 11:53:26 -08001224#ifdef __APPLE__
1225 UNUSED(elf_filename);
1226 LOG(FATAL) << "Should not reach here.";
1227 UNREACHABLE();
1228#else
Andreas Gampe049cff02015-12-01 23:27:12 -08001229 struct dl_iterate_context {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001230 static int callback(dl_phdr_info* info, size_t size ATTRIBUTE_UNUSED, void* data) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001231 auto* context = reinterpret_cast<dl_iterate_context*>(data);
Vladimir Markoc09cd052018-08-23 16:36:36 +01001232 static_assert(std::is_same<Elf32_Half, Elf64_Half>::value, "Half must match");
1233 using Elf_Half = Elf64_Half;
1234
Andreas Gampe4075f832016-05-18 13:09:54 -07001235 context->shared_objects_seen++;
1236 if (context->shared_objects_seen < context->shared_objects_before) {
1237 // We haven't been called yet for anything we haven't seen before. Just continue.
1238 // Note: this is aggressively optimistic. If another thread was unloading a library,
1239 // we may miss out here. However, this does not happen often in practice.
1240 return 0;
1241 }
1242
Andreas Gampe049cff02015-12-01 23:27:12 -08001243 // See whether this callback corresponds to the file which we have just loaded.
1244 bool contains_begin = false;
Vladimir Markoc09cd052018-08-23 16:36:36 +01001245 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001246 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1247 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1248 info->dlpi_phdr[i].p_vaddr);
1249 size_t memsz = info->dlpi_phdr[i].p_memsz;
1250 if (vaddr <= context->begin_ && context->begin_ < vaddr + memsz) {
1251 contains_begin = true;
1252 break;
1253 }
1254 }
1255 }
1256 // Add dummy mmaps for this file.
1257 if (contains_begin) {
Vladimir Markoc09cd052018-08-23 16:36:36 +01001258 for (Elf_Half i = 0; i < info->dlpi_phnum; i++) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001259 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
1260 uint8_t* vaddr = reinterpret_cast<uint8_t*>(info->dlpi_addr +
1261 info->dlpi_phdr[i].p_vaddr);
1262 size_t memsz = info->dlpi_phdr[i].p_memsz;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001263 MemMap mmap = MemMap::MapDummy(info->dlpi_name, vaddr, memsz);
1264 context->dlopen_mmaps_->push_back(std::move(mmap));
Andreas Gampe049cff02015-12-01 23:27:12 -08001265 }
1266 }
1267 return 1; // Stop iteration and return 1 from dl_iterate_phdr.
1268 }
1269 return 0; // Continue iteration and return 0 from dl_iterate_phdr when finished.
1270 }
1271 const uint8_t* const begin_;
Vladimir Markoc34bebf2018-08-16 16:12:49 +01001272 std::vector<MemMap>* const dlopen_mmaps_;
Andreas Gampe4075f832016-05-18 13:09:54 -07001273 const size_t shared_objects_before;
1274 size_t shared_objects_seen;
1275 };
1276 dl_iterate_context context = { Begin(), &dlopen_mmaps_, shared_objects_before_, 0};
Andreas Gampe049cff02015-12-01 23:27:12 -08001277
1278 if (dl_iterate_phdr(dl_iterate_context::callback, &context) == 0) {
Andreas Gampe4075f832016-05-18 13:09:54 -07001279 // Hm. Maybe our optimization went wrong. Try another time with shared_objects_before == 0
1280 // before giving up. This should be unusual.
1281 VLOG(oat) << "Need a second run in PreSetup, didn't find with shared_objects_before="
1282 << shared_objects_before_;
1283 dl_iterate_context context0 = { Begin(), &dlopen_mmaps_, 0, 0};
1284 if (dl_iterate_phdr(dl_iterate_context::callback, &context0) == 0) {
1285 // OK, give up and print an error.
Andreas Gampe170331f2017-12-07 18:41:03 -08001286 PrintFileToLog("/proc/self/maps", android::base::LogSeverity::WARNING);
Andreas Gampe4075f832016-05-18 13:09:54 -07001287 LOG(ERROR) << "File " << elf_filename << " loaded with dlopen but cannot find its mmaps.";
1288 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001289 }
Andreas Gampe74f07b52015-12-02 11:53:26 -08001290#endif
Andreas Gampe049cff02015-12-01 23:27:12 -08001291}
1292
1293////////////////////////////////////////////////
1294// OatFile via our own ElfFile implementation //
1295////////////////////////////////////////////////
1296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001297class ElfOatFile final : public OatFileBase {
Andreas Gampe049cff02015-12-01 23:27:12 -08001298 public:
1299 ElfOatFile(const std::string& filename, bool executable) : OatFileBase(filename, executable) {}
1300
Nicolas Geoffray30025092018-04-19 14:43:29 +01001301 static ElfOatFile* OpenElfFile(int zip_fd,
1302 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001303 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001304 bool writable,
1305 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001306 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001307 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001308 /*inout*/MemMap* reservation, // Where to load if not null.
1309 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001310
Nicolas Geoffray30025092018-04-19 14:43:29 +01001311 bool InitializeFromElfFile(int zip_fd,
1312 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001313 VdexFile* vdex_file,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001314 ArrayRef<const std::string> dex_filenames,
Andreas Gampe049cff02015-12-01 23:27:12 -08001315 std::string* error_msg);
1316
1317 protected:
1318 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001319 std::string* error_msg) const override {
Andreas Gampe049cff02015-12-01 23:27:12 -08001320 const uint8_t* ptr = elf_file_->FindDynamicSymbolAddress(symbol_name);
1321 if (ptr == nullptr) {
1322 *error_msg = "(Internal implementation could not find symbol)";
1323 }
1324 return ptr;
1325 }
1326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001327 void PreLoad() override {
Andreas Gampe4075f832016-05-18 13:09:54 -07001328 }
1329
Andreas Gampe049cff02015-12-01 23:27:12 -08001330 bool Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001331 bool writable,
1332 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001333 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001334 /*inout*/MemMap* reservation, // Where to load if not null.
1335 /*out*/std::string* error_msg) override;
Andreas Gampe049cff02015-12-01 23:27:12 -08001336
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001337 bool Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001338 bool writable,
1339 bool executable,
1340 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001341 /*inout*/MemMap* reservation, // Where to load if not null.
1342 /*out*/std::string* error_msg) override;
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001344 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) override {
Andreas Gampe049cff02015-12-01 23:27:12 -08001345 }
1346
1347 private:
1348 bool ElfFileOpen(File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001349 bool writable,
1350 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001351 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001352 /*inout*/MemMap* reservation, // Where to load if not null.
1353 /*out*/std::string* error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001354
1355 private:
1356 // Backing memory map for oat file during cross compilation.
1357 std::unique_ptr<ElfFile> elf_file_;
1358
1359 DISALLOW_COPY_AND_ASSIGN(ElfOatFile);
1360};
1361
Nicolas Geoffray30025092018-04-19 14:43:29 +01001362ElfOatFile* ElfOatFile::OpenElfFile(int zip_fd,
1363 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001364 const std::string& location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001365 bool writable,
1366 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001367 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001368 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001369 /*inout*/MemMap* reservation, // Where to load if not null.
1370 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001371 ScopedTrace trace("Open elf file " + location);
Andreas Gampe049cff02015-12-01 23:27:12 -08001372 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, executable));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001373 bool success = oat_file->ElfFileOpen(file,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001374 writable,
1375 low_4gb,
1376 executable,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001377 reservation,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001378 error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001379 if (!success) {
1380 CHECK(!error_msg->empty());
1381 return nullptr;
1382 }
1383
1384 // Complete the setup.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001385 if (!oat_file->ComputeFields(file->GetPath(), error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001386 return nullptr;
1387 }
1388
Vladimir Markob7bf8432019-12-03 13:18:50 +00001389 if (!oat_file->Setup(zip_fd, dex_filenames, error_msg)) {
Andreas Gampe049cff02015-12-01 23:27:12 -08001390 return nullptr;
1391 }
1392
1393 return oat_file.release();
1394}
1395
Nicolas Geoffray30025092018-04-19 14:43:29 +01001396bool ElfOatFile::InitializeFromElfFile(int zip_fd,
1397 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001398 VdexFile* vdex_file,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001399 ArrayRef<const std::string> dex_filenames,
Andreas Gampe049cff02015-12-01 23:27:12 -08001400 std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001401 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001402 if (IsExecutable()) {
1403 *error_msg = "Cannot initialize from elf file in executable mode.";
1404 return false;
1405 }
1406 elf_file_.reset(elf_file);
David Brazdilc93b3be2016-09-12 18:49:58 +01001407 SetVdex(vdex_file);
Andreas Gampe049cff02015-12-01 23:27:12 -08001408 uint64_t offset, size;
1409 bool has_section = elf_file->GetSectionOffsetAndSize(".rodata", &offset, &size);
1410 CHECK(has_section);
1411 SetBegin(elf_file->Begin() + offset);
1412 SetEnd(elf_file->Begin() + size + offset);
1413 // Ignore the optional .bss section when opening non-executable.
Vladimir Markob7bf8432019-12-03 13:18:50 +00001414 return Setup(zip_fd, dex_filenames, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001415}
1416
1417bool ElfOatFile::Load(const std::string& elf_filename,
Andreas Gampe049cff02015-12-01 23:27:12 -08001418 bool writable,
1419 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001420 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001421 /*inout*/MemMap* reservation,
1422 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001423 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001424 std::unique_ptr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
1425 if (file == nullptr) {
1426 *error_msg = StringPrintf("Failed to open oat filename for reading: %s", strerror(errno));
1427 return false;
1428 }
1429 return ElfOatFile::ElfFileOpen(file.get(),
Andreas Gampe049cff02015-12-01 23:27:12 -08001430 writable,
1431 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001432 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001433 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001434 error_msg);
1435}
1436
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001437bool ElfOatFile::Load(int oat_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001438 bool writable,
1439 bool executable,
1440 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001441 /*inout*/MemMap* reservation,
1442 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001443 ScopedTrace trace(__PRETTY_FUNCTION__);
1444 if (oat_fd != -1) {
Josh Gaoafeec9f2018-08-30 14:05:56 -07001445 int duped_fd = DupCloexec(oat_fd);
1446 std::unique_ptr<File> file = std::make_unique<File>(duped_fd, false);
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001447 if (file == nullptr) {
1448 *error_msg = StringPrintf("Failed to open oat filename for reading: %s",
1449 strerror(errno));
1450 return false;
1451 }
1452 return ElfOatFile::ElfFileOpen(file.get(),
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001453 writable,
1454 executable,
1455 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001456 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001457 error_msg);
1458 }
1459 return false;
1460}
1461
Andreas Gampe049cff02015-12-01 23:27:12 -08001462bool ElfOatFile::ElfFileOpen(File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001463 bool writable,
1464 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001465 bool low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001466 /*inout*/MemMap* reservation,
1467 /*out*/std::string* error_msg) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001468 ScopedTrace trace(__PRETTY_FUNCTION__);
Andreas Gampe049cff02015-12-01 23:27:12 -08001469 elf_file_.reset(ElfFile::Open(file,
1470 writable,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001471 /*program_header_only=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001472 low_4gb,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001473 error_msg));
Andreas Gampe049cff02015-12-01 23:27:12 -08001474 if (elf_file_ == nullptr) {
1475 DCHECK(!error_msg->empty());
1476 return false;
1477 }
Vladimir Markoc09cd052018-08-23 16:36:36 +01001478 bool loaded = elf_file_->Load(file, executable, low_4gb, reservation, error_msg);
Andreas Gampe049cff02015-12-01 23:27:12 -08001479 DCHECK(loaded || !error_msg->empty());
1480 return loaded;
1481}
1482
David Brazdil7126c5b2019-03-05 00:02:51 +00001483class OatFileBackedByVdex final : public OatFileBase {
1484 public:
1485 explicit OatFileBackedByVdex(const std::string& filename)
1486 : OatFileBase(filename, /*executable=*/ false) {}
1487
1488 static OatFileBackedByVdex* Open(const std::vector<const DexFile*>& dex_files,
1489 std::unique_ptr<VdexFile>&& vdex_file,
1490 const std::string& location) {
1491 std::unique_ptr<OatFileBackedByVdex> oat_file(new OatFileBackedByVdex(location));
1492 oat_file->Initialize(dex_files, std::move(vdex_file));
1493 return oat_file.release();
1494 }
1495
1496 void Initialize(const std::vector<const DexFile*>& dex_files,
1497 std::unique_ptr<VdexFile>&& vdex_file) {
1498 DCHECK(!IsExecutable());
1499
1500 // SetVdex will take ownership of the VdexFile.
1501 SetVdex(vdex_file.release());
1502
Nicolas Geoffraydee09f92019-10-23 15:18:20 +01001503 // Create a dummy OatHeader with a key store containing only the compiler
1504 // filter (it helps debugging and is required by
1505 // OatHeader::GetCompilerFilter).
David Brazdil7126c5b2019-03-05 00:02:51 +00001506 std::unique_ptr<const InstructionSetFeatures> isa_features =
1507 InstructionSetFeatures::FromCppDefines();
Nicolas Geoffraydee09f92019-10-23 15:18:20 +01001508 SafeMap<std::string, std::string> store;
1509 store.Put(OatHeader::kCompilerFilter, CompilerFilter::NameOfFilter(CompilerFilter::kVerify));
David Brazdil7126c5b2019-03-05 00:02:51 +00001510 oat_header_.reset(OatHeader::Create(kRuntimeISA,
1511 isa_features.get(),
1512 dex_files.size(),
Nicolas Geoffraydee09f92019-10-23 15:18:20 +01001513 &store));
David Brazdil7126c5b2019-03-05 00:02:51 +00001514 const uint8_t* begin = reinterpret_cast<const uint8_t*>(oat_header_.get());
1515 SetBegin(begin);
1516 SetEnd(begin + oat_header_->GetHeaderSize());
1517
1518 // Load VerifierDeps from VDEX and copy bit vectors of verified classes.
1519 ArrayRef<const uint8_t> deps_data = GetVdexFile()->GetVerifierDepsData();
1520 verified_classes_per_dex_ = verifier::VerifierDeps::ParseVerifiedClasses(dex_files, deps_data);
1521
1522 // Initialize OatDexFiles.
1523 Setup(dex_files);
1524 }
1525
1526 bool IsClassVerifiedInVdex(const OatDexFile& oat_dex_file, uint16_t class_def_index) const {
1527 // Determine the index of the DexFile, assuming the order of OatDexFiles
1528 // in `oat_dex_files_storage_` is the same.
1529 const std::vector<const OatDexFile*>& oat_dex_files = GetOatDexFiles();
1530 auto oat_dex_file_it = std::find(oat_dex_files.begin(), oat_dex_files.end(), &oat_dex_file);
1531 DCHECK(oat_dex_file_it != oat_dex_files.end());
1532 size_t dex_index = oat_dex_file_it - oat_dex_files.begin();
1533 // Check the bitvector of verified classes from the vdex.
1534 return verified_classes_per_dex_[dex_index][class_def_index];
1535 }
1536
1537 protected:
1538 void PreLoad() override {}
1539
1540 bool Load(const std::string& elf_filename ATTRIBUTE_UNUSED,
1541 bool writable ATTRIBUTE_UNUSED,
1542 bool executable ATTRIBUTE_UNUSED,
1543 bool low_4gb ATTRIBUTE_UNUSED,
1544 MemMap* reservation ATTRIBUTE_UNUSED,
1545 std::string* error_msg ATTRIBUTE_UNUSED) override {
1546 LOG(FATAL) << "Unsupported";
1547 UNREACHABLE();
1548 }
1549
1550 bool Load(int oat_fd ATTRIBUTE_UNUSED,
1551 bool writable ATTRIBUTE_UNUSED,
1552 bool executable ATTRIBUTE_UNUSED,
1553 bool low_4gb ATTRIBUTE_UNUSED,
1554 MemMap* reservation ATTRIBUTE_UNUSED,
1555 std::string* error_msg ATTRIBUTE_UNUSED) override {
1556 LOG(FATAL) << "Unsupported";
1557 UNREACHABLE();
1558 }
1559
1560 void PreSetup(const std::string& elf_filename ATTRIBUTE_UNUSED) override {}
1561
1562 const uint8_t* FindDynamicSymbolAddress(const std::string& symbol_name ATTRIBUTE_UNUSED,
1563 std::string* error_msg) const override {
1564 *error_msg = "Unsupported";
1565 return nullptr;
1566 }
1567
1568 private:
1569 std::unique_ptr<OatHeader> oat_header_;
1570 std::vector<std::vector<bool>> verified_classes_per_dex_;
1571
1572 DISALLOW_COPY_AND_ASSIGN(OatFileBackedByVdex);
1573};
1574
Andreas Gampe049cff02015-12-01 23:27:12 -08001575//////////////////////////
1576// General OatFile code //
1577//////////////////////////
1578
Andreas Gampe049cff02015-12-01 23:27:12 -08001579static void CheckLocation(const std::string& location) {
1580 CHECK(!location.empty());
1581}
1582
Nicolas Geoffray30025092018-04-19 14:43:29 +01001583OatFile* OatFile::OpenWithElfFile(int zip_fd,
1584 ElfFile* elf_file,
David Brazdilc93b3be2016-09-12 18:49:58 +01001585 VdexFile* vdex_file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001586 const std::string& location,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001587 ArrayRef<const std::string> dex_filenames,
Andreas Gampe049cff02015-12-01 23:27:12 -08001588 std::string* error_msg) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001589 std::unique_ptr<ElfOatFile> oat_file(new ElfOatFile(location, /*executable=*/ false));
Vladimir Markob7bf8432019-12-03 13:18:50 +00001590 return oat_file->InitializeFromElfFile(zip_fd, elf_file, vdex_file, dex_filenames, error_msg)
Andreas Gampe049cff02015-12-01 23:27:12 -08001591 ? oat_file.release()
1592 : nullptr;
1593}
1594
Nicolas Geoffray30025092018-04-19 14:43:29 +01001595OatFile* OatFile::Open(int zip_fd,
1596 const std::string& oat_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001597 const std::string& oat_location,
Andreas Gampe049cff02015-12-01 23:27:12 -08001598 bool executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001599 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001600 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001601 /*inout*/MemMap* reservation,
1602 /*out*/std::string* error_msg) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001603 ScopedTrace trace("Open oat file " + oat_location);
1604 CHECK(!oat_filename.empty()) << oat_location;
1605 CheckLocation(oat_location);
Andreas Gampe54315c72016-05-18 21:10:42 -07001606
Calin Juravle367b9d82017-05-15 18:18:39 -07001607 std::string vdex_filename = GetVdexFilename(oat_filename);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001608
1609 // Check that the files even exist, fast-fail.
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001610 if (!OS::FileExists(vdex_filename.c_str())) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001611 *error_msg = StringPrintf("File %s does not exist.", vdex_filename.c_str());
1612 return nullptr;
1613 } else if (!OS::FileExists(oat_filename.c_str())) {
1614 *error_msg = StringPrintf("File %s does not exist.", oat_filename.c_str());
Andreas Gampe54315c72016-05-18 21:10:42 -07001615 return nullptr;
1616 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001617
1618 // Try dlopen first, as it is required for native debuggability. This will fail fast if dlopen is
1619 // disabled.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001620 OatFile* with_dlopen = OatFileBase::OpenOatFile<DlOpenOatFile>(zip_fd,
1621 vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001622 oat_filename,
1623 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001624 /*writable=*/ false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001625 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001626 low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001627 dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001628 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001629 error_msg);
1630 if (with_dlopen != nullptr) {
1631 return with_dlopen;
1632 }
1633 if (kPrintDlOpenErrorMessage) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001634 LOG(ERROR) << "Failed to dlopen: " << oat_filename << " with error " << *error_msg;
Andreas Gampe049cff02015-12-01 23:27:12 -08001635 }
Andreas Gampe049cff02015-12-01 23:27:12 -08001636 // If we aren't trying to execute, we just use our own ElfFile loader for a couple reasons:
1637 //
1638 // On target, dlopen may fail when compiling due to selinux restrictions on installd.
1639 //
1640 // We use our own ELF loader for Quick to deal with legacy apps that
1641 // open a generated dex file by name, remove the file, then open
1642 // another generated dex file with the same name. http://b/10614658
1643 //
1644 // On host, dlopen is expected to fail when cross compiling, so fall back to OpenElfFile.
1645 //
1646 //
1647 // Another independent reason is the absolute placement of boot.oat. dlopen on the host usually
1648 // does honor the virtual address encoded in the ELF file only for ET_EXEC files, not ET_DYN.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001649 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd,
1650 vdex_filename,
David Brazdil7b49e6c2016-09-01 11:06:18 +01001651 oat_filename,
1652 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001653 /*writable=*/ false,
Andreas Gampe049cff02015-12-01 23:27:12 -08001654 executable,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001655 low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001656 dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001657 reservation,
Andreas Gampe049cff02015-12-01 23:27:12 -08001658 error_msg);
1659 return with_internal;
1660}
1661
Nicolas Geoffray30025092018-04-19 14:43:29 +01001662OatFile* OatFile::Open(int zip_fd,
1663 int vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001664 int oat_fd,
1665 const std::string& oat_location,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001666 bool executable,
1667 bool low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001668 ArrayRef<const std::string> dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001669 /*inout*/MemMap* reservation,
1670 /*out*/std::string* error_msg) {
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001671 CHECK(!oat_location.empty()) << oat_location;
1672
1673 std::string vdex_location = GetVdexFilename(oat_location);
1674
Nicolas Geoffray30025092018-04-19 14:43:29 +01001675 OatFile* with_internal = OatFileBase::OpenOatFile<ElfOatFile>(zip_fd,
1676 vdex_fd,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001677 oat_fd,
1678 vdex_location,
1679 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001680 /*writable=*/ false,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001681 executable,
1682 low_4gb,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001683 dex_filenames,
Vladimir Markoc09cd052018-08-23 16:36:36 +01001684 reservation,
Shubham Ajmerab22dea02017-10-04 18:36:41 -07001685 error_msg);
1686 return with_internal;
1687}
1688
Nicolas Geoffray30025092018-04-19 14:43:29 +01001689OatFile* OatFile::OpenWritable(int zip_fd,
1690 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001691 const std::string& location,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001692 ArrayRef<const std::string> dex_filenames,
Andreas Gampe049cff02015-12-01 23:27:12 -08001693 std::string* error_msg) {
1694 CheckLocation(location);
Nicolas Geoffray30025092018-04-19 14:43:29 +01001695 return ElfOatFile::OpenElfFile(zip_fd,
1696 file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001697 location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001698 /*writable=*/ true,
1699 /*executable=*/ false,
1700 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001701 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001702 /*reservation=*/ nullptr,
Andreas Gampe049cff02015-12-01 23:27:12 -08001703 error_msg);
1704}
1705
Nicolas Geoffray30025092018-04-19 14:43:29 +01001706OatFile* OatFile::OpenReadable(int zip_fd,
1707 File* file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001708 const std::string& location,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001709 ArrayRef<const std::string> dex_filenames,
Andreas Gampe049cff02015-12-01 23:27:12 -08001710 std::string* error_msg) {
1711 CheckLocation(location);
Nicolas Geoffray30025092018-04-19 14:43:29 +01001712 return ElfOatFile::OpenElfFile(zip_fd,
1713 file,
Andreas Gampe049cff02015-12-01 23:27:12 -08001714 location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001715 /*writable=*/ false,
1716 /*executable=*/ false,
1717 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00001718 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001719 /*reservation=*/ nullptr,
Andreas Gampe049cff02015-12-01 23:27:12 -08001720 error_msg);
1721}
1722
David Brazdil7126c5b2019-03-05 00:02:51 +00001723OatFile* OatFile::OpenFromVdex(const std::vector<const DexFile*>& dex_files,
1724 std::unique_ptr<VdexFile>&& vdex_file,
1725 const std::string& location) {
1726 CheckLocation(location);
1727 return OatFileBackedByVdex::Open(dex_files, std::move(vdex_file), location);
1728}
1729
Andreas Gampe049cff02015-12-01 23:27:12 -08001730OatFile::OatFile(const std::string& location, bool is_executable)
1731 : location_(location),
David Brazdil7b49e6c2016-09-01 11:06:18 +01001732 vdex_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001733 begin_(nullptr),
1734 end_(nullptr),
Vladimir Markob066d432018-01-03 13:14:37 +00001735 data_bimg_rel_ro_begin_(nullptr),
1736 data_bimg_rel_ro_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001737 bss_begin_(nullptr),
1738 bss_end_(nullptr),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001739 bss_methods_(nullptr),
Vladimir Markoaad75c62016-10-03 08:46:48 +00001740 bss_roots_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001741 is_executable_(is_executable),
David Srbeckyec2cdf42017-12-08 16:21:25 +00001742 vdex_begin_(nullptr),
1743 vdex_end_(nullptr),
Andreas Gampe049cff02015-12-01 23:27:12 -08001744 secondary_lookup_lock_("OatFile secondary lookup lock", kOatFileSecondaryLookupLock) {
1745 CHECK(!location_.empty());
1746}
1747
1748OatFile::~OatFile() {
1749 STLDeleteElements(&oat_dex_files_storage_);
1750}
1751
Brian Carlstrome24fa612011-09-29 00:53:55 -07001752const OatHeader& OatFile::GetOatHeader() const {
Ian Rogers30fab402012-01-23 15:43:46 -08001753 return *reinterpret_cast<const OatHeader*>(Begin());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001754}
1755
Ian Rogers13735952014-10-08 12:43:28 -07001756const uint8_t* OatFile::Begin() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001757 CHECK(begin_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001758 return begin_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001759}
1760
Ian Rogers13735952014-10-08 12:43:28 -07001761const uint8_t* OatFile::End() const {
Andreas Gampefa8429b2015-04-07 18:34:42 -07001762 CHECK(end_ != nullptr);
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001763 return end_;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001764}
1765
David Brazdil7b49e6c2016-09-01 11:06:18 +01001766const uint8_t* OatFile::DexBegin() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001767 return vdex_->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001768}
1769
1770const uint8_t* OatFile::DexEnd() const {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001771 return vdex_->End();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001772}
1773
Vladimir Markob066d432018-01-03 13:14:37 +00001774ArrayRef<const uint32_t> OatFile::GetBootImageRelocations() const {
1775 if (data_bimg_rel_ro_begin_ != nullptr) {
1776 const uint32_t* relocations = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_begin_);
1777 const uint32_t* relocations_end = reinterpret_cast<const uint32_t*>(data_bimg_rel_ro_end_);
1778 return ArrayRef<const uint32_t>(relocations, relocations_end - relocations);
1779 } else {
1780 return ArrayRef<const uint32_t>();
1781 }
1782}
1783
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001784ArrayRef<ArtMethod*> OatFile::GetBssMethods() const {
1785 if (bss_methods_ != nullptr) {
1786 ArtMethod** methods = reinterpret_cast<ArtMethod**>(bss_methods_);
1787 ArtMethod** methods_end =
1788 reinterpret_cast<ArtMethod**>(bss_roots_ != nullptr ? bss_roots_ : bss_end_);
1789 return ArrayRef<ArtMethod*>(methods, methods_end - methods);
1790 } else {
1791 return ArrayRef<ArtMethod*>();
1792 }
1793}
1794
Vladimir Markoaad75c62016-10-03 08:46:48 +00001795ArrayRef<GcRoot<mirror::Object>> OatFile::GetBssGcRoots() const {
1796 if (bss_roots_ != nullptr) {
1797 auto* roots = reinterpret_cast<GcRoot<mirror::Object>*>(bss_roots_);
1798 auto* roots_end = reinterpret_cast<GcRoot<mirror::Object>*>(bss_end_);
1799 return ArrayRef<GcRoot<mirror::Object>>(roots, roots_end - roots);
1800 } else {
1801 return ArrayRef<GcRoot<mirror::Object>>();
1802 }
1803}
1804
Andreas Gampeb40d3612018-06-26 15:49:42 -07001805const OatDexFile* OatFile::GetOatDexFile(const char* dex_location,
1806 const uint32_t* dex_location_checksum,
1807 std::string* error_msg) const {
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001808 // NOTE: We assume here that the canonical location for a given dex_location never
1809 // changes. If it does (i.e. some symlink used by the filename changes) we may return
1810 // an incorrect OatDexFile. As long as we have a checksum to check, we shall return
1811 // an identical file or fail; otherwise we may see some unpredictable failures.
Calin Juravle4e1d5792014-07-15 23:56:47 +01001812
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001813 // TODO: Additional analysis of usage patterns to see if this can be simplified
1814 // without any performance loss, for example by not doing the first lock-free lookup.
1815
Andreas Gampeb40d3612018-06-26 15:49:42 -07001816 const OatDexFile* oat_dex_file = nullptr;
Vladimir Marko59ae4f92019-02-04 14:06:02 +00001817 std::string_view key(dex_location);
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001818 // Try to find the key cheaply in the oat_dex_files_ map which holds dex locations
1819 // directly mentioned in the oat file and doesn't require locking.
1820 auto primary_it = oat_dex_files_.find(key);
1821 if (primary_it != oat_dex_files_.end()) {
1822 oat_dex_file = primary_it->second;
1823 DCHECK(oat_dex_file != nullptr);
1824 } else {
1825 // This dex_location is not one of the dex locations directly mentioned in the
1826 // oat file. The correct lookup is via the canonical location but first see in
1827 // the secondary_oat_dex_files_ whether we've looked up this location before.
1828 MutexLock mu(Thread::Current(), secondary_lookup_lock_);
1829 auto secondary_lb = secondary_oat_dex_files_.lower_bound(key);
1830 if (secondary_lb != secondary_oat_dex_files_.end() && key == secondary_lb->first) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001831 oat_dex_file = secondary_lb->second; // May be null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001832 } else {
1833 // We haven't seen this dex_location before, we must check the canonical location.
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001834 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001835 if (dex_canonical_location != dex_location) {
Vladimir Marko59ae4f92019-02-04 14:06:02 +00001836 std::string_view canonical_key(dex_canonical_location);
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001837 auto canonical_it = oat_dex_files_.find(canonical_key);
1838 if (canonical_it != oat_dex_files_.end()) {
1839 oat_dex_file = canonical_it->second;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001840 } // else keep null.
1841 } // else keep null.
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001842
1843 // Copy the key to the string_cache_ and store the result in secondary map.
1844 string_cache_.emplace_back(key.data(), key.length());
Vladimir Marko59ae4f92019-02-04 14:06:02 +00001845 std::string_view key_copy(string_cache_.back());
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001846 secondary_oat_dex_files_.PutBefore(secondary_lb, key_copy, oat_dex_file);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001847 }
1848 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001849
1850 if (oat_dex_file == nullptr) {
1851 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001852 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001853 *error_msg = "Failed to find OatDexFile for DexFile " + std::string(dex_location)
1854 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation();
1855 }
1856 return nullptr;
Vladimir Marko3f5838d2014-08-07 18:07:18 +01001857 }
Brian Carlstrom756ee4e2013-10-03 15:46:12 -07001858
Richard Uhler9a37efc2016-08-05 16:32:55 -07001859 if (dex_location_checksum != nullptr &&
1860 oat_dex_file->GetDexFileLocationChecksum() != *dex_location_checksum) {
1861 if (error_msg != nullptr) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001862 std::string dex_canonical_location = DexFileLoader::GetDexCanonicalLocation(dex_location);
Richard Uhler9a37efc2016-08-05 16:32:55 -07001863 std::string checksum = StringPrintf("0x%08x", oat_dex_file->GetDexFileLocationChecksum());
1864 std::string required_checksum = StringPrintf("0x%08x", *dex_location_checksum);
1865 *error_msg = "OatDexFile for DexFile " + std::string(dex_location)
1866 + " (canonical path " + dex_canonical_location + ") in OatFile " + GetLocation()
1867 + " has checksum " + checksum + " but " + required_checksum + " was required";
Brian Carlstrom0d6adac2014-02-05 17:39:16 -08001868 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001869 return nullptr;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001870 }
Richard Uhler9a37efc2016-08-05 16:32:55 -07001871 return oat_dex_file;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001872}
1873
Andreas Gampeb40d3612018-06-26 15:49:42 -07001874OatDexFile::OatDexFile(const OatFile* oat_file,
1875 const std::string& dex_file_location,
1876 const std::string& canonical_dex_file_location,
1877 uint32_t dex_file_location_checksum,
1878 const uint8_t* dex_file_pointer,
1879 const uint8_t* lookup_table_data,
1880 const IndexBssMapping* method_bss_mapping_data,
1881 const IndexBssMapping* type_bss_mapping_data,
1882 const IndexBssMapping* string_bss_mapping_data,
1883 const uint32_t* oat_class_offsets_pointer,
1884 const DexLayoutSections* dex_layout_sections)
Brian Carlstrome24fa612011-09-29 00:53:55 -07001885 : oat_file_(oat_file),
1886 dex_file_location_(dex_file_location),
Vladimir Markoaa4497d2014-09-05 14:01:17 +01001887 canonical_dex_file_location_(canonical_dex_file_location),
Brian Carlstrom5b332c82012-02-01 15:02:31 -08001888 dex_file_location_checksum_(dex_file_location_checksum),
Brian Carlstrom89521892011-12-07 22:05:07 -08001889 dex_file_pointer_(dex_file_pointer),
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001890 lookup_table_data_(lookup_table_data),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001891 method_bss_mapping_(method_bss_mapping_data),
Vladimir Markof3c52b42017-11-17 17:32:12 +00001892 type_bss_mapping_(type_bss_mapping_data),
1893 string_bss_mapping_(string_bss_mapping_data),
Vladimir Marko09d09432015-09-08 13:47:48 +01001894 oat_class_offsets_pointer_(oat_class_offsets_pointer),
Vladimir Markoea341d22018-05-11 10:33:37 +01001895 lookup_table_(),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001896 dex_layout_sections_(dex_layout_sections) {
David Sehr9aa352e2016-09-15 18:13:52 -07001897 // Initialize TypeLookupTable.
1898 if (lookup_table_data_ != nullptr) {
1899 // Peek the number of classes from the DexFile.
1900 const DexFile::Header* dex_header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer_);
1901 const uint32_t num_class_defs = dex_header->class_defs_size_;
1902 if (lookup_table_data_ + TypeLookupTable::RawDataLength(num_class_defs) > GetOatFile()->End()) {
1903 LOG(WARNING) << "found truncated lookup table in " << dex_file_location_;
1904 } else {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001905 const uint8_t* dex_data = dex_file_pointer_;
1906 // TODO: Clean this up to create the type lookup table after the dex file has been created?
1907 if (CompactDexFile::IsMagicValid(dex_header->magic_)) {
1908 dex_data += dex_header->data_off_;
1909 }
1910 lookup_table_ = TypeLookupTable::Open(dex_data, lookup_table_data_, num_class_defs);
David Sehr9aa352e2016-09-15 18:13:52 -07001911 }
1912 }
David Brazdil7126c5b2019-03-05 00:02:51 +00001913 DCHECK(!IsBackedByVdexOnly());
1914}
1915
1916OatDexFile::OatDexFile(const OatFile* oat_file,
1917 const DexFile* dex_file,
1918 const std::string& dex_file_location,
1919 const std::string& canonical_dex_file_location)
1920 : oat_file_(oat_file),
1921 dex_file_location_(dex_file_location),
1922 canonical_dex_file_location_(canonical_dex_file_location),
1923 dex_file_location_checksum_(dex_file->GetLocationChecksum()),
1924 dex_file_pointer_(reinterpret_cast<const uint8_t*>(dex_file)) {
1925 dex_file->SetOatDexFile(this);
1926 DCHECK(IsBackedByVdexOnly());
David Sehr9aa352e2016-09-15 18:13:52 -07001927}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001928
Andreas Gampe875b4f22018-11-19 12:59:15 -08001929OatDexFile::OatDexFile(TypeLookupTable&& lookup_table) : lookup_table_(std::move(lookup_table)) {
David Srbeckyb5649f92019-05-14 15:27:52 +01001930 // Stripped-down OatDexFile only allowed in the compiler, the zygote, or the system server.
Nicolas Geoffraydc2fbb62019-04-11 22:55:50 +01001931 CHECK(Runtime::Current() == nullptr ||
1932 Runtime::Current()->IsAotCompiler() ||
David Srbeckyb5649f92019-05-14 15:27:52 +01001933 Runtime::Current()->IsZygote() ||
1934 Runtime::Current()->IsSystemServer());
Andreas Gampe875b4f22018-11-19 12:59:15 -08001935}
Mathieu Chartier1b868492016-11-16 16:22:37 -08001936
Andreas Gampeb40d3612018-06-26 15:49:42 -07001937OatDexFile::~OatDexFile() {}
Brian Carlstrome24fa612011-09-29 00:53:55 -07001938
Andreas Gampeb40d3612018-06-26 15:49:42 -07001939size_t OatDexFile::FileSize() const {
Andreas Gampe875b4f22018-11-19 12:59:15 -08001940 DCHECK(dex_file_pointer_ != nullptr);
Ian Rogers05f28c62012-10-23 18:12:13 -07001941 return reinterpret_cast<const DexFile::Header*>(dex_file_pointer_)->file_size_;
1942}
1943
Andreas Gampeb40d3612018-06-26 15:49:42 -07001944std::unique_ptr<const DexFile> OatDexFile::OpenDexFile(std::string* error_msg) const {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001945 ScopedTrace trace(__PRETTY_FUNCTION__);
Aart Bik37d6a3b2016-06-21 18:30:10 -07001946 static constexpr bool kVerify = false;
1947 static constexpr bool kVerifyChecksum = false;
David Sehr013fd802018-01-11 22:55:24 -08001948 const ArtDexFileLoader dex_file_loader;
1949 return dex_file_loader.Open(dex_file_pointer_,
1950 FileSize(),
1951 dex_file_location_,
1952 dex_file_location_checksum_,
1953 this,
1954 kVerify,
1955 kVerifyChecksum,
1956 error_msg);
Brian Carlstrom89521892011-12-07 22:05:07 -08001957}
1958
Andreas Gampeb40d3612018-06-26 15:49:42 -07001959uint32_t OatDexFile::GetOatClassOffset(uint16_t class_def_index) const {
Andreas Gampe875b4f22018-11-19 12:59:15 -08001960 DCHECK(oat_class_offsets_pointer_ != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001961 return oat_class_offsets_pointer_[class_def_index];
1962}
1963
David Brazdil7126c5b2019-03-05 00:02:51 +00001964bool OatDexFile::IsBackedByVdexOnly() const {
1965 return oat_class_offsets_pointer_ == nullptr;
1966}
1967
Andreas Gampeb40d3612018-06-26 15:49:42 -07001968OatFile::OatClass OatDexFile::GetOatClass(uint16_t class_def_index) const {
David Brazdil7126c5b2019-03-05 00:02:51 +00001969 // If this is an OatFileBackedByVdex, initialize the OatClass using the vdex's VerifierDeps.
1970 if (IsBackedByVdexOnly()) {
1971 bool is_vdex_verified = down_cast<const OatFileBackedByVdex*>(oat_file_)->IsClassVerifiedInVdex(
1972 *this,
1973 class_def_index);
1974 return OatFile::OatClass(oat_file_,
1975 is_vdex_verified ? ClassStatus::kVerified : ClassStatus::kNotReady,
1976 /* type= */ kOatClassNoneCompiled,
1977 /* bitmap_size= */ 0u,
1978 /* bitmap_pointer= */ nullptr,
1979 /* methods_pointer= */ nullptr);
1980 }
1981
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001982 uint32_t oat_class_offset = GetOatClassOffset(class_def_index);
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001983
Ian Rogers13735952014-10-08 12:43:28 -07001984 const uint8_t* oat_class_pointer = oat_file_->Begin() + oat_class_offset;
Brian Carlstrom7571e8b2013-08-12 17:04:14 -07001985 CHECK_LT(oat_class_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstrom0755ec52012-01-11 15:19:46 -08001986
Ian Rogers13735952014-10-08 12:43:28 -07001987 const uint8_t* status_pointer = oat_class_pointer;
Brian Carlstromba150c32013-08-27 17:31:03 -07001988 CHECK_LT(status_pointer, oat_file_->End()) << oat_file_->GetLocation();
Vladimir Marko2c64a832018-01-04 11:31:56 +00001989 ClassStatus status = enum_cast<ClassStatus>(*reinterpret_cast<const int16_t*>(status_pointer));
1990 CHECK_LE(status, ClassStatus::kLast);
Brian Carlstromba150c32013-08-27 17:31:03 -07001991
Ian Rogers13735952014-10-08 12:43:28 -07001992 const uint8_t* type_pointer = status_pointer + sizeof(uint16_t);
Brian Carlstromba150c32013-08-27 17:31:03 -07001993 CHECK_LT(type_pointer, oat_file_->End()) << oat_file_->GetLocation();
1994 OatClassType type = static_cast<OatClassType>(*reinterpret_cast<const uint16_t*>(type_pointer));
1995 CHECK_LT(type, kOatClassMax);
1996
Ian Rogers13735952014-10-08 12:43:28 -07001997 const uint8_t* after_type_pointer = type_pointer + sizeof(int16_t);
Brian Carlstromcd937a92014-03-04 22:53:23 -08001998 CHECK_LE(after_type_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromba150c32013-08-27 17:31:03 -07001999
Brian Carlstromcd937a92014-03-04 22:53:23 -08002000 uint32_t bitmap_size = 0;
Ian Rogers13735952014-10-08 12:43:28 -07002001 const uint8_t* bitmap_pointer = nullptr;
2002 const uint8_t* methods_pointer = nullptr;
Ian Rogers97b52f82014-08-14 11:34:07 -07002003 if (type != kOatClassNoneCompiled) {
2004 if (type == kOatClassSomeCompiled) {
2005 bitmap_size = static_cast<uint32_t>(*reinterpret_cast<const uint32_t*>(after_type_pointer));
2006 bitmap_pointer = after_type_pointer + sizeof(bitmap_size);
2007 CHECK_LE(bitmap_pointer, oat_file_->End()) << oat_file_->GetLocation();
2008 methods_pointer = bitmap_pointer + bitmap_size;
2009 } else {
2010 methods_pointer = after_type_pointer;
2011 }
2012 CHECK_LE(methods_pointer, oat_file_->End()) << oat_file_->GetLocation();
Brian Carlstromcd937a92014-03-04 22:53:23 -08002013 }
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002014
Richard Uhler07b3c232015-03-31 15:57:54 -07002015 return OatFile::OatClass(oat_file_,
2016 status,
2017 type,
2018 bitmap_size,
2019 reinterpret_cast<const uint32_t*>(bitmap_pointer),
2020 reinterpret_cast<const OatMethodOffsets*>(methods_pointer));
Brian Carlstrome24fa612011-09-29 00:53:55 -07002021}
2022
Hans Boehm206348c2018-12-05 11:11:33 -08002023ArrayRef<const uint8_t> OatDexFile::GetQuickenedInfoOf(const DexFile& dex_file,
2024 uint32_t dex_method_idx) const {
2025 const OatFile* oat_file = GetOatFile();
2026 if (oat_file == nullptr) {
2027 return ArrayRef<const uint8_t>();
2028 } else {
2029 return oat_file->GetVdexFile()->GetQuickenedInfoOf(dex_file, dex_method_idx);
2030 }
2031}
2032
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002033const dex::ClassDef* OatDexFile::FindClassDef(const DexFile& dex_file,
2034 const char* descriptor,
2035 size_t hash) {
Andreas Gampeb40d3612018-06-26 15:49:42 -07002036 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
David Sehr9aa352e2016-09-15 18:13:52 -07002037 DCHECK_EQ(ComputeModifiedUtf8Hash(descriptor), hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08002038 bool used_lookup_table = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002039 const dex::ClassDef* lookup_table_classdef = nullptr;
Vladimir Markoea341d22018-05-11 10:33:37 +01002040 if (LIKELY((oat_dex_file != nullptr) && oat_dex_file->GetTypeLookupTable().Valid())) {
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08002041 used_lookup_table = true;
Vladimir Markoea341d22018-05-11 10:33:37 +01002042 const uint32_t class_def_idx = oat_dex_file->GetTypeLookupTable().Lookup(descriptor, hash);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08002043 lookup_table_classdef = (class_def_idx != dex::kDexNoIndex)
2044 ? &dex_file.GetClassDef(class_def_idx)
2045 : nullptr;
2046 if (!kIsDebugBuild) {
2047 return lookup_table_classdef;
2048 }
David Sehr9aa352e2016-09-15 18:13:52 -07002049 }
2050 // Fast path for rare no class defs case.
2051 const uint32_t num_class_defs = dex_file.NumClassDefs();
2052 if (num_class_defs == 0) {
Vladimir Markoea341d22018-05-11 10:33:37 +01002053 DCHECK(!used_lookup_table);
David Sehr9aa352e2016-09-15 18:13:52 -07002054 return nullptr;
2055 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002056 const dex::TypeId* type_id = dex_file.FindTypeId(descriptor);
David Sehr9aa352e2016-09-15 18:13:52 -07002057 if (type_id != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002058 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002059 const dex::ClassDef* found_class_def = dex_file.FindClassDef(type_idx);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08002060 if (kIsDebugBuild && used_lookup_table) {
2061 DCHECK_EQ(found_class_def, lookup_table_classdef);
2062 }
2063 return found_class_def;
David Sehr9aa352e2016-09-15 18:13:52 -07002064 }
2065 return nullptr;
2066}
2067
Mathieu Chartier120aa282017-08-05 16:03:03 -07002068// Madvise the dex file based on the state we are moving to.
2069void OatDexFile::MadviseDexFile(const DexFile& dex_file, MadviseState state) {
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07002070 Runtime* const runtime = Runtime::Current();
2071 const bool low_ram = runtime->GetHeap()->IsLowMemoryMode();
Mathieu Chartier150d25d2017-08-28 09:52:55 -07002072 // TODO: Also do madvise hints for non low ram devices.
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07002073 if (!low_ram) {
Mathieu Chartierbe8303d2017-08-17 17:39:39 -07002074 return;
2075 }
Mathieu Chartierc42cb0e2017-10-13 11:35:00 -07002076 if (state == MadviseState::kMadviseStateAtLoad && runtime->MAdviseRandomAccess()) {
2077 // Default every dex file to MADV_RANDOM when its loaded by default for low ram devices.
2078 // Other devices have enough page cache to get performance benefits from loading more pages
2079 // into the page cache.
David Sehrc431b9d2018-03-02 12:01:51 -08002080 DexLayoutSection::MadviseLargestPageAlignedRegion(dex_file.Begin(),
2081 dex_file.Begin() + dex_file.Size(),
2082 MADV_RANDOM);
Mathieu Chartier120aa282017-08-05 16:03:03 -07002083 }
Andreas Gampeb40d3612018-06-26 15:49:42 -07002084 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier120aa282017-08-05 16:03:03 -07002085 if (oat_dex_file != nullptr) {
2086 // Should always be there.
2087 const DexLayoutSections* const sections = oat_dex_file->GetDexLayoutSections();
2088 CHECK(sections != nullptr);
2089 sections->Madvise(&dex_file, state);
2090 }
2091}
2092
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002093OatFile::OatClass::OatClass(const OatFile* oat_file,
Vladimir Marko2c64a832018-01-04 11:31:56 +00002094 ClassStatus status,
Brian Carlstromba150c32013-08-27 17:31:03 -07002095 OatClassType type,
2096 uint32_t bitmap_size,
2097 const uint32_t* bitmap_pointer,
Brian Carlstrom0755ec52012-01-11 15:19:46 -08002098 const OatMethodOffsets* methods_pointer)
Brian Carlstromba150c32013-08-27 17:31:03 -07002099 : oat_file_(oat_file), status_(status), type_(type),
Vladimir Markod3c5beb2014-04-11 16:32:51 +01002100 bitmap_(bitmap_pointer), methods_pointer_(methods_pointer) {
Brian Carlstromba150c32013-08-27 17:31:03 -07002101 switch (type_) {
2102 case kOatClassAllCompiled: {
2103 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08002104 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07002105 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07002106 break;
2107 }
2108 case kOatClassSomeCompiled: {
2109 CHECK_NE(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08002110 CHECK(bitmap_pointer != nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07002111 CHECK(methods_pointer != nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07002112 break;
2113 }
2114 case kOatClassNoneCompiled: {
2115 CHECK_EQ(0U, bitmap_size);
Brian Carlstromcd937a92014-03-04 22:53:23 -08002116 CHECK(bitmap_pointer == nullptr);
Ian Rogers97b52f82014-08-14 11:34:07 -07002117 CHECK(methods_pointer_ == nullptr);
Brian Carlstromba150c32013-08-27 17:31:03 -07002118 break;
2119 }
2120 case kOatClassMax: {
2121 LOG(FATAL) << "Invalid OatClassType " << type_;
Elliott Hughesc1896c92018-11-29 11:33:18 -08002122 UNREACHABLE();
Brian Carlstromba150c32013-08-27 17:31:03 -07002123 }
2124 }
2125}
Brian Carlstrome24fa612011-09-29 00:53:55 -07002126
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002127uint32_t OatFile::OatClass::GetOatMethodOffsetsOffset(uint32_t method_index) const {
2128 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
2129 if (oat_method_offsets == nullptr) {
2130 return 0u;
2131 }
2132 return reinterpret_cast<const uint8_t*>(oat_method_offsets) - oat_file_->Begin();
2133}
2134
2135const OatMethodOffsets* OatFile::OatClass::GetOatMethodOffsets(uint32_t method_index) const {
Vladimir Markod3c5beb2014-04-11 16:32:51 +01002136 // 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 -07002137 if (methods_pointer_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07002138 CHECK_EQ(kOatClassNoneCompiled, type_);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002139 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07002140 }
2141 size_t methods_pointer_index;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002142 if (bitmap_ == nullptr) {
Brian Carlstromba150c32013-08-27 17:31:03 -07002143 CHECK_EQ(kOatClassAllCompiled, type_);
2144 methods_pointer_index = method_index;
2145 } else {
2146 CHECK_EQ(kOatClassSomeCompiled, type_);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01002147 if (!BitVector::IsBitSet(bitmap_, method_index)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002148 return nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -07002149 }
Vladimir Markod3c5beb2014-04-11 16:32:51 +01002150 size_t num_set_bits = BitVector::NumSetBits(bitmap_, method_index);
2151 methods_pointer_index = num_set_bits;
Brian Carlstromba150c32013-08-27 17:31:03 -07002152 }
2153 const OatMethodOffsets& oat_method_offsets = methods_pointer_[methods_pointer_index];
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002154 return &oat_method_offsets;
2155}
2156
2157const OatFile::OatMethod OatFile::OatClass::GetOatMethod(uint32_t method_index) const {
2158 const OatMethodOffsets* oat_method_offsets = GetOatMethodOffsets(method_index);
2159 if (oat_method_offsets == nullptr) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08002160 return OatMethod(nullptr, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002161 }
2162 if (oat_file_->IsExecutable() ||
2163 Runtime::Current() == nullptr || // This case applies for oatdump.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002164 Runtime::Current()->IsAotCompiler()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08002165 return OatMethod(oat_file_->Begin(), oat_method_offsets->code_offset_);
Alex Light9dcc4572014-08-14 14:16:26 -07002166 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002167 // We aren't allowed to use the compiled code. We just force it down the interpreted / jit
2168 // version.
2169 return OatMethod(oat_file_->Begin(), 0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07002170}
2171
Sebastien Hertz0de11332015-05-13 12:14:05 +02002172bool OatFile::IsDebuggable() const {
2173 return GetOatHeader().IsDebuggable();
2174}
2175
Andreas Gampe29d38e72016-03-23 15:31:51 +00002176CompilerFilter::Filter OatFile::GetCompilerFilter() const {
2177 return GetOatHeader().GetCompilerFilter();
Calin Juravleb077e152016-02-18 18:47:37 +00002178}
2179
Calin Juravle44e5efa2017-09-12 00:54:26 -07002180std::string OatFile::GetClassLoaderContext() const {
2181 return GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002182}
Calin Juravle44e5efa2017-09-12 00:54:26 -07002183
Calin Juravle0e09dfc2018-02-12 19:01:09 -08002184const char* OatFile::GetCompilationReason() const {
2185 return GetOatHeader().GetStoreValueByKey(OatHeader::kCompilationReasonKey);
2186}
2187
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01002188OatFile::OatClass OatFile::FindOatClass(const DexFile& dex_file,
2189 uint16_t class_def_idx,
2190 bool* found) {
2191 DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16);
Andreas Gampeb40d3612018-06-26 15:49:42 -07002192 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
Mathieu Chartier1b868492016-11-16 16:22:37 -08002193 if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) {
Vladimir Marko97d7e1c2016-10-04 14:44:28 +01002194 *found = false;
2195 return OatFile::OatClass::Invalid();
2196 }
2197 *found = true;
2198 return oat_dex_file->GetOatClass(class_def_idx);
2199}
2200
Vladimir Marko1cedb4a2019-02-06 14:13:28 +00002201static void DCheckIndexToBssMapping(const OatFile* oat_file,
2202 uint32_t number_of_indexes,
2203 size_t slot_size,
2204 const IndexBssMapping* index_bss_mapping) {
2205 if (kIsDebugBuild && index_bss_mapping != nullptr) {
2206 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
2207 const IndexBssMappingEntry* prev_entry = nullptr;
2208 for (const IndexBssMappingEntry& entry : *index_bss_mapping) {
2209 CHECK_ALIGNED_PARAM(entry.bss_offset, slot_size);
2210 CHECK_LT(entry.bss_offset, oat_file->BssSize());
2211 uint32_t mask = entry.GetMask(index_bits);
2212 CHECK_LE(POPCOUNT(mask) * slot_size, entry.bss_offset);
2213 size_t index_mask_span = (mask != 0u) ? 32u - index_bits - CTZ(mask) : 0u;
2214 CHECK_LE(index_mask_span, entry.GetIndex(index_bits));
2215 if (prev_entry != nullptr) {
2216 CHECK_LT(prev_entry->GetIndex(index_bits), entry.GetIndex(index_bits) - index_mask_span);
2217 }
2218 prev_entry = &entry;
2219 }
2220 CHECK(prev_entry != nullptr);
2221 CHECK_LT(prev_entry->GetIndex(index_bits), number_of_indexes);
2222 }
2223}
2224
2225void OatFile::InitializeRelocations() const {
2226 DCHECK(IsExecutable());
2227
2228 // Initialize the .data.bimg.rel.ro section.
2229 if (!GetBootImageRelocations().empty()) {
2230 uint8_t* reloc_begin = const_cast<uint8_t*>(DataBimgRelRoBegin());
2231 CheckedCall(mprotect,
2232 "un-protect boot image relocations",
2233 reloc_begin,
2234 DataBimgRelRoSize(),
2235 PROT_READ | PROT_WRITE);
Vladimir Marko7cde4582019-07-05 13:26:11 +01002236 uint32_t boot_image_begin = Runtime::Current()->GetHeap()->GetBootImagesStartAddress();
Vladimir Marko1cedb4a2019-02-06 14:13:28 +00002237 for (const uint32_t& relocation : GetBootImageRelocations()) {
2238 const_cast<uint32_t&>(relocation) += boot_image_begin;
2239 }
2240 CheckedCall(mprotect,
2241 "protect boot image relocations",
2242 reloc_begin,
2243 DataBimgRelRoSize(),
2244 PROT_READ);
2245 }
2246
2247 // Before initializing .bss, check the .bss mappings in debug mode.
2248 if (kIsDebugBuild) {
2249 PointerSize pointer_size = GetInstructionSetPointerSize(GetOatHeader().GetInstructionSet());
2250 for (const OatDexFile* odf : GetOatDexFiles()) {
2251 const DexFile::Header* header =
2252 reinterpret_cast<const DexFile::Header*>(odf->GetDexFilePointer());
2253 DCheckIndexToBssMapping(this,
2254 header->method_ids_size_,
2255 static_cast<size_t>(pointer_size),
2256 odf->GetMethodBssMapping());
2257 DCheckIndexToBssMapping(this,
2258 header->type_ids_size_,
2259 sizeof(GcRoot<mirror::Class>),
2260 odf->GetTypeBssMapping());
2261 DCheckIndexToBssMapping(this,
2262 header->string_ids_size_,
2263 sizeof(GcRoot<mirror::String>),
2264 odf->GetStringBssMapping());
2265 }
2266 }
2267
2268 // Initialize the .bss section.
2269 // TODO: Pre-initialize from boot/app image?
2270 ArtMethod* resolution_method = Runtime::Current()->GetResolutionMethod();
2271 for (ArtMethod*& entry : GetBssMethods()) {
2272 entry = resolution_method;
2273 }
2274}
2275
Andreas Gampeb40d3612018-06-26 15:49:42 -07002276void OatDexFile::AssertAotCompiler() {
Mathieu Chartier1b868492016-11-16 16:22:37 -08002277 CHECK(Runtime::Current()->IsAotCompiler());
2278}
2279
Brian Carlstrome24fa612011-09-29 00:53:55 -07002280} // namespace art