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 package work
31
32 import (
33 "fmt"
34 "internal/lazyregexp"
35 "regexp"
36 "strings"
37
38 "cmd/go/internal/cfg"
39 "cmd/go/internal/load"
40 )
41
42 var re = lazyregexp.New
43
44 var validCompilerFlags = []*lazyregexp.Regexp{
45 re(`-D([A-Za-z_][A-Za-z0-9_]*)(=[^@\-]*)?`),
46 re(`-U([A-Za-z_][A-Za-z0-9_]*)`),
47 re(`-F([^@\-].*)`),
48 re(`-I([^@\-].*)`),
49 re(`-O`),
50 re(`-O([^@\-].*)`),
51 re(`-W`),
52 re(`-W([^@,]+)`),
53 re(`-Wa,-mbig-obj`),
54 re(`-Wp,-D([A-Za-z_][A-Za-z0-9_]*)(=[^@,\-]*)?`),
55 re(`-Wp,-U([A-Za-z_][A-Za-z0-9_]*)`),
56 re(`-ansi`),
57 re(`-f(no-)?asynchronous-unwind-tables`),
58 re(`-f(no-)?blocks`),
59 re(`-f(no-)builtin-[a-zA-Z0-9_]*`),
60 re(`-f(no-)?common`),
61 re(`-f(no-)?constant-cfstrings`),
62 re(`-fdiagnostics-show-note-include-stack`),
63 re(`-f(no-)?eliminate-unused-debug-types`),
64 re(`-f(no-)?exceptions`),
65 re(`-f(no-)?fast-math`),
66 re(`-f(no-)?inline-functions`),
67 re(`-finput-charset=([^@\-].*)`),
68 re(`-f(no-)?fat-lto-objects`),
69 re(`-f(no-)?keep-inline-dllexport`),
70 re(`-f(no-)?lto`),
71 re(`-fmacro-backtrace-limit=(.+)`),
72 re(`-fmessage-length=(.+)`),
73 re(`-f(no-)?modules`),
74 re(`-f(no-)?objc-arc`),
75 re(`-f(no-)?objc-nonfragile-abi`),
76 re(`-f(no-)?objc-legacy-dispatch`),
77 re(`-f(no-)?omit-frame-pointer`),
78 re(`-f(no-)?openmp(-simd)?`),
79 re(`-f(no-)?permissive`),
80 re(`-f(no-)?(pic|PIC|pie|PIE)`),
81 re(`-f(no-)?plt`),
82 re(`-f(no-)?rtti`),
83 re(`-f(no-)?split-stack`),
84 re(`-f(no-)?stack-(.+)`),
85 re(`-f(no-)?strict-aliasing`),
86 re(`-f(un)signed-char`),
87 re(`-f(no-)?use-linker-plugin`),
88 re(`-f(no-)?visibility-inlines-hidden`),
89 re(`-fsanitize=(.+)`),
90 re(`-ftemplate-depth-(.+)`),
91 re(`-fvisibility=(.+)`),
92 re(`-g([^@\-].*)?`),
93 re(`-m32`),
94 re(`-m64`),
95 re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`),
96 re(`-m(no-)?v?aes`),
97 re(`-marm`),
98 re(`-m(no-)?avx[0-9a-z]*`),
99 re(`-mfloat-abi=([^@\-].*)`),
100 re(`-mfpmath=[0-9a-z,+]*`),
101 re(`-m(no-)?avx[0-9a-z.]*`),
102 re(`-m(no-)?ms-bitfields`),
103 re(`-m(no-)?stack-(.+)`),
104 re(`-mmacosx-(.+)`),
105 re(`-mios-simulator-version-min=(.+)`),
106 re(`-miphoneos-version-min=(.+)`),
107 re(`-mtvos-simulator-version-min=(.+)`),
108 re(`-mtvos-version-min=(.+)`),
109 re(`-mwatchos-simulator-version-min=(.+)`),
110 re(`-mwatchos-version-min=(.+)`),
111 re(`-mnop-fun-dllimport`),
112 re(`-m(no-)?sse[0-9.]*`),
113 re(`-m(no-)?ssse3`),
114 re(`-mthumb(-interwork)?`),
115 re(`-mthreads`),
116 re(`-mwindows`),
117 re(`--param=ssp-buffer-size=[0-9]*`),
118 re(`-pedantic(-errors)?`),
119 re(`-pipe`),
120 re(`-pthread`),
121 re(`-?-std=([^@\-].*)`),
122 re(`-?-stdlib=([^@\-].*)`),
123 re(`--sysroot=([^@\-].*)`),
124 re(`-w`),
125 re(`-x([^@\-].*)`),
126 re(`-v`),
127 }
128
129 var validCompilerFlagsWithNextArg = []string{
130 "-arch",
131 "-D",
132 "-U",
133 "-I",
134 "-F",
135 "-framework",
136 "-include",
137 "-isysroot",
138 "-isystem",
139 "--sysroot",
140 "-target",
141 "-x",
142 }
143
144 var validLinkerFlags = []*lazyregexp.Regexp{
145 re(`-F([^@\-].*)`),
146 re(`-l([^@\-].*)`),
147 re(`-L([^@\-].*)`),
148 re(`-O`),
149 re(`-O([^@\-].*)`),
150 re(`-f(no-)?(pic|PIC|pie|PIE)`),
151 re(`-f(no-)?openmp(-simd)?`),
152 re(`-fsanitize=([^@\-].*)`),
153 re(`-flat_namespace`),
154 re(`-g([^@\-].*)?`),
155 re(`-headerpad_max_install_names`),
156 re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`),
157 re(`-mfloat-abi=([^@\-].*)`),
158 re(`-mmacosx-(.+)`),
159 re(`-mios-simulator-version-min=(.+)`),
160 re(`-miphoneos-version-min=(.+)`),
161 re(`-mthreads`),
162 re(`-mwindows`),
163 re(`-(pic|PIC|pie|PIE)`),
164 re(`-pthread`),
165 re(`-rdynamic`),
166 re(`-shared`),
167 re(`-?-static([-a-z0-9+]*)`),
168 re(`-?-stdlib=([^@\-].*)`),
169 re(`-v`),
170
171
172
173
174
175
176 re(`-Wl,--(no-)?allow-multiple-definition`),
177 re(`-Wl,--(no-)?allow-shlib-undefined`),
178 re(`-Wl,--(no-)?as-needed`),
179 re(`-Wl,-Bdynamic`),
180 re(`-Wl,-berok`),
181 re(`-Wl,-Bstatic`),
182 re(`-Wl,-Bsymbolic-functions`),
183 re(`-Wl,-O([^@,\-][^,]*)?`),
184 re(`-Wl,-d[ny]`),
185 re(`-Wl,--disable-new-dtags`),
186 re(`-Wl,-e[=,][a-zA-Z0-9]*`),
187 re(`-Wl,--enable-new-dtags`),
188 re(`-Wl,--end-group`),
189 re(`-Wl,--(no-)?export-dynamic`),
190 re(`-Wl,-E`),
191 re(`-Wl,-framework,[^,@\-][^,]+`),
192 re(`-Wl,--hash-style=(sysv|gnu|both)`),
193 re(`-Wl,-headerpad_max_install_names`),
194 re(`-Wl,--no-undefined`),
195 re(`-Wl,-R([^@\-][^,@]*$)`),
196 re(`-Wl,--just-symbols[=,]([^,@\-][^,@]+)`),
197 re(`-Wl,-rpath(-link)?[=,]([^,@\-][^,]+)`),
198 re(`-Wl,-s`),
199 re(`-Wl,-search_paths_first`),
200 re(`-Wl,-sectcreate,([^,@\-][^,]+),([^,@\-][^,]+),([^,@\-][^,]+)`),
201 re(`-Wl,--start-group`),
202 re(`-Wl,-?-static`),
203 re(`-Wl,-?-subsystem,(native|windows|console|posix|xbox)`),
204 re(`-Wl,-syslibroot[=,]([^,@\-][^,]+)`),
205 re(`-Wl,-undefined[=,]([^,@\-][^,]+)`),
206 re(`-Wl,-?-unresolved-symbols=[^,]+`),
207 re(`-Wl,--(no-)?warn-([^,]+)`),
208 re(`-Wl,-?-wrap[=,][^,@\-][^,]*`),
209 re(`-Wl,-z,(no)?execstack`),
210 re(`-Wl,-z,relro`),
211
212 re(`[a-zA-Z0-9_/].*\.(a|o|obj|dll|dylib|so|tbd)`),
213 re(`\./.*\.(a|o|obj|dll|dylib|so|tbd)`),
214 }
215
216 var validLinkerFlagsWithNextArg = []string{
217 "-arch",
218 "-F",
219 "-l",
220 "-L",
221 "-framework",
222 "-isysroot",
223 "--sysroot",
224 "-target",
225 "-Wl,-framework",
226 "-Wl,-rpath",
227 "-Wl,-R",
228 "-Wl,--just-symbols",
229 "-Wl,-undefined",
230 }
231
232 func checkCompilerFlags(name, source string, list []string) error {
233 return checkFlags(name, source, list, validCompilerFlags, validCompilerFlagsWithNextArg)
234 }
235
236 func checkLinkerFlags(name, source string, list []string) error {
237 return checkFlags(name, source, list, validLinkerFlags, validLinkerFlagsWithNextArg)
238 }
239
240 func checkFlags(name, source string, list []string, valid []*lazyregexp.Regexp, validNext []string) error {
241
242 var (
243 allow *regexp.Regexp
244 disallow *regexp.Regexp
245 )
246 if env := cfg.Getenv("CGO_" + name + "_ALLOW"); env != "" {
247 r, err := regexp.Compile(env)
248 if err != nil {
249 return fmt.Errorf("parsing $CGO_%s_ALLOW: %v", name, err)
250 }
251 allow = r
252 }
253 if env := cfg.Getenv("CGO_" + name + "_DISALLOW"); env != "" {
254 r, err := regexp.Compile(env)
255 if err != nil {
256 return fmt.Errorf("parsing $CGO_%s_DISALLOW: %v", name, err)
257 }
258 disallow = r
259 }
260
261 Args:
262 for i := 0; i < len(list); i++ {
263 arg := list[i]
264 if disallow != nil && disallow.FindString(arg) == arg {
265 goto Bad
266 }
267 if allow != nil && allow.FindString(arg) == arg {
268 continue Args
269 }
270 for _, re := range valid {
271 if re.FindString(arg) == arg {
272 continue Args
273 }
274 }
275 for _, x := range validNext {
276 if arg == x {
277 if i+1 < len(list) && load.SafeArg(list[i+1]) {
278 i++
279 continue Args
280 }
281
282
283 if i+1 < len(list) &&
284 strings.HasPrefix(arg, "-Wl,") &&
285 strings.HasPrefix(list[i+1], "-Wl,") &&
286 load.SafeArg(list[i+1][4:]) &&
287 !strings.Contains(list[i+1][4:], ",") {
288 i++
289 continue Args
290 }
291
292
293 if i+1 < len(list) && arg == "-I" {
294 if (strings.HasPrefix(list[i+1], "=") || strings.HasPrefix(list[i+1], "$SYSROOT")) &&
295 load.SafeArg(list[i+1][1:]) {
296 i++
297 continue Args
298 }
299 }
300
301 if i+1 < len(list) {
302 return fmt.Errorf("invalid flag in %s: %s %s (see https://golang.org/s/invalidflag)", source, arg, list[i+1])
303 }
304 return fmt.Errorf("invalid flag in %s: %s without argument (see https://golang.org/s/invalidflag)", source, arg)
305 }
306 }
307 Bad:
308 return fmt.Errorf("invalid flag in %s: %s", source, arg)
309 }
310 return nil
311 }
312
View as plain text