• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "SymbolDatabase.h"
18 
19 #include "SymbolFileParser.h"
20 
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 
getSymbols(const std::string & filename)40 std::unordered_set<std::string> getSymbols(const std::string& filename) {
41   std::unordered_set<std::string> result;
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());
46   }
47 
48   ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binaryOrError.get().getBinary());
49   if (!elf) {
50     errx(1, "failed to parse %s as ELF", filename.c_str());
51   }
52 
53   for (const ELFSymbolRef symbol : elf->getDynamicSymbolIterators()) {
54     Expected<StringRef> symbolNameOrError = symbol.getName();
55 
56     if (!symbolNameOrError) {
57       errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(),
58            llvm::toString(symbolNameOrError.takeError()).c_str());
59     }
60 
61     result.insert(symbolNameOrError.get().str());
62   }
63 
64   return result;
65 }
66 
parsePlatform(const CompilationType & type,const std::string & platform_dir)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 
parsePlatforms(const std::set<CompilationType> & types,const std::string & platform_dir)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 }
116