Subversion Repositories HelenOS

Rev

Rev 4354 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4354 Rev 4372
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup rtld rtld
29
/** @addtogroup rtld rtld
30
 * @brief
30
 * @brief
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 */
35
 */
36
 
36
 
37
#include <stdio.h>
37
#include <stdio.h>
38
#include <stdlib.h>
38
#include <stdlib.h>
39
 
39
 
40
#include <elf_dyn.h>
40
#include <elf_dyn.h>
41
#include <symbol.h>
41
#include <symbol.h>
42
#include <rtld.h>
42
#include <rtld.h>
43
 
43
 
44
#include <rtld_arch.h>
44
#include <rtld_arch.h>
45
 
45
 
46
void module_process_pre_arch(module_t *m)
46
void module_process_pre_arch(module_t *m)
47
{
47
{
48
    /* Unused */
48
    /* Unused */
49
}
49
}
50
 
50
 
51
 
51
 
52
/**
52
/**
53
 * Process (fixup) all relocations in a relocation table.
53
 * Process (fixup) all relocations in a relocation table.
54
 */
54
 */
55
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
55
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
56
{
56
{
57
    int i;
57
    int i;
58
 
58
 
59
    size_t rt_entries;
59
    size_t rt_entries;
60
    size_t r_offset;
60
    size_t r_offset;
61
    elf_word r_info;
61
    elf_word r_info;
62
    unsigned rel_type;
62
    unsigned rel_type;
63
    elf_word sym_idx;
63
    elf_word sym_idx;
64
    uintptr_t sym_addr;
64
    uintptr_t sym_addr;
65
   
65
   
66
    elf_symbol_t *sym_table;
66
    elf_symbol_t *sym_table;
67
    elf_symbol_t *sym;
67
    elf_symbol_t *sym;
68
    uint32_t *r_ptr;
68
    uint32_t *r_ptr;
69
    uint32_t sym_size;
69
    uint32_t sym_size;
70
    char *str_tab;
70
    char *str_tab;
71
   
71
   
72
    elf_symbol_t *sym_def;
72
    elf_symbol_t *sym_def;
73
    module_t *dest;
73
    module_t *dest;
74
 
74
 
75
    DPRINTF("parse relocation table\n");
75
    DPRINTF("parse relocation table\n");
76
 
76
 
77
    sym_table = m->dyn.sym_tab;
77
    sym_table = m->dyn.sym_tab;
78
    rt_entries = rt_size / sizeof(elf_rel_t);
78
    rt_entries = rt_size / sizeof(elf_rel_t);
79
    str_tab = m->dyn.str_tab;
79
    str_tab = m->dyn.str_tab;
80
 
80
 
81
    DPRINTF("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries);
81
    DPRINTF("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries);
82
   
82
   
83
    for (i = 0; i < rt_entries; ++i) {
83
    for (i = 0; i < rt_entries; ++i) {
84
//      DPRINTF("symbol %d: ", i);
84
//      DPRINTF("symbol %d: ", i);
85
        r_offset = rt[i].r_offset;
85
        r_offset = rt[i].r_offset;
86
        r_info = rt[i].r_info;
86
        r_info = rt[i].r_info;
87
 
87
 
88
        sym_idx = ELF32_R_SYM(r_info);
88
        sym_idx = ELF32_R_SYM(r_info);
89
        sym = &sym_table[sym_idx];
89
        sym = &sym_table[sym_idx];
90
 
90
 
91
/*      DPRINTF("name '%s', value 0x%x, size 0x%x\n",
91
/*      DPRINTF("name '%s', value 0x%x, size 0x%x\n",
92
            str_tab + sym->st_name,
92
            str_tab + sym->st_name,
93
            sym->st_value,
93
            sym->st_value,
94
            sym->st_size);
94
            sym->st_size);
95
*/
95
*/
96
        rel_type = ELF32_R_TYPE(r_info);
96
        rel_type = ELF32_R_TYPE(r_info);
97
        r_ptr = (uint32_t *)(r_offset + m->bias);
97
        r_ptr = (uint32_t *)(r_offset + m->bias);
98
 
98
 
99
        if (sym->st_name != 0) {
99
        if (sym->st_name != 0) {
100
//          DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
100
//          DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
101
            sym_def = symbol_def_find(str_tab + sym->st_name,
101
            sym_def = symbol_def_find(str_tab + sym->st_name,
102
                m, &dest);
102
                m, &dest);
103
//          DPRINTF("dest name: '%s'\n", dest->dyn.soname);
103
//          DPRINTF("dest name: '%s'\n", dest->dyn.soname);
104
//          DPRINTF("dest bias: 0x%x\n", dest->bias);
104
//          DPRINTF("dest bias: 0x%x\n", dest->bias);
105
            if (sym_def) {
105
            if (sym_def) {
106
                sym_addr = symbol_get_addr(sym_def, dest);
106
                sym_addr = symbol_get_addr(sym_def, dest);
107
//              DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
107
//              DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
108
            } else {
108
            } else {
109
                DPRINTF("symbol definition not found\n");
109
                printf("Definition of '%s' not found.\n",
-
 
110
                    str_tab + sym->st_name);
110
                continue;
111
                continue;
111
            }
112
            }
112
        }
113
        }
113
 
114
 
114
        switch (rel_type) {
115
        switch (rel_type) {
115
        case R_386_GLOB_DAT:
116
        case R_386_GLOB_DAT:
116
        case R_386_JUMP_SLOT:
117
        case R_386_JUMP_SLOT:
117
            DPRINTF("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n");
118
            DPRINTF("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n");
118
            *r_ptr = sym_addr;
119
            *r_ptr = sym_addr;
119
            break;
120
            break;
120
 
121
 
121
        case R_386_32:
122
        case R_386_32:
122
            DPRINTF("fixup R_386_32 (b+v+a)\n");
123
            DPRINTF("fixup R_386_32 (b+v+a)\n");
123
            *r_ptr += sym_addr;
124
            *r_ptr += sym_addr;
124
            break;
125
            break;
125
 
126
 
126
        case R_386_PC32:
127
        case R_386_PC32:
127
            DPRINTF("fixup R_386_PC32 (b+v+a-p)\n");
128
            DPRINTF("fixup R_386_PC32 (b+v+a-p)\n");
128
            *r_ptr += sym_addr - (uint32_t) r_ptr;
129
            *r_ptr += sym_addr - (uint32_t) r_ptr;
129
            break;
130
            break;
130
 
131
 
131
        case R_386_COPY:
132
        case R_386_COPY:
132
            /*
133
            /*
133
             * Copy symbol data from shared object to specified
134
             * Copy symbol data from shared object to specified
134
             * location.
135
             * location.
135
             */
136
             */
136
            DPRINTF("fixup R_386_COPY (s)\n");
137
            DPRINTF("fixup R_386_COPY (s)\n");
137
            sym_size = sym->st_size;
138
            sym_size = sym->st_size;
138
            if (sym_size != sym_def->st_size) {
139
            if (sym_size != sym_def->st_size) {
139
                printf("Warning: Mismatched symbol sizes.\n");
140
                printf("Warning: Mismatched symbol sizes.\n");
140
                /* Take the lower value. */
141
                /* Take the lower value. */
141
                if (sym_size > sym_def->st_size)
142
                if (sym_size > sym_def->st_size)
142
                    sym_size = sym_def->st_size;
143
                    sym_size = sym_def->st_size;
143
            }
144
            }
144
            memcpy(r_ptr, (const void *)sym_addr, sym_size);
145
            memcpy(r_ptr, (const void *)sym_addr, sym_size);
145
            break;
146
            break;
146
           
147
           
147
        case R_386_RELATIVE:
148
        case R_386_RELATIVE:
148
            DPRINTF("fixup R_386_RELATIVE (b+a)\n");
149
            DPRINTF("fixup R_386_RELATIVE (b+a)\n");
149
            *r_ptr += m->bias;
150
            *r_ptr += m->bias;
150
            break;
151
            break;
151
 
152
 
152
        case R_386_TLS_DTPMOD32:
153
        case R_386_TLS_DTPMOD32:
153
            /*
154
            /*
154
             * We can ignore this as long as the only module
155
             * We can ignore this as long as the only module
155
             * with TLS variables is libc.so.
156
             * with TLS variables is libc.so.
156
             */
157
             */
157
            DPRINTF("Ignoring R_386_TLS_DTPMOD32\n");
158
            DPRINTF("Ignoring R_386_TLS_DTPMOD32\n");
158
            break;
159
            break;
159
 
160
 
160
        default:
161
        default:
161
            printf("Error: Unknown relocation type %d\n",
162
            printf("Error: Unknown relocation type %d\n",
162
                rel_type);
163
                rel_type);
163
            exit(1);
164
            exit(1);
164
        }
165
        }
165
 
166
 
166
    }
167
    }
167
 
168
 
168
}
169
}
169
 
170
 
170
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
171
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
171
{
172
{
172
    /* Unused */
173
    /* Unused */
173
    (void)m; (void)rt; (void)rt_size;
174
    (void)m; (void)rt; (void)rt_size;
174
}
175
}
175
 
176
 
176
/** @}
177
/** @}
177
 */
178
 */
178
 
179