Created
November 21, 2018 11:47
-
-
Save Hollerberg/5fc64f8abf0f16d4e801c4ad348f21b6 to your computer and use it in GitHub Desktop.
golang musl libc shared object constructor reproducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
docker build -t golang-alpine . && docker run --tty --interactive -h golang-alpine golang-alpine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:1.11.2-alpine | |
USER root | |
RUN apk update && apk add musl-dbg gcc libc-dev | |
COPY preload.c /go | |
COPY gogo.go /go | |
COPY runtest.sh /go | |
RUN gcc -ggdb -fPIC -shared -o libpreload.so preload.c && go build -o gogo-int-linker gogo.go && go build -ldflags '-linkmode external' -o gogo-ext-linker gogo.go | |
ENTRYPOINT [ "/bin/sh" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
func main() { | |
// add explicit reference to net/http to enforce dynamic linking | |
rc := http.StatusOK | |
rc *= 2 | |
fmt.Println("hi there") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
// ---------------------------------------------------------------------------- | |
__attribute__((constructor)) void doInit(void) { | |
static const char msg[] = "---> your preloaded shared object says hey <---\n"; | |
write(1, msg, sizeof(msg) - 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
LIB=libpreload.so | |
run() { | |
echo LD_LIBRARY_PATH=$PWD LD_PRELOAD=$LIB $1 | |
LD_LIBRARY_PATH=$PWD LD_PRELOAD=$LIB $1 | |
} | |
echo 'running gogo (built with internal linker' | |
run ./gogo-int-linker | |
echo | |
echo 'running /go/gogo-ext-linker (built with external linker)' | |
run ./gogo-ext-linker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment