Ethansong
Ethansong

Reputation: 53

gcc linker giant bin file

I was using and old verison of ARM official develop environment RVDS, and currently i'd like to switch to linux gcc tool chain.

So i'd like to write a simple hello world to run on our own board, print from UART. but the linker and objcopy stuck me.

here is my LD file:

MEMORY
{
  rom (rx) : ORIGIN = 0, LENGTH = 128K
  ram (rwx) : ORIGIN = 0x60000000, LENGTH = 128K
}

SECTIONS
{
    . = 0x0;
    .text : {
        *(vectors)
        *(.text)
        *(.rodata)
    }
    . = 0x60000000;

    .data : {
        *(.data)
    }
    .bss : {
        *(.bss)
    }
}

after make, it will generate a giant bin file about 1.6G, which is just the size of 0x60000000.

how can I fix this problem? thanks a lot.

Upvotes: 0

Views: 257

Answers (2)

Ethansong
Ethansong

Reputation: 53

the giant file is gone when i changed to these compiler settings:

CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
OBJCP = arm-none-eabi-objcopy

CFLAGS += -mcpu=arm1176jz-s -mthumb -mlittle-endian
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -fshort-enums -fomit-frame-pointer -fno-strict-aliasing
CFLAGS += -Wall -std=c99

LFLAGS += -nostartfiles
LFLAGS += -T my_ld.ld

CPFLAGS = -O binary

Upvotes: 1

harper
harper

Reputation: 13690

The data section should be located in the RAM. Change your linker script:

/*  . = 0x60000000; removed */

.data : {
    *(.data)
} >RAM

.bss : {
    *(.bss)
} >RAM

Your startup code should copy the data from the ROM to the RAM. For that purpose it needs the addresses of the initialization values and where your data should be present at runtime.

Change your script again:

/* used by the startup to initialize data */
_sidata = .;

.data : AT( _sidata )
{
    _sdata = .;
    *(.data)
    _edata = .;
} >RAM

.bss : {
    _sbss = .;
    *(.bss)
    _ebss = .;
} >RAM

Add code

/* 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

Add similar code for the .bss section.

  ldr  r2, =_sbss
  b  LoopFillZerobss
/* Zero fill the bss segment. */
FillZerobss:
  movs  r3, #0
  str  r3, [r2], #4

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

Upvotes: 2

Related Questions