Skip to content

Commit 8cad4dd

Browse files
committed
[MC,X86] Property report error for modifiers with incorrect size
1 parent a93e76d commit 8cad4dd

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
140140
}
141141
llvm_unreachable("unexpected relocation type!");
142142
case MCSymbolRefExpr::VK_GOTOFF:
143-
assert(Type == RT64_64);
144143
assert(!IsPCRel);
144+
if (Type != RT64_64)
145+
Ctx.reportError(Loc, "unsupported relocation type");
145146
return ELF::R_X86_64_GOTOFF64;
146147
case MCSymbolRefExpr::VK_TPOFF:
147148
assert(!IsPCRel);
@@ -229,7 +230,7 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
229230

230231
enum X86_32RelType { RT32_NONE, RT32_32, RT32_16, RT32_8 };
231232

232-
static unsigned getRelocType32(MCContext &Ctx,
233+
static unsigned getRelocType32(MCContext &Ctx, SMLoc Loc,
233234
MCSymbolRefExpr::VariantKind Modifier,
234235
X86_32RelType Type, bool IsPCRel,
235236
MCFixupKind Kind) {
@@ -252,7 +253,8 @@ static unsigned getRelocType32(MCContext &Ctx,
252253
}
253254
llvm_unreachable("unexpected relocation type!");
254255
case MCSymbolRefExpr::VK_GOT:
255-
assert(Type == RT32_32);
256+
if (Type != RT32_32)
257+
break;
256258
if (IsPCRel)
257259
return ELF::R_386_GOTPC;
258260
// 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,
264266
? ELF::R_386_GOT32X
265267
: ELF::R_386_GOT32;
266268
case MCSymbolRefExpr::VK_GOTOFF:
267-
assert(Type == RT32_32);
268269
assert(!IsPCRel);
270+
if (Type != RT32_32)
271+
break;
269272
return ELF::R_386_GOTOFF;
270273
case MCSymbolRefExpr::VK_TLSCALL:
271274
return ELF::R_386_TLS_DESC_CALL;
272275
case MCSymbolRefExpr::VK_TLSDESC:
273276
return ELF::R_386_TLS_GOTDESC;
274277
case MCSymbolRefExpr::VK_TPOFF:
275-
assert(Type == RT32_32);
278+
if (Type != RT32_32)
279+
break;
276280
assert(!IsPCRel);
277281
return ELF::R_386_TLS_LE_32;
278282
case MCSymbolRefExpr::VK_DTPOFF:
279-
assert(Type == RT32_32);
283+
if (Type != RT32_32)
284+
break;
280285
assert(!IsPCRel);
281286
return ELF::R_386_TLS_LDO_32;
282287
case MCSymbolRefExpr::VK_TLSGD:
283-
assert(Type == RT32_32);
288+
if (Type != RT32_32)
289+
break;
284290
assert(!IsPCRel);
285291
return ELF::R_386_TLS_GD;
286292
case MCSymbolRefExpr::VK_GOTTPOFF:
287-
assert(Type == RT32_32);
293+
if (Type != RT32_32)
294+
break;
288295
assert(!IsPCRel);
289296
return ELF::R_386_TLS_IE_32;
290297
case MCSymbolRefExpr::VK_PLT:
291-
assert(Type == RT32_32);
298+
if (Type != RT32_32)
299+
break;
292300
return ELF::R_386_PLT32;
293301
case MCSymbolRefExpr::VK_INDNTPOFF:
294-
assert(Type == RT32_32);
302+
if (Type != RT32_32)
303+
break;
295304
assert(!IsPCRel);
296305
return ELF::R_386_TLS_IE;
297306
case MCSymbolRefExpr::VK_NTPOFF:
298-
assert(Type == RT32_32);
307+
if (Type != RT32_32)
308+
break;
299309
assert(!IsPCRel);
300310
return ELF::R_386_TLS_LE;
301311
case MCSymbolRefExpr::VK_GOTNTPOFF:
302-
assert(Type == RT32_32);
312+
if (Type != RT32_32)
313+
break;
303314
assert(!IsPCRel);
304315
return ELF::R_386_TLS_GOTIE;
305316
case MCSymbolRefExpr::VK_TLSLDM:
306-
assert(Type == RT32_32);
317+
if (Type != RT32_32)
318+
break;
307319
assert(!IsPCRel);
308320
return ELF::R_386_TLS_LDM;
309321
}
322+
Ctx.reportError(Loc, "unsupported relocation type");
323+
return ELF::R_386_NONE;
310324
}
311325

312326
unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
@@ -329,7 +343,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
329343
break;
330344
case RT64_64:
331345
Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
332-
break;
346+
return ELF::R_386_NONE;
333347
case RT64_32:
334348
case RT64_32S:
335349
RelType = RT32_32;
@@ -341,7 +355,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
341355
RelType = RT32_8;
342356
break;
343357
}
344-
return getRelocType32(Ctx, Modifier, RelType, IsPCRel, Kind);
358+
return getRelocType32(Ctx, Fixup.getLoc(), Modifier, RelType, IsPCRel, Kind);
345359
}
346360

347361
std::unique_ptr<MCObjectTargetWriter>

llvm/test/MC/ELF/relocation-386.s

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// 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
22
// 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
3+
// 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:
34

45
// Test that we produce the correct relocation types and that the relocations
56
// correctly point to the section or the symbol.
@@ -127,6 +128,19 @@ bar2:
127128
.word foo
128129
.byte foo
129130

131+
.ifdef ERR
132+
// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
133+
.quad foo@GOT
134+
// ERR: [[#@LINE+1]]:8: error: unsupported relocation type
135+
.short foo@GOTOFF
136+
// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
137+
.dc.w foo@TPOFF
138+
// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
139+
.dc.w foo@INDNTPOFF
140+
// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
141+
.dc.w foo@NTPOFF
142+
.endif
143+
130144
.section zedsec,"awT",@progbits
131145
zed:
132146
.long 0

llvm/test/MC/ELF/relocation.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -S --sr - | FileCheck %s
2+
// 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:
23

34
// Test that we produce the correct relocation.
45

@@ -110,3 +111,8 @@ weak_sym:
110111
// CHECK-NEXT: 0x105 R_X86_64_PC32 pr23272 0x0
111112
// CHECK-NEXT: ]
112113
// CHECK-NEXT: }
114+
115+
.ifdef ERR
116+
// ERR: [[#@LINE+1]]:7: error: unsupported relocation type
117+
.long foo@gotoff
118+
.endif

0 commit comments

Comments
 (0)