From d9b388168ec5b1b34a45ed6ed21d08458abeed3a Mon Sep 17 00:00:00 2001 From: Solarboaters <354059055+Solarboaters@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:01:04 +0000 Subject: [PATCH] Normalize position-independent FLM offsets to the code origin --- pyocd/target/pack/flash_algo.py | 20 ++++--- test/unit/test_flm_code_origin.py | 95 +++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 test/unit/test_flm_code_origin.py diff --git a/pyocd/target/pack/flash_algo.py b/pyocd/target/pack/flash_algo.py index 98a31c853..ce35e7232 100644 --- a/pyocd/target/pack/flash_algo.py +++ b/pyocd/target/pack/flash_algo.py @@ -1,6 +1,7 @@ # pyOCD debugger # Copyright (c) 2017-2019,2025-2026 Arm Limited # Copyright (c) 2021-2022 Chris Reed +# Copyright (c) 2026 Solarboaters # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -105,17 +106,19 @@ def __init__(self, data: Union[str, IO[bytes]]) -> None: symbols: Dict[str, int] = {} symbols.update(self._extract_symbols(self.REQUIRED_SYMBOLS, required=True)) symbols.update(self._extract_symbols(self.EXTRA_SYMBOLS)) - self.symbols = symbols ro_rw_zi = self._find_sections(self.SECTIONS_TO_FIND) ro_rw_zi = self._algo_fill_missing_sections(ro_rw_zi) sect_ro, sect_rw, sect_zi = ro_rw_zi - self.ro_start = sect_ro.start + # The generated blob and entry points are relative to the code section, + # even when the position-independent FLM was linked at a nonzero origin. + self.symbols = {name: address - sect_ro.start for name, address in symbols.items()} + self.ro_start = 0 self.ro_size = sect_ro.length - self.rw_start = sect_rw.start + self.rw_start = sect_rw.start - sect_ro.start self.rw_size = sect_rw.length - self.zi_start = sect_zi.start + self.zi_start = sect_zi.start - sect_ro.start self.zi_size = sect_zi.length self.algo_data = self._create_algo_bin(ro_rw_zi) @@ -290,8 +293,6 @@ def _algo_fill_missing_sections(self, ro_rw_zi: RoRwZiType) -> RoRwZiType: s_ro, s_rw, s_zi = ro_rw_zi if s_ro is None: raise FlashAlgoException("RO section is missing") - elif s_ro.start != 0: - raise FlashAlgoException("RO section must start at 0x0, but starts at 0x%x" % s_ro.start) if s_rw is None: # Create a dummy RW section s_rw = MemoryRange(start=(s_ro.start + s_ro.length), length=0) @@ -304,12 +305,15 @@ def _create_algo_bin(self, ro_rw_zi: RoRwZiType) -> bytearray: """Create a binary blob of the flash algo which can execute from ram""" sect_ro, sect_rw, sect_zi = ro_rw_zi assert sect_ro and sect_rw and sect_zi - algo_size = max(sect_ro.end, sect_rw.end, sect_zi.end) + 1 + if sect_rw.start < sect_ro.start or sect_zi.start < sect_ro.start: + raise FlashAlgoException("RW and ZI sections must not precede the RO section") + algo_size = max(sect_ro.end, sect_rw.end, sect_zi.end) + 1 - sect_ro.start algo_data = bytearray(algo_size) for section in (sect_ro, sect_rw): if hasattr(section, "data"): assert len(section.data) == section.length - algo_data[section.start : section.start + section.length] = section.data + offset = section.start - sect_ro.start + algo_data[offset : offset + section.length] = section.data return algo_data diff --git a/test/unit/test_flm_code_origin.py b/test/unit/test_flm_code_origin.py new file mode 100644 index 000000000..1c49fc3b8 --- /dev/null +++ b/test/unit/test_flm_code_origin.py @@ -0,0 +1,95 @@ +# pyOCD debugger +# Copyright (c) 2026 Solarboaters +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Equivalent position-independent FLMs may have different link origins (#1459).""" + +import ast +from io import BytesIO +import os +from pathlib import Path +import struct +import subprocess +import sys + +from elftools.elf.elffile import ELFFile +import pytest + +from pyocd.core.memory_map import RamRegion +from pyocd.target.pack.flash_algo import PackFlashAlgo + + +DATA = Path(__file__).parents[1] / 'data' / 'packs' + + +def move_elf_origin(data: bytes, origin: int) -> BytesIO: + """Move allocated ELF32 sections and their symbols, preserving instruction bytes.""" + elf = ELFFile(BytesIO(data)) + assert elf.elfclass == 32 and elf.little_endian + moved = bytearray(data) + allocated = {i for i, section in enumerate(elf.iter_sections()) if section['sh_flags'] & 2} + for i, section in enumerate(elf.iter_sections()): + if i in allocated: + offset = elf['e_shoff'] + i * elf['e_shentsize'] + 12 + struct.pack_into('