[lld] r247745 - [elf2] Add error checking for the R_X86_64_32 relocation.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 17:19:00 PDT 2015


On Tue, Sep 15, 2015 at 4:36 PM, Michael J. Spencer via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: mspencer
> Date: Tue Sep 15 18:36:30 2015
> New Revision: 247745
>
> URL: http://llvm.org/viewvc/llvm-project?rev=247745&view=rev
> Log:
> [elf2] Add error checking for the R_X86_64_32 relocation.
>
> Added:
>     lld/trunk/test/elf2/relocation-errors.s
> Modified:
>     lld/trunk/ELF/Writer.cpp
>     lld/trunk/test/elf2/Inputs/abs.s
>
> Modified: lld/trunk/ELF/Writer.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247745&r1=247744&r2=247745&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Writer.cpp (original)
> +++ lld/trunk/ELF/Writer.cpp Tue Sep 15 18:36:30 2015
> @@ -14,6 +14,7 @@
>  #include "Symbols.h"
>  #include "SymbolTable.h"
>
> +#include "llvm/ADT/APInt.h"
>  #include "llvm/ADT/DenseMap.h"
>  #include "llvm/ADT/STLExtras.h"
>  #include "llvm/MC/StringTableBuilder.h"
> @@ -499,9 +500,16 @@ template <class ELFT> void OutputSection
>          case llvm::ELF::R_X86_64_64:
>            support::endian::write64le(Location, SymVA + RI.r_addend);
>            break;
> -        case llvm::ELF::R_X86_64_32:
> -          support::endian::write32le(Location, SymVA + RI.r_addend);
> +        case llvm::ELF::R_X86_64_32: {
> +          APInt VA(64, SymVA);
> +          APInt Addend(64, RI.r_addend, true);
> +          APInt Result64 = VA + Addend;
> +          APInt Result = Result64.trunc(32);
> +          if (Result.zext(64) != Result64)
> +            error("Relocation out of range");
> +          support::endian::write32le(Location, Result.getZExtValue());
>

We don't need the arbitrary precision integer to check for overflow. This
can be written like this.

uint64_t VA = SymVA + RI.r_addend;
if ((RI.r_addend > 0 && VA < SymVA) || (RI.r_addend < 0 && VA > SymVA) ||
VA != uint32_t(VA))
  error("Relocation out of range");
support::endian::write32le(Location, VA);

           break;
> +        }
>          default:
>            llvm::errs() << Twine("unrecognized reloc ") + Twine(Type) <<
> '\n';
>            break;
>
> Modified: lld/trunk/test/elf2/Inputs/abs.s
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/abs.s?rev=247745&r1=247744&r2=247745&view=diff
>
> ==============================================================================
> --- lld/trunk/test/elf2/Inputs/abs.s (original)
> +++ lld/trunk/test/elf2/Inputs/abs.s Tue Sep 15 18:36:30 2015
> @@ -1,2 +1,4 @@
>  .global abs
>  abs = 0x42
> +.global big
> +big = 0x1000000000
>
> Added: lld/trunk/test/elf2/relocation-errors.s
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/relocation-errors.s?rev=247745&view=auto
>
> ==============================================================================
> --- lld/trunk/test/elf2/relocation-errors.s (added)
> +++ lld/trunk/test/elf2/relocation-errors.s Tue Sep 15 18:36:30 2015
> @@ -0,0 +1,10 @@
> +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/abs.s -o
> %tabs
> +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
> +// RUN: not lld -flavor gnu2 %tabs %t -o %t2 2>&1 | FileCheck %s
> +// REQUIRES: x86
> +
> +.global _start
> +_start:
> +  movl $big, %edx
> +
> +#CHECK: Relocation out of range
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150915/82576593/attachment.html>


More information about the llvm-commits mailing list