This is an archive of the discontinued LLVM Phabricator instance.

[AVR] Recognize the AVR architecture in lldb
ClosedPublic

Authored by aykevl on Jan 28 2020, 3:40 AM.

Details

Summary

This commit adds AVR support to lldb. With this change, it can load a binary and do basic things like dump a line table.

Not much else has been implemented, that should be done in later changes.


Things that work:

$ ./llvm-build.master/bin/lldb tmp/avr.elf
(lldb) target create "tmp/avr.elf"
Current executable set to '/home/ayke/src/github.com/tinygo-org/tinygo/tmp/avr.elf' (avr).
(lldb) image lookup -F main
1 match found in /home/ayke/src/github.com/tinygo-org/tinygo/tmp/avr.elf:
        Address: avr.elf[0x0080] (avr.elf.PT_LOAD[0]..text + 128)
        Summary: avr.elf`main at avr.c:10
(lldb) image dump line-table avr.c
Line table for /home/ayke/src/github.com/tinygo-org/tinygo/tmp/avr.c in `avr.elf
0x0080: /home/ayke/src/github.com/tinygo-org/tinygo/tmp/avr.c:10
0x0084: /home/ayke/src/github.com/tinygo-org/tinygo/tmp/avr.c:13
0x0088: /usr/lib/avr/include/util/delay.h:163
0x009a: /home/ayke/src/github.com/tinygo-org/tinygo/tmp/avr.c:15
0x009c: /usr/lib/avr/include/util/delay.h:163
0x00b0: /usr/lib/avr/include/util/delay.h:163

Source code (copied from the internet somewhere):

#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRC = 0xFF; //Nakes PORTC as Output
  while(1) //infinite loop
  {
    PORTC = 0xFF; //Turns ON All LEDs
    _delay_ms(1000); //1 second delay
    PORTC= 0x00; //Turns OFF All LEDs
    _delay_ms(1000); //1 second delay
  }
}

Compiled using avr-gcc (with -gdwarf-4 as it defaults to the stabs debug format in Debian):

avr-gcc -Og -gdwarf-4 -mmcu=atmega328p -o avr.elf avr.c

Things like disassembling a binary don't work yet, but I think that can be done independently by getting llvm-objdump to work with the AVR target (it currently results in an assertion failure).

Diff Detail

Event Timeline

aykevl created this revision.Jan 28 2020, 3:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2020, 3:40 AM

Thanks for the patch, and in particular for starting with a small increment instead of a giant implement-all patch.

Could you please add a simple test case that runs yaml2obj | lldb-test object-file and verifies that the avr object file is parsed properly. (The architecture is the most important part, but you might as well check the sections while you're at it.) You can look at the existing tests in test/Shell/ObjectFile for inspiration.

aykevl updated this revision to Diff 240857.Jan 28 2020, 6:22 AM

@labath Thank you for the quick review!

I have added a test per your suggestions. I'm not very familiar with lldb but this passes the tests.

It's worth noting that I haven't managed to get a line table from a binary produced by LLVM (with my own compiler frontend) and linked by avr-gcc. But I believe that is a bug in the AVR backend of LLVM as a binary built entirely by avr-gcc works just fine.

labath accepted this revision.Jan 29 2020, 1:20 AM

Thanks. This looks fine but could you also change the title of the patch please? This patch doesn't really have anything to do with remote debugging (though it's fine to mention that this is enough to make it work). What you're really just doing is ensuring the "avr" architecture is properly recognised by the ArchSpec class.

Also, do you have commit access?

This revision is now accepted and ready to land.Jan 29 2020, 1:20 AM
aykevl retitled this revision from [AVR] Basic support for remote debugging to [AVR] Recognize the AVR architecture in lldb.Jan 29 2020, 8:08 AM
aykevl edited the summary of this revision. (Show Details)

Ok, I've updated the title and the commit message (text until the separator). Does that look good?

I have commit access so I can merge this myself.

That's fine. Thanks.

This revision was automatically updated to reflect the committed changes.