1 ; test source file for assembling to ELF64 shared library
3 ; nasm -f elf64 elf64so.asm
4 ; ld -shared -o elf64so.so elf64so.o
6 ; gcc -o elf64so elftest64.c ./elf64so.so
9 ; This file should test the following:
10 ; [1] Define and export a global text-section symbol
11 ; [2] Define and export a global data-section symbol
12 ; [3] Define and export a global BSS-section symbol
13 ; [4] Define a non-global text-section symbol
14 ; [5] Define a non-global data-section symbol
15 ; [6] Define a non-global BSS-section symbol
16 ; [7] Define a COMMON symbol
17 ; [8] Define a NASM local label
18 ; [9] Reference a NASM local label
19 ; [10] Import an external symbol
20 ; [11] Make a PC-relative call to an external symbol
21 ; [12] Reference a text-section symbol in the text section
22 ; [13] Reference a data-section symbol in the text section
23 ; [14] Reference a BSS-section symbol in the text section
24 ; [15] Reference a text-section symbol in the data section
25 ; [16] Reference a data-section symbol in the data section
26 ; [17] Reference a BSS-section symbol in the data section
29 GLOBAL lrotate:function
; [1]
30 GLOBAL greet_s:function
; [1]
31 GLOBAL greet_m:function
; [1]
32 GLOBAL asmstr:data asmstr.
end-asmstr
; [2]
33 GLOBAL textptr:data
8 ; [2]
34 GLOBAL selfptr:data
8 ; [2]
35 GLOBAL useless:data
8 ; [3]
36 GLOBAL integer:data
8 ; [3]
38 COMMON commvar
8:8 ; [7]
39 EXTERN _GLOBAL_OFFSET_TABLE_
43 ; prototype: long lrotate(long x, int num);
49 .
label rol rax
,1 ; [4] [8]
50 loop .
label ; [9] [12]
55 ;; prototype: void greet_*(void);
57 ;; Arguments are: rdi - rsi - rdx - rcx - r8 - r9
58 ;; Registers: rbx, rbp, r12-r15 are saved
59 ;; greet_s() is Small PIC model, greet_m() is Medium PIC model
60 ;; (Large model cannot be linked with other code)
63 ;; This instruction is useless, this is only a test...
64 cmp qword [rel integer wrt ..got
],0
65 mov rax
,[rel commvar wrt ..got
] ; &commvar
66 mov rcx
,[rax
] ; commvar
67 mov rax
,[rel integer wrt ..got
] ; &integer
70 mov [rel localint
],rdx
; localint = integer+1
71 mov rax
,[rel localptr
] ; localptr
72 mov rdx
,[rax
] ; *localptr = localint
73 lea rdi
,[rel printfstr
]
74 xor eax,eax ; No fp arguments
75 jmp printf wrt ..plt
; [10]
78 push r15
; Used by convention...
79 lea r15
,[rel _GLOBAL_OFFSET_TABLE_
]
80 mov rax
,[rel commvar wrt ..got
] ; &commvar
81 mov rcx
,[rax
] ; commvar
82 mov rax
,[rel integer wrt ..got
] ; &integer
85 mov rax
,localint wrt ..gotoff
; &localint - r15
86 mov [rax
+r15
],rdx
; localint = integer+1
87 mov rax
,localptr wrt ..gotoff
; &localptr - r15
88 mov rax
,[rax
+r15
] ; localptr
89 mov rdx
,[rax
] ; *localptr = localint
90 mov rdi
,printfstr wrt ..gotoff
; &printfstr - r15
91 add rdi
,r15
; &printfstr
92 xor eax,eax ; No fp arguments
94 jmp printf wrt ..plt
; [10]
99 asmstr
db 'hello, world', 0 ; [2]
102 ; a string for Printf
103 printfstr
db "integer=%ld, localint=%ld, commvar=%ld", 10, 0
106 localptr
dq localint
; [5] [17]
107 textptr
dq greet_s wrt ..sym
; [15]
108 selfptr
dq selfptr wrt ..sym
; [16]
118 localint resq
1 ; [6]