baksmali.java revision a55990c876eab2489e824711da23e5abc7bff1a5
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.dexbacked.DexBackedOdexFile; 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 void disassembleDexFile(String dexFilePath, DexFile dexFile, boolean deodex, String outputDirectory, 50 String[] classPathDirs, String bootClassPath, String extraBootClassPath, 51 boolean noParameterRegisters, boolean useLocalsDirective, 52 boolean useSequentialLabels, boolean outputDebugInfo, boolean addCodeOffsets, 53 boolean noAccessorComments, int registerInfo, boolean ignoreErrors, 54 String inlineTable, boolean checkPackagePrivateAccess) 55 { 56 baksmaliOptions options = new baksmaliOptions(); 57 58 options.noParameterRegisters = noParameterRegisters; 59 options.useLocalsDirective = useLocalsDirective; 60 options.useSequentialLabels = useSequentialLabels; 61 options.outputDebugInfo = outputDebugInfo; 62 options.addCodeOffsets = addCodeOffsets; 63 options.noAccessorComments = noAccessorComments; 64 options.deodex = deodex; 65 options.registerInfo = registerInfo; 66 options.bootClassPath = bootClassPath; 67 68 if (registerInfo != 0 || deodex) { 69 try { 70 Iterable<String> extraBootClassPaths = null; 71 if (extraBootClassPath != null && extraBootClassPath.length() > 0) { 72 assert extraBootClassPath.charAt(0) == ':'; 73 extraBootClassPaths = Splitter.on(':').split(extraBootClassPath.substring(1)); 74 } else { 75 extraBootClassPaths = ImmutableList.of(); 76 } 77 78 Iterable<String> bootClassPaths = null; 79 if (bootClassPath != null) { 80 bootClassPaths = Splitter.on(':').split(bootClassPath); 81 } else if (dexFile instanceof DexBackedOdexFile) { 82 bootClassPaths = ((DexBackedOdexFile)dexFile).getDependencies(); 83 }else { 84 bootClassPaths = ImmutableList.of(); 85 } 86 87 options.classPath = ClassPath.fromClassPath(Arrays.asList(classPathDirs), 88 Iterables.concat(bootClassPaths, extraBootClassPaths), dexFile); 89 90 // TODO: uncomment 91 /*if (inlineTable != null) { 92 inlineResolver = new CustomInlineMethodResolver(inlineTable); 93 }*/ 94 } catch (Exception ex) { 95 System.err.println("\n\nError occured while loading boot class path files. Aborting."); 96 ex.printStackTrace(System.err); 97 System.exit(1); 98 } 99 } 100 101 File outputDirectoryFile = new File(outputDirectory); 102 if (!outputDirectoryFile.exists()) { 103 if (!outputDirectoryFile.mkdirs()) { 104 System.err.println("Can't create the output directory " + outputDirectory); 105 System.exit(1); 106 } 107 } 108 109 //sort the classes, so that if we're on a case-insensitive file system and need to handle classes with file 110 //name collisions, then we'll use the same name for each class, if the dex file goes through multiple 111 //baksmali/smali cycles for some reason. If a class with a colliding name is added or removed, the filenames 112 //may still change of course 113 List<ClassDef> classDefs = new ArrayList<ClassDef>(dexFile.getClasses()); 114 Collections.sort(classDefs, new Comparator<ClassDef>() { 115 public int compare(ClassDef classDef1, ClassDef classDef2) { 116 return classDef1.getType().compareTo(classDef2.getType()); 117 } 118 }); 119 classDefs = ImmutableList.copyOf(classDefs); 120 121 if (!noAccessorComments) { 122 options.syntheticAccessorResolver = new SyntheticAccessorResolver(classDefs); 123 } 124 125 ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDirectoryFile, ".smali"); 126 127 for (ClassDef classDef: classDefs) { 128 /** 129 * The path for the disassembly file is based on the package name 130 * The class descriptor will look something like: 131 * Ljava/lang/Object; 132 * Where the there is leading 'L' and a trailing ';', and the parts of the 133 * package name are separated by '/' 134 */ 135 136 String classDescriptor = classDef.getType(); 137 138 //validate that the descriptor is formatted like we expect 139 if (classDescriptor.charAt(0) != 'L' || 140 classDescriptor.charAt(classDescriptor.length()-1) != ';') { 141 System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class"); 142 continue; 143 } 144 145 File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor); 146 147 //create and initialize the top level string template 148 ClassDefinition classDefinition = new ClassDefinition(options, classDef); 149 150 //write the disassembly 151 Writer writer = null; 152 try 153 { 154 File smaliParent = smaliFile.getParentFile(); 155 if (!smaliParent.exists()) { 156 if (!smaliParent.mkdirs()) { 157 System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class"); 158 continue; 159 } 160 } 161 162 if (!smaliFile.exists()){ 163 if (!smaliFile.createNewFile()) { 164 System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class"); 165 continue; 166 } 167 } 168 169 BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter( 170 new FileOutputStream(smaliFile), "UTF8")); 171 172 writer = new IndentingWriter(bufWriter); 173 classDefinition.writeTo((IndentingWriter)writer); 174 } catch (Exception ex) { 175 System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); 176 ex.printStackTrace(); 177 smaliFile.delete(); 178 } 179 finally 180 { 181 if (writer != null) { 182 try { 183 writer.close(); 184 } catch (Throwable ex) { 185 System.err.println("\n\nError occured while closing file " + smaliFile.toString()); 186 ex.printStackTrace(); 187 } 188 } 189 } 190 191 if (!ignoreErrors && classDefinition.hadValidationErrors()) { 192 System.exit(1); 193 } 194 } 195 } 196 197 private static final Pattern extJarPattern = Pattern.compile("(?:^|\\\\|/)ext.(?:jar|odex)$"); 198 private static boolean isExtJar(String dexFilePath) { 199 Matcher m = extJarPattern.matcher(dexFilePath); 200 return m.find(); 201 } 202} 203