Home | History | Annotate | Download | only in src
      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 <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;
     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());
     44   }
     45 
     46   ELFObjectFileBase* elf = dyn_cast_or_null<ELFObjectFileBase>(binaryOrError.get().getBinary());
     47   if (!elf) {
     48     errx(1, "failed to parse %s as ELF", filename.c_str());
     49   }
     50 
     51   for (const ELFSymbolRef symbol : elf->getDynamicSymbolIterators()) {
     52     Expected<StringRef> symbolNameOrError = symbol.getName();
     53 
     54     if (!symbolNameOrError) {
     55       errx(1, "failed to get symbol name for symbol in %s: %s", filename.c_str(),
     56            llvm::toString(symbolNameOrError.takeError()).c_str());
     57     }
     58 
     59     result.insert(symbolNameOrError.get().str());
     60   }
     61 
     62   return result;
     63 }
     64