Skip to content

Commit

Permalink
Add x86 32-bit GNU toolchain (redballoonsecurity#405)
Browse files Browse the repository at this point in the history
* Add GNU 32 bit toolchain

* Update CHANGELOG

* Add test for x86-gnu-32-toolchain

* Fix import error

* Add toolchain to toolchain.conf

* Fix toolchain name

* Remove build of binutils form scratch from ofrak_patch_maker Dockerstub step

* Add CHANGELOG updates

---------

Co-authored-by: Wyatt <[email protected]>
  • Loading branch information
rbs-jacob and whyitfor committed Jan 17, 2024
1 parent 1e78e96 commit 852c18c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 35 deletions.
8 changes: 8 additions & 0 deletions ofrak_patch_maker/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to `ofrak-patch-maker` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/redballoonsecurity/ofrak/tree/master)
### Added
- GNU 32-bit x86 toolchain. ([#405](https://github.com/redballoonsecurity/ofrak/pull/405))

### Removed
- Switch from binutils version of linker, bin_parser, assembler for X86_64 to default Debian versions. ([#405](https://github.com/redballoonsecurity/ofrak/pull/405))

### Fixed
- X86_64 toolchain now installs on Docker image builds for AARCH64 hosts. ([#405](https://github.com/redballoonsecurity/ofrak/pull/405))

## [4.0.2](https://github.com/redballoonsecurity/ofrak/compare/ofrak-patch-maker-v.4.0.1...ofrak-patch-maker-v.4.0.2)
### Fixed
Expand Down
21 changes: 2 additions & 19 deletions ofrak_patch_maker/Dockerstub
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,9 @@ RUN cd /tmp && \
# LINUX GNU + BINUTILS
RUN apt-get -y update && apt-get -y install software-properties-common gcc-10

RUN if [ "$TARGETARCH" = "amd64" ]; then \
cd /tmp && \
apt-get update && apt-get install -y texinfo && \
wget https://archive.ubuntu.com/ubuntu/pool/main/b/binutils/binutils_2.34.orig.tar.xz --show-progress --progress=bar:force:noscroll && \
tar xvf binutils_2.34.orig.tar.xz -C /opt/rbs/toolchain && \
rm -rf binutils_2.34.orig.tar.xz && \
cd /opt/rbs/toolchain/binutils-2.34 && \
./configure CC=/usr/bin/x86_64-linux-gnu-gcc-10 && \
make -j32; \
fi;

#X64-64 toolchain for arm64 Docker images
RUN if [ "$TARGETARCH" = "arm64" ]; then \
cd /tmp && \
apt-get update && apt-get install -y texinfo && \
wget https://archive.ubuntu.com/ubuntu/pool/main/b/binutils/binutils_2.34.orig.tar.xz --show-progress --progress=bar:force:noscroll && \
tar xvf binutils_2.34.orig.tar.xz -C /opt/rbs/toolchain && \
rm -rf binutils_2.34.orig.tar.xz && \
cd /opt/rbs/toolchain/binutils-2.34 && \
./configure CC=/usr/bin/aarch64-linux-gnu-gcc-10 && \
make -j32; \
apt-get update && apt-get install -y gcc-10-x86-64-linux-gnu; \
fi;

#M68k GNU 10 Linux
Expand Down
14 changes: 11 additions & 3 deletions ofrak_patch_maker/ofrak_patch_maker/toolchain.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ BIN_PARSER = /usr/bin/m68k-linux-gnu-objdump
# Expecting the user host is 64-bit GNU/Linux, we're replacing the system compiler
PREPROCESSOR = /usr/bin/x86_64-linux-gnu-gcc-10
COMPILER = /usr/bin/x86_64-linux-gnu-gcc-10
LINKER = /opt/rbs/toolchain/binutils-2.34/ld/ld-new
BIN_PARSER = /opt/rbs/toolchain/binutils-2.34/binutils/objdump
LINKER = /usr/bin/x86_64-linux-gnu-ld
BIN_PARSER = /usr/bin/x86_64-linux-gnu-objdump
LIB = /usr/lib/x86_64-linux-gnu

[GNU_X86_32_LINUX_EABI_10_3_0]
# Expecting the user host is 64-bit GNU/Linux, we're replacing the system compiler
PREPROCESSOR = /usr/bin/x86_64-linux-gnu-gcc-10
COMPILER = /usr/bin/x86_64-linux-gnu-gcc-10
LINKER = /usr/bin/x86_64-linux-gnu-ld
BIN_PARSER = /usr/bin/x86_64-linux-gnu-objdump
LIB = /usr/lib/x86_64-linux-gnu

[GNU_AARCH64_LINUX_10]
Expand All @@ -52,7 +60,7 @@ BIN_PARSER = /usr/bin/powerpc-linux-gnu-objdump

[ASM]
ARM_ASM_PATH = /opt/rbs/toolchain/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-as
X86_64_ASM_PATH = /opt/rbs/toolchain/binutils-2.34/gas/as-new
X86_64_ASM_PATH = /usr/bin/x86_64-linux-gnu-as
M68K_ASM_PATH = /usr/bin/m68k-linux-gnu-as
AARCH64_ASM_PATH = /usr/bin/aarch64-linux-gnu-as
AVR_ASM_PATH = /usr/bin/avr-as
Expand Down
24 changes: 24 additions & 0 deletions ofrak_patch_maker/ofrak_patch_maker/toolchain/gnu_x86.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from ofrak_patch_maker.toolchain.gnu_x64 import GNU_X86_64_LINUX_EABI_10_3_0_Toolchain
from ofrak_type import ArchInfo


class GNU_X86_32_LINUX_EABI_10_3_0_Toolchain(GNU_X86_64_LINUX_EABI_10_3_0_Toolchain):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self._compiler_flags.append("-m32")
self._assembler_flags.append("--32")
self._linker_flags.extend(["-m", "elf_i386"])

@property
def name(self) -> str:
return "GNU_X86_32_LINUX_EABI_10_3_0"

@property
def segment_alignment(self) -> int:
return 4

def _get_assembler_target(self, processor: ArchInfo):
if self._config.assembler_target:
return self._config.assembler_target
return "generic32"
13 changes: 13 additions & 0 deletions ofrak_patch_maker/ofrak_patch_maker_test/test_x86_toolchain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest

from ofrak_type import ArchInfo

from ofrak_patch_maker.toolchain.gnu_x86 import GNU_X86_32_LINUX_EABI_10_3_0_Toolchain
from ofrak_patch_maker.toolchain.gnu_x64 import GNU_X86_64_LINUX_EABI_10_3_0_Toolchain
from ofrak_patch_maker_test import ToolchainUnderTest
from ofrak_patch_maker_test.toolchain_c import run_hello_world_test, run_bounds_check_test
Expand All @@ -27,6 +29,17 @@
),
X86_EXTENSION,
),
ToolchainUnderTest(
GNU_X86_32_LINUX_EABI_10_3_0_Toolchain,
ArchInfo(
InstructionSet.X86,
None,
BitWidth.BIT_32,
Endianness.LITTLE_ENDIAN,
ProcessorType.I386,
),
X86_EXTENSION,
),
]
)
def toolchain_under_test(request) -> ToolchainUnderTest:
Expand Down
19 changes: 6 additions & 13 deletions ofrak_patch_maker/ofrak_patch_maker_test/toolchain_c.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import logging
import os
import tempfile
from ofrak_patch_maker.toolchain.gnu_avr import GNU_AVR_5_Toolchain
from ofrak_patch_maker.toolchain.gnu_x64 import GNU_X86_64_LINUX_EABI_10_3_0_Toolchain
from ofrak_patch_maker_test import ToolchainUnderTest
from ofrak_type.architecture import InstructionSet

from ofrak_patch_maker.model import PatchRegionConfig
from ofrak_patch_maker.patch_maker import PatchMaker
from ofrak_patch_maker.toolchain.gnu_avr import GNU_AVR_5_Toolchain
from ofrak_patch_maker.toolchain.gnu_x64 import GNU_X86_64_LINUX_EABI_10_3_0_Toolchain
from ofrak_patch_maker.toolchain.model import (
ToolchainConfig,
BinFileType,
Segment,
CompilerOptimizationLevel,
)
from ofrak_patch_maker.toolchain.utils import get_file_format
from ofrak_patch_maker_test import ToolchainUnderTest
from ofrak_type.memory_permissions import MemoryPermissions


Expand All @@ -25,15 +25,10 @@ def run_bounds_check_test(toolchain_under_test: ToolchainUnderTest):
source_path = os.path.join(source_dir, "bounds_check.c")
build_dir = tempfile.mkdtemp()

if toolchain_under_test.proc.isa == InstructionSet.AVR:
# avr-gcc does not support relocatable
relocatable = False
else:
relocatable = True
tc_config = ToolchainConfig(
file_format=BinFileType.ELF,
force_inlines=True,
relocatable=relocatable,
relocatable=False,
no_std_lib=True,
no_jump_tables=True,
no_bss_section=True,
Expand Down Expand Up @@ -93,15 +88,13 @@ def run_hello_world_test(toolchain_under_test: ToolchainUnderTest):
build_dir = tempfile.mkdtemp()

if toolchain_under_test.toolchain == GNU_AVR_5_Toolchain:
relocatable = False
base_symbols = {"__mulhi3": 0x1234} # Dummy address to fix missing symbol
else:
relocatable = True
base_symbols = None
tc_config = ToolchainConfig(
file_format=BinFileType.ELF,
force_inlines=True,
relocatable=relocatable,
relocatable=False,
no_std_lib=True,
no_jump_tables=True,
no_bss_section=False,
Expand Down

0 comments on commit 852c18c

Please sign in to comment.