blob: d1983a5ce2d7a713b3a3ff97968c5be689027f7b [file] [log] [blame]
Brian Carlstrome24fa612011-09-29 00:53:55 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OAT_WRITER_H_
4#define ART_SRC_OAT_WRITER_H_
5
6#include <stdint.h>
7
8#include <cstddef>
9
10#include "UniquePtr.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070011#include "compiler.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070012#include "dex_cache.h"
13#include "mem_map.h"
14#include "oat.h"
15#include "object.h"
16#include "os.h"
17#include "space.h"
18
19namespace art {
20
21// OatHeader fixed length with count of D OatDexFiles
22//
Brian Carlstrom389efb02012-01-11 12:06:26 -080023// OatDexFile[0] one variable sized OatDexFile with offsets to Dex and OatClasses
Brian Carlstrome24fa612011-09-29 00:53:55 -070024// OatDexFile[1]
25// ...
26// OatDexFile[D]
27//
Brian Carlstrom89521892011-12-07 22:05:07 -080028// Dex[0] one variable sized DexFile for each OatDexFile.
29// Dex[1] these are literal copies of the input .dex files.
30// ...
31// Dex[D]
32//
Brian Carlstrom389efb02012-01-11 12:06:26 -080033// OatClass[0] one variable sized OatClass for each of C DexFile::ClassDefs
34// OatClass[1] contains OatClass entries with class status, offsets to code, etc.
Brian Carlstrome24fa612011-09-29 00:53:55 -070035// ...
Brian Carlstrom389efb02012-01-11 12:06:26 -080036// OatClass[C]
Brian Carlstrome24fa612011-09-29 00:53:55 -070037//
38// padding if necessary so that the follow code will be page aligned
39//
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040// CompiledMethod one variable sized blob with the contents of each CompiledMethod
41// CompiledMethod
42// CompiledMethod
43// CompiledMethod
44// CompiledMethod
45// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070046// ...
Brian Carlstrom3320cf42011-10-04 14:58:28 -070047// CompiledMethod
Brian Carlstrome24fa612011-09-29 00:53:55 -070048//
49class OatWriter {
50 public:
51 // Write an oat file. Returns true on success, false on failure.
Elliott Hughes234da572011-11-03 22:13:06 -070052 static bool Create(File* file, const ClassLoader* class_loader, const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070053
54 private:
55
Brian Carlstrom3320cf42011-10-04 14:58:28 -070056 OatWriter(const std::vector<const DexFile*>& dex_files,
57 const ClassLoader* class_loader,
58 const Compiler& compiler);
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 ~OatWriter();
60
61 size_t InitOatHeader();
62 size_t InitOatDexFiles(size_t offset);
Brian Carlstrom89521892011-12-07 22:05:07 -080063 size_t InitDexFiles(size_t offset);
Brian Carlstrom389efb02012-01-11 12:06:26 -080064 size_t InitOatClasses(size_t offset);
Brian Carlstrome24fa612011-09-29 00:53:55 -070065 size_t InitOatCode(size_t offset);
66 size_t InitOatCodeDexFiles(size_t offset);
67 size_t InitOatCodeDexFile(size_t offset,
68 size_t& oat_class_index,
69 const DexFile& dex_file);
70 size_t InitOatCodeClassDef(size_t offset,
71 size_t oat_class_index,
72 const DexFile& dex_file,
73 const DexFile::ClassDef& class_def);
Ian Rogers0571d352011-11-03 19:51:38 -070074 size_t InitOatCodeMethod(size_t offset, size_t oat_class_index, size_t class_def_method_index,
75 bool is_static, bool is_direct, uint32_t method_idx, const DexFile*);
Brian Carlstrome24fa612011-09-29 00:53:55 -070076
Elliott Hughes234da572011-11-03 22:13:06 -070077 bool Write(File* file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 bool WriteTables(File* file);
79 size_t WriteCode(File* file);
Ian Rogers0571d352011-11-03 19:51:38 -070080 size_t WriteCodeDexFiles(File* file, size_t offset);
81 size_t WriteCodeDexFile(File* file, size_t offset, size_t& oat_class_index,
Brian Carlstrome24fa612011-09-29 00:53:55 -070082 const DexFile& dex_file);
Ian Rogers0571d352011-11-03 19:51:38 -070083 size_t WriteCodeClassDef(File* file, size_t offset, size_t oat_class_index,
84 const DexFile& dex_file, const DexFile::ClassDef& class_def);
85 size_t WriteCodeMethod(File* file, size_t offset, size_t oat_class_index,
86 size_t class_def_method_index, bool is_static, uint32_t method_idx,
87 const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070088
Ian Rogers0571d352011-11-03 19:51:38 -070089 void ReportWriteFailure(const char* what, uint32_t method_idx, const DexFile& dex_file,
90 File* f) const;
Elliott Hughes234da572011-11-03 22:13:06 -070091
Brian Carlstrome24fa612011-09-29 00:53:55 -070092 class OatDexFile {
93 public:
Elliott Hughesa51a3dd2011-10-17 15:19:26 -070094 explicit OatDexFile(const DexFile& dex_file);
Brian Carlstrome24fa612011-09-29 00:53:55 -070095 size_t SizeOf() const;
96 void UpdateChecksum(OatHeader& oat_header) const;
97 bool Write(File* file) const;
98
99 // data to write
100 uint32_t dex_file_location_size_;
101 const uint8_t* dex_file_location_data_;
102 uint32_t dex_file_checksum_;
Brian Carlstrom89521892011-12-07 22:05:07 -0800103 uint32_t dex_file_offset_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700104 std::vector<uint32_t> methods_offsets_;
105
106 private:
Brian Carlstrom6e3b1d92012-01-11 01:36:32 -0800107 DISALLOW_COPY_AND_ASSIGN(OatDexFile);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700108 };
109
Brian Carlstrom389efb02012-01-11 12:06:26 -0800110 class OatClass {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700111 public:
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800112 explicit OatClass(Class::Status status, uint32_t methods_count);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700113 size_t SizeOf() const;
114 void UpdateChecksum(OatHeader& oat_header) const;
115 bool Write(File* file) const;
116
117 // data to write
Brian Carlstrom0755ec52012-01-11 15:19:46 -0800118 Class::Status status_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119 std::vector<OatMethodOffsets> method_offsets_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700120
121 private:
Brian Carlstrom389efb02012-01-11 12:06:26 -0800122 DISALLOW_COPY_AND_ASSIGN(OatClass);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 };
124
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700125 const Compiler* compiler_;
126
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 // TODO: remove the ClassLoader when the code storage moves out of Method
128 const ClassLoader* class_loader_;
129
130 // note OatFile does not take ownership of the DexFiles
131 const std::vector<const DexFile*>* dex_files_;
132
133 // data to write
134 OatHeader* oat_header_;
135 std::vector<OatDexFile*> oat_dex_files_;
Brian Carlstrom389efb02012-01-11 12:06:26 -0800136 std::vector<OatClass*> oat_classes_;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700137 uint32_t executable_offset_padding_length_;
138
jeffhaof479dcc2011-11-02 15:54:15 -0700139 template <class T> struct MapCompare {
140 public:
141 bool operator() (const T* const &a, const T* const &b) const {
142 return *a < *b;
143 }
144 };
145
jeffhao55d78212011-11-02 11:41:50 -0700146 // code mappings for deduplication
jeffhaof479dcc2011-11-02 15:54:15 -0700147 std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > code_offsets_;
148 std::map<const std::vector<uint16_t>*, uint32_t, MapCompare<std::vector<uint16_t> > > vmap_table_offsets_;
149 std::map<const std::vector<uint32_t>*, uint32_t, MapCompare<std::vector<uint32_t> > > mapping_table_offsets_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800150 std::map<const std::vector<uint8_t>*, uint32_t, MapCompare<std::vector<uint8_t> > > gc_map_offsets_;
jeffhao55d78212011-11-02 11:41:50 -0700151
Brian Carlstrome24fa612011-09-29 00:53:55 -0700152 DISALLOW_COPY_AND_ASSIGN(OatWriter);
153};
154
155} // namespace art
156
157#endif // ART_SRC_OAT_WRITER_H_