• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HideIrrelevantCommandLineOptions()45 static void HideIrrelevantCommandLineOptions() {
46     for (StringMapEntry<Option *> &P : getRegisteredOptions()) {
47         if (P.second->Category == &VTableDumperCategory) {
48             continue;
49         }
50         if (P.first().startswith("help")) {
51             continue;
52         }
53         P.second->setHiddenFlag(Hidden);
54     }
55 }
56 
PrintCommandVersion()57 static void PrintCommandVersion() {
58     outs() << "vndk-vtable-dumper 0.1\n";
59 }
60 
main(int argc,char ** argv)61 int main(int argc, char **argv) {
62     // Parse command line options.
63     HideIrrelevantCommandLineOptions();
64     SetVersionPrinter(PrintCommandVersion);
65     ParseCommandLineOptions(argc, argv);
66 
67     // Load ELF shared object file and print the vtables.
68     Expected<OwningBinary<ObjectFile>> Binary =
69             ObjectFile::createObjectFile(FilePath);
70     if (!Binary) {
71         outs() << "Couldn't create object File \n";
72         return 1;
73     }
74     ObjectFile *Objfile = dyn_cast<ObjectFile>(&(*Binary.get().getBinary()));
75     if (!Objfile) {
76         return 1;
77     }
78     auto SoFile = SharedObject::create(Objfile);
79     if (!SoFile) {
80         outs() << "Couldn't create ELFObjectFile \n";
81         return 1;
82     }
83     SoFile->printVTables();
84     return 0;
85 }
86