Page 1 of 1

Linker script problem: extraneous alignment

Posted: Tue Jul 16, 2013 9:46 am
by voidf0xb3
I’ve just built a cross-compiler, and before I start actually writing code in C, I want to figure out how to link object files the way I need. So, I’ve got test.o, which contains three small sections with random data (.text, .data, and .rodata). Then I’ve got this linker script, linker.ld:

Code: Select all

OUTPUT_FORMAT(elf64-x86-64)

PHDRS {
    headers PT_PHDR PHDRS;
    text PT_LOAD;
    data PT_LOAD;
    rodata PT_LOAD;
}

SECTIONS {
    .text 0x4000000 /* random address here */ : {
        *(.text)
    } :text
    .data ALIGN(4K) : {
        *(.data)
    } :data
    .rodata ALIGN(4K) : {
        *(.rodata)
    } :rodata
}
I run the linker using a slightly modified version of the command provided in the Bare Bones tutorial (without actually understanding most of the options :)):

Code: Select all

x86_64-elf-gcc -T linker.ld -o result -ffreestanding -O2 -nostdlib test.o -lgcc
And it runs fine, and produces a binary. Problem is, it’s huge: test.o is only about 1.3 KB, while result is over 2 MB!

readelf tells me this:

Code: Select all

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000004000000  00200000
       0000000000000001  0000000000000000  AX       0     0     16
  [ 2] .data             PROGBITS         0000000004001000  00201000
       0000000000000200  0000000000000000  WA       0     0     4
  [ 3] .rodata           PROGBITS         0000000004002000  00202000
       0000000000000014  0000000000000000   A       0     0     4
  [ 4] .shstrtab         STRTAB           0000000000000000  00202014
       000000000000002f  0000000000000000           0     0     1
  [ 5] .symtab           SYMTAB           0000000000000000  00202208
       00000000000000a8  0000000000000018           6     7     8
  [ 6] .strtab           STRTAB           0000000000000000  002022b0
       0000000000000016  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
You can see the problem: .text starts at 200000h! I beilieve (am I right here?) it’s because the linker tries to make sure that the offset of the section in the file and the address where the section is to be loaded are congruent modulo the page size. Which is fine; that’s the behavior I want; with one exception: the page size I use is 4 KiB, not 2 MiB.

So, the question is, how do I tell the linker what page size I want (assuming that it is indeed the cause of the problem)?

Re: Linker script problem: extraneous alignment

Posted: Tue Jul 16, 2013 4:25 pm
by Combuster

Re: Linker script problem: extraneous alignment

Posted: Wed Jul 17, 2013 4:19 am
by voidf0xb3
Ah, that feeling when you write a long long post just to realize that the simple solution was in the wiki all along… :)
Thanks!