elf64so.asm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ; test source file for assembling to ELF64 shared library
  2. ; build with:
  3. ; nasm -f elf64 elf64so.asm
  4. ; ld -shared -o elf64so.so elf64so.o
  5. ; test with:
  6. ; gcc -o elf64so elftest64.c ./elf64so.so
  7. ; ./elf64so
  8. ; This file should test the following:
  9. ; [1] Define and export a global text-section symbol
  10. ; [2] Define and export a global data-section symbol
  11. ; [3] Define and export a global BSS-section symbol
  12. ; [4] Define a non-global text-section symbol
  13. ; [5] Define a non-global data-section symbol
  14. ; [6] Define a non-global BSS-section symbol
  15. ; [7] Define a COMMON symbol
  16. ; [8] Define a NASM local label
  17. ; [9] Reference a NASM local label
  18. ; [10] Import an external symbol
  19. ; [11] Make a PC-relative call to an external symbol
  20. ; [12] Reference a text-section symbol in the text section
  21. ; [13] Reference a data-section symbol in the text section
  22. ; [14] Reference a BSS-section symbol in the text section
  23. ; [15] Reference a text-section symbol in the data section
  24. ; [16] Reference a data-section symbol in the data section
  25. ; [17] Reference a BSS-section symbol in the data section
  26. BITS 64
  27. GLOBAL lrotate:function ; [1]
  28. GLOBAL greet_s:function ; [1]
  29. GLOBAL greet_m:function ; [1]
  30. GLOBAL asmstr:data asmstr.end-asmstr ; [2]
  31. GLOBAL textptr:data 8 ; [2]
  32. GLOBAL selfptr:data 8 ; [2]
  33. GLOBAL useless:data 8 ; [3]
  34. GLOBAL integer:data 8 ; [3]
  35. EXTERN printf ; [10]
  36. COMMON commvar 8:8 ; [7]
  37. EXTERN _GLOBAL_OFFSET_TABLE_
  38. SECTION .text
  39. ; prototype: long lrotate(long x, int num);
  40. lrotate: ; [1]
  41. push rbp
  42. mov rbp,rsp
  43. mov rax,rdi
  44. mov rcx,rsi
  45. .label rol rax,1 ; [4] [8]
  46. loop .label ; [9] [12]
  47. mov rsp,rbp
  48. pop rbp
  49. ret
  50. ;; prototype: void greet_*(void);
  51. ;;
  52. ;; Arguments are: rdi - rsi - rdx - rcx - r8 - r9
  53. ;; Registers: rbx, rbp, r12-r15 are saved
  54. ;; greet_s() is Small PIC model, greet_m() is Medium PIC model
  55. ;; (Large model cannot be linked with other code)
  56. ;;
  57. greet_s:
  58. ;; This instruction is useless, this is only a test...
  59. cmp qword [rel integer wrt ..got],0
  60. mov rax,[rel commvar wrt ..got] ; &commvar
  61. mov rcx,[rax] ; commvar
  62. mov rax,[rel integer wrt ..got] ; &integer
  63. mov rsi,[rax]
  64. lea rdx,[rsi+1]
  65. mov [rel localint],rdx ; localint = integer+1
  66. mov rax,[rel localptr] ; localptr
  67. mov rdx,[rax] ; *localptr = localint
  68. lea rdi,[rel printfstr]
  69. xor eax,eax ; No fp arguments
  70. jmp printf wrt ..plt ; [10]
  71. greet_m:
  72. push r15 ; Used by convention...
  73. lea r15,[rel _GLOBAL_OFFSET_TABLE_]
  74. mov rax,[rel commvar wrt ..got] ; &commvar
  75. mov rcx,[rax] ; commvar
  76. mov rax,[rel integer wrt ..got] ; &integer
  77. mov rsi,[rax]
  78. lea rdx,[rsi+1]
  79. mov rax,localint wrt ..gotoff ; &localint - r15
  80. mov [rax+r15],rdx ; localint = integer+1
  81. mov rax,localptr wrt ..gotoff ; &localptr - r15
  82. mov rax,[rax+r15] ; localptr
  83. mov rdx,[rax] ; *localptr = localint
  84. mov rdi,printfstr wrt ..gotoff ; &printfstr - r15
  85. add rdi,r15 ; &printfstr
  86. xor eax,eax ; No fp arguments
  87. pop r15
  88. jmp printf wrt ..plt ; [10]
  89. SECTION .data
  90. ; a string
  91. asmstr db 'hello, world', 0 ; [2]
  92. .end:
  93. ; a string for Printf
  94. printfstr db "integer=%ld, localint=%ld, commvar=%ld", 10, 0
  95. ; some pointers
  96. localptr dq localint ; [5] [17]
  97. textptr dq greet_s wrt ..sym ; [15]
  98. selfptr dq selfptr wrt ..sym ; [16]
  99. SECTION .bss
  100. ; a useless symbol
  101. useless resq 1
  102. ; an integer
  103. integer resq 1 ; [3]
  104. ; a local integer
  105. localint resq 1 ; [6]