Skip to content

Commit bd1e178

Browse files
committed
fix: use static-pie linking for Go examples, remove patch_interp.py
Build with CGO_ENABLED=1 and -extldflags "-static-pie" instead of patching PT_INTERP out of CGO_ENABLED=0 PIE binaries. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent d10746e commit bd1e178

4 files changed

Lines changed: 4 additions & 78 deletions

File tree

examples/go-http/Dockerfile

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@ WORKDIR /src
55
COPY ./main.go /src/main.go
66

77
RUN set -xe; \
8-
CGO_ENABLED=0 \
8+
CGO_ENABLED=1 \
99
GOARCH=amd64 \
1010
go build \
11-
-buildmode=pie \
12-
-ldflags="-linkmode=internal" \
11+
-ldflags='-linkmode external -extldflags "-static-pie" -s -w' \
1312
-o /server main.go \
1413
;
1514

16-
# Strip PT_INTERP from the static PIE binary: Go's internal linker adds
17-
# /lib64/ld-linux-x86-64.so.2 as interpreter for PIE on Linux, but the
18-
# binary is fully self-contained and does not need a dynamic linker.
19-
# Unikraft's elfloader fails when the interpreter is listed but absent.
20-
# Patch the program header: change PT_INTERP (type=3) -> PT_NULL (type=0).
21-
COPY patch_interp.py /patch_interp.py
22-
RUN python3 /patch_interp.py /server
23-
2415
FROM scratch AS rootfs
2516

2617
COPY --from=build /server /bin/server

examples/go-http/patch_interp.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/go/Dockerfile

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@ WORKDIR /src
55
COPY ./hello.go /src/hello.go
66

77
RUN set -xe; \
8-
CGO_ENABLED=0 \
8+
CGO_ENABLED=1 \
99
GOARCH=amd64 \
1010
go build \
11-
-buildmode=pie \
12-
-ldflags="-linkmode=internal" \
11+
-ldflags='-linkmode external -extldflags "-static-pie" -s -w' \
1312
-o /hello hello.go \
1413
;
1514

16-
# Strip PT_INTERP from the static PIE binary: Go's internal linker adds
17-
# /lib64/ld-linux-x86-64.so.2 as interpreter for PIE on Linux, but the
18-
# binary is fully self-contained and does not need a dynamic linker.
19-
# Unikraft's elfloader fails when the interpreter is listed but absent.
20-
# Patch the program header: change PT_INTERP (type=3) -> PT_NULL (type=0).
21-
COPY patch_interp.py /patch_interp.py
22-
RUN python3 /patch_interp.py /hello
23-
2415
FROM scratch AS rootfs
2516

2617
COPY --from=build /hello /bin/hello

examples/go/patch_interp.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)