Introduce pet-projects dir
[lcapit-junk-code.git] / jos / kernel-stab-dump.c
blobbfdb4853f68c108616c2f45f22bf6acda93f54ef
1 /*
2 * stab-dump: Dump JOS kernel symbol table
3 *
4 * Stab format info at:
5 * http://sources.redhat.com/gdb/onlinedocs/stabs_toc.html
6 *
7 * Luiz Fernando N. Capitulino
8 * <lcapitulino@gmail.com
9 */
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/mman.h>
20 #include <elf.h>
22 struct Stab {
23 uint32_t n_strx; // index into string table of name
24 uint8_t n_type; // type of symbol
25 uint8_t n_other; // misc info (usually empty)
26 uint16_t n_desc; // description field
27 uintptr_t n_value; // value of symbol
30 int main(int argc, char *argv[])
32 int err, fd;
33 char *stabstr;
34 struct stat buf;
35 Elf32_Ehdr *header;
36 Elf32_Shdr *sh, *esh;
37 struct Stab *stab, *estab;
39 if (argc != 2) {
40 printf("usage: %s < file >\n", argv[0]);
41 exit(1);
44 fd = open(argv[1], O_RDONLY);
45 if (fd < 0) {
46 perror("open()");
47 exit(1);
50 memset(&buf, 0, sizeof(buf));
51 err = fstat(fd, &buf);
52 if (err) {
53 perror("fstat()");
54 exit(1);
57 header = (Elf32_Ehdr *) mmap(NULL, buf.st_size, PROT_READ,
58 MAP_PRIVATE, fd, 0);
59 if (header == MAP_FAILED) {
60 perror("mmap()");
61 exit(1);
63 close(fd);
66 * Get .stabstr and .stab
68 * XXX: The code makes the following assumptions:
70 * 1 .stabstr is the first SHT_STRTAB section
71 * 2 .stab section is the section that comes before .stabstr
73 * This looks like hardcoded info for me, since we're couting
74 * on the format of our kernel binary image. But I don't know
75 * how to make this right.
77 sh = (Elf32_Shdr *) ((void *) header + header->e_shoff);
78 esh = sh + header->e_shnum;
79 for (; sh < esh; sh++)
80 if (sh->sh_type == SHT_STRTAB) {
81 stabstr = (char *) ((void *) header + sh->sh_offset);
82 --sh;
83 break;
86 stab = (struct Stab *) ((void *) header + sh->sh_offset);
87 estab = stab + sh->sh_size / sh->sh_entsize;
88 for (; stab < estab; stab++)
89 if (stab->n_type == 0x24) {
90 char *p;
92 printf("%#x ", stab->n_value);
94 /* Stab format for functions is:
96 * func_name:F(x,y)
98 * But we're only interested in func_name
100 for (p = stab->n_strx + stabstr; *p != ':'; p++)
101 putchar(*p);
102 putchar('\n');
105 munmap(header, buf.st_size);
106 return 0;