[PATCH] D37615: [AVR] implement the relocation of LDI for LLD

Leslie Zhai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 8 02:35:44 PDT 2017


xiangzhai created this revision.
Herald added a subscriber: emaste.

Hi LLVM developers,

According to http://www.atmel.com/images/Atmel-0856-AVR-Instruction-Set-Manual.pdf

  1	1	1	0	K K K K	d d d d	K K K K	LDI Rd,K

basic-avr.o was generated by avr-gcc:

  $ avr-gcc -mmcu=atmega328p -o basic-avr.o -c basic-avr.s

then link with avr-ld

  $ avr-objdump -d basic-avr-avr-ld
  
  basic-avr-avr-ld:     file format elf32-avr
  
  
  Disassembly of section .text:
  
  00000000 <__ctors_end>:
     0:   0e 94 05 00     call    0xa     ; 0xa <foo>
     4:   1a e0           ldi     r17, 0x0A       ; 10
     6:   20 e0           ldi     r18, 0x00       ; 0
     8:   3a e0           ldi     r19, 0x0A       ; 10
  
  0000000a <foo>:
     a:   0c 94 05 00     jmp     0xa     ; 0xa <foo>
  
  0000000e <bar>:
     e:   0e 94 05 00     call    0xa     ; 0xa <foo>

is same with LLD:

  $ avr-objdump -d basic-avr-lld 
  
  basic-avr-lld:     file format elf32-avr
  
  
  Disassembly of section .text:
  
  00000000 <main>:
     0:   0e 94 05 00     call    0xa     ; 0xa <foo>
     4:   1a e0           ldi     r17, 0x0A       ; 10
     6:   20 e0           ldi     r18, 0x00       ; 0
     8:   3a e0           ldi     r19, 0x0A       ; 10
  
  0000000a <foo>:
     a:   0c 94 05 00     jmp     0xa     ; 0xa <foo>
  
  0000000e <bar>:
     e:   0e 94 05 00     call    0xa     ; 0xa <foo>

please review my patch, thanks a lot!

PS: do I need to override `getImplicitAddend` for `R_PC` type relocation, for example: `R_AVR_7_PCREL`?

Regards,
Leslie Zhai


Repository:
  rL LLVM

https://reviews.llvm.org/D37615

Files:
  ELF/Arch/AVR.cpp
  test/ELF/basic-avr.s


Index: test/ELF/basic-avr.s
===================================================================
--- test/ELF/basic-avr.s
+++ test/ELF/basic-avr.s
@@ -5,10 +5,20 @@
 
 main:
   call foo
+  ldi r17, lo8(foo)
+  ldi r18, hi8(bar)
+  ldi r19, foo
 foo:
   jmp foo
+bar:
+  call foo
 
 # CHECK:      main:
-# CHECK-NEXT:   0: 0e 94 02 00 <unknown>
+# CHECK-NEXT:   0: 0e 94 05 00 <unknown>
+# CHECK-NEXT:   4: 1a e0 <unknown>
+# CHECK-NEXT:   6: 20 e0 <unknown>
+# CHECK-NEXT:   8: 3a e0 <unknown>
 # CHECK:      foo:
-# CHECK-NEXT:   4: 0c 94 02 00 <unknown>
+# CHECK-NEXT:   a: 0c 94 05 00 <unknown>
+# CHECK:      bar:
+# CHECK-NEXT:   e: 0e 94 05 00 <unknown>
Index: ELF/Arch/AVR.cpp
===================================================================
--- ELF/Arch/AVR.cpp
+++ ELF/Arch/AVR.cpp
@@ -35,6 +35,7 @@
 
 using namespace llvm;
 using namespace llvm::object;
+using namespace llvm::support;
 using namespace llvm::support::endian;
 using namespace llvm::ELF;
 using namespace lld;
@@ -52,6 +53,9 @@
 RelExpr AVR::getRelExpr(uint32_t Type, const SymbolBody &S,
                         const InputFile &File, const uint8_t *Loc) const {
   switch (Type) {
+  case R_AVR_HI8_LDI:
+  case R_AVR_LO8_LDI:
+  case R_AVR_LDI:
   case R_AVR_CALL:
     return R_ABS;
   default:
@@ -62,15 +66,22 @@
 
 void AVR::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
   switch (Type) {
+  case R_AVR_HI8_LDI:
+    break;
+  case R_AVR_LO8_LDI:
+  case R_AVR_LDI: {
+    write<uint8_t, little>(Loc, read<uint8_t, little>(Loc) + Val);
+    break;
+  }
   case R_AVR_CALL: {
     uint16_t Hi = Val >> 17;
     uint16_t Lo = Val >> 1;
     write16le(Loc, read16le(Loc) | ((Hi >> 1) << 4) | (Hi & 1));
     write16le(Loc + 2, Lo);
     break;
   }
   default:
-    error(getErrorLocation(Loc) + "unrecognized reloc " + toString(Type));
+    error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37615.114320.patch
Type: text/x-patch
Size: 1922 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170908/4177beca/attachment.bin>


More information about the llvm-commits mailing list