From patchwork Sat Apr 11 22:16:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 38804 Return-Path: <30UGSXgcKCpgE2KCJ2Q8GG8D6.4GEDA34-2DH92KGMJ46O2J6.GJ8@flex--maskray.bounces.google.com> X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by sourceware.org (Postfix) with ESMTPS id A28ED385DC05 for ; Sat, 11 Apr 2020 22:16:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A28ED385DC05 Received: by mail-pf1-x44a.google.com with SMTP id s8so5018929pfd.23 for ; Sat, 11 Apr 2020 15:16:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=zed2aLu1nNwNOcQ1rySe3IaO91G5cuFOH1nEtRn7d0A=; b=nVnf/2iuiE8YDGiHiCvxPwnnefzPDmIVg0QuP1HX5MeUTLBqw6h0P6kVPY2m3KakSp pzXK1ZyLy6AN3eFcwI6jisx/W+hKybTj489nzvb/DNe42X/Okqhu/oY66ihTKlBaxC+g iaLycIXKhZiY0CjeJYX32i69zqDpH0MPkMjcQgFC2aZhAe4b0qIH3IrZwIG6o7BCg+R5 l80ZzpvQWXv+DX1k5Y7gZghwhJokdubAarAxSWuy7mpOXcBZg/dYnWhFGeXB3JVUXfCQ SnEnGFn0mnkHMUooM6mXuuN2obJ88nJX1dYFCNU8tyLzMOG5jIyAXAUdmxlZ3Q04qOol 9DQw== X-Gm-Message-State: AGi0PubAcdTH18p90yBQUMR3y8mHljx1RlzuaIo52AGo4L1BkyRB7pnu 4mXC+5Ex9hVQMJjqeAppCWq312G68n2S3eCGIwO6Ik9TPrro4htoMOqWux7T+1l2Z+YgCwFKWVR 46ndjBK4OYTXQmp2gHRzwH3uz2JclZy6TdsETPRA0gcVhI2Oh3T6bOb+RWGbDiZL9cxbj X-Google-Smtp-Source: APiQypItNOE4tRJ0NJO9/fxzGYDmqTJXsr2MYVAeU/SHtuJGAaCQkbrT4hEOhzEv2dLpO1MltUNxGb+2jB+M X-Received: by 2002:a17:90a:2170:: with SMTP id a103mr13307967pje.181.1586643409708; Sat, 11 Apr 2020 15:16:49 -0700 (PDT) Date: Sat, 11 Apr 2020 15:16:46 -0700 Message-Id: <20200411221646.185882-1-maskray@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.26.0.110.g2183baf09c-goog Subject: [PATCH] elf: Replace a --defsym trick with an object file to be compatible with lld From: Fangrui Song To: libc-alpha@sourceware.org Cc: Florian Weimer , Fangrui Song X-Spam-Status: No, score=-30.8 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 11 Apr 2020 22:16:52 -0000 The existing code specifies -Wl,--defsym=malloc=0 and other malloc.os definitions before libc_pic.a so that libc_pic.a(malloc.os) is not fetched. This trick is used to avoid multiple definition errors which would happen as a chain result: dl-allobjs.os has an undefined __libc_scratch_buffer_set_array_size __libc_scratch_buffer_set_array_size fetches libc_pic.a(scratch_buffer_set_array_size.os) libc_pic.a(scratch_buffer_set_array_size.os) has an undefined free free fetches libc_pic.a(malloc.os) libc_pic.a(malloc.os) has an undefined __libc_message __libc_message fetches libc_pic.a(libc_fatal.os) libc_fatal.os will cause a multiple definition error (__GI___libc_fatal) >>> defined at dl-fxstatat64.c >>> /tmp/p/glibc/Release/elf/dl-allobjs.os:(__GI___libc_fatal) >>> defined at libc_fatal.c >>> libc_fatal.os:(.text+0x240) in archive /tmp/p/glibc/Release/libc_pic.a lld processes --defsym after all input files, so this trick does not suppress multiple definition errors with lld. Split the step into two and use an object file to make the intention more obvious and make lld work. This is conceptually more appropriate because --defsym defines a SHN_ABS symbol while a normal definition is relative to the image base. See https://sourceware.org/pipermail/libc-alpha/2020-March/111910.html for discussions about the --defsym semantics. --- elf/Makefile | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/elf/Makefile b/elf/Makefile index 9b1d58c7ad..de5dff649d 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -497,10 +497,6 @@ rtld-stubbed-symbols = \ malloc \ realloc \ -# The GCC arguments that implement $(rtld-stubbed-symbols). -rtld-stubbed-symbols-args = \ - $(patsubst %,-Wl$(comma)--defsym=%=0, $(rtld-stubbed-symbols)) - ifeq ($(have-ssp),yes) # rtld is not built with the stack protector, so these references will # go away in the rebuilds. @@ -509,9 +505,10 @@ endif $(objpfx)librtld.map: $(objpfx)dl-allobjs.os $(common-objpfx)libc_pic.a @-rm -f $@T - $(reloc-link) -o $@.o $(rtld-stubbed-symbols-args) \ - '-Wl,-(' $^ -lgcc '-Wl,-)' -Wl,-Map,$@T - rm -f $@.o + echo '$(patsubst %,.globl %;,$(rtld-stubbed-symbols)) $(patsubst %,%:,$(rtld-stubbed-symbols))' | \ + $(CC) -o $@T.o $(ASFLAGS) -c -x assembler - + $(reloc-link) -o $@.o $@T.o '-Wl,-(' $^ -lgcc '-Wl,-)' -Wl,-Map,$@T + rm -f %@T.o $@.o mv -f $@T $@ $(objpfx)librtld.mk: $(objpfx)librtld.map Makefile