Subversion Repositories HelenOS

Rev

Rev 3970 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3772 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup rtld rtld
30
 * @brief
31
 * @{
32
 */
33
/**
34
 * @file
35
 */
36
 
37
#include <stdio.h>
38
#include <stdlib.h>
39
 
40
#include <arch.h>
41
#include <elf_dyn.h>
42
#include <symbol.h>
43
#include <rtld.h>
44
 
45
void module_process_pre_arch(module_t *m)
46
{
47
    elf_symbol_t *sym_table;
48
    elf_symbol_t *sym_def;
49
    elf_symbol_t *sym;
50
    uint32_t gotsym;
3970 svoboda 51
    uint32_t lgotno;
3772 svoboda 52
    uint32_t *got;
53
    char *str_tab;
3970 svoboda 54
    int i, j;
3772 svoboda 55
 
56
    uint32_t sym_addr;
57
    module_t *dest;
58
 
59
    got = (uint32_t *) m->dyn.plt_got;
60
    sym_table = m->dyn.sym_tab;
61
    str_tab = m->dyn.str_tab;
62
    gotsym = m->dyn.arch.gotsym;
3970 svoboda 63
    lgotno = m->dyn.arch.lgotno;
3772 svoboda 64
 
65
    DPRINTF("** Relocate GOT entries **\n");
66
    DPRINTF("MIPS base = 0x%x\n", m->dyn.arch.base);
67
 
68
    /*
69
     * Local entries.
70
     */
71
    for (i = 0; i < gotsym; i++) {
72
        /* FIXME: really subtract MIPS base? */
73
        got[i] += m->bias - m->dyn.arch.base;
74
    }
75
 
76
    DPRINTF("sym_ent = %d, gotsym = %d\n", m->dyn.arch.sym_no, gotsym);
3970 svoboda 77
    DPRINTF("lgotno = %d\n", lgotno);
3772 svoboda 78
 
79
    /*
3970 svoboda 80
     * Iterate over GOT-mapped symbol entries.
3772 svoboda 81
     */
3970 svoboda 82
    for (j = gotsym; j < m->dyn.arch.sym_no; j++) {
83
        /* Corresponding (global) GOT entry. */
84
        i = lgotno + j - gotsym;
85
 
3772 svoboda 86
        DPRINTF("relocate GOT entry %d\n", i);
87
//      getchar();
3970 svoboda 88
//      getchar();
3772 svoboda 89
 
3970 svoboda 90
        sym = &sym_table[j];
3772 svoboda 91
        if (ELF32_R_TYPE(sym->st_info) == STT_FUNC) {
92
            if (sym->st_shndx == SHN_UNDEF) {
93
                if (sym->st_value == 0) {
94
                    /* 1 */
95
                } else {
96
                    if (got[i] == sym->st_value) {
97
                        /* 2 */
98
                        DPRINTF("(2)\n");
99
                        got[i] += m->bias - m->dyn.arch.base;
100
                        continue;
101
                    } else {
102
                        /* 3 */
103
                        DPRINTF("(3)\n");
104
                        got[i] = sym->st_value + m->bias - m->dyn.arch.base;
105
                        continue;
106
                    }
107
                }
108
            } else {
109
                /* 2 */
110
                DPRINTF("(2)\n");
111
                got[i] += m->bias - m->dyn.arch.base;
112
                continue;
113
            }
114
        } else {
115
            if (sym->st_shndx == SHN_UNDEF ||
116
                sym->st_shndx == SHN_COMMON) {
117
                /* 1 */
118
            } else {
119
                /* 1 */
120
            }
121
        }
122
 
123
        DPRINTF("(1) symbol name='%s'\n", str_tab + sym->st_name);
124
        sym_def = symbol_def_find(str_tab + sym->st_name, m, &dest);
125
        if (sym_def) {
126
            sym_addr = symbol_get_addr(sym_def, dest);
127
            DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
128
        } else {
129
            DPRINTF("symbol definition not found\n");
130
            continue;
131
        }
132
        DPRINTF("write 0x%x at 0x%x\n", sym_addr, (uint32_t) &got[i]);
133
        got[i] = sym_addr;
134
    }
135
 
136
    DPRINTF("** Done **\n");
137
}
138
 
139
/**
140
 * Process (fixup) all relocations in a relocation table.
141
 */
142
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
143
{
144
    int i;
145
 
146
    size_t rt_entries;
147
    size_t r_offset;
148
    elf_word r_info;
149
    unsigned rel_type;
150
    elf_word sym_idx;
151
    uintptr_t sym_addr;
152
 
153
    elf_symbol_t *sym_table;
154
    elf_symbol_t *sym;
155
    uint32_t *r_ptr;
156
    uint16_t *r_ptr16;
157
    char *str_tab;
158
 
159
    elf_symbol_t *sym_def;
160
    module_t *dest;
161
 
162
    uint32_t *got;
163
    uint32_t gotsym;
3970 svoboda 164
    uint32_t lgotno;
3772 svoboda 165
    uint32_t ea;
166
 
167
    DPRINTF("parse relocation table\n");
168
 
169
    sym_table = m->dyn.sym_tab;
170
    rt_entries = rt_size / sizeof(elf_rela_t);
171
    str_tab = m->dyn.str_tab;
172
    got = (uint32_t *) m->dyn.plt_got;
173
    gotsym = m->dyn.arch.gotsym;
3970 svoboda 174
    lgotno = m->dyn.arch.lgotno;
3772 svoboda 175
 
176
    DPRINTF("got=0x%lx, gotsym=%d\n", (uintptr_t) got, gotsym);
177
 
178
    DPRINTF("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries);
179
 
180
    for (i = 0; i < rt_entries; ++i) {
181
        DPRINTF("symbol %d: ", i);
182
        r_offset = rt[i].r_offset;
183
        r_info = rt[i].r_info;
184
 
185
        sym_idx = ELF32_R_SYM(r_info);
186
        sym = &sym_table[sym_idx];
187
 
188
        DPRINTF("name '%s', value 0x%x, size 0x%x\n",
189
            str_tab + sym->st_name,
190
            sym->st_value,
191
            sym->st_size);
192
 
193
        rel_type = ELF32_R_TYPE(r_info);
194
        r_ptr = (uint32_t *)(r_offset + m->bias);
195
        r_ptr16 = (uint16_t *)(r_offset + m->bias);
196
 
197
        if (sym->st_name != 0) {
198
            DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
199
            DPRINTF("dest name: '%s'\n", dest->dyn.soname);
200
            DPRINTF("dest bias: 0x%x\n", dest->bias);
201
            if (sym_def) {
202
                sym_addr = symbol_get_addr(sym_def, dest);
203
                DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
204
            } else {
205
                DPRINTF("symbol definition not found\n");
206
                continue;
207
            }
208
        }
209
 
210
        DPRINTF("switch(%u)\n", rel_type);
211
 
212
        switch (rel_type) {
213
        case R_MIPS_NONE:
214
            DPRINTF("Ignoring R_MIPS_NONE\n");
215
            break;
216
 
217
        case R_MIPS_REL32:
218
            DPRINTF("fixup R_MIPS_REL32 (r - ea + s)\n");
219
            if (sym_idx < gotsym)
220
                ea = sym_addr;
221
            else
3970 svoboda 222
                ea = got[lgotno + sym_idx - gotsym];
3772 svoboda 223
 
224
            *r_ptr += sym_addr - ea;
225
            DPRINTF("p = 0x%x, val := 0x%x\n", (uint32_t) r_ptr,
226
                *r_ptr);
227
//          getchar();
228
            break;
229
 
230
        /* No other non-TLS relocation types should appear. */
231
 
232
        case R_MIPS_TLS_DTPMOD32:
233
            /*
234
             * We can ignore this as long as the only module
235
             * with TLS variables is libc.so.
236
             */
237
            DPRINTF("Ignoring R_MIPS_DTPMOD32\n");
238
            break;
239
 
240
        default:
241
            printf("Error: Unknown relocation type %d.\n",
242
                rel_type);
243
            exit(1);
244
            break;
245
        }
246
    }
247
 
248
 
249
    printf("relocation done\n");
250
}
251
 
252
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
253
{
254
    /* Unused */
255
    (void)m; (void)rt; (void)rt_size;
256
}
257
 
258
/** @}
259
 */