From patchwork Thu May 16 15:41:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Georg-Johann Lay X-Patchwork-Id: 90294 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 1BFEF384AB5A for ; Thu, 16 May 2024 15:42:05 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mo4-p00-ob.smtp.rzone.de (mo4-p00-ob.smtp.rzone.de [85.215.255.23]) by sourceware.org (Postfix) with ESMTPS id 4C5E43858D20 for ; Thu, 16 May 2024 15:41:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C5E43858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gjlay.de Authentication-Results: sourceware.org; spf=none smtp.mailfrom=gjlay.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4C5E43858D20 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=85.215.255.23 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1715874096; cv=pass; b=Nk1DMuaLiculu7t+NfuTzfNgdChvVJeXnnViK+tWLArfVFQmcFKIz3n0GyLUT0/2XpiKZAMsyVWoO0/kTreuctSUkfnr/TIpT2XTHgqU3izUZTiEWrW0icN5dZtuaza8smY9JuFX3/moWx3SyzoPxKgRWE2sxk/q1wa0TyVTlYo= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1715874096; c=relaxed/simple; bh=Y8yAui4LwEeQqQLdsE06r6X4Q0iK92eFD3uHcfpEAus=; h=DKIM-Signature:DKIM-Signature:Message-ID:Date:MIME-Version:From: To:Subject; b=eU/CBCCxKCzQEaYKbWjOlCiNkFvCKuTO/1S6aq2ik+fAwUvoHbvkBqqIi+Lg3lciFetOLMKD/hTJBBD8XP0VxD4Jy80jKiZ/kY28ZkLZrDYjkHkxA4ZOq/EGxUfbhRIaY24X7QWJEoGXigZR0BdnDhzfuZ5pDSEcb2PjU4oMkGk= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1715874090; cv=none; d=strato.com; s=strato-dkim-0002; b=qMpyAaReFRwvFrsbgbEZqGZM+4C83oONic0V8Oh9k0FZ7RLRFyNtU+xKph6yK/Yfqj Q+y1gQwJmK+uSxKVzihNF9pB96BONhHqE1LTMviQkPBdc2RGTH82k5I0CUBGWEppUCoI oTT+mXz4h1ryh8nDzeB7Y61RrOmR+Jv16Vwd2/+Yd6OOadeMNpD9AsJtbIQmbH3GAOyI qc1ls1aSomC0hs0NjuviYQljwzAIkm/W0xYXOwp/nBOAWAH6ij6refJpoCjXiZtak7Jr xUZoiUYudii0OcRVIIgJNPkPrEackofgto/JxBErUgQF/iTXIloHPFwsP2zXxwjPlzTT 33yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1715874090; s=strato-dkim-0002; d=strato.com; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=g/oldEkkrqIY8wgEWJrqQ+h9LgbSqhVPwXvGs8w+7pw=; b=K1p59kcxib0n5PPpCa5T/Yqi7786CsRjlKEhPWdkWIzZZGoWaB2uieSUR4bkaJNQ15 fx2hIWEQutEsWiotjIu9Vv7zS4c+sc3o7geNoswcSvS7UvOJ5VhJeRqjEvcpCLk5qEBC flUXbLeZvcwhKUtTMAI42TYTxhkvMBlWZ35huxF1XBFx/mWbFZChVt13KXJs+fZh1xbN TTEHww8kwYQMFBD7qjDIbhf2wi23PDx+8qKjSAHedGfJeozcXyEuJawUnYWcwD8q2SDy NwV3vRN+vpTMgQD4dZlBfGgTfEggXYT4Rw5z/XReV+ad+vCVPpBtMRaIEmK1FKecnmTd gS/g== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1715874090; s=strato-dkim-0002; d=gjlay.de; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=g/oldEkkrqIY8wgEWJrqQ+h9LgbSqhVPwXvGs8w+7pw=; b=i7sddAsn5rBlJPrOwzcOqLM3HesM8RQySnIsZ0yrrxiZd8n+AH/YBPGljQmn3Vv9+6 bLc4MLKbWiqBWCMWZS3XAUVAPLvYibglC8MRZs+A2jLcB9Nnsd+D5a0kAdPZSYBDGslD oflxM1tS5HkACrxjoUQhUDB3HEX+4JK3FybLlYu1Gra4MYMa0CqueXCx12Ek9Rv38mF/ zTYbYlOXvGPBFF43LwVbSyIHBQ7F1bUlQjigiglW4DQxTlWWZBUhikSXlXYCFgQSAAv5 0OlFQqS+RXQOm+y0Cw127lC9v70c8HBIAIE6fzmyFMN5Cwev78okzi87d6i8APmecLoq hD8w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1715874090; s=strato-dkim-0003; d=gjlay.de; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=g/oldEkkrqIY8wgEWJrqQ+h9LgbSqhVPwXvGs8w+7pw=; b=6UXRelT2HX9hQvo02XLXSOU7EV8P/z8oQ21gsxX7ifxZM9cI182etI24BBa+wEMyR2 PCuW3UJKin0RfEju9gDQ== X-RZG-AUTH: ":LXoWVUeid/7A29J/hMvvT3koxZnKT7Qq0xotTetVnKkbjtK7q2y9L+qNvyhO" Received: from [192.168.2.102] by smtp.strato.de (RZmta 50.5.0 DYNA|AUTH) with ESMTPSA id xcdf4404GFfUVfX (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 16 May 2024 17:41:30 +0200 (CEST) Message-ID: <7022f827-de61-483d-a39b-7bbfd8d8e36e@gjlay.de> Date: Thu, 16 May 2024 17:41:29 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Georg-Johann Lay Content-Language: en-US To: binutils@sourceware.org, Nick Clifton Subject: [patch,avr] PR31687 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP 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: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org AVR: binutils/31687 - Include .rodata size in avr-objdump -P mem-usage. AVR emulations with a .rodata output section were missing the size of that section in avr-objdump -P mem-usage. These are the emulations that keep .rodata in program memory, which can be supported by devices that see (a part of) the program memory in the RAM address space. The patch also introduces a code clean-up related to PR 27283, which was about broken binaries that lead to overflow / UB in size computation. The section size is now just truncated or saturated, alongside with a diagnostic about the truncation and *which* section length is being truncated. binutils/ PR binutils/31687 * od-elf32_avr.c (elf32_avr_get_truncated_size): New static function. (elf32_avr_get_memory_usage): Use it to get (truncated) section sizes. diff --git a/binutils/od-elf32_avr.c b/binutils/od-elf32_avr.c index d32bd4fc049..a6105c1b071 100644 --- a/binutils/od-elf32_avr.c +++ b/binutils/od-elf32_avr.c @@ -164,79 +164,62 @@ elf32_avr_get_device_info (bfd *abfd, char *description, device->name = str_table + device_name_index; } -static void -elf32_avr_get_memory_usage (bfd *abfd, - bfd_size_type *text_usage, - bfd_size_type *data_usage, - bfd_size_type *eeprom_usage) -{ - bfd_size_type avr_datasize = 0; - bfd_size_type avr_textsize = 0; - bfd_size_type avr_bsssize = 0; - bfd_size_type bootloadersize = 0; - bfd_size_type noinitsize = 0; - bfd_size_type eepromsize = 0; - bfd_size_type res; +/* Get the size of section *SECNAME, truncated to a reasonable value in + order to catch PR 27285 and dysfunctional binaries. */ + +static bfd_size_type +elf32_avr_get_truncated_size (bfd *abfd, const char *secname) +{ + /* Max size of around 1 MiB is more than any reasonable AVR will + ever be able to swallow. And it's small enough so that we won't + get overflows / UB as demonstrated in PR 27285. */ + const bfd_size_type max_size = 1000000; + bfd_size_type size = 0; asection *section; - if ((section = bfd_get_section_by_name (abfd, ".data")) != NULL) - avr_datasize = bfd_section_size (section); - if ((section = bfd_get_section_by_name (abfd, ".text")) != NULL) - avr_textsize = bfd_section_size (section); - if ((section = bfd_get_section_by_name (abfd, ".bss")) != NULL) - avr_bsssize = bfd_section_size (section); - if ((section = bfd_get_section_by_name (abfd, ".bootloader")) != NULL) - bootloadersize = bfd_section_size (section); - if ((section = bfd_get_section_by_name (abfd, ".noinit")) != NULL) - noinitsize = bfd_section_size (section); - if ((section = bfd_get_section_by_name (abfd, ".eeprom")) != NULL) - eepromsize = bfd_section_size (section); - - /* PR 27285: Check for overflow. */ - res = avr_textsize + avr_datasize; - if (res < avr_textsize || res < avr_datasize) - { - fprintf (stderr, _("Warning: textsize (%#lx) + datasize (%#lx) overflows size type\n"), - (long) avr_textsize, (long) avr_datasize); - res = (bfd_size_type) -1; - } - else + section = bfd_get_section_by_name (abfd, secname); + + if (section != NULL) { - bfd_size_type res2; - res2 = res + bootloadersize; - if (res2 < bootloadersize || res2 < res) + size = bfd_section_size (section); + if (size > INT32_MAX) { - fprintf (stderr, _("Warning: textsize (%#lx) + datasize (%#lx) + bootloadersize (%#lx) overflows size type\n"), - (long) avr_textsize, (long) avr_datasize, (long) bootloadersize); - res2 = (bfd_size_type) -1; + fprintf (stderr, _("Warning: section %s has a negative size of" + " %ld bytes, saturating to 0 bytes\n"), + secname, (long) (int32_t) size); + size = 0; } - res = res2; - } - *text_usage = res; - - res = avr_datasize + avr_bsssize; - if (res < avr_datasize || res < avr_bsssize) - { - fprintf (stderr, _("Warning: datatsize (%#lx) + bssssize (%#lx) overflows size type\n"), - (long) avr_datasize, (long) avr_bsssize); - res = (bfd_size_type) -1; - } - else - { - bfd_size_type res2; - - res2 = res + noinitsize; - if (res2 < res || res2 < noinitsize) + else if (size > max_size) { - fprintf (stderr, _("Warning: datasize (%#lx) + bsssize (%#lx) + noinitsize (%#lx) overflows size type\n"), - (long) avr_datasize, (long) avr_bsssize, (long) noinitsize); - res2 = (bfd_size_type) -1; + fprintf (stderr, _("Warning: section %s has an impossible size of" + " %lu bytes, truncating to %lu bytes\n"), + secname, (unsigned long) size, (unsigned long) max_size); + size = max_size; } - res = res2; } - *data_usage = res; + return size; +} + + +static void +elf32_avr_get_memory_usage (bfd *abfd, + bfd_size_type *text_usage, + bfd_size_type *data_usage, + bfd_size_type *eeprom_usage) +{ + bfd_size_type avr_textsize = elf32_avr_get_truncated_size (abfd, ".text"); + bfd_size_type avr_datasize = elf32_avr_get_truncated_size (abfd, ".data");; + bfd_size_type avr_bsssize = elf32_avr_get_truncated_size (abfd, ".bss"); + bfd_size_type noinitsize = elf32_avr_get_truncated_size (abfd, ".noinit"); + bfd_size_type rodatasize = elf32_avr_get_truncated_size (abfd, ".rodata"); + bfd_size_type eepromsize = elf32_avr_get_truncated_size (abfd, ".eeprom"); + bfd_size_type bootloadersize = elf32_avr_get_truncated_size (abfd, + ".bootloader"); + + *text_usage = avr_textsize + avr_datasize + rodatasize + bootloadersize; + *data_usage = avr_datasize + avr_bsssize + noinitsize; *eeprom_usage = eepromsize; } @@ -274,7 +257,7 @@ elf32_avr_dump_mem_usage (bfd *abfd) if (device.flash_size > 0) printf (" (%2.1f%% Full)", (double) text_usage / device.flash_size * 100); - printf ("\n(.text + .data + .bootloader)\n\n"); + printf ("\n(.text + .data + .rodata + .bootloader)\n\n"); /* Data size */ printf ("Data: %8" PRIu64 " bytes", (uint64_t) data_usage);