Subversion Repositories HelenOS

Rev

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

Rev 2981 Rev 2989
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 <elf_dyn.h>
37
#include <elf_dyn.h>
38
#include <rtld.h>
38
#include <rtld.h>
-
 
39
#include <pcb.h>
39
 
40
 
40
void __main(void);
41
void __main(void);
41
void __io_init(void);
42
void __io_init(void);
42
void __exit(void);
43
void __exit(void);
43
 
44
 
44
static void kputint(unsigned i)
45
static void kputint(unsigned i)
45
{
46
{
46
    unsigned dummy;
47
    unsigned dummy;
47
    asm volatile (
48
    asm volatile (
48
        "movl $30, %%eax;"
49
        "movl $30, %%eax;"
49
        "int $0x30"
50
        "int $0x30"
50
        : "=d" (dummy) /* output - %edx clobbered */
51
        : "=d" (dummy) /* output - %edx clobbered */
51
        : "d" (i) /* input */
52
        : "d" (i) /* input */
52
        : "%eax","%ecx" /* all scratch registers clobbered */
53
        : "%eax","%ecx" /* all scratch registers clobbered */
53
    ); 
54
    ); 
54
}
55
}
55
 
56
 
56
void __bootstrap(void)
57
void __bootstrap(void)
57
{
58
{
58
    unsigned bias;
59
    unsigned bias;
59
    unsigned *got;
60
    unsigned *got;
60
    elf_dyn_t *dynamic;
61
    elf_dyn_t *dynamic;
61
    void *dptr;
62
    void *dptr;
62
    unsigned dval;
63
    unsigned dval;
63
    int i;
64
    int i;
64
 
65
 
65
    size_t rel_entries;
66
    size_t rel_entries;
66
    size_t r_offset;
67
    size_t r_offset;
67
    elf_word r_info;
68
    elf_word r_info;
68
    unsigned rel_type;
69
    unsigned rel_type;
69
    elf_word sym_idx;
70
    elf_word sym_idx;
70
    uintptr_t sym_addr;
71
    uintptr_t sym_addr;
71
   
72
   
72
    elf_symbol_t *sym_table;
73
    elf_symbol_t *sym_table;
73
    elf_rel_t *rel_table;
74
    elf_rel_t *rel_table;
74
    elf_rel_t *jmp_rel_table;
75
    elf_rel_t *jmp_rel_table;
75
    size_t jmp_rel_entries;
76
    size_t jmp_rel_entries;
-
 
77
    pcb_t *pcb;
76
 
78
   
-
 
79
    pcb = (pcb_t *)PCB_ADDRESS;
-
 
80
 
-
 
81
    /* The program loader (iloader) kindly provided us with these */
-
 
82
    dynamic = pcb->rtld_dynamic;
-
 
83
    bias = pcb->rtld_bias;
-
 
84
/*
77
asm volatile (
85
asm volatile (
78
    /* Calculate the bias into %0 */
86
    // Calculate the bias into %0
79
    /* Generates "fake" R_386_RELATIVE run-time relocation */
87
    // Generates "fake" R_386_RELATIVE run-time relocation
80
"   call .L0;"
88
"   call .L0;"
81
".L0:   pop %0;"
89
".L0:   pop %0;"
82
"   subl $.L0, %0;"
90
"   subl $.L0, %0;"
83
 
91
 
84
    /* Calculate run-time address of _DYNAMIC into %1 */
92
    // Calculate run-time address of _DYNAMIC into %1
85
    /* Generates "fake" R_386_RELATIVE run-time relocation */
93
    // Generates "fake" R_386_RELATIVE run-time relocation
86
"   movl $_DYNAMIC, %1;"    /* Again, at link time 0-based VMA gets in */
94
"   movl $_DYNAMIC, %1;"    // Again, at link time 0-based VMA gets in
87
"   addl %0, %1;"       /* Add bias to compute run-time address */
95
"   addl %0, %1;"       // Add bias to compute run-time address
88
 
96
 
89
: "=r" (bias), "=r" (dynamic)
97
: "=r" (bias), "=r" (dynamic)
90
);
98
);
91
 
99
*/
92
    kputint(bias);
100
    kputint(bias);
93
    kputint((unsigned)dynamic);
101
    kputint((unsigned)dynamic);
94
 
102
 
95
    /* parse DYNAMIC */
103
    /* parse DYNAMIC */
96
    got = 0;
104
    got = 0;
97
    sym_table = 0;
105
    sym_table = 0;
98
    rel_table = 0;
106
    rel_table = 0;
99
    rel_entries = 0;
107
    rel_entries = 0;
100
    jmp_rel_table = 0;
108
    jmp_rel_table = 0;
101
    jmp_rel_entries = 0;
109
    jmp_rel_entries = 0;
102
 
110
 
103
    i = 0;
111
    i = 0;
104
    while (dynamic[i].d_tag != 0) {
112
    while (dynamic[i].d_tag != 0) {
105
        dptr = (void *)(dynamic[i].d_un.d_val + bias);
113
        dptr = (void *)(dynamic[i].d_un.d_val + bias);
106
        dval = dynamic[i].d_un.d_val;
114
        dval = dynamic[i].d_un.d_val;
107
 
115
 
108
        switch (dynamic[i].d_tag) {
116
        switch (dynamic[i].d_tag) {
109
        case DT_PLTRELSZ: jmp_rel_entries = dval/8; break;
117
        case DT_PLTRELSZ: jmp_rel_entries = dval/8; break;
110
        case DT_JMPREL: jmp_rel_table = dptr; break;
118
        case DT_JMPREL: jmp_rel_table = dptr; break;
111
        case DT_PLTGOT:
119
        case DT_PLTGOT:
112
            /* GOT address */
120
            /* GOT address */
113
            got = dptr; break;
121
            got = dptr; break;
114
        case DT_SYMTAB: sym_table = dptr; break;
122
        case DT_SYMTAB: sym_table = dptr; break;
115
        case DT_REL: rel_table = dptr; break;
123
        case DT_REL: rel_table = dptr; break;
116
        case DT_RELSZ: rel_entries = dval / 8; break;
124
        case DT_RELSZ: rel_entries = dval / 8; break;
117
        default: break;
125
        default: break;
118
        }
126
        }
119
 
127
 
120
        ++i;
128
        ++i;
121
    }
129
    }
122
   
130
   
123
    kputint(1);
131
    kputint(1);
124
    kputint((unsigned)sym_table);
132
    kputint((unsigned)sym_table);
125
    kputint((unsigned)rel_table);
133
    kputint((unsigned)rel_table);
126
    kputint((unsigned)rel_entries);
134
    kputint((unsigned)rel_entries);
127
 
135
 
128
    /* Now relocate all our dynsyms */
136
    /* Now relocate all our dynsyms */
129
    kputint(-1);
137
    kputint(-1);
130
   
138
   
131
    for (i=0; i<rel_entries; i++) {
139
    for (i=0; i<rel_entries; i++) {
132
        kputint(i);
140
        kputint(i);
133
        r_offset = rel_table[i].r_offset;
141
        r_offset = rel_table[i].r_offset;
134
        r_info = rel_table[i].r_info;
142
        r_info = rel_table[i].r_info;
135
 
143
 
136
        rel_type = ELF32_R_TYPE(r_info);
144
        rel_type = ELF32_R_TYPE(r_info);
137
 
145
 
138
        kputint(rel_type);
146
        kputint(rel_type);
139
        kputint(r_offset);
147
        kputint(r_offset);
140
 
148
 
141
        switch (rel_type) {
149
        switch (rel_type) {
142
        case R_386_GLOB_DAT:
150
        case R_386_GLOB_DAT:
143
        case R_386_JUMP_SLOT:
151
        case R_386_JUMP_SLOT:
144
            kputint(16);
152
            kputint(16);
145
            sym_idx = ELF32_R_SYM(r_info);
153
            sym_idx = ELF32_R_SYM(r_info);
146
 
154
 
147
            sym_addr = sym_table[sym_idx].st_value + bias;
155
            sym_addr = sym_table[sym_idx].st_value + bias;
148
            kputint(sym_idx);
156
            kputint(sym_idx);
149
            kputint(sym_addr);
157
            kputint(sym_addr);
150
 
158
 
151
            *(unsigned *)(r_offset+bias) = sym_addr;
159
            *(unsigned *)(r_offset+bias) = sym_addr;
152
            break;
160
            break;
153
 
161
 
154
        case R_386_32:
162
        case R_386_32:
155
            kputint(16);
163
            kputint(16);
156
            sym_idx = ELF32_R_SYM(r_info);
164
            sym_idx = ELF32_R_SYM(r_info);
157
 
165
 
158
            sym_addr = sym_table[sym_idx].st_value + bias;
166
            sym_addr = sym_table[sym_idx].st_value + bias;
159
            kputint(sym_idx);
167
            kputint(sym_idx);
160
            kputint(sym_addr);
168
            kputint(sym_addr);
161
 
169
 
162
            *(unsigned *)(r_offset+bias) += sym_addr;
170
            *(unsigned *)(r_offset+bias) += sym_addr;
163
            break;
171
            break;
164
           
172
           
165
        case R_386_RELATIVE:
173
        case R_386_RELATIVE:
166
            kputint(16);
174
            kputint(16);
167
            *(unsigned *)(r_offset+bias) += bias;
175
            *(unsigned *)(r_offset+bias) += bias;
168
            break;
176
            break;
169
        }
177
        }
170
    }
178
    }
171
 
179
 
172
    kputint(-1);
180
    kputint(-1);
173
   
181
   
174
    for (i=0; i<jmp_rel_entries; i++) {
182
    for (i=0; i<jmp_rel_entries; i++) {
175
        kputint(i);
183
        kputint(i);
176
        r_offset = jmp_rel_table[i].r_offset;
184
        r_offset = jmp_rel_table[i].r_offset;
177
        r_info = jmp_rel_table[i].r_info;
185
        r_info = jmp_rel_table[i].r_info;
178
 
186
 
179
        rel_type = ELF32_R_TYPE(r_info);
187
        rel_type = ELF32_R_TYPE(r_info);
180
 
188
 
181
        kputint(rel_type);
189
        kputint(rel_type);
182
        kputint(r_offset);
190
        kputint(r_offset);
183
 
191
 
184
        switch (rel_type) {
192
        switch (rel_type) {
185
        case R_386_GLOB_DAT:
193
        case R_386_GLOB_DAT:
186
        case R_386_JUMP_SLOT:
194
        case R_386_JUMP_SLOT:
187
            kputint(16);
195
            kputint(16);
188
            sym_idx = ELF32_R_SYM(r_info);
196
            sym_idx = ELF32_R_SYM(r_info);
189
 
197
 
190
            sym_addr = sym_table[sym_idx].st_value + bias;
198
            sym_addr = sym_table[sym_idx].st_value + bias;
191
            kputint(sym_idx);
199
            kputint(sym_idx);
192
            kputint(sym_addr);
200
            kputint(sym_addr);
193
 
201
 
194
            *(unsigned *)(r_offset+bias) = sym_addr;
202
            *(unsigned *)(r_offset+bias) = sym_addr;
195
            break;
203
            break;
196
 
204
 
197
        case R_386_32:
205
        case R_386_32:
198
            kputint(16);
206
            kputint(16);
199
            sym_idx = ELF32_R_SYM(r_info);
207
            sym_idx = ELF32_R_SYM(r_info);
200
 
208
 
201
            sym_addr = sym_table[sym_idx].st_value + bias;
209
            sym_addr = sym_table[sym_idx].st_value + bias;
202
            kputint(sym_idx);
210
            kputint(sym_idx);
203
            kputint(sym_addr);
211
            kputint(sym_addr);
204
 
212
 
205
            *(unsigned *)(r_offset+bias) += sym_addr;
213
            *(unsigned *)(r_offset+bias) += sym_addr;
206
            break;
214
            break;
207
           
215
           
208
        case R_386_RELATIVE:
216
        case R_386_RELATIVE:
209
            kputint(16);
217
            kputint(16);
210
            *(unsigned *)(r_offset+bias) += bias;
218
            *(unsigned *)(r_offset+bias) += bias;
211
            break;
219
            break;
212
        }
220
        }
213
    }
221
    }
214
 
222
 
215
    kputint(-1);
223
    kputint(-1);
216
 
224
 
217
    /* This will come in handy */
225
    /* This will come in handy */
218
    runtime_env.rtld_dynamic = dynamic;
226
    runtime_env.rtld_dynamic = dynamic;
219
    runtime_env.rtld.bias = bias;
227
    runtime_env.rtld.bias = bias;
220
   
228
   
221
    /* Init libc and run rtld main */
229
    /* Init libc and run rtld main */
222
    __main();
230
    __main();
223
 
231
 
224
    kputint(33);
232
    kputint(33);
225
    __io_init();
233
    __io_init();
226
    kputint(34);
234
    kputint(34);
227
    _rtld_main();
235
    _rtld_main();
228
    kputint(35);
236
    kputint(35);
229
    __exit();
237
    __exit();
230
 
238
 
231
    kputint(36);
239
    kputint(36);
232
 
240
 
233
    asm (
241
    asm (
234
        "movl $250, %%eax;"
242
        "movl $250, %%eax;"
235
        "int $0x30"
243
        "int $0x30"
236
        : /* output */
244
        : /* output */
237
        : /* input */
245
        : /* input */
238
        : "%eax","%ecx","%edx" /* all scratch registers clobbered */
246
        : "%eax","%ecx","%edx" /* all scratch registers clobbered */
239
    );
247
    );
240
}
248
}
241
 
249
 
242
/** @}
250
/** @}
243
 */
251
 */
244
 
252