From afaf95b4d87a8a3307c134dee90dd667aa2ffc42 Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Fri, 3 Jan 2020 03:48:19 +0800 Subject: [PATCH 1/6] golang: Re-format environment variable unexports This changes the formatting of environment variable unexports in golang-values.mk to be more maintainable, and to document the sources of variables. There should be no change to the actual list of variables unexported. Signed-off-by: Jeffery To --- lang/golang/golang-values.mk | 93 +++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 12 deletions(-) diff --git a/lang/golang/golang-values.mk b/lang/golang/golang-values.mk index a52dcc92d..d127ef622 100644 --- a/lang/golang/golang-values.mk +++ b/lang/golang/golang-values.mk @@ -12,22 +12,91 @@ endif include $(GO_INCLUDE_DIR)/golang-version.mk +# Unset environment variables +# There are more magic variables to track down, but ain't nobody got time for that + +# From https://golang.org/cmd/go/#hdr-Environment_variables + +# General-purpose environment variables: +unexport \ + GCCGO \ + GOARCH \ + GOBIN \ + GOCACHE \ + GODEBUG \ + GOFLAGS \ + GOOS \ + GOPATH \ + GOROOT \ + GOTMPDIR +# Unmodified: +# GOPRIVATE +# GOPROXY +# GONOPROXY +# GOSUMDB +# GONOSUMDB + +# Environment variables for use with cgo: unexport \ - GOARCH GOBIN GOCACHE GOFLAGS GOHOSTARCH GOOS GOPATH GORACE GOROOT GOTMPDIR GCCGO \ - GOGC GODEBUG GOMAXPROCS GOTRACEBACK \ CGO_ENABLED \ - CGO_CFLAGS CGO_CFLAGS_ALLOW CGO_CFLAGS_DISALLOW \ + CGO_CFLAGS CGO_CFLAGS_ALLOW CGO_CFLAGS_DISALLOW \ CGO_CPPFLAGS CGO_CPPFLAGS_ALLOW CGO_CPPFLAGS_DISALLOW \ CGO_CXXFLAGS CGO_CXXFLAGS_ALLOW CGO_CXXFLAGS_DISALLOW \ - CGO_FFLAGS CGO_FFLAGS_ALLOW CGO_FFLAGS_DISALLOW \ - CGO_LDFLAGS CGO_LDFLAGS_ALLOW CGO_LDFLAGS_DISALLOW \ - GOARM GO386 GOMIPS GOMIPS64 \ - GO111MODULE \ - GOROOT_FINAL GO_EXTLINK_ENABLED GIT_ALLOW_PROTOCOL \ - CC_FOR_TARGET CXX_FOR_TARGET GO_DISTFLAGS GO_GCFLAGS GO_LDFLAGS GOBUILDTIMELOGFILE GOROOT_BOOTSTRAP \ - BOOT_GO_GCFLAGS GOEXPERIMENT GOBOOTSTRAP_TOOLEXEC - # there are more magic environment variables to track down, but ain't nobody got time for that - # deliberately left untouched: GOPROXY GONOPROXY GOSUMDB GONOSUMDB GOPRIVATE + CGO_FFLAGS CGO_FFLAGS_ALLOW CGO_FFLAGS_DISALLOW \ + CGO_LDFLAGS CGO_LDFLAGS_ALLOW CGO_LDFLAGS_DISALLOW + +# Architecture-specific environment variables: +unexport \ + GOARM \ + GO386 \ + GOMIPS \ + GOMIPS64 + +# Special-purpose environment variables: +unexport \ + GOROOT_FINAL \ + GO_EXTLINK_ENABLED \ + GIT_ALLOW_PROTOCOL + +# From https://golang.org/cmd/go/#hdr-Module_support +unexport \ + GO111MODULE + +# From https://golang.org/pkg/runtime/#hdr-Environment_Variables +unexport \ + GOGC \ + GOMAXPROCS \ + GORACE \ + GOTRACEBACK + +# From https://golang.org/cmd/cgo/#hdr-Using_cgo_with_the_go_command +unexport \ + CC_FOR_TARGET \ + CXX_FOR_TARGET + +# From https://golang.org/doc/install/source#environment +unexport \ + GOHOSTARCH + +# From https://golang.org/src/make.bash +unexport \ + GO_GCFLAGS \ + GO_LDFLAGS \ + GO_DISTFLAGS \ + GOBUILDTIMELOGFILE \ + GOROOT_BOOTSTRAP + +# From https://golang.org/src/cmd/dist/build.go +unexport \ + BOOT_GO_GCFLAGS + +# From https://golang.org/src/cmd/dist/buildtool.go +unexport \ + GOBOOTSTRAP_TOOLEXEC + +# From https://golang.org/src/cmd/internal/objabi/util.go +unexport \ + GOEXPERIMENT go_arch=$(subst \ aarch64,arm64,$(subst \ From c9781b027a8c3b704f6cbacec99bf57c70c6ec0c Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Fri, 3 Jan 2020 04:56:42 +0800 Subject: [PATCH 2/6] golang: Update variables unexported in golang-values.mk The lists of variables are updated for Go 1.13. Signed-off-by: Jeffery To --- lang/golang/golang-values.mk | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lang/golang/golang-values.mk b/lang/golang/golang-values.mk index d127ef622..02b10b1ed 100644 --- a/lang/golang/golang-values.mk +++ b/lang/golang/golang-values.mk @@ -1,5 +1,5 @@ # -# Copyright (C) 2018 Jeffery To +# Copyright (C) 2018, 2020 Jeffery To # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. @@ -24,6 +24,7 @@ unexport \ GOBIN \ GOCACHE \ GODEBUG \ + GOENV \ GOFLAGS \ GOOS \ GOPATH \ @@ -38,25 +39,34 @@ unexport \ # Environment variables for use with cgo: unexport \ + AR \ + CC \ CGO_ENABLED \ CGO_CFLAGS CGO_CFLAGS_ALLOW CGO_CFLAGS_DISALLOW \ CGO_CPPFLAGS CGO_CPPFLAGS_ALLOW CGO_CPPFLAGS_DISALLOW \ CGO_CXXFLAGS CGO_CXXFLAGS_ALLOW CGO_CXXFLAGS_DISALLOW \ CGO_FFLAGS CGO_FFLAGS_ALLOW CGO_FFLAGS_DISALLOW \ - CGO_LDFLAGS CGO_LDFLAGS_ALLOW CGO_LDFLAGS_DISALLOW + CGO_LDFLAGS CGO_LDFLAGS_ALLOW CGO_LDFLAGS_DISALLOW \ + CXX \ + FC +# Unmodified: +# PKG_CONFIG # Architecture-specific environment variables: unexport \ GOARM \ GO386 \ GOMIPS \ - GOMIPS64 + GOMIPS64 \ + GOWASM # Special-purpose environment variables: unexport \ + GCCGOTOOLDIR \ GOROOT_FINAL \ - GO_EXTLINK_ENABLED \ - GIT_ALLOW_PROTOCOL + GO_EXTLINK_ENABLED +# Unmodified: +# GIT_ALLOW_PROTOCOL # From https://golang.org/cmd/go/#hdr-Module_support unexport \ @@ -73,22 +83,33 @@ unexport \ unexport \ CC_FOR_TARGET \ CXX_FOR_TARGET +# Todo: +# CC_FOR_${GOOS}_${GOARCH} +# CXX_FOR_${GOOS}_${GOARCH} # From https://golang.org/doc/install/source#environment unexport \ - GOHOSTARCH + GOHOSTOS \ + GOHOSTARCH \ + GOPPC64 # From https://golang.org/src/make.bash unexport \ GO_GCFLAGS \ GO_LDFLAGS \ + GO_LDSO \ GO_DISTFLAGS \ GOBUILDTIMELOGFILE \ GOROOT_BOOTSTRAP +# From https://golang.org/doc/go1.9#parallel-compile +unexport \ + GO19CONCURRENTCOMPILATION + # From https://golang.org/src/cmd/dist/build.go unexport \ - BOOT_GO_GCFLAGS + BOOT_GO_GCFLAGS \ + BOOT_GO_LDFLAGS # From https://golang.org/src/cmd/dist/buildtool.go unexport \ From 12812d328f9bb1c83acfbf8d751c373baae82c64 Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Fri, 3 Jan 2020 05:18:49 +0800 Subject: [PATCH 3/6] golang: Update valid os/arch combinations for host Go The list is updated for Go 1.13. Signed-off-by: Jeffery To --- lang/golang/golang/Makefile | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lang/golang/golang/Makefile b/lang/golang/golang/Makefile index be6b050bc..e7ac30cf6 100644 --- a/lang/golang/golang/Makefile +++ b/lang/golang/golang/Makefile @@ -1,5 +1,5 @@ # -# Copyright (C) 2018 Jeffery To +# Copyright (C) 2018, 2020 Jeffery To # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. @@ -42,18 +42,25 @@ HOST_GO_VERSION_ID:=cross HOST_GO_ROOT:=$(HOST_GO_PREFIX)/lib/go-$(HOST_GO_VERSION_ID) HOST_GO_VALID_OS_ARCH:= \ - android_arm \ - darwin_386 darwin_amd64 darwin_arm darwin_arm64 \ - dragonfly_amd64 \ - freebsd_386 freebsd_amd64 freebsd_arm \ - linux_386 linux_amd64 linux_arm linux_arm64 \ - netbsd_386 netbsd_amd64 netbsd_arm \ - openbsd_386 openbsd_amd64 openbsd_arm \ - plan9_386 plan9_amd64 \ - solaris_amd64 \ + android_386 android_amd64 android_arm android_arm64 \ + darwin_386 darwin_amd64 darwin_arm darwin_arm64 \ + linux_386 linux_amd64 linux_arm linux_arm64 \ + openbsd_386 openbsd_amd64 openbsd_arm openbsd_arm64 \ + \ + freebsd_386 freebsd_amd64 freebsd_arm \ + netbsd_386 netbsd_amd64 netbsd_arm \ + plan9_386 plan9_amd64 plan9_arm \ + \ windows_386 windows_amd64 \ \ - linux_ppc64 linux_ppc64le linux_mips linux_mipsle linux_mips64 linux_mips64le + dragonfly_amd64 \ + illumos_amd64 \ + solaris_amd64 \ + \ + aix_ppc64 \ + js_wasm \ + \ + linux_ppc64 linux_ppc64le linux_mips linux_mipsle linux_mips64 linux_mips64le linux_s390x BOOTSTRAP_SOURCE:=go1.4-bootstrap-20171003.tar.gz BOOTSTRAP_SOURCE_URL:=$(GO_SOURCE_URLS) From 232634fb9a353019ca549a4891f55c2eebdc40a8 Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Wed, 8 Jan 2020 01:22:05 +0800 Subject: [PATCH 4/6] golang: Strip whitespace when using Go package variables This adds calls to the strip function in golang-package.mk when using variables expected to be set by Go packages. Signed-off-by: Jeffery To --- lang/golang/golang-package.mk | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lang/golang/golang-package.mk b/lang/golang/golang-package.mk index db700ee89..ec7b0e99b 100644 --- a/lang/golang/golang-package.mk +++ b/lang/golang/golang-package.mk @@ -107,7 +107,7 @@ include $(GO_INCLUDE_DIR)/golang-values.mk # for building packages, not user code GO_PKG_PATH:=/usr/share/gocode -GO_PKG_BUILD_PKG?=$(GO_PKG)/... +GO_PKG_BUILD_PKG?=$(strip $(GO_PKG))/... GO_PKG_WORK_DIR_NAME:=.go_work GO_PKG_WORK_DIR:=$(PKG_BUILD_DIR)/$(GO_PKG_WORK_DIR_NAME) @@ -116,8 +116,7 @@ GO_PKG_BUILD_DIR:=$(GO_PKG_WORK_DIR)/build GO_PKG_CACHE_DIR:=$(GO_PKG_WORK_DIR)/cache GO_PKG_TMP_DIR:=$(GO_PKG_WORK_DIR)/tmp -GO_PKG_BUILD_BIN_DIR:=$(GO_PKG_BUILD_DIR)/bin$(if \ - $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS)_$(GO_ARCH)) +GO_PKG_BUILD_BIN_DIR:=$(GO_PKG_BUILD_DIR)/bin$(if $(GO_HOST_TARGET_DIFFERENT),/$(GO_OS_ARCH)) GO_PKG_BUILD_DEPENDS_SRC:=$(STAGING_DIR)$(GO_PKG_PATH)/src @@ -160,7 +159,7 @@ define GoPackage/Environment/Default CGO_CXXFLAGS="$(filter-out $(GO_CFLAGS_TO_REMOVE),$(TARGET_CXXFLAGS))" endef -GoPackage/Environment=$(call GoPackage/Environment/Default,) +GoPackage/Environment=$(call GoPackage/Environment/Default) # false if directory does not exist GoPackage/is_dir_not_empty=$$$$($(FIND) $(1) -maxdepth 0 -type d \! -empty 2>/dev/null) @@ -178,7 +177,7 @@ define GoPackage/Build/Configure \! -type d -print | \ sed 's|^\./||') ; \ \ - if [ "$(GO_PKG_INSTALL_ALL)" != 1 ]; then \ + if [ "$(strip $(GO_PKG_INSTALL_ALL))" != 1 ]; then \ code=$$$$(echo "$$$$files" | grep '\.\(c\|cc\|cpp\|go\|h\|hh\|hpp\|proto\|s\)$$$$') ; \ testdata=$$$$(echo "$$$$files" | grep '\(^\|/\)testdata/') ; \ gomod=$$$$(echo "$$$$files" | grep '\(^\|/\)go\.\(mod\|sum\)$$$$') ; \ @@ -193,10 +192,10 @@ define GoPackage/Build/Configure \ IFS=$$$$'\n' ; \ \ - echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)" ; \ + echo "Copying files from $(PKG_BUILD_DIR) into $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))" ; \ for file in $$$$files; do \ echo $$$$file ; \ - dest=$(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/$$$$file ; \ + dest=$(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG))/$$$$file ; \ mkdir -p $$$$(dirname $$$$dest) ; \ $(CP) $$$$file $$$$dest ; \ done ; \ @@ -217,8 +216,8 @@ define GoPackage/Build/Configure base=$$$$(basename $$$$dir) ; \ if [ -d $$$$dest/$$$$base ]; then \ case $$$$dir in \ - *$(GO_PKG_PATH)/src/$(GO_PKG)) \ - echo "$(GO_PKG) is already installed. Please check for circular dependencies." ;; \ + *$(GO_PKG_PATH)/src/$(strip $(GO_PKG))) \ + echo "$(strip $(GO_PKG)) is already installed. Please check for circular dependencies." ;; \ *) \ link_contents $$$$src/$$$$base $$$$dest/$$$$base ;; \ esac ; \ @@ -229,7 +228,7 @@ define GoPackage/Build/Configure done ; \ } ; \ \ - if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \ + if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \ if [ -d $(GO_PKG_BUILD_DEPENDS_SRC) ]; then \ echo "Symlinking directories from $(GO_PKG_BUILD_DEPENDS_SRC) into $(GO_PKG_BUILD_DIR)/src" ; \ link_contents $(GO_PKG_BUILD_DEPENDS_SRC) $(GO_PKG_BUILD_DIR)/src ; \ @@ -262,13 +261,13 @@ define GoPackage/Build/Compile done ; \ echo ; \ \ - if [ "$(GO_PKG_GO_GENERATE)" = 1 ]; then \ + if [ "$(strip $(GO_PKG_GO_GENERATE))" = 1 ]; then \ echo "Calling go generate" ; \ go generate -v $(1) $$$$targets ; \ echo ; \ fi ; \ \ - if [ "$(GO_PKG_SOURCE_ONLY)" != 1 ]; then \ + if [ "$(strip $(GO_PKG_SOURCE_ONLY))" != 1 ]; then \ echo "Building targets" ; \ case $(GO_ARCH) in \ arm) installsuffix="v$(GO_ARM)" ;; \ @@ -276,8 +275,8 @@ define GoPackage/Build/Compile mips64|mips64le) installsuffix="$(GO_MIPS64)" ;; \ esac ; \ ldflags="-linkmode external -extldflags '$(TARGET_LDFLAGS:-z%=-Wl,-z,%)'" ; \ - pkg_gcflags="$(GO_PKG_GCFLAGS)" ; \ - pkg_ldflags="$(GO_PKG_LDFLAGS)" ; \ + pkg_gcflags="$(strip $(GO_PKG_GCFLAGS))" ; \ + pkg_ldflags="$(strip $(GO_PKG_LDFLAGS))" ; \ for def in $(GO_PKG_LDFLAGS_X); do \ pkg_ldflags="$$$$pkg_ldflags -X $$$$def" ; \ done ; \ @@ -320,7 +319,7 @@ endef define GoPackage/Package/Install/Src dir=$$$$(dirname $(GO_PKG)) ; \ $(INSTALL_DIR) $(1)$(GO_PKG_PATH)/src/$$$$dir ; \ - $(CP) $(GO_PKG_BUILD_DIR)/src/$(GO_PKG) $(1)$(GO_PKG_PATH)/src/$$$$dir/ + $(CP) $(GO_PKG_BUILD_DIR)/src/$(strip $(GO_PKG)) $(1)$(GO_PKG_PATH)/src/$$$$dir/ endef define GoPackage/Package/Install @@ -329,7 +328,7 @@ define GoPackage/Package/Install endef -ifneq ($(GO_PKG),) +ifneq ($(strip $(GO_PKG)),) Build/Configure=$(call GoPackage/Build/Configure) Build/Compile=$(call GoPackage/Build/Compile) Build/InstallDev=$(call GoPackage/Build/InstallDev,$(1)) From 53603abaaecb63cfa786752d19878ea42974dc52 Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Wed, 8 Jan 2020 02:15:59 +0800 Subject: [PATCH 5/6] golang: Make golang-values.mk more readable This replaces conditional functions with conditional directives to make golang-values.mk more readable. Signed-off-by: Jeffery To --- lang/golang/golang-values.mk | 59 ++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/lang/golang/golang-values.mk b/lang/golang/golang-values.mk index 02b10b1ed..873d71524 100644 --- a/lang/golang/golang-values.mk +++ b/lang/golang/golang-values.mk @@ -119,6 +119,9 @@ unexport \ unexport \ GOEXPERIMENT + +# GOOS / GOARCH + go_arch=$(subst \ aarch64,arm64,$(subst \ i386,386,$(subst \ @@ -135,28 +138,52 @@ GO_HOST_OS:=$(call tolower,$(HOST_OS)) GO_HOST_ARCH:=$(call go_arch,$(subst \ armv6l,arm,$(subst \ armv7l,arm,$(subst \ - i486,i386,$(subst \ - i586,i386,$(subst \ - i686,i386,$(HOST_ARCH))))))) + i686,i386,$(HOST_ARCH))))) GO_HOST_OS_ARCH:=$(GO_HOST_OS)_$(GO_HOST_ARCH) -GO_HOST_TARGET_SAME:=$(if $(and $(findstring $(GO_OS_ARCH),$(GO_HOST_OS_ARCH)),$(findstring $(GO_HOST_OS_ARCH),$(GO_OS_ARCH))),1) -GO_HOST_TARGET_DIFFERENT:=$(if $(GO_HOST_TARGET_SAME),,1) - -# ensure binaries can run on older CPUs -GO_386:=387 +ifeq ($(GO_OS_ARCH),$(GO_HOST_OS_ARCH)) + GO_HOST_TARGET_SAME:=1 +else + GO_HOST_TARGET_DIFFERENT:=1 +endif -GO_ARM:=$(if $(CONFIG_arm_v7),7,$(if $(CONFIG_arm_v6),6,$(if $(findstring $(GO_ARCH),arm),5,))) +ifeq ($(GO_ARCH),386) + # ensure binaries can run on older CPUs + GO_386:=387 + + # -fno-plt: causes "unexpected GOT reloc for non-dynamic symbol" errors + GO_CFLAGS_TO_REMOVE:=-fno-plt + +else ifeq ($(GO_ARCH),arm) + ifeq ($(CONFIG_arm_v7),y) + GO_ARM:=7 + else ifeq ($(CONFIG_arm_v6),y) + GO_ARM:=6 + else + GO_ARM:=5 + endif + +else ifneq ($(filter $(GO_ARCH),mips mipsle),) + ifeq ($(CONFIG_HAS_FPU),y) + GO_MIPS:=hardfloat + else + GO_MIPS:=softfloat + endif + + # -mips32r2: conflicts with -march=mips32 set by go + GO_CFLAGS_TO_REMOVE:=-mips32r2 + +else ifneq ($(filter $(GO_ARCH),mips64 mips64le),) + ifeq ($(CONFIG_HAS_FPU),y) + GO_MIPS64:=hardfloat + else + GO_MIPS64:=softfloat + endif -GO_MIPS:=$(if $(filter $(GO_ARCH),mips mipsle),$(if $(CONFIG_HAS_FPU),hardfloat,softfloat),) +endif -GO_MIPS64:=$(if $(filter $(GO_ARCH),mips64 mips64le),$(if $(CONFIG_HAS_FPU),hardfloat,softfloat),) -# -fno-plt: causes "unexpected GOT reloc for non-dynamic symbol" errors -# -mips32r2: conflicts with -march=mips32 set by go -GO_CFLAGS_TO_REMOVE:=$(if \ -$(filter $(GO_ARCH),386),-fno-plt,$(if \ -$(filter $(GO_ARCH),mips mipsle),-mips32r2,)) +# Target Go GO_ARCH_DEPENDS:=@(aarch64||arm||i386||i686||mips||mips64||mips64el||mipsel||powerpc64||x86_64) From c3775767341fa641d8a38412f5c5836620ae352f Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Wed, 8 Jan 2020 19:13:44 +0800 Subject: [PATCH 6/6] golang: Add support for ASLR/PIE for host and target Go This adds support to compile host and target Go as position-independent executables. Host Go will have PIE enabled if Go supports PIE on the host platform. Target Go will have PIE enabled if Go supports PIE on the target platform and CONFIG_PKG_ASLR_PIE is selected. Go 1.13 supports PIE for x86 and arm targets; mips support is in progress[1]. [1]: https://github.com/golang/go/issues/21222#issuecomment-542064462 Signed-off-by: Jeffery To --- lang/golang/golang-compiler.mk | 8 ++++--- lang/golang/golang-values.mk | 26 ++++++++++++++++++++++ lang/golang/golang/Makefile | 40 +++++++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/lang/golang/golang-compiler.mk b/lang/golang/golang-compiler.mk index 5e7fe689d..fa8f3bc9f 100644 --- a/lang/golang/golang-compiler.mk +++ b/lang/golang/golang-compiler.mk @@ -1,5 +1,5 @@ # -# Copyright (C) 2018 Jeffery To +# Copyright (C) 2018, 2020 Jeffery To # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. @@ -51,6 +51,7 @@ endef # $(2) destination prefix # $(3) go version id # $(4) GOOS_GOARCH +# $(5) install suffix (optional) define GoCompiler/Default/Install/Bin $(call GoCompiler/Default/Install/make-dirs,$(2),$(3)) @@ -73,7 +74,7 @@ define GoCompiler/Default/Install/Bin endif $(INSTALL_DIR) $(2)/lib/go-$(3)/pkg - $(CP) $(1)/pkg/$(4) $(2)/lib/go-$(3)/pkg/ + $(CP) $(1)/pkg/$(4)$(if $(5),_$(5)) $(2)/lib/go-$(3)/pkg/ $(INSTALL_DIR) $(2)/lib/go-$(3)/pkg/tool/$(4) $(INSTALL_BIN) -p $(1)/pkg/tool/$(4)/* $(2)/lib/go-$(3)/pkg/tool/$(4)/ @@ -141,6 +142,7 @@ endef # $(3) destination prefix # $(4) go version id # $(5) GOOS_GOARCH +# $(6) install suffix (optional) define GoCompiler/AddProfile # $$(1) valid GOOS_GOARCH combinations @@ -155,7 +157,7 @@ define GoCompiler/AddProfile # $$(1) override install prefix (optional) define GoCompiler/$(1)/Install/Bin - $$(call GoCompiler/Default/Install/Bin,$(2),$$(or $$(1),$(3)),$(4),$(5)) + $$(call GoCompiler/Default/Install/Bin,$(2),$$(or $$(1),$(3)),$(4),$(5),$(6)) endef # $$(1) override install prefix (optional) diff --git a/lang/golang/golang-values.mk b/lang/golang/golang-values.mk index 873d71524..ee4ba7f2a 100644 --- a/lang/golang/golang-values.mk +++ b/lang/golang/golang-values.mk @@ -190,3 +190,29 @@ GO_ARCH_DEPENDS:=@(aarch64||arm||i386||i686||mips||mips64||mips64el||mipsel||pow GO_TARGET_PREFIX:=/usr GO_TARGET_VERSION_ID:=$(GO_VERSION_MAJOR_MINOR) GO_TARGET_ROOT:=$(GO_TARGET_PREFIX)/lib/go-$(GO_TARGET_VERSION_ID) + + +# ASLR/PIE + +GO_PIE_SUPPORTED_OS_ARCH:= \ + android_386 android_amd64 android_arm android_arm64 \ + linux_386 linux_amd64 linux_arm linux_arm64 \ + \ + darwin_amd64 \ + freebsd_amd64 \ + \ + aix_ppc64 \ + \ + linux_ppc64le linux_s390x + +go_pie_install_suffix=$(if $(filter $(1),aix_ppc64),,shared) + +ifneq ($(filter $(GO_HOST_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),) + GO_HOST_PIE_SUPPORTED:=1 + GO_HOST_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_HOST_OS_ARCH)) +endif + +ifneq ($(filter $(GO_OS_ARCH),$(GO_PIE_SUPPORTED_OS_ARCH)),) + GO_TARGET_PIE_SUPPORTED:=1 + GO_TARGET_PIE_INSTALL_SUFFIX:=$(call go_pie_install_suffix,$(GO_OS_ARCH)) +endif diff --git a/lang/golang/golang/Makefile b/lang/golang/golang/Makefile index e7ac30cf6..75badf953 100644 --- a/lang/golang/golang/Makefile +++ b/lang/golang/golang/Makefile @@ -10,7 +10,7 @@ include ../golang-version.mk PKG_NAME:=golang PKG_VERSION:=$(GO_VERSION_MAJOR_MINOR)$(if $(GO_VERSION_PATCH),.$(GO_VERSION_PATCH)) -PKG_RELEASE:=1 +PKG_RELEASE:=2 GO_SOURCE_URLS:=https://dl.google.com/go/ \ https://mirrors.ustc.edu.cn/golang/ \ @@ -92,6 +92,18 @@ BOOTSTRAP_UNPACK:=$(HOST_TAR) -C $(BOOTSTRAP_BUILD_DIR) --strip-components=1 -xz RSTRIP:=: STRIP:=: +ifeq ($(CONFIG_PKG_ASLR_PIE),y) + ifeq ($(GO_TARGET_PIE_SUPPORTED),1) + PKG_GO_ENABLE_PIE:=1 + PKG_GO_INSTALL_SUFFIX:=$(GO_TARGET_PIE_INSTALL_SUFFIX) + endif +endif + +ifeq ($(GO_HOST_PIE_SUPPORTED),1) + HOST_GO_ENABLE_PIE:=1 + HOST_GO_INSTALL_SUFFIX:=$(GO_HOST_PIE_INSTALL_SUFFIX) +endif + define Package/golang/Default $(call GoPackage/GoSubMenu) TITLE:=Go programming language @@ -158,8 +170,8 @@ endef $(eval $(call Download,golang-bootstrap)) $(eval $(call GoCompiler/AddProfile,Bootstrap,$(BOOTSTRAP_BUILD_DIR),,bootstrap,$(GO_HOST_OS_ARCH))) -$(eval $(call GoCompiler/AddProfile,Host,$(HOST_BUILD_DIR),$(HOST_GO_PREFIX),$(HOST_GO_VERSION_ID),$(GO_HOST_OS_ARCH))) -$(eval $(call GoCompiler/AddProfile,Package,$(PKG_BUILD_DIR),$(GO_TARGET_PREFIX),$(GO_TARGET_VERSION_ID),$(GO_OS_ARCH))) +$(eval $(call GoCompiler/AddProfile,Host,$(HOST_BUILD_DIR),$(HOST_GO_PREFIX),$(HOST_GO_VERSION_ID),$(GO_HOST_OS_ARCH),$(HOST_GO_INSTALL_SUFFIX))) +$(eval $(call GoCompiler/AddProfile,Package,$(PKG_BUILD_DIR),$(GO_TARGET_PREFIX),$(GO_TARGET_VERSION_ID),$(GO_OS_ARCH),$(PKG_GO_INSTALL_SUFFIX))) define Host/Prepare $(call Host/Prepare/Default) @@ -167,6 +179,9 @@ define Host/Prepare $(BOOTSTRAP_UNPACK) endef +# when https://github.com/golang/go/issues/31544 is fixed, +# we should be able to set GO_LDFLAGS=-buildmode=pie for host make +# instead of doing a rebuild for pie define Host/Compile $(call GoCompiler/Bootstrap/CheckHost,$(BOOTSTRAP_GO_VALID_OS_ARCH)) $(call GoCompiler/Host/CheckHost,$(HOST_GO_VALID_OS_ARCH)) @@ -181,6 +196,21 @@ define Host/Compile CC=$(HOSTCC_NOCACHE) \ CXX=$(HOSTCXX_NOCACHE) \ ) + + ifneq ($(HOST_GO_ENABLE_PIE),) + @echo "Rebuilding host Go with PIE" + + ( \ + cd $(HOST_BUILD_DIR)/bin ; \ + $(CP) go go-nopie ; \ + CC=$(HOSTCC_NOCACHE) \ + CXX=$(HOSTCXX_NOCACHE) \ + ./go-nopie install -a -buildmode=pie std cmd ; \ + retval=$$$$? ; \ + rm -f go-nopie ; \ + exit $$$$retval ; \ + ) + endif endef # if host and target os/arch are the same, @@ -194,7 +224,7 @@ define Host/Install $(call GoCompiler/Host/Install/BinLinks,) - rm -rf $(HOST_GO_ROOT)/pkg/$(GO_HOST_OS_ARCH) + rm -rf $(HOST_GO_ROOT)/pkg/$(GO_HOST_OS_ARCH)$(if $(HOST_GO_INSTALL_SUFFIX),_$(HOST_GO_INSTALL_SUFFIX)) $(INSTALL_DIR) $(HOST_GO_ROOT)/openwrt $(INSTALL_BIN) ./files/go-gcc-helper $(HOST_GO_ROOT)/openwrt/ @@ -247,7 +277,7 @@ define Build/Compile PKG_CONFIG=pkg-config \ PATH=$(HOST_GO_ROOT)/openwrt:$$$$PATH \ $(call GoPackage/Environment) \ - ./go-host install -a -v std cmd ; \ + ./go-host install -a $(if $(PKG_GO_ENABLE_PIE),-buildmode=pie) std cmd ; \ retval=$$$$? ; \ rm -f go-host ; \ exit $$$$retval ; \