blob: c483c0fc47404b4152d9036a5e57e1d0433b6d3b [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
Elliott Hughesdfb74c52016-10-24 12:53:17 -07002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaobf8a2852016-05-27 11:59:09 -07003 *
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 Hughesdfb74c52016-10-24 12:53:17 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Josh Gaobf8a2852016-05-27 11:59:09 -07009 *
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 Chien9c123232018-10-23 10:47:17 +080019#include "SymbolFileParser.h"
20
Josh Gaobf8a2852016-05-27 11:59:09 -070021#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
37using namespace llvm;
38using namespace llvm::object;
39
40std::unordered_set<std::string> getSymbols(const std::string& filename) {
41 std::unordered_set<std::string> result;
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070042 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 Gaobf8a2852016-05-27 11:59:09 -070046 }
47
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070048 ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binaryOrError.get().getBinary());
Josh Gaobf8a2852016-05-27 11:59:09 -070049 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 Nainar079be162016-09-16 16:56:15 -070054 Expected<StringRef> symbolNameOrError = symbol.getName();
Josh Gaobf8a2852016-05-27 11:59:09 -070055
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070056 if (!symbolNameOrError) {
Josh Gaobf8a2852016-05-27 11:59:09 -070057 errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(),
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070058 llvm::toString(symbolNameOrError.takeError()).c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -070059 }
60
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070061 result.insert(symbolNameOrError.get().str());
Josh Gaobf8a2852016-05-27 11:59:09 -070062 }
63
64 return result;
65}
Logan Chien9c123232018-10-23 10:47:17 +080066
67static 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
105std::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}