Specify physical address for an ELF32 section in yasm?

Viewed 118

I'm trying to use yasm to build a simple ELF program. However, I can't figure out how to get it to target the .TEXT section so that it's VMA address begins at 0x1000, rather than 0. I've tried using START and ORG directives, but these only appear to be valid when targeting bin, not elf. Is this possible to do in yasm?

1 Answers

This is not possible in ELF objects in general. ELF objects are relocatable. That means, their loading address is not yet fixed. Typically, you use a separate program called the linker to resolve relocations and fix a loading address. You can configure what loading address the linker uses and in what order it places the sections using a linker script. For example, this simple linker script for the GNU linker links code to be suitable for COM files:

SECTIONS {
    . = 0x0100;

    .text : {
        *(.text.startup);
        *(.text);
    }

    .data : {
        *(.data);
        *(.rodata);
    }

    .bss : {
        *(.bss);
    }
}

The . = 0x0100 line at the beginning tells the linker to place the following sections at address 0x100. You could use a similar script to place your code at address 0x1000 instead. Read the manual for further details.

Related