root / Whitix / branches / hybrid / kernel / symbols.c

Revision 538, 3.1 kB (checked in by mwhitworth, 3 months ago)

Create startup module.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 *
17 */
18
19#include <config.h>
20#include <console.h>
21#include <elf.h>
22#include <init.h>
23#include <sections.h>
24#include <symbols.h>
25
26#ifdef CONFIG_ALL_SYMBOLS
27
28#define KERNEL_BASE_ADDRESS             0x10000
29
30#define DATA __attribute__((section(".data")))
31
32static struct ElfSymbol* symTable DATA;
33static int symTableSize DATA;
34static char* strTable DATA;
35static int strTableSize DATA;
36
37const char* SymbolLookup(char* stringTable, struct ElfSymbol* symbolTable, int symSize, DWORD address, DWORD* size, DWORD* offset)
38{
39        int i=0;
40        struct ElfSymbol* currSym=NULL;
41        struct ElfSymbol* symbol;
42        DWORD currOffset=~0;
43
44        while (i < (symSize/sizeof(struct ElfSymbol)))
45        {
46                symbol=&symbolTable[i++];
47
48                if (ELF_ST_TYPE(symbol->symInfo) != STT_FUNC)
49                        continue;
50
51                if (address-symbol->symValue > currOffset)
52                        continue;
53
54                currOffset=address-symbol->symValue;
55
56                currSym=symbol;
57        }
58
59        if (!currSym)
60                return NULL;
61
62        *offset=currOffset;
63        *size=currSym->symSize;
64
65        if (*offset > *size)
66        {
67                KePrint("%#X-%#X = %#X %#X\n", address, symbol->symValue, *offset, *size);
68                while (1);
69        }
70
71        return stringTable+currSym->symName;
72}
73
74void SymbolPrint(DWORD address)
75{
76        DWORD size, offset;
77
78        const char* name=SymbolLookup(strTable, symTable, symTableSize, address, &size, &offset);
79
80        if (name)
81                KePrint("%s+%#X/%#X\n", name, offset, size);
82        else
83                KePrint("%#X\n", address);
84}
85
86void SymbolsCopy()
87{
88        struct ElfHeader* elfHeader=(struct ElfHeader*)KERNEL_BASE_ADDRESS;
89        struct ElfSectionHeader* sectionHeaders=(struct ElfSectionHeader*)(KERNEL_BASE_ADDRESS+elfHeader->shOffset);
90        int i;
91
92        /* Search the section headers for the symbol and string tables. */
93        for (i=1; i<elfHeader->shEntries; i++)
94        {
95                if (sectionHeaders[i].shType == SEC_TYPE_SYMTAB)
96                {
97                        symTable=(struct ElfSymbol*)(KERNEL_BASE_ADDRESS+sectionHeaders[i].shOffset);
98                        symTableSize=sectionHeaders[i].shSize;
99                }
100
101                if (sectionHeaders[i].shType == SEC_TYPE_STRTAB)
102                {
103                        strTable=(char*)(KERNEL_BASE_ADDRESS+sectionHeaders[i].shOffset);
104                        strTableSize=sectionHeaders[i].shSize;
105                }
106        }
107
108        /* Place the symbol and string tables after the kernel in memory. */
109        struct ElfSymbol* dest=(struct ElfSymbol*)end;
110
111        memcpy(dest, symTable, symTableSize);
112
113        char* strDest=(char*)((DWORD)end+symTableSize);
114
115        memcpy(strDest, strTable, strTableSize);
116
117        symTable=dest;
118        strTable=strDest;
119}
120
121#else
122/* Stub functions. */
123
124void SymbolPrint(DWORD address)
125{
126        KePrint("%10X\n", address);
127}
128
129void SymbolsCopy()
130{
131}
132
133#endif
Note: See TracBrowser for help on using the browser.