]> git.alrj.org Git - bold.git/blob - runtime/bold_ibh-x86_64.asm
a9a37320c7e9c851f39db67e03780be1506cb2da
[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 ; nasm -f elf64 -o bold_ibh-x86_64.o bold_ibh-x86_64.asm
23
24
25 BITS 64
26 CPU X64
27
28 global _bold__ibh_start
29 global exit
30
31 extern _dt_debug                        ; defined by bold linker
32 extern _bold__functions_hash            ; in .data, generated by bold
33 extern _bold__functions_pointers        ; in .bss, generated by bold
34 extern _bold__functions_count           ; immediate 32 bits
35 extern main                             ; must be declared when using this
36
37 %define SYS_exit      60
38 %define DT_HASH       4
39
40 segment .text
41
42 _bold__ibh_start:
43 ; {{{ Do the RTLD
44   mov r14, [rel _dt_debug]              ; r14 points to r_debug
45   mov r14, [r14 + 8]                    ; r14 points to link_map
46   mov r14, [r14 + 24]                   ; skip the first two link_map entries
47   mov r14, [r14 + 24]
48
49   mov esi, _bold__functions_hash        ; Implicitly zero-extended
50   mov edi, _bold__functions_pointers    ; ditto
51   push byte _bold__functions_count      ; Warning: Max 127 external symbols.
52   pop rcx                               ;  Linker should enforce this.
53
54   ; Load all the symbols
55   .symbol_loop:
56     lodsd                                 ; Load symbol hash
57     xchg ebx, eax                         ; into ebx
58     push rsi
59     push rcx
60
61 ;   {{{ For each hash
62
63     ; Iterate over libraries found in link_map
64     .libloop:
65       mov rdx, [r14 + 16]                 ; link_map->l_ld
66
67       ; Find the interesting entries in the DYNAMIC table.
68       .dynamic_loop:
69
70         push byte DT_HASH                 ; DT_HASH == 4
71         pop rax
72         cmp [rdx], eax
73         cmove r9, [rdx+8]                 ; r9 : pointer to the hash table
74
75         inc eax                           ; DT_STRTAB == 5
76         cmp [rdx], eax
77         cmove r10, [rdx+8]                ; r10 : pointer to strtab
78
79         inc eax                           ; DT_SYMTAB == 6
80         cmp [rdx], eax
81         cmove r11, [rdx+8]                ; r11 : pointer to symtab
82
83         ; Next dynamic entry
84         lea rdx, [rdx + 16]               ; add rdx, 16
85         xor eax, eax
86         cmp [rdx], eax
87         ;cmp [rdx], dword 0               ; Gut feeling: would harm compression
88         jnz short .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, ebx                      ; Compare with stored hash
111         je short .found
112         lea r11, [r11 + 24]               ; Next symtab entry
113       loop .symbolloop
114
115       ; Symbol was not found in this library
116       mov r14, [r14 + 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, [r14]                        ; add link_map->l_addr
121     stosq                                 ; Store function pointer
122 ;   }}}
123
124     pop rcx
125     pop rsi
126     loop .symbol_loop
127 ; }}}
128
129   ; When all is resolved, call main()
130   call main
131
132 exit:
133   ; Exit cleanly
134   xchg edi, eax
135   push byte SYS_exit
136   pop rax
137   syscall
138
139 %assign code_size $ - _bold__ibh_start
140 %warning Code size is: code_size