ST/개발 환경 및 구조

Linker Script(2) - 구조 및 설명

engineer4ever 2025. 3. 17. 00:15

 

MCU
STM32F103VCT(ST)
IDE
Atollic(TrueSTUDIO), STM32CubeMX
목표
Linker Script 구조 분석

 

1. MEMORY

아래는 STM32F103VCT의 Linker Script의 일부분이다.

MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 48K
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 256K
}
 

RAM과 FLASH로 구분되어 있다.

RAM(xrw)은 48Kbyte

FLASH(rx)은 256Kbyte

 

2. SECTION

아래는 STM32F103VCT의 Linker Script의 일부분이다.

SECTIONS
{
  /* The startup code goes first into FLASH */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
  } >FLASH

  /* The program code and other data goes into FLASH */
  .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 goes into FLASH */
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >FLASH

  .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
  .ARM : {
    __exidx_start = .;
    *(.ARM.exidx*)
    __exidx_end = .;
  } >FLASH

  .preinit_array     :
  {
    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array*))
    PROVIDE_HIDDEN (__preinit_array_end = .);
  } >FLASH
  .init_array :
  {
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array*))
    PROVIDE_HIDDEN (__init_array_end = .);
  } >FLASH
  .fini_array :
  {
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >FLASH

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

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .data : 
  {
    . = ALIGN(4);
    _sdata = .;        /* create a global symbol at data start */
    *(.data)           /* .data sections */
    *(.data*)          /* .data* sections */

    . = ALIGN(4);
    _edata = .;        /* define a global symbol at data end */
  } >RAM AT> FLASH

  
  /* Uninitialized data section */
  . = ALIGN(4);
  .bss :
  {
    /* This is used by the startup in order to initialize the .bss secion */
    _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 left */
  ._user_heap_stack :
  {
    . = ALIGN(4);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(4);
  } >RAM

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

  .ARM.attributes 0 : { *(.ARM.attributes) }
}
 

복잡해 보이지만 모두 다 이해할 필요는 없다.

예를 들어 메모리가 부족해 SDRAM을 추가 후, bss나 heap을 SDRAM에 할당 하는 정도만 알면 충분하다.

 

(1) .isr_vector, .text, .rodata

  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
  } >FLASH
 

.isr_vector는 FLASH에 쓰여지며, 0x8000000 부터 할당 된다.

.isr_vector, .text, .rodata(read only) 모두 FLASH에 할당 된다.

 

(2) .data, .bss, .heap, .stack

위 영역은 RAM에 할당 된다. data 영역은 다시 0이 아닌값으로 초기화 되는 .data영역과, 0으로 초기화 되는 .bss 영역으로 나눌 수 있다.

 

컴파일 후에 map 파일을 열어보면 위와 같이 잘 되었는지 확인 할 수 있다. 아래는 map 파일의 일부분이다.

.isr_vector     0x08000000      0x1e4
                0x08000000                . = ALIGN (0x4)
 *(.isr_vector)
 .isr_vector    0x08000000      0x1e4 startup\startup_stm32f103xe.o
                0x08000000                g_pfnVectors
                0x080001e4                . = ALIGN (0x4)

.text           0x080001e8     0x9ae8
                0x080001e8                . = ALIGN (0x4)
 *(.text)
 

실제로 0이외의 값으로 초기화한 변수는 .data 영역으로 할당 되었고

.data           0x20000000     0x2850 load address 0x0800a680
                0x20000000                . = ALIGN (0x4)
                0x20000000                _sdata = .
 *(.data)
 *(.data*)
 .data.uwTickFreq
                0x20000000        0x1 Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.o
                0x20000000                uwTickFreq
 *fill*         0x20000001        0x3 
 .data.uwTickPrio
                0x20000004        0x4 Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.o
                0x20000004                uwTickPrio
 .data.MemPara  0x20000008       0x52 Src\main.o
                0x20000008                MemPara
 *fill*         0x2000005a        0x2 
 .data.adc_read
                0x2000005c       0x10 Src\main.o
                0x2000005c                adc_read
 .data.max_hall_sensor_value
 

0으로 초기화한 변수는 .bss로 할당 되었다.

.bss            0x20002850      0x368 load address 0x0800ced0
                0x20002850                _sbss = .
                0x20002850                __bss_start__ = _sbss
 *(.bss)
 .bss           0x20002850       0x1c c:/program files (x86)/atollic/truestudio for stm32 9.3.0/armtools/bin/../lib/gcc/arm-atollic-eabi/6.3.1/armv7-m/crtbegin.o
 *(.bss*)
 .bss.TM1638_FND
                0x2000286c        0x4 Src\main.o
                0x2000286c                TM1638_FND
 .bss.TM1638_LED
                0x20002870        0x4 Src\main.o
                0x20002870                TM1638_LED
 .bss.adc_flag  0x20002874        0x4 Src\main.o
                0x20002874                adc_flag
 

 

'ST > 개발 환경 및 구조' 카테고리의 다른 글

STM32 CubeMX LL driver  (0) 2025.03.18
STM32 Driver(Library)  (0) 2025.03.18
Linker Script(1) - Segment  (0) 2025.03.17
ST 개발 보드 소개(2) - 32F746GDISCOVERY  (0) 2025.03.17
ST 개발 보드 소개(1) - STM32duino(Blue Pill)  (0) 2025.03.17