Set Location Counter to a new value in ld linker script

Hao Lee haolee.swjtu at gmail.com
Wed Apr 4 23:25:23 EDT 2018


Hi,

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[1], 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!


[1] https://sourceware.org/binutils/docs-2.30/ld/Location-Counter.html#Location-Counter

Regards,
Hao Lee



More information about the Kernelnewbies mailing list