[lld] r325444 - Factor out common code from applySecRel functions.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 17 12:28:15 PST 2018


Author: ruiu
Date: Sat Feb 17 12:28:15 2018
New Revision: 325444

URL: http://llvm.org/viewvc/llvm-project?rev=325444&view=rev
Log:
Factor out common code from applySecRel functions.

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

Modified:
    lld/trunk/COFF/Chunks.cpp

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=325444&r1=325443&r2=325444&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Sat Feb 17 12:28:15 2018
@@ -51,13 +51,21 @@ static void add64(uint8_t *P, int64_t 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,
   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 Section
 
 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
   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));




More information about the llvm-commits mailing list