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 <elf.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 #include "ArmExidx.h"
29 #include "Elf.h"
30 #include "ElfInterface.h"
31 #include "ElfInterfaceArm.h"
32 #include "Log.h"
33 
DumpArm(ElfInterfaceArm * interface)34 void DumpArm(ElfInterfaceArm* interface) {
35   if (interface == nullptr) {
36     printf("No ARM Unwind Information.\n\n");
37     return;
38   }
39 
40   printf("ARM Unwind Information:\n");
41   for (const auto& entry : interface->pt_loads()) {
42     uint64_t load_bias = entry.second.table_offset;
43     printf(" PC Range 0x%" PRIx64 " - 0x%" PRIx64 "\n", entry.second.offset + load_bias,
44            entry.second.table_size + load_bias);
45     for (auto addr : *interface) {
46       std::string name;
47       printf("  PC 0x%" PRIx64, addr + load_bias);
48       uint64_t func_offset;
49       if (interface->GetFunctionName(addr + load_bias + 1, &name, &func_offset) && !name.empty()) {
50         printf(" <%s>", name.c_str());
51       }
52       printf("\n");
53       uint64_t entry;
54       if (!interface->FindEntry(addr + load_bias, &entry)) {
55         printf("    Cannot find entry for address.\n");
56         continue;
57       }
58       ArmExidx arm(nullptr, interface->memory(), nullptr);
59       arm.set_log(true);
60       arm.set_log_skip_execution(true);
61       arm.set_log_indent(2);
62       if (!arm.ExtractEntryData(entry)) {
63         if (arm.status() != ARM_STATUS_NO_UNWIND) {
64           printf("    Error trying to extract data.\n");
65         }
66         continue;
67       }
68       if (arm.data()->size() > 0) {
69         if (!arm.Eval() && arm.status() != ARM_STATUS_NO_UNWIND) {
70           printf("      Error trying to evaluate dwarf data.\n");
71         }
72       }
73     }
74   }
75   printf("\n");
76 }
77 
main(int argc,char ** argv)78 int main(int argc, char** argv) {
79   if (argc != 2) {
80     printf("Need to pass the name of an elf file to the program.\n");
81     return 1;
82   }
83 
84   struct stat st;
85   if (stat(argv[1], &st) == -1) {
86     printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
87     return 1;
88   }
89   if (!S_ISREG(st.st_mode)) {
90     printf("%s is not a regular file.\n", argv[1]);
91     return 1;
92   }
93   if (S_ISDIR(st.st_mode)) {
94     printf("%s is a directory.\n", argv[1]);
95     return 1;
96   }
97 
98   // Send all log messages to stdout.
99   log_to_stdout(true);
100 
101   MemoryFileAtOffset* memory = new MemoryFileAtOffset;
102   if (!memory->Init(argv[1], 0)) {
103     // Initializatation failed.
104     printf("Failed to init\n");
105     return 1;
106   }
107 
108   Elf elf(memory);
109   if (!elf.Init() || !elf.valid()) {
110     printf("%s is not a valid elf file.\n", argv[1]);
111     return 1;
112   }
113 
114   ElfInterface* interface = elf.interface();
115   if (elf.machine_type() == EM_ARM) {
116     DumpArm(reinterpret_cast<ElfInterfaceArm*>(interface));
117     printf("\n");
118   }
119 
120   return 0;
121 }
122