如何专门排除将libgcc函数放入闪存中?

huangapple go评论63阅读模式
英文:

How can I specifically exclude a libgcc function from being placed in flash memory?

问题

我想将libgcc的某个特定部分放在我的STM32L053R8T6微控制器的RAM中,而不是Flash中。即使在链接器脚本中明确排除了特定对象文件中的特定函数,它仍然会出现在我的.map文件中。具体来说,我想从我的微控制器的内存映射的Flash部分中排除函数__gnu_thumb1_case_sqi

要查看正在链接的确切函数的详细信息,我使用了以下命令对libgcc.a归档进行了obj dump:

arm-none-eabi-objdump -S /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a | tee dump.txt

对于相关函数,它给出了以下输出:

_thumb1_case_uqi.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <__gnu_thumb1_case_uqi>:
   0:	b402      	push	{r1}
   2:	4671      	mov	r1, lr
   4:	0849      	lsrs	r1, r1, #1
   6:	0049      	lsls	r1, r1, #1
   8:	5c09      	ldrb	r1, [r1, r0]
   a:	0049      	lsls	r1, r1, #1
   c:	448e      	add	lr, r1
   e:	bc02      	pop	{r1}
  10:	4770      	bx	lr
  12:	46c0      	nop			@ (mov r8, r8)

为了确保链接器脚本排除我的库的这个确切部分,我在输出文件的SECTIONS定义中使用EXCLUDE_FILE命令:

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

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

    *(EXCLUDE_FILE (libgcc.a:_thumb1_case_uqi.o) .text)           /* .text sections (code) */

    /* Exclude file(s) from libgcc.a from .text.* section   */
    *(EXCLUDE_FILE (libgcc.a:_thumb1_case_uqi.o) .text*)

    /* *(.text*) */          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)

然而,当我检查链接器生成的.map文件时,它仍然将这个函数放在错误的区域。以下是相关的输出:

                0x20002000                        _estack = (ORIGIN (RAM) + LENGTH (RAM))
                0x00000200                        _Min_Heap_Size = 0x200
                0x00000300                        _Min_Stack_Size = 0x300

.isr_vector     0x08000000       0xc0
                0x08000000                        . = ALIGN (0x4)
 *(.isr_vector)
 .isr_vector    0x08000000       0xc0 CMakeFiles/MYPROJECT.dir/Platform/MYPROJECT/GCC/startup_stm32l053xx.s.obj
                0x08000000                g_pfnVectors
                0x080000c0                        . = ALIGN (0x4)

.text           0x080000c0     0x76b8
                0x080000c0                        . = ALIGN (0x4)
 *(EXCLUDE_FILE(libgcc.a:_thumb1_case_uqi.o) .text)
 .text          0x080000c0       0x98 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/crtbegin.o
 .text          0x08000158       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_sqi.o)
                0x08000158                __gnu_thumb1_case_sqi
 .text          0x0800016c       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_uqi.o)
                0x0800016c                __gnu_thumb1_case_uqi

为什么它会将这个函数放在.text区域,而在链接器文件中,我明确地制定了不要放置来自相关对象文件的任何内容?

英文:

I want to place a certain section of libgcc in RAM instead of in Flash of my STM32L053R8T6 microcontroller. Even while explicitly excluding a specific function in the specific object file in my linker script, it keeps reappearing in my .map file. Specifically, I want to exclude the function __gnu_thumb1_case_sqi from the flash section of the memory map of my microcontroller.

如何专门排除将libgcc函数放入闪存中?

To examine the details of the exact function being linked, I did an obj dump of the libgcc.a archive using this command

arm-none-eabi-objdump -S /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a | tee dump.txt

which gives the following output for the relevant function:

_thumb1_case_uqi.o:     file format elf32-littlearm


Disassembly of section .text:

00000000 &lt;__gnu_thumb1_case_uqi&gt;:
   0:	b402      	push	{r1}
   2:	4671      	mov	r1, lr
   4:	0849      	lsrs	r1, r1, #1
   6:	0049      	lsls	r1, r1, #1
   8:	5c09      	ldrb	r1, [r1, r0]
   a:	0049      	lsls	r1, r1, #1
   c:	448e      	add	lr, r1
   e:	bc02      	pop	{r1}
  10:	4770      	bx	lr
  12:	46c0      	nop			@ (mov r8, r8)

To make sure I make the linker script exclude this exact portion of my library, I use the EXCLUDE_FILE command in the SECTIONS definition of my output file:

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

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

  /* The program code and other data into &quot;FLASH&quot; Rom type memory */
  .text :
  {
    . = ALIGN(4);

    *(EXCLUDE_FILE (libgcc.a:_thumb1_case_uqi.o) .text)           /* .text sections (code) */

    /* Exclude file(s) from libgcc.a from .text.* section   */
    *(EXCLUDE_FILE (libgcc.a:_thumb1_case_uqi.o) .text*)

    /* *(.text*) */          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)

However, when I examine the .map file generated by the linker, it still places this function in the wrong region. Here is the relevant output:

                0x20002000                        _estack = (ORIGIN (RAM) + LENGTH (RAM))
                0x00000200                        _Min_Heap_Size = 0x200
                0x00000300                        _Min_Stack_Size = 0x300

.isr_vector     0x08000000       0xc0
                0x08000000                        . = ALIGN (0x4)
 *(.isr_vector)
 .isr_vector    0x08000000       0xc0 CMakeFiles/MYPROJECT.dir/Platform/MYPROJECT/GCC/startup_stm32l053xx.s.obj
                0x08000000                g_pfnVectors
                0x080000c0                        . = ALIGN (0x4)

.text           0x080000c0     0x76b8
                0x080000c0                        . = ALIGN (0x4)
 *(EXCLUDE_FILE(libgcc.a:_thumb1_case_uqi.o) .text)
 .text          0x080000c0       0x98 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/crtbegin.o
 .text          0x08000158       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_sqi.o)
                0x08000158                __gnu_thumb1_case_sqi
 .text          0x0800016c       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_uqi.o)
                0x0800016c                __gnu_thumb1_case_uqi

How come it places this in the .text region when in the linker file I explicitly made it not place anything from the related object file?

答案1

得分: 0

你不能控制特定函数的位置,只能控制部分。 使用 ar 来从相关库中提取目标文件,并使用 objdump 来查找包含你所需函数的部分,然后将该文件中的整个部分分配到RAM。

如果你幸运的话,该库可能是使用 -ffunction-sections 编译的,那么它将是一个小的部分。如果你不幸的话,你将不得不分配该文件中的整个 .text 部分。

英文:

You can't control where specific function are placed, only sections. Use ar to extract the object files from the library in question and use objdump to find which section contains the function you want, then assign the whole section from that file to RAM.

If you are lucky the library was compiled with -ffunction-sections and it will be a small section. If you are unlucky you will have to assign the whole of .text from that file.

答案2

得分: 0

正如artless-noise指出的,问题主要是链接脚本中的格式问题。我忽视的主要点是,当使用EXCLUDE_FILE文件命令时,链接器脚本需要库的完整路径,而不仅仅是库的名称。

我需要使用的行是:

    *(EXCLUDE_FILE(*libgcc.a:_thumb1_case_uqi.o *libgcc.a:_thumb1_case_sqi.o) .text)

注意:我包括了sqiuqi两种开关情况,因为两者都可以编译。

我的.map文件中的RAM部分:

 .RamFunc       0x2000027c       0xcc CMakeFiles/ThincHV_ST573NPFF.dir/Platform/ThincHV/stm32l0xx_it.c.obj
                0x2000027c                SysTick_Handler
                0x20000290                ADC1_COMP_IRQHandler
                0x2000029a                TIM2_IRQHandler
                0x200002ee                TIM21_IRQHandler
                0x20000324                TIM22_IRQHandler
                0x20000326                EXTI4_15_IRQHandler
                0x20000344                TIM6_DAC_IRQHandler
 .RamFunc       0x20000348      0x304 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Application/Heating/TriacQ2Q3.c.obj
                0x2000035c                TriacQ2Q3_PostEvent
 .RamFunc       0x2000064c       0x38 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Drivers/STM32_TouchSensing_Library/src/tsl_time.c.obj
                0x2000064c                TSL_tim_ProcessIT
 .RamFunc       0x20000684       0x68 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Peripherals/CRCManagement.c.obj
                0x20000684                CRCManagement_CalculateCrc
 .RamFunc       0x200006ec       0xd4 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Peripherals/EEPROMManagement.c.obj
 .RamFunc       0x200007c0       0x14 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Peripherals/SystemClocks.c.obj
                0x200007c0                HAL_IncTick
 *libgcc.a:_thumb1_case_uqi.o(.text)
 .text          0x200007d4       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_uqi.o)
                0x200007d4                __gnu_thumb1_case_uqi
 *libgcc.a:_thumb1_case_sqi.o(.text)
 .text          0x200007e8       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_sqi.o)
                0x200007e8                __gnu_thumb1_case_sqi
 *(.RamFunc*)
                0x200007fc                        . = ALIGN (0x4)
                0x200007fc                        _edata = .
英文:

As artless-noise points out, the issue was mainly about the formatting in the linker script. The main point that I overlooked was that the linker script needs the whole path of the library when using the EXCLUDE_FILE file command and not just the name of the library.

The line I needed to use was:

    *(EXCLUDE_FILE(*libgcc.a:_thumb1_case_uqi.o *libgcc.a:_thumb1_case_sqi.o) .text)

Note: I included both sqi and uqi switch cases because both can be compiled.

The RAM section in my .map file:

 .RamFunc       0x2000027c       0xcc CMakeFiles/ThincHV_ST573NPFF.dir/Platform/ThincHV/stm32l0xx_it.c.obj
                0x2000027c                SysTick_Handler
                0x20000290                ADC1_COMP_IRQHandler
                0x2000029a                TIM2_IRQHandler
                0x200002ee                TIM21_IRQHandler
                0x20000324                TIM22_IRQHandler
                0x20000326                EXTI4_15_IRQHandler
                0x20000344                TIM6_DAC_IRQHandler
 .RamFunc       0x20000348      0x304 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Application/Heating/TriacQ2Q3.c.obj
                0x2000035c                TriacQ2Q3_PostEvent
 .RamFunc       0x2000064c       0x38 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Drivers/STM32_TouchSensing_Library/src/tsl_time.c.obj
                0x2000064c                TSL_tim_ProcessIT
 .RamFunc       0x20000684       0x68 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Peripherals/CRCManagement.c.obj
                0x20000684                CRCManagement_CalculateCrc
 .RamFunc       0x200006ec       0xd4 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Peripherals/EEPROMManagement.c.obj
 .RamFunc       0x200007c0       0x14 CMakeFiles/ThincHV_ST573NPFF.dir/Components/Peripherals/SystemClocks.c.obj
                0x200007c0                HAL_IncTick
 *libgcc.a:_thumb1_case_uqi.o(.text)
 .text          0x200007d4       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_uqi.o)
                0x200007d4                __gnu_thumb1_case_uqi
 *libgcc.a:_thumb1_case_sqi.o(.text)
 .text          0x200007e8       0x14 /usr/lib/gcc/arm-none-eabi/12.2.1/thumb/v6-m/nofp/libgcc.a(_thumb1_case_sqi.o)
                0x200007e8                __gnu_thumb1_case_sqi
 *(.RamFunc*)
                0x200007fc                        . = ALIGN (0x4)
                0x200007fc                        _edata = .

huangapple
  • 本文由 发表于 2023年5月25日 06:27:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定