]> git.alrj.org Git - bold.git/blob - runtime/bold_ibh-x86_64.asm
033620075238860bf1d30504a47e8bf2714ae5c8
[bold.git] / runtime / bold_ibh-x86_64.asm
1 ; kate: syntax Intel x86 (NASM);
2
3 ; Copyright (C) 2009 Amand "alrj" Tihon <amand.tihon@alrj.org>
4
5 ; Import by hash for linux/amd64 (elf64-x86-64)
6 ; This file is part of bold, the Byte Optimized Linker.
7
8 ; You can redistribute this file and/or modify it under the terms of the
9 ; GNU General Public License as published by the Free Software Foundation,
10 ; either version 3 of the License or (at your option) any later version.
11
12 ; Under Section 7 of GPL version 3, you are granted additional
13 ; permissions described in the Bold Runtime Library Exception, version
14 ; 1.0, as published by Amand Tihon.
15
16 ;------------------------------------------------------------------------------
17 ; alrj's x86_64 version of the import by hash method by parapete, las, leblane.
18 ; See the wonderful thread at http://www.pouet.net/topic.php?which=5392 to
19 ; learn everything about import by hash on Linux.
20
21 ; Compile with
22 ; yasm -f elf64 -o bold_ibh-x86_64.o bold_ibh-x86_64.asm
23 ; (or replace yasm by nasm)
24
25
26 BITS 64
27 CPU X64
28
29 global _bold__ibh
30 global exit
31
32 extern _dt_debug                        ; defined by bold linker
33 extern _bold__functions_hash            ; in .data, generated by bold
34 extern _bold__functions_pointers        ; in .bss, generated by bold
35 extern _bold__functions_count           ; immediate 32 bits
36 extern main                             ; must be declared when using this
37
38 %define SYS_exit      60
39 %define DT_HASH       4
40
41 segment .text
42
43 _bold__ibh_start:
44 ; {{{ Do the RTLD
45   mov rbx, [_dt_debug]                  ; rbx points to r_debug
46   mov rbx, [rbx + 8]                    ; rbx points to link_map
47   mov rbx, [rbx + 24]                   ; skip the first two link_map entries
48   mov rbx, [rbx + 24]
49
50   mov esi, _bold__functions_hash        ; Implicitly zero-extended
51   mov edi, _bold__functions_pointers    ; ditto
52   mov ecx, _bold__functions_count
53
54   ; Load all the symbols
55   .symbol_loop:
56     lodsd                               ; Load symbol hash in eax
57     push rsi
58     push rcx
59
60 ;   {{{ For each hash
61     mov r15d, eax                         ; Save function hash
62     mov r13, rbx                          ; copy link_map's pseudo-head
63
64     ; Iterate over libraries found in link_map
65     .libloop:
66       mov rdx, [r13 + 16]                 ; link_map->l_ld
67
68       ; Find the interesting entries in the DYNAMIC table.
69       .dynamic_loop:
70         xor eax, eax                      ; enough because hash was 32 bits
71
72         mov al, DT_HASH                   ; DT_HASH == 4
73         cmp [rdx], rax
74         cmove r9, [rdx+8]                 ; r9 : pointer to the hash table
75
76         inc al                            ; DT_STRTAB == 5
77         cmp [rdx], rax
78         cmove r10, [rdx+8]                ; r10 : pointer to strtab
79
80         inc al                            ; DT_SYMTAB == 6
81         cmp [rdx], rax
82         cmove r11, [rdx+8]                ; r11 : pointer to symtab
83
84         ; Next dynamic entry
85         add rdx, 16
86         xor al, al
87         cmp [rdx], rax
88         jnz .dynamic_loop
89
90       ; All DYNAMIC entries have been read.
91       mov ecx, [r9 + 4]                   ; nchain, number of exported symbols
92
93       ; Iterate over the symbols in the library (symtab entries).
94       .symbolloop:
95         ; Find the symbol name in strtab
96         mov esi, [r11]                    ; st_name, offset in strtab
97         add rsi, r10                      ; pointer to symbol name
98
99         ; Compute the hash
100         xor edx, edx
101         xor eax, eax
102         .hash_loop:                       ; over each char
103           imul edx, edx, byte 0x21
104           xor edx, eax
105           lodsb
106           test al, al
107           jnz short .hash_loop
108
109         .hash_end:
110         cmp edx, r15d                     ; Compare with stored hash
111         je .found
112         add r11, 24                       ; Next symtab entry
113       loop .symbolloop
114
115       ; Symbol was not found in this library
116       mov r13, [r13 + 24]                 ; Next link_map entry
117       jmp short .libloop
118     .found:
119     mov rax, [r11 + 8]                    ; st_value, offset of the symbol
120     add rax, [r13]                        ; add link_map->l_addr
121 ;   }}}
122
123     pop rcx
124     pop rsi
125     stosq                               ; Store function pointer
126     loop .symbol_loop
127 ; }}}
128
129   ; When all is resolved, call main()
130   call main
131   mov edi, eax
132
133 exit:
134   ; Exit cleanly
135   mov eax, SYS_exit
136   syscall