baksmali.java revision c1cc0e093492722e7eecb20455b5242176ca63db
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 (typically the 3rd jar in the BCP). If the user didn't
78                    //specify a -c option, we should add framework.jar to the boot class path by default, so that it
79                    //"just works"
80                    if (extraBootClassPathArray == null && isExtJar(dexFilePath)) {
81                        extraBootClassPathArray = new String[] {"framework.jar"};
82                    }
83                    ClassPath.InitializeClassPathFromOdex(classPathDirs, extraBootClassPathArray, dexFilePath, dexFile);
84                } else {
85                    String[] bootClassPathArray = null;
86                    if (bootClassPath != null) {
87                        bootClassPathArray = bootClassPath.split(":");
88                    }
89                    ClassPath.InitializeClassPath(classPathDirs, bootClassPathArray, extraBootClassPathArray,
90                            dexFilePath, dexFile);
91                }
92            } catch (Exception ex) {
93                System.err.println("\n\nError occured while loading boot class path files. Aborting.");
94                ex.printStackTrace(System.err);
95                System.exit(1);
96            }
97        }
98
99        File outputDirectoryFile = new File(outputDirectory);
100        if (!outputDirectoryFile.exists()) {
101            if (!outputDirectoryFile.mkdirs()) {
102                System.err.println("Can't create the output directory " + outputDirectory);
103                System.exit(1);
104            }
105        }
106
107        for (ClassDefItem classDefItem: dexFile.ClassDefsSection.getItems()) {
108            /**
109             * The path for the disassembly file is based on the package name
110             * The class descriptor will look something like:
111             * Ljava/lang/Object;
112             * Where the there is leading 'L' and a trailing ';', and the parts of the
113             * package name are separated by '/'
114             */
115
116            if (registerInfo != 0 || deodex || verify) {
117                //If we are analyzing the bytecode, make sure that this class is loaded into the ClassPath. If it isn't
118                //then there was some error while loading it, and we should skip it
119                ClassPath.ClassDef classDef = ClassPath.getClassDef(classDefItem.getClassType(), false);
120                if (classDef == null || classDef instanceof ClassPath.UnresolvedClassDef) {
121                    continue;
122                }
123            }
124
125            String classDescriptor = classDefItem.getClassType().getTypeDescriptor();
126
127            //validate that the descriptor is formatted like we expect
128            if (classDescriptor.charAt(0) != 'L' ||
129                classDescriptor.charAt(classDescriptor.length()-1) != ';') {
130                System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
131                continue;
132            }
133
134            //trim off the leading L and trailing ;
135            classDescriptor = classDescriptor.substring(1, classDescriptor.length()-1);
136
137            //trim off the leading 'L' and trailing ';', and get the individual package elements
138            String[] pathElements = classDescriptor.split("/");
139
140            //build the path to the smali file to generate for this class
141            StringBuilder smaliPath = new StringBuilder(outputDirectory);
142            for (String pathElement: pathElements) {
143                smaliPath.append(File.separatorChar);
144                smaliPath.append(pathElement);
145            }
146            smaliPath.append(".smali");
147
148            File smaliFile = new File(smaliPath.toString());
149
150            //create and initialize the top level string template
151            ClassDefinition classDefinition = new ClassDefinition(classDefItem);
152
153            //write the disassembly
154            Writer writer = null;
155            try
156            {
157                File smaliParent = smaliFile.getParentFile();
158                if (!smaliParent.exists()) {
159                    if (!smaliParent.mkdirs()) {
160                        System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
161                        continue;
162                    }
163                }
164
165                if (!smaliFile.exists()){
166                    if (!smaliFile.createNewFile()) {
167                        System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
168                        continue;
169                    }
170                }
171
172                BufferedWriter bufWriter = new BufferedWriter(new FileWriter(smaliFile));
173
174                writer = new IndentingWriter(bufWriter);
175                classDefinition.writeTo((IndentingWriter)writer);
176            } catch (Exception ex) {
177                System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
178                ex.printStackTrace();
179            }
180            finally
181            {
182                if (writer != null) {
183                    try {
184                        writer.close();
185                    } catch (Throwable ex) {
186                        System.err.println("\n\nError occured while closing file " + smaliFile.toString());
187                        ex.printStackTrace();
188                    }
189                }
190            }
191        }
192    }
193
194    private static final Pattern extJarPattern = Pattern.compile("(?:^|\\\\|/)ext.(?:jar|odex)$");
195    private static boolean isExtJar(String dexFilePath) {
196        Matcher m = extJarPattern.matcher(dexFilePath);
197        return m.find();
198    }
199}
200