github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/pkg/mod/golang.org/x/sys@v0.0.0-20210927094055-39ccf1dd6fa6/unix/mksyscall_solaris.go (about) 1 // Copyright 2019 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. 4 5 //go:build ignore 6 // +build ignore 7 8 /* 9 This program reads a file containing function prototypes 10 (like syscall_solaris.go) and generates system call bodies. 11 The prototypes are marked by lines beginning with "//sys" 12 and read like func declarations if //sys is replaced by func, but: 13 * The parameter lists must give a name for each argument. 14 This includes return parameters. 15 * The parameter lists must give a type for each argument: 16 the (x, y, z int) shorthand is not allowed. 17 * If the return parameter is an error number, it must be named err. 18 * If go func name needs to be different than its libc name, 19 * or the function is not in libc, name could be specified 20 * at the end, after "=" sign, like 21 //sys getsockopt(s int, level int, name int, val uintptr, vallen *_Socklen) (err error) = libsocket.getsockopt 22 */ 23 24 package main 25 26 import ( 27 "bufio" 28 "flag" 29 "fmt" 30 "os" 31 "regexp" 32 "strings" 33 ) 34 35 var ( 36 b32 = flag.Bool("b32", false, "32bit big-endian") 37 l32 = flag.Bool("l32", false, "32bit little-endian") 38 tags = flag.String("tags", "", "build tags") 39 illumos = flag.Bool("illumos", false, "illumos specific code generation") 40 ) 41 42 // cmdLine returns this programs's commandline arguments 43 func cmdLine() string { 44 return "go run mksyscall_solaris.go " + strings.Join(os.Args[1:], " ") 45 } 46 47 // goBuildTags returns build tags in the go:build format. 48 func goBuildTags() string { 49 return strings.ReplaceAll(*tags, ",", " && ") 50 } 51 52 // plusBuildTags returns build tags in the +build format. 53 func plusBuildTags() string { 54 return *tags 55 } 56 57 // Param is function parameter 58 type Param struct { 59 Name string 60 Type string 61 } 62 63 // usage prints the program usage 64 func usage() { 65 fmt.Fprintf(os.Stderr, "usage: go run mksyscall_solaris.go [-b32 | -l32] [-tags x,y] [file ...]\n") 66 os.Exit(1) 67 } 68 69 // parseParamList parses parameter list and returns a slice of parameters 70 func parseParamList(list string) []string { 71 list = strings.TrimSpace(list) 72 if list == "" { 73 return []string{} 74 } 75 return regexp.MustCompile(`\s*,\s*`).Split(list, -1) 76 } 77 78 // parseParam splits a parameter into name and type 79 func parseParam(p string) Param { 80 ps := regexp.MustCompile(`^(\S*) (\S*)$`).FindStringSubmatch(p) 81 if ps == nil { 82 fmt.Fprintf(os.Stderr, "malformed parameter: %s\n", p) 83 os.Exit(1) 84 } 85 return Param{ps[1], ps[2]} 86 } 87 88 func main() { 89 flag.Usage = usage 90 flag.Parse() 91 if len(flag.Args()) <= 0 { 92 fmt.Fprintf(os.Stderr, "no files to parse provided\n") 93 usage() 94 } 95 96 endianness := "" 97 if *b32 { 98 endianness = "big-endian" 99 } else if *l32 { 100 endianness = "little-endian" 101 } 102 103 pack := "" 104 text := "" 105 dynimports := "" 106 linknames := "" 107 var vars []string 108 for _, path := range flag.Args() { 109 file, err := os.Open(path) 110 if err != nil { 111 fmt.Fprintf(os.Stderr, err.Error()) 112 os.Exit(1) 113 } 114 s := bufio.NewScanner(file) 115 for s.Scan() { 116 t := s.Text() 117 if p := regexp.MustCompile(`^package (\S+)$`).FindStringSubmatch(t); p != nil && pack == "" { 118 pack = p[1] 119 } 120 nonblock := regexp.MustCompile(`^\/\/sysnb\t`).FindStringSubmatch(t) 121 if regexp.MustCompile(`^\/\/sys\t`).FindStringSubmatch(t) == nil && nonblock == nil { 122 continue 123 } 124 125 // Line must be of the form 126 // func Open(path string, mode int, perm int) (fd int, err error) 127 // Split into name, in params, out params. 128 f := regexp.MustCompile(`^\/\/sys(nb)?\t(\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*(?:(\w*)\.)?(\w*))?$`).FindStringSubmatch(t) 129 if f == nil { 130 fmt.Fprintf(os.Stderr, "%s:%s\nmalformed //sys declaration\n", path, t) 131 os.Exit(1) 132 } 133 funct, inps, outps, modname, sysname := f[2], f[3], f[4], f[5], f[6] 134 135 // Split argument lists on comma. 136 in := parseParamList(inps) 137 out := parseParamList(outps) 138 139 inps = strings.Join(in, ", ") 140 outps = strings.Join(out, ", ") 141 142 // Try in vain to keep people from editing this file. 143 // The theory is that they jump into the middle of the file 144 // without reading the header. 145 text += "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n" 146 147 // So file name. 148 if modname == "" { 149 modname = "libc" 150 } 151 152 // System call name. 153 if sysname == "" { 154 sysname = funct 155 } 156 157 // System call pointer variable name. 158 sysvarname := fmt.Sprintf("proc%s", sysname) 159 160 strconvfunc := "BytePtrFromString" 161 strconvtype := "*byte" 162 163 sysname = strings.ToLower(sysname) // All libc functions are lowercase. 164 165 // Runtime import of function to allow cross-platform builds. 166 dynimports += fmt.Sprintf("//go:cgo_import_dynamic libc_%s %s \"%s.so\"\n", sysname, sysname, modname) 167 // Link symbol to proc address variable. 168 linknames += fmt.Sprintf("//go:linkname %s libc_%s\n", sysvarname, sysname) 169 // Library proc address variable. 170 vars = append(vars, sysvarname) 171 172 // Go function header. 173 outlist := strings.Join(out, ", ") 174 if outlist != "" { 175 outlist = fmt.Sprintf(" (%s)", outlist) 176 } 177 if text != "" { 178 text += "\n" 179 } 180 text += fmt.Sprintf("func %s(%s)%s {\n", funct, strings.Join(in, ", "), outlist) 181 182 // Check if err return available 183 errvar := "" 184 for _, param := range out { 185 p := parseParam(param) 186 if p.Type == "error" { 187 errvar = p.Name 188 continue 189 } 190 } 191 192 // Prepare arguments to Syscall. 193 var args []string 194 n := 0 195 for _, param := range in { 196 p := parseParam(param) 197 if regexp.MustCompile(`^\*`).FindStringSubmatch(p.Type) != nil { 198 args = append(args, "uintptr(unsafe.Pointer("+p.Name+"))") 199 } else if p.Type == "string" && errvar != "" { 200 text += fmt.Sprintf("\tvar _p%d %s\n", n, strconvtype) 201 text += fmt.Sprintf("\t_p%d, %s = %s(%s)\n", n, errvar, strconvfunc, p.Name) 202 text += fmt.Sprintf("\tif %s != nil {\n\t\treturn\n\t}\n", errvar) 203 args = append(args, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n)) 204 n++ 205 } else if p.Type == "string" { 206 fmt.Fprintf(os.Stderr, path+":"+funct+" uses string arguments, but has no error return\n") 207 text += fmt.Sprintf("\tvar _p%d %s\n", n, strconvtype) 208 text += fmt.Sprintf("\t_p%d, _ = %s(%s)\n", n, strconvfunc, p.Name) 209 args = append(args, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n)) 210 n++ 211 } else if s := regexp.MustCompile(`^\[\](.*)`).FindStringSubmatch(p.Type); s != nil { 212 // Convert slice into pointer, length. 213 // Have to be careful not to take address of &a[0] if len == 0: 214 // pass nil in that case. 215 text += fmt.Sprintf("\tvar _p%d *%s\n", n, s[1]) 216 text += fmt.Sprintf("\tif len(%s) > 0 {\n\t\t_p%d = &%s[0]\n\t}\n", p.Name, n, p.Name) 217 args = append(args, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n), fmt.Sprintf("uintptr(len(%s))", p.Name)) 218 n++ 219 } else if p.Type == "int64" && endianness != "" { 220 if endianness == "big-endian" { 221 args = append(args, fmt.Sprintf("uintptr(%s>>32)", p.Name), fmt.Sprintf("uintptr(%s)", p.Name)) 222 } else { 223 args = append(args, fmt.Sprintf("uintptr(%s)", p.Name), fmt.Sprintf("uintptr(%s>>32)", p.Name)) 224 } 225 } else if p.Type == "bool" { 226 text += fmt.Sprintf("\tvar _p%d uint32\n", n) 227 text += fmt.Sprintf("\tif %s {\n\t\t_p%d = 1\n\t} else {\n\t\t_p%d = 0\n\t}\n", p.Name, n, n) 228 args = append(args, fmt.Sprintf("uintptr(_p%d)", n)) 229 n++ 230 } else { 231 args = append(args, fmt.Sprintf("uintptr(%s)", p.Name)) 232 } 233 } 234 nargs := len(args) 235 236 // Determine which form to use; pad args with zeros. 237 asm := "sysvicall6" 238 if nonblock != nil { 239 asm = "rawSysvicall6" 240 } 241 if len(args) <= 6 { 242 for len(args) < 6 { 243 args = append(args, "0") 244 } 245 } else { 246 fmt.Fprintf(os.Stderr, "%s: too many arguments to system call\n", path) 247 os.Exit(1) 248 } 249 250 // Actual call. 251 arglist := strings.Join(args, ", ") 252 call := fmt.Sprintf("%s(uintptr(unsafe.Pointer(&%s)), %d, %s)", asm, sysvarname, nargs, arglist) 253 254 // Assign return values. 255 body := "" 256 ret := []string{"_", "_", "_"} 257 doErrno := false 258 for i := 0; i < len(out); i++ { 259 p := parseParam(out[i]) 260 reg := "" 261 if p.Name == "err" { 262 reg = "e1" 263 ret[2] = reg 264 doErrno = true 265 } else { 266 reg = fmt.Sprintf("r%d", i) 267 ret[i] = reg 268 } 269 if p.Type == "bool" { 270 reg = fmt.Sprintf("%d != 0", reg) 271 } 272 if p.Type == "int64" && endianness != "" { 273 // 64-bit number in r1:r0 or r0:r1. 274 if i+2 > len(out) { 275 fmt.Fprintf(os.Stderr, "%s: not enough registers for int64 return\n", path) 276 os.Exit(1) 277 } 278 if endianness == "big-endian" { 279 reg = fmt.Sprintf("int64(r%d)<<32 | int64(r%d)", i, i+1) 280 } else { 281 reg = fmt.Sprintf("int64(r%d)<<32 | int64(r%d)", i+1, i) 282 } 283 ret[i] = fmt.Sprintf("r%d", i) 284 ret[i+1] = fmt.Sprintf("r%d", i+1) 285 } 286 if reg != "e1" { 287 body += fmt.Sprintf("\t%s = %s(%s)\n", p.Name, p.Type, reg) 288 } 289 } 290 if ret[0] == "_" && ret[1] == "_" && ret[2] == "_" { 291 text += fmt.Sprintf("\t%s\n", call) 292 } else { 293 text += fmt.Sprintf("\t%s, %s, %s := %s\n", ret[0], ret[1], ret[2], call) 294 } 295 text += body 296 297 if doErrno { 298 text += "\tif e1 != 0 {\n" 299 text += "\t\terr = e1\n" 300 text += "\t}\n" 301 } 302 text += "\treturn\n" 303 text += "}\n" 304 } 305 if err := s.Err(); err != nil { 306 fmt.Fprintf(os.Stderr, err.Error()) 307 os.Exit(1) 308 } 309 file.Close() 310 } 311 imp := "" 312 if pack != "unix" { 313 imp = "import \"golang.org/x/sys/unix\"\n" 314 } 315 316 syscallimp := "" 317 if !*illumos { 318 syscallimp = "\"syscall\"" 319 } 320 321 vardecls := "\t" + strings.Join(vars, ",\n\t") 322 vardecls += " syscallFunc" 323 fmt.Printf(srcTemplate, cmdLine(), goBuildTags(), plusBuildTags(), pack, syscallimp, imp, dynimports, linknames, vardecls, text) 324 } 325 326 const srcTemplate = `// %s 327 // Code generated by the command above; see README.md. DO NOT EDIT. 328 329 //go:build %s 330 // +build %s 331 332 package %s 333 334 import ( 335 "unsafe" 336 %s 337 ) 338 %s 339 %s 340 %s 341 var ( 342 %s 343 ) 344 345 %s 346 `