How to place code to a specific address using gas?

190 Views Asked by At

I used IDA Pro to disassemble an existing software running on a Motorola 68K CPU. The output of IDA is a disassembly that follows the MRI notation. 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. The "stuffing" sections you'll find between the parts are filled with 0xFF and IDA translated them into constants.

Layout

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). As this increases the overall size, I simply remove the corresponding number of constants 0xFF to compensate.

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. Using the MRI assembler, the following would do:

.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. But m68k-elf-as does not support using the .org directive!

So instead of using the .org directive, I tried to use the .section directive:

.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:

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.

How can I solve my issue?

1

There are 1 best solutions below

0
tofro On

In the linker script, you can assign a start address for each of your named sections. 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


     ...

}