//             position independent executables (PIE). Packages not named
 //             main are ignored.
 //
+//     -buildmode=plugin
+//             Build the listed main packages, plus all packages that they
+//             import, into a Go plugin. Packages not named main are ignored.
+//
 //
 // File types
 //
 
                        fatalf("-buildmode=shared and -o not supported together")
                }
                ldBuildmode = "shared"
+       case "plugin":
+               pkgsFilter = pkgsMain
+               if gccgo {
+                       codegenArg = "-fPIC"
+               } else {
+                       switch platform {
+                       case "linux/amd64", "linux/arm", "linux/arm64", "linux/386",
+                               "android/amd64", "android/arm", "android/arm64", "android/386":
+                       default:
+                               fatalf("-buildmode=plugin not supported on %s\n", platform)
+                       }
+                       codegenArg = "-dynlink"
+               }
+               exeSuffix = ".so"
+               ldBuildmode = "plugin"
        default:
                fatalf("buildmode=%s not supported", buildBuildmode)
        }
        perm := os.FileMode(0666)
        if a1.link {
                switch buildBuildmode {
-               case "c-archive", "c-shared":
+               case "c-archive", "c-shared", "plugin":
                default:
                        perm = 0777
                }
 // maybePIC adds -fPIC to the list of arguments if needed.
 func (tools gccgoToolchain) maybePIC(args []string) []string {
        switch buildBuildmode {
-       case "c-shared", "shared":
+       case "c-shared", "shared", "plugin":
                args = append(args, "-fPIC")
        }
        return args
 
                Build the listed main packages and everything they import into
                position independent executables (PIE). Packages not named
                main are ignored.
+
+       -buildmode=plugin
+               Build the listed main packages, plus all packages that they
+               import, into a Go plugin. Packages not named main are ignored.
 `,
 }
 
        useBindir := p.Name == "main"
        if !p.Standard {
                switch buildBuildmode {
-               case "c-archive", "c-shared":
+               case "c-archive", "c-shared", "plugin":
                        useBindir = false
                }
        }
                importPaths = append(importPaths, "syscall")
        }
 
-       // Currently build modes c-shared, pie, and -linkshared force
+       // Currently build modes c-shared, pie, plugin, and -linkshared force
        // external linking mode, and external linking mode forces an
        // import of runtime/cgo.
        pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64")
-       if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || pieCgo || buildLinkshared) {
+       if p.Name == "main" && !p.Goroot && (buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared) {
                importPaths = append(importPaths, "runtime/cgo")
        }