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 ; 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.
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.
22 ; yasm -f elf64 -o bold_ibh-x86_64.o bold_ibh-x86_64.asm
23 ; (or replace yasm by nasm)
29 global _bold__ibh_start
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
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
50 mov esi, _bold__functions_hash ; Implicitly zero-extended
51 mov edi, _bold__functions_pointers ; ditto
52 mov ecx, _bold__functions_count
54 ; Load all the symbols
56 lodsd ; Load symbol hash in eax
61 mov r15d, eax ; Save function hash
62 mov r13, rbx ; copy link_map's pseudo-head
64 ; Iterate over libraries found in link_map
66 mov rdx, [r13 + 16] ; link_map->l_ld
68 ; Find the interesting entries in the DYNAMIC table.
70 xor eax, eax ; enough because hash was 32 bits
72 mov al, DT_HASH ; DT_HASH == 4
74 cmove r9, [rdx+8] ; r9 : pointer to the hash table
76 inc al ; DT_STRTAB == 5
78 cmove r10, [rdx+8] ; r10 : pointer to strtab
80 inc al ; DT_SYMTAB == 6
82 cmove r11, [rdx+8] ; r11 : pointer to symtab
90 ; All DYNAMIC entries have been read.
91 mov ecx, [r9 + 4] ; nchain, number of exported symbols
93 ; Iterate over the symbols in the library (symtab entries).
95 ; Find the symbol name in strtab
96 mov esi, [r11] ; st_name, offset in strtab
97 add rsi, r10 ; pointer to symbol name
102 .hash_loop: ; over each char
103 imul edx, edx, byte 0x21
110 cmp edx, r15d ; Compare with stored hash
112 add r11, 24 ; Next symtab entry
115 ; Symbol was not found in this library
116 mov r13, [r13 + 24] ; Next link_map entry
119 mov rax, [r11 + 8] ; st_value, offset of the symbol
120 add rax, [r13] ; add link_map->l_addr
125 stosq ; Store function pointer
129 ; When all is resolved, call main()