Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -buildmode=pie for supported platform #24964

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions dev-tools/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,38 @@ func DefaultBuildArgs() BuildArgs {
if versionQualified {
args.Vars[elasticBeatsModulePath+"/libbeat/version.qualifier"] = "{{ .Qualifier }}"
}

if positionIndependendCodeSupported() {
args.ExtraFlags = append(args.ExtraFlags, "-buildmode", "pie")
}

return args
}

// positionIndependendCodeSupported checks if the target platform support position independen code (or ASLR).
//
// The list of supported platforms is compiled based on the Go releasse notes: https://golang.org/doc/devel/release.html
urso marked this conversation as resolved.
Show resolved Hide resolved
// The list has been updated according to the Go version: 1.16
func positionIndependendCodeSupported() bool {
return oneOf(Platform.GOOS, "darwin") ||
(Platform.GOOS == "linux" && oneOf(Platform.GOARCH, "riscv64", "amd64", "arm", "arm64", "ppc64le", "386")) ||
(Platform.GOOS == "aix" && Platform.GOARCH == "ppc64") ||

// Windows 32bit supports ASLR, but Windows Server 2003 and earlier do not.
// According to the support matrix (https://www.elastic.co/support/matrix), these old versions
// are not supported.
(Platform.GOOS == "windows")
}

func oneOf(value string, lst ...string) bool {
for _, other := range lst {
if other == value {
return true
}
}
return false
}

// DefaultGolangCrossBuildArgs returns the default BuildArgs for use in
// cross-builds.
func DefaultGolangCrossBuildArgs() BuildArgs {
Expand Down