Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7DF75C43334 for ; Fri, 15 Jul 2022 07:35:36 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 675FF8025C; Fri, 15 Jul 2022 09:35:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.b="268v2uKx"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CA29F8021D; Fri, 15 Jul 2022 09:35:31 +0200 (CEST) Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2058.outbound.protection.outlook.com [40.107.94.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D2B0C802C2 for ; Fri, 15 Jul 2022 09:35:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=michal.simek@amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Wo/R140EdDclcVusN5E4JwssviUxxLdpewCJc29IexlLdTMlafvCJIe3bDIEPi2+xzwyPudTxH/kxZwSCxTx5peQd4iWPTX/yZE5xTuOfXFnyzWkEMQl2N1GH3nijde5I7hO+maLng2as0kuQL7ITSZ2baWX9Vs7hxPbB+mQ4Z22gCYIrnVzxfbpL8XaS27EvpC3MeAqNQrNTmot2r2XV3MWMP63/TGaidu9fhOqS4tONI8GiBzAaeDY6NyljS0aEs/uhZ+Rfh1KI3dj27e4FuMzbYPj3FECoyznplDiy5EvUtgQeGkMjCvb09+lwtNQ6VnsBPmAEhTJ0NfN14/pKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=utQQSqBsVvJJmInGsgyMi7zGbQnMvkEAu+qPZ1ru/bU=; b=GBsOB4NAFzJpKhFcO77mzQcT1qk29pcjFDaB03C1/TQDB/oAPyi4F9U97Li7Ljbru8W/XOsZ5C44jFiJ53GxDACK4oaEMZcxVU5IiQSYAkcSf5khnkFb198znZOYygui7Wt8CUR7EvT+P7BwmugNRftr+EQWhR4VlY2BUTcj/BRVwN5H66k4AbJkzjQHh0QHC10EHwGFPUBqxFYFV7Pj0Ve8yb2U3bBPRRVa32glLlFB8xWW3qTw/sYNiPg9BbbSEID4MN61GVQxqxI0ijoQsb2/HGIA/3USUexCx9W1UYIie+b2SFrNSqo3LtpJIWNU/PbACyiWJsnwguXV9um1Gg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=sholland.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=utQQSqBsVvJJmInGsgyMi7zGbQnMvkEAu+qPZ1ru/bU=; b=268v2uKx2m2JJ4E2wQ0Xfn+uQVM98mDFB8xQWWDEmPgA0Qd+c6hQlV7CE4TblsBWOZ/u4tC2vLakKmMEGMdTuJQoVjWEMj3w3Dty04OUOwax7j4vG8gxjNPk8SVCBPznXMc+/cxSkG/qY4oWv+RwOmuwsTvoTBZcQSTQ7WU1c1c= Received: from BN7PR02CA0018.namprd02.prod.outlook.com (2603:10b6:408:20::31) by PH0PR12MB5420.namprd12.prod.outlook.com (2603:10b6:510:e8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.12; Fri, 15 Jul 2022 07:35:20 +0000 Received: from BN8NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:408:20:cafe::33) by BN7PR02CA0018.outlook.office365.com (2603:10b6:408:20::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.14 via Frontend Transport; Fri, 15 Jul 2022 07:35:20 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT016.mail.protection.outlook.com (10.13.176.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5438.12 via Frontend Transport; Fri, 15 Jul 2022 07:35:19 +0000 Received: from [10.254.241.52] (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Fri, 15 Jul 2022 02:35:18 -0500 Message-ID: Date: Fri, 15 Jul 2022 09:35:15 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH] tools: relocate-rela: Fix ELF decoding on big-endian hosts Content-Language: en-US To: Samuel Holland , References: <20220715064026.54551-1-samuel@sholland.org> From: Michal Simek In-Reply-To: <20220715064026.54551-1-samuel@sholland.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e963376f-632f-4d63-b374-08da663491f7 X-MS-TrafficTypeDiagnostic: PH0PR12MB5420:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ChOc3AGWjoqmZJc8c0GQckIPioYeln9zHCR5sQ2/xdOL6BU90AgzgqUlOUCoT3fp9ExLWUlpb7ERIq8jyEle4K9LfcEwn7UEsAeRUXO1fg8ood4uInZ0kuG8u0HI3Ks+n7KzIpqg5vGNia2iZb6MvCrwzJ2Hyo9BgThWK+yQFthYpgXXWNmwYpiPC+geSiCkxV831VijpzJAKEbgH4vf6AomoGKBvFmxp+3dJ9zD+lXo6EwzXYjWvOaBGpvf6mVGgDaEa6d1GqSHJUhPVrR93Aad+3LjE3Y1N/UqvjUo+JAj25m9vvA1sVxPc61NMsio1aZRygcAOMWAVM+SxP+Dpaoe0vBdzSmKz383peNzzQ2978GXH8f34vcVnaPVfuUtUN3iGjAzVNeh6wrxD74GVDE0deK1tzKbJlwvfGMA6FbHqA9S6aF6hQnCouJuEQaDyIlyM2t9d8/xcYMtpd8yGGUUwb3ZkBoASYKsmeDJCG6j3kJtG0H1H3nWuz/5SBNQmh3SR14N7Vx7C7dglY06MLyTmqqr3q9nPa30a/RVwEezrDVUscRgVzO0thVDyXuoDYqR6YH04zXvDs3T8eZJhEUvea5S1G+CtT1gC+NE11eeFo5UUdYpyAb0f3QiSN0+G37AEW2CqBVI1B9JKW6HH0kUmD1unHFn2kPiK5DhmxhyldT+gaxO/QsZj+4kzndza3ZZNndQj2+yGrShldxKhJYZa4pHVvCWRLHbMw4hUoHMffYqQrkqSyXfBvm4OScNmIGYe2/taiFZvPLJVuQVZRT/Lg3Jqomcm0GWRfqQAlLOKKUSRcv3rZr0FInP43tBDQMRYUOIz+frVP7681wG51WFQxHPiFXDt1HnzvIK0P5njU8slclWGNWQbaX+/YEv X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB04.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230016)(4636009)(346002)(39860400002)(396003)(376002)(136003)(40470700004)(46966006)(36840700001)(36860700001)(5660300002)(83380400001)(8676002)(40480700001)(110136005)(31686004)(36756003)(26005)(53546011)(8936002)(2616005)(186003)(16526019)(336012)(426003)(47076005)(31696002)(2906002)(86362001)(44832011)(82310400005)(6666004)(70586007)(70206006)(16576012)(316002)(82740400003)(40460700003)(81166007)(356005)(41300700001)(478600001)(43740500002)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jul 2022 07:35:19.8983 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e963376f-632f-4d63-b374-08da663491f7 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB5420 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Hi, On 7/15/22 08:40, Samuel Holland wrote: > The new ELF decoding logic assumed that the target binary has the same > endianness as the host, which broke building ARM64 firmware binaries on > big-endian machines. > > This commit fixes the ELF64 decoding to be host-endianness-neutral, and > applies the same changes to the ELF32 decoding. It does not fix the > microblaze-specific dynamic symbol decoding. > > It also corrects the functions used for byte swapping in rela_elf64() > and rela_elf32(). The result is the same, but semantically the code is > converting bytes read from a foreign-endianness file to host byte order. > > Fixes: 4c9e2d643460 ("tools: relocate-rela: Read rela start/end directly from ELF") > Fixes: a1405d9cfedb ("tools: relocate-rela: Check that relocation works only for EM_AARCH64") > Signed-off-by: Samuel Holland > --- > > tools/relocate-rela.c | 125 +++++++++++++++++++++++------------------- > 1 file changed, 68 insertions(+), 57 deletions(-) > > diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c > index 090fb1acb20c..fcf3fb201f07 100644 > --- a/tools/relocate-rela.c > +++ b/tools/relocate-rela.c > @@ -60,7 +60,9 @@ static int decode_elf64(FILE *felf, char **argv) > { > size_t size; > Elf64_Ehdr header; > - uint64_t section_header_base, section_header_size, sh_offset, sh_size; > + uint64_t section_header_base, section_header_size; > + uint64_t sh_addr, sh_offset, sh_size; > + Elf64_Half sh_index, sh_num; > Elf64_Shdr *sh_table; /* Elf symbol table */ > int ret, i, machine; > char *sh_str; > @@ -76,7 +78,7 @@ static int decode_elf64(FILE *felf, char **argv) > return 25; > } > > - machine = header.e_machine; > + machine = le16_to_cpu(header.e_machine); > debug("Machine\t%d\n", machine); > > if (machine != EM_AARCH64) { > @@ -84,9 +86,10 @@ static int decode_elf64(FILE *felf, char **argv) > return 30; > } > > - text_base = header.e_entry; > - section_header_base = header.e_shoff; > - section_header_size = header.e_shentsize * header.e_shnum; > + text_base = le64_to_cpu(header.e_entry); > + section_header_base = le64_to_cpu(header.e_shoff); > + section_header_size = le16_to_cpu(header.e_shentsize) * > + le16_to_cpu(header.e_shnum); > > sh_table = malloc(section_header_size); > if (!sh_table) { > @@ -114,9 +117,9 @@ static int decode_elf64(FILE *felf, char **argv) > return 27; > } > > - sh_size = sh_table[header.e_shstrndx].sh_size; > - debug("e_shstrndx\t0x%08x\n", header.e_shstrndx); > - debug("sh_size\t\t0x%08lx\n", sh_size); > + sh_index = le16_to_cpu(header.e_shstrndx); > + sh_size = le64_to_cpu(sh_table[sh_index].sh_size); > + debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size); > > sh_str = malloc(sh_size); > if (!sh_str) { > @@ -130,9 +133,8 @@ static int decode_elf64(FILE *felf, char **argv) > * Specifies the byte offset from the beginning of the file > * to the first byte in the section. > */ > - sh_offset = sh_table[header.e_shstrndx].sh_offset; > - > - debug("sh_offset\t0x%08x\n", header.e_shnum); > + sh_offset = le64_to_cpu(sh_table[sh_index].sh_offset); > + sh_num = le16_to_cpu(header.e_shnum); > > ret = fseek(felf, sh_offset, SEEK_SET); > if (ret) { > @@ -153,18 +155,22 @@ static int decode_elf64(FILE *felf, char **argv) > return 30; > } > > - for (i = 0; i < header.e_shnum; i++) { > - /* fprintf(stderr, "%s\n", sh_str + sh_table[i].sh_name); Debug only */ > - if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) { > + for (i = 0; i < sh_num; i++) { > + char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name); > + > + debug("%s\n", sh_name); > + > + sh_addr = le64_to_cpu(sh_table[i].sh_addr); > + sh_offset = le64_to_cpu(sh_table[i].sh_offset); > + sh_size = le64_to_cpu(sh_table[i].sh_size); nit: We could move this below sh_name checking but that's fine. > + > + if (!strcmp(".rela.dyn", sh_name)) { > debug("Found section\t\".rela_dyn\"\n"); > - debug(" at addr\t0x%08x\n", > - (unsigned int)sh_table[i].sh_addr); > - debug(" at offset\t0x%08x\n", > - (unsigned int)sh_table[i].sh_offset); > - debug(" of size\t0x%08x\n", > - (unsigned int)sh_table[i].sh_size); > - rela_start = sh_table[i].sh_addr; > - rela_end = rela_start + sh_table[i].sh_size; > + debug(" at addr\t0x%08x\n", sh_addr); > + debug(" at offset\t0x%08x\n", sh_offset); > + debug(" of size\t0x%08x\n", sh_size); > + rela_start = sh_addr; > + rela_end = rela_start + sh_size; > break; > } > } > @@ -188,7 +194,9 @@ static int decode_elf32(FILE *felf, char **argv) > { > size_t size; > Elf32_Ehdr header; > - uint64_t section_header_base, section_header_size, sh_offset, sh_size; > + uint64_t section_header_base, section_header_size; > + uint32_t sh_addr, sh_offset, sh_size; > + Elf32_Half sh_index, sh_num; > Elf32_Shdr *sh_table; /* Elf symbol table */ > int ret, i, machine; > char *sh_str; > @@ -204,7 +212,7 @@ static int decode_elf32(FILE *felf, char **argv) > return 25; > } > > - machine = header.e_machine; > + machine = le16_to_cpu(header.e_machine); > debug("Machine %d\n", machine); > > if (machine != EM_MICROBLAZE) { > @@ -212,14 +220,10 @@ static int decode_elf32(FILE *felf, char **argv) > return 30; > } > > - text_base = header.e_entry; > - section_header_base = header.e_shoff; > - > - debug("Section header base %x\n", section_header_base); > - > - section_header_size = header.e_shentsize * header.e_shnum; > - > - debug("Section header size %d\n", section_header_size); > + text_base = le32_to_cpu(header.e_entry); > + section_header_base = le32_to_cpu(header.e_shoff); > + section_header_size = le16_to_cpu(header.e_shentsize) * > + le16_to_cpu(header.e_shnum); > > sh_table = malloc(section_header_size); > if (!sh_table) { > @@ -247,8 +251,9 @@ static int decode_elf32(FILE *felf, char **argv) > return 27; > } > > - sh_size = sh_table[header.e_shstrndx].sh_size; > - debug("e_shstrndx %x, sh_size %lx\n", header.e_shstrndx, sh_size); > + sh_index = le16_to_cpu(header.e_shstrndx); > + sh_size = le32_to_cpu(sh_table[sh_index].sh_size); > + debug("e_shstrndx %x, sh_size %lx\n", sh_index, sh_size); > > sh_str = malloc(sh_size); > if (!sh_str) { > @@ -262,9 +267,8 @@ static int decode_elf32(FILE *felf, char **argv) > * Specifies the byte offset from the beginning of the file > * to the first byte in the section. > */ > - sh_offset = sh_table[header.e_shstrndx].sh_offset; > - > - debug("sh_offset %x\n", header.e_shnum); > + sh_offset = le32_to_cpu(sh_table[sh_index].sh_offset); > + sh_num = le16_to_cpu(header.e_shnum); > > ret = fseek(felf, sh_offset, SEEK_SET); > if (ret) { > @@ -277,7 +281,7 @@ static int decode_elf32(FILE *felf, char **argv) > > size = fread(sh_str, 1, sh_size, felf); > if (size != sh_size) { > - fprintf(stderr, "%s: Can't read section: %lx/%lx\n", > + fprintf(stderr, "%s: Can't read section: %lx/%x\n", > argv[0], size, sh_size); > free(sh_str); > free(sh_table); > @@ -285,22 +289,29 @@ static int decode_elf32(FILE *felf, char **argv) > return 30; > } > > - for (i = 0; i < header.e_shnum; i++) { > - debug("%s\n", sh_str + sh_table[i].sh_name); > - if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) { > + for (i = 0; i < sh_num; i++) { > + char *sh_name = sh_str + le32_to_cpu(sh_table[i].sh_name); > + > + debug("%s\n", sh_name); > + > + sh_addr = le64_to_cpu(sh_table[i].sh_addr); > + sh_offset = le64_to_cpu(sh_table[i].sh_offset); > + sh_size = le64_to_cpu(sh_table[i].sh_size); > + > + if (!strcmp(".rela.dyn", sh_name)) { > debug("Found section\t\".rela_dyn\"\n"); > - debug(" at addr\t0x%08x\n", (unsigned int)sh_table[i].sh_addr); > - debug(" at offset\t0x%08x\n", (unsigned int)sh_table[i].sh_offset); > - debug(" of size\t0x%08x\n", (unsigned int)sh_table[i].sh_size); > - rela_start = sh_table[i].sh_addr; > - rela_end = rela_start + sh_table[i].sh_size; > + debug(" at addr\t0x%08x\n", sh_addr); > + debug(" at offset\t0x%08x\n", sh_offset); > + debug(" of size\t0x%08x\n", sh_size); > + rela_start = sh_addr; > + rela_end = rela_start + sh_size; > } > - if (!strcmp(".dynsym", (sh_str + sh_table[i].sh_name))) { > + if (!strcmp(".dynsym", sh_name)) { > debug("Found section\t\".dynsym\"\n"); > - debug(" at addr\t0x%08x\n", (unsigned int)sh_table[i].sh_addr); > - debug(" at offset\t0x%08x\n", (unsigned int)sh_table[i].sh_offset); > - debug(" of size\t0x%08x\n", (unsigned int)sh_table[i].sh_size); > - dyn_start = sh_table[i].sh_addr; > + debug(" at addr\t0x%08x\n", sh_addr); > + debug(" at offset\t0x%08x\n", sh_offset); > + debug(" of size\t0x%08x\n", sh_size); > + dyn_start = sh_addr; > } > } > > @@ -386,9 +397,9 @@ static int rela_elf64(char **argv, FILE *f) > return 4; > } > > - swrela.r_offset = cpu_to_le64(rela.r_offset); > - swrela.r_info = cpu_to_le64(rela.r_info); > - swrela.r_addend = cpu_to_le64(rela.r_addend); > + swrela.r_offset = le64_to_cpu(rela.r_offset); > + swrela.r_info = le64_to_cpu(rela.r_info); > + swrela.r_addend = le64_to_cpu(rela.r_addend); > > if (!supported_rela(&swrela)) > continue; > @@ -487,9 +498,9 @@ static int rela_elf32(char **argv, FILE *f) > PRIu32 " r_addend:\t%" PRIx32 "\n", > rela.r_offset, rela.r_info, rela.r_addend); > > - swrela.r_offset = cpu_to_le32(rela.r_offset); > - swrela.r_info = cpu_to_le32(rela.r_info); > - swrela.r_addend = cpu_to_le32(rela.r_addend); > + swrela.r_offset = le32_to_cpu(rela.r_offset); > + swrela.r_info = le32_to_cpu(rela.r_info); > + swrela.r_addend = le32_to_cpu(rela.r_addend); > > debug("SWRela:\toffset:\t%" PRIx32 " r_info:\t%" > PRIu32 " r_addend:\t%" PRIx32 "\n", I have tested it on arm64 and also microblaze and it works fine. Applied. M