Subversion Repositories HelenOS

Rev

Rev 3688 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3688 Rev 3772
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 <arch.h>
40
#include <arch.h>
41
#include <elf_dyn.h>
41
#include <elf_dyn.h>
42
#include <symbol.h>
42
#include <symbol.h>
43
#include <rtld.h>
43
#include <rtld.h>
44
#include <smc.h>
44
#include <smc.h>
45
 
45
 
46
#define __L(ptr) ((uint32_t)(ptr) & 0x0000ffff)
46
#define __L(ptr) ((uint32_t)(ptr) & 0x0000ffff)
47
#define __HA(ptr) ((uint32_t)(ptr) >> 16)
47
#define __HA(ptr) ((uint32_t)(ptr) >> 16)
48
 
48
 
49
// ldis r11, .PLTtable@ha
49
// ldis r11, .PLTtable@ha
50
static inline uint32_t _ldis(unsigned rD, uint16_t imm16)
50
static inline uint32_t _ldis(unsigned rD, uint16_t imm16)
51
{
51
{
52
    /* Special case of addis: ldis rD,SIMM == addis rD,0,SIMM */
52
    /* Special case of addis: ldis rD,SIMM == addis rD,0,SIMM */
53
    return 0x3C000000 | (rD << 21) | imm16;
53
    return 0x3C000000 | (rD << 21) | imm16;
54
}
54
}
55
 
55
 
56
static inline uint32_t _lwz(unsigned rD, uint16_t disp16, unsigned rA)
56
static inline uint32_t _lwz(unsigned rD, uint16_t disp16, unsigned rA)
57
{
57
{
58
    return 0x80000000 | (rD << 21) | (rA << 16) | disp16;
58
    return 0x80000000 | (rD << 21) | (rA << 16) | disp16;
59
}
59
}
60
 
60
 
61
static inline uint32_t _mtctr(unsigned rS)
61
static inline uint32_t _mtctr(unsigned rS)
62
{
62
{
63
    /* mtctr rD == mtspr 9, rD */
63
    /* mtctr rD == mtspr 9, rD */
64
    return 0x7c0003a6 | (rS << 21) | (9/*CTR*/ << 16);
64
    return 0x7c0003a6 | (rS << 21) | (9/*CTR*/ << 16);
65
}
65
}
66
 
66
 
67
static inline uint32_t _bctr()
67
static inline uint32_t _bctr()
68
{
68
{
69
    /* bcctr 0x1f, 0 */
69
    /* bcctr 0x1f, 0 */
70
    return 0x4c000420 | (0x1f/*always*/ << 21);
70
    return 0x4c000420 | (0x1f/*always*/ << 21);
71
}
71
}
72
 
72
 
73
/* branch */
73
/* branch */
74
static inline uint32_t _b(uint32_t *addr, uint32_t *location)
74
static inline uint32_t _b(uint32_t *addr, uint32_t *location)
75
{
75
{
76
    uint32_t raddr = ((uint32_t)addr - (uint32_t)location) & 0x03fffffc;
76
    uint32_t raddr = ((uint32_t)addr - (uint32_t)location) & 0x03fffffc;
77
    return 0x48000000 | raddr;
77
    return 0x48000000 | raddr;
78
}
78
}
79
 
79
 
80
 
80
 
81
/*
81
/*
82
 * Fill in PLT
82
 * Fill in PLT
83
 */
83
 */
84
void module_process_pre_arch(module_t *m)
84
void module_process_pre_arch(module_t *m)
85
{
85
{
86
    uint32_t *plt;
86
    uint32_t *plt;
87
    uint32_t *_plt_ent;
87
    uint32_t *_plt_ent;
-
 
88
   
-
 
89
    /* No lazy linking -- no pre-processing yet. */
-
 
90
    return;
88
 
91
 
89
    plt = m->dyn.plt_got;
92
    plt = m->dyn.plt_got;
90
    if (!plt) {
93
    if (!plt) {
91
        /* Module has no PLT */
94
        /* Module has no PLT */
92
        return;
95
        return;
93
    }
96
    }
94
 
97
 
95
    // PLT entries start here. However, each occupies 2 words
98
    // PLT entries start here. However, each occupies 2 words
96
    _plt_ent = plt + 18;
99
    _plt_ent = plt + 18;
97
 
100
 
98
    // By definition of the ppc ABI, there's 1:1 correspondence
101
    // By definition of the ppc ABI, there's 1:1 correspondence
99
    // between JMPREL entries and PLT entries
102
    // between JMPREL entries and PLT entries
100
    unsigned plt_n = m->dyn.plt_rel_sz / sizeof(elf_rela_t);
103
    unsigned plt_n = m->dyn.plt_rel_sz / sizeof(elf_rela_t);
101
 
104
 
102
    uint32_t *_plt_table;
105
    uint32_t *_plt_table;
103
    uint32_t *_plt_call;
106
    uint32_t *_plt_call;
104
    uint32_t *_plt_resolve;
107
    uint32_t *_plt_resolve;
105
 
108
 
106
    _plt_resolve = plt;
109
    _plt_resolve = plt;
107
    _plt_call = plt + 6;
110
    _plt_call = plt + 6;
108
    _plt_table = plt + 18 + plt_n;
111
    _plt_table = plt + 18 + plt_n;
109
 
112
 
110
/* .PLTcall: */
113
/* .PLTcall: */
111
    plt[6] = _ldis(11, __HA(_plt_table));   // ldis r11, .PLTtable@ha
114
    plt[6] = _ldis(11, __HA(_plt_table));   // ldis r11, .PLTtable@ha
112
    plt[7] = _lwz(11, __L(_plt_table), 11); // lwz r11, .PLTtable@l(r11)
115
    plt[7] = _lwz(11, __L(_plt_table), 11); // lwz r11, .PLTtable@l(r11)
113
    plt[8] = _mtctr(11);            // mtctr r11
116
    plt[8] = _mtctr(11);            // mtctr r11
114
    plt[9] = _bctr();
117
    plt[9] = _bctr();
115
 
118
 
116
/* .PLTi, i = 0..N-1 */
119
/* .PLTi, i = 0..N-1 */
117
//  kputint(-4);
120
//  kputint(-4);
118
/*  for (i = 0; i < plt_n; ++i) {
121
/*  for (i = 0; i < plt_n; ++i) {
119
        //_plt_table[i] == function address;
122
        //_plt_table[i] == function address;
120
        plt[18+i] = _b(_plt_call, &plt[18+i]);  // b .PLTcall
123
        plt[18+i] = _b(_plt_call, &plt[18+i]);  // b .PLTcall
121
    }*/
124
    }*/
122
}
125
}
123
 
126
 
124
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
127
void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
125
{
128
{
126
    /* Unused */
129
    /* Unused */
127
    (void)m; (void)rt; (void)rt_size;
130
    (void)m; (void)rt; (void)rt_size;
128
}
131
}
129
 
132
 
130
/**
133
/**
131
 * Process (fixup) all relocations in a relocation table.
134
 * Process (fixup) all relocations in a relocation table.
132
 */
135
 */
133
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
136
void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
134
{
137
{
135
    int i;
138
    int i;
136
 
139
 
137
    size_t rt_entries;
140
    size_t rt_entries;
138
    size_t r_offset;
141
    size_t r_offset;
139
    elf_word r_info;
142
    elf_word r_info;
140
    unsigned rel_type;
143
    unsigned rel_type;
141
    elf_word sym_idx;
144
    elf_word sym_idx;
142
    uintptr_t sym_addr;
145
    uintptr_t sym_addr;
143
    uintptr_t r_addend;
146
    uintptr_t r_addend;
144
   
147
   
145
    elf_symbol_t *sym_table;
148
    elf_symbol_t *sym_table;
146
    elf_symbol_t *sym;
149
    elf_symbol_t *sym;
147
    uint32_t *r_ptr;
150
    uint32_t *r_ptr;
148
    uint16_t *r_ptr16;
151
    uint16_t *r_ptr16;
149
    char *str_tab;
152
    char *str_tab;
150
   
153
   
151
    elf_symbol_t *sym_def;
154
    elf_symbol_t *sym_def;
152
    module_t *dest;
155
    module_t *dest;
153
 
156
 
154
    uint32_t *plt;
157
    uint32_t *plt;
155
    uint32_t *_plt_table;
158
    uint32_t *_plt_table;
156
    uint32_t *_plt_ent;
159
    uint32_t *_plt_ent;
157
    uint32_t plt_n;
160
    uint32_t plt_n;
158
    uint32_t pidx;
161
    uint32_t pidx;
159
    uint32_t t_addr;
162
    uint32_t t_addr;
160
    uint32_t sym_size;
163
    uint32_t sym_size;
161
 
164
 
162
    plt = m->dyn.plt_got;
165
    plt = m->dyn.plt_got;
163
    plt_n = m->dyn.plt_rel_sz / sizeof(elf_rela_t);
166
    plt_n = m->dyn.plt_rel_sz / sizeof(elf_rela_t);
164
    _plt_ent = plt+ 18;
167
    _plt_ent = plt+ 18;
165
    _plt_table = plt + 18 + plt_n;
168
    _plt_table = plt + 18 + plt_n;
166
 
169
 
167
    DPRINTF("parse relocation table\n");
170
    DPRINTF("parse relocation table\n");
168
 
171
 
169
    sym_table = m->dyn.sym_tab;
172
    sym_table = m->dyn.sym_tab;
170
    rt_entries = rt_size / sizeof(elf_rela_t);
173
    rt_entries = rt_size / sizeof(elf_rela_t);
171
    str_tab = m->dyn.str_tab;
174
    str_tab = m->dyn.str_tab;
172
 
175
 
173
    DPRINTF("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries);
176
    DPRINTF("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries);
174
   
177
   
175
    for (i = 0; i < rt_entries; ++i) {
178
    for (i = 0; i < rt_entries; ++i) {
176
        DPRINTF("symbol %d: ", i);
179
        DPRINTF("symbol %d: ", i);
177
        r_offset = rt[i].r_offset;
180
        r_offset = rt[i].r_offset;
178
        r_info = rt[i].r_info;
181
        r_info = rt[i].r_info;
179
        r_addend = rt[i].r_addend;
182
        r_addend = rt[i].r_addend;
180
 
183
 
181
        sym_idx = ELF32_R_SYM(r_info);
184
        sym_idx = ELF32_R_SYM(r_info);
182
        sym = &sym_table[sym_idx];
185
        sym = &sym_table[sym_idx];
183
 
186
 
184
        DPRINTF("name '%s', value 0x%x, size 0x%x\n",
187
        DPRINTF("name '%s', value 0x%x, size 0x%x\n",
185
            str_tab + sym->st_name,
188
            str_tab + sym->st_name,
186
            sym->st_value,
189
            sym->st_value,
187
            sym->st_size);
190
            sym->st_size);
188
 
191
 
189
        rel_type = ELF32_R_TYPE(r_info);
192
        rel_type = ELF32_R_TYPE(r_info);
190
        r_ptr = (uint32_t *)(r_offset + m->bias);
193
        r_ptr = (uint32_t *)(r_offset + m->bias);
191
        r_ptr16 = (uint16_t *)(r_offset + m->bias);
194
        r_ptr16 = (uint16_t *)(r_offset + m->bias);
192
 
195
 
193
        if (sym->st_name != 0) {
196
        if (sym->st_name != 0) {
194
            DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
197
            DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
195
            sym_def = symbol_def_find(str_tab + sym->st_name,
198
            sym_def = symbol_def_find(str_tab + sym->st_name,
196
                m, &dest);
199
                m, &dest);
197
            DPRINTF("dest name: '%s'\n", dest->dyn.soname);
200
            DPRINTF("dest name: '%s'\n", dest->dyn.soname);
198
            DPRINTF("dest bias: 0x%x\n", dest->bias);
201
            DPRINTF("dest bias: 0x%x\n", dest->bias);
199
            if (sym_def) {
202
            if (sym_def) {
200
                sym_addr = symbol_get_addr(sym_def, dest);
203
                sym_addr = symbol_get_addr(sym_def, dest);
201
                DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
204
                DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
202
            } else {
205
            } else {
203
                DPRINTF("symbol definition not found\n");
206
                DPRINTF("symbol definition not found\n");
204
                continue;
207
                continue;
205
            }
208
            }
206
        }
209
        }
207
 
210
 
208
        switch (rel_type) {
211
        switch (rel_type) {
209
        case R_PPC_ADDR16_LO:
212
        case R_PPC_ADDR16_LO:
210
            DPRINTF("fixup R_PPC_ADDR16_LO (#lo(s+a))\n");
213
            DPRINTF("fixup R_PPC_ADDR16_LO (#lo(s+a))\n");
211
            *r_ptr16 = (sym_addr + r_addend) & 0xffff;
214
            *r_ptr16 = (sym_addr + r_addend) & 0xffff;
212
            break;
215
            break;
213
 
216
 
214
        case R_PPC_ADDR16_HI:
217
        case R_PPC_ADDR16_HI:
215
            DPRINTF("fixup R_PPC_ADDR16_HI (#hi(s+a))\n");
218
            DPRINTF("fixup R_PPC_ADDR16_HI (#hi(s+a))\n");
216
            *r_ptr16 = (sym_addr + r_addend) >> 16;
219
            *r_ptr16 = (sym_addr + r_addend) >> 16;
217
            break;
220
            break;
218
 
221
 
219
        case R_PPC_ADDR16_HA:
222
        case R_PPC_ADDR16_HA:
220
            DPRINTF("fixup R_PPC_ADDR16_HA (#ha(s+a))\n");
223
            DPRINTF("fixup R_PPC_ADDR16_HA (#ha(s+a))\n");
221
            t_addr = sym_addr + r_addend;
224
            t_addr = sym_addr + r_addend;
222
            *r_ptr16 = (t_addr >> 16) + ((t_addr & 0x8000) ? 1 : 0);
225
            *r_ptr16 = (t_addr >> 16) + ((t_addr & 0x8000) ? 1 : 0);
223
            break;
226
            break;
224
 
227
 
225
        case R_PPC_JMP_SLOT:
228
        case R_PPC_JMP_SLOT:
226
            DPRINTF("fixup R_PPC_JMP_SLOT (b+v)\n");
229
            DPRINTF("fixup R_PPC_JMP_SLOT (b+v)\n");
227
            pidx = (r_ptr - _plt_ent) / 2;
230
            pidx = (r_ptr - _plt_ent) / 2;
228
            if (pidx >= plt_n) {
231
            if (pidx >= plt_n) {
229
                DPRINTF("error: proc index out of range\n");
232
                DPRINTF("error: proc index out of range\n");
230
                exit(1);
233
                exit(1);
231
            }
234
            }
232
            plt[18+2*pidx] = _b((void *)sym_addr, &plt[18+2*pidx]);
235
            plt[18+2*pidx] = _b((void *)sym_addr, &plt[18+2*pidx]);
233
            break;
236
            break;
234
 
237
 
235
        case R_PPC_ADDR32:
238
        case R_PPC_ADDR32:
236
            DPRINTF("fixup R_PPC_ADDR32 (b+v+a)\n");
239
            DPRINTF("fixup R_PPC_ADDR32 (b+v+a)\n");
237
            *r_ptr = r_addend + sym_addr;
240
            *r_ptr = r_addend + sym_addr;
238
            break;
241
            break;
239
 
242
 
240
        case R_PPC_COPY:
243
        case R_PPC_COPY:
241
            /*
244
            /*
242
             * Copy symbol data from shared object to specified
245
             * Copy symbol data from shared object to specified
243
             * location.
246
             * location.
244
             */
247
             */
245
            DPRINTF("fixup R_PPC_COPY (s)\n");
248
            DPRINTF("fixup R_PPC_COPY (s)\n");
246
            sym_size = sym->st_size;
249
            sym_size = sym->st_size;
247
            if (sym_size != sym_def->st_size) {
250
            if (sym_size != sym_def->st_size) {
248
                printf("warning: mismatched symbol sizes\n");
251
                printf("warning: mismatched symbol sizes\n");
249
                /* Take the lower value. */
252
                /* Take the lower value. */
250
                if (sym_size > sym_def->st_size)
253
                if (sym_size > sym_def->st_size)
251
                    sym_size = sym_def->st_size;
254
                    sym_size = sym_def->st_size;
252
            }
255
            }
253
            memcpy(r_ptr, (const void *)sym_addr, sym_size);
256
            memcpy(r_ptr, (const void *)sym_addr, sym_size);
254
            break;
257
            break;
255
           
258
           
256
        case R_PPC_RELATIVE:
259
        case R_PPC_RELATIVE:
257
            DPRINTF("fixup R_PPC_RELATIVE (b+a)\n");
260
            DPRINTF("fixup R_PPC_RELATIVE (b+a)\n");
258
            *r_ptr = r_addend + m->bias;
261
            *r_ptr = r_addend + m->bias;
259
            break;
262
            break;
260
 
263
 
261
        case R_PPC_REL24:
264
        case R_PPC_REL24:
262
            DPRINTF("fixup R_PPC_REL24 (s+a-p)>>2\n");
265
            DPRINTF("fixup R_PPC_REL24 (s+a-p)>>2\n");
263
            *r_ptr = (sym_addr + r_addend - (uint32_t)r_ptr) >> 2;
266
            *r_ptr = (sym_addr + r_addend - (uint32_t)r_ptr) >> 2;
264
            break;
267
            break;
265
 
268
 
266
        case R_PPC_DTPMOD32:
269
        case R_PPC_DTPMOD32:
267
            /*
270
            /*
268
             * We can ignore this as long as the only module
271
             * We can ignore this as long as the only module
269
             * with TLS variables is libc.so.
272
             * with TLS variables is libc.so.
270
             */
273
             */
271
            DPRINTF("Ignoring R_PPC_DTPMOD32\n");
274
            DPRINTF("Ignoring R_PPC_DTPMOD32\n");
272
            break;
275
            break;
273
 
276
 
274
        default:
277
        default:
275
            printf("Error: Unknown relocation type %d.\n",
278
            printf("Error: Unknown relocation type %d.\n",
276
                rel_type);
279
                rel_type);
277
            exit(1);
280
            exit(1);
278
            break;
281
            break;
279
        }
282
        }
280
    }
283
    }
281
 
284
 
282
    /*
285
    /*
283
     * Synchronize the used portion of PLT. This is necessary since
286
     * Synchronize the used portion of PLT. This is necessary since
284
     * we are writing instructions.
287
     * we are writing instructions.
285
     */
288
     */
286
    smc_coherence(&plt[18], plt_n * 2 * sizeof(uint32_t));
289
    smc_coherence(&plt[18], plt_n * 2 * sizeof(uint32_t));
287
}
290
}
288
 
291
 
289
/** @}
292
/** @}
290
 */
293
 */
291
 
294