Index: lld/ELF/Arch/AVR.cpp =================================================================== --- lld/ELF/Arch/AVR.cpp +++ lld/ELF/Arch/AVR.cpp @@ -28,6 +28,7 @@ #include "InputFiles.h" #include "Symbols.h" #include "Target.h" +#include "Thunks.h" #include "lld/Common/ErrorHandler.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/Support/Endian.h" @@ -42,9 +43,13 @@ namespace { class AVR final : public TargetInfo { public: + AVR() { needsThunks = true; } uint32_t calcEFlags() const override; RelExpr getRelExpr(RelType type, const Symbol &s, const uint8_t *loc) const override; + bool needsThunk(RelExpr expr, RelType type, const InputFile *file, + uint64_t branchAddr, const Symbol &s, + int64_t a) const override; void relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const override; }; @@ -98,6 +103,20 @@ write16le(loc, (read16le(loc) & 0xf0f0) | (val & 0xf0) << 4 | (val & 0x0f)); } +bool AVR::needsThunk(RelExpr expr, RelType type, const InputFile *file, + uint64_t branchAddr, const Symbol &s, int64_t a) const { + switch (type) { + case R_AVR_LO8_LDI_GS: + case R_AVR_HI8_LDI_GS: + // A thunk is needed if the symbol's virtual address is out of range + // [0, 0x1fffe]. + assert((s.getVA() & 1) == 0 && "function address must be aligned to 2."); + return s.getVA() >= 0x20000; + default: + return false; + } +} + void AVR::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { switch (rel.type) { case R_AVR_8: Index: lld/ELF/Thunks.cpp =================================================================== --- lld/ELF/Thunks.cpp +++ lld/ELF/Thunks.cpp @@ -268,6 +268,15 @@ void addSymbols(ThunkSection &isec) override; }; +// AVR thunk +class AVRThunk : public Thunk { +public: + AVRThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {} + uint32_t size() override { return 4; } + void writeTo(uint8_t *buf) override; + void addSymbols(ThunkSection &isec) override; +}; + // MIPS LA25 thunk class MipsThunk final : public Thunk { public: @@ -847,6 +856,16 @@ addSymbol("$d", STT_NOTYPE, 16, isec); } +void AVRThunk::writeTo(uint8_t *buf) { + write32(buf, 0x940c); // Use the direct jump instruction. + target->relocateNoSym(buf, R_AVR_CALL, destination.getVA()); +} + +void AVRThunk::addSymbols(ThunkSection &isec) { + addSymbol(saver().save("__AVRThunk_" + destination.getName()), STT_FUNC, 0, + isec); +} + // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. void MipsThunk::writeTo(uint8_t *buf) { uint64_t s = destination.getVA(); @@ -1267,6 +1286,16 @@ fatal("unrecognized relocation type"); } +static Thunk *addThunkAVR(RelType type, Symbol &s, int64_t a) { + switch (type) { + case R_AVR_LO8_LDI_GS: + case R_AVR_HI8_LDI_GS: + return make(s, a); + default: + fatal("unrecognized relocation type " + toString(type)); + } +} + static Thunk *addThunkMips(RelType type, Symbol &s) { if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6()) return make(s); @@ -1318,6 +1347,8 @@ return addThunkAArch64(rel.type, s, a); case EM_ARM: return addThunkArm(rel.type, s, a); + case EM_AVR: + return addThunkAVR(rel.type, s, a); case EM_MIPS: return addThunkMips(rel.type, s); case EM_PPC: Index: lld/test/ELF/avr-thunk.s =================================================================== --- /dev/null +++ lld/test/ELF/avr-thunk.s @@ -0,0 +1,27 @@ +; REQUIRES: avr +; RUN: llvm-mc -filetype=obj -triple=avr -mcpu=atmega2560 %s -o %t.o +; RUN: ld.lld %t.o --defsym=a=0x1fffe --defsym=b=0x20000 -o %t +; RUN: llvm-objdump -d --print-imm-hex --mcpu=atmega2560 %t | FileCheck %s + +.section .LDI,"ax",@progbits + +; CHECK: 000110b4 <__AVRThunk_b>: +; CHECK-NEXT: jmp 0x20000 +; CHECK: 000110b8 <__init>: +; CHECK-NEXT: ldi r30, 0xff +; CHECK-NEXT: ldi r31, 0xff +; CHECK-NEXT: eicall +; CHECK-NEXT: ldi r30, 0x5a +; CHECK-NEXT: ldi r31, 0x88 +; CHECK-NEXT: eicall +; CHECK-NOT: __AVRThunk_a + +__init: +;; No thunk is needed, since the destination is in range [0, 0x1fffe]. +ldi r30, lo8_gs(a) ; R_AVR_LO8_LDI_GS +ldi r31, hi8_gs(a) ; R_AVR_HI8_LDI_GS +eicall +;; A thunk is needed, since the destination is out of range. +ldi r30, lo8_gs(b) ; R_AVR_LO8_LDI_GS +ldi r31, hi8_gs(b) ; R_AVR_HI8_LDI_GS +eicall