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 ; nasm -f elf64 -o bold_ibh-x86_64.o bold_ibh-x86_64.asm
28 global _bold__ibh_start
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
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
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.
54 ; Load all the symbols
56 lodsd ; Load symbol hash
57 xchg ebx, eax ; into ebx
63 ; Iterate over libraries found in link_map
65 mov rdx, [r14 + 16] ; link_map->l_ld
67 ; Find the interesting entries in the DYNAMIC table.
70 push byte DT_HASH ; DT_HASH == 4
73 cmove r9, [rdx+8] ; r9 : pointer to the hash table
75 inc eax ; DT_STRTAB == 5
77 cmove r10, [rdx+8] ; r10 : pointer to strtab
79 inc eax ; DT_SYMTAB == 6
81 cmove r11, [rdx+8] ; r11 : pointer to symtab
84 lea rdx, [rdx + 16] ; add rdx, 16
87 ;cmp [rdx], dword 0 ; Gut feeling: would harm compression
88 jnz short .dynamic_loop
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, ebx ; Compare with stored hash
112 lea r11, [r11 + 24] ; Next symtab entry
115 ; Symbol was not found in this library
116 mov r14, [r14 + 24] ; Next link_map entry
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
129 ; When all is resolved, call main()
139 %assign code_size $ - _bold__ibh_start
140 %warning Code size is: code_size