Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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 | * |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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 | #include "SymbolDatabase.h" |
| 18 | |
| 19 | #include <err.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include <fstream> |
| 24 | #include <streambuf> |
| 25 | #include <string> |
| 26 | #include <unordered_set> |
| 27 | |
| 28 | #include <llvm/ADT/SmallVector.h> |
| 29 | #include <llvm/ADT/StringRef.h> |
| 30 | #include <llvm/Object/Binary.h> |
| 31 | #include <llvm/Object/ELFObjectFile.h> |
| 32 | |
| 33 | #include "versioner.h" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | using namespace llvm::object; |
| 37 | |
| 38 | std::unordered_set<std::string> getSymbols(const std::string& filename) { |
| 39 | std::unordered_set<std::string> result; |
| 40 | auto binary = createBinary(filename); |
| 41 | if (std::error_code ec = binary.getError()) { |
| 42 | errx(1, "failed to open library at %s: %s\n", filename.c_str(), ec.message().c_str()); |
| 43 | } |
| 44 | |
| 45 | ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binary.get().getBinary()); |
| 46 | if (!elf) { |
| 47 | errx(1, "failed to parse %s as ELF", filename.c_str()); |
| 48 | } |
| 49 | |
| 50 | for (const ELFSymbolRef symbol : elf->getDynamicSymbolIterators()) { |
| 51 | ErrorOr<StringRef> symbol_name = symbol.getName(); |
| 52 | |
| 53 | if (std::error_code ec = binary.getError()) { |
| 54 | errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(), |
| 55 | ec.message().c_str()); |
| 56 | } |
| 57 | |
| 58 | result.insert(symbol_name.get().str()); |
| 59 | } |
| 60 | |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | // The NDK platforms are built by copying the platform directories on top of |
| 65 | // each other to build each successive API version. Thus, we need to walk |
| 66 | // backwards to find each desired file. |
| 67 | static std::string readPlatformFile(const CompilationType& type, llvm::StringRef platform_dir, |
| 68 | const std::string& filename, bool required) { |
| 69 | int api_level = type.api_level; |
| 70 | std::ifstream stream; |
| 71 | while (api_level >= arch_min_api[type.arch]) { |
| 72 | if (supported_levels.count(api_level) == 0) { |
| 73 | --api_level; |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | std::string path = std::string(platform_dir) + "/android-" + std::to_string(api_level) + |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 78 | "/arch-" + to_string(type.arch) + "/symbols/" + filename; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 79 | |
| 80 | stream = std::ifstream(path); |
| 81 | if (stream) { |
| 82 | return std::string(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()); |
| 83 | } |
| 84 | |
| 85 | --api_level; |
| 86 | } |
| 87 | |
| 88 | if (required) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 89 | errx(1, "failed to find platform file '%s' for %s", filename.c_str(), to_string(type).c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return std::string(); |
| 93 | } |
| 94 | |
| 95 | static std::map<std::string, NdkSymbolType> parsePlatform(const CompilationType& type, |
| 96 | const std::string& platform_dir) { |
| 97 | std::map<std::string, NdkSymbolType> result; |
| 98 | std::map<std::string, bool /*required*/> wanted_files = { |
| 99 | { "libc.so.functions.txt", true }, |
| 100 | { "libc.so.variables.txt", false }, |
| 101 | { "libdl.so.functions.txt", false }, |
| 102 | { "libm.so.functions.txt", false }, |
| 103 | { "libm.so.variables.txt", false }, |
| 104 | }; |
| 105 | |
| 106 | for (const auto& pair : wanted_files) { |
| 107 | llvm::StringRef file = pair.first; |
| 108 | bool required = pair.second; |
| 109 | NdkSymbolType symbol_type; |
| 110 | if (file.endswith(".functions.txt")) { |
| 111 | symbol_type = NdkSymbolType::function; |
| 112 | } else if (file.endswith(".variables.txt")) { |
| 113 | symbol_type = NdkSymbolType::variable; |
| 114 | } else { |
| 115 | errx(1, "internal error: unexpected platform filename '%s'\n", file.str().c_str()); |
| 116 | } |
| 117 | |
| 118 | std::string platform_file = readPlatformFile(type, platform_dir, file, required); |
| 119 | if (platform_file.empty()) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | llvm::SmallVector<llvm::StringRef, 0> symbols; |
| 124 | llvm::StringRef(platform_file).split(symbols, "\n"); |
| 125 | |
| 126 | for (llvm::StringRef symbol_name : symbols) { |
| 127 | if (symbol_name.empty()) { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | if (result.count(symbol_name) != 0) { |
| 132 | if (verbose) { |
| 133 | printf("duplicated symbol '%s' in '%s'\n", symbol_name.str().c_str(), file.str().c_str()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | result[symbol_name] = symbol_type; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | NdkSymbolDatabase parsePlatforms(const std::set<CompilationType>& types, |
| 145 | const std::string& platform_dir) { |
| 146 | std::map<std::string, std::map<CompilationType, NdkSymbolType>> result; |
| 147 | for (const CompilationType& type : types) { |
| 148 | std::map<std::string, NdkSymbolType> symbols = parsePlatform(type, platform_dir); |
| 149 | for (const auto& it : symbols) { |
| 150 | result[it.first][type] = it.second; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return result; |
| 155 | } |