Skip to main content
newbie_stm32
Associate III
May 6, 2023
Solved

STM32CubeProgrammer is writing to the wrong memory address for the given ELF file

  • May 6, 2023
  • 6 replies
  • 7360 views

Hi, I have an ELF file modified to write to the main memory address 0x08004000 instead of 0x08000000. Please find the screenshot below and the ELF file in the attachment.


_legacyfs_online_stmicro_images_0693W00000bjWLTQA2.pngBut instead of writing to 0x08004000 it constantly writes to 0x08000000. Why is this happening?

This topic has been closed for replies.

6 replies

KnarfB
Super User
May 7, 2023

> Hi, I have an ELF file modified to write to the main memory address 0x08004000 instead of 0x08000000

No, you haven't. Your screenshot shows the old address, as well as

$ arm-none-eabi-readelf -l blink_50ms.elf
 
Elf file type is EXEC (Executable file)
Entry point 0x800435d
There are 3 program headers, starting at offset 52
 
Program Headers:
 Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
 LOAD 0x000000 0x08000000 0x08000000 0x04e7c 0x04e7c RWE 0x10000
 LOAD 0x010000 0x20000000 0x08004e7c 0x0000c 0x0002c RW 0x10000
 LOAD 0x00002c 0x2000002c 0x08004e88 0x00000 0x00604 RW 0x10000
 
 Section to Segment mapping:
 Segment Sections...
 00 .isr_vector .text .rodata .init_array .fini_array
 01 .data .bss
 02 ._user_heap_stack

hth

KnarfB

Tesla DeLorean
Guru
May 7, 2023

What created the .ELF file? Show the linker script (.LD) or relatedGUI settings

The program header is unhelpful

========================================================================
 
** Program header #0 (PT_LOAD) [PF_X + PF_W + PF_R]
 Size : 20092 bytes
 Virtual address: 0x08000000 (Alignment 65536)
 
 
====================================
 
** Program header #1 (PT_LOAD) [PF_W + PF_R]
 Size : 44 bytes (12 bytes in file)
 Virtual address: 0x20000000 (Alignment 65536)
 
 
====================================
 
** Program header #2 (PT_LOAD) [PF_W + PF_R]
 Size : 1540 bytes (0 bytes in file)
 Virtual address: 0x2000002c (Alignment 65536)
 
 
========================================================================
 
** Section #1 '.isr_vector' (***_PROGBITS) [SHF_ALLOC]
 Size : 472 bytes
 Address: 0x08004000
 
 0x8004000: 00 c0 01 20 5d 43 00 08 2d 43 00 08 2f 43 00 08 ... ]C..-C../C..
 0x8004010: 31 43 00 08 33 43 00 08 35 43 00 08 00 00 00 00 1C..3C..5C......
 0x8004020: 00 00 00 00 00 00 00 00 00 00 00 00 37 43 00 08 ............7C..
..

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
newbie_stm32
Associate III
May 7, 2023

Hi, @KnarfB​ @Community member​ .ELF file is created using STM32CubeIDE please find the linker file in the attachment.

File Name: STM32G491RETX_FLASH.ld

/*
******************************************************************************
**
** @file : LinkerScript.ld
**
** @author : Auto-generated by STM32CubeIDE
**
** @brief : Linker script for STM32G491RETx Device from STM32G4 series
** 512Kbytes FLASH
** 112Kbytes RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used
**
** Target : STMicroelectronics STM32
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
******************************************************************************
** @attention
**
** Copyright (c) 2023 STMicroelectronics.
** All rights reserved.
**
** This software is licensed under terms that can be found in the LICENSE file
** in the root directory of this software component.
** If no LICENSE file comes with this software, it is provided AS-IS.
**
******************************************************************************
*/
 
/* Entry Point */
ENTRY(Reset_Handler)
 
/* Highest address of the user mode stack */
_estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
 
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */
 
/* Memories definition */
MEMORY
{
 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 112K
 FLASH (rx) : ORIGIN = 0x8004000, LENGTH = 496K
}
 
/* Sections */
SECTIONS
{
 /* The startup code into "FLASH" Rom type memory */
 .isr_vector :
 {
 . = ALIGN(4);
 KEEP(*(.isr_vector)) /* Startup code */
 . = ALIGN(4);
 } >FLASH
 
 /* The program code and other data into "FLASH" Rom type memory */
 .text :
 {
 . = ALIGN(4);
 *(.text) /* .text sections (code) */
 *(.text*) /* .text* sections (code) */
 *(.glue_7) /* glue arm to thumb code */
 *(.glue_7t) /* glue thumb to arm code */
 *(.eh_frame)
 
 KEEP (*(.init))
 KEEP (*(.fini))
 
 . = ALIGN(4);
 _etext = .; /* define a global symbols at end of code */
 } >FLASH
 
 /* Constant data into "FLASH" Rom type memory */
 .rodata :
 {
 . = ALIGN(4);
 *(.rodata) /* .rodata sections (constants, strings, etc.) */
 *(.rodata*) /* .rodata* sections (constants, strings, etc.) */
 . = ALIGN(4);
 } >FLASH
 
 .ARM.extab : {
 . = ALIGN(4);
 *(.ARM.extab* .gnu.linkonce.armextab.*)
 . = ALIGN(4);
 } >FLASH
 
 .ARM : {
 . = ALIGN(4);
 __exidx_start = .;
 *(.ARM.exidx*)
 __exidx_end = .;
 . = ALIGN(4);
 } >FLASH
 
 .preinit_array :
 {
 . = ALIGN(4);
 PROVIDE_HIDDEN (__preinit_array_start = .);
 KEEP (*(.preinit_array*))
 PROVIDE_HIDDEN (__preinit_array_end = .);
 . = ALIGN(4);
 } >FLASH
 
 .init_array :
 {
 . = ALIGN(4);
 PROVIDE_HIDDEN (__init_array_start = .);
 KEEP (*(SORT(.init_array.*)))
 KEEP (*(.init_array*))
 PROVIDE_HIDDEN (__init_array_end = .);
 . = ALIGN(4);
 } >FLASH
 
 .fini_array :
 {
 . = ALIGN(4);
 PROVIDE_HIDDEN (__fini_array_start = .);
 KEEP (*(SORT(.fini_array.*)))
 KEEP (*(.fini_array*))
 PROVIDE_HIDDEN (__fini_array_end = .);
 . = ALIGN(4);
 } >FLASH
 
 /* Used by the startup to initialize data */
 _sidata = LOADADDR(.data);
 
 /* Initialized data sections into "RAM" Ram type memory */
 .data :
 {
 . = ALIGN(4);
 _sdata = .; /* create a global symbol at data start */
 *(.data) /* .data sections */
 *(.data*) /* .data* sections */
 *(.RamFunc) /* .RamFunc sections */
 *(.RamFunc*) /* .RamFunc* sections */
 
 . = ALIGN(4);
 _edata = .; /* define a global symbol at data end */
 
 } >RAM AT> FLASH
 
 /* Uninitialized data section into "RAM" Ram type memory */
 . = ALIGN(4);
 .bss :
 {
 /* This is used by the startup in order to initialize the .bss section */
 _sbss = .; /* define a global symbol at bss start */
 __bss_start__ = _sbss;
 *(.bss)
 *(.bss*)
 *(COMMON)
 
 . = ALIGN(4);
 _ebss = .; /* define a global symbol at bss end */
 __bss_end__ = _ebss;
 } >RAM
 
 /* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */
 ._user_heap_stack :
 {
 . = ALIGN(8);
 PROVIDE ( end = . );
 PROVIDE ( _end = . );
 . = . + _Min_Heap_Size;
 . = . + _Min_Stack_Size;
 . = ALIGN(8);
 } >RAM
 
 /* Remove information from the compiler libraries */
 /DISCARD/ :
 {
 libc.a ( * )
 libm.a ( * )
 libgcc.a ( * )
 }
 
 .ARM.attributes 0 : { *(.ARM.attributes) }
}

Tesla DeLorean
Guru
May 7, 2023

Suspect it's not using that, or something is broken in the chain of custody.

I'd rename the ,isr_vector section name, and confirm it propagates.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Bob S
Super User
May 8, 2023

>  I don't have enough knowledge on these

Then it is time to learn. @Community member​ gave you something to try. Surely you can edit the linker file? Then search for how to use OBJCOPY, or maybe easier, look at the MAP file and see if that new name appears or if the old "isr_vector" name appears. If your build doesn't generate a map file, the update your CubeIDE project (project properties, C/C++Build, Settings, Tool Settings, Linker, general).

newbie_stm32
Associate III
May 9, 2023

Hi @bob S​ thanks for the answer. I tried editing as below

/* Sections */
SECTIONS
{
 /* The startup code into "FLASH" Rom type memory */
 .isr_vector_new :
 {
 . = ALIGN(4);
 KEEP(*(.isr_vector)) /* Startup code */
 . = ALIGN(4);
 } >FLASH

And found it updated in the ELF file as well as the MAP file below

ELF file: readelf -S blink_50ms.elf


_legacyfs_online_stmicro_images_0693W00000bjeR0QAI.png 

MAP file: blink_50ms.map

A quick search shows it updated in the MAP file as well


_legacyfs_online_stmicro_images_0693W00000bjeRAQAY.png 

Please guide me further.

Bob S
Super User
May 9, 2023

OK - so what happens when you try to program that file with CubeProgrammer?

Bob S
Super User
May 9, 2023

How are you loading the ELF? It looks like it was loaded as a raw binary file (see the "ELF" text at 0x08000000). When I load ELF files I get messages in the log pane like:

14:10:01 Number of segments: 2
14:10:01 segment(0): address= 0x8000000, size=0x2FF00
14:10:01 segment(1): address 0x2000014c, size=0x0

BTW - I don't usually use ELF files for programming. I use hex or binary. Mostly because of many decades of habit with CPUs/compilers that don't support ELF.

newbie_stm32
Associate III
May 10, 2023

Hi @bob S​, there is a tab named Open file in the IDE by which I had opened it.


_legacyfs_online_stmicro_images_0693W00000bjkxgQAA.png

yo3hcv
Associate II
May 9, 2023
Tesla DeLorean
Guru
May 9, 2023

Wow, that seems borderline ret***ed on the the part of the LINKER to make assumptions when addresses are actually furnished, and encountered before the sections directing the data..

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
yo3hcv
Associate II
May 9, 2023

This is my post there, I spent a lot of time to fix it.

I suspect (gnu) linker has some problems too (not joining segments correctly especially if you put more flash data after .data section) but this is other topic. However, setting PHDR will solve the initial problem, STCube Programmer will correct load & program from ELF file as expected.

newbie_stm32
Associate III
May 10, 2023

Hi @yo3hcv​, thanks for pointing it out. I will try this fix.

newbie_stm32
Associate III
May 10, 2023

This fix solved my issue. Now I can see the updated address in the program header


_legacyfs_online_stmicro_images_0693W00000bjmlDQAQ.png@Community member​ @bob S​ can we point this to STMicroelectronics such that they can fix them in the future release?