thunks_32.S 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * thunks_32.S - assembly helpers for mixed-bitness code
  3. * Copyright (c) 2015 Denys Vlasenko
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * These are little helpers that make it easier to switch bitness on
  15. * the fly.
  16. */
  17. .text
  18. .code32
  19. .global call64_from_32
  20. .type call32_from_64, @function
  21. // 4(%esp): function to call
  22. call64_from_32:
  23. // Fetch function address
  24. mov 4(%esp), %eax
  25. // Save registers which are callee-clobbered by 64-bit ABI
  26. push %ecx
  27. push %edx
  28. push %esi
  29. push %edi
  30. // Switch to long mode
  31. jmp $0x33,$1f
  32. 1: .code64
  33. // Call the function
  34. call *%rax
  35. // Switch to compatibility mode
  36. push $0x23 /* USER32_CS */
  37. .code32; push $1f; .code64 /* hack: can't have X86_64_32S relocation in 32-bit ELF */
  38. lretq
  39. 1: .code32
  40. pop %edi
  41. pop %esi
  42. pop %edx
  43. pop %ecx
  44. ret
  45. .size call64_from_32, .-call64_from_32