public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Default entry point for ELF shared objects
@ 2021-09-13  7:55 Florian Weimer
  2021-09-15 16:49 ` Fangrui Song
  0 siblings, 1 reply; 10+ messages in thread
From: Florian Weimer @ 2021-09-13  7:55 UTC (permalink / raw)
  To: binutils

BFD ld currently sets a non-zero entry point address for ELF shared
objects even if the object does not have a _start symbol.

Is there a reason for this behavior (particularly for ELF ET_DYN
output)?

On Linux, the kernel will happily load and execute shared objects using
this entry point address, typically leading to crashes.

If the entry point address in the ELF header were zero, it might be
possible to detect the missing entry point, and refuse to execute the
shared object as if it were a program.

Thanks,
Florian


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

* Re: Default entry point for ELF shared objects
  2021-09-13  7:55 Default entry point for ELF shared objects Florian Weimer
@ 2021-09-15 16:49 ` Fangrui Song
  2021-09-16  5:01   ` Florian Weimer
  0 siblings, 1 reply; 10+ messages in thread
From: Fangrui Song @ 2021-09-15 16:49 UTC (permalink / raw)
  To: Florian Weimer; +Cc: binutils

On 2021-09-13, Florian Weimer via Binutils wrote:
>BFD ld currently sets a non-zero entry point address for ELF shared
>objects even if the object does not have a _start symbol.
>
>Is there a reason for this behavior (particularly for ELF ET_DYN
>output)?
>
>On Linux, the kernel will happily load and execute shared objects using
>this entry point address, typically leading to crashes.
>
>If the entry point address in the ELF header were zero, it might be
>possible to detect the missing entry point, and refuse to execute the
>shared object as if it were a program.
>
>Thanks,
>Florian

This behavior is documented at https://sourceware.org/binutils/docs/ld/Entry-Point.html

"the address of the first byte of the ‘.text’ section, if present;"

The rule is quite ad-hoc and I'd support the removal.

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

* Re: Default entry point for ELF shared objects
  2021-09-15 16:49 ` Fangrui Song
@ 2021-09-16  5:01   ` Florian Weimer
  2021-09-16  7:41     ` Andreas Schwab
  2021-09-16 15:14     ` Hans-Peter Nilsson
  0 siblings, 2 replies; 10+ messages in thread
From: Florian Weimer @ 2021-09-16  5:01 UTC (permalink / raw)
  To: Fangrui Song; +Cc: binutils

* Fangrui Song:

> On 2021-09-13, Florian Weimer via Binutils wrote:
>>BFD ld currently sets a non-zero entry point address for ELF shared
>>objects even if the object does not have a _start symbol.
>>
>>Is there a reason for this behavior (particularly for ELF ET_DYN
>>output)?
>>
>>On Linux, the kernel will happily load and execute shared objects using
>>this entry point address, typically leading to crashes.
>>
>>If the entry point address in the ELF header were zero, it might be
>>possible to detect the missing entry point, and refuse to execute the
>>shared object as if it were a program.
>>
>>Thanks,
>>Florian
>
> This behavior is documented at https://sourceware.org/binutils/docs/ld/Entry-Point.html
>
> "the address of the first byte of the ‘.text’ section, if present;"
>
> The rule is quite ad-hoc and I'd support the removal.

I saw that.  I still wonder where it came from originally.  It seems
more like something for a bare-metal target.  Maybe it can be kept for
those, but removed for Linux ELF targets, especially ET_DYN output
objects.

Thanks,
Florian


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

* Re: Default entry point for ELF shared objects
  2021-09-16  5:01   ` Florian Weimer
@ 2021-09-16  7:41     ` Andreas Schwab
  2021-09-16 15:14     ` Hans-Peter Nilsson
  1 sibling, 0 replies; 10+ messages in thread
From: Andreas Schwab @ 2021-09-16  7:41 UTC (permalink / raw)
  To: Florian Weimer via Binutils; +Cc: Fangrui Song, Florian Weimer

On Sep 16 2021, Florian Weimer via Binutils wrote:

> I saw that.  I still wonder where it came from originally.

For a.out, it makes totally sense.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

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

* Re: Default entry point for ELF shared objects
  2021-09-16  5:01   ` Florian Weimer
  2021-09-16  7:41     ` Andreas Schwab
@ 2021-09-16 15:14     ` Hans-Peter Nilsson
  2021-09-16 16:39       ` Paul Koning
  1 sibling, 1 reply; 10+ messages in thread
From: Hans-Peter Nilsson @ 2021-09-16 15:14 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Fangrui Song, binutils

On Thu, 16 Sep 2021, Florian Weimer via Binutils wrote:

> * Fangrui Song:
>
> > On 2021-09-13, Florian Weimer via Binutils wrote:
> >>BFD ld currently sets a non-zero entry point address for ELF shared
> >>objects even if the object does not have a _start symbol.
> >>
> >>Is there a reason for this behavior (particularly for ELF ET_DYN
> >>output)?
> >>
> >>On Linux, the kernel will happily load and execute shared objects using
> >>this entry point address, typically leading to crashes.
> >>
> >>If the entry point address in the ELF header were zero, it might be
> >>possible to detect the missing entry point, and refuse to execute the
> >>shared object as if it were a program.
> >>
> >>Thanks,
> >>Florian
> >
> > This behavior is documented at https://sourceware.org/binutils/docs/ld/Entry-Point.html
> >
> > "the address of the first byte of the ?.text? section, if present;"
> >
> > The rule is quite ad-hoc and I'd support the removal.
>
> I saw that.  I still wonder where it came from originally.  It seems
> more like something for a bare-metal target.  Maybe it can be kept for
> those,

Even if so, IMHO it should be updated to be "the text segment"
or words to that effect, as the .text *section* is likely just
main code located after .startup or .init or similar.

> but removed for Linux ELF targets, especially ET_DYN output
> objects.
>
> Thanks,
> Florian
>

brgds, H-P

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

* Re: Default entry point for ELF shared objects
  2021-09-16 15:14     ` Hans-Peter Nilsson
@ 2021-09-16 16:39       ` Paul Koning
  2021-09-16 16:54         ` Hans-Peter Nilsson
  0 siblings, 1 reply; 10+ messages in thread
From: Paul Koning @ 2021-09-16 16:39 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: Florian Weimer, binutils



> On Sep 16, 2021, at 11:14 AM, Hans-Peter Nilsson <hp@bitrange.com> wrote:
> 
> On Thu, 16 Sep 2021, Florian Weimer via Binutils wrote:
> 
>> * Fangrui Song:
>> 
>>> ...
>>> This behavior is documented at https://sourceware.org/binutils/docs/ld/Entry-Point.html
>>> 
>>> "the address of the first byte of the ?.text? section, if present;"
>>> 
>>> The rule is quite ad-hoc and I'd support the removal.
>> 
>> I saw that.  I still wonder where it came from originally.  It seems
>> more like something for a bare-metal target.  Maybe it can be kept for
>> those,
> 
> Even if so, IMHO it should be updated to be "the text segment"
> or words to that effect, as the .text *section* is likely just
> main code located after .startup or .init or similar.

A default entry point really doesn't make any sense for any target, bare metal or otherwise.  Yes, it's possible such a random guess is the right answer some of the time, but clearly any build can always supply an explicit start address -- and in my view that should be required.

As an example, the DEC object format has a special code point meaning "no start address", and that is the default.

	paul


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

* Re: Default entry point for ELF shared objects
  2021-09-16 16:39       ` Paul Koning
@ 2021-09-16 16:54         ` Hans-Peter Nilsson
  2021-09-16 17:01           ` Florian Weimer
  2021-09-16 17:02           ` Paul Koning
  0 siblings, 2 replies; 10+ messages in thread
From: Hans-Peter Nilsson @ 2021-09-16 16:54 UTC (permalink / raw)
  To: Paul Koning; +Cc: Florian Weimer, binutils



On Thu, 16 Sep 2021, Paul Koning wrote:

>
>
> > On Sep 16, 2021, at 11:14 AM, Hans-Peter Nilsson <hp@bitrange.com> wrote:
> >
> > On Thu, 16 Sep 2021, Florian Weimer via Binutils wrote:
> >
> >> * Fangrui Song:
> >>
> >>> ...
> >>> This behavior is documented at https://sourceware.org/binutils/docs/ld/Entry-Point.html
> >>>
> >>> "the address of the first byte of the ?.text? section, if present;"
> >>>
> >>> The rule is quite ad-hoc and I'd support the removal.
> >>
> >> I saw that.  I still wonder where it came from originally.  It seems
> >> more like something for a bare-metal target.  Maybe it can be kept for
> >> those,
> >
> > Even if so, IMHO it should be updated to be "the text segment"
> > or words to that effect, as the .text *section* is likely just
> > main code located after .startup or .init or similar.
>
> A default entry point really doesn't make any sense for any
> target, bare metal or otherwise.  Yes, it's possible such a
> random guess is the right answer some of the time, but clearly
> any build can always supply an explicit start address -- and in
> my view that should be required.
>
> As an example, the DEC object format has a special code point
> meaning "no start address", and that is the default.

Let me rephrase: since we *can't* say "no start address", a
guess that is much more likely to be correct, is "the address of
the first byte of the text *segment*" (for formats where that
term applies) than the that of the .text *section*.  This
because code for embedded targets is likely linked into one blob
with the .text section being located only after other named
sections in the text segment and the start address being the
first address of that blob.

Perhaps we just talk past each other.

brgds, H-P

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

* Re: Default entry point for ELF shared objects
  2021-09-16 16:54         ` Hans-Peter Nilsson
@ 2021-09-16 17:01           ` Florian Weimer
  2021-09-16 17:02           ` Paul Koning
  1 sibling, 0 replies; 10+ messages in thread
From: Florian Weimer @ 2021-09-16 17:01 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: Paul Koning, binutils

* Hans-Peter Nilsson:

> Let me rephrase: since we *can't* say "no start address", a
> guess that is much more likely to be correct, is "the address of
> the first byte of the text *segment*" (for formats where that
> term applies) than the that of the .text *section*.

Just to be clear, ELF specifies 0 as “no start address”.  And I'm only
interested in ELF these days …

Thanks,
Florian


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

* Re: Default entry point for ELF shared objects
  2021-09-16 16:54         ` Hans-Peter Nilsson
  2021-09-16 17:01           ` Florian Weimer
@ 2021-09-16 17:02           ` Paul Koning
  1 sibling, 0 replies; 10+ messages in thread
From: Paul Koning @ 2021-09-16 17:02 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: Florian Weimer, binutils



> On Sep 16, 2021, at 12:54 PM, Hans-Peter Nilsson <hp@bitrange.com> wrote:
> 
> On Thu, 16 Sep 2021, Paul Koning wrote:
>> ...
>> A default entry point really doesn't make any sense for any
>> target, bare metal or otherwise.  Yes, it's possible such a
>> random guess is the right answer some of the time, but clearly
>> any build can always supply an explicit start address -- and in
>> my view that should be required.
>> 
>> As an example, the DEC object format has a special code point
>> meaning "no start address", and that is the default.
> 
> Let me rephrase: since we *can't* say "no start address", a
> guess that is much more likely to be correct, is "the address of
> the first byte of the text *segment*" (for formats where that
> term applies) than the that of the .text *section*.  This
> because code for embedded targets is likely linked into one blob
> with the .text section being located only after other named
> sections in the text segment and the start address being the
> first address of that blob.

I see the problem.  Yes, if there is no encoding for "no address" (not even zero) then your suggestion is the best available option.

	paul


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

* Re: Default entry point for ELF shared objects
@ 2021-09-17  5:40 dvalin
  0 siblings, 0 replies; 10+ messages in thread
From: dvalin @ 2021-09-17  5:40 UTC (permalink / raw)
  To: binutils


  On 16.09.21 19:01, Florian Weimer via Binutils wrote:
  > * Hans-Peter Nilsson:
  >
  > > Let me rephrase: since we *can't* say "no start address", a
  > > guess that is much more likely to be correct, is "the address
of
  > > the first byte of the text *segment*" (for formats where that
  > > term applies) than the that of the .text *section*.
  
  The linker script has to put .vector and .init_n input sections
into the .text 
  output section, if it doesn't place them first in the text segment,
so in my  decades of embedded experience, it comes down to how you
hack the  script.
  > Just to be clear, ELF specifies 0 as “no start address”. 
And I'm only
  > interested in ELF these days …

  Is it then not ideal to leave things as they are? With “no start
address”
  specified, ELF defaults to indicating “no start address”.

  The status quo evidently emanates from the glory days when bare
metal
  applications were all there was. (And still is here, 40 years
later.)

  On reset, many a microprocessor sets the program counter to zero,
which
  is the start of the text segment. (OK, that'll be the start of the
  interrupt vector table in many cases that I've encountered, and
hardware 
  reset can be viewed as the mother of all interrupts.)

  There are a few exceptions, requiring some target-specific start
address,
  sometimes selectable between piggy-in-the-middle and a bit short of
top
  of the address space. Exceptions have always required an explicit
start
  address, and there is nothing to take exception to there.

   So the safest course is to not rock the boat. That said, a change
from 
   hard-coded zero to start of the text segment ought to have zero
negative   effect in the common low-end embedded case, and may
beneficially 
  serve a number of non-zero start targets. (E.g. Boot from high
ROM,  then copy at least vectors down to zero address, possibly
followed by  the whole app. There, text LMA is ROM, VMA is in RAM for
all but the  bootstrap.)

  /2c

Erik

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

end of thread, other threads:[~2021-09-17  5:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-13  7:55 Default entry point for ELF shared objects Florian Weimer
2021-09-15 16:49 ` Fangrui Song
2021-09-16  5:01   ` Florian Weimer
2021-09-16  7:41     ` Andreas Schwab
2021-09-16 15:14     ` Hans-Peter Nilsson
2021-09-16 16:39       ` Paul Koning
2021-09-16 16:54         ` Hans-Peter Nilsson
2021-09-16 17:01           ` Florian Weimer
2021-09-16 17:02           ` Paul Koning
2021-09-17  5:40 dvalin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).