Repository: go
Issue: cmd/link: Support buildmode=pie with internal linking on Linux 386
Labels: NeedsInvestigation,compiler/runtime

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.17 linux/386
</pre>

### Does this issue reproduce with the latest release?
Yes, I suppose this is the latest stable version for 386.


### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="386"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_386"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
GO386="sse2"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m32 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build698535093=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

<details><summary><code>uname -a</code> Output</summary><br><pre>
$ uname -a
Linux XXXX 4.19.0-17-686-pae #1 SMP Debian 4.19.194-3 (2021-07-18) i686 GNU/Linux
</pre></details>

<details><summary><code>cat /etc/os-release</code> Output</summary><br><pre>
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->
Tried compiling a hello world program with `buildmode=pie` and `-linkmode=internal` on a `386` machine.

```hello_world.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, world!")
}

```

```
go build -buildmode=pie -ldflags="-linkmode=internal"  -o hello_world_pie ./hello_world.go
```

### What did you expect to see?
I expected the program to get compiled successfully with PIE and internal linking enabled.


### What did you see instead?
Build fails with the following error:
```
# command-line-arguments
/usr/local/go/pkg/tool/linux_386/link: internal linking requested but external linking required: buildmode=pie
```

For context, as mentioned in #31343, building a program with PIE and internal linking works on amd64.
<details><summary><code>uname -a</code> Output</summary><br><pre>
Linux xxxx-yyyy 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
</pre></details>

<details><summary><code>cat /etc/os-release</code> Output</summary><br><pre>
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
</pre></details>

Build command:
```
go build -buildmode=pie -ldflags="-linkmode=internal" -o hello_world_pie hello_world.go
```

The compilation is successful, and I have ensured that both PIE and internal linking are enabled.

<details><summary><code>hardening-check ./hello_world_pie</code> Output</summary><br><pre>
./hello_world_pie:
 Position Independent Executable: yes
 Stack protected: no, not found!
 Fortify Source functions: unknown, no protectable libc functions used
 Read-only relocations: yes
 Immediate binding: no, not found!
 Stack clash protection: unknown, no -fstack-clash-protection instructions found
 Control flow integrity: unknown, no -fcf-protection instructions found!
</pre></details>

<details><summary><code>objdump -p hello_world_pie</code> Output</summary><br><pre>
hello_world_pie:     file format elf64-x86-64

Program Header:
    PHDR off    0x0000000000000040 vaddr 0x0000000000400040 paddr 0x0000000000400040 align 2**12
         filesz 0x00000000000002a0 memsz 0x00000000000002a0 flags r--
  INTERP off    0x0000000000000fe4 vaddr 0x0000000000400fe4 paddr 0x0000000000400fe4 align 2**0
         filesz 0x000000000000001c memsz 0x000000000000001c flags r--
    NOTE off    0x0000000000000f80 vaddr 0x0000000000400f80 paddr 0x0000000000400f80 align 2**2
         filesz 0x0000000000000064 memsz 0x0000000000000064 flags r--
    LOAD off    0x0000000000000000 vaddr 0x0000000000400000 paddr 0x0000000000400000 align 2**12
         filesz 0x0000000000099b60 memsz 0x0000000000099b60 flags r-x
    LOAD off    0x000000000009a000 vaddr 0x000000000049a000 paddr 0x000000000049a000 align 2**12
         filesz 0x0000000000072c85 memsz 0x0000000000072c85 flags r--
    LOAD off    0x000000000010d000 vaddr 0x000000000050d000 paddr 0x000000000050d000 align 2**12
         filesz 0x0000000000086239 memsz 0x0000000000086239 flags rw-
   RELRO off    0x000000000010d000 vaddr 0x000000000050d000 paddr 0x000000000050d000 align 2**12
         filesz 0x0000000000086239 memsz 0x0000000000086239 flags ---
    LOAD off    0x0000000000194000 vaddr 0x0000000000594000 paddr 0x0000000000594000 align 2**12
         filesz 0x0000000000015a20 memsz 0x00000000000481c8 flags rw-
 DYNAMIC off    0x0000000000194040 vaddr 0x0000000000594040 paddr 0x0000000000594040 align 2**3
         filesz 0x00000000000000b0 memsz 0x00000000000000b0 flags rw-
     TLS off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**3
         filesz 0x0000000000000000 memsz 0x0000000000000008 flags r--
   STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**3
         filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-
0x65041580 off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**3
         filesz 0x0000000000000000 memsz 0x0000000000000000 flags --- 2a00

Dynamic Section:
  HASH                 0x000000000050c9e0
  SYMTAB               0x000000000050ca00
  SYMENT               0x0000000000000018
  STRTAB               0x000000000050c9d8
  STRSZ                0x0000000000000001
  RELA                 0x00000000004b87b0
  RELASZ               0x0000000000054228
  RELAENT              0x0000000000000018
  PLTGOT               0x0000000000594020
  DEBUG                0x0000000000000000


</pre></details>