baksmali.java revision c91b03ba45ccacfa7b0ad52592a42e8fd8c18da1
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29package org.jf.baksmali; 30 31import com.google.common.base.Splitter; 32import com.google.common.collect.ImmutableList; 33import com.google.common.collect.Iterables; 34import org.jf.baksmali.Adaptors.ClassDefinition; 35import org.jf.dexlib2.analysis.ClassPath; 36import org.jf.dexlib2.analysis.InlineMethodResolver; 37import org.jf.dexlib2.iface.ClassDef; 38import org.jf.dexlib2.iface.DexFile; 39import org.jf.dexlib2.util.SyntheticAccessorResolver; 40import org.jf.util.ClassFileNameHandler; 41import org.jf.util.IndentingWriter; 42 43import java.io.*; 44import java.util.*; 45import java.util.regex.Matcher; 46import java.util.regex.Pattern; 47 48public class baksmali { 49 public static boolean noParameterRegisters = false; 50 public static boolean useLocalsDirective = false; 51 public static boolean useSequentialLabels = false; 52 public static boolean outputDebugInfo = true; 53 public static boolean addCodeOffsets = false; 54 public static boolean noAccessorComments = false; 55 public static boolean deodex = false; 56 public static InlineMethodResolver inlineResolver = null; 57 public static int registerInfo = 0; 58 public static String bootClassPath; 59 public static ClassPath classPath = null; 60 61 public static SyntheticAccessorResolver syntheticAccessorResolver = null; 62 63 public static void disassembleDexFile(String dexFilePath, DexFile dexFile, boolean deodex, String outputDirectory, 64 String[] classPathDirs, String bootClassPath, String extraBootClassPath, 65 boolean noParameterRegisters, boolean useLocalsDirective, 66 boolean useSequentialLabels, boolean outputDebugInfo, boolean addCodeOffsets, 67 boolean noAccessorComments, int registerInfo, boolean ignoreErrors, 68 String inlineTable, boolean checkPackagePrivateAccess) 69 { 70 baksmali.noParameterRegisters = noParameterRegisters; 71 baksmali.useLocalsDirective = useLocalsDirective; 72 baksmali.useSequentialLabels = useSequentialLabels; 73 baksmali.outputDebugInfo = outputDebugInfo; 74 baksmali.addCodeOffsets = addCodeOffsets; 75 baksmali.noAccessorComments = noAccessorComments; 76 baksmali.deodex = deodex; 77 baksmali.registerInfo = registerInfo; 78 baksmali.bootClassPath = bootClassPath; 79 80 if (registerInfo != 0 || deodex) { 81 try { 82 Iterable<String> extraBootClassPaths = null; 83 if (extraBootClassPath != null && extraBootClassPath.length() > 0) { 84 assert extraBootClassPath.charAt(0) == ':'; 85 extraBootClassPaths = Splitter.on(':').split(extraBootClassPath.substring(1)); 86 } else { 87 extraBootClassPaths = ImmutableList.of(); 88 } 89 90 Iterable<String> bootClassPaths = null; 91 if (bootClassPath != null) { 92 bootClassPaths = Splitter.on(':').split(bootClassPath); 93 } 94 95 classPath = ClassPath.fromClassPath(Arrays.asList(classPathDirs), 96 Iterables.concat(bootClassPaths, extraBootClassPaths), dexFile); 97 98 // TODO: uncomment 99 /*if (inlineTable != null) { 100 inlineResolver = new CustomInlineMethodResolver(inlineTable); 101 }*/ 102 } catch (Exception ex) { 103 System.err.println("\n\nError occured while loading boot class path files. Aborting."); 104 ex.printStackTrace(System.err); 105 System.exit(1); 106 } 107 } 108 109 File outputDirectoryFile = new File(outputDirectory); 110 if (!outputDirectoryFile.exists()) { 111 if (!outputDirectoryFile.mkdirs()) { 112 System.err.println("Can't create the output directory " + outputDirectory); 113 System.exit(1); 114 } 115 } 116 117 //sort the classes, so that if we're on a case-insensitive file system and need to handle classes with file 118 //name collisions, then we'll use the same name for each class, if the dex file goes through multiple 119 //baksmali/smali cycles for some reason. If a class with a colliding name is added or removed, the filenames 120 //may still change of course 121 List<ClassDef> classDefs = new ArrayList<ClassDef>(dexFile.getClasses()); 122 Collections.sort(classDefs, new Comparator<ClassDef>() { 123 public int compare(ClassDef classDef1, ClassDef classDef2) { 124 return classDef1.getType().compareTo(classDef2.getType()); 125 } 126 }); 127 classDefs = ImmutableList.copyOf(classDefs); 128 129 if (!noAccessorComments) { 130 syntheticAccessorResolver = new SyntheticAccessorResolver(classDefs); 131 } 132 133 ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDirectoryFile, ".smali"); 134 135 for (ClassDef classDef: classDefs) { 136 /** 137 * The path for the disassembly file is based on the package name 138 * The class descriptor will look something like: 139 * Ljava/lang/Object; 140 * Where the there is leading 'L' and a trailing ';', and the parts of the 141 * package name are separated by '/' 142 */ 143 144 String classDescriptor = classDef.getType(); 145 146 //validate that the descriptor is formatted like we expect 147 if (classDescriptor.charAt(0) != 'L' || 148 classDescriptor.charAt(classDescriptor.length()-1) != ';') { 149 System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class"); 150 continue; 151 } 152 153 File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor); 154 155 //create and initialize the top level string template 156 ClassDefinition classDefinition = new ClassDefinition(classDef); 157 158 //write the disassembly 159 Writer writer = null; 160 try 161 { 162 File smaliParent = smaliFile.getParentFile(); 163 if (!smaliParent.exists()) { 164 if (!smaliParent.mkdirs()) { 165 System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class"); 166 continue; 167 } 168 } 169 170 if (!smaliFile.exists()){ 171 if (!smaliFile.createNewFile()) { 172 System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class"); 173 continue; 174 } 175 } 176 177 BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter( 178 new FileOutputStream(smaliFile), "UTF8")); 179 180 writer = new IndentingWriter(bufWriter); 181 classDefinition.writeTo((IndentingWriter)writer); 182 } catch (Exception ex) { 183 System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); 184 ex.printStackTrace(); 185 smaliFile.delete(); 186 } 187 finally 188 { 189 if (writer != null) { 190 try { 191 writer.close(); 192 } catch (Throwable ex) { 193 System.err.println("\n\nError occured while closing file " + smaliFile.toString()); 194 ex.printStackTrace(); 195 } 196 } 197 } 198 199 if (!ignoreErrors && classDefinition.hadValidationErrors()) { 200 System.exit(1); 201 } 202 } 203 } 204 205 private static final Pattern extJarPattern = Pattern.compile("(?:^|\\\\|/)ext.(?:jar|odex)$"); 206 private static boolean isExtJar(String dexFilePath) { 207 Matcher m = extJarPattern.matcher(dexFilePath); 208 return m.find(); 209 } 210} 211