Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address 0x801468 of .elf section '.data' is not within region 'data' #398

Closed
xphoniex opened this issue Mar 2, 2023 · 14 comments
Closed

Comments

@xphoniex
Copy link

xphoniex commented Mar 2, 2023

I have a main that creates a struct using a 3rd crate, e.g. other_crate::new() then immediately calls panic!("") with nothing else on stack.

I have copied the panic_handler from this example. It compiles but then I get:

  = note: /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x801468 of /project/target/avr-atmega328p/release/deps/lib-59d76aad33ce9344.elf section `.data' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x801469 of /project/target/avr-atmega328p/release/deps/lib-59d76aad33ce9344.elf section `.bss' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x801468 of /project/target/avr-atmega328p/release/deps/lib-59d76aad33ce9344.elf section `.data' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x801469 of /project/target/avr-atmega328p/release/deps/lib-59d76aad33ce9344.elf section `.bss' is not within region `data'
          collect2: error: ld returned 1 exit status

something doesn't fit in the ram? or in the static area? or do I need to supply a linker parameter?

@xphoniex xphoniex changed the title address 0x801468 of .elf section .data' is not within region data' address 0x801468 of .elf section '.data' is not within region 'data' Mar 2, 2023
@Rahix
Copy link
Owner

Rahix commented Mar 3, 2023

Hi, what chip are you compiling for?

@xphoniex
Copy link
Author

xphoniex commented Mar 3, 2023

arduino uno, atmega328p

@xphoniex
Copy link
Author

xphoniex commented Mar 3, 2023

btw, I'm also getting unstable behavior, e.g.:

  • compiling with opt-level = "s" shows serial output as
5.1
��00 = 79be667ef9dcbbac55a

and compiling with opt-level = "z" returns the correct output:

5.1
public-key = 79be667ef9dcbbac55

also, declaring extra stuff on stack causes weird/unexpected behavior. in my code, if I add

let var_i_wont_use = num::new();

my program won't return the same output, depending on the variation, sometimes it's garbage output and sometimes it's program re-starting over and over. (I'm testing in qemu)


also I can't debug with avr-gdb -ex 'target remote :1234' ... either. I have zero idea how to debug things. can't use panic and can't use a debugger.

@Rahix
Copy link
Owner

Rahix commented Mar 5, 2023

Without knowing more, this looks a lot like you are using way too much memory and thus corrupting your stack and data in cases where it does compile and overflowing available program flash when it doesn't compile.

Maybe you are using a crate in your project which needs way too much space?

@xphoniex
Copy link
Author

xphoniex commented Mar 5, 2023

Yeah, it looks like it. I think I'm overflowing stack, however original issue still remains, I can't add panic when creating a struct on stack which only takes ~200 bytes. If I check avr-size, flash size is ~10K and data size (which is in ram?) is 500 bytes.

Unless adding a panic_handler takes 1000+ bytes, I don't see how that should not work.

@Rahix
Copy link
Owner

Rahix commented Mar 5, 2023

Can you maybe prepare a minimal reproducer project? It's easier to troubleshoot this when I can also reproduce the exact error.

Unless adding a panic_handler takes 1000+ bytes

This may happen when your panicking with a message. In such situations, a lot of the rust standard library formatting code is pulled in which is huge. But right now it is hard for me to tell whether this is what is happening in your case...

@xphoniex
Copy link
Author

xphoniex commented Mar 6, 2023

I moved a lot of stuff around to optimize and now I can't re-produce it. I guess struct having less impl made it possible to compile.

Still, I'm very close to the limit, already using 2040 bytes of ram!

I've tried other targets just to have more ram, such as:

  • avr-atmega1280.json
  • avr-atmega1284.json
  • avr-atmega2560.json

But I can't run any of them in qemu-system-avr, usually something like CPU 5.1 is not supported but CPU 5 is. Any idea how to fix it?

How do your normally debug an avr program?

@Rahix
Copy link
Owner

Rahix commented Mar 6, 2023

simavr may have more target support than qemu but I'm not 100% sure.

How do your normally debug an avr program?

To be perfectly honest, I usually don't bother with a real debugger. Due to LTO, the debug info is often not quite accurate and for me, this just eats more time than it saves. I prefer going with primitive "printf debugging" over the serial line.

@xphoniex
Copy link
Author

xphoniex commented Mar 6, 2023

I prefer going with primitive "printf debugging" over the serial line.

I don't know how to do that. I couldn't extract the type into an static value. Can you add an example?

@Rahix
Copy link
Owner

Rahix commented Apr 8, 2023

Sorry, this issue got lost in my inbox... I assume you mean how to put the UART object into a static so it can be accessed everywhere? If yes, I pushed an example here: https://github.com/Rahix/avr-hal/blob/main/examples/arduino-uno/src/bin/uno-println.rs

@xphoniex
Copy link
Author

Sorry, this issue got lost in my inbox... I assume you mean how to put the UART object into a static so it can be accessed everywhere? If yes, I pushed an example here: https://github.com/Rahix/avr-hal/blob/main/examples/arduino-uno/src/bin/uno-println.rs

Thanks, this is what I wanted.

--

For anyone coming across this issue, this error means your compiled elf is bigger than target's flash size. You can get a more detailed message if you add --print-memory-usage in your avr-atmega328p.json file:

…
  "pre-link-args": {
    "gcc": [
      "-mmcu=atmega328p",
      "-Wl,--as-needed,--print-memory-usage",
…      
  = note: /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800e28 of /../target/avr-atmega328p/release/deps/arduino_lib-510d9ad51ede9a40.elf section `.data' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: /../target/avr-atmega328p/release/deps/arduino_lib-510d9ad51ede9a40.elf section `.data' will not fit in region `text'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800e29 of /../target/avr-atmega328p/release/deps/arduino_lib-510d9ad51ede9a40.elf section `.bss' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800e28 of /../target/avr-atmega328p/release/deps/arduino_lib-510d9ad51ede9a40.elf section `.data' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: address 0x800e29 of /../target/avr-atmega328p/release/deps/arduino_lib-510d9ad51ede9a40.elf section `.bss' is not within region `data'
          /usr/lib/gcc/avr/5.4.0/../../../avr/bin/ld: region `text' overflowed by 1956 bytes
          collect2: error: ld returned 1 exit status
          Memory region         Used Size  Region Size  %age Used
                      text:       34724 B        32 KB    105.97%
                      data:        3369 B         2 KB    164.50%
                    eeprom:          0 GB         1 KB      0.00%
                      fuse:          0 GB          3 B      0.00%
                      lock:          0 GB         1 KB      0.00%
                 signature:          0 GB         1 KB      0.00%
           user_signatures:          0 GB         1 KB      0.00%

@Rahix
Copy link
Owner

Rahix commented Apr 16, 2023

You can get a more detailed message if you add --print-memory-usage in your avr-atmega328p.json file:

That's a nice trick, maybe we should just include that by default?

@kallemooo
Copy link
Contributor

You can get a more detailed message if you add --print-memory-usage in your avr-atmega328p.json file:

That's a nice trick, maybe we should just include that by default?

Yes, as this is a common problem in embedded projects. Specially with small devices such as the AVR.
And as memory usage printout works even on linker memory overflow error it will help the developer a lot.

@Rahix
Copy link
Owner

Rahix commented Apr 16, 2023

@kallemooo, can you maybe send a PR to integrate it into our https://github.com/Rahix/avr-hal/blob/main/avr-specs/sync-from-upstream.py script (and then regenerate the specs files)? I'm super short on time right now so it'd take a while until I can sit down to take care of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants