Merge "Store native libs aligned to PAGE_SIZE"

This commit is contained in:
Narayan Kamath
2015-02-26 12:38:33 +00:00
committed by Gerrit Code Review
5 changed files with 62 additions and 18 deletions

View File

@@ -20,6 +20,7 @@ LOCAL_UNSTRIPPED_PATH:=
LOCAL_MODULE_CLASS:= LOCAL_MODULE_CLASS:=
LOCAL_MODULE_SUFFIX:= LOCAL_MODULE_SUFFIX:=
LOCAL_PACKAGE_NAME:= LOCAL_PACKAGE_NAME:=
LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES:=
LOCAL_OVERRIDES_PACKAGES:= LOCAL_OVERRIDES_PACKAGES:=
LOCAL_EXPORT_PACKAGE_RESOURCES:= LOCAL_EXPORT_PACKAGE_RESOURCES:=
LOCAL_MANIFEST_PACKAGE_NAME:= LOCAL_MANIFEST_PACKAGE_NAME:=
@@ -94,6 +95,7 @@ LOCAL_COMPRESS_MODULE_SYMBOLS:=
LOCAL_STRIP_MODULE:= LOCAL_STRIP_MODULE:=
LOCAL_JNI_SHARED_LIBRARIES:= LOCAL_JNI_SHARED_LIBRARIES:=
LOCAL_JNI_SHARED_LIBRARIES_ABI:= LOCAL_JNI_SHARED_LIBRARIES_ABI:=
LOCAL_JNI_SHARED_LIBRARIES_ZIP_OPTIONS:=
LOCAL_PREBUILT_JNI_LIBS:= LOCAL_PREBUILT_JNI_LIBS:=
LOCAL_JAR_MANIFEST:= LOCAL_JAR_MANIFEST:=
LOCAL_INSTRUMENTATION_FOR:= LOCAL_INSTRUMENTATION_FOR:=

View File

@@ -1837,7 +1837,7 @@ $(hide) mkdir -p $(addprefix $(dir $@)lib/,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI))
$(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\ $(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
$(call _add-jni-shared-libs-to-package-per-abi,$(abi),\ $(call _add-jni-shared-libs-to-package-per-abi,$(abi),\
$(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES))))) $(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
$(hide) (cd $(dir $@) && zip -r $(notdir $@) lib) $(hide) (cd $(dir $@) && zip -r $(PRIVATE_JNI_SHARED_LIBRARIES_ZIP_OPTIONS) $(notdir $@) lib)
$(hide) rm -rf $(dir $@)lib $(hide) rm -rf $(dir $@)lib
endef endef
@@ -1865,12 +1865,15 @@ $(hide) java -jar $(SIGNAPK_JAR) \
$(hide) mv $@.signed $@ $(hide) mv $@.signed $@
endef endef
# Align STORED entries of a package on 4-byte boundaries # Align STORED entries of a package on 4-byte boundaries to make them easier to mmap.
# to make them easier to mmap.
# #
define align-package define align-package
$(hide) mv $@ $@.unaligned $(hide) mv $@ $@.unaligned
$(hide) $(ZIPALIGN) -f 4 $@.unaligned $@.aligned $(hide) $(ZIPALIGN) \
-f \
$(if $(findstring true, $(PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES)),-p ,) \
4 \
$@.unaligned $@.aligned
$(hide) mv $@.aligned $@ $(hide) mv $@.aligned $@
endef endef

View File

@@ -63,6 +63,14 @@ ifeq ($(LOCAL_MODULE_TAGS),)
LOCAL_MODULE_TAGS := optional LOCAL_MODULE_TAGS := optional
endif endif
ifeq ($(LOCAL_PACKAGE_ALIGNMENT),)
LOCAL_PACKAGE_ALIGNMENT := $(DEFAULT_PACKAGE_ALIGNMENT)
endif
ifeq ($(LOCAL_JNI_SHARED_LIBRARIES_PACKAGE_ALIGNMENT),)
LOCAL_JNI_SHARED_LIBRARIES_PACKAGE_ALIGNMENT := $(DEFAULT_JNI_SHARED_LIBRARIES_PACKAGE_ALIGNMENT)
endif
ifeq ($(filter tests, $(LOCAL_MODULE_TAGS)),) ifeq ($(filter tests, $(LOCAL_MODULE_TAGS)),)
# Force localization check if it's not tagged as tests. # Force localization check if it's not tagged as tests.
LOCAL_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) -z LOCAL_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) -z
@@ -373,6 +381,8 @@ $(LOCAL_BUILT_MODULE): $(AAPT) | $(ZIPALIGN)
$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis) $(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis)
# PRIVATE_JNI_SHARED_LIBRARIES_ABI is a list of ABI names. # PRIVATE_JNI_SHARED_LIBRARIES_ABI is a list of ABI names.
$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abis) $(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abis)
$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES_ZIP_OPTIONS := $(LOCAL_JNI_SHARED_LIBRARIES_ZIP_OPTIONS)
$(LOCAL_BUILT_MODULE): PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES := $(LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES)
ifneq ($(TARGET_BUILD_APPS),) ifneq ($(TARGET_BUILD_APPS),)
# Include all resources for unbundled apps. # Include all resources for unbundled apps.
LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true
@@ -485,7 +495,7 @@ $(apk_jni_stripped) : $(LOCAL_BUILT_MODULE) | $(ZIPALIGN)
@rm -rf $(dir $@) && mkdir -p $(dir $@) @rm -rf $(dir $@) && mkdir -p $(dir $@)
$(hide) cp $< $@ $(hide) cp $< $@
$(hide) zip -d $@ $(foreach f,$(PRIVATE_JNI_SHARED_LIBRARIES),\*/$(f)) $(hide) zip -d $@ $(foreach f,$(PRIVATE_JNI_SHARED_LIBRARIES),\*/$(f))
$(call align-package) $(align-package)
$(call dist-for-goals, apps_only, $(apk_jni_stripped):$(dist_subdir)/$(LOCAL_PACKAGE_NAME).apk) $(call dist-for-goals, apps_only, $(apk_jni_stripped):$(dist_subdir)/$(LOCAL_PACKAGE_NAME).apk)

View File

@@ -170,6 +170,7 @@ endif
include $(BUILD_SYSTEM)/dex_preopt_odex_install.mk include $(BUILD_SYSTEM)/dex_preopt_odex_install.mk
####################################### #######################################
# Sign and align non-presigned .apks. # Sign and align non-presigned .apks.
$(built_module) : PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES := $(LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES)
$(built_module) : $(my_prebuilt_src_file) | $(ACP) $(ZIPALIGN) $(SIGNAPK_JAR) $(built_module) : $(my_prebuilt_src_file) | $(ACP) $(ZIPALIGN) $(SIGNAPK_JAR)
$(transform-prebuilt-to-target) $(transform-prebuilt-to-target)
ifdef extracted_jni_libs ifdef extracted_jni_libs

View File

@@ -32,20 +32,39 @@ void usage(void)
fprintf(stderr, "Zip alignment utility\n"); fprintf(stderr, "Zip alignment utility\n");
fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n"); fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
fprintf(stderr, fprintf(stderr,
"Usage: zipalign [-f] [-v] [-z] <align> infile.zip outfile.zip\n" "Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip\n"
" zipalign -c [-v] <align> infile.zip\n\n" ); " zipalign -c [-v] <align> infile.zip [list of files]\n\n" );
fprintf(stderr, fprintf(stderr,
" <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n"); " <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
fprintf(stderr, " -c: check alignment only (does not modify file)\n"); fprintf(stderr, " -c: check alignment only (does not modify file)\n");
fprintf(stderr, " -f: overwrite existing outfile.zip\n"); fprintf(stderr, " -f: overwrite existing outfile.zip\n");
fprintf(stderr, " -p: page align stored shared object: files\n");
fprintf(stderr, " -v: verbose output\n"); fprintf(stderr, " -v: verbose output\n");
fprintf(stderr, " -z: recompress using Zopfli\n"); fprintf(stderr, " -z: recompress using Zopfli\n");
} }
static const int kPageAlignment = 4096;
static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
ZipEntry* pEntry) {
if (!pageAlignSharedLibs) {
return defaultAlignment;
}
const char* ext = strrchr(pEntry->getFileName(), '.');
if (ext && strcmp(ext, ".so") == 0) {
return kPageAlignment;
}
return defaultAlignment;
}
/* /*
* Copy all entries from "pZin" to "pZout", aligning as needed. * Copy all entries from "pZin" to "pZout", aligning as needed.
*/ */
static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli) static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
bool pageAlignSharedLibs)
{ {
int numEntries = pZin->getNumEntries(); int numEntries = pZin->getNumEntries();
ZipEntry* pEntry; ZipEntry* pEntry;
@@ -75,13 +94,15 @@ static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfl
status = pZout->add(pZin, pEntry, padding, &pNewEntry); status = pZout->add(pZin, pEntry, padding, &pNewEntry);
} }
} else { } else {
const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
/* /*
* Copy the entry, adjusting as required. We assume that the * Copy the entry, adjusting as required. We assume that the
* file position in the new file will be equal to the file * file position in the new file will be equal to the file
* position in the original. * position in the original.
*/ */
long newOffset = pEntry->getFileOffset() + bias; long newOffset = pEntry->getFileOffset() + bias;
padding = (alignment - (newOffset % alignment)) % alignment; padding = (alignTo - (newOffset % alignTo)) % alignTo;
//printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n", //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
// pEntry->getFileName(), (long) pEntry->getFileOffset(), // pEntry->getFileName(), (long) pEntry->getFileOffset(),
@@ -105,7 +126,7 @@ static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfl
* output file exists and "force" wasn't specified. * output file exists and "force" wasn't specified.
*/ */
static int process(const char* inFileName, const char* outFileName, static int process(const char* inFileName, const char* outFileName,
int alignment, bool force, bool zopfli) int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
{ {
ZipFile zin, zout; ZipFile zin, zout;
@@ -136,7 +157,7 @@ static int process(const char* inFileName, const char* outFileName,
return 1; return 1;
} }
int result = copyAndAlign(&zin, &zout, alignment, zopfli); int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
if (result != 0) { if (result != 0) {
printf("zipalign: failed rewriting '%s' to '%s'\n", printf("zipalign: failed rewriting '%s' to '%s'\n",
inFileName, outFileName); inFileName, outFileName);
@@ -147,7 +168,8 @@ static int process(const char* inFileName, const char* outFileName,
/* /*
* Verify the alignment of a zip archive. * Verify the alignment of a zip archive.
*/ */
static int verify(const char* fileName, int alignment, bool verbose) static int verify(const char* fileName, int alignment, bool verbose,
bool pageAlignSharedLibs)
{ {
ZipFile zipFile; ZipFile zipFile;
bool foundBad = false; bool foundBad = false;
@@ -172,11 +194,12 @@ static int verify(const char* fileName, int alignment, bool verbose)
} }
} else { } else {
long offset = pEntry->getFileOffset(); long offset = pEntry->getFileOffset();
if ((offset % alignment) != 0) { const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
if ((offset % alignTo) != 0) {
if (verbose) { if (verbose) {
printf("%8ld %s (BAD - %ld)\n", printf("%8ld %s (BAD - %ld)\n",
(long) offset, pEntry->getFileName(), (long) offset, pEntry->getFileName(),
offset % alignment); offset % alignTo);
} }
foundBad = true; foundBad = true;
} else { } else {
@@ -204,6 +227,7 @@ int main(int argc, char* const argv[])
bool force = false; bool force = false;
bool verbose = false; bool verbose = false;
bool zopfli = false; bool zopfli = false;
bool pageAlignSharedLibs = false;
int result = 1; int result = 1;
int alignment; int alignment;
char* endp; char* endp;
@@ -233,6 +257,9 @@ int main(int argc, char* const argv[])
case 'z': case 'z':
zopfli = true; zopfli = true;
break; break;
case 'p':
pageAlignSharedLibs = true;
break;
default: default:
fprintf(stderr, "ERROR: unknown flag -%c\n", *cp); fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
wantUsage = true; wantUsage = true;
@@ -260,14 +287,15 @@ int main(int argc, char* const argv[])
if (check) { if (check) {
/* check existing archive for correct alignment */ /* check existing archive for correct alignment */
result = verify(argv[1], alignment, verbose); result = verify(argv[1], alignment, verbose, pageAlignSharedLibs);
} else { } else {
/* create the new archive */ /* create the new archive */
result = process(argv[1], argv[2], alignment, force, zopfli); result = process(argv[1], argv[2], alignment, force, zopfli, pageAlignSharedLibs);
/* trust, but verify */ /* trust, but verify */
if (result == 0) if (result == 0) {
result = verify(argv[2], alignment, verbose); result = verify(argv[2], alignment, verbose, pageAlignSharedLibs);
}
} }
bail: bail: