1
2
3
4
5 package ld
6
7 import (
8 "cmd/link/internal/loader"
9 "encoding/binary"
10 "fmt"
11 "os"
12 )
13
14 var atExitFuncs []func()
15
16 func AtExit(f func()) {
17 atExitFuncs = append(atExitFuncs, f)
18 }
19
20
21 func runAtExitFuncs() {
22 for i := len(atExitFuncs) - 1; i >= 0; i-- {
23 atExitFuncs[i]()
24 }
25 atExitFuncs = nil
26 }
27
28
29 func Exit(code int) {
30 runAtExitFuncs()
31 os.Exit(code)
32 }
33
34
35 func Exitf(format string, a ...interface{}) {
36 fmt.Fprintf(os.Stderr, os.Args[0]+": "+format+"\n", a...)
37 nerrors++
38 if *flagH {
39 panic("error")
40 }
41 Exit(2)
42 }
43
44
45
46 func afterErrorAction() {
47 nerrors++
48 if *flagH {
49 panic("error")
50 }
51 if nerrors > 20 {
52 Exitf("too many errors")
53 }
54 }
55
56
57
58
59
60
61
62
63 func Errorf(format string, args ...interface{}) {
64 format += "\n"
65 fmt.Fprintf(os.Stderr, format, args...)
66 afterErrorAction()
67 }
68
69
70
71
72
73
74
75 func (ctxt *Link) Errorf(s loader.Sym, format string, args ...interface{}) {
76 if ctxt.loader != nil {
77 ctxt.loader.Errorf(s, format, args...)
78 return
79 }
80
81 format = fmt.Sprintf("sym %d: %s", s, format)
82 format += "\n"
83 fmt.Fprintf(os.Stderr, format, args...)
84 afterErrorAction()
85 }
86
87 func artrim(x []byte) string {
88 i := 0
89 j := len(x)
90 for i < len(x) && x[i] == ' ' {
91 i++
92 }
93 for j > i && x[j-1] == ' ' {
94 j--
95 }
96 return string(x[i:j])
97 }
98
99 func stringtouint32(x []uint32, s string) {
100 for i := 0; len(s) > 0; i++ {
101 var buf [4]byte
102 s = s[copy(buf[:], s):]
103 x[i] = binary.LittleEndian.Uint32(buf[:])
104 }
105 }
106
View as plain text