|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +package gen |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/cilium/ebpf/internal/testutils" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + func1 = `__attribute__((section("socket"), used)) int func1() { return 1; }` |
| 15 | + func2 = `__attribute__((section("socket"), used)) int func2() { return 2; }` |
| 16 | +) |
| 17 | + |
| 18 | +func TestLink(t *testing.T) { |
| 19 | + if testing.Short() { |
| 20 | + t.SkipNow() |
| 21 | + } |
| 22 | + |
| 23 | + dir := t.TempDir() |
| 24 | + mustWriteFile(t, dir, "func1.c", func1) |
| 25 | + mustWriteFile(t, dir, "func2.c", func2) |
| 26 | + |
| 27 | + // Compile first object |
| 28 | + obj1 := filepath.Join(dir, "func1.o") |
| 29 | + err := Compile(CompileArgs{ |
| 30 | + CC: testutils.ClangBin(t), |
| 31 | + DisableStripping: true, |
| 32 | + Workdir: dir, |
| 33 | + Source: filepath.Join(dir, "func1.c"), |
| 34 | + Dest: obj1, |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + t.Fatal("Can't compile func1:", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Compile second object |
| 41 | + obj2 := filepath.Join(dir, "func2.o") |
| 42 | + err = Compile(CompileArgs{ |
| 43 | + CC: testutils.ClangBin(t), |
| 44 | + DisableStripping: true, |
| 45 | + Workdir: dir, |
| 46 | + Source: filepath.Join(dir, "func2.c"), |
| 47 | + Dest: obj2, |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + t.Fatal("Can't compile func2:", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Link both objects |
| 54 | + linked := filepath.Join(dir, "linked.o") |
| 55 | + err = Link(LinkArgs{ |
| 56 | + Dest: linked, |
| 57 | + Sources: []string{obj1, obj2}, |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + t.Fatal("Can't link objects:", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Verify the linked file exists and has content |
| 64 | + stat, err := os.Stat(linked) |
| 65 | + if err != nil { |
| 66 | + t.Fatal("Can't stat linked file:", err) |
| 67 | + } |
| 68 | + |
| 69 | + if stat.Size() == 0 { |
| 70 | + t.Error("Linked file is empty") |
| 71 | + } |
| 72 | +} |
0 commit comments