1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 #include <sys/types.h> 2 #include <sys/types.h> 3 #include <stdio.h> 3 #include <stdio.h> 4 #include <stdlib.h> 4 #include <stdlib.h> 5 #include <string.h> 5 #include <string.h> 6 #include "symbol.h" 6 #include "symbol.h" 7 7 8 #include "demangle-java.h" 8 #include "demangle-java.h" 9 9 10 #include <linux/ctype.h> 10 #include <linux/ctype.h> 11 #include <linux/kernel.h> 11 #include <linux/kernel.h> 12 12 13 enum { 13 enum { 14 MODE_PREFIX = 0, 14 MODE_PREFIX = 0, 15 MODE_CLASS = 1, 15 MODE_CLASS = 1, 16 MODE_FUNC = 2, 16 MODE_FUNC = 2, 17 MODE_TYPE = 3, 17 MODE_TYPE = 3, 18 MODE_CTYPE = 4, /* class arg */ !! 18 MODE_CTYPE = 3, /* class arg */ 19 }; 19 }; 20 20 21 #define BASE_ENT(c, n) [c - 'A']=n 21 #define BASE_ENT(c, n) [c - 'A']=n 22 static const char *base_types['Z' - 'A' + 1] = 22 static const char *base_types['Z' - 'A' + 1] = { 23 BASE_ENT('B', "byte" ), 23 BASE_ENT('B', "byte" ), 24 BASE_ENT('C', "char" ), 24 BASE_ENT('C', "char" ), 25 BASE_ENT('D', "double" ), 25 BASE_ENT('D', "double" ), 26 BASE_ENT('F', "float" ), 26 BASE_ENT('F', "float" ), 27 BASE_ENT('I', "int" ), 27 BASE_ENT('I', "int" ), 28 BASE_ENT('J', "long" ), 28 BASE_ENT('J', "long" ), 29 BASE_ENT('S', "short" ), 29 BASE_ENT('S', "short" ), 30 BASE_ENT('Z', "boolean" ), !! 30 BASE_ENT('Z', "bool" ), 31 }; 31 }; 32 32 33 /* 33 /* 34 * demangle Java symbol between str and end po 34 * demangle Java symbol between str and end positions and stores 35 * up to maxlen characters into buf. The parse 35 * up to maxlen characters into buf. The parser starts in mode. 36 * 36 * 37 * Use MODE_PREFIX to process entire prototype 37 * Use MODE_PREFIX to process entire prototype till end position 38 * Use MODE_TYPE to process return type if str 38 * Use MODE_TYPE to process return type if str starts on return type char 39 * 39 * 40 * Return: 40 * Return: 41 * success: buf 41 * success: buf 42 * error : NULL 42 * error : NULL 43 */ 43 */ 44 static char * 44 static char * 45 __demangle_java_sym(const char *str, const cha 45 __demangle_java_sym(const char *str, const char *end, char *buf, int maxlen, int mode) 46 { 46 { 47 int rlen = 0; 47 int rlen = 0; 48 int array = 0; 48 int array = 0; 49 int narg = 0; 49 int narg = 0; 50 const char *q; 50 const char *q; 51 51 52 if (!end) 52 if (!end) 53 end = str + strlen(str); 53 end = str + strlen(str); 54 54 55 for (q = str; q != end; q++) { 55 for (q = str; q != end; q++) { 56 56 57 if (rlen == (maxlen - 1)) 57 if (rlen == (maxlen - 1)) 58 break; 58 break; 59 59 60 switch (*q) { 60 switch (*q) { 61 case 'L': 61 case 'L': 62 if (mode == MODE_PREFI !! 62 if (mode == MODE_PREFIX || mode == MODE_CTYPE) { 63 if (mode == MO !! 63 if (mode == MODE_CTYPE) { 64 if (na 64 if (narg) 65 65 rlen += scnprintf(buf + rlen, maxlen - rlen, ", "); 66 narg++ 66 narg++; 67 } 67 } >> 68 rlen += scnprintf(buf + rlen, maxlen - rlen, "class "); 68 if (mode == MO 69 if (mode == MODE_PREFIX) 69 mode = 70 mode = MODE_CLASS; 70 else << 71 mode = << 72 } else 71 } else 73 buf[rlen++] = 72 buf[rlen++] = *q; 74 break; 73 break; 75 case 'B': 74 case 'B': 76 case 'C': 75 case 'C': 77 case 'D': 76 case 'D': 78 case 'F': 77 case 'F': 79 case 'I': 78 case 'I': 80 case 'J': 79 case 'J': 81 case 'S': 80 case 'S': 82 case 'Z': 81 case 'Z': 83 if (mode == MODE_TYPE) 82 if (mode == MODE_TYPE) { 84 if (narg) 83 if (narg) 85 rlen + 84 rlen += scnprintf(buf + rlen, maxlen - rlen, ", "); 86 rlen += scnpri 85 rlen += scnprintf(buf + rlen, maxlen - rlen, "%s", base_types[*q - 'A']); 87 while (array-- 86 while (array--) 88 rlen + 87 rlen += scnprintf(buf + rlen, maxlen - rlen, "[]"); 89 array = 0; 88 array = 0; 90 narg++; 89 narg++; 91 } else 90 } else 92 buf[rlen++] = 91 buf[rlen++] = *q; 93 break; 92 break; 94 case 'V': 93 case 'V': 95 if (mode == MODE_TYPE) 94 if (mode == MODE_TYPE) { 96 rlen += scnpri 95 rlen += scnprintf(buf + rlen, maxlen - rlen, "void"); 97 while (array-- 96 while (array--) 98 rlen + 97 rlen += scnprintf(buf + rlen, maxlen - rlen, "[]"); 99 array = 0; 98 array = 0; 100 } else 99 } else 101 buf[rlen++] = 100 buf[rlen++] = *q; 102 break; 101 break; 103 case '[': 102 case '[': 104 if (mode != MODE_TYPE) 103 if (mode != MODE_TYPE) 105 goto error; 104 goto error; 106 array++; 105 array++; 107 break; 106 break; 108 case '(': 107 case '(': 109 if (mode != MODE_FUNC) 108 if (mode != MODE_FUNC) 110 goto error; 109 goto error; 111 buf[rlen++] = *q; 110 buf[rlen++] = *q; 112 mode = MODE_TYPE; 111 mode = MODE_TYPE; 113 break; 112 break; 114 case ')': 113 case ')': 115 if (mode != MODE_TYPE) 114 if (mode != MODE_TYPE) 116 goto error; 115 goto error; 117 buf[rlen++] = *q; 116 buf[rlen++] = *q; 118 narg = 0; 117 narg = 0; 119 break; 118 break; 120 case ';': 119 case ';': 121 if (mode != MODE_CLASS 120 if (mode != MODE_CLASS && mode != MODE_CTYPE) 122 goto error; 121 goto error; 123 /* safe because at lea 122 /* safe because at least one other char to process */ 124 if (isalpha(*(q + 1)) !! 123 if (isalpha(*(q + 1))) 125 rlen += scnpri 124 rlen += scnprintf(buf + rlen, maxlen - rlen, "."); 126 if (mode == MODE_CLASS 125 if (mode == MODE_CLASS) 127 mode = MODE_FU 126 mode = MODE_FUNC; 128 else if (mode == MODE_ 127 else if (mode == MODE_CTYPE) 129 mode = MODE_TY 128 mode = MODE_TYPE; 130 break; 129 break; 131 case '/': 130 case '/': 132 if (mode != MODE_CLASS 131 if (mode != MODE_CLASS && mode != MODE_CTYPE) 133 goto error; 132 goto error; 134 rlen += scnprintf(buf 133 rlen += scnprintf(buf + rlen, maxlen - rlen, "."); 135 break; 134 break; 136 default : 135 default : 137 buf[rlen++] = *q; 136 buf[rlen++] = *q; 138 } 137 } 139 } 138 } 140 buf[rlen] = '\0'; 139 buf[rlen] = '\0'; 141 return buf; 140 return buf; 142 error: 141 error: 143 return NULL; 142 return NULL; 144 } 143 } 145 144 146 /* 145 /* 147 * Demangle Java function signature (openJDK, 146 * Demangle Java function signature (openJDK, not GCJ) 148 * input: 147 * input: 149 * str: string to parse. String is not mo 148 * str: string to parse. String is not modified 150 * flags: combination of JAVA_DEMANGLE_* fl !! 149 * flags: comobination of JAVA_DEMANGLE_* flags to modify demangling 151 * return: 150 * return: 152 * if input can be demangled, then a newl 151 * if input can be demangled, then a newly allocated string is returned. 153 * if input cannot be demangled, then NUL 152 * if input cannot be demangled, then NULL is returned 154 * 153 * 155 * Note: caller is responsible for freeing dem 154 * Note: caller is responsible for freeing demangled string 156 */ 155 */ 157 char * 156 char * 158 java_demangle_sym(const char *str, int flags) 157 java_demangle_sym(const char *str, int flags) 159 { 158 { 160 char *buf, *ptr; 159 char *buf, *ptr; 161 char *p; 160 char *p; 162 size_t len, l1 = 0; 161 size_t len, l1 = 0; 163 162 164 if (!str) 163 if (!str) 165 return NULL; 164 return NULL; 166 165 167 /* find start of return type */ !! 166 /* find start of retunr type */ 168 p = strrchr(str, ')'); 167 p = strrchr(str, ')'); 169 if (!p) 168 if (!p) 170 return NULL; 169 return NULL; 171 170 172 /* 171 /* 173 * expansion factor estimated to 3x 172 * expansion factor estimated to 3x 174 */ 173 */ 175 len = strlen(str) * 3 + 1; 174 len = strlen(str) * 3 + 1; 176 buf = malloc(len); 175 buf = malloc(len); 177 if (!buf) 176 if (!buf) 178 return NULL; 177 return NULL; 179 178 180 buf[0] = '\0'; 179 buf[0] = '\0'; 181 if (!(flags & JAVA_DEMANGLE_NORET)) { 180 if (!(flags & JAVA_DEMANGLE_NORET)) { 182 /* 181 /* 183 * get return type first 182 * get return type first 184 */ 183 */ 185 ptr = __demangle_java_sym(p + 184 ptr = __demangle_java_sym(p + 1, NULL, buf, len, MODE_TYPE); 186 if (!ptr) 185 if (!ptr) 187 goto error; 186 goto error; 188 187 189 /* add space between return ty 188 /* add space between return type and function prototype */ 190 l1 = strlen(buf); 189 l1 = strlen(buf); 191 buf[l1++] = ' '; 190 buf[l1++] = ' '; 192 } 191 } 193 192 194 /* process function up to return type 193 /* process function up to return type */ 195 ptr = __demangle_java_sym(str, p + 1, 194 ptr = __demangle_java_sym(str, p + 1, buf + l1, len - l1, MODE_PREFIX); 196 if (!ptr) 195 if (!ptr) 197 goto error; 196 goto error; 198 197 199 return buf; 198 return buf; 200 error: 199 error: 201 free(buf); 200 free(buf); 202 return NULL; 201 return NULL; 203 } 202 } 204 203
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.