Bug 31687 - [avr] Output of avr-objdump -P mem-usage missing .rodata
Summary: [avr] Output of avr-objdump -P mem-usage missing .rodata
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.42
: P3 normal
Target Milestone: 2.43
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-04-30 16:46 UTC by Georg-Johann Lay
Modified: 2024-05-20 18:35 UTC (History)
0 users

See Also:
Host:
Target: avr
Build:
Last reconfirmed:


Attachments
attiny40.elf (2.59 KB, application/x-executable)
2024-05-06 08:35 UTC, Georg-Johann Lay
Details
Test case for AVR128DA32 (11.25 KB, application/x-executable)
2024-05-06 08:39 UTC, Georg-Johann Lay
Details
Proposed patch (1.37 KB, patch)
2024-05-17 09:43 UTC, Georg-Johann Lay
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Georg-Johann Lay 2024-04-30 16:46:58 UTC
As is appears, objdump deas ot consider output section .rodata.

.rodata output section is only present on emulations avrtiny, avrxmega3, avrxmega2_flmap and avrxmega4_flmap.

For example, for some avrxmega3 device with the following

$ avr-objdump -h foo.elf

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         0000001c  00802800  0000015f  00000213  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  1 .text         00000158  00000000  00000000  000000b4  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .rodata       00000007  00004158  00000158  0000020c  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .bss          00000008  0080281c  0080281c  0000022f  2**0
                  ALLOC
  4 .noinit       00000001  00802824  00802824  0000022f  2**0
                  ALLOC

Which are the following sizes in decimal:

.data   = 28
.text   = 344
.rodata = 7
.bss    = 8
.noinit = 1

The expected flash consumption is .text + .data + .rodata = 379 bytes, and RAM consumption is .data + .bss + .noinit = 37 bytes.

The report from avr-size is correct:

   text	   data	    bss	    dec	    hex	filename
    351	     28	      9	    388	    184	foo.elf

but the report from avr-objdump -P mem-usage is not:

AVR Memory Usage
----------------
Device: atmega4808

Program:     372 bytes (0.8% Full)
(.text + .data + .bootloader)

Data:         37 bytes (0.6% Full)
(.data + .bss + .noinit)

The "Program" size is missing .rodata.
Comment 1 Georg-Johann Lay 2024-05-06 08:35:29 UTC
Created attachment 15493 [details]
attiny40.elf

Test case for ATtiny40

$ avr-objdump -h attiny40.elf 

attiny40.elf:     file format elf32-avr

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00000040  00000000  00000000  00000094  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .rodata       0000000a  00004040  00000040  000000d4  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .data         00000000  00800040  0000004a  000000de  2**0
                  CONTENTS, ALLOC, LOAD, DATA

$ avr-objdump -P mem-usage attiny40.elf 

attiny40.elf:     file format elf32-avr
AVR Memory Usage
----------------
Device: attiny40

Program:      64 bytes (1.6% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)
Comment 2 Georg-Johann Lay 2024-05-06 08:39:53 UTC
Created attachment 15494 [details]
Test case for AVR128DA32

$ avr-objdump -h avr128da32.elf 

avr128da32.elf:     file format elf32-avr

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         00000000  00804000  000000d2  00000166  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  1 .text         000000d2  00000000  00000000  00000094  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .rodata       0000000a  00a08000  00018000  00000166  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA

$ avr-objdump -P mem-usage avr128da32.elf 

avr128da32.elf:     file format elf32-avr
AVR Memory Usage
----------------
Device: avr128da32

Program:     210 bytes (0.2% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)
Comment 3 Georg-Johann Lay 2024-05-17 09:43:50 UTC
Created attachment 15526 [details]
Proposed patch

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.
Comment 4 Sourceware Commits 2024-05-20 12:34:29 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e8c83191cc6fee517d2029109c61981b4092439d

commit e8c83191cc6fee517d2029109c61981b4092439d
Author: Georg-Johann Lay <avr@gjlay.de>
Date:   Mon May 20 13:33:55 2024 +0100

    Include .rodata size in avr-objdump -P mem-usage.
    
      PR 31687
Comment 5 Georg-Johann Lay 2024-05-20 18:35:22 UTC
Fixed in v2.43