From patchwork Wed Apr 27 12:35:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 53263 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D01983857C48 for ; Wed, 27 Apr 2022 12:35:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D01983857C48 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1651062950; bh=D3iwuVS0kIY1XKv1LCMXOy1CbPaaMaPr9QOhX6r+mSI=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=DnIisg0BG6x2l7CU7aro85tBG1viFEmUddbyjNI/5GXxomnQguqPUdaC6zrMRaVhB h5EP8mQaDihjxbTzYAGAcK+DGP9vP4LOF0i/sxhAFYpTPwWO2hkcPvAnVtRQ+cQORr OdFwQu5BSCvS4ZtPjx+IhuhyiVaptl++pDoUvRCw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id 31E473857428 for ; Wed, 27 Apr 2022 12:35:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 31E473857428 Received: by mail-pj1-x1032.google.com with SMTP id fv2so1362782pjb.4 for ; Wed, 27 Apr 2022 05:35:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=D3iwuVS0kIY1XKv1LCMXOy1CbPaaMaPr9QOhX6r+mSI=; b=WxqZKStw9WGDkpdhVCb0x2gR6A1Q54NbzXcgOLYzsih6xvqTnXGMqUr8mmiyR6tIxw 56GRsb2+og3stAVAplDOFHrlN8A6IJ/u4hDOnuWHRAI7viN7Qatly5g5+rOxdr5XLyn5 L6qhIUWZJlJk0diQHxSHQ6TdFOWsngHn2wTwLhuw29dq/v2Y7YRobdyTK6NO9rNckp26 FHQyLNHacZdxYywBaha2CEcTwVU01I+g5xPhR4M97eB/Gbtm1uo50BTH9/5VzGoVUohE HZH9zUnvIfkb/sl2PFl+zSUopaArdU2l0AKewy0rtHX1AA9rXIqNmoEqRjKNkQWf5DqY 7KYg== X-Gm-Message-State: AOAM5310hINW0B271DShVjlNfZ3UsEhQEmg6CID85uMAo/uorMpVjK9+ niJ/iP6lEyPLiqd6Gg2uILBjjEaH8A0= X-Google-Smtp-Source: ABdhPJwByUMAeppW93wK72BEuciLTvtII04YObtuBg0IncczrHXSm+P01gE35c/z16q2yv7bKGQ9dg== X-Received: by 2002:a17:902:c215:b0:14f:f1c2:9fe3 with SMTP id 21-20020a170902c21500b0014ff1c29fe3mr28596109pll.145.1651062904857; Wed, 27 Apr 2022 05:35:04 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:e95f:654b:adec:28c9]) by smtp.gmail.com with ESMTPSA id w8-20020a17090a380800b001d7d8b33121sm2862898pjb.5.2022.04.27.05.35.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 05:35:03 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 15B5F1140085; Wed, 27 Apr 2022 22:05:01 +0930 (ACST) Date: Wed, 27 Apr 2022 22:05:01 +0930 To: libc-alpha@sourceware.org Subject: Use __ehdr_start rather than _begin in _dl_start_final Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3036.7 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-Patchwork-Original-From: Alan Modra via Libc-alpha From: Alan Modra Reply-To: Alan Modra Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" __ehdr_start is already used in rltld.c:dl_main, and can serve the same purpose as _begin. Besides tidying the code, using linker defined section relative symbols rather than "-defsym _begin=0" better reflects the intent of _dl_start_final use of _begin, which is to refer to the load address of ld.so rather than absolute address zero. The motive for this patch is to finally tidy ppc32 GNU ld treatment of absolute symbols. On ppc32, the reference to _begin generates a GOT entry. A GOT entry for an absolute symbol shouldn't be dynamically relocated, so this usage in glibc of an absolute _begin will fail once I fix ppc32 ld. Note that on many other targets, including ppc64, gcc generates pc relative or got-pointer relative addressing for _begin. ie. the compiler assumes _begin is *not* at an absolute address, and generally linkers go along with that. Other uses of absolute symbols in glibc, eg. see _NL_CURRENT_DEFINE_ABS should not be dynamically relocated, but are with current ppc32 GNU ld. This doesn't cause a problem in glibc since the references are either undefined weak (and value zero) or non-zero and relocated to another non-zero value, and glibc just tests for zero/non-zero. diff --git a/elf/Makefile b/elf/Makefile index ad253defdd..c8a351e2ae 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1326,8 +1326,7 @@ $(objpfx)ld.so: $(objpfx)librtld.os $(ld-map) $(LINK.o) -nostdlib -nostartfiles -shared -o $@.new \ $(LDFLAGS-rtld) -Wl,-z,defs $(z-now-$(bind-now)) \ $(filter-out $(map-file),$^) $(load-map-file) \ - -Wl,-soname=$(rtld-installed-name) \ - -Wl,-defsym=_begin=0 + -Wl,-soname=$(rtld-installed-name) $(call after-link,$@.new) $(READELF) -s $@.new \ | $(AWK) '($$7 ~ /^UND(|EF)$$/ && $$1 != "0:" && $$4 != "REGISTER") { print; p=1 } END { exit p != 0 }' diff --git a/elf/rtld.c b/elf/rtld.c index be6daa1c44..3b2e05bf4c 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -440,8 +440,8 @@ static ElfW(Addr) _dl_start_final (void *arg, struct dl_start_final_info *info); #endif -/* These defined magically in the linker script. */ -extern char _begin[] attribute_hidden; +/* These are defined magically by the linker. */ +extern const ElfW(Ehdr) __ehdr_start attribute_hidden; extern char _etext[] attribute_hidden; extern char _end[] attribute_hidden; @@ -490,7 +490,7 @@ _dl_start_final (void *arg, struct dl_start_final_info *info) #endif _dl_setup_hash (&GL(dl_rtld_map)); GL(dl_rtld_map).l_real = &GL(dl_rtld_map); - GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin; + GL(dl_rtld_map).l_map_start = (ElfW(Addr)) &__ehdr_start; GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end; GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext; /* Copy the TLS related data if necessary. */ @@ -1741,7 +1741,6 @@ dl_main (const ElfW(Phdr) *phdr, segment that also includes the phdrs. If that's not available, we use the old method that assumes the beginning of the file is part of the lowest-addressed PT_LOAD segment. */ - extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden"))); /* Set up the program header information for the dynamic linker itself. It is needed in the dl_iterate_phdr callbacks. */