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