From patchwork Tue Sep 7 13:16:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Majewski X-Patchwork-Id: 44886 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 43C093851C0B for ; Tue, 7 Sep 2021 13:16:47 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) by sourceware.org (Postfix) with ESMTPS id 81F77385842D for ; Tue, 7 Sep 2021 13:16:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81F77385842D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=denx.de Received: from localhost.localdomain (85-222-111-42.dynamic.chello.pl [85.222.111.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: lukma@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id C837E82C30; Tue, 7 Sep 2021 15:16:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1631020593; bh=9XDsMxBWHHYMHAloDutkWqeTOKMeDs2MTxmwLAZFlgk=; h=From:To:Cc:Subject:Date:From; b=rCKWDpowhQAFup8v/gOP3AXsIyhM0SNy37Pz7tR/lF0KVoQU9D5JKRN76N3yxrt6T G5XQGD6q8YW8pL2K9acG9BbXr1Z0sgWAhcBboKcGnmo55SxqPDjmLBXGXu8U6Tz2Fn fkmKX/86JVUmOHZOwl9Gb2ufJA/8N2bbGVQE5iH7wh0+QZr1R5X5D6Mz2Qt4SHYLgI Kipxs+LmwRJu0xYpQY+jQcZgOXOqtUacFv8fK7ApnqCq05Miyg9m8RuKZ3dvh5RU+7 HI/2xQs2DFlujosRsNFWet6djUIqjbzo4GynQH1sdoAvR5Jh3IqWJxUYChQP5asLZT A/Iwi77LJQjGQ== From: Lukasz Majewski To: Adhemerval Zanella , Fangrui Song , Florian Weimer Subject: [PATCH] dl: Use "adr" assembler command to get proper load address Date: Tue, 7 Sep 2021 15:16:16 +0200 Message-Id: <20210907131616.23472-1-lukma@denx.de> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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: , Cc: libc-alpha , Joseph Myers Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This change is a partial revert of commit bca0f5cbc9257c13322b99e55235c4f21ba0bd82 which imposed usage of __ehdr_start linker variable to get the address of loaded program. The elf_machine_load_address() function is declared in the sysdeps/arm/dl-machine.h header. It is called from _dl_start() entry point for the program. It shall return the load address of the dynamic linker program. With this revert the 'adr' assembler instruction is used instead of a place holder: arm-poky-linux-gnueabi-objdump -t ld-linux-armhf.so.3 | grep ehdr 00000000 l .note.gnu.build-id 00000000 __ehdr_start which shall be pre-set by binutils. This is crucial in the QEMU ARM environment for which (when /sbin/init is executed) values set in __ehdr_start symbol are wrong. This causes the program to crash very early - when the /lib/ld-linux-armhf.so.3 is executed as a prerequisite to /sbin/init execution. The kernel's fs/binfmt_elf.c is though responsible for setting up execution environment, not binutils. It looks like the only robust way to obtain the _dl_start offset is to use assembler instruction - not rely on values provided by binutils. HW: Hardware name: ARM-Versatile Express (Run with QEMU) Tested (affected) kernels v5.1.12, v5.10.62 and v5.14.1 When the /sbin/init is setup for run from Linux kernel's very small environment with LD_DEBUG=all the __ehdr_start is not shown at all. Fixes: BZ #28293 --- sysdeps/arm/dl-machine.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h index eb13cb8b57..58ebef6ecd 100644 --- a/sysdeps/arm/dl-machine.h +++ b/sysdeps/arm/dl-machine.h @@ -38,11 +38,33 @@ elf_machine_matches_host (const Elf32_Ehdr *ehdr) } /* Return the run-time load address of the shared object. */ -static inline ElfW(Addr) __attribute__ ((unused)) +static inline Elf32_Addr __attribute__ ((unused)) elf_machine_load_address (void) { - extern const ElfW(Ehdr) __ehdr_start attribute_hidden; - return (ElfW(Addr)) &__ehdr_start; + Elf32_Addr pcrel_addr; +#ifdef SHARED + extern Elf32_Addr __dl_start (void *) asm ("_dl_start"); + Elf32_Addr got_addr = (Elf32_Addr) &__dl_start; + asm ("adr %0, _dl_start" : "=r" (pcrel_addr)); +#else + extern Elf32_Addr __dl_relocate_static_pie (void *) + asm ("_dl_relocate_static_pie") attribute_hidden; + Elf32_Addr got_addr = (Elf32_Addr) &__dl_relocate_static_pie; + asm ("adr %0, _dl_relocate_static_pie" : "=r" (pcrel_addr)); +#endif +#ifdef __thumb__ + /* Clear the low bit of the function address. + + NOTE: got_addr is from GOT table whose lsb is always set by linker if it's + Thumb function address. PCREL_ADDR comes from PC-relative calculation + which will finish during assembling. GAS assembler before the fix for + PR gas/21458 was not setting the lsb but does after that. Always do the + strip for both, so the code works with various combinations of glibc and + Binutils. */ + got_addr &= ~(Elf32_Addr) 1; + pcrel_addr &= ~(Elf32_Addr) 1; +#endif + return pcrel_addr - got_addr; } /* Return the link-time address of _DYNAMIC. */