github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/link/internal/ld/util.go (about) 1 // Copyright 2015 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 package ld 6 7 import ( 8 "cmd/link/internal/sym" 9 "encoding/binary" 10 "fmt" 11 "os" 12 "time" 13 ) 14 15 var startTime time.Time 16 17 // TODO(josharian): delete. See issue 19865. 18 func Cputime() float64 { 19 if startTime.IsZero() { 20 startTime = time.Now() 21 } 22 return time.Since(startTime).Seconds() 23 } 24 25 var atExitFuncs []func() 26 27 func AtExit(f func()) { 28 atExitFuncs = append(atExitFuncs, f) 29 } 30 31 // Exit exits with code after executing all atExitFuncs. 32 func Exit(code int) { 33 for i := len(atExitFuncs) - 1; i >= 0; i-- { 34 atExitFuncs[i]() 35 } 36 os.Exit(code) 37 } 38 39 // Exitf logs an error message then calls Exit(2). 40 func Exitf(format string, a ...interface{}) { 41 fmt.Fprintf(os.Stderr, os.Args[0]+": "+format+"\n", a...) 42 nerrors++ 43 Exit(2) 44 } 45 46 // Errorf logs an error message. 47 // 48 // If more than 20 errors have been printed, exit with an error. 49 // 50 // Logging an error means that on exit cmd/link will delete any 51 // output file and return a non-zero error code. 52 func Errorf(s *sym.Symbol, format string, args ...interface{}) { 53 if s != nil { 54 format = s.Name + ": " + format 55 } 56 format += "\n" 57 fmt.Fprintf(os.Stderr, format, args...) 58 nerrors++ 59 if *flagH { 60 panic("error") 61 } 62 if nerrors > 20 { 63 Exitf("too many errors") 64 } 65 } 66 67 func artrim(x []byte) string { 68 i := 0 69 j := len(x) 70 for i < len(x) && x[i] == ' ' { 71 i++ 72 } 73 for j > i && x[j-1] == ' ' { 74 j-- 75 } 76 return string(x[i:j]) 77 } 78 79 func stringtouint32(x []uint32, s string) { 80 for i := 0; len(s) > 0; i++ { 81 var buf [4]byte 82 s = s[copy(buf[:], s):] 83 x[i] = binary.LittleEndian.Uint32(buf[:]) 84 } 85 } 86 87 var start = time.Now() 88 89 func elapsed() float64 { 90 return time.Since(start).Seconds() 91 } 92 93 // contains reports whether v is in s. 94 func contains(s []string, v string) bool { 95 for _, x := range s { 96 if x == v { 97 return true 98 } 99 } 100 return false 101 }