From patchwork Sun May 5 19:30:11 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: 89506 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 A7BAC3858C41 for ; Sun, 5 May 2024 19:30:45 +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 [81.169.146.220]) by sourceware.org (Postfix) with ESMTPS id C32ED3858D28 for ; Sun, 5 May 2024 19:30:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C32ED3858D28 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 C32ED3858D28 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=81.169.146.220 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1714937418; cv=pass; b=mmlx+XePkLXt9xpubSqfYx2z6kBu9YlQUWCoLimDcpPSjnOC4yt2DqrigaJvFodKf6qFgh4+RA4iWUk+361XG5Nby7C8qMcKoMMAcsqaPM8u2uQ3sSrvUwV0KraqI1tiMHHHbGp9EyZEnHAR9PwvEK7grNnTZZLkHKIgoDO6xPo= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1714937418; c=relaxed/simple; bh=X5heOEqhRPqjK5km9veeX47Kj/hlZO4xivVlB8KyEKY=; h=DKIM-Signature:DKIM-Signature:Message-ID:Date:MIME-Version:From: To:Subject; b=auCfvjetPsPhEPp9ntidafqhJLb34a6wbKPLW9c05cz+wOU90lnrrXGtITQrg02Z8/mVuH8HBCrkoPX7fuMAdFMPXKnHiAJ9jnlWRj6rui6o8oFJJV0lxAXpHHLf14L50fPMrghqxecl3tYCxfC7HyaGuuXzLyRvLnZagFQfVEs= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1714937412; cv=none; d=strato.com; s=strato-dkim-0002; b=GH8aSktwQToxAAYG1VM4Lrf+c+jyX3TfCmlNR//UfCY3PfBoKe6rHYIZnZ0gqZYTAv Py0+gjhXxWD5kfPcCxZYyyeRW+0jVIm1jq1emZs0EkJu2VvUTLtsO67gNuVglkJsEarL zHZGjH2Cu4Til/05xl/sZVZL7ON0A3H/LNbSgXUu/b/8P04mGsLvXJbfew7J7o0r8SpM rnE4atJRw2JZZ68qxjRJDkxX6Im6ijULV0B10OM5IjxnbNF7wK6w4HBCXyDJfXDtHndx KMEfqw6Cc1BejKie6AxTOwMZluX3gK7yCduNiTIdK3BSRIBBMtvO5FlnP+AkQNJtt77i zOPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1714937412; s=strato-dkim-0002; d=strato.com; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=EAWw6z49IxRtu/zcXFsCc23tPgAS4MIIKEcZmtZdI7I=; b=tvxuFXCkKtFg1GYeLIvX3wxNZUl5z3m+QZyE5l3bjcufkInNvBuUa3Mv7bB3JX1vxw eSceewLFuxG/kpE/SyZDJTjo5GdgV0SApv0kz+HYJ6N4yIFuZ4a1XS7DAXBqfAeXr3zR pWhapSe2oc1Lo6evdgt6tZbBRnNLNGHNCg+9dtTDv//eaWshbIdmuXlVa3xlBeui+HHp 1lfkHAR2kGkSoa/wdHmCJkh4P0TJUiANDFnT8BqO9uxVfxZaKK+Y2K2ECZ7ePp3P3YJ9 VasP63uhMhnwooWEyqGOH5i5jSxkQ1ndK4youO+y/raCo1ClCT7dnhlacy4vcrPwKJK3 3b2A== 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=1714937412; s=strato-dkim-0002; d=gjlay.de; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=EAWw6z49IxRtu/zcXFsCc23tPgAS4MIIKEcZmtZdI7I=; b=kIQjZbL1EA+SfCmpir1Qem58S/1V71vv3pTp4NB0kW6lcr1iUVgAAgRuadvjS3qZv5 8hO/u91kH7jIRzurgjUVI3Myl+pY5rNr47qLfpHqRPCjgU9dgoRvRrpj/lFXjdc44CXd 0aDKwWp349qNLwB7L4sbHj14EZQkELqyAv7zNhop11FYAOLerY3OzvqxAdKYKPZZUm2s ctMkcRgEsm768ccfoodZ3WfcCgNOgVEg2PuSZCp96A5AN3SPUYPnuJLKZpSuE25PlFx2 le5n/svUomXplyIC/p8Bk9hpt1SQEiRl1k2PRcYEqPrZChc0k8dJg9iEcTPQGef7urTw ia4w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1714937412; s=strato-dkim-0003; d=gjlay.de; h=Subject:To:From:Date:Message-ID:Cc:Date:From:Subject:Sender; bh=EAWw6z49IxRtu/zcXFsCc23tPgAS4MIIKEcZmtZdI7I=; b=ngyW1JO97crFAEK47gCc+RejUVDYKyqZ9TcQ76pSNNCM5/yjNzZq91HAUNWtzTHqcM l/Mom9E9+1fki/WHcwBg== X-RZG-AUTH: ":LXoWVUeid/7A29J/hMvvT3koxZnKT7Qq0xotTetVnKkbgtK4q2y4KBIJzcU=" Received: from [192.168.2.102] by smtp.strato.de (RZmta 50.5.0 DYNA|AUTH) with ESMTPSA id xcdf44045JUBhea (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Sun, 5 May 2024 21:30:11 +0200 (CEST) Message-ID: <39c6854c-ccc8-492e-86fd-57f01edb451c@gjlay.de> Date: Sun, 5 May 2024 21:30:11 +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] Fix PR31687: Account for .rodata size in avr-objdump -P mem-usage X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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/31684 - 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. This 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, alongside with a diagnostic about the truncation and *which* section length is being truncated. binutils/ PR binutils/31684 * 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..1e7a8b49935 100644 --- a/binutils/od-elf32_avr.c +++ b/binutils/od-elf32_avr.c @@ -164,79 +164,55 @@ 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 > max_size) { - 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 an impossible size of" + " %lu bytes, truncating to %lu bytes\n"), + secname, (unsigned long) size, (unsigned long) max_size); + size = max_size; } - 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; + return size; +} - res2 = res + noinitsize; - if (res2 < res || res2 < noinitsize) - { - 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; - } - res = res2; - } - *data_usage = res; +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 +250,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);