Skip to content

Commit

Permalink
Add big-endian support in the patchmaker. Also add ARM BE8 support (#284
Browse files Browse the repository at this point in the history
)

* Add big-endian support in the patchmaker. Also add ARM BE8 support

* Update changelogs

* Moved big endian flags from abstract to ARM toolchain

* Added tests for ARM big endian and BE8 modes

* Linter

* Added missing import

* Fix import

* Fixed alignement test for big-endian

---------

Co-authored-by: Paul Noalhyt <[email protected]>
  • Loading branch information
paulnoalhyt and Paul Noalhyt authored Dec 4, 2024
1 parent ee2b985 commit 54c2956
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ofrak_patch_maker/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Extend parsed symbol dictionary to include LinkableSymbolType.
- Extend AssembledObject and BOM types to include relocation and unresolved symbols.
- Add separate data sections support to LLVM toolchain, and add general flag for including subsections
- Add support for big endian in the GNU toolchain
- Add support for ARM BE8 in the GNU toolchain

### Changed
- Switch to standard GCC-like frontend for LLVM, which supports C attribute(weak)
Expand Down
1 change: 0 additions & 1 deletion ofrak_patch_maker/ofrak_patch_maker/toolchain/gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def __init__(
# if sections contain more than one function =/
"-fno-merge-constants", # avoids sections like .rodata.cst16, .rodata.str1.1 etc
"-fno-reorder-functions",
"-Wall",
]
)
if self._config.separate_data_sections:
Expand Down
10 changes: 9 additions & 1 deletion ofrak_patch_maker/ofrak_patch_maker/toolchain/gnu_arm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from ofrak_patch_maker.binary_parser.gnu import GNU_ELF_Parser
from ofrak_patch_maker.toolchain.gnu import GNU_10_Toolchain
from ofrak_patch_maker.toolchain.model import ToolchainConfig, ToolchainException
from ofrak_type.architecture import InstructionSet, SubInstructionSet, ArchInfo
from ofrak_type.architecture import InstructionSet, SubInstructionSet, ArchInfo, ProcessorType
from ofrak_type.endianness import Endianness
import logging


Expand All @@ -19,6 +20,13 @@ def __init__(
self._compiler_flags.append("-mfloat-abi=hard")
else:
self._compiler_flags.append("-msoft-float")
if self._processor.processor == ProcessorType.GENERIC_ARM_BE8:
self._compiler_flags.append("-mbe8")
self._linker_flags.append("-be8")
if self._processor.endianness == Endianness.BIG_ENDIAN:
self._compiler_flags.append("-mbig-endian")
self._linker_flags.append("-EB")
self._assembler_flags.append("-mbig-endian")

@property
def name(self) -> str:
Expand Down
32 changes: 31 additions & 1 deletion ofrak_patch_maker/ofrak_patch_maker_test/test_arm_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@
),
ARM_EXTENSION,
),
ToolchainUnderTest(
GNU_ARM_NONE_EABI_10_2_1_Toolchain,
ArchInfo(
InstructionSet.ARM,
SubInstructionSet.ARMv8A,
BitWidth.BIT_32,
Endianness.BIG_ENDIAN,
ProcessorType.GENERIC_A9_V7_THUMB,
),
ARM_EXTENSION,
),
ToolchainUnderTest(
GNU_ARM_NONE_EABI_10_2_1_Toolchain,
ArchInfo(
InstructionSet.ARM,
SubInstructionSet.ARMv8A,
BitWidth.BIT_32,
Endianness.BIG_ENDIAN,
ProcessorType.GENERIC_ARM_BE8,
),
ARM_EXTENSION,
),
ToolchainUnderTest(
LLVM_12_0_1_Toolchain,
ArchInfo(
Expand Down Expand Up @@ -164,4 +186,12 @@ def test_arm_alignment(toolchain_under_test: ToolchainUnderTest):
with open(exec_path, "rb") as f:
dat = f.read()
code_offset = code_segments[0].offset
assert dat[code_offset : code_offset + 2] == b"\x05\xe0"
if (
toolchain_under_test.proc.endianness == Endianness.LITTLE_ENDIAN
or toolchain_under_test.proc.processor == ProcessorType.GENERIC_ARM_BE8
):
# little-endian code instructions
expected_bytes = b"\x05\xe0"
else:
expected_bytes = b"\xe0\x05"
assert dat[code_offset : code_offset + 2] == expected_bytes
1 change: 1 addition & 0 deletions ofrak_type/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
- `ProcessorType.CORTEX_A72`
- LinkableSymbolType enum for generalized representation of symbol types (essentially functions vs. data)
- Added a `GENERIC_ARM_BE8` ProcessorType for ARM BE8

## [2.1.0](https://github.com/redballoonsecurity/ofrak/compare/ofrak-type-v2.0.0...ofrak-type-v2.1.0) - 2023-01-20
### Added
Expand Down
1 change: 1 addition & 0 deletions ofrak_type/ofrak_type/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ProcessorType(Enum):
GENERIC_A9_V6 = "generic_a9_v6"
GENERIC_A9_V7 = "generic_a9_v7"
GENERIC_A9_V7_THUMB = "generic_a9_v7_thumb"
GENERIC_ARM_BE8 = "generic_be8" # ARM with little endian code but big endian data
MSP430 = "msp340"
MIPS_LITTLE = "mips"
MIPS_RM5721_BIG = "mips_rm5721_big"
Expand Down

0 comments on commit 54c2956

Please sign in to comment.