1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package obj
32
33
34
35
36
37 func brloop(p *Prog) *Prog {
38 c := 0
39 for q := p; q != nil; q = q.To.Target() {
40 if q.As != AJMP || q.To.Target() == nil {
41 return q
42 }
43 c++
44 if c >= 5000 {
45
46 return nil
47 }
48 }
49 panic("unreachable")
50 }
51
52
53 func checkaddr(ctxt *Link, p *Prog, a *Addr) {
54 switch a.Type {
55 case TYPE_NONE, TYPE_REGREG2, TYPE_REGLIST:
56 return
57
58 case TYPE_BRANCH, TYPE_TEXTSIZE:
59 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 {
60 break
61 }
62 return
63
64 case TYPE_MEM:
65 return
66
67 case TYPE_CONST:
68
69 if a.Name != 0 || a.Sym != nil || a.Reg != 0 {
70 ctxt.Diag("argument is TYPE_CONST, should be TYPE_ADDR, in %v", p)
71 return
72 }
73
74 if a.Reg != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
75 break
76 }
77 return
78
79 case TYPE_FCONST, TYPE_SCONST:
80 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Offset != 0 || a.Sym != nil {
81 break
82 }
83 return
84
85 case TYPE_REG:
86
87
88 if a.Scale != 0 || a.Name != 0 || a.Sym != nil {
89 break
90 }
91 return
92
93 case TYPE_ADDR:
94 if a.Val != nil {
95 break
96 }
97 if a.Reg == 0 && a.Index == 0 && a.Scale == 0 && a.Name == 0 && a.Sym == nil {
98 ctxt.Diag("argument is TYPE_ADDR, should be TYPE_CONST, in %v", p)
99 }
100 return
101
102 case TYPE_SHIFT, TYPE_REGREG:
103 if a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Sym != nil || a.Val != nil {
104 break
105 }
106 return
107
108 case TYPE_INDIR:
109
110
111 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name == 0 || a.Offset != 0 || a.Sym == nil || a.Val != nil {
112 break
113 }
114 return
115 }
116
117 ctxt.Diag("invalid encoding for argument %v", p)
118 }
119
120 func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
121 for p := sym.Func().Text; p != nil; p = p.Link {
122 checkaddr(ctxt, p, &p.From)
123 if p.GetFrom3() != nil {
124 checkaddr(ctxt, p, p.GetFrom3())
125 }
126 checkaddr(ctxt, p, &p.To)
127
128 if ctxt.Arch.Progedit != nil {
129 ctxt.Arch.Progedit(ctxt, p, newprog)
130 }
131 if p.To.Type != TYPE_BRANCH {
132 continue
133 }
134 if p.To.Val != nil {
135 continue
136 }
137
138 if p.To.Sym != nil {
139 continue
140 }
141 q := sym.Func().Text
142 for q != nil && p.To.Offset != q.Pc {
143 if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc {
144 q = q.Forwd
145 } else {
146 q = q.Link
147 }
148 }
149
150 if q == nil {
151 name := "<nil>"
152 if p.To.Sym != nil {
153 name = p.To.Sym.Name
154 }
155 ctxt.Diag("branch out of range (%#x)\n%v [%s]", uint32(p.To.Offset), p, name)
156 p.To.Type = TYPE_NONE
157 }
158
159 p.To.SetTarget(q)
160 }
161
162 if !ctxt.Flag_optimize {
163 return
164 }
165
166
167 for p := sym.Func().Text; p != nil; p = p.Link {
168 if p.To.Target() == nil {
169 continue
170 }
171 p.To.SetTarget(brloop(p.To.Target()))
172 if p.To.Target() != nil && p.To.Type == TYPE_BRANCH {
173 p.To.Offset = p.To.Target().Pc
174 }
175 }
176 }
177
View as plain text