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 case TYPE_SPECIAL:
116 if a.Reg != 0 || a.Index != 0 || a.Scale != 0 || a.Name != 0 || a.Class != 0 || a.Sym != nil {
117 break
118 }
119 return
120 }
121
122 ctxt.Diag("invalid encoding for argument %v", p)
123 }
124
125 func linkpatch(ctxt *Link, sym *LSym, newprog ProgAlloc) {
126 for p := sym.Func().Text; p != nil; p = p.Link {
127 checkaddr(ctxt, p, &p.From)
128 for _, v := range p.RestArgs {
129 checkaddr(ctxt, p, &v.Addr)
130 }
131 checkaddr(ctxt, p, &p.To)
132
133 if ctxt.Arch.Progedit != nil {
134 ctxt.Arch.Progedit(ctxt, p, newprog)
135 }
136 if p.To.Type != TYPE_BRANCH {
137 continue
138 }
139 if p.To.Val != nil {
140 continue
141 }
142
143 if p.To.Sym != nil {
144 continue
145 }
146 q := sym.Func().Text
147 for q != nil && p.To.Offset != q.Pc {
148 if q.Forwd != nil && p.To.Offset >= q.Forwd.Pc {
149 q = q.Forwd
150 } else {
151 q = q.Link
152 }
153 }
154
155 if q == nil {
156 name := "<nil>"
157 if p.To.Sym != nil {
158 name = p.To.Sym.Name
159 }
160 ctxt.Diag("branch out of range (%#x)\n%v [%s]", uint32(p.To.Offset), p, name)
161 p.To.Type = TYPE_NONE
162 }
163
164 p.To.SetTarget(q)
165 }
166
167 if !ctxt.Flag_optimize {
168 return
169 }
170
171
172 for p := sym.Func().Text; p != nil; p = p.Link {
173 if p.To.Target() == nil {
174 continue
175 }
176 p.To.SetTarget(brloop(p.To.Target()))
177 if p.To.Target() != nil && p.To.Type == TYPE_BRANCH {
178 p.To.Offset = p.To.Target().Pc
179 }
180 }
181 }
182
View as plain text