Index: lld/trunk/COFF/Chunks.cpp =================================================================== --- lld/trunk/COFF/Chunks.cpp +++ lld/trunk/COFF/Chunks.cpp @@ -213,6 +213,34 @@ applyArm64Imm(Off, Imm >> Size, Size); } +static void applySecRelAdd(const SectionChunk *Sec, uint8_t *Off, + OutputSection *OS, uint64_t S, int Shift) { + if (!OS) { + if (Sec->isCodeView()) + return; + fatal("SECREL relocation cannot be applied to absolute symbols"); + } + uint64_t SecRel = S - OS->getRVA(); + SecRel >>= Shift; + if (Shift > 0 && SecRel > 0xfff) { + error("overflow in SECREL_HIGH12A relocation in section: " + + Sec->getSectionName()); + return; + } + applyArm64Imm(Off, SecRel & 0xfff, 0); +} + +static void applySecRelLdr(const SectionChunk *Sec, uint8_t *Off, + OutputSection *OS, uint64_t S) { + if (!OS) { + if (Sec->isCodeView()) + return; + fatal("SECREL relocation cannot be applied to absolute symbols"); + } + uint64_t SecRel = S - OS->getRVA(); + applyArm64Ldr(Off, SecRel & 0xfff); +} + void SectionChunk::applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S, uint64_t P) const { switch (Type) { @@ -224,6 +252,9 @@ case IMAGE_REL_ARM64_ADDR32NB: add32(Off, S); break; case IMAGE_REL_ARM64_ADDR64: add64(Off, S + Config->ImageBase); break; case IMAGE_REL_ARM64_SECREL: applySecRel(this, Off, OS, S); break; + case IMAGE_REL_ARM64_SECREL_LOW12A: applySecRelAdd(this, Off, OS, S, 0); break; + case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelAdd(this, Off, OS, S, 12); break; + case IMAGE_REL_ARM64_SECREL_LOW12L: applySecRelLdr(this, Off, OS, S); break; default: fatal("unsupported relocation type 0x" + Twine::utohexstr(Type)); } Index: lld/trunk/test/COFF/arm64-relocs-imports.test =================================================================== --- lld/trunk/test/COFF/arm64-relocs-imports.test +++ lld/trunk/test/COFF/arm64-relocs-imports.test @@ -38,12 +38,15 @@ # BEFORE: 74: 00 00 00 00 # BEFORE: 78: 01 00 00 00 # BEFORE: 7c: 01 00 00 00 +# BEFORE: 80: 00 00 00 91 add x0, x0, #0 +# BEFORE: 84: 00 00 40 91 add x0, x0, #0, lsl #12 +# BEFORE: 88: 00 00 40 f9 ldr x0, [x0] # AFTER: Disassembly of section .text: # AFTER: 140002000: fe 0f 1f f8 str x30, [sp, #-16]! # AFTER: 140002004: e0 ff ff f0 adrp x0, #-4096 # AFTER: 140002008: 00 18 00 91 add x0, x0, #6 -# AFTER: 14000200c: 1d 00 00 94 bl #116 +# AFTER: 14000200c: 20 00 00 94 bl #128 # AFTER: 140002010: 00 21 40 39 ldrb w0, [x8, #8] # AFTER: 140002014: 00 11 40 79 ldrh w0, [x8, #8] # AFTER: 140002018: 00 09 40 b9 ldr w0, [x8, #8] @@ -72,9 +75,12 @@ # AFTER: 140002074: 01 00 00 00 # AFTER: 140002078: 09 10 00 00 # AFTER: 14000207c: 09 00 00 00 -# AFTER: 140002080: 10 00 00 b0 adrp x16, #4096 -# AFTER: 140002084: 10 1e 40 f9 ldr x16, [x16, #56] -# AFTER: 140002088: 00 02 1f d6 br x16 +# AFTER: 140002080: 00 20 0e 91 add x0, x0, #904 +# AFTER: 140002084: 00 04 40 91 add x0, x0, #1, lsl #12 +# AFTER: 140002088: 00 c4 41 f9 ldr x0, [x0, #904] +# AFTER: 14000208c: 10 00 00 b0 adrp x16, #4096 +# AFTER: 140002090: 10 1e 40 f9 ldr x16, [x16, #56] +# AFTER: 140002094: 00 02 1f d6 br x16 --- !COFF header: @@ -84,7 +90,7 @@ - Name: .text Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] Alignment: 4 - SectionData: FE0F1FF80000009000080091000000940001403900014079000140B9000140F90001003900010079000100B9000100F90001403D0001407D000140BD000140FD0001C03D0001003D0001007D000100BD000100FD0001803D000540F9201A01B000FC4FF9E0031F2AFE0741F8C0035FD608000000000000000100000001000000 + SectionData: FE0F1FF80000009000080091000000940001403900014079000140B9000140F90001003900010079000100B9000100F90001403D0001407D000140BD000140FD0001C03D0001003D0001007D000100BD000100FD0001803D000540F9201A01B000FC4FF9E0031F2AFE0741F8C0035FD6080000000000000001000000010000000000009100004091000040f9 Relocations: - VirtualAddress: 4 SymbolName: .Lstr @@ -167,6 +173,15 @@ - VirtualAddress: 124 SymbolName: .Lglobal Type: IMAGE_REL_ARM64_SECREL + - VirtualAddress: 128 + SymbolName: .Lglobal5000 + Type: IMAGE_REL_ARM64_SECREL_LOW12A + - VirtualAddress: 132 + SymbolName: .Lglobal5000 + Type: IMAGE_REL_ARM64_SECREL_HIGH12A + - VirtualAddress: 136 + SymbolName: .Lglobal5000 + Type: IMAGE_REL_ARM64_SECREL_LOW12L - Name: .data Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ] Alignment: 4 @@ -240,4 +255,10 @@ SimpleType: IMAGE_SYM_TYPE_NULL ComplexType: IMAGE_SYM_DTYPE_NULL StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: .Lglobal5000 + Value: 5000 + SectionNumber: 4 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC ...