elfso.asm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ; test source file for assembling to ELF shared library
  4. ; build with:
  5. ; nasm -f elf elfso.asm
  6. ; ld -shared -o elfso.so elfso.o
  7. ; test with:
  8. ; gcc -o elfso elftest.c ./elfso.so
  9. ; ./elfso
  10. ; (assuming your gcc is ELF, and you're running bash)
  11. ; This file should test the following:
  12. ; [1] Define and export a global text-section symbol
  13. ; [2] Define and export a global data-section symbol
  14. ; [3] Define and export a global BSS-section symbol
  15. ; [4] Define a non-global text-section symbol
  16. ; [5] Define a non-global data-section symbol
  17. ; [6] Define a non-global BSS-section symbol
  18. ; [7] Define a COMMON symbol
  19. ; [8] Define a NASM local label
  20. ; [9] Reference a NASM local label
  21. ; [10] Import an external symbol
  22. ; [11] Make a PC-relative call to an external symbol
  23. ; [12] Reference a text-section symbol in the text section
  24. ; [13] Reference a data-section symbol in the text section
  25. ; [14] Reference a BSS-section symbol in the text section
  26. ; [15] Reference a text-section symbol in the data section
  27. ; [16] Reference a data-section symbol in the data section
  28. ; [17] Reference a BSS-section symbol in the data section
  29. BITS 32
  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]
  36. EXTERN printf ; [10]
  37. COMMON commvar 4:4 ; [7]
  38. EXTERN _GLOBAL_OFFSET_TABLE_
  39. SECTION .text
  40. ; prototype: long lrotate(long x, int num);
  41. lrotate: ; [1]
  42. push ebp
  43. mov ebp,esp
  44. mov eax,[ebp+8]
  45. mov ecx,[ebp+12]
  46. .label rol eax,1 ; [4] [8]
  47. loop .label ; [9] [12]
  48. mov esp,ebp
  49. pop ebp
  50. ret
  51. ; prototype: void greet(void);
  52. greet push ebx ; we'll use EBX for GOT, so save it
  53. call .getgot
  54. .getgot: pop ebx
  55. add ebx,_GLOBAL_OFFSET_TABLE_ + $$ - .getgot wrt ..gotpc
  56. mov eax,[ebx+integer wrt ..got] ; [14]
  57. mov eax,[eax]
  58. inc eax
  59. mov [ebx+localint wrt ..gotoff],eax ; [14]
  60. mov eax,[ebx+commvar wrt ..got]
  61. push dword [eax]
  62. mov eax,[ebx+localptr wrt ..gotoff] ; [13]
  63. push dword [eax]
  64. mov eax,[ebx+integer wrt ..got] ; [1] [14]
  65. push dword [eax]
  66. lea eax,[ebx+printfstr wrt ..gotoff]
  67. push eax ; [13]
  68. call printf wrt ..plt ; [11]
  69. add esp,16
  70. pop ebx
  71. ret
  72. SECTION .data
  73. ; a string
  74. asmstr db 'hello, world', 0 ; [2]
  75. .end
  76. ; a string for Printf
  77. printfstr db "integer==%d, localint==%d, commvar=%d"
  78. db 10, 0
  79. ; some pointers
  80. localptr dd localint ; [5] [17]
  81. textptr dd greet wrt ..sym ; [15]
  82. selfptr dd selfptr wrt ..sym ; [16]
  83. SECTION .bss
  84. ; an integer
  85. integer resd 1 ; [3]
  86. ; a local integer
  87. localint resd 1 ; [6]