2

I would like to reduce the size of the plugin (.so file) by removing common symbols used in both the plugin and the main program, e.g. go runtime and standard library.

Here is my test project.

main.go:

package main

import (
    "fmt"
    "os"
    "plugin"
)

type Plugin interface {
    SayHello()
    SayGoodbye()
}

func main() {
    if len(os.Args) < 2 {
        fmt.Fprintf(os.Stderr, "Usage: %s <plugin.so>\n", os.Args[0])
        os.Exit(1)
    }

    p, err := plugin.Open(os.Args[1])
    if err != nil {
        fmt.Fprintf(os.Stderr, "plugin.Open: %v\n", err)
        os.Exit(1)
    }

    piTemp, err := p.Lookup("Plugin")
    if err != nil {
        fmt.Fprintf(os.Stderr, "plugin.Open: %v\n", err)
        os.Exit(1)
    }

    pi, ok := piTemp.(Plugin)
    if !ok {
        fmt.Fprintf(os.Stderr, "Unable to cast helloTemp to Plugin interface\n")
        os.Exit(1)
    }

    pi.SayHello()
    pi.SayGoodbye()
}

plugin/plugin.go:

package main

import "fmt"

type PluginImpl struct {}

var Plugin PluginImpl

func (pi *PluginImpl) SayHello() {
    fmt.Printf("Hello from the plugin!\n")
}

func (pi *PluginImpl) SayGoodbye() {
    fmt.Printf("Goodbye from the plugin!\n")
}

build.sh:

#!/bin/sh

set -e

go clean ./...
go build
cd plugin
go build -buildmode=plugin

Usage example:

$ ./plugins-test ./plugin/plugin.so
Hello from the plugin!
Goodbye from the plugin!

I tried to follow the steps from this article and here what I got:

$ go install -buildmode=shared std
$ go install -buildmode=shared -linkshared go-sandbox/plugins-test
panic: runtime error: index out of range

goroutine 1 [running]:
cmd/go/internal/work.(*Builder).linkSharedAction.func2(0x84607e0)
    /usr/local/go/src/cmd/go/internal/work/action.go:745 +0x7dd
cmd/go/internal/work.(*Builder).cacheAction(0x8d7cfc0, 0x90fdd00, 0x14, 0x0, 0x8efc740, 0x90fdd00)
    /usr/local/go/src/cmd/go/internal/work/action.go:314 +0x84
cmd/go/internal/work.(*Builder).linkSharedAction(0x8d7cfc0, 0x1, 0x1, 0x8e2373a, 0x6, 0x8eec840, 0x8e2373a)
    /usr/local/go/src/cmd/go/internal/work/action.go:738 +0x201
cmd/go/internal/work.(*Builder).buildmodeShared(0x8d7cfc0, 0x1, 0x1, 0x8c18050, 0x1, 0x2, 0x0, 0x0, 0x0, 0x8eec840, ...)
    /usr/local/go/src/cmd/go/internal/work/action.go:649 +0xe8
cmd/go/internal/work.InstallPackages(0x8c18050, 0x1, 0x2, 0x0, 0x0, 0x0)
    /usr/local/go/src/cmd/go/internal/work/build.go:503 +0xb9c
cmd/go/internal/work.runInstall(0x884ae20, 0x8c18050, 0x1, 0x2)
    /usr/local/go/src/cmd/go/internal/work/build.go:417 +0x52
main.main()
    /usr/local/go/src/cmd/go/main.go:219 +0x8a4

The behavior is the same on go1.11.2 linux/386 and go1.11.2 linux/arm. On MacOS the feature seems to be missing --- -buildmode=shared not supported on darwin/amd64.

Am I doing something wrong or this feature is a little bit broken?

Aleksander Alekseev
  • 1,854
  • 4
  • 25
  • 57
  • 1
    I'm running into the same, did you finally found out what was the problem ? – Oswin Apr 05 '19 at 15:37
  • Yep. After facing a few more problems we came to the following conclusion. Just never use go plugins for anything :) – Aleksander Alekseev Apr 05 '19 at 16:21
  • That's not the answer I was hoping for :-/ For others, I don't run into this problem if I build from the root folder. In your example "go build -buildmode=plugin plugin/plugin.so" works. After that I run into this problem : https://github.com/golang/go/issues/18671 so not sure if I'm now a step further or not :-/ – Oswin Apr 08 '19 at 13:08

0 Answers0