| 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 |
| 3 |
|
| 4 |
; test source file for assembling to ELF shared library |
| 5 |
; build with: |
| 6 |
; nasm -f elf elfso.asm |
| 7 |
; ld -shared -o elfso.so elfso.o |
| 8 |
; test with: |
| 9 |
; gcc -o elfso elftest.c ./elfso.so |
| 10 |
; ./elfso |
| 11 |
; (assuming your gcc is ELF, and you're running bash) |
| 12 |
|
| 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 |
| 31 |
|
| 32 |
BITS 32 |
| 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] |
| 39 |
EXTERN printf ; [10] |
| 40 |
COMMON commvar 4:4 ; [7] |
| 41 |
EXTERN _GLOBAL_OFFSET_TABLE_ |
| 42 |
|
| 43 |
SECTION .text |
| 44 |
|
| 45 |
; prototype: long lrotate(long x, int num); |
| 46 |
lrotate: ; [1] |
| 47 |
push ebp |
| 48 |
mov ebp,esp |
| 49 |
mov eax,[ebp+8] |
| 50 |
mov ecx,[ebp+12] |
| 51 |
.label rol eax,1 ; [4] [8] |
| 52 |
loop .label ; [9] [12] |
| 53 |
mov esp,ebp |
| 54 |
pop ebp |
| 55 |
ret |
| 56 |
|
| 57 |
; prototype: void greet(void); |
| 58 |
greet push ebx ; we'll use EBX for GOT, so save it |
| 59 |
call .getgot |
| 60 |
.getgot: pop ebx |
| 61 |
add ebx,_GLOBAL_OFFSET_TABLE_ + $$ - .getgot wrt ..gotpc |
| 62 |
mov eax,[ebx+integer wrt ..got] ; [14] |
| 63 |
mov eax,[eax] |
| 64 |
inc eax |
| 65 |
mov [ebx+localint wrt ..gotoff],eax ; [14] |
| 66 |
mov eax,[ebx+commvar wrt ..got] |
| 67 |
push dword [eax] |
| 68 |
mov eax,[ebx+localptr wrt ..gotoff] ; [13] |
| 69 |
push dword [eax] |
| 70 |
mov eax,[ebx+integer wrt ..got] ; [1] [14] |
| 71 |
push dword [eax] |
| 72 |
lea eax,[ebx+printfstr wrt ..gotoff] |
| 73 |
push eax ; [13] |
| 74 |
call printf wrt ..plt ; [11] |
| 75 |
add esp,16 |
| 76 |
pop ebx |
| 77 |
ret |
| 78 |
|
| 79 |
SECTION .data |
| 80 |
|
| 81 |
; a string |
| 82 |
asmstr db 'hello, world', 0 ; [2] |
| 83 |
.end |
| 84 |
|
| 85 |
; a string for Printf |
| 86 |
printfstr db "integer==%d, localint==%d, commvar=%d" |
| 87 |
db 10, 0 |
| 88 |
|
| 89 |
; some pointers |
| 90 |
localptr dd localint ; [5] [17] |
| 91 |
textptr dd greet wrt ..sym ; [15] |
| 92 |
selfptr dd selfptr wrt ..sym ; [16] |
| 93 |
|
| 94 |
SECTION .bss |
| 95 |
|
| 96 |
; an integer |
| 97 |
integer resd 1 ; [3] |
| 98 |
|
| 99 |
; a local integer |
| 100 |
localint resd 1 ; [6] |