From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id DD8273858D28 for ; Fri, 24 Sep 2021 06:55:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DD8273858D28 Received: by mail-pj1-x1031.google.com with SMTP id p12-20020a17090adf8c00b0019c959bc795so6878889pjv.1 for ; Thu, 23 Sep 2021 23:55:13 -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:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=paK64ZtI7ZFVUMV1HyXCeUTJODtF7SJvffxiCgA636k=; b=CDY611NCQ15HEPwCGSndSSgQerfeg+X7GZPN+Yccz13xQCc+T6kfguyj/ZJWdvi+Qw CmNMI1hNmvY02fBu4kKuxehv/FhGQFiGHay3o70VtfCbBTAy3Xo2z8vump02NZQEw3Qo Nb/Y0T54shSN+U7IZ8KKI5bGTmsi0kyevT3zclrCHcG3Is8ZlP3E6lveh+qBUTPKpvsH sHe0JjeunLNEPwjGPDkpIIqK50mPyKpMFgH98pDkUu9MnDCrncyZUWaiv+RPsc/K64ea co8yEFZ1szKU0W2IVx6lTHlZN/99yHfneR3+zkQihxSUd6hyT3X1HFCeIAQLaZoEJOq/ 6RDQ== X-Gm-Message-State: AOAM5323fL5DhV30tUx6wI7oTjpWvOTFuFQpxR90WFkYn1TOSw0cvBU0 wuYdIud7Ow2TNOTtBTWdIgqkfaQOhpsoKQ== X-Google-Smtp-Source: ABdhPJzThhsTH5bUPCqrvAGCXg+u/lY4FwYktlIYA69oLkUUB7XO9/TS3beELVCpAj1zi70Xu2J7IA== X-Received: by 2002:a17:90b:4c87:: with SMTP id my7mr442659pjb.10.1632466512715; Thu, 23 Sep 2021 23:55:12 -0700 (PDT) Received: from google.com ([2620:15c:2ce:200:7876:453c:557e:a27a]) by smtp.gmail.com with ESMTPSA id z12sm7398231pjb.52.2021.09.23.23.55.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Sep 2021 23:55:12 -0700 (PDT) Date: Thu, 23 Sep 2021 23:55:08 -0700 From: Fangrui Song To: "H.J. Lu" Cc: GNU C Library Subject: Re: [PATCH] i386: Port elf_machine_{load_address, dynamic} from x86-64 Message-ID: <20210924065508.ewyzqjczfx4coipf@google.com> References: <20210924042623.3899762-1-maskray@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH, FSL_HELO_FAKE, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL 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-List-Received-Date: Fri, 24 Sep 2021 06:55:15 -0000 On 2021-09-23, H.J. Lu wrote: >On Thu, Sep 23, 2021 at 9:26 PM Fangrui Song wrote: >> >> This drops reliance on _GLOBAL_OFFSET_TABLE_[0] being the link-time >> address of _DYNAMIC. >> >> The code sequence length does not change. >> --- >> sysdeps/i386/dl-machine.h | 29 +++++++++++------------------ >> 1 file changed, 11 insertions(+), 18 deletions(-) >> >> diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h >> index 590b41d8d7..9f0eeaf66e 100644 >> --- a/sysdeps/i386/dl-machine.h >> +++ b/sysdeps/i386/dl-machine.h >> @@ -34,27 +34,20 @@ elf_machine_matches_host (const Elf32_Ehdr *ehdr) >> } >> >> >> -/* Return the link-time address of _DYNAMIC. Conveniently, this is the >> - first element of the GOT, a special entry that is never relocated. */ >> -static inline Elf32_Addr __attribute__ ((unused, const)) >> -elf_machine_dynamic (void) >> -{ >> - /* This produces a GOTOFF reloc that resolves to zero at link time, so in >> - fact just loads from the GOT register directly. By doing it without >> - an asm we can let the compiler choose any register. */ >> - extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden; >> - return _GLOBAL_OFFSET_TABLE_[0]; >> -} >> - >> /* Return the run-time load address of the shared object. */ >> -static inline Elf32_Addr __attribute__ ((unused)) >> +static inline ElfW(Addr) __attribute__ ((unused)) >> elf_machine_load_address (void) >> { >> - /* Compute the difference between the runtime address of _DYNAMIC as seen >> - by a GOTOFF reference, and the link-time address found in the special >> - unrelocated first GOT entry. */ >> - extern Elf32_Dyn bygotoff[] asm ("_DYNAMIC") attribute_hidden; >> - return (Elf32_Addr) &bygotoff - elf_machine_dynamic (); >> + extern const ElfW(Ehdr) __ehdr_start attribute_hidden; >> + return (ElfW(Addr)) &__ehdr_start; >> +} >> + >> +/* Return the link-time address of _DYNAMIC. */ >> +static inline ElfW(Addr) __attribute__ ((unused)) >> +elf_machine_dynamic (void) >> +{ >> + extern ElfW(Dyn) _DYNAMIC[] attribute_hidden; >> + return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address (); >> } >> >> /* Set up the loaded object described by L so its unrelocated PLT >> -- >> 2.33.0.685.g46640cef36-goog >> > >what are the code differences before and after? long ehdr(void) { extern char __ehdr_start[] __attribute__((visibility("hidden"))); return (long)__ehdr_start; } long got(void) { extern long _GLOBAL_OFFSET_TABLE_[] __attribute__((visibility("hidden"))); return _GLOBAL_OFFSET_TABLE_[0]; } ehdr: call __x86.get_pc_thunk.ax addl $_GLOBAL_OFFSET_TABLE_, %eax leal __ehdr_start@GOTOFF(%eax), %eax ret got: call __x86.get_pc_thunk.ax addl $_GLOBAL_OFFSET_TABLE_, %eax movl _GLOBAL_OFFSET_TABLE_@GOTOFF(%eax), %eax ret In GCC generated elf/rtld.os, the local code sequence related to __ehdr_start/_GLOBAL_OFFSET_TABLE_ does not change its size, but globally the new code triggers some code motion and eventually makes the file smaller. FWIW ld.so:.text is 48 bytes smaller. New code doesn't have memory load. I guess it may allow GCC to optimize more.