• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (c) 2023 Huawei Device Co., Ltd.
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  #ifndef NAMEMANGLER_H
17  #define NAMEMANGLER_H
18  #include <string>
19  #include <vector>
20  
21  // This is a general name mangler which is shared between maple compiler and runtime.
22  // maple-compiler-specific data structure may NOT be used here.
23  namespace namemangler {
24  #define TO_STR(s) TO_STR2(s)
25  #define TO_STR2(s) #s
26  
27  #define VTAB_PREFIX __vtb_
28  #define ITAB_PREFIX __itb_
29  #define VTAB_AND_ITAB_PREFIX __vtb_and_itb_
30  #define ITAB_CONFLICT_PREFIX __itbC_
31  #define CLASSINFO_PREFIX __cinf_
32  #define CLASSINFO_RO_PREFIX __classinforo__
33  #define SUPERCLASSINFO_PREFIX __superclasses__
34  #define PRIMITIVECLASSINFO_PREFIX __pinf_
35  #define CLASS_INIT_BRIDGE_PREFIX __ClassInitBridge__
36  #define GCTIB_PREFIX MCC_GCTIB__
37  #define REF_PREFIX REF_
38  #define JARRAY_PREFIX A
39  
40  #define VTAB_PREFIX_STR TO_STR(VTAB_PREFIX)
41  #define ITAB_PREFIX_STR TO_STR(ITAB_PREFIX)
42  #define VTAB_AND_ITAB_PREFIX_STR TO_STR(VTAB_AND_ITAB_PREFIX)
43  #define ITAB_CONFLICT_PREFIX_STR TO_STR(ITAB_CONFLICT_PREFIX)
44  #define CLASSINFO_PREFIX_STR TO_STR(CLASSINFO_PREFIX)
45  #define CLASSINFO_RO_PREFIX_STR TO_STR(CLASSINFO_RO_PREFIX)
46  #define SUPERCLASSINFO_PREFIX_STR TO_STR(SUPERCLASSINFO_PREFIX)
47  #define PRIMITIVECLASSINFO_PREFIX_STR TO_STR(PRIMITIVECLASSINFO_PREFIX)
48  #define CLASS_INIT_BRIDGE_PREFIX_STR TO_STR(CLASS_INIT_BRIDGE_PREFIX)
49  #define GCTIB_PREFIX_STR TO_STR(GCTIB_PREFIX)
50  #define REF_PREFIX_STR TO_STR(REF_PREFIX)
51  #define JARRAY_PREFIX_STR TO_STR(JARRAY_PREFIX)
52  
53  // Names of all compiler-generated tables and accessed by runtime
54  static constexpr const char kMuidPrefixStr[] = "__muid_";
55  static constexpr const char kMuidRoPrefixStr[] = "__muid_ro";
56  static constexpr const char kMuidFuncDefTabPrefixStr[] = "__muid_func_def_tab";
57  static constexpr const char kMuidFuncDefOrigTabPrefixStr[] = "__muid_ro_func_def_orig_tab";
58  static constexpr const char kMuidFuncInfTabPrefixStr[] = "__muid_ro_func_inf_tab";
59  static constexpr const char kMuidFuncMuidIdxTabPrefixStr[] = "__muid_ro_func_muid_idx_tab";
60  static constexpr const char kMuidDataDefTabPrefixStr[] = "__muid_data_def_tab";
61  static constexpr const char kMuidDataDefOrigTabPrefixStr[] = "__muid_ro_data_def_orig_tab";
62  static constexpr const char kMuidFuncUndefTabPrefixStr[] = "__muid_func_undef_tab";
63  static constexpr const char kMuidDataUndefTabPrefixStr[] = "__muid_data_undef_tab";
64  static constexpr const char kMuidFuncDefMuidTabPrefixStr[] = "__muid_ro_func_def_muid_tab";
65  static constexpr const char kMuidDataDefMuidTabPrefixStr[] = "__muid_ro_data_def_muid_tab";
66  static constexpr const char kMuidFuncUndefMuidTabPrefixStr[] = "__muid_ro_func_undef_muid_tab";
67  static constexpr const char kMuidDataUndefMuidTabPrefixStr[] = "__muid_ro_data_undef_muid_tab";
68  static constexpr const char kMuidVtabAndItabPrefixStr[] = "__muid_vtab_and_itab";
69  static constexpr const char kMuidItabConflictPrefixStr[] = "__muid_itab_conflict";
70  static constexpr const char kMuidColdVtabAndItabPrefixStr[] = "__muid_cold_vtab_and_itab";
71  static constexpr const char kMuidColdItabConflictPrefixStr[] = "__muid_cold_itab_conflict";
72  static constexpr const char kMuidVtabOffsetPrefixStr[] = "__muid_vtab_offset_tab";
73  static constexpr const char kMuidFieldOffsetPrefixStr[] = "__muid_field_offset_tab";
74  static constexpr const char kMuidVtabOffsetKeyPrefixStr[] = "__muid_vtable_offset_key_tab";
75  static constexpr const char kMuidFieldOffsetKeyPrefixStr[] = "__muid_field_offset_key_tab";
76  static constexpr const char kMuidValueOffsetPrefixStr[] = "__muid_offset_value_table";
77  static constexpr const char kMuidLocalClassInfoStr[] = "__muid_local_classinfo_tab";
78  static constexpr const char kMuidSuperclassPrefixStr[] = "__muid_superclass";
79  static constexpr const char kMuidGlobalRootlistPrefixStr[] = "__muid_globalrootlist";
80  static constexpr const char kMuidClassMetadataPrefixStr[] = "__muid_classmetadata";
81  static constexpr const char kMuidClassMetadataBucketPrefixStr[] = "__muid_classmetadata_bucket";
82  static constexpr const char kMuidJavatextPrefixStr[] = "java_text";
83  static constexpr const char kMuidDataSectionStr[] = "__data_section";
84  static constexpr const char kMuidRangeTabPrefixStr[] = "__muid_range_tab";
85  static constexpr const char kMuidConststrPrefixStr[] = "__muid_conststr";
86  static constexpr const char kVtabOffsetTabStr[] = "__vtable_offset_table";
87  static constexpr const char kFieldOffsetTabKeyStr[] = "__field_offset_key_table";
88  static constexpr const char kFieldOffsetTabStr[] = "__field_offset_table";
89  static constexpr const char kVtableKeyOffsetTabStr[] = "__vtable_offset_key_table";
90  static constexpr const char kVtableOffsetTabKeyStr[] = "__vtable_offset_key_table";
91  static constexpr const char kFieldKeyOffsetTabStr[] = "__field_offset_table";
92  static constexpr const char kOffsetTabStr[] = "__offset_value_table";
93  static constexpr const char kInlineCacheTabStr[] = "__inline_cache_table";
94  static constexpr const char kLocalClassInfoStr[] = "__local_classinfo_table";
95  static constexpr const char kMethodsInfoPrefixStr[] = "__methods_info__";
96  static constexpr const char kMethodsInfoCompactPrefixStr[] = "__methods_infocompact__";
97  static constexpr const char kFieldsInfoPrefixStr[] = "__fields_info__";
98  static constexpr const char kFieldsInfoCompactPrefixStr[] = "__fields_infocompact__";
99  static constexpr const char kFieldOffsetDataPrefixStr[] = "__fieldOffsetData__";
100  static constexpr const char kMethodAddrDataPrefixStr[] = "__methodAddrData__";
101  static constexpr const char kMethodSignaturePrefixStr[] = "__methodSignature__";
102  static constexpr const char kParameterTypesPrefixStr[] = "__parameterTypes__";
103  static constexpr const char kRegJNITabPrefixStr[] = "__reg_jni_tab";
104  static constexpr const char kRegJNIFuncTabPrefixStr[] = "__reg_jni_func_tab";
105  static constexpr const char kReflectionStrtabPrefixStr[] = "__reflection_strtab";
106  static constexpr const char kReflectionStartHotStrtabPrefixStr[] = "__reflection_start_hot_strtab";
107  static constexpr const char kReflectionBothHotStrTabPrefixStr[] = "__reflection_both_hot_strtab";
108  static constexpr const char kReflectionRunHotStrtabPrefixStr[] = "__reflection_run_hot_strtab";
109  static constexpr const char kReflectionNoEmitStrtabPrefixStr[] = "__reflection_no_emit_strtab";
110  static constexpr const char kMarkMuidFuncDefStr[] = "muid_func_def:";
111  static constexpr const char kMarkMuidFuncUndefStr[] = "muid_func_undef:";
112  static constexpr const char kGcRootList[] = "gcRootNewList";
113  static constexpr const char kDecoupleOption[] = "__decouple_option";
114  static constexpr const char kDecoupleStr[] = "__decouple";
115  static constexpr const char kCompilerVersionNum[] = "__compilerVersionNum";
116  static constexpr const char kCompilerVersionNumStr[] = "__compilerVersionNumTab";
117  static constexpr const char kCompilerMfileStatus[] = "__compiler_mfile_status";
118  static constexpr const char kMapleGlobalVariable[] = "maple_global_variable";
119  static constexpr const char kMapleLiteralString[] = "maple_literal_string";
120  
121  static constexpr const char kSourceMuid[] = "__sourceMuid";
122  static constexpr const char kSourceMuidSectionStr[] = "__sourceMuidTab";
123  static constexpr const char kDecoupleStaticKeyStr[] = "__staticDecoupleKeyOffset";
124  static constexpr const char kDecoupleStaticValueStr[] = "__staticDecoupleValueOffset";
125  static constexpr const char kMarkDecoupleStaticStr[] = "decouple_static:";
126  static constexpr const char kClassInfoPrefix[] = "__cinf";
127  static constexpr const char kBssSectionStr[] = "__bss_section";
128  static constexpr const char kLinkerHashSoStr[] = "__linkerHashSo";
129  
130  static constexpr const char kStaticFieldNamePrefixStr[] = "__static_field_name";
131  static constexpr const char kMplSuffix[] = ".mpl";
132  static constexpr const char kClinvocation[] = ".clinvocation";
133  static constexpr const char kPackageNameSplitterStr[] = "_2F";
134  static constexpr const char kFileNameSplitterStr[] = "$$";
135  static constexpr const char kNameSplitterStr[] = "_7C";  // 7C is the ascii code for |
136  static constexpr const char kRightBracketStr[] = "_29";  // 29 is the ascii code for )
137  static constexpr const char kClassNameSplitterStr[] = "_3B_7C";
138  static constexpr const char kJavaLangClassStr[] = "Ljava_2Flang_2FClass_3B";
139  static constexpr const char kJavaLangObjectStr[] = "Ljava_2Flang_2FObject_3B";
140  static constexpr const char kJavaLangClassloader[] = "Ljava_2Flang_2FClassLoader_3B";
141  static constexpr const char kJavaLangObjectStrJVersion[] = "Ljava/lang/Object;";
142  static constexpr const char kJavaLangStringStr[] = "Ljava_2Flang_2FString_3B";
143  static constexpr const char kJavaLangExceptionStr[] = "Ljava_2Flang_2FException_3B";
144  static constexpr const char kThrowClassStr[] = "Ljava_2Flang_2FThrowable_3B";
145  static constexpr const char kReflectionClassesPrefixStr[] = "Ljava_2Flang_2Freflect_2F";
146  static constexpr const char kReflectionClassMethodStr[] = "Ljava_2Flang_2Freflect_2FMethod_241_3B";
147  static constexpr const char kClassMetadataTypeName[] = "__class_meta__";
148  static constexpr const char kPtrPrefixStr[] = "_PTR";
149  static constexpr const char kClassINfoPtrPrefixStr[] = "_PTR__cinf_";
150  static constexpr const char kArrayClassInfoPrefixStr[] = "__cinf_A";
151  static constexpr const char kShadowClassName[] = "shadow_24__klass__";
152  static constexpr const char kClinitSuffix[] = "_7C_3Cclinit_3E_7C_28_29V";
153  static constexpr const char kCinitStr[] = "_7C_3Cinit_3E_7C_28";
154  static constexpr const char kClinitSubStr[] = "7C_3Cinit_3E_7C";
155  
156  static constexpr const char kPreNativeFunc[] = "MCC_PreNativeCall";
157  static constexpr const char kPostNativeFunc[] = "MCC_PostNativeCall";
158  static constexpr const char kDecodeRefFunc[] = "MCC_DecodeReference";
159  static constexpr const char kFindNativeFunc[] = "MCC_FindNativeMethodPtr";
160  static constexpr const char kFindNativeFuncNoeh[] = "MCC_FindNativeMethodPtrWithoutException";
161  static constexpr const char kDummyNativeFunc[] = "MCC_DummyNativeMethodPtr";
162  static constexpr const char kCheckThrowPendingExceptionFunc[] = "MCC_CheckThrowPendingException";
163  static constexpr const char kCallFastNative[] = "MCC_CallFastNative";
164  static constexpr const char kCallFastNativeExt[] = "MCC_CallFastNativeExt";
165  static constexpr const char kCallSlowNativeExt[] = "MCC_CallSlowNativeExt";
166  static constexpr const char kSetReliableUnwindContextFunc[] = "MCC_SetReliableUnwindContext";
167  
168  static constexpr const char kArrayClassCacheTable[] = "__arrayClassCacheTable";
169  static constexpr const char kArrayClassCacheNameTable[] = "__muid_ro_arrayClassCacheNameTable";
170  static constexpr const char kFunctionLayoutStr[] = "__func_layout__";
171  
172  static constexpr const char kFunctionProfileTabPrefixStr[] = "__muid_profile_func_tab";
173  
174  static constexpr const char kBBProfileTabPrefixStr[] = "__muid_prof_counter_tab";
175  static constexpr const char kFuncIRProfInfTabPrefixStr[] = "__muid_prof_ir_desc_tab";
176  
177  static constexpr const char kprefixProfModDesc[] = "__mpl_prof_moddesc_";
178  static constexpr const char kprefixProfCtrTbl[] = "__mpl_prof_ctrtbl_";
179  static constexpr const char kprefixProfFuncDesc[] = "__mpl_prof_funcdesc_";
180  static constexpr const char kprefixProfFuncDescTbl[] = "__mpl_func_prof_desc_tbl_";
181  static constexpr const char kprefixProfInit[] = "__mpl_prof_init_";
182  static constexpr const char kprefixProfExit[] = "__mpl_prof_exit_";
183  static constexpr const char kGCCProfInit[] = "__gcov_init";
184  static constexpr const char kGCCProfExit[] = "__gcov_exit";
185  static constexpr const char kMplMergeFuncAdd[] = "__gcov_merge_add";
186  static constexpr const char kProfFileNameExt[] = ".gcda";
187  
188  static constexpr const char kBindingProtectedRegionStr[] = "__BindingProtectRegion__";
189  
190  static constexpr const char kClassNamePrefixStr[] = "L";
191  static constexpr const char kClassMethodSplitterStr[] = "_3B";
192  static constexpr const char kFuncGetCurrentCl[] = "MCC_GetCurrentClassLoader";
193  // Serve as a global flag to indicate whether frequent strings have been compressed
194  extern bool doCompression;
195  
196  // Return the input string if the compression is not on; otherwise, return its compressed version
197  std::string GetInternalNameLiteral(std::string name);
198  std::string GetOriginalNameLiteral(std::string name);
199  
200  std::string EncodeName(const std::string &name);
201  std::string DecodeName(const std::string &name);
202  void DecodeMapleNameToJavaDescriptor(const std::string &nameIn, std::string &nameOut);
203  
204  std::string NativeJavaName(const std::string &name, bool overLoaded = true);
205  
206  __attribute__((visibility("default"))) unsigned UTF16ToUTF8(std::string &str, const std::u16string &str16,
207                                                              unsigned short num = 0, bool isBigEndian = false);
208  __attribute__((visibility("default"))) unsigned UTF8ToUTF16(std::u16string &str16, const std::string &str,
209                                                              unsigned short num = 0, bool isBigEndian = false);
210  void GetUnsignedLeb128Encode(std::vector<uint8_t> &dest, uint32_t value);
211  uint32_t GetUnsignedLeb128Decode(const uint8_t **data);
212  uint64_t GetUleb128Encode(uint64_t val);
213  uint64_t GetSleb128Encode(int64_t val);
214  uint64_t GetUleb128Decode(uint64_t val);
215  int64_t GetSleb128Decode(uint64_t val);
216  size_t GetUleb128Size(uint64_t val);
217  size_t GetSleb128Size(int32_t val);
218  bool NeedConvertUTF16(const std::string &str8);
219  }  // namespace namemangler
220  
221  #endif
222