dup2 on x64

duplicate a file descriptor

sys_dup2(unsigned int oldfd, unsigned int newfd)
number33 (0x21)
abicommon
entry pointsys_dup2
sinceLinux 3.3 or earlier (3.3 is the first release scanned for x64)
man pagedup2(2)
sourceelixir.bootlin.com

Registers

registervalue
rax33, the syscall number
rdiunsigned int oldfd
rsiunsigned int newfd
raxthe return value

Then run syscall to enter the kernel.

The syscall instruction clobbers rcx (the return address lands in rcx) and r11 (the flags land in r11).

Since Linux 5.14 the kernel reads the syscall number from eax and ignores the upper 32 bits of rax. Older kernels fail with ENOSYS when those bits are not zero, and mov eax, N clears them, so it works on both.

Same syscall on other architectures

Get this as JSON: https://api.syscall.sh/v2/syscalls/x64/dup2