]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VtoyTool/vtoyksym.c
1.0.96 release
[Ventoy.git] / VtoyTool / vtoyksym.c
1 /******************************************************************************
2 * vtoyksym.c ---- ventoy ksym
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 static int verbose = 0;
28 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
29
30 int vtoyksym_main(int argc, char **argv)
31 {
32 int i;
33 unsigned long long addr1 = 0;
34 unsigned long long addr2 = 0;
35 char sym[256];
36 char line[1024];
37 const char *name = NULL;
38 FILE *fp;
39
40 for (i = 0; i < argc; i++)
41 {
42 if (argv[i][0] == '-' && argv[i][1] == 'p')
43 {
44 printf("%d", getpagesize());
45 return 0;
46 }
47 }
48
49 for (i = 0; i < argc; i++)
50 {
51 if (argv[i][0] == '-' && argv[i][1] == 'v')
52 {
53 verbose = 1;
54 break;
55 }
56 }
57
58 name = argv[2] ? argv[2] : "/proc/kallsyms";
59 fp = fopen(name, "r");
60 if (!fp)
61 {
62 fprintf(stderr, "Failed to open file %s err:%d\n", name, errno);
63 return 1;
64 }
65
66 debug("open %s success\n", name);
67
68 snprintf(sym, sizeof(sym), " %s", argv[1]);
69 debug("lookup for <%s>\n", sym);
70
71 while (fgets(line, sizeof(line), fp))
72 {
73 if (strstr(line, sym))
74 {
75 addr1 = strtoull(line, NULL, 16);
76 if (!fgets(line, sizeof(line), fp))
77 {
78 addr1 = 0;
79 fprintf(stderr, "Failed to read next line\n");
80 }
81 else
82 {
83 addr2 = strtoull(line, NULL, 16);
84 }
85
86 debug("addr1=<0x%llx> addr2=<0x%llx>\n", addr1, addr2);
87 break;
88 }
89 }
90
91 if (addr1 > addr2)
92 {
93 debug("Invalid addr range\n");
94 printf("0 0\n");
95 }
96 else
97 {
98 printf("0x%llx %llu\n", addr1, addr2 - addr1);
99 }
100
101 fclose(fp);
102
103 return 0;
104 }
105
106 // wrapper main
107 #ifndef BUILD_VTOY_TOOL
108 int main(int argc, char **argv)
109 {
110 return vtoyksym_main(argc, argv);
111 }
112 #endif
113