Bug 26268

Summary: --gc-sections -u __start_cident inconsistent for -r and non-relocatable links
Product: binutils Reporter: Fangrui Song <i>
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED NOTABUG    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Fangrui Song 2020-07-20 17:53:26 UTC
% cat a.s
.section cident,"a",@progbits
.byte 0
% as a.s -o a.o

% ld.bfd -r --gc-sections --print-gc-sections -u __start_cident a.o -o a
ld.bfd: removing unused section 'cident' in file 'a.o'
% ld.bfd --gc-sections --print-gc-sections -u __start_cident a.o -o a   
ld.bfd: warning: cannot find entry symbol _start; not setting start address


Non-relocatable links retain 'cident' while relocatable links discard it. I don't know whether the rule should be consistent here.

I am also thinking how 'cident' should behave when it is in a section group.
Comment 1 Andreas Schwab 2020-07-20 18:06:11 UTC
A relocatable link doesn't create __start and __end symbols, thus the undefined reference to __start_cident is not related to any section.  Use -Ur if you want them.
Comment 2 Fangrui Song 2020-07-20 18:55:31 UTC
(In reply to Andreas Schwab from comment #1)
> A relocatable link doesn't create __start and __end symbols, thus the
> undefined reference to __start_cident is not related to any section.  Use
> -Ur if you want them.

The documentation says -Ur is equivalent to -r for non-C++ programs

@kindex -Ur
@cindex constructors
@item -Ur
For anything other than C++ programs, this option is equivalent to
@samp{-r}: it generates relocatable output---i.e., an output file that can in
turn serve as input to @command{ld}.  When linking C++ programs, @samp{-Ur}
@emph{does} resolve references to constructors, unlike @samp{-r}.
It does not work to use @samp{-Ur} on files that were themselves linked
with @samp{-Ur}; once the constructor table has been built, it cannot
be added to.  Use @samp{-Ur} only for the last partial link, and
@samp{-r} for the others.


ld -r is allowed to not defined __start_cindent, but it should not discard the sections. Not only -u __start_cindent does not retain 'cindent', a relocation '.quad __start_cindent' does not retain 'cindent' as well.

I don't think this is invalid.
Comment 3 H.J. Lu 2020-07-26 12:34:15 UTC
__start_cident isn't meaningful for -r since the output cident section isn't
the final one.  Try "-T keep.t" with

--keep.t--
SECTIONS
{
  cident : { KEEP(*(cident)) }
}
--
Comment 4 Alan Modra 2024-02-22 03:39:10 UTC
comment #1 is the answer to this question