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 integer:data
8 ; [3]
37 COMMON commvar
8:8 ; [7]
38 EXTERN _GLOBAL_OFFSET_TABLE_
42 ; prototype: long lrotate(long x, int num);
48 .
label rol rax
,1 ; [4] [8]
49 loop .
label ; [9] [12]
54 ;; prototype: void greet_*(void);
56 ;; Arguments are: rdi - rsi - rdx - rcx - r8 - r9
57 ;; Registers: rbx, rbp, r12-r15 are saved
58 ;; greet_s() is Small PIC model, greet_m() is Medium PIC model
59 ;; (Large model cannot be linked with other code)
62 mov rax
,[rel commvar wrt ..got
] ; &commvar
63 mov rcx
,[rax
] ; commvar
64 mov rax
,[rel integer wrt ..got
] ; &integer
67 mov [rel localint
],rdx
; localint = integer+1
68 mov rax
,[rel localptr
] ; localptr
69 mov rdx
,[rax
] ; *localptr = localint
70 lea rdi
,[rel printfstr
]
71 xor eax,eax ; No fp arguments
72 jmp printf wrt ..plt
; [10]
75 push r15
; Used by convention...
76 lea r15
,[rel _GLOBAL_OFFSET_TABLE_
]
77 mov rax
,[rel commvar wrt ..got
] ; &commvar
78 mov rcx
,[rax
] ; commvar
79 mov rax
,[rel integer wrt ..got
] ; &integer
82 mov rax
,localint wrt ..gotoff
; &localint - r15
83 mov [rax
+r15
],rdx
; localint = integer+1
84 mov rax
,localptr wrt ..gotoff
; &localptr - r15
85 mov rax
,[rax
+r15
] ; localptr
86 mov rdx
,[rax
] ; *localptr = localint
87 mov rdi
,printfstr wrt ..gotoff
; &printfstr - r15
88 add rdi
,r15
; &printfstr
89 xor eax,eax ; No fp arguments
91 jmp printf wrt ..plt
; [10]
96 asmstr
db 'hello, world', 0 ; [2]
100 printfstr
db "integer=%ld, localint=%ld, commvar=%ld", 10, 0
103 localptr
dq localint
; [5] [17]
104 textptr
dq greet_s wrt ..sym
; [15]
105 selfptr
dq selfptr wrt ..sym
; [16]
113 localint resq
1 ; [6]