amx_base.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2011-2012 Zeex
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined AMX_BASE_INC
  21. #endinput
  22. #endif
  23. #define AMX_BASE_INC
  24. static stock GetAmxBaseAddress_helper() {
  25. return 0;
  26. }
  27. // Returns the AMX base address i.e. amx->base.
  28. stock GetAmxBaseAddressNow() {
  29. new cod, dat;
  30. #emit lctrl 0
  31. #emit stor.s.pri cod
  32. #emit lctrl 1
  33. #emit stor.s.pri dat
  34. // Get code section start address relative to data.
  35. new code_start = cod - dat;
  36. // Get address of GetAmxBaseAddress_helper().
  37. new fn_addr;
  38. #emit const.pri GetAmxBaseAddress_helper
  39. #emit stor.s.pri fn_addr
  40. // Get absolute address from the CALL instruction.
  41. new fn_addr_reloc, call_addr;
  42. GetAmxBaseAddress_helper();
  43. #emit lctrl 6
  44. #emit stor.s.pri call_addr
  45. call_addr = call_addr - 12 + code_start;
  46. #emit lref.s.pri call_addr
  47. #emit stor.s.pri fn_addr_reloc
  48. return fn_addr_reloc - fn_addr - cod;
  49. }
  50. stock GetAmxBaseAddress() {
  51. static amx_base = 0;
  52. if (amx_base == 0) {
  53. amx_base = GetAmxBaseAddressNow();
  54. }
  55. return amx_base;
  56. }