David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | * Implementation file of the dexlayout utility. |
| 17 | * |
| 18 | * This is a tool to read dex files into an internal representation, |
| 19 | * reorganize the representation, and emit dex files with a better |
| 20 | * file layout. |
| 21 | */ |
| 22 | |
| 23 | #include "dexlayout.h" |
| 24 | |
| 25 | #include <inttypes.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include <iostream> |
| 29 | #include <memory> |
| 30 | #include <sstream> |
| 31 | #include <vector> |
| 32 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 33 | #include "android-base/stringprintf.h" |
| 34 | |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 35 | #include "dex_ir_builder.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Jeff Hao | b756815 | 2017-03-09 18:14:48 -0800 | [diff] [blame] | 37 | #include "dex_file_verifier.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 38 | #include "dex_instruction-inl.h" |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame^] | 39 | #include "dex_verify.h" |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 40 | #include "dex_visualize.h" |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 41 | #include "dex_writer.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 42 | #include "jit/profile_compilation_info.h" |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 43 | #include "mem_map.h" |
Nicolas Geoffray | fd1a6c2 | 2016-10-04 11:01:17 +0000 | [diff] [blame] | 44 | #include "os.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 45 | #include "utils.h" |
| 46 | |
| 47 | namespace art { |
| 48 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 49 | using android::base::StringPrintf; |
| 50 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 51 | static constexpr uint32_t kDexCodeItemAlignment = 4; |
| 52 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 53 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 54 | * Flags for use with createAccessFlagStr(). |
| 55 | */ |
| 56 | enum AccessFor { |
| 57 | kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX |
| 58 | }; |
| 59 | const int kNumFlags = 18; |
| 60 | |
| 61 | /* |
| 62 | * Gets 2 little-endian bytes. |
| 63 | */ |
| 64 | static inline uint16_t Get2LE(unsigned char const* src) { |
| 65 | return src[0] | (src[1] << 8); |
| 66 | } |
| 67 | |
| 68 | /* |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 69 | * Converts a type descriptor to human-readable "dotted" form. For |
| 70 | * example, "Ljava/lang/String;" becomes "java.lang.String", and |
| 71 | * "[I" becomes "int[]". Also converts '$' to '.', which means this |
| 72 | * form can't be converted back to a descriptor. |
| 73 | */ |
| 74 | static std::string DescriptorToDotWrapper(const char* descriptor) { |
| 75 | std::string result = DescriptorToDot(descriptor); |
| 76 | size_t found = result.find('$'); |
| 77 | while (found != std::string::npos) { |
| 78 | result[found] = '.'; |
| 79 | found = result.find('$', found); |
| 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 85 | * Converts the class name portion of a type descriptor to human-readable |
| 86 | * "dotted" form. For example, "Ljava/lang/String;" becomes "String". |
| 87 | */ |
| 88 | static std::string DescriptorClassToDot(const char* str) { |
| 89 | std::string descriptor(str); |
| 90 | // Reduce to just the class name prefix. |
| 91 | size_t last_slash = descriptor.rfind('/'); |
| 92 | if (last_slash == std::string::npos) { |
| 93 | last_slash = 0; |
| 94 | } |
| 95 | // Start past the '/' or 'L'. |
| 96 | last_slash++; |
| 97 | |
| 98 | // Copy class name over, trimming trailing ';'. |
| 99 | size_t size = descriptor.size() - 1 - last_slash; |
| 100 | std::string result(descriptor.substr(last_slash, size)); |
| 101 | |
| 102 | // Replace '$' with '.'. |
| 103 | size_t dollar_sign = result.find('$'); |
| 104 | while (dollar_sign != std::string::npos) { |
| 105 | result[dollar_sign] = '.'; |
| 106 | dollar_sign = result.find('$', dollar_sign); |
| 107 | } |
| 108 | |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Returns string representing the boolean value. |
| 114 | */ |
| 115 | static const char* StrBool(bool val) { |
| 116 | return val ? "true" : "false"; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Returns a quoted string representing the boolean value. |
| 121 | */ |
| 122 | static const char* QuotedBool(bool val) { |
| 123 | return val ? "\"true\"" : "\"false\""; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Returns a quoted string representing the access flags. |
| 128 | */ |
| 129 | static const char* QuotedVisibility(uint32_t access_flags) { |
| 130 | if (access_flags & kAccPublic) { |
| 131 | return "\"public\""; |
| 132 | } else if (access_flags & kAccProtected) { |
| 133 | return "\"protected\""; |
| 134 | } else if (access_flags & kAccPrivate) { |
| 135 | return "\"private\""; |
| 136 | } else { |
| 137 | return "\"package\""; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Counts the number of '1' bits in a word. |
| 143 | */ |
| 144 | static int CountOnes(uint32_t val) { |
| 145 | val = val - ((val >> 1) & 0x55555555); |
| 146 | val = (val & 0x33333333) + ((val >> 2) & 0x33333333); |
| 147 | return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Creates a new string with human-readable access flags. |
| 152 | * |
| 153 | * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t. |
| 154 | */ |
| 155 | static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) { |
| 156 | static const char* kAccessStrings[kAccessForMAX][kNumFlags] = { |
| 157 | { |
| 158 | "PUBLIC", /* 0x00001 */ |
| 159 | "PRIVATE", /* 0x00002 */ |
| 160 | "PROTECTED", /* 0x00004 */ |
| 161 | "STATIC", /* 0x00008 */ |
| 162 | "FINAL", /* 0x00010 */ |
| 163 | "?", /* 0x00020 */ |
| 164 | "?", /* 0x00040 */ |
| 165 | "?", /* 0x00080 */ |
| 166 | "?", /* 0x00100 */ |
| 167 | "INTERFACE", /* 0x00200 */ |
| 168 | "ABSTRACT", /* 0x00400 */ |
| 169 | "?", /* 0x00800 */ |
| 170 | "SYNTHETIC", /* 0x01000 */ |
| 171 | "ANNOTATION", /* 0x02000 */ |
| 172 | "ENUM", /* 0x04000 */ |
| 173 | "?", /* 0x08000 */ |
| 174 | "VERIFIED", /* 0x10000 */ |
| 175 | "OPTIMIZED", /* 0x20000 */ |
| 176 | }, { |
| 177 | "PUBLIC", /* 0x00001 */ |
| 178 | "PRIVATE", /* 0x00002 */ |
| 179 | "PROTECTED", /* 0x00004 */ |
| 180 | "STATIC", /* 0x00008 */ |
| 181 | "FINAL", /* 0x00010 */ |
| 182 | "SYNCHRONIZED", /* 0x00020 */ |
| 183 | "BRIDGE", /* 0x00040 */ |
| 184 | "VARARGS", /* 0x00080 */ |
| 185 | "NATIVE", /* 0x00100 */ |
| 186 | "?", /* 0x00200 */ |
| 187 | "ABSTRACT", /* 0x00400 */ |
| 188 | "STRICT", /* 0x00800 */ |
| 189 | "SYNTHETIC", /* 0x01000 */ |
| 190 | "?", /* 0x02000 */ |
| 191 | "?", /* 0x04000 */ |
| 192 | "MIRANDA", /* 0x08000 */ |
| 193 | "CONSTRUCTOR", /* 0x10000 */ |
| 194 | "DECLARED_SYNCHRONIZED", /* 0x20000 */ |
| 195 | }, { |
| 196 | "PUBLIC", /* 0x00001 */ |
| 197 | "PRIVATE", /* 0x00002 */ |
| 198 | "PROTECTED", /* 0x00004 */ |
| 199 | "STATIC", /* 0x00008 */ |
| 200 | "FINAL", /* 0x00010 */ |
| 201 | "?", /* 0x00020 */ |
| 202 | "VOLATILE", /* 0x00040 */ |
| 203 | "TRANSIENT", /* 0x00080 */ |
| 204 | "?", /* 0x00100 */ |
| 205 | "?", /* 0x00200 */ |
| 206 | "?", /* 0x00400 */ |
| 207 | "?", /* 0x00800 */ |
| 208 | "SYNTHETIC", /* 0x01000 */ |
| 209 | "?", /* 0x02000 */ |
| 210 | "ENUM", /* 0x04000 */ |
| 211 | "?", /* 0x08000 */ |
| 212 | "?", /* 0x10000 */ |
| 213 | "?", /* 0x20000 */ |
| 214 | }, |
| 215 | }; |
| 216 | |
| 217 | // Allocate enough storage to hold the expected number of strings, |
| 218 | // plus a space between each. We over-allocate, using the longest |
| 219 | // string above as the base metric. |
| 220 | const int kLongest = 21; // The strlen of longest string above. |
| 221 | const int count = CountOnes(flags); |
| 222 | char* str; |
| 223 | char* cp; |
| 224 | cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1)); |
| 225 | |
| 226 | for (int i = 0; i < kNumFlags; i++) { |
| 227 | if (flags & 0x01) { |
| 228 | const char* accessStr = kAccessStrings[for_what][i]; |
| 229 | const int len = strlen(accessStr); |
| 230 | if (cp != str) { |
| 231 | *cp++ = ' '; |
| 232 | } |
| 233 | memcpy(cp, accessStr, len); |
| 234 | cp += len; |
| 235 | } |
| 236 | flags >>= 1; |
| 237 | } // for |
| 238 | |
| 239 | *cp = '\0'; |
| 240 | return str; |
| 241 | } |
| 242 | |
| 243 | static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) { |
| 244 | if (proto == nullptr) { |
| 245 | return "<no signature>"; |
| 246 | } |
| 247 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 248 | std::string result("("); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 249 | const dex_ir::TypeList* type_list = proto->Parameters(); |
| 250 | if (type_list != nullptr) { |
| 251 | for (const dex_ir::TypeId* type_id : *type_list->GetTypeList()) { |
| 252 | result += type_id->GetStringId()->Data(); |
| 253 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 254 | } |
| 255 | result += ")"; |
| 256 | result += proto->ReturnType()->GetStringId()->Data(); |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Copies character data from "data" to "out", converting non-ASCII values |
| 262 | * to fprintf format chars or an ASCII filler ('.' or '?'). |
| 263 | * |
| 264 | * The output buffer must be able to hold (2*len)+1 bytes. The result is |
| 265 | * NULL-terminated. |
| 266 | */ |
| 267 | static void Asciify(char* out, const unsigned char* data, size_t len) { |
| 268 | while (len--) { |
| 269 | if (*data < 0x20) { |
| 270 | // Could do more here, but we don't need them yet. |
| 271 | switch (*data) { |
| 272 | case '\0': |
| 273 | *out++ = '\\'; |
| 274 | *out++ = '0'; |
| 275 | break; |
| 276 | case '\n': |
| 277 | *out++ = '\\'; |
| 278 | *out++ = 'n'; |
| 279 | break; |
| 280 | default: |
| 281 | *out++ = '.'; |
| 282 | break; |
| 283 | } // switch |
| 284 | } else if (*data >= 0x80) { |
| 285 | *out++ = '?'; |
| 286 | } else { |
| 287 | *out++ = *data; |
| 288 | } |
| 289 | data++; |
| 290 | } // while |
| 291 | *out = '\0'; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Dumps a string value with some escape characters. |
| 296 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 297 | static void DumpEscapedString(const char* p, FILE* out_file) { |
| 298 | fputs("\"", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 299 | for (; *p; p++) { |
| 300 | switch (*p) { |
| 301 | case '\\': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 302 | fputs("\\\\", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 303 | break; |
| 304 | case '\"': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 305 | fputs("\\\"", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 306 | break; |
| 307 | case '\t': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 308 | fputs("\\t", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 309 | break; |
| 310 | case '\n': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 311 | fputs("\\n", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 312 | break; |
| 313 | case '\r': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 314 | fputs("\\r", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 315 | break; |
| 316 | default: |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 317 | putc(*p, out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 318 | } // switch |
| 319 | } // for |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 320 | fputs("\"", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Dumps a string as an XML attribute value. |
| 325 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 326 | static void DumpXmlAttribute(const char* p, FILE* out_file) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 327 | for (; *p; p++) { |
| 328 | switch (*p) { |
| 329 | case '&': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 330 | fputs("&", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 331 | break; |
| 332 | case '<': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 333 | fputs("<", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 334 | break; |
| 335 | case '>': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 336 | fputs(">", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 337 | break; |
| 338 | case '"': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 339 | fputs(""", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 340 | break; |
| 341 | case '\t': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 342 | fputs("	", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 343 | break; |
| 344 | case '\n': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 345 | fputs("
", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 346 | break; |
| 347 | case '\r': |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 348 | fputs("
", out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 349 | break; |
| 350 | default: |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 351 | putc(*p, out_file); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 352 | } // switch |
| 353 | } // for |
| 354 | } |
| 355 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 356 | /* |
| 357 | * Helper for dumpInstruction(), which builds the string |
| 358 | * representation for the index in the given instruction. |
| 359 | * Returns a pointer to a buffer of sufficient size. |
| 360 | */ |
| 361 | static std::unique_ptr<char[]> IndexString(dex_ir::Header* header, |
| 362 | const Instruction* dec_insn, |
| 363 | size_t buf_size) { |
| 364 | std::unique_ptr<char[]> buf(new char[buf_size]); |
| 365 | // Determine index and width of the string. |
| 366 | uint32_t index = 0; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 367 | uint32_t secondary_index = DexFile::kDexNoIndex; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 368 | uint32_t width = 4; |
| 369 | switch (Instruction::FormatOf(dec_insn->Opcode())) { |
| 370 | // SOME NOT SUPPORTED: |
| 371 | // case Instruction::k20bc: |
| 372 | case Instruction::k21c: |
| 373 | case Instruction::k35c: |
| 374 | // case Instruction::k35ms: |
| 375 | case Instruction::k3rc: |
| 376 | // case Instruction::k3rms: |
| 377 | // case Instruction::k35mi: |
| 378 | // case Instruction::k3rmi: |
| 379 | index = dec_insn->VRegB(); |
| 380 | width = 4; |
| 381 | break; |
| 382 | case Instruction::k31c: |
| 383 | index = dec_insn->VRegB(); |
| 384 | width = 8; |
| 385 | break; |
| 386 | case Instruction::k22c: |
| 387 | // case Instruction::k22cs: |
| 388 | index = dec_insn->VRegC(); |
| 389 | width = 4; |
| 390 | break; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 391 | case Instruction::k45cc: |
| 392 | case Instruction::k4rcc: |
| 393 | index = dec_insn->VRegB(); |
| 394 | secondary_index = dec_insn->VRegH(); |
| 395 | width = 4; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 396 | default: |
| 397 | break; |
| 398 | } // switch |
| 399 | |
| 400 | // Determine index type. |
| 401 | size_t outSize = 0; |
| 402 | switch (Instruction::IndexTypeOf(dec_insn->Opcode())) { |
| 403 | case Instruction::kIndexUnknown: |
| 404 | // This function should never get called for this type, but do |
| 405 | // something sensible here, just to help with debugging. |
| 406 | outSize = snprintf(buf.get(), buf_size, "<unknown-index>"); |
| 407 | break; |
| 408 | case Instruction::kIndexNone: |
| 409 | // This function should never get called for this type, but do |
| 410 | // something sensible here, just to help with debugging. |
| 411 | outSize = snprintf(buf.get(), buf_size, "<no-index>"); |
| 412 | break; |
| 413 | case Instruction::kIndexTypeRef: |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 414 | if (index < header->GetCollections().TypeIdsSize()) { |
| 415 | const char* tp = header->GetCollections().GetTypeId(index)->GetStringId()->Data(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 416 | outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index); |
| 417 | } else { |
| 418 | outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index); |
| 419 | } |
| 420 | break; |
| 421 | case Instruction::kIndexStringRef: |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 422 | if (index < header->GetCollections().StringIdsSize()) { |
| 423 | const char* st = header->GetCollections().GetStringId(index)->Data(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 424 | outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index); |
| 425 | } else { |
| 426 | outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index); |
| 427 | } |
| 428 | break; |
| 429 | case Instruction::kIndexMethodRef: |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 430 | if (index < header->GetCollections().MethodIdsSize()) { |
| 431 | dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 432 | const char* name = method_id->Name()->Data(); |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 433 | std::string type_descriptor = GetSignatureForProtoId(method_id->Proto()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 434 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 435 | outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x", |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 436 | back_descriptor, name, type_descriptor.c_str(), width, index); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 437 | } else { |
| 438 | outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index); |
| 439 | } |
| 440 | break; |
| 441 | case Instruction::kIndexFieldRef: |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 442 | if (index < header->GetCollections().FieldIdsSize()) { |
| 443 | dex_ir::FieldId* field_id = header->GetCollections().GetFieldId(index); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 444 | const char* name = field_id->Name()->Data(); |
| 445 | const char* type_descriptor = field_id->Type()->GetStringId()->Data(); |
| 446 | const char* back_descriptor = field_id->Class()->GetStringId()->Data(); |
| 447 | outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x", |
| 448 | back_descriptor, name, type_descriptor, width, index); |
| 449 | } else { |
| 450 | outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index); |
| 451 | } |
| 452 | break; |
| 453 | case Instruction::kIndexVtableOffset: |
| 454 | outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x", |
| 455 | width, index, width, index); |
| 456 | break; |
| 457 | case Instruction::kIndexFieldOffset: |
| 458 | outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index); |
| 459 | break; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 460 | case Instruction::kIndexMethodAndProtoRef: { |
| 461 | std::string method("<method?>"); |
| 462 | std::string proto("<proto?>"); |
| 463 | if (index < header->GetCollections().MethodIdsSize()) { |
| 464 | dex_ir::MethodId* method_id = header->GetCollections().GetMethodId(index); |
| 465 | const char* name = method_id->Name()->Data(); |
| 466 | std::string type_descriptor = GetSignatureForProtoId(method_id->Proto()); |
| 467 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 468 | method = StringPrintf("%s.%s:%s", back_descriptor, name, type_descriptor.c_str()); |
| 469 | } |
| 470 | if (secondary_index < header->GetCollections().ProtoIdsSize()) { |
| 471 | dex_ir::ProtoId* proto_id = header->GetCollections().GetProtoId(secondary_index); |
| 472 | proto = GetSignatureForProtoId(proto_id); |
| 473 | } |
| 474 | outSize = snprintf(buf.get(), buf_size, "%s, %s // method@%0*x, proto@%0*x", |
| 475 | method.c_str(), proto.c_str(), width, index, width, secondary_index); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 476 | } |
| 477 | break; |
| 478 | // SOME NOT SUPPORTED: |
| 479 | // case Instruction::kIndexVaries: |
| 480 | // case Instruction::kIndexInlineMethod: |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 481 | default: |
| 482 | outSize = snprintf(buf.get(), buf_size, "<?>"); |
| 483 | break; |
| 484 | } // switch |
| 485 | |
| 486 | // Determine success of string construction. |
| 487 | if (outSize >= buf_size) { |
| 488 | // The buffer wasn't big enough; retry with computed size. Note: snprintf() |
| 489 | // doesn't count/ the '\0' as part of its returned size, so we add explicit |
| 490 | // space for it here. |
| 491 | return IndexString(header, dec_insn, outSize + 1); |
| 492 | } |
| 493 | return buf; |
| 494 | } |
| 495 | |
| 496 | /* |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 497 | * Dumps encoded annotation. |
| 498 | */ |
| 499 | void DexLayout::DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation) { |
| 500 | fputs(annotation->GetType()->GetStringId()->Data(), out_file_); |
| 501 | // Display all name=value pairs. |
| 502 | for (auto& subannotation : *annotation->GetAnnotationElements()) { |
| 503 | fputc(' ', out_file_); |
| 504 | fputs(subannotation->GetName()->Data(), out_file_); |
| 505 | fputc('=', out_file_); |
| 506 | DumpEncodedValue(subannotation->GetValue()); |
| 507 | } |
| 508 | } |
| 509 | /* |
| 510 | * Dumps encoded value. |
| 511 | */ |
| 512 | void DexLayout::DumpEncodedValue(const dex_ir::EncodedValue* data) { |
| 513 | switch (data->Type()) { |
| 514 | case DexFile::kDexAnnotationByte: |
| 515 | fprintf(out_file_, "%" PRId8, data->GetByte()); |
| 516 | break; |
| 517 | case DexFile::kDexAnnotationShort: |
| 518 | fprintf(out_file_, "%" PRId16, data->GetShort()); |
| 519 | break; |
| 520 | case DexFile::kDexAnnotationChar: |
| 521 | fprintf(out_file_, "%" PRIu16, data->GetChar()); |
| 522 | break; |
| 523 | case DexFile::kDexAnnotationInt: |
| 524 | fprintf(out_file_, "%" PRId32, data->GetInt()); |
| 525 | break; |
| 526 | case DexFile::kDexAnnotationLong: |
| 527 | fprintf(out_file_, "%" PRId64, data->GetLong()); |
| 528 | break; |
| 529 | case DexFile::kDexAnnotationFloat: { |
| 530 | fprintf(out_file_, "%g", data->GetFloat()); |
| 531 | break; |
| 532 | } |
| 533 | case DexFile::kDexAnnotationDouble: { |
| 534 | fprintf(out_file_, "%g", data->GetDouble()); |
| 535 | break; |
| 536 | } |
| 537 | case DexFile::kDexAnnotationString: { |
| 538 | dex_ir::StringId* string_id = data->GetStringId(); |
| 539 | if (options_.output_format_ == kOutputPlain) { |
| 540 | DumpEscapedString(string_id->Data(), out_file_); |
| 541 | } else { |
| 542 | DumpXmlAttribute(string_id->Data(), out_file_); |
| 543 | } |
| 544 | break; |
| 545 | } |
| 546 | case DexFile::kDexAnnotationType: { |
| 547 | dex_ir::TypeId* type_id = data->GetTypeId(); |
| 548 | fputs(type_id->GetStringId()->Data(), out_file_); |
| 549 | break; |
| 550 | } |
| 551 | case DexFile::kDexAnnotationField: |
| 552 | case DexFile::kDexAnnotationEnum: { |
| 553 | dex_ir::FieldId* field_id = data->GetFieldId(); |
| 554 | fputs(field_id->Name()->Data(), out_file_); |
| 555 | break; |
| 556 | } |
| 557 | case DexFile::kDexAnnotationMethod: { |
| 558 | dex_ir::MethodId* method_id = data->GetMethodId(); |
| 559 | fputs(method_id->Name()->Data(), out_file_); |
| 560 | break; |
| 561 | } |
| 562 | case DexFile::kDexAnnotationArray: { |
| 563 | fputc('{', out_file_); |
| 564 | // Display all elements. |
| 565 | for (auto& value : *data->GetEncodedArray()->GetEncodedValues()) { |
| 566 | fputc(' ', out_file_); |
| 567 | DumpEncodedValue(value.get()); |
| 568 | } |
| 569 | fputs(" }", out_file_); |
| 570 | break; |
| 571 | } |
| 572 | case DexFile::kDexAnnotationAnnotation: { |
| 573 | DumpEncodedAnnotation(data->GetEncodedAnnotation()); |
| 574 | break; |
| 575 | } |
| 576 | case DexFile::kDexAnnotationNull: |
| 577 | fputs("null", out_file_); |
| 578 | break; |
| 579 | case DexFile::kDexAnnotationBoolean: |
| 580 | fputs(StrBool(data->GetBoolean()), out_file_); |
| 581 | break; |
| 582 | default: |
| 583 | fputs("????", out_file_); |
| 584 | break; |
| 585 | } // switch |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Dumps the file header. |
| 590 | */ |
| 591 | void DexLayout::DumpFileHeader() { |
| 592 | char sanitized[8 * 2 + 1]; |
| 593 | dex_ir::Collections& collections = header_->GetCollections(); |
| 594 | fprintf(out_file_, "DEX file header:\n"); |
| 595 | Asciify(sanitized, header_->Magic(), 8); |
| 596 | fprintf(out_file_, "magic : '%s'\n", sanitized); |
| 597 | fprintf(out_file_, "checksum : %08x\n", header_->Checksum()); |
| 598 | fprintf(out_file_, "signature : %02x%02x...%02x%02x\n", |
| 599 | header_->Signature()[0], header_->Signature()[1], |
| 600 | header_->Signature()[DexFile::kSha1DigestSize - 2], |
| 601 | header_->Signature()[DexFile::kSha1DigestSize - 1]); |
| 602 | fprintf(out_file_, "file_size : %d\n", header_->FileSize()); |
| 603 | fprintf(out_file_, "header_size : %d\n", header_->HeaderSize()); |
| 604 | fprintf(out_file_, "link_size : %d\n", header_->LinkSize()); |
| 605 | fprintf(out_file_, "link_off : %d (0x%06x)\n", |
| 606 | header_->LinkOffset(), header_->LinkOffset()); |
| 607 | fprintf(out_file_, "string_ids_size : %d\n", collections.StringIdsSize()); |
| 608 | fprintf(out_file_, "string_ids_off : %d (0x%06x)\n", |
| 609 | collections.StringIdsOffset(), collections.StringIdsOffset()); |
| 610 | fprintf(out_file_, "type_ids_size : %d\n", collections.TypeIdsSize()); |
| 611 | fprintf(out_file_, "type_ids_off : %d (0x%06x)\n", |
| 612 | collections.TypeIdsOffset(), collections.TypeIdsOffset()); |
| 613 | fprintf(out_file_, "proto_ids_size : %d\n", collections.ProtoIdsSize()); |
| 614 | fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n", |
| 615 | collections.ProtoIdsOffset(), collections.ProtoIdsOffset()); |
| 616 | fprintf(out_file_, "field_ids_size : %d\n", collections.FieldIdsSize()); |
| 617 | fprintf(out_file_, "field_ids_off : %d (0x%06x)\n", |
| 618 | collections.FieldIdsOffset(), collections.FieldIdsOffset()); |
| 619 | fprintf(out_file_, "method_ids_size : %d\n", collections.MethodIdsSize()); |
| 620 | fprintf(out_file_, "method_ids_off : %d (0x%06x)\n", |
| 621 | collections.MethodIdsOffset(), collections.MethodIdsOffset()); |
| 622 | fprintf(out_file_, "class_defs_size : %d\n", collections.ClassDefsSize()); |
| 623 | fprintf(out_file_, "class_defs_off : %d (0x%06x)\n", |
| 624 | collections.ClassDefsOffset(), collections.ClassDefsOffset()); |
| 625 | fprintf(out_file_, "data_size : %d\n", header_->DataSize()); |
| 626 | fprintf(out_file_, "data_off : %d (0x%06x)\n\n", |
| 627 | header_->DataOffset(), header_->DataOffset()); |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Dumps a class_def_item. |
| 632 | */ |
| 633 | void DexLayout::DumpClassDef(int idx) { |
| 634 | // General class information. |
| 635 | dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx); |
| 636 | fprintf(out_file_, "Class #%d header:\n", idx); |
| 637 | fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetIndex()); |
| 638 | fprintf(out_file_, "access_flags : %d (0x%04x)\n", |
| 639 | class_def->GetAccessFlags(), class_def->GetAccessFlags()); |
| 640 | uint32_t superclass_idx = class_def->Superclass() == nullptr ? |
| 641 | DexFile::kDexNoIndex16 : class_def->Superclass()->GetIndex(); |
| 642 | fprintf(out_file_, "superclass_idx : %d\n", superclass_idx); |
| 643 | fprintf(out_file_, "interfaces_off : %d (0x%06x)\n", |
| 644 | class_def->InterfacesOffset(), class_def->InterfacesOffset()); |
| 645 | uint32_t source_file_offset = 0xffffffffU; |
| 646 | if (class_def->SourceFile() != nullptr) { |
| 647 | source_file_offset = class_def->SourceFile()->GetIndex(); |
| 648 | } |
| 649 | fprintf(out_file_, "source_file_idx : %d\n", source_file_offset); |
| 650 | uint32_t annotations_offset = 0; |
| 651 | if (class_def->Annotations() != nullptr) { |
| 652 | annotations_offset = class_def->Annotations()->GetOffset(); |
| 653 | } |
| 654 | fprintf(out_file_, "annotations_off : %d (0x%06x)\n", |
| 655 | annotations_offset, annotations_offset); |
| 656 | if (class_def->GetClassData() == nullptr) { |
| 657 | fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0); |
| 658 | } else { |
| 659 | fprintf(out_file_, "class_data_off : %d (0x%06x)\n", |
| 660 | class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset()); |
| 661 | } |
| 662 | |
| 663 | // Fields and methods. |
| 664 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 665 | if (class_data != nullptr && class_data->StaticFields() != nullptr) { |
| 666 | fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size()); |
| 667 | } else { |
| 668 | fprintf(out_file_, "static_fields_size : 0\n"); |
| 669 | } |
| 670 | if (class_data != nullptr && class_data->InstanceFields() != nullptr) { |
| 671 | fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size()); |
| 672 | } else { |
| 673 | fprintf(out_file_, "instance_fields_size: 0\n"); |
| 674 | } |
| 675 | if (class_data != nullptr && class_data->DirectMethods() != nullptr) { |
| 676 | fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size()); |
| 677 | } else { |
| 678 | fprintf(out_file_, "direct_methods_size : 0\n"); |
| 679 | } |
| 680 | if (class_data != nullptr && class_data->VirtualMethods() != nullptr) { |
| 681 | fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size()); |
| 682 | } else { |
| 683 | fprintf(out_file_, "virtual_methods_size: 0\n"); |
| 684 | } |
| 685 | fprintf(out_file_, "\n"); |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Dumps an annotation set item. |
| 690 | */ |
| 691 | void DexLayout::DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) { |
| 692 | if (set_item == nullptr || set_item->GetItems()->size() == 0) { |
| 693 | fputs(" empty-annotation-set\n", out_file_); |
| 694 | return; |
| 695 | } |
| 696 | for (dex_ir::AnnotationItem* annotation : *set_item->GetItems()) { |
| 697 | if (annotation == nullptr) { |
| 698 | continue; |
| 699 | } |
| 700 | fputs(" ", out_file_); |
| 701 | switch (annotation->GetVisibility()) { |
| 702 | case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break; |
| 703 | case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break; |
| 704 | case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break; |
| 705 | default: fputs("VISIBILITY_UNKNOWN ", out_file_); break; |
| 706 | } // switch |
| 707 | DumpEncodedAnnotation(annotation->GetAnnotation()); |
| 708 | fputc('\n', out_file_); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Dumps class annotations. |
| 714 | */ |
| 715 | void DexLayout::DumpClassAnnotations(int idx) { |
| 716 | dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx); |
| 717 | dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations(); |
| 718 | if (annotations_directory == nullptr) { |
| 719 | return; // none |
| 720 | } |
| 721 | |
| 722 | fprintf(out_file_, "Class #%d annotations:\n", idx); |
| 723 | |
| 724 | dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation(); |
| 725 | dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations(); |
| 726 | dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations(); |
| 727 | dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations(); |
| 728 | |
| 729 | // Annotations on the class itself. |
| 730 | if (class_set_item != nullptr) { |
| 731 | fprintf(out_file_, "Annotations on class\n"); |
| 732 | DumpAnnotationSetItem(class_set_item); |
| 733 | } |
| 734 | |
| 735 | // Annotations on fields. |
| 736 | if (fields != nullptr) { |
| 737 | for (auto& field : *fields) { |
| 738 | const dex_ir::FieldId* field_id = field->GetFieldId(); |
| 739 | const uint32_t field_idx = field_id->GetIndex(); |
| 740 | const char* field_name = field_id->Name()->Data(); |
| 741 | fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name); |
| 742 | DumpAnnotationSetItem(field->GetAnnotationSetItem()); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // Annotations on methods. |
| 747 | if (methods != nullptr) { |
| 748 | for (auto& method : *methods) { |
| 749 | const dex_ir::MethodId* method_id = method->GetMethodId(); |
| 750 | const uint32_t method_idx = method_id->GetIndex(); |
| 751 | const char* method_name = method_id->Name()->Data(); |
| 752 | fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name); |
| 753 | DumpAnnotationSetItem(method->GetAnnotationSetItem()); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | // Annotations on method parameters. |
| 758 | if (parameters != nullptr) { |
| 759 | for (auto& parameter : *parameters) { |
| 760 | const dex_ir::MethodId* method_id = parameter->GetMethodId(); |
| 761 | const uint32_t method_idx = method_id->GetIndex(); |
| 762 | const char* method_name = method_id->Name()->Data(); |
| 763 | fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name); |
| 764 | uint32_t j = 0; |
| 765 | for (dex_ir::AnnotationSetItem* annotation : *parameter->GetAnnotations()->GetItems()) { |
| 766 | fprintf(out_file_, "#%u\n", j); |
| 767 | DumpAnnotationSetItem(annotation); |
| 768 | ++j; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | fputc('\n', out_file_); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Dumps an interface that a class declares to implement. |
| 778 | */ |
| 779 | void DexLayout::DumpInterface(const dex_ir::TypeId* type_item, int i) { |
| 780 | const char* interface_name = type_item->GetStringId()->Data(); |
| 781 | if (options_.output_format_ == kOutputPlain) { |
| 782 | fprintf(out_file_, " #%d : '%s'\n", i, interface_name); |
| 783 | } else { |
| 784 | std::string dot(DescriptorToDotWrapper(interface_name)); |
| 785 | fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str()); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Dumps the catches table associated with the code. |
| 791 | */ |
| 792 | void DexLayout::DumpCatches(const dex_ir::CodeItem* code) { |
| 793 | const uint16_t tries_size = code->TriesSize(); |
| 794 | |
| 795 | // No catch table. |
| 796 | if (tries_size == 0) { |
| 797 | fprintf(out_file_, " catches : (none)\n"); |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | // Dump all table entries. |
| 802 | fprintf(out_file_, " catches : %d\n", tries_size); |
| 803 | std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries(); |
| 804 | for (uint32_t i = 0; i < tries_size; i++) { |
| 805 | const dex_ir::TryItem* try_item = (*tries)[i].get(); |
| 806 | const uint32_t start = try_item->StartAddr(); |
| 807 | const uint32_t end = start + try_item->InsnCount(); |
| 808 | fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end); |
| 809 | for (auto& handler : *try_item->GetHandlers()->GetHandlers()) { |
| 810 | const dex_ir::TypeId* type_id = handler->GetTypeId(); |
| 811 | const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data(); |
| 812 | fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress()); |
| 813 | } // for |
| 814 | } // for |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Dumps all positions table entries associated with the code. |
| 819 | */ |
| 820 | void DexLayout::DumpPositionInfo(const dex_ir::CodeItem* code) { |
| 821 | dex_ir::DebugInfoItem* debug_info = code->DebugInfo(); |
| 822 | if (debug_info == nullptr) { |
| 823 | return; |
| 824 | } |
| 825 | std::vector<std::unique_ptr<dex_ir::PositionInfo>>& positions = debug_info->GetPositionInfo(); |
| 826 | for (size_t i = 0; i < positions.size(); ++i) { |
| 827 | fprintf(out_file_, " 0x%04x line=%d\n", positions[i]->address_, positions[i]->line_); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Dumps all locals table entries associated with the code. |
| 833 | */ |
| 834 | void DexLayout::DumpLocalInfo(const dex_ir::CodeItem* code) { |
| 835 | dex_ir::DebugInfoItem* debug_info = code->DebugInfo(); |
| 836 | if (debug_info == nullptr) { |
| 837 | return; |
| 838 | } |
| 839 | std::vector<std::unique_ptr<dex_ir::LocalInfo>>& locals = debug_info->GetLocalInfo(); |
| 840 | for (size_t i = 0; i < locals.size(); ++i) { |
| 841 | dex_ir::LocalInfo* entry = locals[i].get(); |
| 842 | fprintf(out_file_, " 0x%04x - 0x%04x reg=%d %s %s %s\n", |
| 843 | entry->start_address_, entry->end_address_, entry->reg_, |
| 844 | entry->name_.c_str(), entry->descriptor_.c_str(), entry->signature_.c_str()); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 849 | * Dumps a single instruction. |
| 850 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 851 | void DexLayout::DumpInstruction(const dex_ir::CodeItem* code, |
| 852 | uint32_t code_offset, |
| 853 | uint32_t insn_idx, |
| 854 | uint32_t insn_width, |
| 855 | const Instruction* dec_insn) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 856 | // Address of instruction (expressed as byte offset). |
| 857 | fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2); |
| 858 | |
| 859 | // Dump (part of) raw bytes. |
| 860 | const uint16_t* insns = code->Insns(); |
| 861 | for (uint32_t i = 0; i < 8; i++) { |
| 862 | if (i < insn_width) { |
| 863 | if (i == 7) { |
| 864 | fprintf(out_file_, " ... "); |
| 865 | } else { |
| 866 | // Print 16-bit value in little-endian order. |
| 867 | const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i]; |
| 868 | fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]); |
| 869 | } |
| 870 | } else { |
| 871 | fputs(" ", out_file_); |
| 872 | } |
| 873 | } // for |
| 874 | |
| 875 | // Dump pseudo-instruction or opcode. |
| 876 | if (dec_insn->Opcode() == Instruction::NOP) { |
| 877 | const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]); |
| 878 | if (instr == Instruction::kPackedSwitchSignature) { |
| 879 | fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width); |
| 880 | } else if (instr == Instruction::kSparseSwitchSignature) { |
| 881 | fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width); |
| 882 | } else if (instr == Instruction::kArrayDataSignature) { |
| 883 | fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width); |
| 884 | } else { |
| 885 | fprintf(out_file_, "|%04x: nop // spacer", insn_idx); |
| 886 | } |
| 887 | } else { |
| 888 | fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name()); |
| 889 | } |
| 890 | |
| 891 | // Set up additional argument. |
| 892 | std::unique_ptr<char[]> index_buf; |
| 893 | if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 894 | index_buf = IndexString(header_, dec_insn, 200); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | // Dump the instruction. |
| 898 | // |
| 899 | // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original. |
| 900 | // |
| 901 | switch (Instruction::FormatOf(dec_insn->Opcode())) { |
| 902 | case Instruction::k10x: // op |
| 903 | break; |
| 904 | case Instruction::k12x: // op vA, vB |
| 905 | fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB()); |
| 906 | break; |
| 907 | case Instruction::k11n: // op vA, #+B |
| 908 | fprintf(out_file_, " v%d, #int %d // #%x", |
| 909 | dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB()); |
| 910 | break; |
| 911 | case Instruction::k11x: // op vAA |
| 912 | fprintf(out_file_, " v%d", dec_insn->VRegA()); |
| 913 | break; |
| 914 | case Instruction::k10t: // op +AA |
| 915 | case Instruction::k20t: { // op +AAAA |
| 916 | const int32_t targ = (int32_t) dec_insn->VRegA(); |
| 917 | fprintf(out_file_, " %04x // %c%04x", |
| 918 | insn_idx + targ, |
| 919 | (targ < 0) ? '-' : '+', |
| 920 | (targ < 0) ? -targ : targ); |
| 921 | break; |
| 922 | } |
| 923 | case Instruction::k22x: // op vAA, vBBBB |
| 924 | fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB()); |
| 925 | break; |
| 926 | case Instruction::k21t: { // op vAA, +BBBB |
| 927 | const int32_t targ = (int32_t) dec_insn->VRegB(); |
| 928 | fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(), |
| 929 | insn_idx + targ, |
| 930 | (targ < 0) ? '-' : '+', |
| 931 | (targ < 0) ? -targ : targ); |
| 932 | break; |
| 933 | } |
| 934 | case Instruction::k21s: // op vAA, #+BBBB |
| 935 | fprintf(out_file_, " v%d, #int %d // #%x", |
| 936 | dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB()); |
| 937 | break; |
| 938 | case Instruction::k21h: // op vAA, #+BBBB0000[00000000] |
| 939 | // The printed format varies a bit based on the actual opcode. |
| 940 | if (dec_insn->Opcode() == Instruction::CONST_HIGH16) { |
| 941 | const int32_t value = dec_insn->VRegB() << 16; |
| 942 | fprintf(out_file_, " v%d, #int %d // #%x", |
| 943 | dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB()); |
| 944 | } else { |
| 945 | const int64_t value = ((int64_t) dec_insn->VRegB()) << 48; |
| 946 | fprintf(out_file_, " v%d, #long %" PRId64 " // #%x", |
| 947 | dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB()); |
| 948 | } |
| 949 | break; |
| 950 | case Instruction::k21c: // op vAA, thing@BBBB |
| 951 | case Instruction::k31c: // op vAA, thing@BBBBBBBB |
| 952 | fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get()); |
| 953 | break; |
| 954 | case Instruction::k23x: // op vAA, vBB, vCC |
| 955 | fprintf(out_file_, " v%d, v%d, v%d", |
| 956 | dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC()); |
| 957 | break; |
| 958 | case Instruction::k22b: // op vAA, vBB, #+CC |
| 959 | fprintf(out_file_, " v%d, v%d, #int %d // #%02x", |
| 960 | dec_insn->VRegA(), dec_insn->VRegB(), |
| 961 | (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC()); |
| 962 | break; |
| 963 | case Instruction::k22t: { // op vA, vB, +CCCC |
| 964 | const int32_t targ = (int32_t) dec_insn->VRegC(); |
| 965 | fprintf(out_file_, " v%d, v%d, %04x // %c%04x", |
| 966 | dec_insn->VRegA(), dec_insn->VRegB(), |
| 967 | insn_idx + targ, |
| 968 | (targ < 0) ? '-' : '+', |
| 969 | (targ < 0) ? -targ : targ); |
| 970 | break; |
| 971 | } |
| 972 | case Instruction::k22s: // op vA, vB, #+CCCC |
| 973 | fprintf(out_file_, " v%d, v%d, #int %d // #%04x", |
| 974 | dec_insn->VRegA(), dec_insn->VRegB(), |
| 975 | (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC()); |
| 976 | break; |
| 977 | case Instruction::k22c: // op vA, vB, thing@CCCC |
| 978 | // NOT SUPPORTED: |
| 979 | // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC |
| 980 | fprintf(out_file_, " v%d, v%d, %s", |
| 981 | dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get()); |
| 982 | break; |
| 983 | case Instruction::k30t: |
| 984 | fprintf(out_file_, " #%08x", dec_insn->VRegA()); |
| 985 | break; |
| 986 | case Instruction::k31i: { // op vAA, #+BBBBBBBB |
| 987 | // This is often, but not always, a float. |
| 988 | union { |
| 989 | float f; |
| 990 | uint32_t i; |
| 991 | } conv; |
| 992 | conv.i = dec_insn->VRegB(); |
| 993 | fprintf(out_file_, " v%d, #float %g // #%08x", |
| 994 | dec_insn->VRegA(), conv.f, dec_insn->VRegB()); |
| 995 | break; |
| 996 | } |
| 997 | case Instruction::k31t: // op vAA, offset +BBBBBBBB |
| 998 | fprintf(out_file_, " v%d, %08x // +%08x", |
| 999 | dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB()); |
| 1000 | break; |
| 1001 | case Instruction::k32x: // op vAAAA, vBBBB |
| 1002 | fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB()); |
| 1003 | break; |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 1004 | case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB |
| 1005 | case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, meth@BBBB, proto@HHHH |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1006 | // NOT SUPPORTED: |
| 1007 | // case Instruction::k35ms: // [opt] invoke-virtual+super |
| 1008 | // case Instruction::k35mi: // [opt] inline invoke |
| 1009 | uint32_t arg[Instruction::kMaxVarArgRegs]; |
| 1010 | dec_insn->GetVarArgs(arg); |
| 1011 | fputs(" {", out_file_); |
| 1012 | for (int i = 0, n = dec_insn->VRegA(); i < n; i++) { |
| 1013 | if (i == 0) { |
| 1014 | fprintf(out_file_, "v%d", arg[i]); |
| 1015 | } else { |
| 1016 | fprintf(out_file_, ", v%d", arg[i]); |
| 1017 | } |
| 1018 | } // for |
| 1019 | fprintf(out_file_, "}, %s", index_buf.get()); |
| 1020 | break; |
| 1021 | } |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 1022 | case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB |
| 1023 | case Instruction::k4rcc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB, proto@HHHH |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1024 | // NOT SUPPORTED: |
| 1025 | // case Instruction::k3rms: // [opt] invoke-virtual+super/range |
| 1026 | // case Instruction::k3rmi: // [opt] execute-inline/range |
| 1027 | { |
| 1028 | // This doesn't match the "dx" output when some of the args are |
| 1029 | // 64-bit values -- dx only shows the first register. |
| 1030 | fputs(" {", out_file_); |
| 1031 | for (int i = 0, n = dec_insn->VRegA(); i < n; i++) { |
| 1032 | if (i == 0) { |
| 1033 | fprintf(out_file_, "v%d", dec_insn->VRegC() + i); |
| 1034 | } else { |
| 1035 | fprintf(out_file_, ", v%d", dec_insn->VRegC() + i); |
| 1036 | } |
| 1037 | } // for |
| 1038 | fprintf(out_file_, "}, %s", index_buf.get()); |
| 1039 | } |
| 1040 | break; |
| 1041 | case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB |
| 1042 | // This is often, but not always, a double. |
| 1043 | union { |
| 1044 | double d; |
| 1045 | uint64_t j; |
| 1046 | } conv; |
| 1047 | conv.j = dec_insn->WideVRegB(); |
| 1048 | fprintf(out_file_, " v%d, #double %g // #%016" PRIx64, |
| 1049 | dec_insn->VRegA(), conv.d, dec_insn->WideVRegB()); |
| 1050 | break; |
| 1051 | } |
| 1052 | // NOT SUPPORTED: |
| 1053 | // case Instruction::k00x: // unknown op or breakpoint |
| 1054 | // break; |
| 1055 | default: |
| 1056 | fprintf(out_file_, " ???"); |
| 1057 | break; |
| 1058 | } // switch |
| 1059 | |
| 1060 | fputc('\n', out_file_); |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Dumps a bytecode disassembly. |
| 1065 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1066 | void DexLayout::DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) { |
| 1067 | dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1068 | const char* name = method_id->Name()->Data(); |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 1069 | std::string type_descriptor = GetSignatureForProtoId(method_id->Proto()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1070 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 1071 | |
| 1072 | // Generate header. |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1073 | std::string dot(DescriptorToDotWrapper(back_descriptor)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1074 | fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n", |
David Sehr | 7235922 | 2016-09-07 13:04:01 -0700 | [diff] [blame] | 1075 | code_offset, code_offset, dot.c_str(), name, type_descriptor.c_str()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1076 | |
| 1077 | // Iterate over all instructions. |
| 1078 | const uint16_t* insns = code->Insns(); |
| 1079 | for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) { |
| 1080 | const Instruction* instruction = Instruction::At(&insns[insn_idx]); |
| 1081 | const uint32_t insn_width = instruction->SizeInCodeUnits(); |
| 1082 | if (insn_width == 0) { |
| 1083 | fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insn_idx); |
| 1084 | break; |
| 1085 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1086 | DumpInstruction(code, code_offset, insn_idx, insn_width, instruction); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1087 | insn_idx += insn_width; |
| 1088 | } // for |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Dumps code of a method. |
| 1093 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1094 | void DexLayout::DumpCode(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1095 | fprintf(out_file_, " registers : %d\n", code->RegistersSize()); |
| 1096 | fprintf(out_file_, " ins : %d\n", code->InsSize()); |
| 1097 | fprintf(out_file_, " outs : %d\n", code->OutsSize()); |
| 1098 | fprintf(out_file_, " insns size : %d 16-bit code units\n", |
| 1099 | code->InsnsSize()); |
| 1100 | |
| 1101 | // Bytecode disassembly, if requested. |
| 1102 | if (options_.disassemble_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1103 | DumpBytecodes(idx, code, code_offset); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | // Try-catch blocks. |
| 1107 | DumpCatches(code); |
| 1108 | |
| 1109 | // Positions and locals table in the debug info. |
| 1110 | fprintf(out_file_, " positions : \n"); |
| 1111 | DumpPositionInfo(code); |
| 1112 | fprintf(out_file_, " locals : \n"); |
| 1113 | DumpLocalInfo(code); |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * Dumps a method. |
| 1118 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1119 | void DexLayout::DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1120 | // Bail for anything private if export only requested. |
| 1121 | if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) { |
| 1122 | return; |
| 1123 | } |
| 1124 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1125 | dex_ir::MethodId* method_id = header_->GetCollections().GetMethodId(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1126 | const char* name = method_id->Name()->Data(); |
| 1127 | char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str()); |
| 1128 | const char* back_descriptor = method_id->Class()->GetStringId()->Data(); |
| 1129 | char* access_str = CreateAccessFlagStr(flags, kAccessForMethod); |
| 1130 | |
| 1131 | if (options_.output_format_ == kOutputPlain) { |
| 1132 | fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor); |
| 1133 | fprintf(out_file_, " name : '%s'\n", name); |
| 1134 | fprintf(out_file_, " type : '%s'\n", type_descriptor); |
| 1135 | fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str); |
| 1136 | if (code == nullptr) { |
| 1137 | fprintf(out_file_, " code : (none)\n"); |
| 1138 | } else { |
| 1139 | fprintf(out_file_, " code -\n"); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1140 | DumpCode(idx, code, code->GetOffset()); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1141 | } |
| 1142 | if (options_.disassemble_) { |
| 1143 | fputc('\n', out_file_); |
| 1144 | } |
| 1145 | } else if (options_.output_format_ == kOutputXml) { |
| 1146 | const bool constructor = (name[0] == '<'); |
| 1147 | |
| 1148 | // Method name and prototype. |
| 1149 | if (constructor) { |
| 1150 | std::string dot(DescriptorClassToDot(back_descriptor)); |
| 1151 | fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str()); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1152 | dot = DescriptorToDotWrapper(back_descriptor); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1153 | fprintf(out_file_, " type=\"%s\"\n", dot.c_str()); |
| 1154 | } else { |
| 1155 | fprintf(out_file_, "<method name=\"%s\"\n", name); |
| 1156 | const char* return_type = strrchr(type_descriptor, ')'); |
| 1157 | if (return_type == nullptr) { |
| 1158 | fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor); |
| 1159 | goto bail; |
| 1160 | } |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1161 | std::string dot(DescriptorToDotWrapper(return_type + 1)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1162 | fprintf(out_file_, " return=\"%s\"\n", dot.c_str()); |
| 1163 | fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0)); |
| 1164 | fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0)); |
| 1165 | fprintf(out_file_, " synchronized=%s\n", QuotedBool( |
| 1166 | (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0)); |
| 1167 | } |
| 1168 | |
| 1169 | // Additional method flags. |
| 1170 | fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0)); |
| 1171 | fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0)); |
| 1172 | // The "deprecated=" not knowable w/o parsing annotations. |
| 1173 | fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags)); |
| 1174 | |
| 1175 | // Parameters. |
| 1176 | if (type_descriptor[0] != '(') { |
| 1177 | fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor); |
| 1178 | goto bail; |
| 1179 | } |
| 1180 | char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1)); |
| 1181 | const char* base = type_descriptor + 1; |
| 1182 | int arg_num = 0; |
| 1183 | while (*base != ')') { |
| 1184 | char* cp = tmp_buf; |
| 1185 | while (*base == '[') { |
| 1186 | *cp++ = *base++; |
| 1187 | } |
| 1188 | if (*base == 'L') { |
| 1189 | // Copy through ';'. |
| 1190 | do { |
| 1191 | *cp = *base++; |
| 1192 | } while (*cp++ != ';'); |
| 1193 | } else { |
| 1194 | // Primitive char, copy it. |
| 1195 | if (strchr("ZBCSIFJD", *base) == nullptr) { |
| 1196 | fprintf(stderr, "ERROR: bad method signature '%s'\n", base); |
| 1197 | break; // while |
| 1198 | } |
| 1199 | *cp++ = *base++; |
| 1200 | } |
| 1201 | // Null terminate and display. |
| 1202 | *cp++ = '\0'; |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1203 | std::string dot(DescriptorToDotWrapper(tmp_buf)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1204 | fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n" |
| 1205 | "</parameter>\n", arg_num++, dot.c_str()); |
| 1206 | } // while |
| 1207 | free(tmp_buf); |
| 1208 | if (constructor) { |
| 1209 | fprintf(out_file_, "</constructor>\n"); |
| 1210 | } else { |
| 1211 | fprintf(out_file_, "</method>\n"); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | bail: |
| 1216 | free(type_descriptor); |
| 1217 | free(access_str); |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * Dumps a static (class) field. |
| 1222 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1223 | void DexLayout::DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1224 | // Bail for anything private if export only requested. |
| 1225 | if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) { |
| 1226 | return; |
| 1227 | } |
| 1228 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1229 | dex_ir::FieldId* field_id = header_->GetCollections().GetFieldId(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1230 | const char* name = field_id->Name()->Data(); |
| 1231 | const char* type_descriptor = field_id->Type()->GetStringId()->Data(); |
| 1232 | const char* back_descriptor = field_id->Class()->GetStringId()->Data(); |
| 1233 | char* access_str = CreateAccessFlagStr(flags, kAccessForField); |
| 1234 | |
| 1235 | if (options_.output_format_ == kOutputPlain) { |
| 1236 | fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor); |
| 1237 | fprintf(out_file_, " name : '%s'\n", name); |
| 1238 | fprintf(out_file_, " type : '%s'\n", type_descriptor); |
| 1239 | fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str); |
| 1240 | if (init != nullptr) { |
| 1241 | fputs(" value : ", out_file_); |
| 1242 | DumpEncodedValue(init); |
| 1243 | fputs("\n", out_file_); |
| 1244 | } |
| 1245 | } else if (options_.output_format_ == kOutputXml) { |
| 1246 | fprintf(out_file_, "<field name=\"%s\"\n", name); |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1247 | std::string dot(DescriptorToDotWrapper(type_descriptor)); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1248 | fprintf(out_file_, " type=\"%s\"\n", dot.c_str()); |
| 1249 | fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0)); |
| 1250 | fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0)); |
| 1251 | // The "value=" is not knowable w/o parsing annotations. |
| 1252 | fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0)); |
| 1253 | fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0)); |
| 1254 | // The "deprecated=" is not knowable w/o parsing annotations. |
| 1255 | fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags)); |
| 1256 | if (init != nullptr) { |
| 1257 | fputs(" value=\"", out_file_); |
| 1258 | DumpEncodedValue(init); |
| 1259 | fputs("\"\n", out_file_); |
| 1260 | } |
| 1261 | fputs(">\n</field>\n", out_file_); |
| 1262 | } |
| 1263 | |
| 1264 | free(access_str); |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Dumps an instance field. |
| 1269 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1270 | void DexLayout::DumpIField(uint32_t idx, uint32_t flags, int i) { |
| 1271 | DumpSField(idx, flags, i, nullptr); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1275 | * Dumps the class. |
| 1276 | * |
| 1277 | * Note "idx" is a DexClassDef index, not a DexTypeId index. |
| 1278 | * |
| 1279 | * If "*last_package" is nullptr or does not match the current class' package, |
| 1280 | * the value will be replaced with a newly-allocated string. |
| 1281 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1282 | void DexLayout::DumpClass(int idx, char** last_package) { |
| 1283 | dex_ir::ClassDef* class_def = header_->GetCollections().GetClassDef(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1284 | // Omitting non-public class. |
| 1285 | if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) { |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | if (options_.show_section_headers_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1290 | DumpClassDef(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | if (options_.show_annotations_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1294 | DumpClassAnnotations(idx); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1297 | // For the XML output, show the package name. Ideally we'd gather |
| 1298 | // up the classes, sort them, and dump them alphabetically so the |
| 1299 | // package name wouldn't jump around, but that's not a great plan |
| 1300 | // for something that needs to run on the device. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1301 | const char* class_descriptor = |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1302 | header_->GetCollections().GetClassDef(idx)->ClassType()->GetStringId()->Data(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1303 | if (!(class_descriptor[0] == 'L' && |
| 1304 | class_descriptor[strlen(class_descriptor)-1] == ';')) { |
| 1305 | // Arrays and primitives should not be defined explicitly. Keep going? |
| 1306 | fprintf(stderr, "Malformed class name '%s'\n", class_descriptor); |
| 1307 | } else if (options_.output_format_ == kOutputXml) { |
| 1308 | char* mangle = strdup(class_descriptor + 1); |
| 1309 | mangle[strlen(mangle)-1] = '\0'; |
| 1310 | |
| 1311 | // Reduce to just the package name. |
| 1312 | char* last_slash = strrchr(mangle, '/'); |
| 1313 | if (last_slash != nullptr) { |
| 1314 | *last_slash = '\0'; |
| 1315 | } else { |
| 1316 | *mangle = '\0'; |
| 1317 | } |
| 1318 | |
| 1319 | for (char* cp = mangle; *cp != '\0'; cp++) { |
| 1320 | if (*cp == '/') { |
| 1321 | *cp = '.'; |
| 1322 | } |
| 1323 | } // for |
| 1324 | |
| 1325 | if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) { |
| 1326 | // Start of a new package. |
| 1327 | if (*last_package != nullptr) { |
| 1328 | fprintf(out_file_, "</package>\n"); |
| 1329 | } |
| 1330 | fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle); |
| 1331 | free(*last_package); |
| 1332 | *last_package = mangle; |
| 1333 | } else { |
| 1334 | free(mangle); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | // General class information. |
| 1339 | char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass); |
| 1340 | const char* superclass_descriptor = nullptr; |
| 1341 | if (class_def->Superclass() != nullptr) { |
| 1342 | superclass_descriptor = class_def->Superclass()->GetStringId()->Data(); |
| 1343 | } |
| 1344 | if (options_.output_format_ == kOutputPlain) { |
| 1345 | fprintf(out_file_, "Class #%d -\n", idx); |
| 1346 | fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor); |
| 1347 | fprintf(out_file_, " Access flags : 0x%04x (%s)\n", |
| 1348 | class_def->GetAccessFlags(), access_str); |
| 1349 | if (superclass_descriptor != nullptr) { |
| 1350 | fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor); |
| 1351 | } |
| 1352 | fprintf(out_file_, " Interfaces -\n"); |
| 1353 | } else { |
| 1354 | std::string dot(DescriptorClassToDot(class_descriptor)); |
| 1355 | fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str()); |
| 1356 | if (superclass_descriptor != nullptr) { |
Jeff Hao | c3acfc5 | 2016-08-29 14:18:26 -0700 | [diff] [blame] | 1357 | dot = DescriptorToDotWrapper(superclass_descriptor); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1358 | fprintf(out_file_, " extends=\"%s\"\n", dot.c_str()); |
| 1359 | } |
| 1360 | fprintf(out_file_, " interface=%s\n", |
| 1361 | QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0)); |
| 1362 | fprintf(out_file_, " abstract=%s\n", |
| 1363 | QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0)); |
| 1364 | fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0)); |
| 1365 | fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0)); |
| 1366 | // The "deprecated=" not knowable w/o parsing annotations. |
| 1367 | fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags())); |
| 1368 | fprintf(out_file_, ">\n"); |
| 1369 | } |
| 1370 | |
| 1371 | // Interfaces. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1372 | const dex_ir::TypeIdVector* interfaces = class_def->Interfaces(); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1373 | if (interfaces != nullptr) { |
| 1374 | for (uint32_t i = 0; i < interfaces->size(); i++) { |
| 1375 | DumpInterface((*interfaces)[i], i); |
| 1376 | } // for |
| 1377 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1378 | |
| 1379 | // Fields and methods. |
| 1380 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 1381 | // Prepare data for static fields. |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1382 | dex_ir::EncodedArrayItem* static_values = class_def->StaticValues(); |
| 1383 | dex_ir::EncodedValueVector* encoded_values = |
| 1384 | static_values == nullptr ? nullptr : static_values->GetEncodedValues(); |
| 1385 | const uint32_t encoded_values_size = (encoded_values == nullptr) ? 0 : encoded_values->size(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1386 | |
| 1387 | // Static fields. |
| 1388 | if (options_.output_format_ == kOutputPlain) { |
| 1389 | fprintf(out_file_, " Static fields -\n"); |
| 1390 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1391 | if (class_data != nullptr) { |
| 1392 | dex_ir::FieldItemVector* static_fields = class_data->StaticFields(); |
| 1393 | if (static_fields != nullptr) { |
| 1394 | for (uint32_t i = 0; i < static_fields->size(); i++) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1395 | DumpSField((*static_fields)[i]->GetFieldId()->GetIndex(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1396 | (*static_fields)[i]->GetAccessFlags(), |
| 1397 | i, |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1398 | i < encoded_values_size ? (*encoded_values)[i].get() : nullptr); |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1399 | } // for |
| 1400 | } |
| 1401 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1402 | |
| 1403 | // Instance fields. |
| 1404 | if (options_.output_format_ == kOutputPlain) { |
| 1405 | fprintf(out_file_, " Instance fields -\n"); |
| 1406 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1407 | if (class_data != nullptr) { |
| 1408 | dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields(); |
| 1409 | if (instance_fields != nullptr) { |
| 1410 | for (uint32_t i = 0; i < instance_fields->size(); i++) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1411 | DumpIField((*instance_fields)[i]->GetFieldId()->GetIndex(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1412 | (*instance_fields)[i]->GetAccessFlags(), |
| 1413 | i); |
| 1414 | } // for |
| 1415 | } |
| 1416 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1417 | |
| 1418 | // Direct methods. |
| 1419 | if (options_.output_format_ == kOutputPlain) { |
| 1420 | fprintf(out_file_, " Direct methods -\n"); |
| 1421 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1422 | if (class_data != nullptr) { |
| 1423 | dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods(); |
| 1424 | if (direct_methods != nullptr) { |
| 1425 | for (uint32_t i = 0; i < direct_methods->size(); i++) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1426 | DumpMethod((*direct_methods)[i]->GetMethodId()->GetIndex(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1427 | (*direct_methods)[i]->GetAccessFlags(), |
| 1428 | (*direct_methods)[i]->GetCodeItem(), |
| 1429 | i); |
| 1430 | } // for |
| 1431 | } |
| 1432 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1433 | |
| 1434 | // Virtual methods. |
| 1435 | if (options_.output_format_ == kOutputPlain) { |
| 1436 | fprintf(out_file_, " Virtual methods -\n"); |
| 1437 | } |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1438 | if (class_data != nullptr) { |
| 1439 | dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods(); |
| 1440 | if (virtual_methods != nullptr) { |
| 1441 | for (uint32_t i = 0; i < virtual_methods->size(); i++) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1442 | DumpMethod((*virtual_methods)[i]->GetMethodId()->GetIndex(), |
David Sehr | 853a8e1 | 2016-09-01 13:03:50 -0700 | [diff] [blame] | 1443 | (*virtual_methods)[i]->GetAccessFlags(), |
| 1444 | (*virtual_methods)[i]->GetCodeItem(), |
| 1445 | i); |
| 1446 | } // for |
| 1447 | } |
| 1448 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1449 | |
| 1450 | // End of class. |
| 1451 | if (options_.output_format_ == kOutputPlain) { |
| 1452 | const char* file_name = "unknown"; |
| 1453 | if (class_def->SourceFile() != nullptr) { |
| 1454 | file_name = class_def->SourceFile()->Data(); |
| 1455 | } |
| 1456 | const dex_ir::StringId* source_file = class_def->SourceFile(); |
| 1457 | fprintf(out_file_, " source_file_idx : %d (%s)\n\n", |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1458 | source_file == nullptr ? 0xffffffffU : source_file->GetIndex(), file_name); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1459 | } else if (options_.output_format_ == kOutputXml) { |
| 1460 | fprintf(out_file_, "</class>\n"); |
| 1461 | } |
| 1462 | |
| 1463 | free(access_str); |
| 1464 | } |
| 1465 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1466 | void DexLayout::DumpDexFile() { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1467 | // Headers. |
| 1468 | if (options_.show_file_headers_) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1469 | DumpFileHeader(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | // Open XML context. |
| 1473 | if (options_.output_format_ == kOutputXml) { |
| 1474 | fprintf(out_file_, "<api>\n"); |
| 1475 | } |
| 1476 | |
| 1477 | // Iterate over all classes. |
| 1478 | char* package = nullptr; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1479 | const uint32_t class_defs_size = header_->GetCollections().ClassDefsSize(); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1480 | for (uint32_t i = 0; i < class_defs_size; i++) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1481 | DumpClass(i, &package); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1482 | } // for |
| 1483 | |
| 1484 | // Free the last package allocated. |
| 1485 | if (package != nullptr) { |
| 1486 | fprintf(out_file_, "</package>\n"); |
| 1487 | free(package); |
| 1488 | } |
| 1489 | |
| 1490 | // Close XML context. |
| 1491 | if (options_.output_format_ == kOutputXml) { |
| 1492 | fprintf(out_file_, "</api>\n"); |
| 1493 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1494 | } |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1495 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1496 | std::vector<dex_ir::ClassData*> DexLayout::LayoutClassDefsAndClassData(const DexFile* dex_file) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1497 | std::vector<dex_ir::ClassDef*> new_class_def_order; |
| 1498 | for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) { |
| 1499 | dex::TypeIndex type_idx(class_def->ClassType()->GetIndex()); |
| 1500 | if (info_->ContainsClass(*dex_file, type_idx)) { |
| 1501 | new_class_def_order.push_back(class_def.get()); |
| 1502 | } |
| 1503 | } |
| 1504 | for (std::unique_ptr<dex_ir::ClassDef>& class_def : header_->GetCollections().ClassDefs()) { |
| 1505 | dex::TypeIndex type_idx(class_def->ClassType()->GetIndex()); |
| 1506 | if (!info_->ContainsClass(*dex_file, type_idx)) { |
| 1507 | new_class_def_order.push_back(class_def.get()); |
| 1508 | } |
| 1509 | } |
| 1510 | uint32_t class_defs_offset = header_->GetCollections().ClassDefsOffset(); |
| 1511 | uint32_t class_data_offset = header_->GetCollections().ClassDatasOffset(); |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1512 | std::unordered_set<dex_ir::ClassData*> visited_class_data; |
| 1513 | std::vector<dex_ir::ClassData*> new_class_data_order; |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1514 | for (uint32_t i = 0; i < new_class_def_order.size(); ++i) { |
| 1515 | dex_ir::ClassDef* class_def = new_class_def_order[i]; |
| 1516 | class_def->SetIndex(i); |
| 1517 | class_def->SetOffset(class_defs_offset); |
| 1518 | class_defs_offset += dex_ir::ClassDef::ItemSize(); |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1519 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 1520 | if (class_data != nullptr && visited_class_data.find(class_data) == visited_class_data.end()) { |
| 1521 | class_data->SetOffset(class_data_offset); |
| 1522 | class_data_offset += class_data->GetSize(); |
| 1523 | visited_class_data.insert(class_data); |
| 1524 | new_class_data_order.push_back(class_data); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1525 | } |
| 1526 | } |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1527 | return new_class_data_order; |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1530 | // Orders code items according to specified class data ordering. |
| 1531 | // NOTE: If the section following the code items is byte aligned, the last code item is left in |
| 1532 | // place to preserve alignment. Layout needs an overhaul to handle movement of other sections. |
| 1533 | int32_t DexLayout::LayoutCodeItems(std::vector<dex_ir::ClassData*> new_class_data_order) { |
Jeff Hao | 863f1d7 | 2017-03-01 12:18:19 -0800 | [diff] [blame] | 1534 | // Do not move code items if class data section precedes code item section. |
| 1535 | // ULEB encoding is variable length, causing problems determining the offset of the code items. |
| 1536 | // TODO: We should swap the order of these sections in the future to avoid this issue. |
| 1537 | uint32_t class_data_offset = header_->GetCollections().ClassDatasOffset(); |
| 1538 | uint32_t code_item_offset = header_->GetCollections().CodeItemsOffset(); |
| 1539 | if (class_data_offset < code_item_offset) { |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1543 | // Find the last code item so we can leave it in place if the next section is not 4 byte aligned. |
| 1544 | std::unordered_set<dex_ir::CodeItem*> visited_code_items; |
Jeff Hao | 863f1d7 | 2017-03-01 12:18:19 -0800 | [diff] [blame] | 1545 | bool is_code_item_aligned = IsNextSectionCodeItemAligned(code_item_offset); |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1546 | if (!is_code_item_aligned) { |
| 1547 | dex_ir::CodeItem* last_code_item = nullptr; |
| 1548 | for (auto& code_item_pair : header_->GetCollections().CodeItems()) { |
| 1549 | std::unique_ptr<dex_ir::CodeItem>& code_item = code_item_pair.second; |
| 1550 | if (last_code_item == nullptr || last_code_item->GetOffset() < code_item->GetOffset()) { |
| 1551 | last_code_item = code_item.get(); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1552 | } |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1553 | } |
| 1554 | // Preserve the last code item by marking it already visited. |
| 1555 | visited_code_items.insert(last_code_item); |
| 1556 | } |
| 1557 | |
| 1558 | int32_t diff = 0; |
| 1559 | for (dex_ir::ClassData* class_data : new_class_data_order) { |
| 1560 | class_data->SetOffset(class_data->GetOffset() + diff); |
| 1561 | for (auto& method : *class_data->DirectMethods()) { |
| 1562 | dex_ir::CodeItem* code_item = method->GetCodeItem(); |
| 1563 | if (code_item != nullptr && visited_code_items.find(code_item) == visited_code_items.end()) { |
| 1564 | visited_code_items.insert(code_item); |
Jeff Hao | 863f1d7 | 2017-03-01 12:18:19 -0800 | [diff] [blame] | 1565 | diff += UnsignedLeb128Size(code_item_offset) - UnsignedLeb128Size(code_item->GetOffset()); |
| 1566 | code_item->SetOffset(code_item_offset); |
| 1567 | code_item_offset += RoundUp(code_item->GetSize(), kDexCodeItemAlignment); |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | for (auto& method : *class_data->VirtualMethods()) { |
| 1571 | dex_ir::CodeItem* code_item = method->GetCodeItem(); |
| 1572 | if (code_item != nullptr && visited_code_items.find(code_item) == visited_code_items.end()) { |
| 1573 | visited_code_items.insert(code_item); |
Jeff Hao | 863f1d7 | 2017-03-01 12:18:19 -0800 | [diff] [blame] | 1574 | diff += UnsignedLeb128Size(code_item_offset) - UnsignedLeb128Size(code_item->GetOffset()); |
| 1575 | code_item->SetOffset(code_item_offset); |
| 1576 | code_item_offset += RoundUp(code_item->GetSize(), kDexCodeItemAlignment); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | } |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1580 | // Adjust diff to be 4-byte aligned. |
| 1581 | return RoundUp(diff, kDexCodeItemAlignment); |
| 1582 | } |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1583 | |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1584 | bool DexLayout::IsNextSectionCodeItemAligned(uint32_t offset) { |
| 1585 | dex_ir::Collections& collections = header_->GetCollections(); |
| 1586 | std::set<uint32_t> section_offsets; |
| 1587 | section_offsets.insert(collections.MapListOffset()); |
| 1588 | section_offsets.insert(collections.TypeListsOffset()); |
| 1589 | section_offsets.insert(collections.AnnotationSetRefListsOffset()); |
| 1590 | section_offsets.insert(collections.AnnotationSetItemsOffset()); |
| 1591 | section_offsets.insert(collections.ClassDatasOffset()); |
| 1592 | section_offsets.insert(collections.CodeItemsOffset()); |
| 1593 | section_offsets.insert(collections.StringDatasOffset()); |
| 1594 | section_offsets.insert(collections.DebugInfoItemsOffset()); |
| 1595 | section_offsets.insert(collections.AnnotationItemsOffset()); |
| 1596 | section_offsets.insert(collections.EncodedArrayItemsOffset()); |
| 1597 | section_offsets.insert(collections.AnnotationsDirectoryItemsOffset()); |
| 1598 | |
| 1599 | auto found = section_offsets.find(offset); |
| 1600 | if (found != section_offsets.end()) { |
| 1601 | found++; |
| 1602 | if (found != section_offsets.end()) { |
| 1603 | return *found % kDexCodeItemAlignment == 0; |
| 1604 | } |
| 1605 | } |
| 1606 | return false; |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | // Adjust offsets of every item in the specified section by diff bytes. |
| 1610 | template<class T> void DexLayout::FixupSection(std::map<uint32_t, std::unique_ptr<T>>& map, |
| 1611 | uint32_t diff) { |
| 1612 | for (auto& pair : map) { |
| 1613 | std::unique_ptr<T>& item = pair.second; |
| 1614 | item->SetOffset(item->GetOffset() + diff); |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | // Adjust offsets of all sections with an address after the specified offset by diff bytes. |
| 1619 | void DexLayout::FixupSections(uint32_t offset, uint32_t diff) { |
| 1620 | dex_ir::Collections& collections = header_->GetCollections(); |
| 1621 | uint32_t map_list_offset = collections.MapListOffset(); |
| 1622 | if (map_list_offset > offset) { |
| 1623 | collections.SetMapListOffset(map_list_offset + diff); |
| 1624 | } |
| 1625 | |
| 1626 | uint32_t type_lists_offset = collections.TypeListsOffset(); |
| 1627 | if (type_lists_offset > offset) { |
| 1628 | collections.SetTypeListsOffset(type_lists_offset + diff); |
| 1629 | FixupSection(collections.TypeLists(), diff); |
| 1630 | } |
| 1631 | |
| 1632 | uint32_t annotation_set_ref_lists_offset = collections.AnnotationSetRefListsOffset(); |
| 1633 | if (annotation_set_ref_lists_offset > offset) { |
| 1634 | collections.SetAnnotationSetRefListsOffset(annotation_set_ref_lists_offset + diff); |
| 1635 | FixupSection(collections.AnnotationSetRefLists(), diff); |
| 1636 | } |
| 1637 | |
| 1638 | uint32_t annotation_set_items_offset = collections.AnnotationSetItemsOffset(); |
| 1639 | if (annotation_set_items_offset > offset) { |
| 1640 | collections.SetAnnotationSetItemsOffset(annotation_set_items_offset + diff); |
| 1641 | FixupSection(collections.AnnotationSetItems(), diff); |
| 1642 | } |
| 1643 | |
| 1644 | uint32_t class_datas_offset = collections.ClassDatasOffset(); |
| 1645 | if (class_datas_offset > offset) { |
| 1646 | collections.SetClassDatasOffset(class_datas_offset + diff); |
| 1647 | FixupSection(collections.ClassDatas(), diff); |
| 1648 | } |
| 1649 | |
| 1650 | uint32_t code_items_offset = collections.CodeItemsOffset(); |
| 1651 | if (code_items_offset > offset) { |
| 1652 | collections.SetCodeItemsOffset(code_items_offset + diff); |
| 1653 | FixupSection(collections.CodeItems(), diff); |
| 1654 | } |
| 1655 | |
| 1656 | uint32_t string_datas_offset = collections.StringDatasOffset(); |
| 1657 | if (string_datas_offset > offset) { |
| 1658 | collections.SetStringDatasOffset(string_datas_offset + diff); |
| 1659 | FixupSection(collections.StringDatas(), diff); |
| 1660 | } |
| 1661 | |
| 1662 | uint32_t debug_info_items_offset = collections.DebugInfoItemsOffset(); |
| 1663 | if (debug_info_items_offset > offset) { |
| 1664 | collections.SetDebugInfoItemsOffset(debug_info_items_offset + diff); |
| 1665 | FixupSection(collections.DebugInfoItems(), diff); |
| 1666 | } |
| 1667 | |
| 1668 | uint32_t annotation_items_offset = collections.AnnotationItemsOffset(); |
| 1669 | if (annotation_items_offset > offset) { |
| 1670 | collections.SetAnnotationItemsOffset(annotation_items_offset + diff); |
| 1671 | FixupSection(collections.AnnotationItems(), diff); |
| 1672 | } |
| 1673 | |
| 1674 | uint32_t encoded_array_items_offset = collections.EncodedArrayItemsOffset(); |
| 1675 | if (encoded_array_items_offset > offset) { |
| 1676 | collections.SetEncodedArrayItemsOffset(encoded_array_items_offset + diff); |
| 1677 | FixupSection(collections.EncodedArrayItems(), diff); |
| 1678 | } |
| 1679 | |
| 1680 | uint32_t annotations_directory_items_offset = collections.AnnotationsDirectoryItemsOffset(); |
| 1681 | if (annotations_directory_items_offset > offset) { |
| 1682 | collections.SetAnnotationsDirectoryItemsOffset(annotations_directory_items_offset + diff); |
| 1683 | FixupSection(collections.AnnotationsDirectoryItems(), diff); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | void DexLayout::LayoutOutputFile(const DexFile* dex_file) { |
Jeff Hao | e17f589 | 2017-02-23 16:14:04 -0800 | [diff] [blame] | 1688 | std::vector<dex_ir::ClassData*> new_class_data_order = LayoutClassDefsAndClassData(dex_file); |
| 1689 | int32_t diff = LayoutCodeItems(new_class_data_order); |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1690 | // Move sections after ClassData by diff bytes. |
| 1691 | FixupSections(header_->GetCollections().ClassDatasOffset(), diff); |
| 1692 | // Update file size. |
| 1693 | header_->SetFileSize(header_->FileSize() + diff); |
| 1694 | } |
| 1695 | |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame^] | 1696 | void DexLayout::OutputDexFile(const DexFile* dex_file) { |
| 1697 | const std::string& dex_file_location = dex_file->GetLocation(); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1698 | std::string error_msg; |
| 1699 | std::unique_ptr<File> new_file; |
| 1700 | if (!options_.output_to_memmap_) { |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 1701 | std::string output_location(options_.output_dex_directory_); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1702 | size_t last_slash = dex_file_location.rfind("/"); |
| 1703 | std::string dex_file_directory = dex_file_location.substr(0, last_slash + 1); |
| 1704 | if (output_location == dex_file_directory) { |
| 1705 | output_location = dex_file_location + ".new"; |
| 1706 | } else if (last_slash != std::string::npos) { |
| 1707 | output_location += dex_file_location.substr(last_slash); |
| 1708 | } else { |
| 1709 | output_location += "/" + dex_file_location + ".new"; |
| 1710 | } |
| 1711 | new_file.reset(OS::CreateEmptyFile(output_location.c_str())); |
| 1712 | ftruncate(new_file->Fd(), header_->FileSize()); |
| 1713 | mem_map_.reset(MemMap::MapFile(header_->FileSize(), PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1714 | new_file->Fd(), 0, /*low_4gb*/ false, output_location.c_str(), &error_msg)); |
| 1715 | } else { |
| 1716 | mem_map_.reset(MemMap::MapAnonymous("layout dex", nullptr, header_->FileSize(), |
| 1717 | PROT_READ | PROT_WRITE, /* low_4gb */ false, /* reuse */ false, &error_msg)); |
| 1718 | } |
| 1719 | if (mem_map_ == nullptr) { |
| 1720 | LOG(ERROR) << "Could not create mem map for dex writer output: " << error_msg; |
| 1721 | if (new_file.get() != nullptr) { |
| 1722 | new_file->Erase(); |
| 1723 | } |
| 1724 | return; |
| 1725 | } |
| 1726 | DexWriter::Output(header_, mem_map_.get()); |
| 1727 | if (new_file != nullptr) { |
| 1728 | UNUSED(new_file->FlushCloseOrErase()); |
| 1729 | } |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame^] | 1730 | // Verify the output dex file's structure for debug builds. |
Jeff Hao | 4a436ac | 2017-03-10 17:05:01 -0800 | [diff] [blame] | 1731 | if (kIsDebugBuild) { |
| 1732 | std::string location = "memory mapped file for " + dex_file_location; |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame^] | 1733 | std::unique_ptr<const DexFile> output_dex_file(DexFile::Open(mem_map_->Begin(), |
| 1734 | mem_map_->Size(), |
| 1735 | location, |
| 1736 | header_->Checksum(), |
| 1737 | /*oat_dex_file*/ nullptr, |
| 1738 | /*verify*/ true, |
| 1739 | /*verify_checksum*/ false, |
| 1740 | &error_msg)); |
| 1741 | DCHECK(output_dex_file != nullptr) << "Failed to re-open output file:" << error_msg; |
| 1742 | } |
| 1743 | // Do IR-level comparison between input and output. This check ignores potential differences |
| 1744 | // due to layout, so offsets are not checked. Instead, it checks the data contents of each item. |
| 1745 | if (options_.verify_output_) { |
| 1746 | std::unique_ptr<dex_ir::Header> orig_header(dex_ir::DexIrBuilder(*dex_file)); |
| 1747 | CHECK(VerifyOutputDexFile(orig_header.get(), header_, &error_msg)) << error_msg; |
Jeff Hao | 4a436ac | 2017-03-10 17:05:01 -0800 | [diff] [blame] | 1748 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Dumps the requested sections of the file. |
| 1753 | */ |
| 1754 | void DexLayout::ProcessDexFile(const char* file_name, |
| 1755 | const DexFile* dex_file, |
| 1756 | size_t dex_file_index) { |
| 1757 | std::unique_ptr<dex_ir::Header> header(dex_ir::DexIrBuilder(*dex_file)); |
| 1758 | SetHeader(header.get()); |
| 1759 | |
| 1760 | if (options_.verbose_) { |
| 1761 | fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n", |
| 1762 | file_name, dex_file->GetHeader().magic_ + 4); |
| 1763 | } |
| 1764 | |
| 1765 | if (options_.visualize_pattern_) { |
| 1766 | VisualizeDexLayout(header_, dex_file, dex_file_index, info_); |
| 1767 | return; |
| 1768 | } |
| 1769 | |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 1770 | if (options_.show_section_statistics_) { |
| 1771 | ShowDexSectionStatistics(header_, dex_file_index); |
| 1772 | return; |
| 1773 | } |
| 1774 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1775 | // Dump dex file. |
| 1776 | if (options_.dump_) { |
| 1777 | DumpDexFile(); |
| 1778 | } |
| 1779 | |
| 1780 | // Output dex file as file or memmap. |
| 1781 | if (options_.output_dex_directory_ != nullptr || options_.output_to_memmap_) { |
Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 1782 | if (info_ != nullptr) { |
| 1783 | LayoutOutputFile(dex_file); |
| 1784 | } |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame^] | 1785 | OutputDexFile(dex_file); |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 1786 | } |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Processes a single file (either direct .dex or indirect .zip/.jar/.apk). |
| 1791 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 1792 | int DexLayout::ProcessFile(const char* file_name) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1793 | if (options_.verbose_) { |
| 1794 | fprintf(out_file_, "Processing '%s'...\n", file_name); |
| 1795 | } |
| 1796 | |
| 1797 | // If the file is not a .dex file, the function tries .zip/.jar/.apk files, |
| 1798 | // all of which are Zip archives with "classes.dex" inside. |
| 1799 | const bool verify_checksum = !options_.ignore_bad_checksum_; |
| 1800 | std::string error_msg; |
| 1801 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1802 | if (!DexFile::Open(file_name, file_name, verify_checksum, &error_msg, &dex_files)) { |
| 1803 | // Display returned error message to user. Note that this error behavior |
| 1804 | // differs from the error messages shown by the original Dalvik dexdump. |
| 1805 | fputs(error_msg.c_str(), stderr); |
| 1806 | fputc('\n', stderr); |
| 1807 | return -1; |
| 1808 | } |
| 1809 | |
| 1810 | // Success. Either report checksum verification or process |
| 1811 | // all dex files found in given file. |
| 1812 | if (options_.checksum_only_) { |
| 1813 | fprintf(out_file_, "Checksum verified\n"); |
| 1814 | } else { |
| 1815 | for (size_t i = 0; i < dex_files.size(); i++) { |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 1816 | ProcessDexFile(file_name, dex_files[i].get(), i); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | } // namespace art |