blob: 280c6f70ccd2797dc2b57a70843d761d9a20cd85 [file] [log] [blame]
Mathieu Chartiercf76bf82017-09-25 16:22:36 -07001/*
2 * Copyright (C) 2017 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
David Sehr9e734c72018-01-04 17:56:19 -080017#ifndef ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_
18#define ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070019
Mathieu Chartierf6e31472017-12-28 13:32:08 -080020#include "base/casts.h"
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070021#include "dex_file.h"
22
23namespace art {
24
25// CompactDex is a currently ART internal dex file format that aims to reduce storage/RAM usage.
26class CompactDexFile : public DexFile {
27 public:
Mathieu Chartier69147f12017-11-06 20:02:24 -080028 static constexpr uint8_t kDexMagic[kDexMagicSize] = { 'c', 'd', 'e', 'x' };
29 static constexpr uint8_t kDexMagicVersion[] = {'0', '0', '1', '\0'};
30
Mathieu Chartierf6e31472017-12-28 13:32:08 -080031 enum class FeatureFlags : uint32_t {
32 kDefaultMethods = 0x1,
33 };
34
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070035 class Header : public DexFile::Header {
Mathieu Chartierf6e31472017-12-28 13:32:08 -080036 public:
37 uint32_t GetFeatureFlags() const {
38 return feature_flags_;
39 }
40
41 private:
42 uint32_t feature_flags_ = 0u;
43
44 friend class CompactDexWriter;
Mathieu Chartierf95a75e2017-11-03 15:25:52 -070045 };
Mathieu Chartier69147f12017-11-06 20:02:24 -080046
47 struct CodeItem : public DexFile::CodeItem {
48 private:
49 // TODO: Insert compact dex specific fields here.
Mathieu Chartier6238c832018-01-04 09:55:13 -080050 friend class CompactDexFile;
Mathieu Chartier69147f12017-11-06 20:02:24 -080051 DISALLOW_COPY_AND_ASSIGN(CodeItem);
52 };
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070053
Mathieu Chartier603ccab2017-10-20 14:34:28 -070054 // Write the compact dex specific magic.
55 static void WriteMagic(uint8_t* magic);
56
57 // Write the current version, note that the input is the address of the magic.
58 static void WriteCurrentVersion(uint8_t* magic);
59
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070060 // Returns true if the byte string points to the magic value.
61 static bool IsMagicValid(const uint8_t* magic);
62 virtual bool IsMagicValid() const OVERRIDE;
63
64 // Returns true if the byte string after the magic is the correct value.
65 static bool IsVersionValid(const uint8_t* magic);
66 virtual bool IsVersionValid() const OVERRIDE;
67
Mathieu Chartierf6e31472017-12-28 13:32:08 -080068 const Header& GetHeader() const {
69 return down_cast<const Header&>(DexFile::GetHeader());
70 }
71
72 virtual bool SupportsDefaultMethods() const OVERRIDE;
73
Mathieu Chartier6238c832018-01-04 09:55:13 -080074 uint32_t GetCodeItemSize(const DexFile::CodeItem& item) const OVERRIDE;
75
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070076 private:
77 // Not supported yet.
78 CompactDexFile(const uint8_t* base,
79 size_t size,
80 const std::string& location,
81 uint32_t location_checksum,
David Sehr733bd4d2017-10-26 10:39:15 -070082 const OatDexFile* oat_dex_file,
83 DexFileContainer* container)
Mathieu Chartier69147f12017-11-06 20:02:24 -080084 : DexFile(base,
85 size,
86 location,
87 location_checksum,
88 oat_dex_file,
89 container,
90 /*is_compact_dex*/ true) {}
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070091
92 friend class DexFile;
Mathieu Chartier603ccab2017-10-20 14:34:28 -070093 friend class DexFileLoader;
Mathieu Chartiercf76bf82017-09-25 16:22:36 -070094
95 DISALLOW_COPY_AND_ASSIGN(CompactDexFile);
96};
97
98} // namespace art
99
David Sehr9e734c72018-01-04 17:56:19 -0800100#endif // ART_RUNTIME_DEX_COMPACT_DEX_FILE_H_