• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 
17 #ifndef ART_COMPILER_COMPILED_METHOD_H_
18 #define ART_COMPILER_COMPILED_METHOD_H_
19 
20 #include <memory>
21 #include <iosfwd>
22 #include <string>
23 #include <vector>
24 
25 #include "arch/instruction_set.h"
26 #include "base/array_ref.h"
27 #include "base/bit_utils.h"
28 #include "base/length_prefixed_array.h"
29 #include "dex_file_types.h"
30 #include "method_reference.h"
31 
32 namespace art {
33 
34 class CompilerDriver;
35 class CompiledMethodStorage;
36 
37 class CompiledCode {
38  public:
39   // For Quick to supply an code blob
40   CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
41                const ArrayRef<const uint8_t>& quick_code);
42 
43   virtual ~CompiledCode();
44 
GetInstructionSet()45   InstructionSet GetInstructionSet() const {
46     return instruction_set_;
47   }
48 
GetQuickCode()49   ArrayRef<const uint8_t> GetQuickCode() const {
50     return GetArray(quick_code_);
51   }
52 
53   bool operator==(const CompiledCode& rhs) const;
54 
55   // To align an offset from a page-aligned value to make it suitable
56   // for code storage. For example on ARM, to ensure that PC relative
57   // valu computations work out as expected.
58   size_t AlignCode(size_t offset) const;
59   static size_t AlignCode(size_t offset, InstructionSet instruction_set);
60 
61   // returns the difference between the code address and a usable PC.
62   // mainly to cope with kThumb2 where the lower bit must be set.
63   size_t CodeDelta() const;
64   static size_t CodeDelta(InstructionSet instruction_set);
65 
66   // Returns a pointer suitable for invoking the code at the argument
67   // code_pointer address.  Mainly to cope with kThumb2 where the
68   // lower bit must be set to indicate Thumb mode.
69   static const void* CodePointer(const void* code_pointer,
70                                  InstructionSet instruction_set);
71 
72  protected:
73   template <typename T>
GetArray(const LengthPrefixedArray<T> * array)74   static ArrayRef<const T> GetArray(const LengthPrefixedArray<T>* array) {
75     if (array == nullptr) {
76       return ArrayRef<const T>();
77     }
78     DCHECK_NE(array->size(), 0u);
79     return ArrayRef<const T>(&array->At(0), array->size());
80   }
81 
GetCompilerDriver()82   CompilerDriver* GetCompilerDriver() {
83     return compiler_driver_;
84   }
85 
86  private:
87   CompilerDriver* const compiler_driver_;
88 
89   const InstructionSet instruction_set_;
90 
91   // Used to store the PIC code for Quick.
92   const LengthPrefixedArray<uint8_t>* const quick_code_;
93 };
94 
95 class SrcMapElem {
96  public:
97   uint32_t from_;
98   int32_t to_;
99 };
100 
101 inline bool operator<(const SrcMapElem& lhs, const SrcMapElem& rhs) {
102   if (lhs.from_ != rhs.from_) {
103     return lhs.from_ < rhs.from_;
104   }
105   return lhs.to_ < rhs.to_;
106 }
107 
108 inline bool operator==(const SrcMapElem& lhs, const SrcMapElem& rhs) {
109   return lhs.from_ == rhs.from_ && lhs.to_ == rhs.to_;
110 }
111 
112 class LinkerPatch {
113  public:
114   // Note: We explicitly specify the underlying type of the enum because GCC
115   // would otherwise select a bigger underlying type and then complain that
116   //     'art::LinkerPatch::patch_type_' is too small to hold all
117   //     values of 'enum class art::LinkerPatch::Type'
118   // which is ridiculous given we have only a handful of values here. If we
119   // choose to squeeze the Type into fewer than 8 bits, we'll have to declare
120   // patch_type_ as an uintN_t and do explicit static_cast<>s.
121   enum class Type : uint8_t {
122     kMethodRelative,          // NOTE: Actual patching is instruction_set-dependent.
123     kMethodBssEntry,          // NOTE: Actual patching is instruction_set-dependent.
124     kCall,
125     kCallRelative,            // NOTE: Actual patching is instruction_set-dependent.
126     kTypeRelative,            // NOTE: Actual patching is instruction_set-dependent.
127     kTypeBssEntry,            // NOTE: Actual patching is instruction_set-dependent.
128     kStringRelative,          // NOTE: Actual patching is instruction_set-dependent.
129     kStringBssEntry,          // NOTE: Actual patching is instruction_set-dependent.
130     kBakerReadBarrierBranch,  // NOTE: Actual patching is instruction_set-dependent.
131   };
132 
RelativeMethodPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_method_idx)133   static LinkerPatch RelativeMethodPatch(size_t literal_offset,
134                                          const DexFile* target_dex_file,
135                                          uint32_t pc_insn_offset,
136                                          uint32_t target_method_idx) {
137     LinkerPatch patch(literal_offset, Type::kMethodRelative, target_dex_file);
138     patch.method_idx_ = target_method_idx;
139     patch.pc_insn_offset_ = pc_insn_offset;
140     return patch;
141   }
142 
MethodBssEntryPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_method_idx)143   static LinkerPatch MethodBssEntryPatch(size_t literal_offset,
144                                          const DexFile* target_dex_file,
145                                          uint32_t pc_insn_offset,
146                                          uint32_t target_method_idx) {
147     LinkerPatch patch(literal_offset, Type::kMethodBssEntry, target_dex_file);
148     patch.method_idx_ = target_method_idx;
149     patch.pc_insn_offset_ = pc_insn_offset;
150     return patch;
151   }
152 
CodePatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t target_method_idx)153   static LinkerPatch CodePatch(size_t literal_offset,
154                                const DexFile* target_dex_file,
155                                uint32_t target_method_idx) {
156     LinkerPatch patch(literal_offset, Type::kCall, target_dex_file);
157     patch.method_idx_ = target_method_idx;
158     return patch;
159   }
160 
RelativeCodePatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t target_method_idx)161   static LinkerPatch RelativeCodePatch(size_t literal_offset,
162                                        const DexFile* target_dex_file,
163                                        uint32_t target_method_idx) {
164     LinkerPatch patch(literal_offset, Type::kCallRelative, target_dex_file);
165     patch.method_idx_ = target_method_idx;
166     return patch;
167   }
168 
RelativeTypePatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_type_idx)169   static LinkerPatch RelativeTypePatch(size_t literal_offset,
170                                        const DexFile* target_dex_file,
171                                        uint32_t pc_insn_offset,
172                                        uint32_t target_type_idx) {
173     LinkerPatch patch(literal_offset, Type::kTypeRelative, target_dex_file);
174     patch.type_idx_ = target_type_idx;
175     patch.pc_insn_offset_ = pc_insn_offset;
176     return patch;
177   }
178 
TypeBssEntryPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_type_idx)179   static LinkerPatch TypeBssEntryPatch(size_t literal_offset,
180                                        const DexFile* target_dex_file,
181                                        uint32_t pc_insn_offset,
182                                        uint32_t target_type_idx) {
183     LinkerPatch patch(literal_offset, Type::kTypeBssEntry, target_dex_file);
184     patch.type_idx_ = target_type_idx;
185     patch.pc_insn_offset_ = pc_insn_offset;
186     return patch;
187   }
188 
RelativeStringPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_string_idx)189   static LinkerPatch RelativeStringPatch(size_t literal_offset,
190                                          const DexFile* target_dex_file,
191                                          uint32_t pc_insn_offset,
192                                          uint32_t target_string_idx) {
193     LinkerPatch patch(literal_offset, Type::kStringRelative, target_dex_file);
194     patch.string_idx_ = target_string_idx;
195     patch.pc_insn_offset_ = pc_insn_offset;
196     return patch;
197   }
198 
StringBssEntryPatch(size_t literal_offset,const DexFile * target_dex_file,uint32_t pc_insn_offset,uint32_t target_string_idx)199   static LinkerPatch StringBssEntryPatch(size_t literal_offset,
200                                          const DexFile* target_dex_file,
201                                          uint32_t pc_insn_offset,
202                                          uint32_t target_string_idx) {
203     LinkerPatch patch(literal_offset, Type::kStringBssEntry, target_dex_file);
204     patch.string_idx_ = target_string_idx;
205     patch.pc_insn_offset_ = pc_insn_offset;
206     return patch;
207   }
208 
209   static LinkerPatch BakerReadBarrierBranchPatch(size_t literal_offset,
210                                                  uint32_t custom_value1 = 0u,
211                                                  uint32_t custom_value2 = 0u) {
212     LinkerPatch patch(literal_offset, Type::kBakerReadBarrierBranch, nullptr);
213     patch.baker_custom_value1_ = custom_value1;
214     patch.baker_custom_value2_ = custom_value2;
215     return patch;
216   }
217 
218   LinkerPatch(const LinkerPatch& other) = default;
219   LinkerPatch& operator=(const LinkerPatch& other) = default;
220 
LiteralOffset()221   size_t LiteralOffset() const {
222     return literal_offset_;
223   }
224 
GetType()225   Type GetType() const {
226     return patch_type_;
227   }
228 
IsPcRelative()229   bool IsPcRelative() const {
230     switch (GetType()) {
231       case Type::kMethodRelative:
232       case Type::kMethodBssEntry:
233       case Type::kCallRelative:
234       case Type::kTypeRelative:
235       case Type::kTypeBssEntry:
236       case Type::kStringRelative:
237       case Type::kStringBssEntry:
238       case Type::kBakerReadBarrierBranch:
239         return true;
240       default:
241         return false;
242     }
243   }
244 
TargetMethod()245   MethodReference TargetMethod() const {
246     DCHECK(patch_type_ == Type::kMethodRelative ||
247            patch_type_ == Type::kMethodBssEntry ||
248            patch_type_ == Type::kCall ||
249            patch_type_ == Type::kCallRelative);
250     return MethodReference(target_dex_file_, method_idx_);
251   }
252 
TargetTypeDexFile()253   const DexFile* TargetTypeDexFile() const {
254     DCHECK(patch_type_ == Type::kTypeRelative ||
255            patch_type_ == Type::kTypeBssEntry);
256     return target_dex_file_;
257   }
258 
TargetTypeIndex()259   dex::TypeIndex TargetTypeIndex() const {
260     DCHECK(patch_type_ == Type::kTypeRelative ||
261            patch_type_ == Type::kTypeBssEntry);
262     return dex::TypeIndex(type_idx_);
263   }
264 
TargetStringDexFile()265   const DexFile* TargetStringDexFile() const {
266     DCHECK(patch_type_ == Type::kStringRelative ||
267            patch_type_ == Type::kStringBssEntry);
268     return target_dex_file_;
269   }
270 
TargetStringIndex()271   dex::StringIndex TargetStringIndex() const {
272     DCHECK(patch_type_ == Type::kStringRelative ||
273            patch_type_ == Type::kStringBssEntry);
274     return dex::StringIndex(string_idx_);
275   }
276 
PcInsnOffset()277   uint32_t PcInsnOffset() const {
278     DCHECK(patch_type_ == Type::kMethodRelative ||
279            patch_type_ == Type::kMethodBssEntry ||
280            patch_type_ == Type::kTypeRelative ||
281            patch_type_ == Type::kTypeBssEntry ||
282            patch_type_ == Type::kStringRelative ||
283            patch_type_ == Type::kStringBssEntry);
284     return pc_insn_offset_;
285   }
286 
GetBakerCustomValue1()287   uint32_t GetBakerCustomValue1() const {
288     DCHECK(patch_type_ == Type::kBakerReadBarrierBranch);
289     return baker_custom_value1_;
290   }
291 
GetBakerCustomValue2()292   uint32_t GetBakerCustomValue2() const {
293     DCHECK(patch_type_ == Type::kBakerReadBarrierBranch);
294     return baker_custom_value2_;
295   }
296 
297  private:
LinkerPatch(size_t literal_offset,Type patch_type,const DexFile * target_dex_file)298   LinkerPatch(size_t literal_offset, Type patch_type, const DexFile* target_dex_file)
299       : target_dex_file_(target_dex_file),
300         literal_offset_(literal_offset),
301         patch_type_(patch_type) {
302     cmp1_ = 0u;
303     cmp2_ = 0u;
304     // The compiler rejects methods that are too big, so the compiled code
305     // of a single method really shouln't be anywhere close to 16MiB.
306     DCHECK(IsUint<24>(literal_offset));
307   }
308 
309   const DexFile* target_dex_file_;
310   // TODO: Clean up naming. Some patched locations are literals but others are not.
311   uint32_t literal_offset_ : 24;  // Method code size up to 16MiB.
312   Type patch_type_ : 8;
313   union {
314     uint32_t cmp1_;             // Used for relational operators.
315     uint32_t method_idx_;       // Method index for Call/Method patches.
316     uint32_t type_idx_;         // Type index for Type patches.
317     uint32_t string_idx_;       // String index for String patches.
318     uint32_t baker_custom_value1_;
319     static_assert(sizeof(method_idx_) == sizeof(cmp1_), "needed by relational operators");
320     static_assert(sizeof(type_idx_) == sizeof(cmp1_), "needed by relational operators");
321     static_assert(sizeof(string_idx_) == sizeof(cmp1_), "needed by relational operators");
322     static_assert(sizeof(baker_custom_value1_) == sizeof(cmp1_), "needed by relational operators");
323   };
324   union {
325     // Note: To avoid uninitialized padding on 64-bit systems, we use `size_t` for `cmp2_`.
326     // This allows a hashing function to treat an array of linker patches as raw memory.
327     size_t cmp2_;             // Used for relational operators.
328     // Literal offset of the insn loading PC (same as literal_offset if it's the same insn,
329     // may be different if the PC-relative addressing needs multiple insns).
330     uint32_t pc_insn_offset_;
331     uint32_t baker_custom_value2_;
332     static_assert(sizeof(pc_insn_offset_) <= sizeof(cmp2_), "needed by relational operators");
333     static_assert(sizeof(baker_custom_value2_) <= sizeof(cmp2_), "needed by relational operators");
334   };
335 
336   friend bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs);
337   friend bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs);
338 };
339 std::ostream& operator<<(std::ostream& os, const LinkerPatch::Type& type);
340 
341 inline bool operator==(const LinkerPatch& lhs, const LinkerPatch& rhs) {
342   return lhs.literal_offset_ == rhs.literal_offset_ &&
343       lhs.patch_type_ == rhs.patch_type_ &&
344       lhs.target_dex_file_ == rhs.target_dex_file_ &&
345       lhs.cmp1_ == rhs.cmp1_ &&
346       lhs.cmp2_ == rhs.cmp2_;
347 }
348 
349 inline bool operator<(const LinkerPatch& lhs, const LinkerPatch& rhs) {
350   return (lhs.literal_offset_ != rhs.literal_offset_) ? lhs.literal_offset_ < rhs.literal_offset_
351       : (lhs.patch_type_ != rhs.patch_type_) ? lhs.patch_type_ < rhs.patch_type_
352       : (lhs.target_dex_file_ != rhs.target_dex_file_) ? lhs.target_dex_file_ < rhs.target_dex_file_
353       : (lhs.cmp1_ != rhs.cmp1_) ? lhs.cmp1_ < rhs.cmp1_
354       : lhs.cmp2_ < rhs.cmp2_;
355 }
356 
357 class CompiledMethod FINAL : public CompiledCode {
358  public:
359   // Constructs a CompiledMethod.
360   // Note: Consider using the static allocation methods below that will allocate the CompiledMethod
361   //       in the swap space.
362   CompiledMethod(CompilerDriver* driver,
363                  InstructionSet instruction_set,
364                  const ArrayRef<const uint8_t>& quick_code,
365                  const size_t frame_size_in_bytes,
366                  const uint32_t core_spill_mask,
367                  const uint32_t fp_spill_mask,
368                  const ArrayRef<const uint8_t>& method_info,
369                  const ArrayRef<const uint8_t>& vmap_table,
370                  const ArrayRef<const uint8_t>& cfi_info,
371                  const ArrayRef<const LinkerPatch>& patches);
372 
373   virtual ~CompiledMethod();
374 
375   static CompiledMethod* SwapAllocCompiledMethod(
376       CompilerDriver* driver,
377       InstructionSet instruction_set,
378       const ArrayRef<const uint8_t>& quick_code,
379       const size_t frame_size_in_bytes,
380       const uint32_t core_spill_mask,
381       const uint32_t fp_spill_mask,
382       const ArrayRef<const uint8_t>& method_info,
383       const ArrayRef<const uint8_t>& vmap_table,
384       const ArrayRef<const uint8_t>& cfi_info,
385       const ArrayRef<const LinkerPatch>& patches);
386 
387   static void ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m);
388 
GetFrameSizeInBytes()389   size_t GetFrameSizeInBytes() const {
390     return frame_size_in_bytes_;
391   }
392 
GetCoreSpillMask()393   uint32_t GetCoreSpillMask() const {
394     return core_spill_mask_;
395   }
396 
GetFpSpillMask()397   uint32_t GetFpSpillMask() const {
398     return fp_spill_mask_;
399   }
400 
GetMethodInfo()401   ArrayRef<const uint8_t> GetMethodInfo() const {
402     return GetArray(method_info_);
403   }
404 
GetVmapTable()405   ArrayRef<const uint8_t> GetVmapTable() const {
406     return GetArray(vmap_table_);
407   }
408 
GetCFIInfo()409   ArrayRef<const uint8_t> GetCFIInfo() const {
410     return GetArray(cfi_info_);
411   }
412 
GetPatches()413   ArrayRef<const LinkerPatch> GetPatches() const {
414     return GetArray(patches_);
415   }
416 
417  private:
418   // For quick code, the size of the activation used by the code.
419   const size_t frame_size_in_bytes_;
420   // For quick code, a bit mask describing spilled GPR callee-save registers.
421   const uint32_t core_spill_mask_;
422   // For quick code, a bit mask describing spilled FPR callee-save registers.
423   const uint32_t fp_spill_mask_;
424   // For quick code, method specific information that is not very dedupe friendly (method indices).
425   const LengthPrefixedArray<uint8_t>* const method_info_;
426   // For quick code, holds code infos which contain stack maps, inline information, and etc.
427   const LengthPrefixedArray<uint8_t>* const vmap_table_;
428   // For quick code, a FDE entry for the debug_frame section.
429   const LengthPrefixedArray<uint8_t>* const cfi_info_;
430   // For quick code, linker patches needed by the method.
431   const LengthPrefixedArray<LinkerPatch>* const patches_;
432 };
433 
434 }  // namespace art
435 
436 #endif  // ART_COMPILER_COMPILED_METHOD_H_
437