652443
From 89129bd096c8bfac4ff84fc19726898cc901c1fc Mon Sep 17 00:00:00 2001
bab63a
From: Debarshi Ray <rishi@fedoraproject.org>
bab63a
Date: Mon, 29 Jun 2020 17:57:47 +0200
bab63a
Subject: [PATCH] build: Make the build flags match RHEL's %{gobuild}
bab63a
bab63a
The Go toolchain doesn't play well with passing compiler and linker
bab63a
flags via environment variables. The linker flags require a second
bab63a
level of quoting, which leaves the build system without a quote level
bab63a
to assign the flags to an environment variable like GOFLAGS.
bab63a
bab63a
This is one reason why RHEL doesn't have a RPM macro with only the
bab63a
flags. The %{gobuild} RPM macro includes the entire 'go build ...'
bab63a
invocation.
bab63a
bab63a
The Go toolchain also doesn't like the LDFLAGS environment variable as
bab63a
exported by RHEL's %{meson} RPM macro, and RHEL's RPM toolchain doesn't
bab63a
like the compressed DWARF data generated by the Go toolchain.
bab63a
bab63a
Note that these flags are meant for every CPU architecture other than
bab63a
PPC64, and should be kept updated to match RHEL's Go guidelines. Use
bab63a
'rpm --eval "%{gobuild}"' to expand the %{gobuild} macro.
bab63a
---
652443
 src/go-build-wrapper | 14 ++++++++++----
652443
 1 file changed, 10 insertions(+), 4 deletions(-)
bab63a
bab63a
diff --git a/src/go-build-wrapper b/src/go-build-wrapper
652443
index ef4aafc8b024..e82e42ca8151 100755
bab63a
--- a/src/go-build-wrapper
bab63a
+++ b/src/go-build-wrapper
652443
@@ -32,9 +32,9 @@ if ! cd "$1"; then
bab63a
     exit 1
bab63a
 fi
bab63a
 
652443
-tags=""
652443
+tags="-tags rpm_crashtraceback,${BUILDTAGS:-}"
652443
 if $6; then
652443
-    tags="-tags migration_path_for_coreos_toolbox"
652443
+    tags="$tags,migration_path_for_coreos_toolbox"
652443
 fi
652443
 
652443
 if ! libc_dir=$("$4" --print-file-name=libc.so); then
652443
@@ -69,11 +69,17 @@ fi
652443
 
652443
 dynamic_linker="/run/host$dynamic_linker_canonical_dirname/$dynamic_linker_basename"
652443
 
bab63a
+unset LDFLAGS
652443
+
652443
 # shellcheck disable=SC2086
652443
 go build \
652443
+        -buildmode pie \
652443
+        -compiler gc \
652443
         $tags \
652443
-        -trimpath \
652443
-        -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" \
652443
+        -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" \
652443
+        -a \
652443
+        -v \
652443
+        -x \
652443
         -o "$2/toolbox"
652443
 
bab63a
 exit "$?"
bab63a
-- 
bab63a
2.31.1
bab63a