LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
DEX/File.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
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#ifndef LIEF_DEX_FILE_H
17#define LIEF_DEX_FILE_H
18#include <string_view>
19#include <memory>
20
21#include "LIEF/Object.hpp"
22#include "LIEF/visibility.h"
23
24#include "LIEF/DEX/Header.hpp"
25#include "LIEF/DEX/MapList.hpp"
26#include "LIEF/DEX/deopt.hpp"
28#include "LIEF/DEX/types.hpp"
29
30
31namespace LIEF::DEX {
32class Parser;
33class Class;
34class Method;
35class Type;
36class Prototype;
37class Field;
38
40class LIEF_API File : public Object {
41 friend class Parser;
42
43 public:
44 using classes_t = std::unordered_map<std::string, Class*>;
45 using classes_list_t = std::vector<std::unique_ptr<Class>>;
48
49 using methods_t = std::vector<std::unique_ptr<Method>>;
52
53 using strings_t = std::vector<std::unique_ptr<std::string>>;
57
58 using types_t = std::vector<std::unique_ptr<Type>>;
61
62 using prototypes_t = std::vector<std::unique_ptr<Prototype>>;
66
67 using fields_t = std::vector<std::unique_ptr<Field>>;
70
71 public:
72 File& operator=(const File& copy) = delete;
73 File(const File& copy) = delete;
74
77
79 std::string_view name() const;
80
81 void name(const std::string& name);
82
84 std::string_view location() const;
85 void location(const std::string& location);
86
90
94
96 bool has_class(const std::string& class_name) const;
97
99 const Class* get_class(const std::string& class_name) const LIEF_LIFETIMEBOUND;
100
101 Class* get_class(const std::string& class_name) LIEF_LIFETIMEBOUND;
102
104 const Class* get_class(size_t index) const LIEF_LIFETIMEBOUND;
105
107
110
112 std::string dex2dex_json_info() const;
113
117
121
125
129
133
135 const MapList& map() const;
137
139 std::string save(const std::string& path = "", bool deoptimize = true) const;
140
141 std::vector<uint8_t> raw(bool deoptimize = true) const;
142
143 void accept(Visitor& visitor) const override;
144
145
146 ~File() override;
147
148 LIEF_API friend std::ostream& operator<<(std::ostream& os, const File& file);
149
150 private:
151 File();
152
153 void add_class(std::unique_ptr<Class> cls);
154
155 static void deoptimize_nop(uint8_t* inst_ptr, uint32_t value);
156 static void deoptimize_return(uint8_t* inst_ptr, uint32_t value);
157 static void deoptimize_invoke_virtual(uint8_t* inst_ptr, uint32_t value,
158 OPCODES new_inst);
159 static void deoptimize_instance_field_access(uint8_t* inst_ptr, uint32_t value,
160 OPCODES new_inst);
161
162 std::string name_;
163 std::string location_;
164
165 Header header_;
166 classes_t classes_;
167 methods_t methods_;
168 fields_t fields_;
169 strings_t strings_;
170 types_t types_;
171 prototypes_t prototypes_;
172 MapList map_;
173
174 classes_list_t class_list_;
175 std::vector<uint8_t> original_data_;
176};
177
178}
179
180
181#endif
Class which represents a DEX Class (i.e. a Java/Kotlin class).
Definition DEX/Class.hpp:37
Class which represents a DEX Field.
Definition Field.hpp:35
void accept(Visitor &visitor) const override
std::vector< std::unique_ptr< Type > > types_t
Definition DEX/File.hpp:58
ref_iterator< classes_list_t &, Class * > it_classes
Definition DEX/File.hpp:46
std::string_view name() const
Name of this file.
it_const_methods methods() const
Return an iterator over all the DEX::Method used in this DEX file.
void location(const std::string &location)
std::vector< uint8_t > raw(bool deoptimize=true) const
it_const_fields fields() const
Return an iterator over all the DEX::Field used in this DEX file.
std::unordered_map< std::string, Class * > classes_t
Definition DEX/File.hpp:44
std::string save(const std::string &path="", bool deoptimize=true) const
Extract the current dex file and deoptimize it.
std::vector< std::unique_ptr< Class > > classes_list_t
Definition DEX/File.hpp:45
ref_iterator< fields_t &, Field * > it_fields
Definition DEX/File.hpp:68
std::vector< std::unique_ptr< std::string > > strings_t
Definition DEX/File.hpp:53
const_ref_iterator< const fields_t &, const Field * > it_const_fields
Definition DEX/File.hpp:69
const_ref_iterator< const strings_t &, const std::string * > it_const_strings
Definition DEX/File.hpp:55
it_const_types types() const
Type pool.
const Header & header() const
DEX header.
it_const_strings strings() const
String pool.
it_const_classes classes() const
All classes used in the DEX file
ref_iterator< methods_t &, Method * > it_methods
Definition DEX/File.hpp:50
dex2dex_info_t dex2dex_info() const
De-optimize information.
const Class * get_class(const std::string &class_name) const
Return the DEX::Class object associated with the given name.
bool has_class(const std::string &class_name) const
Check if the given class name exists.
File(const File &copy)=delete
std::string_view location() const
Location of this file.
ref_iterator< strings_t &, std::string * > it_strings
Definition DEX/File.hpp:54
const MapList & map() const
DEX Map.
const_ref_iterator< const prototypes_t &, const Prototype * > it_const_prototypes
Definition DEX/File.hpp:64
std::vector< std::unique_ptr< Field > > fields_t
Definition DEX/File.hpp:67
friend class Parser
Definition DEX/File.hpp:41
const_ref_iterator< const methods_t &, const Method * > it_const_methods
Definition DEX/File.hpp:51
ref_iterator< types_t &, Type * > it_types
Definition DEX/File.hpp:59
const_ref_iterator< const classes_list_t &, const Class * > it_const_classes
Definition DEX/File.hpp:47
const_ref_iterator< const types_t &, const Type * > it_const_types
Definition DEX/File.hpp:60
it_prototypes prototypes()
Prototype pool.
File & operator=(const File &copy)=delete
std::vector< std::unique_ptr< Method > > methods_t
Definition DEX/File.hpp:49
std::string dex2dex_json_info() const
De-optimize information as JSON.
void name(const std::string &name)
std::vector< std::unique_ptr< Prototype > > prototypes_t
Definition DEX/File.hpp:62
ref_iterator< prototypes_t &, Prototype * > it_prototypes
Definition DEX/File.hpp:63
dex_version_t version() const
Version of the current DEX file.
Class which represents the DEX header. This is the first structure that begins the DEX format.
Definition DEX/Header.hpp:39
Class which represents the map_list structure that follows the main DEX header.
Definition MapList.hpp:37
Class which represents a DEX::Method.
Definition DEX/Method.hpp:37
Class which parses a DEX file to produce a DEX::File object.
Definition DEX/Parser.hpp:38
Class which represents a DEX method prototype.
Definition Prototype.hpp:29
Class which represents a DEX type as described in the format specifications: https://source....
Definition DEX/Type.hpp:34
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:47
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition DEX/Class.hpp:31
OPCODES
The Dalvik Opcodes.
Definition instructions.hpp:32
uint32_t dex_version_t
Definition DEX/types.hpp:23
std::unordered_map< Class *, dex2dex_class_info_t > dex2dex_info_t
Definition deopt.hpp:29
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:316
#define LIEF_API
Definition visibility.h:45