1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // linux/mkall.go - Generates all Linux zsysnum, zsyscall, zerror, and ztype
6 // files for all Linux architectures supported by the go compiler. See
7 // README.md for more information about the build system.
9 // To run it you must have a git checkout of the Linux kernel and glibc. Once
10 // the appropriate sources are ready, the program is run as:
11 // go run linux/mkall.go <linux_dir> <glibc_dir>
34 // These will be paths to the appropriate source directories.
38 const TempDir = "/tmp"
39 const IncludeDir = TempDir + "/include" // To hold our C headers
40 const BuildDir = TempDir + "/build" // To hold intermediate build files
42 const GOOS = "linux" // Only for Linux targets
43 const BuildArch = "amd64" // Must be built on this architecture
44 const MinKernel = "2.6.23" // https://golang.org/doc/install#requirements
47 GoArch string // Architecture name according to Go
48 LinuxArch string // Architecture name according to the Linux Kernel
49 GNUArch string // Architecture name according to GNU tools (https://wiki.debian.org/Multiarch/Tuples)
50 BigEndian bool // Default Little Endian
51 SignedChar bool // Is -fsigned-char needed (default no)
55 // List of all Linux targets supported by the go compiler. Currently, riscv64
56 // and sparc64 are not fully supported, but there is enough support already to
57 // generate Go type and error definitions.
58 var targets = []target{
62 GNUArch: "i686-linux-gnu", // Note "i686" not "i386"
68 GNUArch: "x86_64-linux-gnu",
74 GNUArch: "aarch64-linux-gnu",
81 GNUArch: "arm-linux-gnueabi",
87 GNUArch: "mips-linux-gnu",
94 GNUArch: "mipsel-linux-gnu",
100 GNUArch: "mips64-linux-gnuabi64",
107 GNUArch: "mips64el-linux-gnuabi64",
112 LinuxArch: "powerpc",
113 GNUArch: "powerpc64-linux-gnu",
119 LinuxArch: "powerpc",
120 GNUArch: "powerpc64le-linux-gnu",
126 GNUArch: "riscv64-linux-gnu",
132 GNUArch: "s390x-linux-gnu",
140 GNUArch: "sparc64-linux-gnu",
146 // ptracePairs is a list of pairs of targets that can, in some cases,
147 // run each other's binaries.
148 var ptracePairs = []struct{ a1, a2 string }{
152 {"mipsle", "mips64le"},
156 if runtime.GOOS != GOOS || runtime.GOARCH != BuildArch {
157 fmt.Printf("Build system has GOOS_GOARCH = %s_%s, need %s_%s\n",
158 runtime.GOOS, runtime.GOARCH, GOOS, BuildArch)
162 // Check that we are using the new build system if we should
163 if os.Getenv("GOLANG_SYS_BUILD") != "docker" {
164 fmt.Println("In the new build system, mkall.go should not be called directly.")
165 fmt.Println("See README.md")
169 // Parse the command line options
170 if len(os.Args) != 3 {
171 fmt.Println("USAGE: go run linux/mkall.go <linux_dir> <glibc_dir>")
174 LinuxDir = os.Args[1]
175 GlibcDir = os.Args[2]
177 for _, t := range targets {
178 fmt.Printf("----- GENERATING: %s -----\n", t.GoArch)
179 if err := t.generateFiles(); err != nil {
180 fmt.Printf("%v\n***** FAILURE: %s *****\n\n", err, t.GoArch)
182 fmt.Printf("----- SUCCESS: %s -----\n\n", t.GoArch)
186 fmt.Printf("----- GENERATING ptrace pairs -----\n")
188 for _, p := range ptracePairs {
189 if err := generatePtracePair(p.a1, p.a2); err != nil {
190 fmt.Printf("%v\n***** FAILURE: %s/%s *****\n\n", err, p.a1, p.a2)
195 fmt.Printf("----- SUCCESS ptrace pairs -----\n\n")
199 // Makes an exec.Cmd with Stderr attached to os.Stderr
200 func makeCommand(name string, args ...string) *exec.Cmd {
201 cmd := exec.Command(name, args...)
202 cmd.Stderr = os.Stderr
206 // Set GOARCH for target and build environments.
207 func (t *target) setTargetBuildArch(cmd *exec.Cmd) {
208 // Set GOARCH_TARGET so command knows what GOARCH is..
209 cmd.Env = append(os.Environ(), "GOARCH_TARGET="+t.GoArch)
210 // Set GOARCH to host arch for command, so it can run natively.
211 for i, s := range cmd.Env {
212 if strings.HasPrefix(s, "GOARCH=") {
213 cmd.Env[i] = "GOARCH=" + BuildArch
218 // Runs the command, pipes output to a formatter, pipes that to an output file.
219 func (t *target) commandFormatOutput(formatter string, outputFile string,
220 name string, args ...string) (err error) {
221 mainCmd := makeCommand(name, args...)
222 if name == "mksyscall" {
223 args = append([]string{"run", "mksyscall.go"}, args...)
224 mainCmd = makeCommand("go", args...)
225 t.setTargetBuildArch(mainCmd)
226 } else if name == "mksysnum" {
227 args = append([]string{"run", "linux/mksysnum.go"}, args...)
228 mainCmd = makeCommand("go", args...)
229 t.setTargetBuildArch(mainCmd)
232 fmtCmd := makeCommand(formatter)
233 if formatter == "mkpost" {
234 fmtCmd = makeCommand("go", "run", "mkpost.go")
235 t.setTargetBuildArch(fmtCmd)
238 // mainCmd | fmtCmd > outputFile
239 if fmtCmd.Stdin, err = mainCmd.StdoutPipe(); err != nil {
242 if fmtCmd.Stdout, err = os.Create(outputFile); err != nil {
246 // Make sure the formatter eventually closes
247 if err = fmtCmd.Start(); err != nil {
251 fmtErr := fmtCmd.Wait()
260 // Generates all the files for a Linux target
261 func (t *target) generateFiles() error {
262 // Setup environment variables
263 os.Setenv("GOOS", GOOS)
264 os.Setenv("GOARCH", t.GoArch)
266 // Get appropriate compiler and emulator (unless on x86)
267 if t.LinuxArch != "x86" {
268 // Check/Setup cross compiler
269 compiler := t.GNUArch + "-gcc"
270 if _, err := exec.LookPath(compiler); err != nil {
273 os.Setenv("CC", compiler)
275 // Check/Setup emulator (usually first component of GNUArch)
276 qemuArchName := t.GNUArch[:strings.Index(t.GNUArch, "-")]
277 if t.LinuxArch == "powerpc" {
278 qemuArchName = t.GoArch
280 // Fake uname for QEMU to allow running on Host kernel version < 4.15
281 if t.LinuxArch == "riscv" {
282 os.Setenv("QEMU_UNAME", "4.15")
284 os.Setenv("GORUN", "qemu-"+qemuArchName)
286 os.Setenv("CC", "gcc")
289 // Make the include directory and fill it with headers
290 if err := os.MkdirAll(IncludeDir, os.ModePerm); err != nil {
293 defer os.RemoveAll(IncludeDir)
294 if err := t.makeHeaders(); err != nil {
295 return fmt.Errorf("could not make header files: %v", err)
297 fmt.Println("header files generated")
299 // Make each of the four files
300 if err := t.makeZSysnumFile(); err != nil {
301 return fmt.Errorf("could not make zsysnum file: %v", err)
303 fmt.Println("zsysnum file generated")
305 if err := t.makeZSyscallFile(); err != nil {
306 return fmt.Errorf("could not make zsyscall file: %v", err)
308 fmt.Println("zsyscall file generated")
310 if err := t.makeZTypesFile(); err != nil {
311 return fmt.Errorf("could not make ztypes file: %v", err)
313 fmt.Println("ztypes file generated")
315 if err := t.makeZErrorsFile(); err != nil {
316 return fmt.Errorf("could not make zerrors file: %v", err)
318 fmt.Println("zerrors file generated")
323 // Create the Linux, glibc and ABI (C compiler convention) headers in the include directory.
324 func (t *target) makeHeaders() error {
325 // Make the Linux headers we need for this architecture
326 linuxMake := makeCommand("make", "headers_install", "ARCH="+t.LinuxArch, "INSTALL_HDR_PATH="+TempDir)
327 linuxMake.Dir = LinuxDir
328 if err := linuxMake.Run(); err != nil {
332 // A Temporary build directory for glibc
333 if err := os.MkdirAll(BuildDir, os.ModePerm); err != nil {
336 defer os.RemoveAll(BuildDir)
338 // Make the glibc headers we need for this architecture
339 confScript := filepath.Join(GlibcDir, "configure")
340 glibcConf := makeCommand(confScript, "--prefix="+TempDir, "--host="+t.GNUArch, "--enable-kernel="+MinKernel)
341 glibcConf.Dir = BuildDir
342 if err := glibcConf.Run(); err != nil {
345 glibcMake := makeCommand("make", "install-headers")
346 glibcMake.Dir = BuildDir
347 if err := glibcMake.Run(); err != nil {
350 // We only need an empty stubs file
351 stubsFile := filepath.Join(IncludeDir, "gnu/stubs.h")
352 if file, err := os.Create(stubsFile); err != nil {
358 // ABI headers will specify C compiler behavior for the target platform.
359 return t.makeABIHeaders()
362 // makeABIHeaders generates C header files based on the platform's calling convention.
363 // While many platforms have formal Application Binary Interfaces, in practice, whatever the
364 // dominant C compilers generate is the de-facto calling convention.
366 // We generate C headers instead of a Go file, so as to enable references to the ABI from Cgo.
367 func (t *target) makeABIHeaders() (err error) {
368 abiDir := filepath.Join(IncludeDir, "abi")
369 if err = os.Mkdir(abiDir, os.ModePerm); err != nil {
373 cc := os.Getenv("CC")
375 return errors.New("CC (compiler) env var not set")
378 // Build a sacrificial ELF file, to mine for C compiler behavior.
379 binPath := filepath.Join(TempDir, "tmp_abi.o")
380 bin, err := t.buildELF(cc, cCode, binPath)
382 return fmt.Errorf("cannot build ELF to analyze: %v", err)
385 defer os.Remove(binPath)
387 // Right now, we put everything in abi.h, but we may change this later.
388 abiFile, err := os.Create(filepath.Join(abiDir, "abi.h"))
393 if cerr := abiFile.Close(); cerr != nil && err == nil {
398 if err = t.writeBitFieldMasks(bin, abiFile); err != nil {
399 return fmt.Errorf("cannot write bitfield masks: %v", err)
405 func (t *target) buildELF(cc, src, path string) (*elf.File, error) {
406 // Compile the cCode source using the set compiler - we will need its .data section.
407 // Do not link the binary, so that we can find .data section offsets from the symbol values.
408 ccCmd := makeCommand(cc, "-o", path, "-gdwarf", "-x", "c", "-c", "-")
409 ccCmd.Stdin = strings.NewReader(src)
410 ccCmd.Stdout = os.Stdout
411 if err := ccCmd.Run(); err != nil {
412 return nil, fmt.Errorf("compiler error: %v", err)
415 bin, err := elf.Open(path)
417 return nil, fmt.Errorf("cannot read ELF file %s: %v", path, err)
423 func (t *target) writeBitFieldMasks(bin *elf.File, out io.Writer) error {
424 symbols, err := bin.Symbols()
426 return fmt.Errorf("getting ELF symbols: %v", err)
428 var masksSym *elf.Symbol
430 for _, sym := range symbols {
431 if sym.Name == "masks" {
437 return errors.New("could not find the 'masks' symbol in ELF symtab")
440 dataSection := bin.Section(".data")
441 if dataSection == nil {
442 return errors.New("ELF file has no .data section")
445 data, err := dataSection.Data()
447 return fmt.Errorf("could not read .data section: %v\n", err)
450 var bo binary.ByteOrder
452 bo = binary.BigEndian
454 bo = binary.LittleEndian
457 // 64 bit masks of type uint64 are stored in the data section starting at masks.Value.
458 // Here we are running on AMD64, but these values may be big endian or little endian,
459 // depending on target architecture.
460 for i := uint64(0); i < 64; i++ {
461 off := masksSym.Value + i*8
462 // Define each mask in native by order, so as to match target endian.
463 fmt.Fprintf(out, "#define BITFIELD_MASK_%d %dULL\n", i, bo.Uint64(data[off:off+8]))
469 // makes the zsysnum_linux_$GOARCH.go file
470 func (t *target) makeZSysnumFile() error {
471 zsysnumFile := fmt.Sprintf("zsysnum_linux_%s.go", t.GoArch)
472 unistdFile := filepath.Join(IncludeDir, "asm/unistd.h")
474 args := append(t.cFlags(), unistdFile)
475 return t.commandFormatOutput("gofmt", zsysnumFile, "mksysnum", args...)
478 // makes the zsyscall_linux_$GOARCH.go file
479 func (t *target) makeZSyscallFile() error {
480 zsyscallFile := fmt.Sprintf("zsyscall_linux_%s.go", t.GoArch)
481 // Find the correct architecture syscall file (might end with x.go)
482 archSyscallFile := fmt.Sprintf("syscall_linux_%s.go", t.GoArch)
483 if _, err := os.Stat(archSyscallFile); os.IsNotExist(err) {
484 shortArch := strings.TrimSuffix(t.GoArch, "le")
485 archSyscallFile = fmt.Sprintf("syscall_linux_%sx.go", shortArch)
488 args := append(t.mksyscallFlags(), "-tags", "linux,"+t.GoArch,
489 "syscall_linux.go", archSyscallFile)
490 return t.commandFormatOutput("gofmt", zsyscallFile, "mksyscall", args...)
493 // makes the zerrors_linux_$GOARCH.go file
494 func (t *target) makeZErrorsFile() error {
495 zerrorsFile := fmt.Sprintf("zerrors_linux_%s.go", t.GoArch)
497 return t.commandFormatOutput("gofmt", zerrorsFile, "./mkerrors.sh", t.cFlags()...)
500 // makes the ztypes_linux_$GOARCH.go file
501 func (t *target) makeZTypesFile() error {
502 ztypesFile := fmt.Sprintf("ztypes_linux_%s.go", t.GoArch)
504 args := []string{"tool", "cgo", "-godefs", "--"}
505 args = append(args, t.cFlags()...)
506 args = append(args, "linux/types.go")
507 return t.commandFormatOutput("mkpost", ztypesFile, "go", args...)
510 // Flags that should be given to gcc and cgo for this target
511 func (t *target) cFlags() []string {
512 // Compile statically to avoid cross-architecture dynamic linking.
513 flags := []string{"-Wall", "-Werror", "-static", "-I" + IncludeDir}
515 // Architecture-specific flags
517 flags = append(flags, "-fsigned-char")
519 if t.LinuxArch == "x86" {
520 flags = append(flags, fmt.Sprintf("-m%d", t.Bits))
526 // Flags that should be given to mksyscall for this target
527 func (t *target) mksyscallFlags() (flags []string) {
530 flags = append(flags, "-b32")
532 flags = append(flags, "-l32")
536 // This flag means a 64-bit value should use (even, odd)-pair.
537 if t.GoArch == "arm" || (t.LinuxArch == "mips" && t.Bits == 32) {
538 flags = append(flags, "-arm")
543 // generatePtracePair takes a pair of GOARCH values that can run each
544 // other's binaries, such as 386 and amd64. It extracts the PtraceRegs
545 // type for each one. It writes a new file defining the types
546 // PtraceRegsArch1 and PtraceRegsArch2 and the corresponding functions
547 // Ptrace{Get,Set}Regs{arch1,arch2}. This permits debugging the other
548 // binary on a native system.
549 func generatePtracePair(arch1, arch2 string) error {
550 def1, err := ptraceDef(arch1)
554 def2, err := ptraceDef(arch2)
558 f, err := os.Create(fmt.Sprintf("zptrace%s_linux.go", arch1))
562 buf := bufio.NewWriter(f)
563 fmt.Fprintf(buf, "// Code generated by linux/mkall.go generatePtracePair(%s, %s). DO NOT EDIT.\n", arch1, arch2)
564 fmt.Fprintf(buf, "\n")
565 fmt.Fprintf(buf, "// +build linux\n")
566 fmt.Fprintf(buf, "// +build %s %s\n", arch1, arch2)
567 fmt.Fprintf(buf, "\n")
568 fmt.Fprintf(buf, "package unix\n")
569 fmt.Fprintf(buf, "\n")
570 fmt.Fprintf(buf, "%s\n", `import "unsafe"`)
571 fmt.Fprintf(buf, "\n")
572 writeOnePtrace(buf, arch1, def1)
573 fmt.Fprintf(buf, "\n")
574 writeOnePtrace(buf, arch2, def2)
575 if err := buf.Flush(); err != nil {
578 if err := f.Close(); err != nil {
584 // ptraceDef returns the definition of PtraceRegs for arch.
585 func ptraceDef(arch string) (string, error) {
586 filename := fmt.Sprintf("ztypes_linux_%s.go", arch)
587 data, err := ioutil.ReadFile(filename)
589 return "", fmt.Errorf("reading %s: %v", filename, err)
591 start := bytes.Index(data, []byte("type PtraceRegs struct"))
593 return "", fmt.Errorf("%s: no definition of PtraceRegs", filename)
596 end := bytes.Index(data, []byte("\n}\n"))
598 return "", fmt.Errorf("%s: can't find end of PtraceRegs definition", filename)
600 return string(data[:end+2]), nil
603 // writeOnePtrace writes out the ptrace definitions for arch.
604 func writeOnePtrace(w io.Writer, arch, def string) {
605 uarch := string(unicode.ToUpper(rune(arch[0]))) + arch[1:]
606 fmt.Fprintf(w, "// PtraceRegs%s is the registers used by %s binaries.\n", uarch, arch)
607 fmt.Fprintf(w, "%s\n", strings.Replace(def, "PtraceRegs", "PtraceRegs"+uarch, 1))
609 fmt.Fprintf(w, "// PtraceGetRegs%s fetches the registers used by %s binaries.\n", uarch, arch)
610 fmt.Fprintf(w, "func PtraceGetRegs%s(pid int, regsout *PtraceRegs%s) error {\n", uarch, uarch)
611 fmt.Fprintf(w, "\treturn ptrace(PTRACE_GETREGS, pid, 0, uintptr(unsafe.Pointer(regsout)))\n")
612 fmt.Fprintf(w, "}\n")
614 fmt.Fprintf(w, "// PtraceSetRegs%s sets the registers used by %s binaries.\n", uarch, arch)
615 fmt.Fprintf(w, "func PtraceSetRegs%s(pid int, regs *PtraceRegs%s) error {\n", uarch, uarch)
616 fmt.Fprintf(w, "\treturn ptrace(PTRACE_SETREGS, pid, 0, uintptr(unsafe.Pointer(regs)))\n")
617 fmt.Fprintf(w, "}\n")
620 // cCode is compiled for the target architecture, and the resulting data section is carved for
621 // the statically initialized bit masks.
623 // Bit fields are used in some system calls and other ABIs, but their memory layout is
624 // implementation-defined [1]. Even with formal ABIs, bit fields are a source of subtle bugs [2].
625 // Here we generate the offsets for all 64 bits in an uint64.
626 // 1: http://en.cppreference.com/w/c/language/bit_field
627 // 2: https://lwn.net/Articles/478657/
635 uint64_t u64_bit_0 : 1;
636 uint64_t u64_bit_1 : 1;
637 uint64_t u64_bit_2 : 1;
638 uint64_t u64_bit_3 : 1;
639 uint64_t u64_bit_4 : 1;
640 uint64_t u64_bit_5 : 1;
641 uint64_t u64_bit_6 : 1;
642 uint64_t u64_bit_7 : 1;
643 uint64_t u64_bit_8 : 1;
644 uint64_t u64_bit_9 : 1;
645 uint64_t u64_bit_10 : 1;
646 uint64_t u64_bit_11 : 1;
647 uint64_t u64_bit_12 : 1;
648 uint64_t u64_bit_13 : 1;
649 uint64_t u64_bit_14 : 1;
650 uint64_t u64_bit_15 : 1;
651 uint64_t u64_bit_16 : 1;
652 uint64_t u64_bit_17 : 1;
653 uint64_t u64_bit_18 : 1;
654 uint64_t u64_bit_19 : 1;
655 uint64_t u64_bit_20 : 1;
656 uint64_t u64_bit_21 : 1;
657 uint64_t u64_bit_22 : 1;
658 uint64_t u64_bit_23 : 1;
659 uint64_t u64_bit_24 : 1;
660 uint64_t u64_bit_25 : 1;
661 uint64_t u64_bit_26 : 1;
662 uint64_t u64_bit_27 : 1;
663 uint64_t u64_bit_28 : 1;
664 uint64_t u64_bit_29 : 1;
665 uint64_t u64_bit_30 : 1;
666 uint64_t u64_bit_31 : 1;
667 uint64_t u64_bit_32 : 1;
668 uint64_t u64_bit_33 : 1;
669 uint64_t u64_bit_34 : 1;
670 uint64_t u64_bit_35 : 1;
671 uint64_t u64_bit_36 : 1;
672 uint64_t u64_bit_37 : 1;
673 uint64_t u64_bit_38 : 1;
674 uint64_t u64_bit_39 : 1;
675 uint64_t u64_bit_40 : 1;
676 uint64_t u64_bit_41 : 1;
677 uint64_t u64_bit_42 : 1;
678 uint64_t u64_bit_43 : 1;
679 uint64_t u64_bit_44 : 1;
680 uint64_t u64_bit_45 : 1;
681 uint64_t u64_bit_46 : 1;
682 uint64_t u64_bit_47 : 1;
683 uint64_t u64_bit_48 : 1;
684 uint64_t u64_bit_49 : 1;
685 uint64_t u64_bit_50 : 1;
686 uint64_t u64_bit_51 : 1;
687 uint64_t u64_bit_52 : 1;
688 uint64_t u64_bit_53 : 1;
689 uint64_t u64_bit_54 : 1;
690 uint64_t u64_bit_55 : 1;
691 uint64_t u64_bit_56 : 1;
692 uint64_t u64_bit_57 : 1;
693 uint64_t u64_bit_58 : 1;
694 uint64_t u64_bit_59 : 1;
695 uint64_t u64_bit_60 : 1;
696 uint64_t u64_bit_61 : 1;
697 uint64_t u64_bit_62 : 1;
698 uint64_t u64_bit_63 : 1;
703 struct bitfield masks[] = {
770 int main(int argc, char **argv) {
771 struct bitfield *mask_ptr = &masks[0];
772 return mask_ptr->val;