1 ; kate: syntax Intel x86 (NASM);
3 ; Copyright (C) 2009 Amand "alrj" Tihon <amand.tihon@alrj.org>
5 ; Import by hash for linux/amd64 (elf64-x86-64)
6 ; This file is part of bold, the Byte Optimized Linker.
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.
12 ;------------------------------------------------------------------------------
13 ; alrj's x86_64 version of the import by hash method by parapete, las, leblane.
14 ; See the wonderful thread at http://www.pouet.net/topic.php?which=5392 to
15 ; learn everything about import by hash on Linux.
18 ; yasm -f elf64 -o bold_ibh-x86_64.o bold_ibh-x86_64.asm
19 ; (or replace yasm by nasm)
28 extern _dt_debug ; defined by bold linker
29 extern _bold__functions_hash ; in .data, generated by bold
30 extern _bold__functions_pointers ; in .bss, generated by bold
31 extern _bold__functions_count ; immediate 32 bits
32 extern main ; must be declared when using this
41 mov rbx, [_dt_debug] ; rbx points to r_debug
42 mov rbx, [rbx + 8] ; rbx points to link_map
43 mov rbx, [rbx + 24] ; skip the first two link_map entries
46 mov esi, _bold__functions_hash ; Implicitly zero-extended
47 mov edi, _bold__functions_pointers ; ditto
48 mov ecx, _bold__functions_count
50 ; Load all the symbols
52 lodsd ; Load symbol hash in eax
57 mov r15d, eax ; Save function hash
58 mov r13, rbx ; copy link_map's pseudo-head
60 ; Iterate over libraries found in link_map
62 mov rdx, [r13 + 16] ; link_map->l_ld
64 ; Find the interesting entries in the DYNAMIC table.
66 xor eax, eax ; enough because hash was 32 bits
68 mov al, DT_HASH ; DT_HASH == 4
70 cmove r9, [rdx+8] ; r9 : pointer to the hash table
72 inc al ; DT_STRTAB == 5
74 cmove r10, [rdx+8] ; r10 : pointer to strtab
76 inc al ; DT_SYMTAB == 6
78 cmove r11, [rdx+8] ; r11 : pointer to symtab
86 ; All DYNAMIC entries have been read.
87 mov ecx, [r9 + 4] ; nchain, number of exported symbols
89 ; Iterate over the symbols in the library (symtab entries).
91 ; Find the symbol name in strtab
92 mov esi, [r11] ; st_name, offset in strtab
93 add rsi, r10 ; pointer to symbol name
98 .hash_loop: ; over each char
99 imul edx, edx, byte 0x21
106 cmp edx, r15d ; Compare with stored hash
108 add r11, 24 ; Next symtab entry
111 ; Symbol was not found in this library
112 mov r13, [r13 + 24] ; Next link_map entry
115 mov rax, [r11 + 8] ; st_value, offset of the symbol
116 add rax, [r13] ; add link_map->l_addr
121 stosq ; Store function pointer
125 ; When all is resolved, call main()