blob: 5b8ed5ab49daffdbf89035ac269120f228f3228c [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
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
35using namespace llvm;
36using namespace llvm::object;
37
38std::unordered_set<std::string> getSymbols(const std::string& filename) {
39 std::unordered_set<std::string> result;
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070040 auto binaryOrError = createBinary(filename);
41 if (!binaryOrError) {
42 errx(1, "failed to open library at %s: %s\n", filename.c_str(),
43 llvm::toString(binaryOrError.takeError()).c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -070044 }
45
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070046 ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binaryOrError.get().getBinary());
Josh Gaobf8a2852016-05-27 11:59:09 -070047 if (!elf) {
48 errx(1, "failed to parse %s as ELF", filename.c_str());
49 }
50
51 for (const ELFSymbolRef symbol : elf->getDynamicSymbolIterators()) {
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070052 Expected<StringRef> symbolNameOrError = symbol.getName();
Josh Gaobf8a2852016-05-27 11:59:09 -070053
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070054 if (!symbolNameOrError) {
Josh Gaobf8a2852016-05-27 11:59:09 -070055 errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(),
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070056 llvm::toString(symbolNameOrError.takeError()).c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -070057 }
58
Pirama Arumuga Nainar079be162016-09-16 16:56:15 -070059 result.insert(symbolNameOrError.get().str());
Josh Gaobf8a2852016-05-27 11:59:09 -070060 }
61
62 return result;
63}