aoutso.asm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ;Testname=unoptimized; Arguments=-O0 -faoutb -oaoutso.o; Files=stdout stderr aoutso.o
  2. ;Testname=optimized; Arguments=-Ox -faoutb -oaoutso.o; Files=stdout stderr aoutso.o
  3. ; test source file for assembling to NetBSD/FreeBSD a.out shared library
  4. ; build with:
  5. ; nasm -f aoutb aoutso.asm
  6. ; ld -Bshareable -o aoutso.so aoutso.o
  7. ; test with:
  8. ; cc -o aoutso aouttest.c aoutso.so
  9. ; ./aoutso
  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
  28. BITS 32
  29. EXTERN __GLOBAL_OFFSET_TABLE_
  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 ; [7]
  38. SECTION .text
  39. ; prototype: long lrotate(long x, int num);
  40. _lrotate: ; [1]
  41. push ebp
  42. mov ebp,esp
  43. mov eax,[ebp+8]
  44. mov ecx,[ebp+12]
  45. .label rol eax,1 ; [4] [8]
  46. loop .label ; [9] [12]
  47. mov esp,ebp
  48. pop ebp
  49. ret
  50. ; prototype: void greet(void);
  51. _greet push ebx ; we'll use EBX for GOT, so save it
  52. call .getgot
  53. .getgot: pop ebx
  54. add ebx,__GLOBAL_OFFSET_TABLE_ + $$ - .getgot wrt ..gotpc
  55. mov eax,[ebx+_integer wrt ..got] ; [14]
  56. mov eax,[eax]
  57. inc eax
  58. mov [ebx+localint wrt ..gotoff],eax ; [14]
  59. mov eax,[ebx+_commvar wrt ..got]
  60. push dword [eax]
  61. mov eax,[ebx+localptr wrt ..gotoff] ; [13]
  62. push dword [eax]
  63. mov eax,[ebx+_integer wrt ..got] ; [1] [14]
  64. push dword [eax]
  65. lea eax,[ebx+_printfstr wrt ..gotoff]
  66. push eax ; [13]
  67. call _printf wrt ..plt ; [11]
  68. add esp,16
  69. pop ebx
  70. ret
  71. SECTION .data
  72. ; a string
  73. _asmstr db 'hello, world', 0 ; [2]
  74. .end
  75. ; a string for Printf
  76. _printfstr db "integer==%d, localint==%d, commvar=%d"
  77. db 10, 0
  78. ; some pointers
  79. localptr dd localint ; [5] [17]
  80. _textptr dd _greet wrt ..sym ; [15]
  81. _selfptr dd _selfptr wrt ..sym ; [16]
  82. SECTION .bss
  83. ; an integer
  84. _integer resd 1 ; [3]
  85. ; a local integer
  86. localint resd 1 ; [6]