This is an archive of the discontinued LLVM Phabricator instance.

[LLD][ELF] Fix ARM and Thumb V7PILongThunk overflow behavior.
ClosedPublic

Authored by peter.smith on Jan 7 2019, 8:46 AM.

Details

Summary

When the range between the source and target of a V7PILongThunk exceeded an int32 we would trigger a relocation out of range error for the R_ARM_MOVT_PREL or R_ARM_THM_MOVT_PREL relocation. This case can happen when linking the linux kernel as it is loaded above 0xf0000000.

There are two parts to the fix.

  • Remove the overflow check for R_ARM_MOVT_PREL or R_ARM_THM_MOVT_PREL. The ELF for the ARM Architecture document defines these relocations as having no overflow checking so the overflow check was spurious anyway.
  • Use int64_t for the offset calculation, in line with similar thunks so that PC + (S - P) < 32-bits. This results in less surprising disassembly.

References: ELF for the ARM Architecture table 4-11 Static ARM instruction relocations
http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044f/IHI0044F_aaelf.pdf

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

peter.smith created this revision.Jan 7 2019, 8:46 AM
grimar accepted this revision.EditedJan 9 2019, 12:42 AM

This LGTM. Minor nits.

Remove the overflow check for R_ARM_MOVT_PREL or R_ARM_THM_MOVT_PREL. The ELF for the ARM Architecture document defines these relocations as having no overflow checking so the overflow check was spurious anyway.

FTR spec says: "In §4.6.1.4 Table 4-11, removed incorrect overflow check on R_ARM_MOVT_ABS,
R_ARM_MOVT_PREL and R_ARM_MOVT_BREL."

I guess that could be a reason why overflow check was implemented in LLD initially. Anyways this patch seems to do what the latest spec says.

test/ELF/arm-extreme-range-pi-thunk.s
43

Please add a few spaces. Before the 8 here and in the expressions below. For consistency.

55

+ 8

69

+ 4

81

= 0xbits32(0x100000131), 0x131 =

This revision is now accepted and ready to land.Jan 9 2019, 12:42 AM

Thanks very much for the review. I'll wait a day before committing (I don't think that this is controversial) and will make the suggested changes in the test there.

ruiu accepted this revision.Jan 9 2019, 3:29 PM

LGTM

test/ELF/arm-extreme-range-pi-thunk.s
4–8

nit: I prefer to use multiple echo commands instead of using line concatenation, because with line concatenation all lines are concatenated to a single very long line, and if something goes wrong, this test would print out a very long line to the terminal.

Thus, I usually do something like this:

echo "SECTIONS {" > %t.script
echo "  . = SIZEOF_HEADERS;" >> %t.script
...
This revision was automatically updated to reflect the committed changes.

Thanks, I've updated the test to use multiple echos. I also specified a load address of .text_high to force the generation of a second program header. A single one was generating a close to 4Gb file.