From 8cad4dd00077226f9fca5d176b7ef6ed9f668008 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 19 Sep 2023 22:14:25 -0700 Subject: [PATCH] [MC,X86] Property report error for modifiers with incorrect size --- .../X86/MCTargetDesc/X86ELFObjectWriter.cpp | 44 ++++++++++++------- llvm/test/MC/ELF/relocation-386.s | 14 ++++++ llvm/test/MC/ELF/relocation.s | 6 +++ 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp index d083bf245af22..373e29bf6a835 100644 --- a/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp +++ b/llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp @@ -140,8 +140,9 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc, } llvm_unreachable("unexpected relocation type!"); case MCSymbolRefExpr::VK_GOTOFF: - assert(Type == RT64_64); assert(!IsPCRel); + if (Type != RT64_64) + Ctx.reportError(Loc, "unsupported relocation type"); return ELF::R_X86_64_GOTOFF64; case MCSymbolRefExpr::VK_TPOFF: assert(!IsPCRel); @@ -229,7 +230,7 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc, enum X86_32RelType { RT32_NONE, RT32_32, RT32_16, RT32_8 }; -static unsigned getRelocType32(MCContext &Ctx, +static unsigned getRelocType32(MCContext &Ctx, SMLoc Loc, MCSymbolRefExpr::VariantKind Modifier, X86_32RelType Type, bool IsPCRel, MCFixupKind Kind) { @@ -252,7 +253,8 @@ static unsigned getRelocType32(MCContext &Ctx, } llvm_unreachable("unexpected relocation type!"); case MCSymbolRefExpr::VK_GOT: - assert(Type == RT32_32); + if (Type != RT32_32) + break; if (IsPCRel) return ELF::R_386_GOTPC; // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we @@ -264,49 +266,61 @@ static unsigned getRelocType32(MCContext &Ctx, ? ELF::R_386_GOT32X : ELF::R_386_GOT32; case MCSymbolRefExpr::VK_GOTOFF: - assert(Type == RT32_32); assert(!IsPCRel); + if (Type != RT32_32) + break; return ELF::R_386_GOTOFF; case MCSymbolRefExpr::VK_TLSCALL: return ELF::R_386_TLS_DESC_CALL; case MCSymbolRefExpr::VK_TLSDESC: return ELF::R_386_TLS_GOTDESC; case MCSymbolRefExpr::VK_TPOFF: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_LE_32; case MCSymbolRefExpr::VK_DTPOFF: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_LDO_32; case MCSymbolRefExpr::VK_TLSGD: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_GD; case MCSymbolRefExpr::VK_GOTTPOFF: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_IE_32; case MCSymbolRefExpr::VK_PLT: - assert(Type == RT32_32); + if (Type != RT32_32) + break; return ELF::R_386_PLT32; case MCSymbolRefExpr::VK_INDNTPOFF: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_IE; case MCSymbolRefExpr::VK_NTPOFF: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_LE; case MCSymbolRefExpr::VK_GOTNTPOFF: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_GOTIE; case MCSymbolRefExpr::VK_TLSLDM: - assert(Type == RT32_32); + if (Type != RT32_32) + break; assert(!IsPCRel); return ELF::R_386_TLS_LDM; } + Ctx.reportError(Loc, "unsupported relocation type"); + return ELF::R_386_NONE; } unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, @@ -329,7 +343,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, break; case RT64_64: Ctx.reportError(Fixup.getLoc(), "unsupported relocation type"); - break; + return ELF::R_386_NONE; case RT64_32: case RT64_32S: RelType = RT32_32; @@ -341,7 +355,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, RelType = RT32_8; break; } - return getRelocType32(Ctx, Modifier, RelType, IsPCRel, Kind); + return getRelocType32(Ctx, Fixup.getLoc(), Modifier, RelType, IsPCRel, Kind); } std::unique_ptr diff --git a/llvm/test/MC/ELF/relocation-386.s b/llvm/test/MC/ELF/relocation-386.s index e49b25a25ce53..dd252f5ff74cb 100644 --- a/llvm/test/MC/ELF/relocation-386.s +++ b/llvm/test/MC/ELF/relocation-386.s @@ -1,5 +1,6 @@ // RUN: llvm-mc -filetype=obj -triple i386-pc-linux-gnu %s -relax-relocations=false -o - | llvm-readobj -r - | FileCheck %s --check-prefix=CHECK --check-prefix=I386 // RUN: llvm-mc -filetype=obj -triple i386-pc-elfiamcu %s -relax-relocations=false -o - | llvm-readobj -r - | FileCheck %s --check-prefix=CHECK --check-prefix=IAMCU +// RUN: not llvm-mc -filetype=obj -triple=i686 --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error: // Test that we produce the correct relocation types and that the relocations // correctly point to the section or the symbol. @@ -127,6 +128,19 @@ bar2: .word foo .byte foo +.ifdef ERR +// ERR: [[#@LINE+1]]:7: error: unsupported relocation type +.quad foo@GOT +// ERR: [[#@LINE+1]]:8: error: unsupported relocation type +.short foo@GOTOFF +// ERR: [[#@LINE+1]]:7: error: unsupported relocation type +.dc.w foo@TPOFF +// ERR: [[#@LINE+1]]:7: error: unsupported relocation type +.dc.w foo@INDNTPOFF +// ERR: [[#@LINE+1]]:7: error: unsupported relocation type +.dc.w foo@NTPOFF +.endif + .section zedsec,"awT",@progbits zed: .long 0 diff --git a/llvm/test/MC/ELF/relocation.s b/llvm/test/MC/ELF/relocation.s index 8802330c90b76..797e31f529b3d 100644 --- a/llvm/test/MC/ELF/relocation.s +++ b/llvm/test/MC/ELF/relocation.s @@ -1,4 +1,5 @@ // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -S --sr - | FileCheck %s +// RUN: not llvm-mc -filetype=obj -triple x86_64 --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR --implicit-check-not=error: // Test that we produce the correct relocation. @@ -110,3 +111,8 @@ weak_sym: // CHECK-NEXT: 0x105 R_X86_64_PC32 pr23272 0x0 // CHECK-NEXT: ] // CHECK-NEXT: } + +.ifdef ERR +// ERR: [[#@LINE+1]]:7: error: unsupported relocation type +.long foo@gotoff +.endif