Skip to content

Commit

Permalink
[MC,X86] Property report error for modifiers with incorrect size
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Sep 20, 2023
1 parent a93e76d commit 8cad4dd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
44 changes: 29 additions & 15 deletions llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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<MCObjectTargetWriter>
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/MC/ELF/relocation-386.s
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions llvm/test/MC/ELF/relocation.s
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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

0 comments on commit 8cad4dd

Please sign in to comment.