David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_VDEX_FILE_H_ |
| 18 | #define ART_RUNTIME_VDEX_FILE_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "base/macros.h" |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 24 | #include "mem_map.h" |
| 25 | #include "os.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | // VDEX files contain extracted DEX files. The VdexFile class maps the file to |
| 30 | // memory and provides tools for accessing its individual sections. |
| 31 | // |
| 32 | // File format: |
| 33 | // VdexFile::Header fixed-length header |
| 34 | // |
| 35 | // DEX[0] array of the input DEX files |
| 36 | // DEX[1] the bytecode may have been quickened |
| 37 | // ... |
| 38 | // DEX[D] |
| 39 | // |
| 40 | |
| 41 | class VdexFile { |
| 42 | public: |
| 43 | struct Header { |
| 44 | public: |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 45 | Header(uint32_t dex_size, uint32_t verifier_deps_size); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 46 | |
| 47 | bool IsMagicValid() const; |
| 48 | bool IsVersionValid() const; |
| 49 | |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 50 | uint32_t GetDexSize() const { return dex_size_; } |
| 51 | uint32_t GetVerifierDepsSize() const { return verifier_deps_size_; } |
| 52 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 53 | private: |
| 54 | static constexpr uint8_t kVdexMagic[] = { 'v', 'd', 'e', 'x' }; |
| 55 | static constexpr uint8_t kVdexVersion[] = { '0', '0', '0', '\0' }; |
| 56 | |
| 57 | uint8_t magic_[4]; |
| 58 | uint8_t version_[4]; |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 59 | uint32_t dex_size_; |
| 60 | uint32_t verifier_deps_size_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static VdexFile* Open(const std::string& vdex_filename, |
| 64 | bool writable, |
| 65 | bool low_4gb, |
| 66 | std::string* error_msg); |
| 67 | |
| 68 | const uint8_t* Begin() const { return mmap_->Begin(); } |
| 69 | const uint8_t* End() const { return mmap_->End(); } |
| 70 | size_t Size() const { return mmap_->Size(); } |
| 71 | |
| 72 | private: |
Andreas Gampe | f7e8223 | 2016-09-12 15:55:56 -0700 | [diff] [blame] | 73 | explicit VdexFile(MemMap* mmap) : mmap_(mmap) {} |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 74 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 75 | std::unique_ptr<MemMap> mmap_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(VdexFile); |
| 78 | }; |
| 79 | |
| 80 | } // namespace art |
| 81 | |
| 82 | #endif // ART_RUNTIME_VDEX_FILE_H_ |