Skip to content

Commit 85ec695

Browse files
committed
Beefed up the cmd line parsing to cope with linking startgroup and endgroup;5Ds
1 parent c627e2b commit 85ec695

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

shared/parser.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ func Parse(argList []string) ParserResult {
208208
"-mno-avx2": {0, pr.compileUnaryCallback},
209209
"-mno-red-zone": {0, pr.compileUnaryCallback},
210210
"-mmmx": {0, pr.compileUnaryCallback},
211+
"-mbmi": {0, pr.compileUnaryCallback},
212+
"-mbmi2": {0, pr.compileUnaryCallback},
213+
"-mf161c": {0, pr.compileUnaryCallback},
214+
"-mfma": {0, pr.compileUnaryCallback},
211215
"-mno-mmx": {0, pr.compileUnaryCallback},
212216
"-mno-global-merge": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
213217
"-mno-80387": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
@@ -290,6 +294,7 @@ func Parse(argList []string) ParserResult {
290294
"-gdwarf-4": {0, pr.compileUnaryCallback},
291295
"-gline-tables-only": {0, pr.compileUnaryCallback},
292296
"-grecord-gcc-switches": {0, pr.compileUnaryCallback},
297+
"-ggnu-pubnames": {0, pr.compileUnaryCallback},
293298

294299
"-p": {0, pr.compileUnaryCallback},
295300
"-pg": {0, pr.compileUnaryCallback},
@@ -364,7 +369,7 @@ func Parse(argList []string) ParserResult {
364369
{`^-W[^l].*$`, flagInfo{0, pr.compileUnaryCallback}},
365370
{`^-W[l][^,].*$`, flagInfo{0, pr.compileUnaryCallback}}, //iam: tor has a few -Wl...
366371
{`^-fsanitize=.+$`, flagInfo{0, pr.compileLinkUnaryCallback}},
367-
{`^-fuse-ld=.+$`, flagInfo{0, pr.linkUnaryCallback}}, //iam: musl stuff
372+
{`^-fuse-ld=.+$`, flagInfo{0, pr.linkUnaryCallback}}, //iam: musl stuff
368373
{`^-flto=.+$`, flagInfo{0, pr.linkTimeOptimizationCallback}}, //iam: new lto stuff
369374
{`^-f.+$`, flagInfo{0, pr.compileUnaryCallback}},
370375
{`^-rtlib=.+$`, flagInfo{0, pr.linkUnaryCallback}},
@@ -391,32 +396,53 @@ func Parse(argList []string) ParserResult {
391396
if fi, ok := argsExactMatches[elem]; ok {
392397
fi.handler(elem, argList[1:1+fi.arity])
393398
argList = argList[1+fi.arity:]
394-
// Else try to match a pattern
399+
// else it is more complicated, either a pattern or a group
395400
} else {
396401
var listShift = 0
397-
var matched = false
398-
399-
for _, argPat := range argPatterns {
400-
pattern := argPat.pattern
401-
fi := argPat.finfo
402-
var regExp = regexp.MustCompile(pattern)
403-
if regExp.MatchString(elem) {
404-
fi.handler(elem, argList[1:1+fi.arity])
405-
listShift = fi.arity
406-
matched = true
407-
break
402+
//need to handle the N-ary grouping flag
403+
if elem == "-Wl,--start-group" {
404+
endgroup := indexOf("-Wl,--end-group", argList)
405+
if endgroup > 0 {
406+
pr.linkerGroupCallback(elem, endgroup+1, argList)
407+
listShift = endgroup
408+
} else {
409+
LogWarning("Failed to find '-Wl,--end-group' matching '-Wl,--start-group'\n")
410+
pr.compileUnaryCallback(elem, argList[1:1])
411+
}
412+
//else try to match a pattern
413+
} else {
414+
var matched = false
415+
for _, argPat := range argPatterns {
416+
pattern := argPat.pattern
417+
fi := argPat.finfo
418+
var regExp = regexp.MustCompile(pattern)
419+
if regExp.MatchString(elem) {
420+
fi.handler(elem, argList[1:1+fi.arity])
421+
listShift = fi.arity
422+
matched = true
423+
break
424+
}
425+
}
426+
if !matched {
427+
LogWarning("Did not recognize the compiler flag: %v\n", elem)
428+
pr.compileUnaryCallback(elem, argList[1:1])
408429
}
409-
}
410-
if !matched {
411-
LogWarning("Did not recognize the compiler flag: %v\n", elem)
412-
pr.compileUnaryCallback(elem, argList[1:1])
413430
}
414431
argList = argList[1+listShift:]
415432
}
416433
}
417434
return pr
418435
}
419436

437+
func indexOf(value string, slice []string) int {
438+
for p, v := range slice {
439+
if v == value {
440+
return p
441+
}
442+
}
443+
return -1
444+
}
445+
420446
// Return the object and bc filenames that correspond to the i-th source file
421447
func getArtifactNames(pr ParserResult, srcFileIndex int, hidden bool) (objBase string, bcBase string) {
422448
if len(pr.InputFiles) == 1 && pr.IsCompileOnly && len(pr.OutputFilename) > 0 {
@@ -470,6 +496,11 @@ func (pr *ParserResult) objectFileCallback(flag string, _ []string) {
470496
pr.LinkArgs = append(pr.LinkArgs, flag)
471497
}
472498

499+
func (pr *ParserResult) linkerGroupCallback(start string, count int, args []string) {
500+
group := args[0:count]
501+
pr.LinkArgs = append(pr.LinkArgs, group...)
502+
}
503+
473504
func (pr *ParserResult) preprocessOnlyCallback(_ string, _ []string) {
474505
pr.IsPreprocessOnly = true
475506
}

tests/parsing_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,30 @@ const input0 = `-pthread -c -Wno-unused-result -Wsign-compare -Wunreachable-code
1010

1111
const input1 = `-pthread -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -march=x86-64 -mtune=generic -O3 -pipe -fno-plt -g -fdebug-prefix-map=/home/legend/makepkgs/python/src=/usr/src/debug -fno-semantic-interposition -flto=thin -g -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fprofile-instr-use=code.profclangd -I./Include/internal -I. -I./Include -D_FORTIFY_SOURCE=2 -D_FORTIFY_SOURCE=2 -fPIC -DPy_BUILD_CORE -DSOABI='"cpython-38-x86_64-linux-gnu"' -o Python/dynload_shlib.o ./Python/dynload_shlib.c`
1212

13-
func pt(input string, t *testing.T) {
13+
const input2 = `-Wl,--fatal-warnings -Wl,--build-id=sha1 -fPIC -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,-z,defs -Wl,--as-needed -fuse-ld=lld -Wl,--icf=all -Wl,--color-diagnostics -flto=thin -Wl,--thinlto-jobs=8 -Wl,--thinlto-cache-dir=thinlto-cache -Wl,--thinlto-cache-policy,cache_size=10\%:cache_size_bytes=10g:cache_size_files=100000 -Wl,--lto-O0 -fwhole-program-vtables -Wl,--no-call-graph-profile-sort -m64 -Wl,-O2 -Wl,--gc-sections -Wl,--gdb-index -rdynamic -fsanitize=cfi-vcall -fsanitize=cfi-icall -pie -Wl,--disable-new-dtags -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o "./brotli" -Wl,--start-group @"./brotli.rsp" -Wl,--end-group -latomic -ldl -lpthread -lrt
14+
`
15+
16+
const input3 = `1.c 2.c 3.c 4.c 5.c -Wl,--start-group 7.o 8.o 9.o -Wl,--end-group 10.c 11.c 12.c 13.c`
17+
18+
func plto(input string, t *testing.T) {
1419
cmds := strings.Fields(input)
1520
parsed := shared.Parse(cmds)
1621
if !parsed.IsLTO {
17-
t.Errorf("Parsing of %v FAILED %v (not LTO)\n", input, parsed)
22+
t.Errorf("Parsing of %v FAILED %v (not LTO)\n", input, parsed)
23+
}
24+
}
25+
26+
func pl(input string, t *testing.T, expected int) {
27+
cmds := strings.Fields(input)
28+
parsed := shared.Parse(cmds)
29+
if expected != len(parsed.LinkArgs) {
30+
t.Errorf("Linking args %v of length %v NOT the expected length %v\n", parsed.LinkArgs, len(parsed.LinkArgs), expected)
1831
}
1932
}
2033

2134
func Test_parsing(t *testing.T) {
22-
pt(input0, t)
23-
pt(input1, t)
35+
plto(input0, t)
36+
plto(input1, t)
37+
pl(input2, t, 32)
38+
pl(input3, t, 5)
2439
}

0 commit comments

Comments
 (0)