1 /* Generate assembler source containing symbol information
3 * Copyright 2002 by Kai Germaschewski
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
8 * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
17 unsigned long long addr;
23 static struct sym_entry *table;
25 static unsigned long long _stext, _etext, _sinittext, _einittext;
26 static int all_symbols = 0;
31 fprintf(stderr, "Usage: kallsyms [--all-symbols] < in.map > out.S\n");
36 read_symbol(FILE *in, struct sym_entry *s)
41 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &s->type, str);
50 /* Ignore most absolute/undefined (?) symbols. */
51 if (strcmp(str, "_stext") == 0)
53 else if (strcmp(str, "_etext") == 0)
55 else if (strcmp(str, "_sinittext") == 0)
57 else if (strcmp(str, "_einittext") == 0)
59 else if (toupper(s->type) == 'A' || toupper(s->type) == 'U')
67 symbol_valid(struct sym_entry *s)
70 if ((s->addr < _stext || s->addr > _etext)
71 && (s->addr < _sinittext || s->addr > _einittext))
75 if (strstr(s->sym, "_compiled."))
87 table = realloc(table, sizeof(*table) * size);
89 fprintf(stderr, "out of memory\n");
93 if (read_symbol(in, &table[cnt]) == 0)
104 printf("#include <asm/types.h>\n");
105 printf("#if BITS_PER_LONG == 64\n");
106 printf("#define PTR .quad\n");
107 printf("#define ALGN .align 8\n");
109 printf("#define PTR .long\n");
110 printf("#define ALGN .align 4\n");
115 printf(".globl kallsyms_addresses\n");
117 printf("kallsyms_addresses:\n");
118 for (i = 0; i < cnt; i++) {
119 if (!symbol_valid(&table[i]))
122 printf("\tPTR\t%#llx\n", table[i].addr);
127 printf(".globl kallsyms_num_syms\n");
129 printf("kallsyms_num_syms:\n");
130 printf("\tPTR\t%d\n", valid);
133 printf(".globl kallsyms_names\n");
135 printf("kallsyms_names:\n");
137 for (i = 0; i < cnt; i++) {
140 if (!symbol_valid(&table[i]))
143 for (k = 0; table[i].sym[k] && table[i].sym[k] == prev[k]; ++k)
146 printf("\t.byte 0x%02x\n\t.asciz\t\"%s\"\n", k, table[i].sym + k);
153 main(int argc, char **argv)
155 if (argc == 2 && strcmp(argv[1], "--all-symbols") == 0)