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 <inttypes.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <unwindstack/Elf.h>
27 #include <unwindstack/Log.h>
28 #include <unwindstack/Memory.h>
29 
main(int argc,char ** argv)30 int main(int argc, char** argv) {
31   if (argc != 2 && argc != 3) {
32     printf("Usage: unwind_symbols <ELF_FILE> [<FUNC_ADDRESS>]\n");
33     printf("  Dump all function symbols in ELF_FILE. If FUNC_ADDRESS is\n");
34     printf("  specified, then get the function at that address.\n");
35     printf("  FUNC_ADDRESS must be a hex number.\n");
36     return 1;
37   }
38 
39   struct stat st;
40   if (stat(argv[1], &st) == -1) {
41     printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
42     return 1;
43   }
44   if (!S_ISREG(st.st_mode)) {
45     printf("%s is not a regular file.\n", argv[1]);
46     return 1;
47   }
48 
49   uint64_t func_addr;
50   if (argc == 3) {
51     char* name;
52     func_addr = strtoull(argv[2], &name, 16);
53     if (*name != '\0') {
54       printf("%s is not a hex number.\n", argv[2]);
55       return 1;
56     }
57   }
58 
59   // Send all log messages to stdout.
60   unwindstack::log_to_stdout(true);
61 
62   unwindstack::Elf elf(unwindstack::Memory::CreateFileMemory(argv[1], 0).release());
63   if (!elf.Init() || !elf.valid()) {
64     printf("%s is not a valid elf file.\n", argv[1]);
65     return 1;
66   }
67 
68   std::string soname(elf.GetSoname());
69   if (!soname.empty()) {
70     printf("Soname: %s\n\n", soname.c_str());
71   }
72 
73   switch (elf.machine_type()) {
74     case EM_ARM:
75       printf("ABI: arm\n");
76       break;
77     case EM_AARCH64:
78       printf("ABI: arm64\n");
79       break;
80     case EM_386:
81       printf("ABI: x86\n");
82       break;
83     case EM_X86_64:
84       printf("ABI: x86_64\n");
85       break;
86     default:
87       printf("ABI: unknown\n");
88       return 1;
89   }
90 
91   std::string name;
92   if (argc == 3) {
93     unwindstack::SharedString cur_name;
94     uint64_t func_offset;
95     if (!elf.GetFunctionName(func_addr, &cur_name, &func_offset)) {
96       printf("No known function at 0x%" PRIx64 "\n", func_addr);
97       return 1;
98     }
99     printf("<0x%" PRIx64 ">", func_addr - func_offset);
100     if (func_offset != 0) {
101       printf("+%" PRId64, func_offset);
102     }
103     printf(": %s\n", cur_name.c_str());
104     return 0;
105   }
106 
107   // This is a crude way to get the symbols in order.
108   for (const auto& entry : elf.interface()->pt_loads()) {
109     uint64_t start = entry.second.offset;
110     uint64_t end = entry.second.table_size;
111     for (uint64_t addr = start; addr < end; addr += 4) {
112       unwindstack::SharedString cur_name;
113       uint64_t func_offset;
114       if (elf.GetFunctionName(addr, &cur_name, &func_offset)) {
115         if (cur_name != name) {
116           printf("<0x%" PRIx64 "> Function: %s\n", addr - func_offset, cur_name.c_str());
117         }
118         name = cur_name;
119       }
120     }
121   }
122 
123   return 0;
124 }
125