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