Home | History | Annotate | Download | only in vtable-dumper
      1 /*
      2  * Copyright (C) 2017 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 "elf_handling.h"
     18 
     19 #include <llvm/Support/CommandLine.h>
     20 
     21 using llvm::Expected;
     22 using llvm::StringMapEntry;
     23 using llvm::cl::Hidden;
     24 using llvm::cl::Option;
     25 using llvm::cl::OptionCategory;
     26 using llvm::cl::ParseCommandLineOptions;
     27 using llvm::cl::Positional;
     28 using llvm::cl::Required;
     29 using llvm::cl::SetVersionPrinter;
     30 using llvm::cl::cat;
     31 using llvm::cl::desc;
     32 using llvm::cl::getRegisteredOptions;
     33 using llvm::cl::opt;
     34 using llvm::dyn_cast;
     35 using llvm::object::ObjectFile;
     36 using llvm::object::OwningBinary;
     37 using llvm::outs;
     38 
     39 OptionCategory VTableDumperCategory("vndk-vtable-dumper options");
     40 
     41 opt<std::string> FilePath(
     42         Positional, Required, cat(VTableDumperCategory),
     43         desc("shared_library.so"));
     44 
     45 opt<bool> Mangled(
     46         "mangled", cat(VTableDumperCategory),
     47         desc("Show mangled symbol names"));
     48 
     49 static void HideIrrelevantCommandLineOptions() {
     50     for (StringMapEntry<Option *> &P : getRegisteredOptions()) {
     51         if (P.second->Category == &VTableDumperCategory) {
     52             continue;
     53         }
     54         if (P.first().startswith("help")) {
     55             continue;
     56         }
     57         P.second->setHiddenFlag(Hidden);
     58     }
     59 }
     60 
     61 static void PrintCommandVersion() {
     62     outs() << "vndk-vtable-dumper 0.1\n";
     63 }
     64 
     65 int main(int argc, char **argv) {
     66     // Parse command line options.
     67     HideIrrelevantCommandLineOptions();
     68     SetVersionPrinter(PrintCommandVersion);
     69     ParseCommandLineOptions(argc, argv);
     70 
     71     // Load ELF shared object file and print the vtables.
     72     Expected<OwningBinary<ObjectFile>> Binary =
     73             ObjectFile::createObjectFile(FilePath);
     74     if (!Binary) {
     75         outs() << "Couldn't create object File \n";
     76         return 1;
     77     }
     78     ObjectFile *Objfile = dyn_cast<ObjectFile>(&(*Binary.get().getBinary()));
     79     if (!Objfile) {
     80         return 1;
     81     }
     82     auto SoFile = SharedObject::create(Objfile);
     83     if (!SoFile) {
     84         outs() << "Couldn't create ELFObjectFile \n";
     85         return 1;
     86     }
     87     SoFile->printVTables(Mangled);
     88     return 0;
     89 }
     90