From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by sourceware.org (Postfix) with ESMTPS id A4E733857C41 for ; Tue, 25 Aug 2020 17:28:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A4E733857C41 Received: by mail-pj1-x1044.google.com with SMTP id i13so1612410pjv.0 for ; Tue, 25 Aug 2020 10:28:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=2vtH1G8txzk6cuTLr/vhs1yyeQ4wG8mW7J0LkdUef/Q=; b=W3CBufSkGyuXmfuemjdppkwtlnxTbZ/AlIm+IHYbLH8rEQDgHvfuWFjiuUunvq+Hh4 R+qF7HxVQnqQbBi60JLTZsqNcpefnR1ALdKwJ83LgrQB3EsZDi2Wdw4qtzmQjI+vBvXA 95gN1te94kZwFlc27vV5fOL45XFOxGN9s/hNfI87tEhqwXvSgPDvnyzKyTcv3p2pv9o7 Ax4th0LL6dwQ7xvJTDeG/qzDZ6mWX3htMettHhRYUDUwO5b07QnfLLaxhwqrzcPU8xfh 2G5poEG0HvSWD4xQziDnk+zeZw8e21H5eH1+2UPad74vndUFSP7en6agmwzifHfylcEe jMuQ== X-Gm-Message-State: AOAM530wtu8oPlvB0m7zkjAoDJqS2P7IbInqEiH7cluk/SQElhJqCq2m J5bZIeJ6se07qSrh5/KjcmLB/+xvCf8= X-Google-Smtp-Source: ABdhPJzL+Km/5+g3+S1CYWDdGzXHLtPfa89hvBPDn0897SZs7Dyrz7IM55JNi4guzjfQkz3fm274iA== X-Received: by 2002:a17:90a:4f0e:: with SMTP id p14mr2545243pjh.31.1598376524430; Tue, 25 Aug 2020 10:28:44 -0700 (PDT) Received: from gnu-cfl-2.localdomain (c-69-181-90-243.hsd1.ca.comcast.net. [69.181.90.243]) by smtp.gmail.com with ESMTPSA id a200sm5998956pfd.182.2020.08.25.10.28.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2020 10:28:43 -0700 (PDT) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id A1BF11A03A1 for ; Tue, 25 Aug 2020 10:28:42 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH] elf: Don't load archive element after dynamic definition Date: Tue, 25 Aug 2020 10:28:42 -0700 Message-Id: <20200825172842.1212936-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Aug 2020 17:28:47 -0000 Don't load the archive element after seeing a definition in a shared object. bfd/ PR ld/26530 * elflink.c (elf_link_add_object_symbols): Also preserve the dynamic_def bit for --as-needed. (elf_link_add_archive_symbols): Don't load the archive element after seeing a definition in a shared object. ld/ PR ld/26530 * testsuite/ld-plugin/lto.exp: Run PR ld/26530 tests. * testsuite/ld-plugin/pr26530a.c: New file. * testsuite/ld-plugin/pr26530b.c: Likewise. * testsuite/ld-plugin/pr26530c.c: Likewise. --- bfd/elflink.c | 20 ++++++++++++++------ ld/testsuite/ld-plugin/lto.exp | 20 ++++++++++++++++++++ ld/testsuite/ld-plugin/pr26530a.c | 5 +++++ ld/testsuite/ld-plugin/pr26530b.c | 7 +++++++ ld/testsuite/ld-plugin/pr26530c.c | 12 ++++++++++++ 5 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 ld/testsuite/ld-plugin/pr26530a.c create mode 100644 ld/testsuite/ld-plugin/pr26530b.c create mode 100644 ld/testsuite/ld-plugin/pr26530c.c diff --git a/bfd/elflink.c b/bfd/elflink.c index 17a423270e..97e6ece97e 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -5340,7 +5340,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) struct elf_link_hash_entry *h; bfd_size_type size; unsigned int alignment_power; - unsigned int non_ir_ref_dynamic; + struct elf_link_hash_entry preserved_h; for (p = htab->root.table.table[i]; p != NULL; p = p->next) { @@ -5362,10 +5362,11 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) size = 0; alignment_power = 0; } - /* Preserve non_ir_ref_dynamic so that this symbol - will be exported when the dynamic lib becomes needed - in the second pass. */ - non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; + /* Preserve some bits so that this symbol will be exported + when the dynamic lib becomes needed in the second pass. + and to avoid linking in an archive after the dynamic + lib. */ + preserved_h = *h; memcpy (p, old_ent, htab->root.table.entsize); old_ent = (char *) old_ent + htab->root.table.entsize; h = (struct elf_link_hash_entry *) p; @@ -5382,7 +5383,9 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) if (alignment_power > h->root.u.c.p->alignment_power) h->root.u.c.p->alignment_power = alignment_power; } - h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; + h->root.non_ir_ref_dynamic + = preserved_h.root.non_ir_ref_dynamic; + h->dynamic_def = preserved_h.dynamic_def; } } @@ -5819,6 +5822,11 @@ elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) if (h == NULL) continue; + /* Don't load the archive element after seeing a definition + in a shared object. */ + if (h->dynamic_def) + continue; + if (h->root.type == bfd_link_hash_undefined) { /* If the archive element has already been loaded then one diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp index 6a37025db5..8331500b33 100644 --- a/ld/testsuite/ld-plugin/lto.exp +++ b/ld/testsuite/ld-plugin/lto.exp @@ -419,6 +419,18 @@ set lto_link_elf_tests [list \ "-shared -Wl,--exclude-libs,ALL tmpdir/pr25618a.o tmpdir/pr25618.a" \ "-fpic" \ {dummy.c} {{readelf {--dyn-syms --wide} pr25618.d}} "pr25618.so" "c++"] \ + [list "Build pr26530a.so" \ + "-shared" "-O2 -fpic $no_lto" \ + {pr26530a.c} {} "pr26530a.so" "c"] \ + [list "Build pr26530b.so" \ + "-shared" "-O2 -fpic $no_lto" \ + {pr26530b.c} {} "pr26530b.so" "c"] \ + [list "Build pr26530a.a" \ + "" "-O2 $no_lto" \ + {pr26530a.c} {} "pr26530a.a" "c"] \ + [list "Build pr26530b.a" \ + "" "-O2 $no_lto" \ + {pr26530b.c} {} "pr26530b.a" "c"] \ ] # PR 14918 checks that libgcc is not spuriously included in a shared link of @@ -582,6 +594,14 @@ set lto_run_elf_shared_tests [list \ [list {pr22220b} \ {-flto -fuse-linker-plugin -Wl,--no-as-needed tmpdir/pr22220lib.so tmpdir/pr22220main.o} {} \ {dummy.c} {pr22220b.exe} {pass.out} {} {c++}] \ + [list "pr26530a" \ + "-O2 -flto -fuse-linker-plugin" "" \ + {pr26530c.c} "pr26530a.exe" "pass.out" "-O2 -flto" "c" {} \ + "-Wl,--as-needed tmpdir/pr26530a.so tmpdir/pr26530b.a"] \ + [list "pr26530b" \ + "-O2 -flto -fuse-linker-plugin" "" \ + {pr26530c.c} "pr26530b.exe" "pass.out" "-O2 -flto" "c" {} \ + "-Wl,--as-needed tmpdir/pr26530a.a tmpdir/pr26530b.so"] \ ] # LTO run-time tests for ELF diff --git a/ld/testsuite/ld-plugin/pr26530a.c b/ld/testsuite/ld-plugin/pr26530a.c new file mode 100644 index 0000000000..08e746911e --- /dev/null +++ b/ld/testsuite/ld-plugin/pr26530a.c @@ -0,0 +1,5 @@ +const char * +lrealpath (const char *a) +{ + return a; +} diff --git a/ld/testsuite/ld-plugin/pr26530b.c b/ld/testsuite/ld-plugin/pr26530b.c new file mode 100644 index 0000000000..0eb003f470 --- /dev/null +++ b/ld/testsuite/ld-plugin/pr26530b.c @@ -0,0 +1,7 @@ +#include + +void +lrealpath (void) +{ + abort (); +} diff --git a/ld/testsuite/ld-plugin/pr26530c.c b/ld/testsuite/ld-plugin/pr26530c.c new file mode 100644 index 0000000000..c8ef23675e --- /dev/null +++ b/ld/testsuite/ld-plugin/pr26530c.c @@ -0,0 +1,12 @@ +#include + +const char *lrealpath(const char *a); + +int +main(int argc, char **argv) +{ + const char *path = lrealpath (argv[0]); + if (path[0] != '\0') + printf ("PASS\n"); + return 0; +} -- 2.26.2