简体   繁体   English

在ARM Cortex M3汇编器中的任何地址进行编程

[英]Program at any address in ARM cortex M3 assembler

Does anyone know how to put a program in an ARM assembler at an arbitrary address in memory? 有谁知道如何将程序放入内存中任意地址的ARM汇编器中? By default, the program is located starting from the address 0x00000008, but it is necessary that it be located, for example, at the address 0x20002000. 默认情况下,该程序位于地址0x00000008开始,但是有必要将其例如定位在地址0x20002000上。 I tried to use DCD 0x20002000 , but this only changes the register-pointer, while the program remains at the old address. 我尝试使用DCD 0x20002000 ,但这只会更改寄存器指针,而程序仍保留在旧地址。 The SPACE 0x400 command works well, but if the address is too large, then the compilation takes a very long time. SPACE 0x400命令运行良好,但是如果地址太大,则编译将花费很长时间。 Tell me, maybe faced with this? 告诉我,也许面对这个?

在此处输入图片说明

There are basically two ways to control the location of the code region using Keil. 基本上,有两种方法可以使用Keil来控制代码区域的位置。 Either using the scatter file, or using the 'target' tab in the build options dialogue. 使用分散文件,或使用构建选项对话框中的“目标”选项卡。

You can define an IROM region which includes the region where you want your code to be located. 您可以定义一个IROM区域,其中包括您要放置代码的区域。 You can also start by using this dialogue to generate an initial scatter file, then modify the scatter file based on the Keil documentation. 您也可以使用此对话框开始生成初始分散文件,然后根据Keil文档修改分散文件。

However, the location you have given here is in the RAM region, which is a little unusual (maybe inefficient, but valid). 但是,您在此处给出的位置在RAM区域中,这有点不寻常(可能效率低下,但有效)。 Have you considered how to initialise your target after power cycling it? 您是否考虑过重启电源后如何初始化目标?

The biggest problem is that at reset, Cortex-M3 will ALWAYS read from addresses 0, 4 (and 8 due to prefetch). 最大的问题是,在复位时,Cortex-M3总是会从地址0、4(由于预取而导致的地址为8)进行读取。 You need to provide an initial stack pointer (or not use the stack), and your reset vector (with bit 0, the T-bit, set). 您需要提供一个初始堆栈指针(或不使用堆栈),以及复位向量(设置为0,即T位)。 There may be hardware banking of the memory map (device specific), but the programmer's view is always that these fetches start at address 0. 内存映射可能存在硬件存储区(特定于设备),但是程序员的观点始终是,这些提取始于地址0。

You can usually achieve this result by modifying the linker script you are using, or by providing certain command-line options to GNU ld. 通常,您可以通过修改所使用的链接描述文件或为GNU ld提供某些命令行选项来获得此结果。

From the information you provided, I am assuming that your code is currently starting right after the two first vector entries table, ie the Initial SP Value, and the Reset Value, and that your vector table is located at 0x00000000. 根据您提供的信息,我假设您的代码当前正好在两个第一个向量条目表(即初始SP值和重置值)之后开始,并且您的向量表位于0x00000000。

example1.s should more or less look like your current code: example1.s应该或多或少看起来像您当前的代码:

        .cpu    cortex-m3
        .thumb    
        .syntax unified
        .global Reset_Handler
        .equ    StackTop, 0x1000
        .word   StackTop         
        .word   Reset_Handler
Reset_Handler:
         b .
        .end


/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-as -o example1.o -c example1.s
/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-ld -g -e Reset_Handler -Ttext-segment=0x00000000 -Map=example1.map -o example1.elf example1.o
/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-objdump -d example1.elf

example1.elf:     file format elf32-littlearm

Disassembly of section .text:

00000000 <Reset_Handler-0x8>:
   0:   00001000        .word   0x00001000
   4:   00000008        .word   0x00000008

00000008 <Reset_Handler>:
   8:   e7fe            b.n     8 <Reset_Handler>

The code will then be located at 0x00000008. 该代码将位于0x00000008。

example2.s defines a new linker section named .vectors. example2.s定义了一个名为.vectors的新链接器节。

We will now use GNU ld --section-start command-line option to force the .vectors section to reside at 0x00000000, and still use -Ttext-segment to force the .text section/ text segment to reside at 0x20002000. 现在,我们将使用GNU ld --section-start命令行选项来强制.vectors节驻留在0x00000000,并且仍然使用-Ttext-segment来强制.text节/文本段驻留在0x20002000。

example2.s: example2.s:

    .cpu    cortex-m3                                                                                                                                                                                                                                              
    .thumb                                                                                                                                                                                                                                                                 
    .syntax unified                                                                                                                                                                                                                                                        
    .global Reset_Handler                                                                                                                                                                                                                                                  
    .equ    StackTop, 0x1000                                                                                                                                                                                                                                               

    .section .vectors                                                                                                                                                                                                                                                      
    .word   StackTop                                                                                                                                                                                                                                                       
    .word   Reset_Handler                                                                                                                                                                                                                                                  

    .section .text                                                                                                                                                                                                                                                         

Reset_Handler: Reset_Handler:
b . b。

/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-as -o example2.o -c example2.s
/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-ld -g -e Reset_Handler --section-start=.vectors=0x00000000 -Ttext-segment=0x20002000 -Map=example2.map -o example2.elf example2.o
/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-ld: warning: address of `text-segment' isn't multiple of maximum page size
/opt/arm/gcc-arm-8.2-2019.01-x86_64-arm-eabi/bin/arm-eabi-objdump -d -j .vectors -j .text -d example2.elf

example2.elf:     file format elf32-littlearm


Disassembly of section .vectors:

00000000 <.vectors>:
   0:   00001000        .word   0x00001000
   4:   20002000        .word   0x20002000

Disassembly of section .text:

20002000 <Reset_Handler>:
20002000:       e7fe            b.n     20002000 <Reset_Handler>
        .end    

The vector table is still located at the correct memory location, but the code now resides at 0x20002000. 向量表仍位于正确的内存位置,但是代码现在位于0x20002000。

In order to describe the exact changes required in the linker script you are using for achieving the same result, I would need to see its content. 为了描述在链接器脚本中实现相同结果所需的确切更改,我需要查看其内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM