Gallopsled / Gallopsled/pwntools

Add ability to specify more parameters to `asm()` function to specify target architecture

Open
#2,539 2 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
Python
Stars
13.7k
Forks
1.9k
Avg merge
6d 23h
Merged PRs (30d)
3

Description

The pwntools `asm` and `make_elf_from_assembly` functions are really amazing - they are great for really quickly testing out small snippets of assembly instructions. It would be great to be able to have more fine grained control with the arguments that are passed to the assembler and linker inside of the `asm` function.

This was spurred by testing a 32-bit RISC-V script that had an assembly instruction that required a RISC-V ISA extension that can be enabled via an assembler flag.

For example, the following RISC-V assembly snippet uses the `bset` instruction specified in the RISC-V `zbs` extension (I tested this with the apt `gcc-14-riscv64-linux-gnu` package installed):

```python
#!/usr/bin/env python3

from pwn import *

context.arch = "riscv32"

RISCV=f"""
li a2, 4
bset a2,zero,a2
"""
out = make_elf_from_assembly(RISCV)

print(out)
print("-"*80)
gdb.debug(out)

pause()
```

These extensions are typically enabled by appending strings to the `-march` flag passed to the assembler. For example, to enable the `zbs` extension, you can change the following line:

https://github.com/Gallopsled/pwntools/blob/3eb690bd38c3311b0e6ce79a45310f80c2b5222e/pwnlib/asm.py#L277

to

```python
'riscv32': [gas, '-march=rv32gc_zbs', '-mabi=ilp32'],
```

(note the `_zbs`)

Perhaps there could be new named parameters such `extensions` that are passed to the assembler, and similarly another parameter to modify/(override?) the binary format.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.