Tweak dexlib's "skip instructions" functionality so that it doesn't read in the CodeItems or DebugInfoItems at all

git-svn-id: https://smali.googlecode.com/svn/trunk@635 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2010-02-22 00:56:26 +00:00
parent 2bdbf739bf
commit d79d9ebbe9
3 changed files with 53 additions and 66 deletions

View File

@ -478,8 +478,13 @@ public class ClassDataItem extends Item<ClassDataItem> {
int previousIndex = previousEncodedMethod==null?0:previousEncodedMethod.method.getIndex(); int previousIndex = previousEncodedMethod==null?0:previousEncodedMethod.method.getIndex();
method = dexFile.MethodIdsSection.getItemByIndex(in.readUnsignedLeb128() + previousIndex); method = dexFile.MethodIdsSection.getItemByIndex(in.readUnsignedLeb128() + previousIndex);
accessFlags = in.readUnsignedLeb128(); accessFlags = in.readUnsignedLeb128();
codeItem = (CodeItem)readContext.getOptionalOffsettedItemByOffset(ItemType.TYPE_CODE_ITEM, if (dexFile.skipInstructions()) {
in.readUnsignedLeb128()); in.readUnsignedLeb128();
codeItem = null;
} else {
codeItem = (CodeItem)readContext.getOptionalOffsettedItemByOffset(ItemType.TYPE_CODE_ITEM,
in.readUnsignedLeb128());
}
if (codeItem != null) { if (codeItem != null) {
codeItem.setParent(this); codeItem.setParent(this);
} }

View File

@ -151,77 +151,55 @@ public class CodeItem extends Item<CodeItem> {
int instructionCount = in.readInt(); int instructionCount = in.readInt();
if (dexFile.skipInstructions()) { final ArrayList<Instruction> instructionList = new ArrayList<Instruction>();
in.skipBytes(instructionCount * 2);
if (triesCount > 0) { byte[] encodedInstructions = in.readBytes(instructionCount * 2);
in.alignTo(4); InstructionIterator.IterateInstructions(dexFile, encodedInstructions,
in.skipBytes(8 * triesCount); new InstructionIterator.ProcessInstructionDelegate() {
public void ProcessInstruction(int codeAddress, Instruction instruction) {
instructionList.add(instruction);
}
});
int handlerCount = in.readUnsignedLeb128(); this.instructions = new Instruction[instructionList.size()];
for (int i=0; i<handlerCount; i++) { instructionList.toArray(instructions);
int size = in.readSignedLeb128();
int absSize = Math.abs(size); if (triesCount > 0) {
for (int j=0; j<absSize; j++) { in.alignTo(4);
in.readUnsignedLeb128();
in.readUnsignedLeb128(); //we need to read in the catch handlers first, so save the offset to the try items for future reference
} int triesOffset = in.getCursor();
if (size <= 0) { in.setCursor(triesOffset + 8 * triesCount);
in.readUnsignedLeb128();
} //read in the encoded catch handlers
int encodedHandlerStart = in.getCursor();
int handlerCount = in.readUnsignedLeb128();
SparseArray<EncodedCatchHandler> handlerMap = new SparseArray<EncodedCatchHandler>(handlerCount);
encodedCatchHandlers = new EncodedCatchHandler[handlerCount];
for (int i=0; i<handlerCount; i++) {
try {
int position = in.getCursor() - encodedHandlerStart;
encodedCatchHandlers[i] = new EncodedCatchHandler(dexFile, in);
handlerMap.append(position, encodedCatchHandlers[i]);
} catch (Exception ex) {
throw ExceptionWithContext.withContext(ex, "Error while reading EncodedCatchHandler at index " + i);
} }
} }
} else { int codeItemEnd = in.getCursor();
final ArrayList<Instruction> instructionList = new ArrayList<Instruction>();
byte[] encodedInstructions = in.readBytes(instructionCount * 2); //now go back and read the tries
InstructionIterator.IterateInstructions(dexFile, encodedInstructions, in.setCursor(triesOffset);
new InstructionIterator.ProcessInstructionDelegate() { tries = new TryItem[triesCount];
public void ProcessInstruction(int codeAddress, Instruction instruction) { for (int i=0; i<triesCount; i++) {
instructionList.add(instruction); try {
} tries[i] = new TryItem(in, handlerMap);
}); } catch (Exception ex) {
throw ExceptionWithContext.withContext(ex, "Error while reading TryItem at index " + i);
this.instructions = new Instruction[instructionList.size()];
instructionList.toArray(instructions);
if (triesCount > 0) {
in.alignTo(4);
//we need to read in the catch handlers first, so save the offset to the try items for future reference
int triesOffset = in.getCursor();
in.setCursor(triesOffset + 8 * triesCount);
//read in the encoded catch handlers
int encodedHandlerStart = in.getCursor();
int handlerCount = in.readUnsignedLeb128();
SparseArray<EncodedCatchHandler> handlerMap = new SparseArray<EncodedCatchHandler>(handlerCount);
encodedCatchHandlers = new EncodedCatchHandler[handlerCount];
for (int i=0; i<handlerCount; i++) {
try {
int position = in.getCursor() - encodedHandlerStart;
encodedCatchHandlers[i] = new EncodedCatchHandler(dexFile, in);
handlerMap.append(position, encodedCatchHandlers[i]);
} catch (Exception ex) {
throw ExceptionWithContext.withContext(ex, "Error while reading EncodedCatchHandler at index " + i);
}
} }
int codeItemEnd = in.getCursor();
//now go back and read the tries
in.setCursor(triesOffset);
tries = new TryItem[triesCount];
for (int i=0; i<triesCount; i++) {
try {
tries[i] = new TryItem(in, handlerMap);
} catch (Exception ex) {
throw ExceptionWithContext.withContext(ex, "Error while reading TryItem at index " + i);
}
}
//and now back to the end of the code item
in.setCursor(codeItemEnd);
} }
//and now back to the end of the code item
in.setCursor(codeItemEnd);
} }
} }

View File

@ -409,6 +409,10 @@ public class DexFile
continue; continue;
} }
if (skipInstructions && (section == CodeItemsSection || section == DebugInfoItemsSection)) {
continue;
}
int sectionOffset = readContext.getSectionOffset(section.ItemType); int sectionOffset = readContext.getSectionOffset(section.ItemType);
if (sectionOffset > 0) { if (sectionOffset > 0) {
int sectionSize = readContext.getSectionSize(section.ItemType); int sectionSize = readContext.getSectionSize(section.ItemType);