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 unwindstack::Elf elf(unwindstack::Memory::CreateFileMemory(argv[1], 0).release());
60 if (!elf.Init() || !elf.valid()) {
61 printf("%s is not a valid elf file.\n", argv[1]);
62 return 1;
63 }
64
65 std::string soname(elf.GetSoname());
66 if (!soname.empty()) {
67 printf("Soname: %s\n\n", soname.c_str());
68 }
69
70 switch (elf.machine_type()) {
71 case EM_ARM:
72 printf("ABI: arm\n");
73 break;
74 case EM_AARCH64:
75 printf("ABI: arm64\n");
76 break;
77 case EM_386:
78 printf("ABI: x86\n");
79 break;
80 case EM_X86_64:
81 printf("ABI: x86_64\n");
82 break;
83 default:
84 printf("ABI: unknown\n");
85 return 1;
86 }
87
88 std::string name;
89 if (argc == 3) {
90 unwindstack::SharedString cur_name;
91 uint64_t func_offset;
92 if (!elf.GetFunctionName(func_addr, &cur_name, &func_offset)) {
93 printf("No known function at 0x%" PRIx64 "\n", func_addr);
94 return 1;
95 }
96 printf("<0x%" PRIx64 ">", func_addr - func_offset);
97 if (func_offset != 0) {
98 printf("+%" PRId64, func_offset);
99 }
100 printf(": %s\n", cur_name.c_str());
101 return 0;
102 }
103
104 // This is a crude way to get the symbols in order.
105 for (const auto& entry : elf.interface()->pt_loads()) {
106 uint64_t start = entry.second.offset;
107 uint64_t end = entry.second.table_size;
108 for (uint64_t addr = start; addr < end; addr += 4) {
109 unwindstack::SharedString cur_name;
110 uint64_t func_offset;
111 if (elf.GetFunctionName(addr, &cur_name, &func_offset)) {
112 if (cur_name != name) {
113 printf("<0x%" PRIx64 "> Function: %s\n", addr - func_offset, cur_name.c_str());
114 }
115 name = cur_name;
116 }
117 }
118 }
119
120 return 0;
121 }
122