elftest64: both Small PIC and Medium PIC model tests
[nasm/perl-rewrite.git] / test / elf64so.asm
bloba6236951bb0495080624075ede90531e468c0acb
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 integer:data 8 ; [3]
36 EXTERN printf ; [10]
37 COMMON commvar 8:8 ; [7]
38 EXTERN _GLOBAL_OFFSET_TABLE_
40 SECTION .text
42 ; prototype: long lrotate(long x, int num);
43 lrotate: ; [1]
44 push rbp
45 mov rbp,rsp
46 mov rax,rdi
47 mov rcx,rsi
48 .label rol rax,1 ; [4] [8]
49 loop .label ; [9] [12]
50 mov rsp,rbp
51 pop rbp
52 ret
54 ;; prototype: void greet_*(void);
55 ;;
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)
61 greet_s:
62 mov rax,[rel commvar wrt ..got] ; &commvar
63 mov rcx,[rax] ; commvar
64 mov rax,[rel integer wrt ..got] ; &integer
65 mov rsi,[rax]
66 lea rdx,[rsi+1]
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]
74 greet_m:
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
80 mov rsi,[rax]
81 lea rdx,[rsi+1]
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
90 pop r15
91 jmp printf wrt ..plt ; [10]
93 SECTION .data
95 ; a string
96 asmstr db 'hello, world', 0 ; [2]
97 .end:
99 ; a string for Printf
100 printfstr db "integer=%ld, localint=%ld, commvar=%ld", 10, 0
102 ; some pointers
103 localptr dq localint ; [5] [17]
104 textptr dq greet_s wrt ..sym ; [15]
105 selfptr dq selfptr wrt ..sym ; [16]
107 SECTION .bss
109 ; an integer
110 integer resq 1 ; [3]
112 ; a local integer
113 localint resq 1 ; [6]