All of lore.kernel.org
 help / color / mirror / Atom feed
* Set Location Counter to a new value in ld linker script
@ 2018-04-05  3:25 Hao Lee
  2018-04-09  3:58 ` Yubin Ruan
  0 siblings, 1 reply; 2+ messages in thread
From: Hao Lee @ 2018-04-05  3:25 UTC (permalink / raw)
  To: kernelnewbies

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Set Location Counter to a new value in ld linker script
  2018-04-05  3:25 Set Location Counter to a new value in ld linker script Hao Lee
@ 2018-04-09  3:58 ` Yubin Ruan
  0 siblings, 0 replies; 2+ messages in thread
From: Yubin Ruan @ 2018-04-09  3:58 UTC (permalink / raw)
  To: kernelnewbies

On Thu, Apr 05, 2018 at 11:25:23AM +0800, Hao Lee wrote:
> 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 it is relative, and 0x10 + 0x20 = 0x30 .
What is the problem?

Yubin

> 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-04-09  3:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-05  3:25 Set Location Counter to a new value in ld linker script Hao Lee
2018-04-09  3:58 ` Yubin Ruan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.