简体   繁体   English

如何使用气体将代码放置到特定地址?

[英]How to place code to a specific address using gas?

I used IDA Pro to disassemble an existing software running on a Motorola 68K CPU.我使用 IDA Pro 反汇编了一个运行在摩托罗拉 68K CPU 上的现有软件。 The output of IDA is a disassembly that follows the MRI notation. IDA的output是一个遵循MRI符号的反汇编。 I analyzed it and found that it consists of five separate parts:我对其进行了分析,发现它由五个独立的部分组成:

  • Default Vectors默认向量
  • Bootloader SW引导程序软件
  • Application SW应用软件
  • Constants常量
  • Test Routines测试例程

The memory layout of the disassembly is roughly as shown below. memory的拆解布局大致如下图。 The "stuffing" sections you'll find between the parts are filled with 0xFF and IDA translated them into constants.您将在各部分之间找到的“填充”部分用 0xFF 填充,IDA 将它们转换为常量。

布局

Now, my task is to add custom code.现在,我的任务是添加自定义代码。 As I need to put it somewhere, I decided to use the "stuffing" section that follows the Application SW (0x31600 onwards).因为我需要把它放在某个地方,所以我决定使用 Application SW(从 0x31600 开始)之后的“填充”部分。 As this increases the overall size, I simply remove the corresponding number of constants 0xFF to compensate.由于这增加了整体大小,我只是删除了相应数量的常量 0xFF 以进行补偿。

This works nicely for the first time, but soon gets annoying: Each time I adapt my custom code, I need to keep track of the number of constants accordingly.这第一次运行良好,但很快就会变得烦人:每次我调整自定义代码时,我都需要相应地跟踪常量的数量。

To find a convenient solution, my idea was to get rid the "stuffing" sections.为了找到一个方便的解决方案,我的想法是去掉“填充”部分。 That is, I would explicitly assign each of my five plus one parts (Bootloader SW, Application SW, ...) manually to its designated address.也就是说,我会明确地将我的五个加一个部分(Bootloader SW、Application SW,...)中的每一个手动分配到其指定地址。 Using the MRI assembler, the following would do:使用 MRI 汇编程序,可以执行以下操作:

.org 0x00000
<Default Vectors>

.org 0x00100
<Bootloader SW>

.org 0x10000
<Application SW>

.org 0x31600
<My new custom code>

.org 0x60000
<Constants>

.org 0x69300
<Test Routines>

Unfortunately, I do not use the MRI assembler, but the GNU m68k-elf-as for some reason.不幸的是,出于某种原因,我不使用 MRI 汇编器,而是使用 GNU m68k-elf-as。 But m68k-elf-as does not support using the .org directive!但是m68k-elf-as不支持使用.org指令!

So instead of using the .org directive, I tried to use the .section directive:因此,我没有使用.org指令,而是尝试使用.section指令:

.section MyVectors, "r"
<Default Vectors>

.section MyBootloader, "x"
<Bootloader SW>

...

Then I tried to make the linker aware of these sections in the linker script:然后我试图让 linker 知道 linker 脚本中的这些部分:

MEMORY
{
    ROM(rx) : ORIGIN = 0x00000, LENGTH = 512K
}

SECTIONS
{
    .MyVectors :
    {
        KEEP(*(.MyVectors))
    } > ROM = 0xFF

    .MyBootloader :
    
    ...
    
}

Unfortunately, the above does not work properly.不幸的是,以上不能正常工作。 Despite my effort, the linker puts all of my sections next to each other without any "stuffing" in between.尽管我很努力,但 linker 将我的所有部分并排放置,中间没有任何“填充”。

How can I solve my issue?我该如何解决我的问题?

In the linker script, you can assign a start address for each of your named sections.在 linker 脚本中,您可以为每个命名部分分配一个起始地址。 Use the ". =" syntax to do that, effectively setting the location counter manually:使用“.=”语法来做到这一点,有效地手动设置位置计数器:

   MEMORY
   {
     ROM(rx) : ORIGIN = 0x00000, LENGTH = 512K
   }

   SECTIONS
   {
     . = 0
     .MyVectors :
     {
       KEEP(*(.MyVectors))
     } > ROM = 0xFF

     . = 0x100
     .MyBootloader :

     . = 0x10000


     ...

} }

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

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