From patchwork Thu May 18 08:28:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: stsp X-Patchwork-Id: 69579 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 5BFE738432D2 for ; Thu, 18 May 2023 08:33:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from forward101c.mail.yandex.net (forward101c.mail.yandex.net [IPv6:2a02:6b8:c03:500:1:45:d181:d101]) by sourceware.org (Postfix) with ESMTPS id 386F13854143 for ; Thu, 18 May 2023 08:29:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 386F13854143 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=yandex.ru Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=yandex.ru Received: from mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net [IPv6:2a02:6b8:c0c:1186:0:640:38cb:0]) by forward101c.mail.yandex.net (Yandex) with ESMTP id 0B83D60140 for ; Thu, 18 May 2023 11:29:42 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id XTYe39MDZ0U0-SQfHF3xi; Thu, 18 May 2023 11:29:41 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1684398581; bh=0ffDyvPXby1bKU1olIDUBEeEH86jH7UYRjSa4MWBF84=; h=Message-Id:Date:In-Reply-To:Cc:Subject:References:To:From; b=MMCUJz9gKWHtgu9iO4EQk/jzUzQiWT+rlxfwPqhW4ukDy5h0lYOnjvfyfnWvXUSx4 +wfyEoWkZJHL4f6c9lPqvnBKWudRNdtI6FnF4MNKSzdQUp9uGQY89e9LRGtNcp+sl7 YKPdWFN1cutTNkX4dHAcGbDvUZjhgaRkBhQl6NXo= Authentication-Results: mail-nwsmtp-smtp-production-main-91.iva.yp-c.yandex.net; dkim=pass header.i=@yandex.ru From: Stas Sergeev To: libc-alpha@sourceware.org Cc: Stas Sergeev Subject: [PATCH 07/14] split out _dl_finalize_segments() Date: Thu, 18 May 2023 13:28:47 +0500 Message-Id: <20230518082854.3903342-8-stsp2@yandex.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230518082854.3903342-1-stsp2@yandex.ru> References: <20230518082854.3903342-1-stsp2@yandex.ru> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This is needed so that further patches can move that to the relocation code. The test-suite was run on x86_64/64 and showed no regressions. Signed-off-by: Stas Sergeev --- elf/dl-load.c | 3 +++ elf/dl-load.h | 5 +++++ elf/dl-map-segments.h | 36 +++++++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index 4007a4aae3..28e582fbdf 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1263,6 +1263,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, l->l_map_start = l->l_map_end = 0; goto lose; } + errstring = _dl_finalize_segments (l, type, loadcmds, nloadcmds); + if (__glibc_unlikely (errstring != NULL)) + goto lose; } if (l->l_ld != 0) diff --git a/elf/dl-load.h b/elf/dl-load.h index 029181e8c8..61f5c4fadf 100644 --- a/elf/dl-load.h +++ b/elf/dl-load.h @@ -120,6 +120,11 @@ static const char *_dl_map_segments (struct link_map *l, int fd, const size_t maplength, struct link_map *loader); +static const char *_dl_finalize_segments (struct link_map *l, + int type, + const struct loadcmd loadcmds[], + size_t nloadcmds); + /* All the error message strings _dl_map_segments might return are listed here so that different implementations in different sysdeps dl-map-segments.h files all use consistent strings that are diff --git a/elf/dl-map-segments.h b/elf/dl-map-segments.h index 080199b76e..da6b762bca 100644 --- a/elf/dl-map-segments.h +++ b/elf/dl-map-segments.h @@ -123,12 +123,10 @@ _dl_map_segments (struct link_map *l, int fd, while (c < &loadcmds[nloadcmds]) { - ElfW(Addr) hole_start, hole_size; - if (c->mapend > c->mapstart /* Map the segment contents from the file. */ && (__mmap ((void *) (l->l_addr + c->mapstart), - c->mapend - c->mapstart, c->prot, + c->mapend - c->mapstart, PROT_READ | PROT_WRITE, MAP_FIXED|MAP_COPY|MAP_FILE, fd, c->mapoff) == MAP_FAILED)) @@ -136,6 +134,34 @@ _dl_map_segments (struct link_map *l, int fd, _dl_postprocess_loadcmd (l, header, c); + ++c; + } + + /* Notify ELF_PREFERRED_ADDRESS that we have to load this one + fixed. */ + ELF_FIXED_ADDRESS (loader, c->mapstart); + + return NULL; +} + +static __always_inline const char * +_dl_finalize_segments (struct link_map *l, int type, + const struct loadcmd loadcmds[], size_t nloadcmds) +{ + const struct loadcmd *c = loadcmds; + + while (c < &loadcmds[nloadcmds]) + { + ElfW(Addr) hole_start, hole_size; + + if (c->mapend > c->mapstart) + { + if (__mprotect ((void *) (l->l_addr + c->mapstart), + c->mapend - c->mapstart, c->prot) < 0) + return DL_MAP_SEGMENTS_ERROR_MPROTECT; + + } + if (c->allocend > c->dataend) { /* Extra zero pages should appear at the end of this segment, @@ -208,9 +234,5 @@ _dl_map_segments (struct link_map *l, int fd, ++c; } - /* Notify ELF_PREFERRED_ADDRESS that we have to load this one - fixed. */ - ELF_FIXED_ADDRESS (loader, c->mapstart); - return NULL; }