Subversion Repositories HelenOS

Rev

Go to most recent revision | Details | 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;
51
    uint32_t *got;
52
    char *str_tab;
53
    int i;
54
 
55
    uint32_t sym_addr;
56
    module_t *dest;
57
 
58
    got = (uint32_t *) m->dyn.plt_got;
59
    sym_table = m->dyn.sym_tab;
60
    str_tab = m->dyn.str_tab;
61
    gotsym = m->dyn.arch.gotsym;
62
 
63
    DPRINTF("** Relocate GOT entries **\n");
64
    DPRINTF("MIPS base = 0x%x\n", m->dyn.arch.base);
65
 
66
    /*
67
     * Local entries.
68
     */
69
    for (i = 0; i < gotsym; i++) {
70
        /* FIXME: really subtract MIPS base? */
71
        got[i] += m->bias - m->dyn.arch.base;
72
    }
73
 
74
    DPRINTF("sym_ent = %d, gotsym = %d\n", m->dyn.arch.sym_no, gotsym);
75
 
76
    /*
77
     * Global entries.
78
     */
79
    for (i = gotsym; i < m->dyn.arch.sym_no; i++) {
80
        DPRINTF("relocate GOT entry %d\n", i);
81
//      getchar();
82
 
83
        sym = &sym_table[i];
84
        if (ELF32_R_TYPE(sym->st_info) == STT_FUNC) {
85
            if (sym->st_shndx == SHN_UNDEF) {
86
                if (sym->st_value == 0) {
87
                    /* 1 */
88
                } else {
89
                    if (got[i] == sym->st_value) {
90
                        /* 2 */
91
                        DPRINTF("(2)\n");
92
                        got[i] += m->bias - m->dyn.arch.base;
93
                        continue;
94
                    } else {
95
                        /* 3 */
96
                        DPRINTF("(3)\n");
97
                        got[i] = sym->st_value + m->bias - m->dyn.arch.base;
98
                        continue;
99
                    }
100
                }
101
            } else {
102
                /* 2 */
103
                DPRINTF("(2)\n");
104
                got[i] += m->bias - m->dyn.arch.base;
105
                continue;
106
            }
107
        } else {
108
            if (sym->st_shndx == SHN_UNDEF ||
109
                sym->st_shndx == SHN_COMMON) {
110
                /* 1 */
111
            } else {
112
                /* 1 */
113
            }
114
        }
115
 
116
        DPRINTF("(1) symbol name='%s'\n", str_tab + sym->st_name);
117
        sym_def = symbol_def_find(str_tab + sym->st_name, m, &dest);
118
        if (sym_def) {
119
            sym_addr = symbol_get_addr(sym_def, dest);
120
            DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
121
        } else {
122
            DPRINTF("symbol definition not found\n");
123
            continue;
124
        }
125
        DPRINTF("write 0x%x at 0x%x\n", sym_addr, (uint32_t) &got[i]);
126
//      getchar();
127
        got[i] = sym_addr;
128
    }
129
 
130
    DPRINTF("** Done **\n");
131
}
132
 
133
/**
134
 * Process (fixup) all relocations in a relocation table.
135
 */
136
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
137
{
138
    int i;
139
 
140
    size_t rt_entries;
141
    size_t r_offset;
142
    elf_word r_info;
143
    unsigned rel_type;
144
    elf_word sym_idx;
145
    uintptr_t sym_addr;
146
 
147
    elf_symbol_t *sym_table;
148
    elf_symbol_t *sym;
149
    uint32_t *r_ptr;
150
    uint16_t *r_ptr16;
151
    char *str_tab;
152
 
153
    elf_symbol_t *sym_def;
154
    module_t *dest;
155
 
156
    uint32_t *got;
157
    uint32_t gotsym;
158
    uint32_t ea;
159
 
160
    DPRINTF("parse relocation table\n");
161
 
162
    sym_table = m->dyn.sym_tab;
163
    rt_entries = rt_size / sizeof(elf_rela_t);
164
    str_tab = m->dyn.str_tab;
165
    got = (uint32_t *) m->dyn.plt_got;
166
    gotsym = m->dyn.arch.gotsym;
167
 
168
    DPRINTF("got=0x%lx, gotsym=%d\n", (uintptr_t) got, gotsym);
169
 
170
    DPRINTF("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries);
171
 
172
    for (i = 0; i < rt_entries; ++i) {
173
        DPRINTF("symbol %d: ", i);
174
        r_offset = rt[i].r_offset;
175
        r_info = rt[i].r_info;
176
 
177
        sym_idx = ELF32_R_SYM(r_info);
178
        sym = &sym_table[sym_idx];
179
 
180
        DPRINTF("name '%s', value 0x%x, size 0x%x\n",
181
            str_tab + sym->st_name,
182
            sym->st_value,
183
            sym->st_size);
184
 
185
        rel_type = ELF32_R_TYPE(r_info);
186
        r_ptr = (uint32_t *)(r_offset + m->bias);
187
        r_ptr16 = (uint16_t *)(r_offset + m->bias);
188
 
189
        if (sym->st_name != 0) {
190
            DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
191
            DPRINTF("dest name: '%s'\n", dest->dyn.soname);
192
            DPRINTF("dest bias: 0x%x\n", dest->bias);
193
            if (sym_def) {
194
                sym_addr = symbol_get_addr(sym_def, dest);
195
                DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
196
            } else {
197
                DPRINTF("symbol definition not found\n");
198
                continue;
199
            }
200
        }
201
 
202
        DPRINTF("switch(%u)\n", rel_type);
203
 
204
        switch (rel_type) {
205
        case R_MIPS_NONE:
206
            DPRINTF("Ignoring R_MIPS_NONE\n");
207
            break;
208
 
209
        case R_MIPS_REL32:
210
            DPRINTF("fixup R_MIPS_REL32 (r - ea + s)\n");
211
            if (sym_idx < gotsym)
212
                ea = sym_addr;
213
            else
214
                ea = got[sym_idx];
215
 
216
            *r_ptr += sym_addr - ea;
217
            DPRINTF("p = 0x%x, val := 0x%x\n", (uint32_t) r_ptr,
218
                *r_ptr);
219
//          getchar();
220
            break;
221
 
222
        /* No other non-TLS relocation types should appear. */
223
 
224
        case R_MIPS_TLS_DTPMOD32:
225
            /*
226
             * We can ignore this as long as the only module
227
             * with TLS variables is libc.so.
228
             */
229
            DPRINTF("Ignoring R_MIPS_DTPMOD32\n");
230
            break;
231
 
232
        default:
233
            printf("Error: Unknown relocation type %d.\n",
234
                rel_type);
235
            exit(1);
236
            break;
237
        }
238
    }
239
 
240
 
241
    printf("relocation done\n");
242
}
243
 
244
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
245
{
246
    /* Unused */
247
    (void)m; (void)rt; (void)rt_size;
248
}
249
 
250
/** @}
251
 */