Bug 927573 - [RFE] ld.gold fails to recognize some commands in linker script.
Summary: [RFE] ld.gold fails to recognize some commands in linker script.
Keywords:
Status: CLOSED WONTFIX
Alias: None
Product: Red Hat Developer Toolset
Classification: Red Hat
Component: binutils
Version: DTS 1.1 RHEL 5
Hardware: All
OS: Mac OS
medium
medium
Target Milestone: alpha
: 8.0
Assignee: Nick Clifton
QA Contact: Martin Cermak
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2013-03-26 09:13 UTC by J.H.M. Dassen (Ray)
Modified: 2018-06-19 16:02 UTC (History)
8 users (show)

Fixed In Version:
Doc Type: Enhancement
Doc Text:
Clone Of:
Environment:
Last Closed: 2018-06-19 16:02:50 UTC
Target Upstream Version:


Attachments (Terms of Use)
Reproducer script (858 bytes, application/x-sh)
2013-03-26 09:13 UTC, J.H.M. Dassen (Ray)
no flags Details


Links
System ID Private Priority Status Summary Last Updated
Sourceware 15373 0 None None None 2019-02-22 13:03:25 UTC

Description J.H.M. Dassen (Ray) 2013-03-26 09:13:33 UTC
Created attachment 716384 [details]
Reproducer script

Description of problem:
ld.gold fails to recognize some commands in linker script. (while good with ld.bfd)

Version-Release number of selected component (if applicable):
devtoolset-1.1-binutils-2.23.51.0.3-3.el5

How reproducible:
100%

Steps to Reproduce:
Refer to attached reproducer script.
  
Actual results:
[ray@rhel5 ~]$ ./reproducer 
+ echo 'Testing ld.bfd (default):'
Testing ld.bfd (default):
+ sudo ln -sf /opt/rh/devtoolset-1.1/root/usr/bin/ld.bfd /opt/rh/devtoolset-1.1/root/etc/alternatives/ld
+ ls -l /opt/rh/devtoolset-1.1/root/etc/alternatives/ld
lrwxrwxrwx 1 root root 42 Mar 26 10:12 /opt/rh/devtoolset-1.1/root/etc/alternatives/ld -> /opt/rh/devtoolset-1.1/root/usr/bin/ld.bfd
+ /opt/rh/devtoolset-1.1/root/usr/bin/g++ -Wl,--script=./fail.lds a.c
+ echo 'Testing ld.gold (default):'
Testing ld.gold (default):
+ sudo ln -sf /opt/rh/devtoolset-1.1/root/usr/bin/ld.gold /opt/rh/devtoolset-1.1/root/etc/alternatives/ld
+ /opt/rh/devtoolset-1.1/root/usr/bin/g++ -Wl,--script=./fail.lds a.c
/opt/rh/devtoolset-1.1/root/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/ld: error: ./fail.lds:6:8: syntax error, unexpected STRING
/opt/rh/devtoolset-1.1/root/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/ld: fatal error: unable to parse script file ./fail.lds
collect2: error: ld returned 1 exit status
+ sudo ln -sf /opt/rh/devtoolset-1.1/root/usr/bin/ld.bfd /opt/rh/devtoolset-1.1/root/etc/alternatives/ld


Expected results:
No linker error when using ld.gold

Comment 1 Brad Hubbard 2013-03-26 11:09:48 UTC
Fails on F18 as well.

$ ld -v
GNU gold (version 2.23.51.0.1-6.fc18 20120806) 1.11
$ g++ -Wl,--script=./fail.lds -Wl,-v a.c
collect2 version 4.7.2 20121109 (Red Hat 4.7.2-8)
/usr/bin/ld --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../.. --script=./fail.lds -v /tmp/ccFivrrT.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o
/usr/bin/ld: error: ./fail.lds:6:8: syntax error, unexpected STRING
/usr/bin/ld: fatal error: unable to parse script file ./fail.lds
collect2: error: ld returned 1 exit status

Comment 5 Jeff Law 2013-04-16 16:01:53 UTC
The INSERT command was added to the BFD linker a few years ago, but no corresponding support was added to the gold linker.

The INSERT command was designed to make it easier to augment the default linker script;  gold, to the best of my knowledge, does not have a default linker script, so the INSERT command may not make sense for gold.  I've opened an upstream bug report to get this clarified.

I looked at the BFD linker's code to support INPUT and as far as I can tell it'd have to be completely rewritten to work with gold.  The earliest this would likely get addressed would be DTS 3.0 (assuming upstream agrees that INPUT has value for a gold linker script).

Comment 14 İsmail Dönmez 2015-12-08 11:39:01 UTC
FWIW Apple's swift on Linux uses this functionality in its linker script and fails with gold as expected.

Comment 15 Nick Clifton 2015-12-08 15:42:04 UTC
(In reply to İsmail Dönmez from comment #14)

It would be helpful to have some more examples of how the INSERT linker script directive is being used.

That said, it is unlikely that the GOLD linker will ever support the INSERT directive.  The reason being that GOLD does not have a default linker script, and so there is nothing into which script fragments can be INSERTed.  Note - the decision not to have a default linker script is a fundamental part of the design of GOLD and it is not going to change.

There are a couple of possible workarounds:

  * Use a full linker script.

  Instead of trying to insert fragments into a non-existent script, create a full script, with all of the extra symbols, alignments and other things as needed.  You can start with the default linker script built in the BFD linker (viewable via the "ld.bfd --verbose" command), and then add in the extra bits you need.  This solution has the added benefit that you can be sure that exactly the same section layout will be used by GOLD and LD.

  * Use object files instead of script fragments.

  For example the Reproducer script supplied as an example for this BZ attempts to insert two fragments into the linker script:

  SECTIONS
  {
    . = ALIGN(0x2000);
    PROVIDE_HIDDEN(__text_begin = .);
  }
  INSERT BEFORE .text

  SECTIONS
  {
    . = ALIGN(0x2000);
    PROVIDE_HIDDEN(__text_end = .);
  }
  INSERT AFTER .text

This functionality could be replicated by creating two small assembly files:

  % cat before.s
	.text
	.align 0x2000
	.global __text_begin
	.hidden __text_begin
  __text_begin:

  % cat after.s
	.text
	.align 0x2000
	.global __text_end
	.hidden __text_end
  __text_end:

and then including them in link stage as the first and last object files respectively.  This is the method recommended by the GOLD developers.

Cheers
  Nick

Comment 16 İsmail Dönmez 2015-12-08 15:59:24 UTC
Nick, thanks for your comments & example. FWIW here is what Swift uses atm:

λ cat stdlib/public/runtime/swift.ld
SECTIONS
{
  .swift2_protocol_conformances :
  {
    .swift2_protocol_conformances_start = . ;
    QUAD(SIZEOF(.swift2_protocol_conformances) - 8) ;
    *(.swift2_protocol_conformances) ;
  }
}
INSERT AFTER .dtors

Comment 17 Nick Clifton 2015-12-08 17:07:14 UTC
(In reply to İsmail Dönmez from comment #16)
 
Assuming that it is not really essential that the .swift2_protocol_conformances section immediately follows the .dtors section, (and that it is OK for GOLD to place it where it thinks is best) then something like this might work:

  % cat swift_start.s
	.section .swift2_protocol_conformances, "awx",@fini_array
	.global .swift2_protocol_conformances_start
  .swift2_protocol_conformances_start:

	.quad (.swift2_protocol_conformances_end - .swift2_protocol_conformances_start) - 8

  % cat swift_end.s
	.section .swift2_protocol_conformances, "awx",@fini_array
	.global .swift2_protocol_conformances_end
  .swift2_protocol_conformances_end:

and then link in swift_start.o before any swift-containing object files and swift_end.o after all swift-containing object files.

Untested of course, but worth investigating maybe ?

Comment 23 RHEL Program Management 2018-06-19 16:02:50 UTC
Development Management has reviewed and declined this request.
You may appeal this decision by reopening this request.


Note You need to log in before you can comment on or make changes to this bug.