From patchwork Mon Jan 3 17:11:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 49484 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 D39723858403 for ; Mon, 3 Jan 2022 17:12:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D39723858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1641229977; bh=Hau9jYNvMRGWnrEigXDAYgk+ex/EnqdiWYQdn3Z1L94=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=H/bB1yPlGxumID79s0FihXpyMOPPux4yQsiG57N3qaLcQ7yQ/zGDcmvvEBBbNLulY TEZO5W9UemMIA541BsHF63RKER22rt6AmuUil3g34EHE0ILKpencD2QMZKKmQLpnFH zB5YIibjHs6KLbLHHWMT3dMARnmZc9LZl0hyz8gw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8781A3858404 for ; Mon, 3 Jan 2022 17:11:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8781A3858404 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-444-dEx9MjUFMeO6BzF_QyoNQw-1; Mon, 03 Jan 2022 12:11:18 -0500 X-MC-Unique: dEx9MjUFMeO6BzF_QyoNQw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9698D2F45 for ; Mon, 3 Jan 2022 17:11:17 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EFC747ED65 for ; Mon, 3 Jan 2022 17:11:16 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 2/3] elf: Set l_contiguous to 1 for the main map in more cases In-Reply-To: References: X-From-Line: 1c4f55eb03365f9ea7238e7ab4a997802081bf6e Mon Sep 17 00:00:00 2001 Message-Id: <1c4f55eb03365f9ea7238e7ab4a997802081bf6e.1641228666.git.fweimer@redhat.com> Date: Mon, 03 Jan 2022 18:11:14 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" l_contiguous was not initialized at all for the main map and always 0. This commit adds code to check if the LOAD segments are adjacent to each other, and sets l_contiguous accordingly. This helps _dl_find_object because it is more efficient if the main mapping is contiguous. Note that not all (PIE or non-PIE) binaries are contiguous in this way because BFD ld creates executables with LOAD holes: ELF LOAD segments creating holes in the process image on GNU/Linux https://sourceware.org/pipermail/binutils/2022-January/119082.html https://sourceware.org/bugzilla/show_bug.cgi?id=28743 Reviewed-by: H.J. Lu --- elf/rtld.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/elf/rtld.c b/elf/rtld.c index ba6e31377d..53293f5b13 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1144,6 +1144,22 @@ rtld_setup_main_map (struct link_map *main_map) main_map->l_map_start = ~0; /* And it was opened directly. */ ++main_map->l_direct_opencount; + main_map->l_contiguous = 1; + + /* A PT_LOAD segment at an unexpected address will clear the + l_contiguous flag. The ELF specification says that PT_LOAD + segments need to be sorted in in increasing order, but perhaps + not all executables follow this requirement. Having l_contiguous + equal to 1 is just an optimization, so the code below does not + try to sort the segments in case they are unordered. + + There is one corner case in which l_contiguous is not set to 1, + but where it could be set: If a PIE (ET_DYN) binary is loaded by + glibc itself (not the kernel), it is always contiguous due to the + way the glibc loader works. However, the kernel loader may still + create holes in this case, and the code here still uses 0 + conservatively for the glibc-loaded case, too. */ + ElfW(Addr) expected_load_address = 0; /* Scan the program header table for the dynamic section. */ for (const ElfW(Phdr) *ph = phdr; ph < &phdr[phnum]; ++ph) @@ -1207,12 +1223,21 @@ rtld_setup_main_map (struct link_map *main_map) if (main_map->l_map_start > mapstart) main_map->l_map_start = mapstart; + if (main_map->l_contiguous && expected_load_address != 0 + && expected_load_address != mapstart) + main_map->l_contiguous = 0; + /* Also where it ends. */ allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz; if (main_map->l_map_end < allocend) main_map->l_map_end = allocend; if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end) main_map->l_text_end = allocend; + + /* The next expected address is the page following this load + segment. */ + expected_load_address = ((allocend + GLRO(dl_pagesize) - 1) + & ~(GLRO(dl_pagesize) - 1)); } break;