Skip to content

Commit 3eb690b

Browse files
authored
Initial LoongArch64 support (#2529)
Signed-off-by: Bingwu Zhang <[email protected]>
1 parent fa7d76d commit 3eb690b

File tree

30 files changed

+3514
-36
lines changed

30 files changed

+3514
-36
lines changed

.github/workflows/ci.yml

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: |
3434
git fetch origin
3535
git log --oneline --graph -10
36-
36+
3737
- name: Fix libcdb-cache permissions
3838
id: fix-perms
3939
run: |
@@ -102,11 +102,12 @@ jobs:
102102
binutils-s390x-linux-gnu \
103103
binutils-sparc64-linux-gnu \
104104
binutils-riscv64-linux-gnu \
105+
binutils-loongarch64-linux-gnu \
105106
gcc-multilib \
106107
libc6-dbg \
107108
elfutils \
108109
patchelf
109-
110+
110111
- name: Testing Corefiles
111112
run: |
112113
ulimit -a
@@ -232,7 +233,7 @@ jobs:
232233
name: coverage-${{ matrix.python_version }}
233234
path: .coverage*
234235
include-hidden-files: true
235-
236+
236237
- name: Fix libcdb-cache permissions
237238
run: |
238239
container_id=$(docker ps --filter volume=/home/runner/libcdb-cache --no-trunc --format "{{.ID}}")
@@ -255,15 +256,15 @@ jobs:
255256
run: |
256257
pip install --upgrade pip
257258
pip install --upgrade --editable .
258-
259+
259260
- name: Install documentation dependencies
260261
run: pip install -r docs/requirements.txt
261-
262+
262263
- name: Sanity checks
263264
run: |
264265
python -bb -c 'from pwn import *'
265266
python -bb examples/text.py
266-
267+
267268
- name: Coverage doctests
268269
run: |
269270
python -bb -m coverage run -m sphinx -b doctest docs/source docs/build/doctest

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The table below shows which release corresponds to each branch, and what date th
8282
- [#2517][2517] Allow to passthru kwargs on `ssh.__getattr__` convenience function to fix SSH motd problems
8383
- [#2527][2527] Allow setting debugger path via `context.gdb_binary`
8484
- [#2530][2530] Do NOT error when passing directory arguments in `checksec` commandline tool.
85+
- [#2529][2529] Add LoongArch64 support
8586

8687
[2519]: https://github.com/Gallopsled/pwntools/pull/2519
8788
[2507]: https://github.com/Gallopsled/pwntools/pull/2507
@@ -91,6 +92,7 @@ The table below shows which release corresponds to each branch, and what date th
9192
[2517]: https://github.com/Gallopsled/pwntools/pull/2517
9293
[2527]: https://github.com/Gallopsled/pwntools/pull/2527
9394
[2530]: https://github.com/Gallopsled/pwntools/pull/2530
95+
[2529]: https://github.com/Gallopsled/pwntools/pull/2529
9496

9597
## 4.15.0 (`beta`)
9698

@@ -209,7 +211,7 @@ The table below shows which release corresponds to each branch, and what date th
209211
- [#2308][2308] Fix WinExec shellcraft to make sure it's 16 byte aligned
210212
- [#2279][2279] Make `pwn template` always set context.binary
211213
- [#2310][2310] Add support to start a process on Windows
212-
- [#2335][2335] Add lookup optimizations in DynELF
214+
- [#2335][2335] Add lookup optimizations in DynELF
213215
- [#2334][2334] Speed up disasm commandline tool with colored output
214216
- [#2328][2328] Lookup using $PATHEXT file extensions in `which` on Windows
215217
- [#2189][2189] Explicitly define p64/u64 functions for IDE support
@@ -296,7 +298,7 @@ The table below shows which release corresponds to each branch, and what date th
296298

297299
## 4.11.0
298300

299-
- [#2185][2185] make fmtstr module able to create payload without $ notation
301+
- [#2185][2185] make fmtstr module able to create payload without $ notation
300302
- [#2103][2103] Add search for libc binary by leaked function addresses `libcdb.search_by_symbol_offsets()`
301303
- [#2177][2177] Support for RISC-V 64-bit architecture
302304
- [#2186][2186] Enhance `ELF.nx` and `ELF.execstack`
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.. testsetup:: *
2+
3+
from pwn import *
4+
context.clear(arch='loongarch64')
5+
6+
import doctest
7+
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
8+
9+
:mod:`pwnlib.shellcraft.loongarch64` --- Shellcode for LoongArch64
10+
==========================================================
11+
12+
:mod:`pwnlib.shellcraft.loongarch64`
13+
--------------------------------
14+
15+
.. automodule:: pwnlib.shellcraft.loongarch64
16+
:members:
17+
18+
:mod:`pwnlib.shellcraft.loongarch64.linux`
19+
--------------------------------------
20+
21+
.. automodule:: pwnlib.shellcraft.loongarch64.linux
22+
:members:

extra/docker/base/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Based on Ubuntu
44
############################################################
55

6-
FROM ubuntu:jammy
6+
FROM ubuntu:noble
77
MAINTAINER Maintainer Gallopsled et al.
88

99
ENV LANG en_US.UTF-8
@@ -33,6 +33,7 @@ RUN apt-get update \
3333
binutils-powerpc64-linux-gnu \
3434
binutils-sparc64-linux-gnu \
3535
binutils-riscv64-linux-gnu \
36+
binutils-loongarch64-linux-gnu \
3637
tmux \
3738
patchelf \
3839
&& locale-gen en_US.UTF-8 \

pwnlib/abi.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class ABI(object):
2727
#: Indicates that this ABI returns to the next address on the slot
2828
returns = True
2929

30-
def __init__(self, stack, regs, align, minimum):
30+
def __init__(self, stack, arg_regs, align, minimum):
3131
self.stack = stack
32-
self.register_arguments = regs
32+
self.register_arguments = arg_regs
3333
self.arg_alignment = align
3434
self.stack_minimum = minimum
3535

@@ -50,6 +50,7 @@ def default():
5050
(64, 'powerpc', 'linux'): linux_ppc64,
5151
(32, 'riscv32', 'linux'): linux_riscv32,
5252
(64, 'riscv64', 'linux'): linux_riscv64,
53+
(64, 'loongarch64', 'linux'): linux_loongarch64,
5354
(32, 'i386', 'freebsd'): freebsd_i386,
5455
(64, 'aarch64', 'freebsd'): freebsd_aarch64,
5556
(64, 'amd64', 'freebsd'): freebsd_amd64,
@@ -82,6 +83,7 @@ def syscall():
8283
(64, 'powerpc', 'linux'): linux_ppc64_syscall,
8384
(32, 'riscv32', 'linux'): linux_riscv32_syscall,
8485
(64, 'riscv64', 'linux'): linux_riscv64_syscall,
86+
(64, 'loongarch64', 'linux'): linux_loongarch64_syscall,
8587
(32, 'i386', 'freebsd'): freebsd_i386_syscall,
8688
(64, 'amd64', 'freebsd'): freebsd_amd64_syscall,
8789
(64, 'aarch64', 'freebsd'): freebsd_aarch64_syscall,
@@ -109,6 +111,7 @@ def sigreturn():
109111
(64, 'aarch64', 'linux'): linux_aarch64_sigreturn,
110112
(32, 'riscv32', 'linux'): linux_riscv32_sigreturn,
111113
(64, 'riscv64', 'linux'): linux_riscv64_sigreturn,
114+
(64, 'loongarch64', 'linux'): linux_loongarch64_sigreturn,
112115
(32, 'i386', 'freebsd'): freebsd_i386_sigreturn,
113116
(64, 'amd64', 'freebsd'): freebsd_amd64_sigreturn,
114117
(32, 'arm', 'freebsd'): freebsd_arm_sigreturn,
@@ -148,6 +151,7 @@ class SigreturnABI(SyscallABI):
148151
linux_ppc64 = ABI('sp', ['r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10'], 8, 0)
149152
linux_riscv32 = ABI('sp', ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7'], 8, 0)
150153
linux_riscv64 = ABI('sp', ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7'], 8, 0)
154+
linux_loongarch64 = ABI('sp', ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7'], 8, 0)
151155

152156
sysv_i386 = linux_i386
153157
sysv_amd64 = linux_amd64
@@ -158,6 +162,7 @@ class SigreturnABI(SyscallABI):
158162
sysv_ppc64 = linux_ppc64
159163
sysv_riscv32 = linux_riscv32
160164
sysv_riscv64 = linux_riscv64
165+
sysv_loongarch64 = linux_loongarch64
161166

162167
# Docs: https://man7.org/linux/man-pages/man2/syscall.2.html
163168
linux_i386_syscall = SyscallABI('esp', ['eax', 'ebx', 'ecx', 'edx', 'esi', 'edi', 'ebp'], 4, 0)
@@ -169,20 +174,23 @@ class SigreturnABI(SyscallABI):
169174
linux_ppc64_syscall = SyscallABI('sp', ['r0', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8'], 8, 0)
170175
linux_riscv32_syscall = SyscallABI('sp', ['a7', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5'], 4, 0)
171176
linux_riscv64_syscall = SyscallABI('sp', ['a7', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5'], 8, 0)
177+
linux_loongarch64_syscall = SyscallABI('sp', ['a7', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6'], 8, 0)
172178

173179
linux_i386_sigreturn = SigreturnABI('esp', ['eax'], 4, 0)
174180
linux_amd64_sigreturn = SigreturnABI('rsp', ['rax'], 8, 0)
175181
linux_arm_sigreturn = SigreturnABI('sp', ['r7'], 4, 0)
176182
linux_aarch64_sigreturn = SigreturnABI('sp', ['x8'], 16, 0)
177183
linux_riscv32_sigreturn = SigreturnABI('sp', ['a7'], 4, 0)
178184
linux_riscv64_sigreturn = SigreturnABI('sp', ['a7'], 8, 0)
185+
linux_loongarch64_sigreturn = SigreturnABI('sp', ['a7'], 8, 0)
179186

180187
sysv_i386_sigreturn = linux_i386_sigreturn
181188
sysv_amd64_sigreturn = linux_amd64_sigreturn
182189
sysv_arm_sigreturn = linux_arm_sigreturn
183190
sysv_aarch64_sigreturn = linux_aarch64_sigreturn
184191
sysv_riscv32_sigreturn = linux_riscv32_sigreturn
185192
sysv_riscv64_sigreturn = linux_riscv64_sigreturn
193+
sysv_loongarch64_sigreturn = linux_loongarch64_sigreturn
186194

187195
freebsd_i386 = sysv_i386
188196
freebsd_amd64 = sysv_amd64

pwnlib/asm.py

+6
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def which_binutils(util, check_version=False):
190190
'sparc64': ['sparc'],
191191
'riscv32': ['riscv32', 'riscv64', 'riscv'],
192192
'riscv64': ['riscv64', 'riscv32', 'riscv'],
193+
'loongarch64': ['loongarch64', 'loong64'],
193194
}.get(arch, [])
194195

195196
# If one of the candidate architectures matches the native
@@ -275,6 +276,9 @@ def _assembler():
275276
# riscv64-unknown-elf-as supports riscv32 as well as riscv64
276277
'riscv32': [gas, '-march=rv32gc', '-mabi=ilp32'],
277278
'riscv64': [gas, '-march=rv64gc', '-mabi=lp64'],
279+
280+
# loongarch64 supports none of -64, -EB, -EL or -march
281+
'loongarch64' : [gas],
278282
}
279283

280284
assembler = assemblers.get(context.arch, [gas])
@@ -376,6 +380,7 @@ def _bfdname():
376380
'powerpc64' : 'elf64-powerpc',
377381
'riscv32' : 'elf%d-%sriscv' % (context.bits, E),
378382
'riscv64' : 'elf%d-%sriscv' % (context.bits, E),
383+
'loongarch64' : 'elf%d-loongarch' % context.bits,
379384
'vax' : 'elf32-vax',
380385
's390' : 'elf%d-s390' % context.bits,
381386
'sparc' : 'elf32-sparc',
@@ -400,6 +405,7 @@ def _bfdarch():
400405
'thumb': 'arm',
401406
'riscv32': 'riscv',
402407
'riscv64': 'riscv',
408+
'loongarch64': 'loongarch64'
403409
}
404410

405411
if arch in convert:

0 commit comments

Comments
 (0)