1 ;Testname=unoptimized; Arguments=-O0 -felf -oelfso.o; Files=.stdout .stderr elfso.o
2 ;Testname=optimized; Arguments=-Ox -felf -oelfso.o; Files=.stdout .stderr elfso.o
4 ; test source file for assembling to ELF shared library
6 ; nasm -f elf elfso.asm
7 ; ld -shared -o elfso.so elfso.o
9 ; gcc -o elfso elftest.c ./elfso.so
11 ; (assuming your gcc is ELF, and you're running bash)
13 ; This file should test the following:
14 ; [1] Define and export a global text-section symbol
15 ; [2] Define and export a global data-section symbol
16 ; [3] Define and export a global BSS-section symbol
17 ; [4] Define a non-global text-section symbol
18 ; [5] Define a non-global data-section symbol
19 ; [6] Define a non-global BSS-section symbol
20 ; [7] Define a COMMON symbol
21 ; [8] Define a NASM local label
22 ; [9] Reference a NASM local label
23 ; [10] Import an external symbol
24 ; [11] Make a PC-relative call to an external symbol
25 ; [12] Reference a text-section symbol in the text section
26 ; [13] Reference a data-section symbol in the text section
27 ; [14] Reference a BSS-section symbol in the text section
28 ; [15] Reference a text-section symbol in the data section
29 ; [16] Reference a data-section symbol in the data section
30 ; [17] Reference a BSS-section symbol in the data section
33 GLOBAL lrotate:function
; [1]
34 GLOBAL greet:function
; [1]
35 GLOBAL asmstr:data asmstr.
end-asmstr
; [2]
36 GLOBAL textptr:data
4 ; [2]
37 GLOBAL selfptr:data
4 ; [2]
38 GLOBAL integer:data
4 ; [3]
40 COMMON commvar
4:4 ; [7]
41 EXTERN _GLOBAL_OFFSET_TABLE_
45 ; prototype: long lrotate(long x, int num);
51 .
label rol eax,1 ; [4] [8]
52 loop .
label ; [9] [12]
57 ; prototype: void greet(void);
58 greet
push ebx ; we'll use EBX for GOT, so save it
61 add ebx,_GLOBAL_OFFSET_TABLE_
+ $$
- .getgot wrt ..gotpc
62 mov eax,[ebx+integer wrt ..got
] ; [14]
65 mov [ebx+localint wrt ..gotoff
],eax ; [14]
66 mov eax,[ebx+commvar wrt ..got
]
68 mov eax,[ebx+localptr wrt ..gotoff
] ; [13]
70 mov eax,[ebx+integer wrt ..got
] ; [1] [14]
72 lea eax,[ebx+printfstr wrt ..gotoff
]
74 call printf wrt ..plt
; [11]
82 asmstr
db 'hello, world', 0 ; [2]
86 printfstr
db "integer==%d, localint==%d, commvar=%d"
90 localptr
dd localint
; [5] [17]
91 textptr
dd greet wrt ..sym
; [15]
92 selfptr
dd selfptr wrt ..sym
; [16]
100 localint resd
1 ; [6]