3 ** Copyright 2016, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
24 #include <sys/types.h>
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <ziparchive/zip_archive.h>
36 explicit ArchiveHelper(int fd, const std::string& debug_filename) : valid_(false) {
37 int rc = OpenArchiveFd(fd, "", &handle_, false);
41 LOG(ERROR) << "Failed to open archive " << debug_filename << ": " << ErrorCodeString(rc);
46 CloseArchive(handle_);
49 bool valid() const { return valid_; }
50 ZipArchiveHandle &archive_handle() { return handle_; }
53 ZipArchiveHandle handle_;
57 std::map<ApkInspector::ApkOffset, std::unique_ptr<EmbeddedElf>> ApkInspector::embedded_elf_cache_;
59 EmbeddedElf* ApkInspector::FindElfInApkByOffset(const std::string& apk_path, off64_t file_offset) {
61 ApkOffset ami(apk_path, file_offset);
62 auto it = embedded_elf_cache_.find(ami);
63 if (it != embedded_elf_cache_.end()) {
64 return it->second.get();
66 std::unique_ptr<EmbeddedElf> elf = FindElfInApkByOffsetWithoutCache(apk_path, file_offset);
67 EmbeddedElf* result = elf.get();
68 embedded_elf_cache_[ami] = std::move(elf);
72 std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByOffsetWithoutCache(const std::string& apk_path,
73 off64_t file_offset) {
74 // Crack open the apk(zip) file and take a look.
75 if (!IsValidApkPath(apk_path)) {
78 FileHelper fhelper(apk_path.c_str());
83 ArchiveHelper ahelper(fhelper.fd(), apk_path);
84 if (!ahelper.valid()) {
87 ZipArchiveHandle &handle = ahelper.archive_handle();
89 // Iterate through the zip file. Look for a zip entry corresponding
90 // to an uncompressed blob whose range intersects with the mmap
91 // offset we're interested in.
92 void* iteration_cookie;
93 if (StartIteration(handle, &iteration_cookie, nullptr, nullptr) < 0) {
100 while ((zrc = Next(iteration_cookie, &zentry, &zname)) == 0) {
101 if (zentry.method == kCompressStored &&
102 file_offset >= zentry.offset &&
103 file_offset < zentry.offset + zentry.uncompressed_length) {
109 EndIteration(iteration_cookie);
114 // We found something in the zip file at the right spot. Is it an ELF?
115 if (lseek(fhelper.fd(), zentry.offset, SEEK_SET) != zentry.offset) {
116 PLOG(ERROR) << "lseek() failed in " << apk_path << " offset " << zentry.offset;
119 std::string entry_name;
120 entry_name.resize(zname.name_length,'\0');
121 memcpy(&entry_name[0], zname.name, zname.name_length);
122 if (!IsValidElfFile(fhelper.fd())) {
123 LOG(ERROR) << "problems reading ELF from in " << apk_path << " entry '"
124 << entry_name << "'";
128 // Elf found: add EmbeddedElf to vector, update cache.
129 return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, entry_name, zentry.offset,
130 zentry.uncompressed_length));
133 std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByName(const std::string& apk_path,
134 const std::string& elf_filename) {
135 if (!IsValidApkPath(apk_path)) {
138 FileHelper fhelper(apk_path.c_str());
142 ArchiveHelper ahelper(fhelper.fd(), apk_path);
143 if (!ahelper.valid()) {
146 ZipArchiveHandle& handle = ahelper.archive_handle();
148 int32_t rc = FindEntry(handle, ZipString(elf_filename.c_str()), &zentry);
150 LOG(ERROR) << "failed to find " << elf_filename << " in " << apk_path
151 << ": " << ErrorCodeString(rc);
154 if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) {
155 LOG(ERROR) << "shared library " << elf_filename << " in " << apk_path << " is compressed";
158 return std::unique_ptr<EmbeddedElf>(new EmbeddedElf(apk_path, elf_filename, zentry.offset,
159 zentry.uncompressed_length));
162 bool IsValidApkPath(const std::string& apk_path) {
163 static const char zip_preamble[] = {0x50, 0x4b, 0x03, 0x04 };
164 if (!IsRegularFile(apk_path)) {
167 std::string mode = std::string("rb") + CLOSE_ON_EXEC_MODE;
168 FILE* fp = fopen(apk_path.c_str(), mode.c_str());
173 if (fread(buf, 4, 1, fp) != 1) {
178 return memcmp(buf, zip_preamble, 4) == 0;
181 // Refer file in apk in compliance with http://developer.android.com/reference/java/net/JarURLConnection.html.
182 std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename) {
183 return apk_path + "!/" + elf_filename;
186 std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path) {
187 size_t pos = path.find("!/");
188 if (pos == std::string::npos) {
189 return std::make_tuple(false, "", "");
191 return std::make_tuple(true, path.substr(0, pos), path.substr(pos + 2));
194 bool GetBuildIdFromApkFile(const std::string& apk_path, const std::string& elf_filename,
196 std::unique_ptr<EmbeddedElf> ee = ApkInspector::FindElfInApkByName(apk_path, elf_filename);
200 return GetBuildIdFromEmbeddedElfFile(apk_path, ee->entry_offset(), ee->entry_size(), build_id);
203 bool ParseSymbolsFromApkFile(const std::string& apk_path, const std::string& elf_filename,
204 const BuildId& expected_build_id,
205 std::function<void(const ElfFileSymbol&)> callback) {
206 std::unique_ptr<EmbeddedElf> ee = ApkInspector::FindElfInApkByName(apk_path, elf_filename);
210 return ParseSymbolsFromEmbeddedElfFile(apk_path, ee->entry_offset(), ee->entry_size(),
211 expected_build_id, callback);