Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

5 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Building `sys/unix`
  2. The sys/unix package provides access to the raw system call interface of the
  3. underlying operating system. See: https://godoc.org/golang.org/x/sys/unix
  4. Porting Go to a new architecture/OS combination or adding syscalls, types, or
  5. constants to an existing architecture/OS pair requires some manual effort;
  6. however, there are tools that automate much of the process.
  7. ## Build Systems
  8. There are currently two ways we generate the necessary files. We are currently
  9. migrating the build system to use containers so the builds are reproducible.
  10. This is being done on an OS-by-OS basis. Please update this documentation as
  11. components of the build system change.
  12. ### Old Build System (currently for `GOOS != "Linux" || GOARCH == "sparc64"`)
  13. The old build system generates the Go files based on the C header files
  14. present on your system. This means that files
  15. for a given GOOS/GOARCH pair must be generated on a system with that OS and
  16. architecture. This also means that the generated code can differ from system
  17. to system, based on differences in the header files.
  18. To avoid this, if you are using the old build system, only generate the Go
  19. files on an installation with unmodified header files. It is also important to
  20. keep track of which version of the OS the files were generated from (ex.
  21. Darwin 14 vs Darwin 15). This makes it easier to track the progress of changes
  22. and have each OS upgrade correspond to a single change.
  23. To build the files for your current OS and architecture, make sure GOOS and
  24. GOARCH are set correctly and run `mkall.sh`. This will generate the files for
  25. your specific system. Running `mkall.sh -n` shows the commands that will be run.
  26. Requirements: bash, perl, go
  27. ### New Build System (currently for `GOOS == "Linux" && GOARCH != "sparc64"`)
  28. The new build system uses a Docker container to generate the go files directly
  29. from source checkouts of the kernel and various system libraries. This means
  30. that on any platform that supports Docker, all the files using the new build
  31. system can be generated at once, and generated files will not change based on
  32. what the person running the scripts has installed on their computer.
  33. The OS specific files for the new build system are located in the `${GOOS}`
  34. directory, and the build is coordinated by the `${GOOS}/mkall.go` program. When
  35. the kernel or system library updates, modify the Dockerfile at
  36. `${GOOS}/Dockerfile` to checkout the new release of the source.
  37. To build all the files under the new build system, you must be on an amd64/Linux
  38. system and have your GOOS and GOARCH set accordingly. Running `mkall.sh` will
  39. then generate all of the files for all of the GOOS/GOARCH pairs in the new build
  40. system. Running `mkall.sh -n` shows the commands that will be run.
  41. Requirements: bash, perl, go, docker
  42. ## Component files
  43. This section describes the various files used in the code generation process.
  44. It also contains instructions on how to modify these files to add a new
  45. architecture/OS or to add additional syscalls, types, or constants. Note that
  46. if you are using the new build system, the scripts cannot be called normally.
  47. They must be called from within the docker container.
  48. ### asm files
  49. The hand-written assembly file at `asm_${GOOS}_${GOARCH}.s` implements system
  50. call dispatch. There are three entry points:
  51. ```
  52. func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
  53. func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr)
  54. func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr)
  55. ```
  56. The first and second are the standard ones; they differ only in how many
  57. arguments can be passed to the kernel. The third is for low-level use by the
  58. ForkExec wrapper. Unlike the first two, it does not call into the scheduler to
  59. let it know that a system call is running.
  60. When porting Go to an new architecture/OS, this file must be implemented for
  61. each GOOS/GOARCH pair.
  62. ### mksysnum
  63. Mksysnum is a script located at `${GOOS}/mksysnum.pl` (or `mksysnum_${GOOS}.pl`
  64. for the old system). This script takes in a list of header files containing the
  65. syscall number declarations and parses them to produce the corresponding list of
  66. Go numeric constants. See `zsysnum_${GOOS}_${GOARCH}.go` for the generated
  67. constants.
  68. Adding new syscall numbers is mostly done by running the build on a sufficiently
  69. new installation of the target OS (or updating the source checkouts for the
  70. new build system). However, depending on the OS, you make need to update the
  71. parsing in mksysnum.
  72. ### mksyscall.pl
  73. The `syscall.go`, `syscall_${GOOS}.go`, `syscall_${GOOS}_${GOARCH}.go` are
  74. hand-written Go files which implement system calls (for unix, the specific OS,
  75. or the specific OS/Architecture pair respectively) that need special handling
  76. and list `//sys` comments giving prototypes for ones that can be generated.
  77. The mksyscall.pl script takes the `//sys` and `//sysnb` comments and converts
  78. them into syscalls. This requires the name of the prototype in the comment to
  79. match a syscall number in the `zsysnum_${GOOS}_${GOARCH}.go` file. The function
  80. prototype can be exported (capitalized) or not.
  81. Adding a new syscall often just requires adding a new `//sys` function prototype
  82. with the desired arguments and a capitalized name so it is exported. However, if
  83. you want the interface to the syscall to be different, often one will make an
  84. unexported `//sys` prototype, an then write a custom wrapper in
  85. `syscall_${GOOS}.go`.
  86. ### types files
  87. For each OS, there is a hand-written Go file at `${GOOS}/types.go` (or
  88. `types_${GOOS}.go` on the old system). This file includes standard C headers and
  89. creates Go type aliases to the corresponding C types. The file is then fed
  90. through godef to get the Go compatible definitions. Finally, the generated code
  91. is fed though mkpost.go to format the code correctly and remove any hidden or
  92. private identifiers. This cleaned-up code is written to
  93. `ztypes_${GOOS}_${GOARCH}.go`.
  94. The hardest part about preparing this file is figuring out which headers to
  95. include and which symbols need to be `#define`d to get the actual data
  96. structures that pass through to the kernel system calls. Some C libraries
  97. preset alternate versions for binary compatibility and translate them on the
  98. way in and out of system calls, but there is almost always a `#define` that can
  99. get the real ones.
  100. See `types_darwin.go` and `linux/types.go` for examples.
  101. To add a new type, add in the necessary include statement at the top of the
  102. file (if it is not already there) and add in a type alias line. Note that if
  103. your type is significantly different on different architectures, you may need
  104. some `#if/#elif` macros in your include statements.
  105. ### mkerrors.sh
  106. This script is used to generate the system's various constants. This doesn't
  107. just include the error numbers and error strings, but also the signal numbers
  108. an a wide variety of miscellaneous constants. The constants come from the list
  109. of include files in the `includes_${uname}` variable. A regex then picks out
  110. the desired `#define` statements, and generates the corresponding Go constants.
  111. The error numbers and strings are generated from `#include <errno.h>`, and the
  112. signal numbers and strings are generated from `#include <signal.h>`. All of
  113. these constants are written to `zerrors_${GOOS}_${GOARCH}.go` via a C program,
  114. `_errors.c`, which prints out all the constants.
  115. To add a constant, add the header that includes it to the appropriate variable.
  116. Then, edit the regex (if necessary) to match the desired constant. Avoid making
  117. the regex too broad to avoid matching unintended constants.
  118. ## Generated files
  119. ### `zerror_${GOOS}_${GOARCH}.go`
  120. A file containing all of the system's generated error numbers, error strings,
  121. signal numbers, and constants. Generated by `mkerrors.sh` (see above).
  122. ### `zsyscall_${GOOS}_${GOARCH}.go`
  123. A file containing all the generated syscalls for a specific GOOS and GOARCH.
  124. Generated by `mksyscall.pl` (see above).
  125. ### `zsysnum_${GOOS}_${GOARCH}.go`
  126. A list of numeric constants for all the syscall number of the specific GOOS
  127. and GOARCH. Generated by mksysnum (see above).
  128. ### `ztypes_${GOOS}_${GOARCH}.go`
  129. A file containing Go types for passing into (or returning from) syscalls.
  130. Generated by godefs and the types file (see above).