This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFH: Assignments folded too early


In the ld manual there's an example that's just perfect for the
problem I'm working on:

`DEFINED(SYMBOL)'
     Return 1 if SYMBOL is in the linker global symbol table and is
     defined before the statement using DEFINED in the script, otherwise
     return 0.  You can use this function to provide default values for
     symbols.  For example, the following script fragment shows how to
     set a global symbol `begin' to the first location in the `.text'
     section--but if a symbol called `begin' already existed, its value
     is preserved:

          SECTIONS { ...
            .text : {
              begin = DEFINED(begin) ? begin : . ;
              ...
            }
            ...
          }

But it wasn't working for me, and I've been able to create a
testcase.  This particular testcase is easy to solve but the overall
problem has me stumped.

Consider this source file:

        .text
padding:
        .long   1
        .long   2

        .section ".text1"
        .globl fallback
fallback:
        .long   3

        .globl begin
begin:
        .long   4

And this linker script:

SECTIONS
{
  .text :
  {
    *(.text)
    *(.text1)
    begin = DEFINED(begin) ? begin : fallback ;
  }
}

Works fine:

000000000000000c T begin
0000000000000008 T fallback
0000000000000000 t padding

But move the assignment higher, and it blows up:

SECTIONS
{
  .text :
  {
    *(.text)
    begin = DEFINED(begin) ? begin : fallback ;
    *(.text1)
  }
}

0000000000000004 T begin
0000000000000008 T fallback
0000000000000000 t padding

See how begin has moved into the middle of the .text input section?

The problem is in fold_name.  We skip folding NAME during
lang_first_phase_enum, but otherwise we read output_section and
output_offset.  But we're called while sizing sections.  output_offset
may not be known yet, and it may change.  If we don't preserve a link
to the original input section, then we can't update the output
symbol.

I can think of two solutions.  Keep relative symbols relative to their
input section, or always look up the "unshadowed" version of the
symbol when processing the RHS.  The former solution breaks for this
version:

    begin = DEFINED(begin) ? ABSOLUTE(begin) : fallback ;

So I think it has to be the latter solution.  I'll poke around at that
today.  It doesn't handle loops gracefully, but I can't think of
anything that does.

Thoughts?

-- 
Daniel Jacobowitz
CodeSourcery


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]