1 ; test source file for assembling to ELF shared library
3 ; nasm -f elf elfso.asm
4 ; ld -shared -o elfso.so elfso.o
6 ; gcc -o elfso elftest.c ./elfso.so
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
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]
37 COMMON commvar
4:4 ; [7]
38 EXTERN _GLOBAL_OFFSET_TABLE_
42 ; prototype: long lrotate(long x, int num);
48 .
label rol eax,1 ; [4] [8]
49 loop .
label ; [9] [12]
54 ; prototype: void greet(void);
55 greet
push ebx ; we'll use EBX for GOT, so save it
58 add ebx,_GLOBAL_OFFSET_TABLE_
+ $$
- .getgot wrt ..gotpc
59 mov eax,[ebx+integer wrt ..got
] ; [14]
62 mov [ebx+localint wrt ..gotoff
],eax ; [14]
63 mov eax,[ebx+commvar wrt ..got
]
65 mov eax,[ebx+localptr wrt ..gotoff
] ; [13]
67 mov eax,[ebx+integer wrt ..got
] ; [1] [14]
69 lea eax,[ebx+printfstr wrt ..gotoff
]
71 call printf wrt ..plt
; [11]
79 asmstr
db 'hello, world', 0 ; [2]
83 printfstr
db "integer==%d, localint==%d, commvar=%d"
87 localptr
dd localint
; [5] [17]
88 textptr
dd greet wrt ..sym
; [15]
89 selfptr
dd selfptr wrt ..sym
; [16]