arm-none-eabi-objcopy:output bin/hex file too large

Asked by allen

arm-none-eabi-objcopy:output bin/hex file too large
ENV: Coretex-M0
target: using arm-none-eabi-objcopy to generate plain bin/hex file for ROM data or flash data for ASIC simulation.
 the flash volume is 8Kx32bit

problems: when arm-none-eabi-obj-copy -ihex or -bin the output file is too large.
So the data file canNOT lager than 8K lines. but it generates 118950 lines!

i have tried -R and -j --only-sections=xxx
ld script: i have 2 sections in FLASH , one is .vector_table , the other is .data

Try1: arm-none-eabi-objcopy -S -bin xxx.bin
       result: filesize =5G too large
try2: arm-none-eabi-objcopy -j --only-sections= .vector_table -j --only-sections= .data -bin xxx.bin
        result: filesize =0

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
allen
Solved:
Last query:
Last reply:
Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#1

Hi Allen,

I'd like to be sure I understand your problem correctly. Does arm-none-eabi-objcopy gives you an error or is it that the file needs to be small for some external reason and you couldn't make objcopy remove the unneeded stuff?

I'm also confused by the invocation you used. I couldn't find a mention of -ihex or -bin in the manual and my objcopy doesn't seem to know. Did you mean -I ihex and -I binary? I'm also puzzled at the second try: -j is equivalent to --only-sections so only one should be needed.

Revision history for this message
allen (allenogz) said :
#2

 Does arm-none-eabi-objcopy gives you an error or is it that the file needs to be small for some external reason and you couldn't make objcopy remove the unneeded stuff?
Ans: the file needs to be small .
i want to generate the flash data file(the data stored in the hardware FLASH). As i mentioned, the flash volume is 8Kx32bit. so the data file is less than 8K lines.
but now the data is 5Gb ,about 150000 lines(most of the lines is 00000000).

 Did you mean -I ihex and -I binary?
Ans: i mean -I ihex and -I binary .

the second try: -j is equivalent to --only-sections so only one should be needed
Ans: i am in a hurry , any way i would like to try .

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#3

Hi Allen,

If using only -j does not work, please can you show me the output of arm-none-eabi-readelf -S xxx.bin before and after doing the objcopy.

Thanks in advance. Best regards.

Revision history for this message
allen (allenogz) said :
#4

/* ld file */

/* Memory Spaces Definitions */

ENTRY(Reset_Handler)

MEMORY
{
  FLASH (rx) : ORIGIN = 0x0, LENGTH = 0x8000 /* 32K */
  RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x1000 /* 4K */
}

/* highest address of the user mode stack */
_estack = 0x20001000;

/* Sections Definitions */

SECTIONS
{
    /* for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, which goes to FLASH */
    .isr_vector :
    {
 . = ALIGN(4);
        KEEP(*(.isr_vector)) /* Startup code */
 . = ALIGN(4);
    } >FLASH

    /* the program code is stored in the .text section, which goes to Flash */
    .text :
    {
     . = ALIGN(4);

        *(.text) /* normal code */
        *(.text.*) /* -ffunction-sections code */
        *(.rodata) /* read-only data (constants) */
        *(.rodata*) /* -fdata-sections read only data */
        *(.glue_7) /* TBD - needed ? */
        *(.glue_7t) /* TBD - needed ? */

 KEEP (*(.init))
 KEEP (*(.fini))

     . = ALIGN(4);
        _etext = .;
     /* This is used by the startup in order to initialize the .data section */
        _sidata = _etext;
    } >FLASH

    /* This is the initialized data section
    The program executes knowing that the data is in the RAM
    but the loader puts the initial values in the FLASH (inidata).
    It is one task of the startup to copy the initial values from FLASH to RAM. */
    .data : AT ( _sidata )
    {
     . = ALIGN(4);
        /* This is used by the startup in order to initialize the .data secion */
        _sdata = . ;
        _data = . ;

        *(.data)
        *(.data.*)
        *(.RAMtext)

     . = ALIGN(4);
     /* This is used by the startup in order to initialize the .data secion */
        _edata = . ;
    } >RAM

    /* This is the uninitialized data section */
    .bss :
    {
     . = ALIGN(4);
        /* This is used by the startup in order to initialize the .bss secion */
        _sbss = .;
        _bss = .;

        *(.bss)
        *(.bss.*) /* patched by elias - allows the use of -fdata-sections */
        *(COMMON)

     . = ALIGN(4);
     /* This is used by the startup in order to initialize the .bss secion */
     _ebss = . ;
    } >RAM

    PROVIDE ( end = _ebss );
    PROVIDE ( _end = _ebss );

    __exidx_start = .;
    __exidx_end = .;

    /* after that it's only debugging information. */

    /* remove the debugging information from the standard libraries */
    /DISCARD/ :
    {
     libc.a ( * )
     libm.a ( * )
     libgcc.a ( * )
     }

    /* Stabs debugging sections. */
    .stab 0 : { *(.stab) }
    .stabstr 0 : { *(.stabstr) }
    .stab.excl 0 : { *(.stab.excl) }
    .stab.exclstr 0 : { *(.stab.exclstr) }
    .stab.index 0 : { *(.stab.index) }
    .stab.indexstr 0 : { *(.stab.indexstr) }
    .comment 0 : { *(.comment) }
    /* DWARF debug sections.
       Symbols in the DWARF debugging sections are relative to the beginning
       of the section so we begin them at 0. */
    /* DWARF 1 */
    .debug 0 : { *(.debug) }
    .line 0 : { *(.line) }
    /* GNU DWARF 1 extensions */
    .debug_srcinfo 0 : { *(.debug_srcinfo) }
    .debug_sfnames 0 : { *(.debug_sfnames) }
    /* DWARF 1.1 and DWARF 2 */
    .debug_aranges 0 : { *(.debug_aranges) }
    .debug_pubnames 0 : { *(.debug_pubnames) }
    /* DWARF 2 */
    .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
    .debug_abbrev 0 : { *(.debug_abbrev) }
    .debug_line 0 : { *(.debug_line) }
    .debug_frame 0 : { *(.debug_frame) }
    .debug_str 0 : { *(.debug_str) }
    .debug_loc 0 : { *(.debug_loc) }
    .debug_macinfo 0 : { *(.debug_macinfo) }
    /* SGI/MIPS DWARF 2 extensions */
    .debug_weaknames 0 : { *(.debug_weaknames) }
    .debug_funcnames 0 : { *(.debug_funcnames) }
    .debug_typenames 0 : { *(.debug_typenames) }
    .debug_varnames 0 : { *(.debug_varnames) }
}

Revision history for this message
allen (allenogz) said :
#5

/* startup file */
/**
  ******************************************************************************
  * @file startup_walnut.s
  * @author Allen
  * @version V1.0.0

  * calls main()).
  * After Reset the Cortex-M0 processor is in Thread mode,
  * priority is Privileged, and the Stack is set to Main.
  *
  */

  .syntax unified
  .cpu cortex-m0
  .fpu softvfp
  .thumb

.global g_pfnVectors
.global Default_Handler

/* start address for the initialization values of the .data section.
defined in linker script */
.word _sidata
/* start address for the .data section. defined in linker script */
.word _sdata
/* end address for the .data section. defined in linker script */
.word _edata
/* start address for the .bss section. defined in linker script */
.word _sbss
/* end address for the .bss section. defined in linker script */
.word _ebss

.equ BootRAM, 0xF108F85F
/**
 * @brief This is the code that gets called when the processor first
 * starts execution following a reset event. Only the absolutely
 * necessary set is performed, after which the application
 * supplied main() routine is called.
 * @param None
 * @retval : None
*/

  .section .text.Reset_Handler
  .weak Reset_Handler
  .type Reset_Handler, %function
Reset_Handler:
  ldr r0, =_estack
  mov sp, r0 /* set stack pointer */

/* Copy the data segment initializers from flash to SRAM */
  movs r1, #0
  b LoopCopyDataInit

CopyDataInit:
  ldr r3, =_sidata
  ldr r3, [r3, r1]
  str r3, [r0, r1]
  adds r1, r1, #4

LoopCopyDataInit:
  ldr r0, =_sdata
  ldr r3, =_edata
  adds r2, r0, r1
  cmp r2, r3
  bcc CopyDataInit
  ldr r2, =_sbss
  b LoopFillZerobss
/* Zero fill the bss segment. */
FillZerobss:
  movs r3, #0
  str r3, [r2]
  adds r2, r2, #4

LoopFillZerobss:
  ldr r3, = _ebss
  cmp r2, r3
  bcc FillZerobss

/* Call the clock system intitialization function.*/
    bl SystemInit
/* Call static constructors */
    bl __libc_init_array
/* Call the application's entry point.*/
  bl main

LoopForever:
    b LoopForever

.size Reset_Handler, .-Reset_Handler

/**
 * @brief This is the code that gets called when the processor receives an
 * unexpected interrupt. This simply enters an infinite loop, preserving
 * the system state for examination by a debugger.
 *
 * @param None
 * @retval : None
*/
    .section .text.Default_Handler,"ax",%progbits
Default_Handler:
Infinite_Loop:
  b Infinite_Loop
  .size Default_Handler, .-Default_Handler
/******************************************************************************
*
* The minimal vector table for a Cortex M0. Note that the proper constructs
* must be placed on this to ensure that it ends up at physical address
* 0x0000.0000.
*
******************************************************************************/
   .section .isr_vector,"a",%progbits
  .type g_pfnVectors, %object
  .size g_pfnVectors, .-g_pfnVectors

g_pfnVectors:
  .word _estack
  .word Reset_Handler
  .word NMI_Handler
  .word HardFault_Handler
  .word 0
  .word 0
  .word 0
  .word 0
  .word 0
  .word 0
  .word 0
  .word SVC_Handler
  .word 0
  .word 0
  .word PendSV_Handler
  .word SysTick_Handler
  .word PowerCtrl_IRQHandler
  .word GPIO_GRP0_IRQHandler
  .word GPIO_GRP1_IRQHandler
  .word WDT_0_IRQHandler
  .word WDT_1_IRQHandler
  .word TIMER_0_IRQHandler
  .word TIMER_1_IRQHandler
  .word UART_0_IRQHandler
  .word UART_1_IRQHandler
  .word SPI_0_IRQHandler
  .word SPI_1_IRQHandler
  .word I2C_0_IRQHandler
  .word ADC_CTRL_IRQHandler
  .word CAP_0_IRQHandler
  .word CAP_1_IRQHandler
  .word CAP_2_IRQHandler
  .word PWM_0A_IRQHandler
  .word PWM_0B_IRQHandler
  .word PWM_1A_IRQHandler
  .word PWM_1B_IRQHandler
  .word PWM_2A_IRQHandler
  .word PWM_2B_IRQHandler
  .word PWM_3A_IRQHandler
  .word PWM_3B_IRQHandler
  .word PWM_4A_IRQHandler
  .word PWM_4B_IRQHandler
  .word 0
  .word 0
  .word BootRAM /* @0x108. This is for boot in RAM mode for
                            xxxxx devices. */

/*******************************************************************************
*
* Provide weak aliases for each Exception handler to the Default_Handler.
* As they are weak aliases, any function with the same name will override
* this definition.
*
*******************************************************************************/

  .weak NMI_Handler
  .thumb_set NMI_Handler,Default_Handler

  .weak HardFault_Handler
  .thumb_set HardFault_Handler,Default_Handler

  .weak SVC_Handler
  .thumb_set SVC_Handler,Default_Handler

  .weak PendSV_Handler
  .thumb_set PendSV_Handler,Default_Handler

  .weak SysTick_Handler
  .thumb_set SysTick_Handler,Default_Handler

  .weak PowerCtrl_IRQHandler
  .thumb_set PowerCtrl_IRQHandler,Default_Handler

  .weak GPIO_GRP0_IRQHandler
  .thumb_set GPIO_GRP0_IRQHandler,Default_Handler

  .weak GPIO_GRP1_IRQHandler
  .thumb_set GPIO_GPR1_IRQHandler,Default_Handler

  .weak WDT_0_IRQHandler
  .thumb_set WDT_0_IRQHandler,Default_Handler

  .weak WDT_1_IRQHandler
  .thumb_set WDT_1_IRQHandler,Default_Handler

  .weak TIMER_0_IRQHandler
  .thumb_set TIMER_0_IRQHandler,Default_Handler

  .weak TIMER_1_IRQHandler
  .thumb_set TIMER_1_IRQHandler,Default_Handler

  .weak UART_0_IRQHandler
  .thumb_set UART_0_IRQHandler,Default_Handler

  .weak UART_1_IRQHandler
  .thumb_set UART_1_IRQHandler,Default_Handler

  .weak SPI_0_IRQHandler
  .thumb_set SPI_0_IRQHandler,Default_Handler

  .weak SPI_1_IRQHandler
  .thumb_set SPI_1_IRQHandler,Default_Handler

  .weak I2C_0_IRQHandler
  .thumb_set I2C_0_IRQHandler,Default_Handler

  .weak ADC_CTRL_IRQHandler
  .thumb_set ADC_CTRL_IRQHandler,Default_Handler

  .weak CAP_0_IRQHandler
  .thumb_set CAP_0_IRQHandler,Default_Handler

  .weak CAP_1_IRQHandler
  .thumb_set CAP_1_IRQHandler,Default_Handler

  .weak CAP_2_IRQHandler
  .thumb_set CAP_2_IRQHandler,Default_Handler

  .weak PWM_0A_IRQHandler
  .thumb_set PWM_0A_IRQHandler,Default_Handler

  .weak PWM_0B_IRQHandler
  .thumb_set PWM_0B_IRQHandler,Default_Handler

  .weak PWM_1A_IRQHandler
  .thumb_set PWM_1A_IRQHandler,Default_Handler

  .weak PWM_1B_IRQHandler
  .thumb_set PWM_1B_IRQHandler,Default_Handler

  .weak PWM_2A_IRQHandler
  .thumb_set PWM_2A_IRQHandler,Default_Handler

  .weak PWM_2B_IRQHandler
  .thumb_set PWM_2B_IRQHandler,Default_Handler

  .weak PWM_3A_IRQHandler
  .thumb_set PWM_3A_IRQHandler,Default_Handler

  .weak PWM_3B_IRQHandler
  .thumb_set PWM_3B_IRQHandler,Default_Handler

  .weak PWM_4A_IRQHandler
  .thumb_set PWM_4A_IRQHandler,Default_Handler

  .weak PWM_4B_IRQHandler
  .thumb_set PWM_4B_IRQHandler,Default_Handler

Revision history for this message
allen (allenogz) said :
#6

what is the command to read after OBJCOPY ?

-----------------------------------------------------------------------------------------
 $(READ) -S $(BIN_DIR)/$(PROJ_NAME).elf
 $(OBJCOPY) -j --only-section=.isr_vector -j --only-section=.text -O binary $(BIN_DIR)/$(PROJ_NAME).elf $(BIN_FILE)
 $(READ) -S $(BIN_FILE)

/tools/ARM/ARM_GCC_2014q2/gcc-arm-none-eabi-4_8-2014q2/bin/arm-none-eabi-readelf -S ./armc_compile/bin0/main.elf
There are 16 section headers, starting at offset 0xa644:

Section Headers:
  [Nr] Name Type Addr Off Size ES Flg Lk Inf Al
  [ 0] NULL 00000000 000000 000000 00 0 0 0
  [ 1] .isr_vector PROGBITS 00000000 008000 0000b4 00 A 0 0 1
  [ 2] .text PROGBITS 000000b4 0080b4 000aa0 00 AX 0 0 4
  [ 3] .comment PROGBITS 00000000 008b54 000070 01 MS 0 0 1
  [ 4] .ARM.attributes ARM_ATTRIBUTES 00000000 008bc4 000028 00 0 0 1
  [ 5] .debug_aranges PROGBITS 00000000 008bf0 0000b0 00 0 0 8
  [ 6] .debug_info PROGBITS 00000000 008ca0 0006bb 00 0 0 1
  [ 7] .debug_abbrev PROGBITS 00000000 00935b 0003e8 00 0 0 1
  [ 8] .debug_line PROGBITS 00000000 009743 000477 00 0 0 1
  [ 9] .debug_frame PROGBITS 00000000 009bbc 0001b8 00 0 0 4
  [10] .debug_str PROGBITS 00000000 009d74 0003e6 01 MS 0 0 1
  [11] .debug_loc PROGBITS 00000000 00a15a 000403 00 0 0 1
  [12] .debug_ranges PROGBITS 00000000 00a560 000038 00 0 0 8
  [13] .shstrtab STRTAB 00000000 00a598 0000ac 00 0 0 1
  [14] .symtab SYMTAB 00000000 00a8c4 000aa0 10 15 101 4
  [15] .strtab STRTAB 00000000 00b364 0006cc 00 0 0 1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
/tools/ARM/ARM_GCC_2014q2/gcc-arm-none-eabi-4_8-2014q2/bin/arm-none-eabi-objcopy -j --only-section=.isr_vector -j --only-section=.text -O binary ./armc_compile/bin0/main.elf ./armc_compile/bin0/main.bin

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#7

Hi Allen,

You should use "-j" *or* "--only-section", *not* both. So you would need to do:

$(OBJCOPY) -j .isr_vector -j .text -O binary $(BIN_DIR)/$(PROJ_NAME).elf $(BIN_FILE)

If I use the same command line as you it doesn't work because objcopy think you want to keep only the 2 sections named "--only-section=.text" and "--only-section=.isr_vector". Since these sections don't exist, objcopy will not keep anything. If you use just -j it should work.

Best regards,

Thomas

Revision history for this message
allen (allenogz) said :
#8

Thank you Thomas.
   it works.
   i misused -j and --only-section in reading http://manned.org/arm-none-eabi-objcopy