HomePhabricator

[AVR] Explicitly set the address of the data section when invoking the linker

Authored by dylanmckay on May 17 2020, 7:18 AM.

Description

[AVR] Explicitly set the address of the data section when invoking the linker

This is required to get avr-gdb correctly showing values at the right
addresses. This problem was discovered by using debug symbols in an
external program to lookup values in an AVR simulator.

Event Timeline

aykevl added a subscriber: aykevl.May 18 2020, 2:56 PM

See my inline comment.

/clang/lib/Driver/ToolChains/AVR.cpp
128

Are you sure this is right? That 0x100 offset looks like the start of the SRAM (after I/O space etc), which varies by chip. I hit something similar in TinyGo, and fixed it by passing the correct -mmcu flag to the compiler driver (see https://github.com/tinygo-org/tinygo/pull/946/commits/2381cf9b661bcfc8187332216b2c88af53ba76b7).

dylanmckay marked an inline comment as done.Jun 23 2020, 3:24 AM
dylanmckay added inline comments.
/clang/lib/Driver/ToolChains/AVR.cpp
128

Good point, I have reverted in 15b9dc46c6d2eed64dc72143172f5fb0d4049f33 and will wire this up in a follow up patch.

dylanmckay marked an inline comment as done.Jun 23 2020, 4:02 AM
dylanmckay added inline comments.
/clang/lib/Driver/ToolChains/AVR.cpp
128

Would I be right in assuming that the compiler driver you refer to is avr-gcc? At the moment, linking with Clang only has a dependency on avr-ld, not avr-gcc. Perhaps we should create expose the address tables in LLVM directly so that other frontends can reuse them?

Does avr-go rely on avr-gcc in this manner? What do you think?

aykevl added inline comments.Jun 23 2020, 3:08 PM
/clang/lib/Driver/ToolChains/AVR.cpp
128

Yes, I was referring to avr-gcc, which passes a parameter to the linker (can't remember which one right now, maybe -Tdata) to reserve the I/O space.

In TinyGo I currently use avr-gcc to link AVR binaries, passing the correct -mmcu flag (such as -mmcu=avr5 for the atmega328p). I'm intentionally not setting the exact MCU as that messes with the linker script I'm using.
I won't be using any exported I/O space offset table from LLVM as all the information that's necessary is already extracted from .atdf files, but perhaps it's useful for Rust (which has a somewhat different toolchain design). For example, this is one of the generated linker scripts:

/* Automatically generated file. DO NOT EDIT. */
/* Generated by gen-device-avr.go from ATmega328P.atdf, see http://packs.download.atmel.com/ */

__flash_size = 0x8000;
__ram_start = 0x100;
__ram_size   = 0x800;
__num_isrs   = 26;