NASM 0.96
[nasm.git] / test / elfso.asm
blob5adb6339631f885ef87886c81e9e01f946058a25
1 ; test source file for assembling to ELF shared library
2 ; build with:
3 ; nasm -f elf elfso.asm
4 ; ld -shared -o elfso.so elfso.o
5 ; test with:
6 ; gcc -o elfso elftest.c ./elfso.so
7 ; ./elfso
8 ; (assuming your gcc is ELF, and you're running bash)
10 ; This file should test the following:
11 ; [1] Define and export a global text-section symbol
12 ; [2] Define and export a global data-section symbol
13 ; [3] Define and export a global BSS-section symbol
14 ; [4] Define a non-global text-section symbol
15 ; [5] Define a non-global data-section symbol
16 ; [6] Define a non-global BSS-section symbol
17 ; [7] Define a COMMON symbol
18 ; [8] Define a NASM local label
19 ; [9] Reference a NASM local label
20 ; [10] Import an external symbol
21 ; [11] Make a PC-relative call to an external symbol
22 ; [12] Reference a text-section symbol in the text section
23 ; [13] Reference a data-section symbol in the text section
24 ; [14] Reference a BSS-section symbol in the text section
25 ; [15] Reference a text-section symbol in the data section
26 ; [16] Reference a data-section symbol in the data section
27 ; [17] Reference a BSS-section symbol in the data section
29 BITS 32
30 GLOBAL lrotate:function ; [1]
31 GLOBAL greet:function ; [1]
32 GLOBAL asmstr:data asmstr.end-asmstr ; [2]
33 GLOBAL textptr:data 4 ; [2]
34 GLOBAL selfptr:data 4 ; [2]
35 GLOBAL integer:data 4 ; [3]
36 EXTERN printf ; [10]
37 COMMON commvar 4:4 ; [7]
38 EXTERN _GLOBAL_OFFSET_TABLE_
40 SECTION .text
42 ; prototype: long lrotate(long x, int num);
43 lrotate: ; [1]
44 push ebp
45 mov ebp,esp
46 mov eax,[ebp+8]
47 mov ecx,[ebp+12]
48 .label rol eax,1 ; [4] [8]
49 loop .label ; [9] [12]
50 mov esp,ebp
51 pop ebp
52 ret
54 ; prototype: void greet(void);
55 greet push ebx ; we'll use EBX for GOT, so save it
56 call .getgot
57 .getgot: pop ebx
58 add ebx,_GLOBAL_OFFSET_TABLE_ + $$ - .getgot wrt ..gotpc
59 mov eax,[ebx+integer wrt ..got] ; [14]
60 mov eax,[eax]
61 inc eax
62 mov [ebx+localint wrt ..gotoff],eax ; [14]
63 mov eax,[ebx+commvar wrt ..got]
64 push dword [eax]
65 mov eax,[ebx+localptr wrt ..gotoff] ; [13]
66 push dword [eax]
67 mov eax,[ebx+integer wrt ..got] ; [1] [14]
68 push dword [eax]
69 lea eax,[ebx+printfstr wrt ..gotoff]
70 push eax ; [13]
71 call printf wrt ..plt ; [11]
72 add esp,16
73 pop ebx
74 ret
76 SECTION .data
78 ; a string
79 asmstr db 'hello, world', 0 ; [2]
80 .end
82 ; a string for Printf
83 printfstr db "integer==%d, localint==%d, commvar=%d"
84 db 10, 0
86 ; some pointers
87 localptr dd localint ; [5] [17]
88 textptr dd greet wrt ..sym ; [15]
89 selfptr dd selfptr wrt ..sym ; [16]
91 SECTION .bss
93 ; an integer
94 integer resd 1 ; [3]
96 ; a local integer
97 localint resd 1 ; [6]