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.
 
 
 
 
 
 

154 lines
4.3 KiB

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