<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Sep 15, 2015 at 4:36 PM, Michael J. Spencer via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: mspencer<br>
Date: Tue Sep 15 18:36:30 2015<br>
New Revision: 247745<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=247745&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=247745&view=rev</a><br>
Log:<br>
[elf2] Add error checking for the R_X86_64_32 relocation.<br>
<br>
Added:<br>
lld/trunk/test/elf2/relocation-errors.s<br>
Modified:<br>
lld/trunk/ELF/Writer.cpp<br>
lld/trunk/test/elf2/Inputs/abs.s<br>
<br>
Modified: lld/trunk/ELF/Writer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247745&r1=247744&r2=247745&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247745&r1=247744&r2=247745&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/ELF/Writer.cpp (original)<br>
+++ lld/trunk/ELF/Writer.cpp Tue Sep 15 18:36:30 2015<br>
@@ -14,6 +14,7 @@<br>
#include "Symbols.h"<br>
#include "SymbolTable.h"<br>
<br>
+#include "llvm/ADT/APInt.h"<br>
#include "llvm/ADT/DenseMap.h"<br>
#include "llvm/ADT/STLExtras.h"<br>
#include "llvm/MC/StringTableBuilder.h"<br>
@@ -499,9 +500,16 @@ template <class ELFT> void OutputSection<br>
case llvm::ELF::R_X86_64_64:<br>
support::endian::write64le(Location, SymVA + RI.r_addend);<br>
break;<br>
- case llvm::ELF::R_X86_64_32:<br>
- support::endian::write32le(Location, SymVA + RI.r_addend);<br>
+ case llvm::ELF::R_X86_64_32: {<br>
+ APInt VA(64, SymVA);<br>
+ APInt Addend(64, RI.r_addend, true);<br>
+ APInt Result64 = VA + Addend;<br>
+ APInt Result = Result64.trunc(32);<br>
+ if (Result.zext(64) != Result64)<br>
+ error("Relocation out of range");<br>
+ support::endian::write32le(Location, Result.getZExtValue());<br></blockquote><div><br></div><div>We don't need the arbitrary precision integer to check for overflow. This can be written like this.</div><div><br></div><div>uint64_t VA = SymVA + RI.r_addend;</div><div>if ((RI.r_addend > 0 && VA < SymVA) || (RI.r_addend < 0 && VA > SymVA) || VA != uint32_t(VA))</div><div> error("Relocation out of range");</div><div>support::endian::write32le(Location, VA);</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
break;<br>
+ }<br>
default:<br>
llvm::errs() << Twine("unrecognized reloc ") + Twine(Type) << '\n';<br>
break;<br>
<br>
Modified: lld/trunk/test/elf2/Inputs/abs.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/abs.s?rev=247745&r1=247744&r2=247745&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/abs.s?rev=247745&r1=247744&r2=247745&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/test/elf2/Inputs/abs.s (original)<br>
+++ lld/trunk/test/elf2/Inputs/abs.s Tue Sep 15 18:36:30 2015<br>
@@ -1,2 +1,4 @@<br>
.global abs<br>
abs = 0x42<br>
+.global big<br>
+big = 0x1000000000<br>
<br>
Added: lld/trunk/test/elf2/relocation-errors.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/relocation-errors.s?rev=247745&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/relocation-errors.s?rev=247745&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/elf2/relocation-errors.s (added)<br>
+++ lld/trunk/test/elf2/relocation-errors.s Tue Sep 15 18:36:30 2015<br>
@@ -0,0 +1,10 @@<br>
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/abs.s -o %tabs<br>
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t<br>
+// RUN: not lld -flavor gnu2 %tabs %t -o %t2 2>&1 | FileCheck %s<br>
+// REQUIRES: x86<br>
+<br>
+.global _start<br>
+_start:<br>
+ movl $big, %edx<br>
+<br>
+#CHECK: Relocation out of range<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>