authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-26 10:46:40+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-26 10:53:30+01:00
logd6f43a1eac6d8d0405b870a46f202d71957b5ba4
tree0bfdd47e5600b7e4430c167d8c30b073c4da8604
parenta96b6ad83f4385dd47112084baeeb24e7e9356fa

bpf: do not invoke lld when linking eBPF relocatables

Due to a deficiency in LLD, we need to special-case BPF to a simple file copy when generating relocatables. Normally, we would expect `lld -r` to work. However, because LLD wants to resolve BPF relocations which it shouldn't, it fails before even generating the relocatable. Co-authored-by: Matthew Knight <mattnite@protonmail.com>

3 files changed, 16 insertions(+), 4 deletions(-)

lib/std/target.zig+11
......@@ -895,6 +895,13 @@ pub const Target = struct {
895895 };
896896 }
897897
898 pub fn isBpf(arch: Arch) bool {
899 return switch (arch) {
900 .bpfel, .bpfeb => true,
901 else => false,
902 };
903 }
904
898905 pub fn parseCpuModel(arch: Arch, cpu_name: []const u8) !*const Cpu.Model {
899906 for (arch.allCpuModels()) |cpu| {
900907 if (mem.eql(u8, cpu_name, cpu.name)) {
......@@ -1421,6 +1428,10 @@ pub const Target = struct {
14211428 return self.os.tag.isBSD();
14221429 }
14231430
1431 pub fn isBpfFreestanding(self: Target) bool {
1432 return self.cpu.arch.isBpf() and self.os.tag == .freestanding;
1433 }
1434
14241435 pub fn isGnuLibC_os_tag_abi(os_tag: Os.Tag, abi: Abi) bool {
14251436 return os_tag == .linux and abi.isGnu();
14261437 }
src/link.zig-3
......@@ -532,9 +532,6 @@ pub const File = struct {
532532 return;
533533 }
534534
535 if (base.options.output_mode == .Obj)
536 return;
537
538535 const use_lld = build_options.have_llvm and base.options.use_lld;
539536 if (use_lld and base.options.output_mode == .Lib and base.options.link_mode == .Static) {
540537 return base.linkAsArchive(comp);
src/link/Elf.zig+5-1
......@@ -1381,7 +1381,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13811381 }
13821382
13831383 const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path});
1384 if (self.base.options.output_mode == .Obj and self.base.options.lto) {
1384
1385 // Due to a deficiency in LLD, we need to special-case BPF to a simple file copy when generating
1386 // relocatables. Normally, we would expect `lld -r` to work. However, because LLD wants to resolve
1387 // BPF relocations which it shouldn't, it fails before even generating the relocatable.
1388 if (self.base.options.output_mode == .Obj and (self.base.options.lto or target.isBpfFreestanding())) {
13851389 // In this case we must do a simple file copy
13861390 // here. TODO: think carefully about how we can avoid this redundant operation when doing
13871391 // build-obj. See also the corresponding TODO in linkAsArchive.