baksmali.java revision 0b2f7d6a57e90424b3ee455c041aab3996c05f2c
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 org.jf.baksmali.Adaptors.ClassDefinition; 32import org.jf.dexlib.Code.Analysis.ClassPath; 33import org.jf.dexlib.DexFile; 34import org.jf.dexlib.ClassDefItem; 35 36import java.io.*; 37import java.util.regex.Matcher; 38import java.util.regex.Pattern; 39 40public class baksmali { 41 public static boolean noParameterRegisters = false; 42 public static boolean useLocalsDirective = false; 43 public static boolean useSequentialLabels = false; 44 public static boolean outputDebugInfo = true; 45 public static boolean addCodeOffsets = false; 46 public static boolean deodex = false; 47 public static boolean verify = false; 48 public static int registerInfo = 0; 49 public static String bootClassPath; 50 51 public static void disassembleDexFile(String dexFilePath, DexFile dexFile, boolean deodex, String outputDirectory, 52 String[] classPathDirs, String bootClassPath, String extraBootClassPath, 53 boolean noParameterRegisters, boolean useLocalsDirective, 54 boolean useSequentialLabels, boolean outputDebugInfo, boolean addCodeOffsets, 55 int registerInfo, boolean verify) 56 { 57 baksmali.noParameterRegisters = noParameterRegisters; 58 baksmali.useLocalsDirective = useLocalsDirective; 59 baksmali.useSequentialLabels = useSequentialLabels; 60 baksmali.outputDebugInfo = outputDebugInfo; 61 baksmali.addCodeOffsets = addCodeOffsets; 62 baksmali.deodex = deodex; 63 baksmali.registerInfo = registerInfo; 64 baksmali.bootClassPath = bootClassPath; 65 baksmali.verify = verify; 66 67 if (registerInfo != 0 || deodex || verify) { 68 try { 69 String[] extraBootClassPathArray = null; 70 if (extraBootClassPath != null && extraBootClassPath.length() > 0) { 71 assert extraBootClassPath.charAt(0) == ':'; 72 extraBootClassPathArray = extraBootClassPath.substring(1).split(":"); 73 } 74 75 if (dexFile.isOdex() && bootClassPath == null) { 76 //ext.jar is a special case - it is typically the 2nd jar in the boot class path, but it also 77 //depends on classes in framework.jar. If the user didn't specify a -c option, we should add 78 //framework.jar to the boot class path by default, so that it "just works" 79 if (extraBootClassPathArray == null && isExtJar(dexFilePath)) { 80 extraBootClassPath = ":framework.jar"; 81 } 82 ClassPath.InitializeClassPathFromOdex(classPathDirs, extraBootClassPathArray, dexFilePath, dexFile); 83 } else { 84 String[] bootClassPathArray = null; 85 if (bootClassPath != null) { 86 bootClassPathArray = bootClassPath.split(":"); 87 } 88 ClassPath.InitializeClassPath(classPathDirs, bootClassPathArray, extraBootClassPathArray, 89 dexFilePath, dexFile); 90 } 91 } catch (Exception ex) { 92 System.err.println("\n\nError occured while loading boot class path files. Aborting."); 93 ex.printStackTrace(System.err); 94 System.exit(1); 95 } 96 } 97 98 File outputDirectoryFile = new File(outputDirectory); 99 if (!outputDirectoryFile.exists()) { 100 if (!outputDirectoryFile.mkdirs()) { 101 System.err.println("Can't create the output directory " + outputDirectory); 102 System.exit(1); 103 } 104 } 105 106 for (ClassDefItem classDefItem: dexFile.ClassDefsSection.getItems()) { 107 /** 108 * The path for the disassembly file is based on the package name 109 * The class descriptor will look something like: 110 * Ljava/lang/Object; 111 * Where the there is leading 'L' and a trailing ';', and the parts of the 112 * package name are separated by '/' 113 */ 114 115 String classDescriptor = classDefItem.getClassType().getTypeDescriptor(); 116 117 //validate that the descriptor is formatted like we expect 118 if (classDescriptor.charAt(0) != 'L' || 119 classDescriptor.charAt(classDescriptor.length()-1) != ';') { 120 System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class"); 121 continue; 122 } 123 124 //trim off the leading L and trailing ; 125 classDescriptor = classDescriptor.substring(1, classDescriptor.length()-1); 126 127 //trim off the leading 'L' and trailing ';', and get the individual package elements 128 String[] pathElements = classDescriptor.split("/"); 129 130 //build the path to the smali file to generate for this class 131 StringBuilder smaliPath = new StringBuilder(outputDirectory); 132 for (String pathElement: pathElements) { 133 smaliPath.append(File.separatorChar); 134 smaliPath.append(pathElement); 135 } 136 smaliPath.append(".smali"); 137 138 File smaliFile = new File(smaliPath.toString()); 139 140 //create and initialize the top level string template 141 ClassDefinition classDefinition = new ClassDefinition(classDefItem); 142 143 //write the disassembly 144 Writer writer = null; 145 try 146 { 147 File smaliParent = smaliFile.getParentFile(); 148 if (!smaliParent.exists()) { 149 if (!smaliParent.mkdirs()) { 150 System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class"); 151 continue; 152 } 153 } 154 155 if (!smaliFile.exists()){ 156 if (!smaliFile.createNewFile()) { 157 System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class"); 158 continue; 159 } 160 } 161 162 BufferedWriter bufWriter = new BufferedWriter(new FileWriter(smaliFile)); 163 164 writer = new IndentingWriter(bufWriter); 165 classDefinition.writeTo((IndentingWriter)writer); 166 } catch (Exception ex) { 167 System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); 168 ex.printStackTrace(); 169 } 170 finally 171 { 172 if (writer != null) { 173 try { 174 writer.close(); 175 } catch (Throwable ex) { 176 System.err.println("\n\nError occured while closing file " + smaliFile.toString()); 177 ex.printStackTrace(); 178 } 179 } 180 } 181 182 //TODO: GROT 183 if (classDefinition.hadValidationErrors()) { 184 System.exit(1); 185 } 186 } 187 } 188 189 private static final Pattern extJarPattern = Pattern.compile("(?:^|\\\\|/)ext.(?:jar|odex)$"); 190 private static boolean isExtJar(String dexFilePath) { 191 Matcher m = extJarPattern.matcher(dexFilePath); 192 return m.find(); 193 } 194} 195