Fixed some issues regarding how the header and map section were being writter

git-svn-id: https://smali.googlecode.com/svn/trunk@198 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2009-06-21 06:21:45 +00:00
parent 87160fc91d
commit b1a620218e
4 changed files with 29 additions and 15 deletions

View File

@ -194,7 +194,7 @@ public class DexFile
public void place() { public void place() {
int offset = 0; int offset = 0;
offset = HeaderItemSection.place(offset); offset = 0x70;
for (IndexedSection indexedSection: indexedSections) { for (IndexedSection indexedSection: indexedSections) {
indexedSection.unplace(); indexedSection.unplace();
offset = indexedSection.place(offset); offset = indexedSection.place(offset);
@ -208,6 +208,11 @@ public class DexFile
offset = offsettedSection.place(offset); offset = offsettedSection.place(offset);
} }
HeaderItemSection.place(0);
if (offset % 4 != 0) {
offset += (4 - (offset % 4));
}
offset = MapSection.place(offset); offset = MapSection.place(offset);
dataSize = offset - dataOffset; dataSize = offset - dataOffset;

View File

@ -56,6 +56,10 @@ public class MapField extends CompositeField<MapField> {
return ItemType.fromInt(sectionTypeField.getCachedValue()); return ItemType.fromInt(sectionTypeField.getCachedValue());
} }
public Section getSection() {
return sectionInfoField.getSection();
}
public int getSectionSize() { public int getSectionSize() {
return sectionInfoField.getSectionSize(); return sectionInfoField.getSectionSize();
} }

View File

@ -52,21 +52,27 @@ public class MapItem extends IndexedItem<MapItem> {
} }
public int place(int index, int offset) { public int place(int index, int offset) {
Collections.sort(mapEntries, new Comparator<MapField>() {
public int compare(MapField o1, MapField o2) {
return ((Integer)o1.getSectionOffset()).compareTo(o2.getSectionOffset());
}
});
//we have to check if there are any empty sections before we place this item,
//because we need to remove the empty sections, which will change the size of
//this item
for (int i=0; i<mapEntries.size(); i++) { for (int i=0; i<mapEntries.size(); i++) {
MapField mapField = mapEntries.get(i); MapField mapField = mapEntries.get(i);
mapField.place(offset); if (mapField.getSection().size() == 0) {
if (mapField.getSectionSize() == 0 /*&& mapField.getSectionItemType().getMapValue() > 0x06*/) {
mapEntries.remove(i--); mapEntries.remove(i--);
} }
} }
offset = super.place(index, offset);
return super.place(index, offset); //make sure the map items are in the same order as the corresponding sections
Collections.sort(mapEntries, new Comparator<MapField>() {
public int compare(MapField o1, MapField o2) {
return ((Integer)o1.getSection().getOffset()).compareTo(o2.getSection().getOffset());
}
});
return offset;
} }
public static MapItem makeBlankMapItem(DexFile dexFile) { public static MapItem makeBlankMapItem(DexFile dexFile) {

View File

@ -54,23 +54,22 @@ public abstract class SectionHeaderInfo extends CompositeField<SectionHeaderInfo
if (!section.isPlaced()) { if (!section.isPlaced()) {
throw new RuntimeException("Trying to write a reference to a section that hasn't been placed."); throw new RuntimeException("Trying to write a reference to a section that hasn't been placed.");
} }
int size = section.size(); int size = section.size();
sectionSizeField.cacheValue(section.size()); sectionSizeField.cacheValue(size);
if (size == 0) { if (size == 0) {
//we have to set the offset to 0 or dalvik will complain //we have to set the offset to 0 or dalvik will complain
sectionOffsetField.cacheValue(0); sectionOffsetField.cacheValue(0);
} else { } else {
sectionOffsetField.cacheValue(section.getOffset()); sectionOffsetField.cacheValue(section.getOffset());
} }
super.writeTo(out); super.writeTo(out);
} }
public int place(int offset) { public int place(int offset) {
offset = super.place(offset); return super.place(offset);
sectionSizeField.cacheValue(getSection().size());
return offset;
} }
public int getSectionSize() { public int getSectionSize() {