f0368f
From 89129bd096c8bfac4ff84fc19726898cc901c1fc Mon Sep 17 00:00:00 2001
f0368f
From: Debarshi Ray <rishi@fedoraproject.org>
f0368f
Date: Mon, 29 Jun 2020 17:57:47 +0200
f0368f
Subject: [PATCH] build: Make the build flags match RHEL's %{gobuild}
f0368f
f0368f
The Go toolchain doesn't play well with passing compiler and linker
f0368f
flags via environment variables. The linker flags require a second
f0368f
level of quoting, which leaves the build system without a quote level
f0368f
to assign the flags to an environment variable like GOFLAGS.
f0368f
f0368f
This is one reason why RHEL doesn't have a RPM macro with only the
f0368f
flags. The %{gobuild} RPM macro includes the entire 'go build ...'
f0368f
invocation.
f0368f
f0368f
The Go toolchain also doesn't like the LDFLAGS environment variable as
f0368f
exported by RHEL's %{meson} RPM macro, and RHEL's RPM toolchain doesn't
f0368f
like the compressed DWARF data generated by the Go toolchain.
f0368f
f0368f
Note that these flags are meant for every CPU architecture other than
f0368f
PPC64, and should be kept updated to match RHEL's Go guidelines. Use
f0368f
'rpm --eval "%{gobuild}"' to expand the %{gobuild} macro.
f0368f
---
f0368f
 src/go-build-wrapper | 14 ++++++++++----
f0368f
 1 file changed, 10 insertions(+), 4 deletions(-)
f0368f
f0368f
diff --git a/src/go-build-wrapper b/src/go-build-wrapper
f0368f
index ef4aafc8b024..e82e42ca8151 100755
f0368f
--- a/src/go-build-wrapper
f0368f
+++ b/src/go-build-wrapper
f0368f
@@ -32,9 +32,9 @@ if ! cd "$1"; then
f0368f
     exit 1
f0368f
 fi
f0368f
 
f0368f
-tags=""
f0368f
+tags="-tags rpm_crashtraceback,${BUILDTAGS:-}"
f0368f
 if $6; then
f0368f
-    tags="-tags migration_path_for_coreos_toolbox"
f0368f
+    tags="$tags,migration_path_for_coreos_toolbox"
f0368f
 fi
f0368f
 
f0368f
 if ! libc_dir=$("$4" --print-file-name=libc.so); then
f0368f
@@ -69,11 +69,17 @@ fi
f0368f
 
f0368f
 dynamic_linker="/run/host$dynamic_linker_canonical_dirname/$dynamic_linker_basename"
f0368f
 
f0368f
+unset LDFLAGS
f0368f
+
f0368f
 # shellcheck disable=SC2086
f0368f
 go build \
f0368f
+        -buildmode pie \
f0368f
+        -compiler gc \
f0368f
         $tags \
f0368f
-        -trimpath \
f0368f
-        -ldflags "-extldflags '-Wl,-dynamic-linker,$dynamic_linker -Wl,-rpath,/run/host$libc_dir_canonical_dirname' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" \
f0368f
+        -ldflags "${LDFLAGS:-} -compressdwarf=false -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \n') -extldflags '-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-dynamic-linker,$dynamic_linker -Wl,-rpath,/run/host$libc_dir_canonical_dirname' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" \
f0368f
+        -a \
f0368f
+        -v \
f0368f
+        -x \
f0368f
         -o "$2/toolbox"
f0368f
 
f0368f
 exit "$?"
f0368f
-- 
f0368f
2.31.1
f0368f