blob: bfad44b26e95421effcd2b43187903d48df155d0 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Dan Bornstein6f9edbe2011-06-02 12:24:11 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
Dan Bornstein6f9edbe2011-06-02 12:24:11 -070018 * Utility functions for dealing with optimized dex files.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080019 */
Dan Bornstein6f9edbe2011-06-02 12:24:11 -070020
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080021#include
22#include
23#include
24#include
25#include
26#include
27#include
28#include
29
30#include "OptInvocation.h"
31#include "DexFile.h"
32
Dan Bornstein6f9edbe2011-06-02 12:24:11 -070033static const char* kCacheDirectoryName = "dalvik-cache";
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080034static const char* kClassesDex = "classes.dex";
35
Ian Rogerse4e5d8c2014-05-01 08:53:35 -070036#if defined(__aarch64__)
37static const char* kInstructionSet = "arm64";
38#elif defined(__arm__)
39static const char* kInstructionSet = "arm";
Narayan Kamath44e824d2014-04-29 13:56:36 +010040#elif defined(__i386__)
Ian Rogerse4e5d8c2014-05-01 08:53:35 -070041static const char* kInstructionSet = "x86";
Narayan Kamath44e824d2014-04-29 13:56:36 +010042#elif defined(__mips__)
Ian Rogerse4e5d8c2014-05-01 08:53:35 -070043static const char* kInstructionSet = "mips";
44#elif defined(__x86_64__)
45static const char* kInstructionSet = "x86_64";
Narayan Kamath44e824d2014-04-29 13:56:36 +010046#else
47#error Unsupported instruction set.
48#endif
49
Brian Carlstrom7d765f12014-05-29 12:50:07 -070050static int dexOptMkdir(const char* path, int mode)
51{
52#ifdef _WIN32
53 return mkdir(path);
54#else
55 return mkdir(path, mode);
56#endif
57}
Narayan Kamath44e824d2014-04-29 13:56:36 +010058
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080059/*
60 * Given the filename of a .jar or .dex file, construct the DEX file cache
61 * name.
62 *
63 * For a Jar, "subFileName" is the name of the entry (usually "classes.dex").
64 * For a DEX, it may be NULL.
65 *
66 * Returns a newly-allocated string, or NULL on failure.
67 */
68char* dexOptGenerateCacheFileName(const char* fileName, const char* subFileName)
69{
70 char nameBuf[512];
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080071 char absoluteFile[sizeof(nameBuf)];
72 const size_t kBufLen = sizeof(nameBuf) - 1;
73 const char* dataRoot;
74 char* cp;
75
76 /*
77 * Get the absolute path of the Jar or DEX file.
78 */
79 absoluteFile[0] = '\0';
80 if (fileName[0] != '/') {
81 /*
82 * Generate the absolute path. This doesn't do everything it
83 * should, e.g. if filename is "./out/whatever" it doesn't crunch
84 * the leading "./" out, but it'll do.
85 */
86 if (getcwd(absoluteFile, kBufLen) == NULL) {
Steve Blockc1a4ab92012-01-06 19:16:58 +000087 ALOGE("Can't get CWD while opening jar file");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080088 return NULL;
89 }
Aart Bik338aeaf2016-04-21 15:51:41 -070090 strncat(absoluteFile, "/", kBufLen - strlen(absoluteFile));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080091 }
Aart Bik338aeaf2016-04-21 15:51:41 -070092 strncat(absoluteFile, fileName, kBufLen - strlen(absoluteFile));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080093
94 /*
95 * Append the name of the Jar file entry, if any. This is not currently
96 * required, but will be if we start putting more than one DEX file
97 * in a Jar.
98 */
99 if (subFileName != NULL) {
Aart Bik338aeaf2016-04-21 15:51:41 -0700100 strncat(absoluteFile, "/", kBufLen - strlen(absoluteFile));
101 strncat(absoluteFile, subFileName, kBufLen - strlen(absoluteFile));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800102 }
103
104 /* Turn the path into a flat filename by replacing
105 * any slashes after the first one with '@' characters.
106 */
107 cp = absoluteFile + 1;
108 while (*cp != '\0') {
109 if (*cp == '/') {
110 *cp = '@';
111 }
112 cp++;
113 }
114
115 /* Build the name of the cache directory.
116 */
117 dataRoot = getenv("ANDROID_DATA");
118 if (dataRoot == NULL)
119 dataRoot = "/data";
Brian Carlstromcc54b982014-05-28 11:04:29 -0700120 snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kCacheDirectoryName);
121 if (strcmp(dataRoot, "/data") != 0) {
Brian Carlstrom7d765f12014-05-29 12:50:07 -0700122 int result = dexOptMkdir(nameBuf, 0700);
Brian Carlstromcc54b982014-05-28 11:04:29 -0700123 if (result != 0 && errno != EEXIST) {
124 ALOGE("Failed to create dalvik-cache directory %s: %s", nameBuf, strerror(errno));
125 return NULL;
126 }
127 }
Narayan Kamath44e824d2014-04-29 13:56:36 +0100128 snprintf(nameBuf, kBufLen, "%s/%s/%s", dataRoot, kCacheDirectoryName, kInstructionSet);
Brian Carlstromcc54b982014-05-28 11:04:29 -0700129 if (strcmp(dataRoot, "/data") != 0) {
Brian Carlstrom7d765f12014-05-29 12:50:07 -0700130 int result = dexOptMkdir(nameBuf, 0700);
Brian Carlstromcc54b982014-05-28 11:04:29 -0700131 if (result != 0 && errno != EEXIST) {
132 ALOGE("Failed to create dalvik-cache directory %s: %s", nameBuf, strerror(errno));
133 return NULL;
134 }
135 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800136
137 /* Tack on the file name for the actual cache file path.
138 */
Aart Bik338aeaf2016-04-21 15:51:41 -0700139 strncat(nameBuf, absoluteFile, kBufLen - strlen(nameBuf));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800140
Steve Block92c1f6f2011-10-20 11:55:54 +0100141 ALOGV("Cache file for '%s' '%s' is '%s'", fileName, subFileName, nameBuf);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800142 return strdup(nameBuf);
143}
144
145/*
146 * Create a skeletal "opt" header in a new file. Most of the fields are
147 * initialized to garbage, but we fill in "dexOffset" so others can
148 * see how large the header is.
149 *
150 * "fd" must be positioned at the start of the file. On return, it will
151 * be positioned just past the header, and the place where the DEX data
152 * should go.
153 *
154 * Returns 0 on success, errno on failure.
155 */
156int dexOptCreateEmptyHeader(int fd)
157{
158 DexOptHeader optHdr;
159 ssize_t actual;
160
161 assert(lseek(fd, 0, SEEK_CUR) == 0);
162
163 /*
164 * The data is only expected to be readable on the current system, so
165 * we just write the structure. We do need the file offset to be 64-bit
166 * aligned to fulfill a DEX requirement.
167 */
168 assert((sizeof(optHdr) & 0x07) == 0);
169 memset(&optHdr, 0xff, sizeof(optHdr));
170 optHdr.dexOffset = sizeof(optHdr);
171 actual = write(fd, &optHdr, sizeof(optHdr));
172 if (actual != sizeof(optHdr)) {
173 int err = errno ? errno : -1;
Steve Blockc1a4ab92012-01-06 19:16:58 +0000174 ALOGE("opt header write failed: %s", strerror(errno));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800175 return errno;
176 }
177
178 return 0;
179}