2

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?

Earlz
  • 62,085
  • 98
  • 303
  • 499

1 Answers1

2

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.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • This is being produced for a custom OS like environment. I'm making sure to not use any of the relocation features. The ELF format has fields available to tell where to load a section, but I'm not seeing any way to specify though in yasm. It can be done in ld with a linker script, but I was hoping to avoid needing to use ld – Earlz Nov 26 '17 at 22:55
  • @Earlz After linking, no relocation is needed as the linker resolves all relocations. I don't think there is any assembler at all that can directly generate ELF executables without using a linker inbetween. That's pretty useless most of the time as ELF is meant to be used with a linker. – fuz Nov 26 '17 at 23:07
  • @Earlz Why were you hoping to avoid having to use a linker? If you can explain this, maybe I can better understand the restrictions you have. – fuz Nov 26 '17 at 23:08
  • 2
    @Earlz The `sh_addr` section header field exists only as a form of debugging information. It's not used in object files, and in ELF executables the section headers are completely ignored and need not be present. Instead the OS uses the program headers to determine where things get loaded into memory. – Ross Ridge Nov 26 '17 at 23:14