Skip to content

Commit

Permalink
Add -buildmode=pie for supported platform (bp #24964) (#25020)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1c49866)

Co-authored-by: Steffen Siering <steffen.siering@elastic.co>
  • Loading branch information
mergify[bot] and Steffen Siering authored Apr 13, 2021
1 parent 205e3dd commit 18f4e40
Showing 1 changed file with 29 additions and 0 deletions.
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 release notes: https://golang.org/doc/devel/release.html
// 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

0 comments on commit 18f4e40

Please sign in to comment.