Add __PATH__ support
[nasm.git] / test / elf64so.asm
blobf1b2346429f88d32863c5c2bddb2b312068605f9
1 ; test source file for assembling to ELF64 shared library
2 ; build with:
3 ; nasm -f elf64 elf64so.asm
4 ; ld -shared -o elf64so.so elf64so.o
5 ; test with:
6 ; gcc -o elf64so elftest64.c ./elf64so.so
7 ; ./elf64so
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
28 BITS 64
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]
37 EXTERN printf ; [10]
38 COMMON commvar 8:8 ; [7]
39 EXTERN _GLOBAL_OFFSET_TABLE_
41 SECTION .text
43 ; prototype: long lrotate(long x, int num);
44 lrotate: ; [1]
45 push rbp
46 mov rbp,rsp
47 mov rax,rdi
48 mov rcx,rsi
49 .label rol rax,1 ; [4] [8]
50 loop .label ; [9] [12]
51 mov rsp,rbp
52 pop rbp
53 ret
55 ;; prototype: void greet_*(void);
56 ;;
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)
62 greet_s:
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
68 mov rsi,[rax]
69 lea rdx,[rsi+1]
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]
77 greet_m:
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
83 mov rsi,[rax]
84 lea rdx,[rsi+1]
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
93 pop r15
94 jmp printf wrt ..plt ; [10]
96 SECTION .data
98 ; a string
99 asmstr db 'hello, world', 0 ; [2]
100 .end:
102 ; a string for Printf
103 printfstr db "integer=%ld, localint=%ld, commvar=%ld", 10, 0
105 ; some pointers
106 localptr dq localint ; [5] [17]
107 textptr dq greet_s wrt ..sym ; [15]
108 selfptr dq selfptr wrt ..sym ; [16]
110 SECTION .bss
111 ; a useless symbol
112 useless resq 1
114 ; an integer
115 integer resq 1 ; [3]
117 ; a local integer
118 localint resq 1 ; [6]