Skip to content

Instantly share code, notes, and snippets.

@Hollerberg
Created November 21, 2018 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hollerberg/5fc64f8abf0f16d4e801c4ad348f21b6 to your computer and use it in GitHub Desktop.
Save Hollerberg/5fc64f8abf0f16d4e801c4ad348f21b6 to your computer and use it in GitHub Desktop.
golang musl libc shared object constructor reproducer
#!/bin/sh
docker build -t golang-alpine . && docker run --tty --interactive -h golang-alpine golang-alpine
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" ]
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")
}
#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);
}
#!/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