From patchwork Sun Dec 19 23:04:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 49105 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 5BC27385841A for ; Sun, 19 Dec 2021 23:04:25 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id 471C73858C60 for ; Sun, 19 Dec 2021 23:04:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 471C73858C60 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x12.wildebeest.org [172.31.17.148]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id AE914302FBA8; Mon, 20 Dec 2021 00:04:16 +0100 (CET) Received: by reform (Postfix, from userid 1000) id 92FB72E8374A; Mon, 20 Dec 2021 00:04:15 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] libdwfl: Handle unaligned Ehdr in dwfl_segment_report_module Date: Mon, 20 Dec 2021 00:04:02 +0100 Message-Id: <20211219230402.991655-1-mark@klomp.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Cc: Mark Wielaard Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" The xlate functions only handle correctly aligned buffers. But they do handle src == dest. So if the source buffer isn't aligned correctly just copy it first into the destination (which is already correctly aligned). Signed-off-by: Mark Wielaard --- libdwfl/ChangeLog | 5 +++++ libdwfl/dwfl_segment_report_module.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 18ffc347..6c7e0c4a 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,8 @@ +2021-12-19 Mark Wielaard + + * dwfl_segment_report_module.c (dwfl_segment_report_module): Copy + buffer and set xlatefrom.d_buf to ehdr when buffer is not aligned. + 2021-12-19 Mark Wielaard * dwfl_segment_report_module.c (dwfl_segment_report_module): Check diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c index a6d6be85..7f756392 100644 --- a/libdwfl/dwfl_segment_report_module.c +++ b/libdwfl/dwfl_segment_report_module.c @@ -367,6 +367,20 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name, e_ident = ((const unsigned char *) buffer); ei_class = e_ident[EI_CLASS]; ei_data = e_ident[EI_DATA]; + /* buffer may be unaligned, in which case xlatetom would not work. + xlatetom does work when the in and out d_buf are equal (but not + for any other overlap). */ + size_t ehdr_align = (ei_class == ELFCLASS32 + ? __alignof__ (Elf32_Ehdr) + : __alignof__ (Elf64_Ehdr)); + if (((uintptr_t) buffer & (ehdr_align - 1)) != 0) + { + memcpy (&ehdr, buffer, + (ei_class == ELFCLASS32 + ? sizeof (Elf32_Ehdr) + : sizeof (Elf64_Ehdr))); + xlatefrom.d_buf = &ehdr; + } switch (ei_class) { case ELFCLASS32: