public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: rednoah <rednoax@163.com>
To: binutils@sourceware.org
Subject: Why GROUP(...) rather than INPUT(...) is used here?
Date: Fri, 17 Nov 2023 17:47:19 +0800 (CST)	[thread overview]
Message-ID: <18014862.5f25.18bdcad7f2b.Coremail.rednoax@163.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3747 bytes --]


Hi all:
If adding -v in gcc linking process for a simple m.c building, it shows -lgcc_s
is used during link:
$ gcc -v m.c -Wl,-t
....
 /usr/lib/gcc/x86_64-linux-gnu/9/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/9/liblto_plugin.so
 -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper -plugin-opt=-fresolution=/tmp/cccTnaHb.res
 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc
 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr -m elf_x86_64
 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro
 /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o
 /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o
 /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/9
 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib
 -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../..
 /tmp/ccYxryTe.o -t -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s
 --pop-state /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crtn.o
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o
/tmp/ccYxryTe.o
/usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a <= libgcc.a is processed for the 1st time
/usr/lib/gcc/x86_64-linux-gnu/9/libgcc_s.so
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libgcc_s.so.1
/usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a <= libgcc.a is processed for the 2nd time
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libc.so
...
the "-Wl,-t" output shows libgcc.a is processed twice as marked above.

on ubuntu 20.04, libgcc_s.so is actually a script.
$ cat  /usr/lib/gcc/x86_64-linux-gnu/9/libgcc_s.so
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library.  */
GROUP ( libgcc_s.so.1 -lgcc )

The "-lgcc" linked here is actually libgcc.a. "libgcc_s.so.1" is an elf
shared object so there is only one archive file "libgcc.a" in GROUP(...).
GROUP(...) equals to "--start-group ... --end-group" and it is to enclose
two or more archive files to search undefined symbols repeatedly. There
seems no need to uss it when there is only one archive file in it. Maybe
INPUT(...) is a better choice here?

To verify it I write some toy .c, there seems no problem:
m.c:
int _start(){
    return b1();
}
b0.c:
int b0() {
    return 0;
}
b1.c:
int b1() {
    return b0();
}
$ gcc -c -fpic *.c
$ ar rcs b.a b0.o b1.o <=b0.o is in front of b1.o
$ gcc -nostdlib -Wl,-t,-Map=1.map,-verbose m.o b.a
......
/usr/bin/ld: mode elf_x86_64
attempt to open m.o succeeded
m.o
attempt to open b.a succeeded
b.a
(b.a)b1.o
(b.a)b0.o

In archive b.a, b0.o is in front of b1.o. b1.o has reference to
b0 in b0.o. Although b.a is processed only once, no any error. I
guess it may be achieved by the archive index, which contains all
symbols of each .o in b.a.

The following is to emulate the afront mentioned gcc's way, the
m.o and b.a are enclosed by --start/end-group:
$ gcc -nostdlib -Wl,-t,-Map=1.map,-verbose '-Wl,-(' m.o b.a '-Wl,-)'
...
/usr/bin/ld: mode elf_x86_64
attempt to open m.o succeeded
m.o
attempt to open b.a succeeded
b.a
(b.a)b1.o
(b.a)b0.o
b.a

No error and b.a is proccessed twice, just like libgcc.a is
processed twice.
Could anyone help explain why libgcc_s.so script use GROUP(...)
for only one archive: GROUP ( libgcc_s.so.1 -lgcc ).
Can it be replaced with INPUT(...)?

Thanks

             reply	other threads:[~2023-11-17  9:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-17  9:47 rednoah [this message]
2023-11-20 15:23 ` Nick Clifton
2023-11-22  0:00   ` rednoah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18014862.5f25.18bdcad7f2b.Coremail.rednoax@163.com \
    --to=rednoax@163.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).