From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2044.outbound.protection.outlook.com [40.107.247.44]) by sourceware.org (Postfix) with ESMTPS id D5D493858C3A for ; Mon, 22 May 2023 06:36:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5D493858C3A Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Y3XBBOxL/jh0xHXYPsL6nClcYLCdzxlY4QUilwqSX/9/niP8v4sEhxan8cd/kYvxg67R4Tya/lTHildlWAqG7o7PEVvZN7PIC5Q6Z6PZVZAAZNICJnzdPtFvuCaPK5a5HzkP8iYspk+wci1GOEeOBkfM+UsvvllyJpANP4TwyEhklokLS27S6PLM/kp3NLmMnmvBeIxj3CXfz/UVN/WNitZ9ILvARt0LZL7Np4MW49LzeGPNHBlZ8xFmlE0LYSKhxSKzhkdQLEf0k4OjcQYVkMXs8mrYTnZIrmQMHOZ11O/7EX+om5/IXVOMbtKLLbjjALrrIVc6CN5i390uRsNe7g== 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=0vw8U8RjH5ijWlzaU+xOlM2PjzZ8SzopIwSL50E73K8=; b=jaF2/jMShBmEZyVlikZqa+DpQYPqCRkmJaAnzpO76H/W3WRSr+t6n94sZuQFjbLrZ4BCbzdp7WRpPOzwV1GQS8LyY3Mc+Ya9s0z7lBcx1xK5Fj2n+C0MrFvZrMK941BJsd34OavKcIpQSO5z5ThKzIY/5V0sYVUFadoUeabDZwR2Fi7SurHJXrfGFGFec0RRaC7ArAhXziMuSCsJkv9pgq2R1TfPml21oVDPx/di/gOAgwy/lBp1tAvIPVK5un7uhhRxlzq/sun1qGPBONaVMv8TpVElQwEJlI24ppQGW9G6kXWvy2YOdSePh8hSj4/Z+3JmuXQV60s1ZTcwE0dH7w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0vw8U8RjH5ijWlzaU+xOlM2PjzZ8SzopIwSL50E73K8=; b=FIeFzltAuaXX9iV2Y4mjPe+SWRdf2VoNa3ZDChtcMj2PHlgYAnb8jCwj3/ibTGCfPfHSVouhJHKAsIRXPODPk32z5ip7kYQK0Zmh1040Sn/UCQo1WIN5dRYNQ4PcfRuZBS0uW4EtULEKHwDltC+0Hybo1WlopFUozfxmIkckp7fkH7dHfGR3c3o7rcgXha+NRkjgcI8Hv7KOzaEFj33iy0CcgExsYktD/6t2u6xItUHDstU74BCYyrsvHeTmUigEabXQq1ZfUn1g2Qg3v4ZDoSSXVbO5nSeaiHspFSjEpuXney5PMseDW53kXQ0YUhUP8O3ON6qCrhguLhVLXyuvGg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM9PR04MB8971.eurprd04.prod.outlook.com (2603:10a6:20b:40a::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.28; Mon, 22 May 2023 06:36:15 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::e442:306f:7711:e24c]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::e442:306f:7711:e24c%5]) with mapi id 15.20.6411.027; Mon, 22 May 2023 06:36:15 +0000 Message-ID: <2f283c17-ebda-442a-568e-5a2cfaeae0bc@suse.com> Date: Mon, 22 May 2023 08:36:15 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH] pe/coff - add support for base64 encoded long section names To: Tristan Gingold References: <243D0799-E3D0-4938-A438-DD8725593F67@free.fr> <040cfba6-009c-4e57-34b9-07469191aa88@free.fr> Content-Language: en-US Cc: Nick Clifton , binutils From: Jan Beulich In-Reply-To: <040cfba6-009c-4e57-34b9-07469191aa88@free.fr> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: FR0P281CA0043.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:48::8) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VE1PR04MB6560:EE_|AM9PR04MB8971:EE_ X-MS-Office365-Filtering-Correlation-Id: d7941a48-f53e-497d-7169-08db5a8ed776 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5Y8XEBoCBCt5h3jKbweM9Um85+019zX3SqIsBUdvuRTVokoFIO2d5x73S8nckCKawFVy65Ky4b9k7k6/MLmBsEO8sclKxqU+nRggwG6JznzATnwZm32kmaXjNVn7Cv94uyJRc8ojPpRXF7UkgWF2j4oKP/lfsmLLiW/4my3b7cjQ13jEcYgtghX2axnGnzEW3X31KKWzF/4k/CSTMVFmbJGsyQMipSkfNIPXGSxzE+R6D8jf/HJfbCDS9NvFge5uyDPH5N/lFGn8zt7ZGbGhZl0ZyY8/nEdTkv7tX05R0ofnoGGzo2ILLlSX7mEy/FK5MmUuriCBnHze92h+rs5h+hHvQ5UeGDISOTmoL+XFuYvZG6QXa/ZRALFSZq39GUeuTyDedKFIlWpi4YprXihW/q3Jzp7tE9oQCCfHQSW5EQ1s/iw6dL8goERYl3UYuMAEYr80BN6fH+CNq0yEcq70MzQ5pVxF7XMWKYW2j3lv77UkfebptaYKZfhCRiC/59OWQ1DH2A1VmNNP+tVbDzQfJvNKJGbT6CteCjltXm+6z/YdPqtl9QaKq6VPuqYZraGFDvq113FkVgbKKFq24+Wo2y4e7sMCNacnttpEPD/ChoP63KLOR6HRKH38UFFtiVNbkrz4buh7n/CJCtOBWEmIFXBdBU1dSowGafl9vdFcHjIv+Q8KHUGGYmqrCeDBeUjq X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(39860400002)(136003)(396003)(346002)(376002)(366004)(451199021)(8676002)(8936002)(5660300002)(83380400001)(53546011)(186003)(6512007)(6506007)(26005)(31696002)(2616005)(86362001)(38100700002)(41300700001)(478600001)(6486002)(4326008)(6916009)(36756003)(66946007)(66556008)(66476007)(316002)(54906003)(2906002)(31686004)(45980500001)(41080700001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?ZTZSZXM5TUlnSVdHazZmck9WaURPQjZ6bEw2THpIM0kwQXJPRTRmd2ovNWk0?= =?utf-8?B?KzhpSmdqSEJYa1hqOXRFZWFCNGREaTRpM2FyMmdTSXlrbEZ1VEZHOWU3bnFz?= =?utf-8?B?bEd6VDMwcTFLVVFSZklkV1FSN3hIL0FJUHNVK1c3NlNQVjdoaitldUNVcjlK?= =?utf-8?B?WE5ZS3ppaDAzMXJJejVXYUxFOGcvUzZEbHk2TnN4cklub3FGRngreUk4NWNz?= =?utf-8?B?cWdiSnRtVnZxd3FjNHp1YWlyZkNBV2VHZGFGa0d4dE52RnF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wMXVwSU1hQUIveU14VkdmMlVvdEJZditYTTlM?= =?utf-8?Q?P84icq7Ul+O2wHhQiS4xrISvf?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: d7941a48-f53e-497d-7169-08db5a8ed776 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 May 2023 06:36:15.1963 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2Yems3ziF2MrYkqCEKftyC767rZO8L0kcoYE5JKEgCq7s84sF2Xez10Aw8n2Izbpw6pzg3N+y4mpM8Q4NHskEg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR04MB8971 X-Spam-Status: No, score=-3033.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,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 List-Id: On 21.05.2023 17:40, Tristan Gingold via Binutils wrote: > On 17/05/2023 15:30, Nick Clifton wrote: >> Is "unsigned" the right type for strindex ?  It seems to me that it might >> be possible to encode really large numbers using base64. > > Ok, I have replaced "unsigned" with "uint32_t" to clarify my choice, > although that doesn't change anything. > > Yes, you can encode large numbers with base64, but as the section name > length in PE/COFF header is 8 bytes, you can only have 6 base64 digits > (the first two bytes are "//"). So you can encode only 6*6=36 bit. > But, and more important, the string table length is limited to 2^32 bit, > as the length is written in the first four bytes. So at the end, the > index must fit on 32b otherwise it's an error. > I added a comment to explain. I wonder whether this wouldn't better be handled either by returning the full 36-bit value and then letting the caller decide (it wants to check against string table size anyway, not just 2³²), or by checking that the first digit is only 'A'...'D'. The latter would of course make the helper function less generic (if any further use appeared). I further wonder what LLVM's motivation is to zero-pad (i.e. 'A'-prefix) the encoded values, rather than - like pre-existing section name representation - nul-padding at the end. At the very least I'm inclined to suggest that we also support that obvious (I think) alternative here. Jan >> Also - it might be nice to move this loop into a stand alone function so >> that it can be used from other parts of the BFD library.  (Assuming that >> the functionality requested in PR 30444 is implemented). > > Yes, I added the decoding in a separate function. > > Thank you for the review, and here is the new patch! > > Tristan. > > 2023-05-21 Tristan Gingold > > * coffgen.c (extract_long_section_name): New function extracted > from ... > (make_a_section_from_file): ... here. Add support for base64 > long section names. > (decode_base64): New function. > > diff --git a/bfd/coffgen.c b/bfd/coffgen.c > index ac936def566..970e06bf2c0 100644 > --- a/bfd/coffgen.c > +++ b/bfd/coffgen.c > @@ -43,6 +43,69 @@ > #include "coff/internal.h" > #include "libcoff.h" > > +/* Extract a long section name at STRINDEX and copy it to the bfd objstack. > + Return NULL in case of error. */ > + > +static char * > +extract_long_section_name(bfd *abfd, unsigned long strindex) > +{ > + const char *strings; > + char *name; > + > + strings = _bfd_coff_read_string_table (abfd); > + if (strings == NULL) > + return NULL; > + if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd)) > + return NULL; > + strings += strindex; > + name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1); > + if (name == NULL) > + return NULL; > + strcpy (name, strings); > + > + return name; > +} > + > +/* Decode a base64 coded string at STR of length LEN, and write the result > + to RES. Return true on success. > + Return false in case of invalid character or overflow. */ > + > +static bool > +decode_base64 (const char *str, unsigned len, uint32_t *res) > +{ > + unsigned i; > + uint32_t val; > + > + val = 0; > + for (i = 0; i < len; i++) > + { > + char c = str[i]; > + unsigned d; > + > + if (c >= 'A' && c <= 'Z') > + d = c - 'A'; > + else if (c >= 'a' && c <= 'z') > + d = c - 'a' + 26; > + else if (c >= '0' && c <= '9') > + d = c - '0' + 52; > + else if (c == '+') > + d = 62; > + else if (c == '/') > + d = 63; > + else > + return false; > + > + /* Check for overflow. */ > + if ((val >> 26) != 0) > + return false; > + > + val = (val << 6) + d; > + } > + > + *res = val; > + return true; > +} > + > /* Take a section header read from a coff file (in HOST byte order), > and make a BFD "section" out of it. This is used by ECOFF. */ > > @@ -67,32 +130,46 @@ make_a_section_from_file (bfd *abfd, > if (bfd_coff_set_long_section_names (abfd, > bfd_coff_long_section_names (abfd)) > && hdr->s_name[0] == '/') > { > - char buf[SCNNMLEN]; > - long strindex; > - char *p; > - const char *strings; > - > /* Flag that this BFD uses long names, even though the format might > expect them to be off by default. This won't directly affect the > format of any output BFD created from this one, but the information > can be used to decide what to do. */ > bfd_coff_set_long_section_names (abfd, true); > - memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1); > - buf[SCNNMLEN - 1] = '\0'; > - strindex = strtol (buf, &p, 10); > - if (*p == '\0' && strindex >= 0) > + > + if (hdr->s_name[1] == '/') > { > - strings = _bfd_coff_read_string_table (abfd); > - if (strings == NULL) > - return false; > - if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd)) > + /* LLVM extension: the '/' is followed by another '/' and then by > + the index in the strtab encoded in base64 without NUL at the > + end. */ > + uint32_t strindex; > + > + /* Decode the index. No overflow is expected as the string table > + length is at most 2^32 - 1 (the length is written on the first > + four bytes). */ > + if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex)) > return false; > - strings += strindex; > - name = (char *) bfd_alloc (abfd, > - (bfd_size_type) strlen (strings) + 1 + 1); > + > + name = extract_long_section_name (abfd, strindex); > if (name == NULL) > return false; > - strcpy (name, strings); > + } > + else > + { > + /* PE classic long section name. The '/' is followed by the index > + in the strtab. The index is formatted as a decimal string. */ > + char buf[SCNNMLEN]; > + long strindex; > + char *p; > + > + memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1); > + buf[SCNNMLEN - 1] = '\0'; > + strindex = strtol (buf, &p, 10); > + if (*p == '\0' && strindex >= 0) > + { > + name = extract_long_section_name (abfd, strindex); > + if (name == NULL) > + return false; > + } > } > } >