loader32.asm 6.29 KB
Newer Older
1
; vim: set ft=nasm et:
PoroCYon's avatar
PoroCYon committed
2
3
4

%include "rtld.inc"

PoroCYon's avatar
PoroCYon committed
5
%define R10_BIAS (0x178)
6

PoroCYon's avatar
PoroCYon committed
7
%ifdef ELF_TYPE
PoroCYon's avatar
PoroCYon committed
8
[section .text.startup.smol]
PoroCYon's avatar
PoroCYon committed
9
10
11
12
%else
; not defined -> debugging!
[section .text]
%endif
PoroCYon's avatar
PoroCYon committed
13

14
15
16
17
18
19
%ifndef USE_DNLOAD_LOADER
_smol_linkmap:
    dd 0
_smol_linkoff:
    dd 0
%endif
PoroCYon's avatar
PoroCYon committed
20
21

_smol_start:
PoroCYon's avatar
PoroCYon committed
22
%ifdef USE_DL_FINI
23
   push edx ; _dl_fini
PoroCYon's avatar
PoroCYon committed
24
%endif
25
26
        ; try to get the 'version-agnostic' pffset of the stuff we're
        ; interested in
PoroCYon's avatar
PoroCYon committed
27
28

%ifdef USE_DT_DEBUG
29
30
    mov eax, [rel _DEBUG]
    mov eax, [eax + 4]
PoroCYon's avatar
PoroCYon committed
31
32
33
%endif

%ifdef SKIP_ENTRIES
34
35
    mov eax, [eax + LM_NEXT_OFFSET] ; skip this binary
;   mov eax, [eax + LM_NEXT_OFFSET] ; skip the vdso
PoroCYon's avatar
PoroCYon committed
36
37
%endif

38
39
40
%ifdef USE_JMP_BYTES
   push _symbols+1
%else
41
   push _symbols
42
43
%endif

44
%ifdef USE_DNLOAD_LOADER
PoroCYon's avatar
PoroCYon committed
45
   push eax
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    pop ebp
    pop edi

    .next_hash:
        mov ecx, [edi]
            ; assume it's nonzero
       push ebp
        pop edx
            ; edx: hash
            ; ebx: link_map* chain

        .next_link:
;           pop edx
            mov edx, [edx + L_NEXT_OFF]
                ; ElfW(Dyn)* dyn(esi) = ebx->l_ld
            mov esi, [edx + L_LD_OFF]

           push edx
                ; get strtab off
            .next_dyn:
              lodsd
                cmp al, DT_STRTAB
              lodsd
                jne short .next_dyn

                ; void* addr(edx) = ebx->l_addr
                ; const char* strtab(ebx)=lookup(esi,DT_STRTAB);
            mov edx, [edx + L_ADDR_OFF]
            cmp eax, edx
            jae short .noreldynaddr
            add eax, edx
        .noreldynaddr:
           push eax
            pop ebx

                ; const ElfW(Sym)* symtab(edx) = lookup(esi, DT_SYMTAB);
          lodsd ; SYMTAB d_tag
          lodsd ; SYMTAB d_un
            cmp eax, edx
            jae short .norelsymaddr
            add eax, edx
        .norelsymaddr:
           push eax
            pop edx

        .next_sym:
            mov esi, [edx + ST_NAME_OFF]
            add esi, ebx

           push ecx
96
%ifndef USE_HASH16
97
98
99
100
101
           push ebx
           push 33
           push 5381
            pop eax
            pop ebx
102
103
104
%else
            xor eax, eax
%endif
105
106
            xor ecx, ecx
        .nexthashiter:
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                    ;
               xchg eax, ecx
              lodsb
                 or al, al
               xchg eax, ecx
                 jz short .breakhash

%ifndef USE_HASH16
               push edx
                mul ebx
                pop edx
;               add eax, ecx
%else
                ror ax, 2
;               add ax, cx
%endif
                add eax, ecx
                jmp short .nexthashiter
125
126

        .breakhash:
127
%ifndef USE_HASH16
128
            pop ebx
129
%endif
130
            pop ecx
131
132
133
134
135
;%ifndef USE_HASH16
;           cmp ecx, eax
;%else
;           cmp  cx,  ax
;%endif
136
137
138
139
140
141
142
143
144
145
146
147
            cmp ecx, eax
             je short .hasheq

            add edx, SYMTAB_SIZE
            cmp edx, ebx
             jb short .next_sym
            pop edx
            jmp short .next_link

        .hasheq:
            mov eax, [edx + ST_VALUE_OFF]
            pop edx
PoroCYon's avatar
PoroCYon committed
148
149
150
151
%ifdef SKIP_ZERO_VALUE
             or eax, eax
             jz short .next_link
%endif
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
            mov esi, [edx + L_ADDR_OFF]
           ;cmp eax, esi
           ; jb short .hasheqnorel
            add eax, esi
        .hasheqnorel:
           ;add eax, [edx + L_ADDR_OFF] ; TODO: CONDITIONAL!
          stosd
%ifdef USE_JMP_BYTES
            inc edi ; skip 0xE9 (jmp) offset
%endif
            cmp word [edi], 0
            jne short .next_hash

; if USE_DNLOAD_LOADER
%else
167
168
169

        mov [_smol_linkmap], eax

PoroCYon's avatar
PoroCYon committed
170
        mov ebx, eax
171
172
173
174
175
176
        mov edi, eax
       push -1
        pop ecx
        mov eax, _smol_start
repne scasd
        sub edi, ebx
PoroCYon's avatar
PoroCYon committed
177
178
       ;sub edi, LM_ENTRY_OFFSET_BASE+4-R10_BIAS
        add edi, R10_BIAS-LM_ENTRY_OFFSET_BASE-4
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        mov [_smol_linkoff], edi

        pop edi ; _symbols

            ; edi: _symbols
            ; ebp: link_map* root
           push ecx
    .next_hash:
            pop ecx
            mov ecx, [edi]

            mov ebp, [_smol_linkmap]
                ; ecx: hash (assumed nonzero)
                ; ebp: link_map* chain

               push ecx
        .next_link:
                pop ecx
                mov ebp, [ebp + L_NEXT_OFF]
                mov esi, ebp
                add esi, [_smol_linkoff]

                    ; edx: btk_ind
               push ecx
               push ecx
               push ecx
                pop eax
PoroCYon's avatar
PoroCYon committed
206
                mov ecx, [esi + LF_NBUCKETS_OFF - R10_BIAS]
207
208
209
210
211
212
                xor edx, edx
                div ecx
                pop ecx
                shr ecx, 1

                    ; ebx: bucket
PoroCYon's avatar
PoroCYon committed
213
                mov ebx, [esi + LF_GNU_BUCKETS_OFF - R10_BIAS]
214
215
216
217
                mov ebx, [ebx + edx * 4]

                .next_chain:
                        ; edx: luhash
PoroCYon's avatar
PoroCYon committed
218
                    mov edx, [esi + LF_GNU_CHAIN_ZERO_OFF - R10_BIAS]
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
                    mov edx, [edx + ebx * 4]

                        ; ecx: hash
                    mov al, dl

                    shr edx, 1
                    cmp edx, ecx
                     je short .chain_break

                    and al, 1
                    jnz short .next_link

                    inc ebx
                    jmp short .next_chain

            .chain_break:
                mov eax, [ebp + L_INFO_DT_SYMTAB_OFF]
                mov eax, [eax + D_UN_PTR_OFF]
                lea eax, [eax + ebx * 8]
                mov eax, [eax + ebx * 8 + ST_VALUE_OFF]
%ifdef SKIP_ZERO_VALUE
                 or eax, eax
                 jz short .next_link
%endif

                add eax, [ebp + L_ADDR_OFF]
              stosd
%ifdef USE_JMP_BYTES
                inc edi
%endif
                cmp word [edi], 0
                jne short .next_hash
PoroCYon's avatar
PoroCYon committed
251

PoroCYon's avatar
PoroCYon committed
252
253
       pop eax ; get rid of leftover ecx on stack

254
255
; if USE_DNLOAD_LOADER ... else ...
%endif
PoroCYon's avatar
PoroCYon committed
256
257
258
259

      ;xor ebp, ebp ; let's put that burden on the user code, so they can leave
                    ; it out if they want to

PoroCYon's avatar
PoroCYon committed
260
%ifdef USE_DL_FINI
PoroCYon's avatar
PoroCYon committed
261
       pop edx      ; _dl_fini
PoroCYon's avatar
PoroCYon committed
262
%endif
263
           ; move esp into eax, *then* increase the stack by 4, as main()
264
265
266
267
268
269
270
           ; expects a return address to be inserted by a call instruction
           ; (which we don't have, so we're doing a 1-byte fixup instead of a
           ; 5-byte call)
      push esp
       pop eax
      push eax
      push eax
PoroCYon's avatar
PoroCYon committed
271
272
273
274

      ;jmp short _start
           ; by abusing the linker script, _start ends up right here :)