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