Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /* Generate assembler source containing symbol information * * Copyright 2002 by Kai Germaschewski * * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * * Usage: nm -n vmlinux | scripts/kallsyms > symbols.S */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct sym_entry { unsigned long long addr; char type; char *sym; }; static struct sym_entry *table; static int size, cnt; static unsigned long long _stext, _etext, _sinittext, _einittext; static void usage(void) { fprintf(stderr, "Usage: kallsyms < in.map > out.S\n"); exit(1); } static int read_symbol(FILE *in, struct sym_entry *s) { char str[500]; int rc; rc = fscanf(in, "%llx %c %499s\n", &s->addr, &s->type, str); if (rc != 3) { if (rc != EOF) { /* skip line */ fgets(str, 500, in); } return -1; } s->sym = strdup(str); return 0; } static int symbol_valid(struct sym_entry *s) { if ((s->addr < _stext || s->addr > _etext) && (s->addr < _sinittext || s->addr > _einittext)) return 0; if (strstr(s->sym, "_compiled.")) return 0; return 1; } static void read_map(FILE *in) { int i; while (!feof(in)) { if (cnt >= size) { size += 10000; table = realloc(table, sizeof(*table) * size); if (!table) { fprintf(stderr, "out of memory\n"); exit (1); } } if (read_symbol(in, &table[cnt]) == 0) cnt++; } for (i = 0; i < cnt; i++) { if (strcmp(table[i].sym, "_stext") == 0) _stext = table[i].addr; if (strcmp(table[i].sym, "_etext") == 0) _etext = table[i].addr; if (strcmp(table[i].sym, "_sinittext") == 0) _sinittext = table[i].addr; if (strcmp(table[i].sym, "_einittext") == 0) _einittext = table[i].addr; } } static void write_src(void) { unsigned long long last_addr; int i, valid = 0; char *prev; printf("#include <asm/types.h>\n"); printf("#if BITS_PER_LONG == 64\n"); printf("#define PTR .quad\n"); printf("#define ALGN .align 8\n"); printf("#else\n"); printf("#define PTR .long\n"); printf("#define ALGN .align 4\n"); printf("#endif\n"); printf(".data\n"); printf(".globl kallsyms_addresses\n"); printf("\tALGN\n"); printf("kallsyms_addresses:\n"); for (i = 0, last_addr = 0; i < cnt; i++) { if (!symbol_valid(&table[i])) continue; if (table[i].addr == last_addr) continue; printf("\tPTR\t%#llx\n", table[i].addr); valid++; last_addr = table[i].addr; } printf("\n"); printf(".globl kallsyms_num_syms\n"); printf("\tALGN\n"); printf("kallsyms_num_syms:\n"); printf("\tPTR\t%d\n", valid); printf("\n"); printf(".globl kallsyms_names\n"); printf("\tALGN\n"); printf("kallsyms_names:\n"); prev = ""; for (i = 0, last_addr = 0; i < cnt; i++) { int k; if (!symbol_valid(&table[i])) continue; if (table[i].addr == last_addr) continue; for (k = 0; table[i].sym[k] && table[i].sym[k] == prev[k]; ++k) ; printf("\t.byte 0x%02x\n\t.asciz\t\"%s\"\n", k, table[i].sym + k); last_addr = table[i].addr; prev = table[i].sym; } printf("\n"); } int main(int argc, char **argv) { if (argc != 1) usage(); read_map(stdin); write_src(); return 0; } |