1

I'm confused about the location counter, especially when setting it to a new value in the output section.

I write a simple program and a linker script to make my question more clear. The code is as follows:

test.s

section .text
        .globl _start
_start:
        movq $1, %rax
        movq $0, %rbx
        int $0x80

test.lds

SECTIONS
{
        . = 0x10;
        label_1 = .;
        custom_section : {
                . = 0x20;
                label_2 = . ;
                label_3 = ABSOLUTE(.) ;
                *(.text) ;
        }
}

After linking, use nm command to print symbol addresses:

0000000000000010 T label_1
0000000000000030 T label_2
0000000000000030 A label_3
0000000000000030 T _start

I can't understand why lable_2 is 0x30. As the LD documentation says, if . is used inside a section description, it refers to the byte offset from the start of that section, not an absolute address. In the custom_section, . is set to 0x20 which is a relative offset, so I think label_2 should also be 0x20. The value of label_3 is reasonable because it's an absolute address.

Could someone please explain why label_2 is 0x30? Thanks!

haolee
  • 892
  • 9
  • 19
  • custom_section starts at VMA 0x10 because of the `. = 0x10` . `. = 0x20` is 0x20 relative to the start of the section it is in which is 0x10 so it advanced the VMA to 0x30. – Michael Petch Apr 05 '18 at 03:24
  • Are the `.` in `. = 0x10` and `. = 0x20` two different variables? – haolee Apr 05 '18 at 03:30
  • No, they are the same but how the location counter assignments are interpreted is different based on whether it appears in a section or outside one. – Michael Petch Apr 05 '18 at 03:37
  • But, `. = 0x20` and `label_2 = .` are also interpreted differently because `label_2` is `0x30` which is an absolute address. – haolee Apr 05 '18 at 03:41
  • I believe that the statement _If . is used inside a section description however, it refers to the byte offset from the start of that section, not an absolute address._ is within the context of setting the location counter with `.=` – Michael Petch Apr 05 '18 at 03:52
  • @MichaelPetch So, in the statement `a = 0x20`, `a` is relative to the start of `custom_section` while in the statement `label_2 = a`, `a` is relative to the start of SECTIONS statement. It seems this is the only explanation for now. – haolee Apr 05 '18 at 04:00

0 Answers0