This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Relax checks for R_386_8/R_386_16 relocations.
ClosedPublic

Authored by grimar on Mar 29 2018, 9:31 AM.

Details

Summary

This fixes PR36927.

The issue is next. Imagine we have -Ttext 0x7c and code below.

.code16
.global _start
_start:
movb $_start+0x83,%ah

So we have R_386_8 relocation and _start at 0x7C.
Addend is 0x83 == 131. We will sign extend it to 0xffffffffffffff83.

Now, 0xffffffffffffff83 + 0x7c gives us 0xFFFFFFFFFFFFFFFF.
Techically 0x83 + 0x7c == 0xFF, we do not exceed 1 byte value, but
currently LLD errors out, because we use checkUInt<8>.

Let's try to use checkInt<8> now and the following code to see if it can help (no):
main.s:
.byte foo

input.s:
.globl foo
.hidden foo
foo = 0xff

Here, foo is 0xFF. And addend is 0x0. Final value is 0x00000000000000FF.
Again, it fits one byte well, but with checkInt<8>,
we would error out it, so we can't use it.

What we want to do is to check that the result fits 1 byte well.
Patch changes the check to checkIntUInt to fix the issue.

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

grimar created this revision.Mar 29 2018, 9:31 AM
ruiu added a comment.Mar 29 2018, 1:20 PM

I believe what you are doing in this patch is correct, but the error message is inappropriate, as the range [-256, 255] is too large. As an error message, it doesn't make sense. -256 is not represenable in one byte.

ruiu added a comment.Mar 29 2018, 1:26 PM

I think what you need to do is to use checkIntUInt<8>

grimar updated this revision to Diff 140395.Mar 30 2018, 1:57 AM
  • Using checkIntUInt.

Maybe the problem is the we should have read the implicit addend as unsigned?

espindola accepted this revision.Apr 2 2018, 1:51 PM

test/ELF/i386-reloc8-reloc16-addend.s shows that we cannot just zero extend the addend. This patch is correct.

This revision is now accepted and ready to land.Apr 2 2018, 1:51 PM
grimar edited the summary of this revision. (Show Details)Apr 3 2018, 5:20 AM
This revision was automatically updated to reflect the committed changes.