Home | History | Annotate | Download | only in amd64
      1 ; This code is UNTESTED, and almost certainly DOES NOT WORK!
      2 ; Do NOT use this as an example of how to write AMD64 shared libraries!
      3 ; This code is simply to test the AMD64 ELF WRT relocations.
      4 
      5 ; This file should test the following:
      6 ; [1] Define and export a global text-section symbol
      7 ; [2] Define and export a global data-section symbol
      8 ; [3] Define and export a global BSS-section symbol
      9 ; [4] Define a non-global text-section symbol
     10 ; [5] Define a non-global data-section symbol
     11 ; [6] Define a non-global BSS-section symbol
     12 ; [7] Define a COMMON symbol
     13 ; [8] Define a NASM local label
     14 ; [9] Reference a NASM local label
     15 ; [10] Import an external symbol
     16 ; [11] Make a PC-relative call to an external symbol
     17 ; [12] Reference a text-section symbol in the text section
     18 ; [13] Reference a data-section symbol in the text section
     19 ; [14] Reference a BSS-section symbol in the text section
     20 ; [15] Reference a text-section symbol in the data section
     21 ; [16] Reference a data-section symbol in the data section
     22 ; [17] Reference a BSS-section symbol in the data section
     23 
     24 	  [BITS 64]
     25 	  [GLOBAL lrotate:function] ; [1]
     26 	  [GLOBAL greet:function]	; [1]
     27 	  [GLOBAL asmstr:data asmstr.end-asmstr] ; [2]
     28 	  [GLOBAL textptr:data 4]	; [2]
     29 	  [GLOBAL selfptr:data 4]	; [2]
     30 	  [GLOBAL integer:data 4]	; [3]
     31 	  [EXTERN printf]		; [10]
     32 	  [COMMON commvar 4:4]	; [7]
     33 	  [EXTERN _GLOBAL_OFFSET_TABLE_]
     34 
     35 	  [SECTION .text]
     36 
     37 ; prototype: long lrotate(long x, int num);
     38 lrotate:			; [1]
     39 	  push rbp
     40 	  mov rbp,rsp
     41 	  mov rax,[rbp+8]
     42 	  mov rcx,[rbp+12]
     43 .label	  rol rax,1		; [4] [8]
     44 	  loop .label		; [9] [12]
     45 	  mov rsp,rbp
     46 	  pop rbp
     47 	  ret
     48 
     49 ; prototype: void greet(void);
     50 greet	  push rbx		; we'll use RBX for GOT, so save it
     51 	  mov rbx,[integer wrt ..gotpcrel wrt rip]
     52 	  mov rax,[rbx] ; [14]
     53 	  inc rax
     54 	  mov rbx,[_GLOBAL_OFFSET_TABLE_ wrt ..gotpcrel wrt rip]
     55 	  mov [rbx+localint wrt ..got],eax ; [14]
     56 	  mov rax,[rbx+commvar wrt ..got]
     57 	  push qword [rax]
     58 	  mov rax,[rbx+localptr wrt ..got] ; [13]
     59 	  push qword [rax]
     60 	  mov rax,[rbx+integer wrt ..got] ; [1] [14]
     61 	  push qword [rax]
     62 	  lea rax,[rbx+printfstr wrt ..got]
     63 	  push rax		; [13]
     64 	  call printf wrt ..plt	; [11]
     65 	  add rsp,16
     66 	  pop rbx
     67 	  ret
     68 
     69 	  [SECTION .data]
     70 
     71 ; a string
     72 asmstr	  db 'hello, world', 0	; [2]
     73 .end
     74 
     75 ; a string for Printf
     76 printfstr db "integer==%d, localint==%d, commvar=%d"
     77 	  db 10, 0
     78 
     79 ; some pointers
     80 localptr  dd localint		; [5] [17]
     81 textptr	  dd greet wrt ..sym	; [15]
     82 selfptr	  dd selfptr wrt ..sym	; [16]
     83 
     84 	  [SECTION .bss]
     85 
     86 ; an integer
     87 integer	  resd 1		; [3]
     88 
     89 ; a local integer
     90 localint  resd 1		; [6]
     91