Issue eBPF kernel programs with ldc?
Alexander Zhirov
azhirov1991 at gmail.com
Fri Feb 13 21:55:56 UTC 2026
On Sunday, 5 January 2025 at 14:17:29 UTC, data pulverizer wrote:
> ```d
> //hello_world.d
>
> @system:
> @nogc:
> extern(C):
>
> @(ldc.attributes.section("license")) __gshared
> immutable(char)[3] LICENSE = "GPL";
>
> import core.stdc.stdlib;
> import core.stdc.stdarg;
> import ldc.attributes;
>
> long bpf_trace_printk(const(char)* fmt, uint fmt_size, ...);
> @(ldc.attributes.section("xdp_md")) struct xdp_md;
>
> @(ldc.attributes.section("xdp")) int xdp_prog(xdp_md* ctx)
> {
> bpf_trace_printk("Hello, eBPF!\n", 14);
> return 0;
> }
> ```
Just fix
```d
@system:
@nogc:
nothrow:
extern(C):
import ldc.attributes;
@(ldc.attributes.section("license"))
__gshared immutable(char)[4] LICENSE = "GPL\0";
struct xdp_md
{
uint data;
uint data_end;
uint data_meta;
uint ingress_ifindex;
uint rx_queue_index;
uint egress_ifindex;
}
enum int BPF_FUNC_trace_printk = 6;
alias BpfTracePrintk =
extern(C) long function(const(char)* fmt, uint fmt_size, ...)
@nogc nothrow;
enum BpfTracePrintk bpf_trace_printk =
cast(BpfTracePrintk)BPF_FUNC_trace_printk;
enum int XDP_PASS = 2;
@(ldc.attributes.section("xdp"))
int xdp_prog(xdp_md* ctx)
{
bpf_trace_printk("Hello, eBPF!\n", 14);
return XDP_PASS;
}
```
Build:
```sh
ldc2 -c -O2 -release -betterC --nogc -fno-moduleinfo --march=bpf
hello_world.d -of=hello_world_d.o
llvm-objcopy --remove-section .eh_frame --remove-section
.rel.eh_frame hello_world_d.o
```
More information about the Digitalmars-d-learn
mailing list