Factor out common code from applySecRel functions.

Differential Revision: https://reviews.llvm.org/D43412

llvm-svn: 325444
This commit is contained in:
Rui Ueyama 2018-02-17 20:28:15 +00:00
parent c9bde5404a
commit 03c7f3a2d0
1 changed files with 29 additions and 24 deletions

View File

@ -51,13 +51,21 @@ static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); }
static void or32(uint8_t *P, uint32_t V) { write32le(P, read32le(P) | V); }
// Verify that given sections are appropriate targets for SECREL
// relocations. This check is relaxed because unfortunately debug
// sections have section-relative relocations against absolute symbols.
static bool checkSecRel(const SectionChunk *Sec, OutputSection *OS) {
if (OS)
return true;
if (Sec->isCodeView())
return false;
fatal("SECREL relocation cannot be applied to absolute symbols");
}
static void applySecRel(const SectionChunk *Sec, uint8_t *Off,
OutputSection *OS, uint64_t S) {
if (!OS) {
if (Sec->isCodeView())
return;
fatal("SECREL relocation cannot be applied to absolute symbols");
}
if (!checkSecRel(Sec, OS))
return;
uint64_t SecRel = S - OS->getRVA();
if (SecRel > UINT32_MAX) {
error("overflow in SECREL relocation in section: " + Sec->getSectionName());
@ -213,16 +221,18 @@ static void applyArm64Ldr(uint8_t *Off, uint64_t Imm) {
applyArm64Imm(Off, Imm >> Size, Size);
}
static void applySecRelAdd(const SectionChunk *Sec, uint8_t *Off,
OutputSection *OS, uint64_t S, int Shift) {
if (!OS) {
if (Sec->isCodeView())
return;
fatal("SECREL relocation cannot be applied to absolute symbols");
}
uint64_t SecRel = S - OS->getRVA();
SecRel >>= Shift;
if (Shift > 0 && SecRel > 0xfff) {
static void applySecRelLow12A(const SectionChunk *Sec, uint8_t *Off,
OutputSection *OS, uint64_t S) {
if (checkSecRel(Sec, OS))
applyArm64Imm(Off, (S - OS->getRVA()) & 0xfff, 0);
}
static void applySecRelHigh12A(const SectionChunk *Sec, uint8_t *Off,
OutputSection *OS, uint64_t S) {
if (!checkSecRel(Sec, OS))
return;
uint64_t SecRel = (S - OS->getRVA()) >> 12;
if (0xfff < SecRel) {
error("overflow in SECREL_HIGH12A relocation in section: " +
Sec->getSectionName());
return;
@ -232,13 +242,8 @@ static void applySecRelAdd(const SectionChunk *Sec, uint8_t *Off,
static void applySecRelLdr(const SectionChunk *Sec, uint8_t *Off,
OutputSection *OS, uint64_t S) {
if (!OS) {
if (Sec->isCodeView())
return;
fatal("SECREL relocation cannot be applied to absolute symbols");
}
uint64_t SecRel = S - OS->getRVA();
applyArm64Ldr(Off, SecRel & 0xfff);
if (checkSecRel(Sec, OS))
applyArm64Ldr(Off, (S - OS->getRVA()) & 0xfff);
}
void SectionChunk::applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS,
@ -252,8 +257,8 @@ void SectionChunk::applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS,
case IMAGE_REL_ARM64_ADDR32NB: add32(Off, S); break;
case IMAGE_REL_ARM64_ADDR64: add64(Off, S + Config->ImageBase); break;
case IMAGE_REL_ARM64_SECREL: applySecRel(this, Off, OS, S); break;
case IMAGE_REL_ARM64_SECREL_LOW12A: applySecRelAdd(this, Off, OS, S, 0); break;
case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelAdd(this, Off, OS, S, 12); break;
case IMAGE_REL_ARM64_SECREL_LOW12A: applySecRelLow12A(this, Off, OS, S); break;
case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, Off, OS, S); break;
case IMAGE_REL_ARM64_SECREL_LOW12L: applySecRelLdr(this, Off, OS, S); break;
default:
fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));