Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
| 16 | |
| 17 | #ifndef SIMPLE_PERF_READ_APK_H_ |
| 18 | #define SIMPLE_PERF_READ_APK_H_ |
| 19 | |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <string> |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 24 | #include <tuple> |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 26 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 27 | #include "read_elf.h" |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 28 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 29 | namespace simpleperf { |
| 30 | |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 31 | // Container for info an on ELF file embedded into an APK file |
| 32 | class EmbeddedElf { |
| 33 | public: |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 34 | EmbeddedElf() : entry_offset_(0), entry_size_(0) {} |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 35 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 36 | EmbeddedElf(const std::string& filepath, const std::string& entry_name, uint64_t entry_offset, |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 37 | size_t entry_size) |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 38 | : filepath_(filepath), |
| 39 | entry_name_(entry_name), |
| 40 | entry_offset_(entry_offset), |
| 41 | entry_size_(entry_size) {} |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 42 | |
| 43 | // Path to APK file |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 44 | const std::string& filepath() const { return filepath_; } |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 45 | |
| 46 | // Entry name within zip archive |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 47 | const std::string& entry_name() const { return entry_name_; } |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 48 | |
| 49 | // Offset of zip entry from start of containing APK file |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 50 | uint64_t entry_offset() const { return entry_offset_; } |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 51 | |
| 52 | // Size of zip entry (length of embedded ELF) |
| 53 | uint32_t entry_size() const { return entry_size_; } |
| 54 | |
| 55 | private: |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 56 | std::string filepath_; // containing APK path |
| 57 | std::string entry_name_; // name of entry in zip index of embedded elf file |
| 58 | uint64_t entry_offset_; // offset of ELF from start of containing APK file |
| 59 | uint32_t entry_size_; // size of ELF file in zip |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 62 | // APK inspector helper class |
| 63 | class ApkInspector { |
| 64 | public: |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 65 | static EmbeddedElf* FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset); |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 66 | static EmbeddedElf* FindElfInApkByName(const std::string& apk_path, |
| 67 | const std::string& entry_name); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 68 | |
| 69 | private: |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 70 | static std::unique_ptr<EmbeddedElf> FindElfInApkByOffsetWithoutCache(const std::string& apk_path, |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 71 | uint64_t file_offset); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 72 | static std::unique_ptr<EmbeddedElf> FindElfInApkByNameWithoutCache(const std::string& apk_path, |
| 73 | const std::string& entry_name); |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 74 | |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 75 | struct ApkNode { |
| 76 | // Map from entry_offset to EmbeddedElf. |
| 77 | std::unordered_map<uint64_t, std::unique_ptr<EmbeddedElf>> offset_map; |
| 78 | // Map from entry_name to EmbeddedElf. |
| 79 | std::unordered_map<std::string, EmbeddedElf*> name_map; |
| 80 | }; |
| 81 | static std::unordered_map<std::string, ApkNode> embedded_elf_cache_; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 84 | std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename); |
| 85 | std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path); |
| 86 | |
Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 87 | // Parse path like "[anon:dalvik-classes.dex extracted in memory from /..base.apk] (deleted)", |
| 88 | // or "/dev/ashmem/dalvik-classes.dex extracted in memory from /..base.apk (deleted)" on Android P. |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 89 | bool ParseExtractedInMemoryPath(const std::string& path, std::string* zip_path, |
| 90 | std::string* entry_name); |
| 91 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 92 | } // namespace simpleperf |
| 93 | |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 94 | #endif // SIMPLE_PERF_READ_APK_H_ |