You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chacha_vec_arm_generate.go 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2014, Google Inc.
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. // This package generates chacha_vec_arm.S from chacha_vec.c.
  15. package main
  16. import (
  17. "bufio"
  18. "bytes"
  19. "os"
  20. "os/exec"
  21. "strings"
  22. )
  23. const defaultCompiler = "/opt/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc"
  24. func main() {
  25. compiler := defaultCompiler
  26. if len(os.Args) > 1 {
  27. compiler = os.Args[1]
  28. }
  29. args := []string{
  30. "-O3",
  31. "-mcpu=cortex-a8",
  32. "-mfpu=neon",
  33. "-fpic",
  34. "-DASM_GEN",
  35. "-I", "../../include",
  36. "-S", "chacha_vec.c",
  37. "-o", "-",
  38. }
  39. output, err := os.OpenFile("chacha_vec_arm.S", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
  40. if err != nil {
  41. panic(err)
  42. }
  43. defer output.Close()
  44. output.WriteString(preamble)
  45. output.WriteString(compiler)
  46. output.WriteString(" ")
  47. output.WriteString(strings.Join(args, " "))
  48. output.WriteString("\n\n#if !defined(OPENSSL_NO_ASM)\n")
  49. output.WriteString("#if defined(__arm__) || defined(__aarch64__)\n\n")
  50. cmd := exec.Command(compiler, args...)
  51. cmd.Stderr = os.Stderr
  52. asm, err := cmd.StdoutPipe()
  53. if err != nil {
  54. panic(err)
  55. }
  56. if err := cmd.Start(); err != nil {
  57. panic(err)
  58. }
  59. attr28 := []byte(".eabi_attribute 28,")
  60. globalDirective := []byte(".global\t")
  61. newLine := []byte("\n")
  62. attr28Handled := false
  63. scanner := bufio.NewScanner(asm)
  64. for scanner.Scan() {
  65. line := scanner.Bytes()
  66. if bytes.Contains(line, attr28) {
  67. output.WriteString(attr28Block)
  68. attr28Handled = true
  69. continue
  70. }
  71. output.Write(line)
  72. output.Write(newLine)
  73. if i := bytes.Index(line, globalDirective); i >= 0 {
  74. output.Write(line[:i])
  75. output.WriteString(".hidden\t")
  76. output.Write(line[i+len(globalDirective):])
  77. output.Write(newLine)
  78. }
  79. }
  80. if err := scanner.Err(); err != nil {
  81. panic(err)
  82. }
  83. if !attr28Handled {
  84. panic("EABI attribute 28 not seen in processing")
  85. }
  86. if err := cmd.Wait(); err != nil {
  87. panic(err)
  88. }
  89. output.WriteString(trailer)
  90. }
  91. const preamble = `# Copyright (c) 2014, Google Inc.
  92. #
  93. # Permission to use, copy, modify, and/or distribute this software for any
  94. # purpose with or without fee is hereby granted, provided that the above
  95. # copyright notice and this permission notice appear in all copies.
  96. #
  97. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  98. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  99. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  100. # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  101. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  102. # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  103. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  104. # This file contains a pre-compiled version of chacha_vec.c for ARM. This is
  105. # needed to support switching on NEON code at runtime. If the whole of OpenSSL
  106. # were to be compiled with the needed flags to build chacha_vec.c, then it
  107. # wouldn't be possible to run on non-NEON systems.
  108. #
  109. # This file was generated by chacha_vec_arm_generate.go using the following
  110. # compiler command:
  111. #
  112. # `
  113. const attr28Block = `
  114. # EABI attribute 28 sets whether VFP register arguments were used to build this
  115. # file. If object files are inconsistent on this point, the linker will refuse
  116. # to link them. Thus we report whatever the compiler expects since we don't use
  117. # VFP arguments.
  118. #if defined(__ARM_PCS_VFP)
  119. .eabi_attribute 28, 1
  120. #else
  121. .eabi_attribute 28, 0
  122. #endif
  123. `
  124. const trailer = `
  125. #endif /* __arm__ || __aarch64__ */
  126. #endif /* !OPENSSL_NO_ASM */
  127. `