Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile of Go plugin or shared library for Linux ARM64 #129

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions test/plugin/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the MIT License

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@hermetic_cc_toolchain//rules:platform.bzl", "platform_binary")

go_library(
name = "plugin_lib",
srcs = ["plugin.go"],
importpath = "github.com/uber/hermetic_cc_toolchain/test/plugin",
visibility = ["//visibility:private"],
)

go_binary(
name = "plugin.so",
embed = [":plugin_lib"],
linkmode = "plugin",
visibility = ["//visibility:private"],
)

# gazelle:exclude plugin_test.go
[
(
platform_binary(
name = "plugin_{}.so".format(name),
src = ":plugin.so",
platform = platform,
visibility = ["//visibility:private"],
),
go_test(
name = "plugin_{}_test".format(name),
srcs = ["plugin_test.go"],
data = [":plugin_{}.so".format(name)],
env = {
"PLUGIN_BINARY": "$(rlocationpath :plugin_{}.so)".format(name),
"PLUGIN_ELF_MACH": elf_mach,
},
deps = ["@io_bazel_rules_go//go/runfiles:go_default_library"],
),
)
for (name, platform, elf_mach) in [
("linux_amd64_musl", "//libc_aware/platform:linux_amd64_musl", "x86_64"),
("linux_amd64", "//libc_aware/platform:linux_amd64_gnu.2.28", "x86_64"),
("linux_arm64", "//libc_aware/platform:linux_arm64_gnu.2.28", "aarch64"),
]
]

test_suite(
name = "plugin_test",
tests = [
":plugin_{}_test".format(name)
for name in (
"linux_amd64_musl",
"linux_amd64",
"linux_arm64",
)
],
)
16 changes: 16 additions & 0 deletions test/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 Uber Technologies, Inc.
// Licensed under the MIT License
//
// Package main tests that Zig can compile a Go plugin.
//
// As of writing, this fails:
//
// $ CGO_ENABLED=1 CC="zig cc" GOOS=linux GOARCH=arm64 go build -linkmode=plugin .
// link: ARM64 external linker must be gold (issue #15696, 22040), but is not: zig ld 0.11.0
//
// More context: https://github.com/uber/hermetic_cc_toolchain/issues/122
//
// This fails, because Go toolchain ask the string "GNU gold" in the output of
// `$CC -fuse-ld=gold -Wl,--version` when linking the Go plugin.
// See: https://go.googlesource.com/go/+/8c92897e15d15fbc664cd5a05132ce800cf4017f/src/cmd/link/internal/ld/lib.go#1628
package main
62 changes: 62 additions & 0 deletions test/plugin/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 Uber Technologies, Inc.
// Licensed under the MIT License

package main

import (
"debug/elf"
"os"
"testing"

"github.com/bazelbuild/rules_go/go/runfiles"
)

func TestBuild(t *testing.T) {
expectedMachineString := os.Getenv("PLUGIN_ELF_MACH")
if expectedMachineString == "" {
t.Fatalf("Env variable PLUGIN_ELF_MACH not defined")
}

pluginBinaryRPath := os.Getenv("PLUGIN_BINARY")

pluginBinaryPath, err := runfiles.Rlocation(pluginBinaryRPath)
if err != nil {
t.Fatalf("can't find runfile %q: %v", pluginBinaryRPath, err)
}

bin, err := elf.Open(pluginBinaryPath)
if err != nil {
t.Fatalf("can't open file %q: %v", pluginBinaryPath, err)
}

t.Cleanup(func() {
if err := bin.Close(); err != nil {
t.Errorf("can't close ELF file: %v", err)
}
})

if bin.Type != elf.ET_DYN {
t.Errorf("ELF type %q incorrect, %q expected", bin.Type, elf.ET_DYN)
}

expectedMachine, _ := map[string]elf.Machine{
"x86_64": elf.EM_X86_64,
"aarch64": elf.EM_AARCH64,
}[expectedMachineString]

if bin.Machine != expectedMachine {
t.Errorf("ELF machine %q incorrect, %q expected", bin.Machine, expectedMachine)
}

var hasDynamicSection bool
for _, s := range bin.Sections {
if s.Type == elf.SHT_DYNAMIC {
hasDynamicSection = true
break
}
}

if !hasDynamicSection {
t.Error("No dynamic section found in the ELF binary")
}
}
19 changes: 19 additions & 0 deletions toolchain/zig-wrapper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ fn parseArgs(
while (argv_it.next()) |arg|
try args.append(arena, arg);

// Go toolchain requires the command `$CC -fuse-ld=gold -Wl,--version`
// return the string "GNU gold" when linking a Go plugin or
// a shared library for Linux ARM64.
// See https://go.googlesource.com/go/+/8c92897e15d15fbc664cd5a05132ce800cf4017f/src/cmd/link/internal/ld/lib.go#1628
if (run_mode == .cc) {
var use_ld_gold: bool = false;
var link_version: bool = false;
for (args.items) |arg| {
if (mem.eql(u8, "-fuse-ld=gold", arg))
use_ld_gold = true;
if (mem.eql(u8, "-Wl,--version", arg))
link_version = true;
}

if (use_ld_gold and link_version) {
std.debug.print("GNU gold\n", .{});
}
}

return ParseResults{ .exec = .{ .args = args, .env = env } };
}

Expand Down
3 changes: 3 additions & 0 deletions tools/mod-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ cd "$(git rev-parse --show-toplevel)"
echo "--- go mod tidy"
tools/bazel run @go_sdk//:bin/go -- mod tidy "$@"

# Ignore import of @io_bazel_rules_go//go/runfiles
sed -ri '/github.com\/bazelbuild\/rules_go/d' go.mod go.sum

echo "--- gazelle-update-repos"
exec tools/bazel run //:gazelle-update-repos