hwcap.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // hwcap.go - ld.so.conf hwcap routines.
  2. // Copyright 2016 Yawning Angel
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. // DEALINGS IN THE SOFTWARE.
  21. package dynlib
  22. // #include <sys/auxv.h>
  23. //
  24. // static char * getPlatform() {
  25. // return (char *)getauxval(AT_PLATFORM);
  26. // }
  27. //
  28. import "C"
  29. import (
  30. "bytes"
  31. "syscall"
  32. )
  33. func getOsVersion() uint32 {
  34. var buf syscall.Utsname
  35. err := syscall.Uname(&buf)
  36. if err != nil {
  37. panic(err)
  38. }
  39. // Split into a slice of digits, stopping when the first non-digit is
  40. // encountered.
  41. var relBuf []byte
  42. for _, v := range buf.Release {
  43. if (v < '0' || v > '9') && v != '.' {
  44. break
  45. }
  46. relBuf = append(relBuf, byte(v))
  47. }
  48. // Parse major, minor, pl into bytes, and jam them together.
  49. //
  50. // glibc as far as I can tell doesn't handle any of versions being larger
  51. // than 256 at all.
  52. var ret uint32
  53. appended := uint(0)
  54. for i, v := range bytes.Split(relBuf, []byte{'.'}) {
  55. if i > 2 {
  56. break
  57. }
  58. var subVer uint8
  59. for _, b := range v {
  60. subVer = subVer * 10
  61. subVer = subVer + (b - '0')
  62. }
  63. ret = ret << 8
  64. ret = ret | uint32(subVer)
  65. appended++
  66. }
  67. return ret << (8 * (3 - appended))
  68. }