From patchwork Fri Oct 15 07:54:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Majewski X-Patchwork-Id: 46262 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 B429D385783A for ; Fri, 15 Oct 2021 07:54:54 +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 202683858412 for ; Fri, 15 Oct 2021 07:54:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 202683858412 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 89F3B8343C; Fri, 15 Oct 2021 09:54:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1634284480; bh=JY/uq7febL1ZBjlZIf3HdshnDmvnfbpSJEXblr5erVo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xDn/JfMsJAihbw5ywCQzBD//PjMYS7+0G14hKdBzcrKR2PUxPoty5c+LiNTf+YK8H W8dkyPHCnWuZZxDob80zMDqsOdWuf/Hqqmq5O2cNjVVFsSo1qQK0xP5xFDKWj4ybyD Z77ymYsgL1bgMxuakiRaOBirfa6aXpibacsQu396uJ2uaCeHyDu7WsArb+5HPkDvC7 qJ6CT8yDL92Vzii6CiMSgYjtqKY4t0yk+yG2O7jKN4NK+TzQRDCHeXQF1i2ldO+UfO NqjL5TUwq5W90QAmM9forWNebhZwXWNsZceR4qk+tX0G4fkndAWvgqnl3U0Vi7OWqt 9OISnEyWoLXPw== From: Lukasz Majewski To: Adhemerval Zanella , Fangrui Song , Florian Weimer Subject: [PATCH v2] dl: Use "adr" assembler command to get proper load address on ARM Date: Fri, 15 Oct 2021 09:54:17 +0200 Message-Id: <20211015075417.29931-1-lukma@denx.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210907131616.23472-1-lukma@denx.de> References: <20210907131616.23472-1-lukma@denx.de> 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=-10.6 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 , Szabolcs Nagy , Andreas Schwab , Joseph Myers Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This change is a partial revert of commit bca0f5cbc9257c13322b99e55235c4f21ba0bd82 "arm: Simplify elf_machine_{load_address,dynamic}" 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 (very early) _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 is pre-set by binutils. The problem starts when one runs 'prelink' on the rootfs created with for example OE/Yocto. Then the _ehdr_start stays as 0x0, but the ELF header's sections have different addresses - for example 0x41000000 instead of the originally set 0x0. This is crucial when /sbin/init is executed. Value set in __ehdr_start symbol is not updated. This causes the program to crash very early when ld-linux-armhf.so.3's _dl_start is executed, as calculated offset for loader relocation is going to hit the kernel space (0xf7xxyyyy). It looks like the correct way to obtain the _dl_start offset on ARM is to use assembler instruction 'adr' at execution time (so the prelink assigned offset is taken into consideration) instead of __ehdr_start. With this patch we only modify the elf_machine_load_address() function, as it is called very early, before the ld-linux-armhf.so.3 is performing relocation (also its own one). HW: Hardware name: - ARM-Versatile Express (Run with QEMU) - Beagle Bone Black Build Environment: OE/Yocto -> poky SHA1: 1e2e9a84d6dd81d7f6dd69c0d119d0149d10ade1 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 dfa05eee44..d6e5f1d5ec 100644 --- a/sysdeps/arm/dl-machine.h +++ b/sysdeps/arm/dl-machine.h @@ -39,11 +39,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. */