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 | |
Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 19 | #include "SymbolFileParser.h" |
| 20 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 21 | #include <err.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include <fstream> |
| 26 | #include <streambuf> |
| 27 | #include <string> |
| 28 | #include <unordered_set> |
| 29 | |
| 30 | #include <llvm/ADT/SmallVector.h> |
| 31 | #include <llvm/ADT/StringRef.h> |
| 32 | #include <llvm/Object/Binary.h> |
| 33 | #include <llvm/Object/ELFObjectFile.h> |
| 34 | |
| 35 | #include "versioner.h" |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace llvm::object; |
| 39 | |
| 40 | std::unordered_set<std::string> getSymbols(const std::string& filename) { |
| 41 | std::unordered_set<std::string> result; |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 42 | auto binaryOrError = createBinary(filename); |
| 43 | if (!binaryOrError) { |
| 44 | errx(1, "failed to open library at %s: %s\n", filename.c_str(), |
| 45 | llvm::toString(binaryOrError.takeError()).c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 48 | ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binaryOrError.get().getBinary()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 49 | if (!elf) { |
| 50 | errx(1, "failed to parse %s as ELF", filename.c_str()); |
| 51 | } |
| 52 | |
| 53 | for (const ELFSymbolRef symbol : elf->getDynamicSymbolIterators()) { |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 54 | Expected<StringRef> symbolNameOrError = symbol.getName(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 55 | |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 56 | if (!symbolNameOrError) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 57 | errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(), |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 58 | llvm::toString(symbolNameOrError.takeError()).c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 61 | result.insert(symbolNameOrError.get().str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return result; |
| 65 | } |
Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 66 | |
| 67 | static std::map<std::string, NdkSymbolType> parsePlatform(const CompilationType& type, |
| 68 | const std::string& platform_dir) { |
| 69 | static const std::pair<const char*, bool> wanted_files[] = { |
| 70 | {"crtbegin.map.txt", false}, |
| 71 | {"libc.map.txt", true}, |
| 72 | }; |
| 73 | |
| 74 | std::map<std::string, NdkSymbolType> result; |
| 75 | |
| 76 | for (auto&& [filename, required] : wanted_files) { |
| 77 | std::string path = platform_dir + "/" + filename; |
| 78 | |
| 79 | std::optional<SymbolMap> symbols = parseSymbolFile(path, type); |
| 80 | if (!symbols) { |
| 81 | if (required) { |
| 82 | errx(1, "error: failed to load: %s", path.c_str()); |
| 83 | } |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | for (auto&& [symbol_name, symbol_type] : *symbols) { |
| 88 | if (symbol_name.empty()) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (result.count(symbol_name) != 0) { |
| 93 | if (strict) { |
| 94 | printf("duplicated symbol '%s' in '%s'\n", symbol_name.c_str(), path.c_str()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | result[symbol_name] = symbol_type; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | std::optional<NdkSymbolDatabase> parsePlatforms(const std::set<CompilationType>& types, |
| 106 | const std::string& platform_dir) { |
| 107 | NdkSymbolDatabase result; |
| 108 | for (const CompilationType& type : types) { |
| 109 | std::map<std::string, NdkSymbolType> symbols = parsePlatform(type, platform_dir); |
| 110 | for (const auto& it : symbols) { |
| 111 | result[it.first][type] = it.second; |
| 112 | } |
| 113 | } |
| 114 | return std::make_optional(std::move(result)); |
| 115 | } |