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; |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 40 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 46 | ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binaryOrError.get().getBinary()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 47 | 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 Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 52 | Expected<StringRef> symbolNameOrError = symbol.getName(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 53 | |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 54 | if (!symbolNameOrError) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 55 | 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] | 56 | llvm::toString(symbolNameOrError.takeError()).c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Pirama Arumuga Nainar | 079be16 | 2016-09-16 16:56:15 -0700 | [diff] [blame] | 59 | result.insert(symbolNameOrError.get().str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return result; |
| 63 | } |