[lld] r353849 - [PPC64] Sort .toc sections accessed with small code model relocs.

Sean Fertile via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 12 07:35:50 PST 2019


Author: sfertile
Date: Tue Feb 12 07:35:49 2019
New Revision: 353849

URL: http://llvm.org/viewvc/llvm-project?rev=353849&view=rev
Log:
[PPC64] Sort .toc sections accessed with small code model relocs.

A follow up to the intial patch that unblocked linking against libgcc.
For lld we don't need to bother tracking which objects have got based small
code model relocations. This is due to the fact that the compilers on
powerpc64 use the .toc section to generate indirections to symbols (rather then
using got relocations) which keeps the got small. This makes overflowing a
small code model got relocation very unlikely.

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

Modified:
    lld/trunk/ELF/Arch/PPC64.cpp
    lld/trunk/ELF/InputFiles.h
    lld/trunk/ELF/Relocations.cpp
    lld/trunk/ELF/Target.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Arch/PPC64.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Arch/PPC64.cpp?rev=353849&r1=353848&r2=353849&view=diff
==============================================================================
--- lld/trunk/ELF/Arch/PPC64.cpp (original)
+++ lld/trunk/ELF/Arch/PPC64.cpp Tue Feb 12 07:35:49 2019
@@ -98,13 +98,9 @@ unsigned elf::getPPC64GlobalEntryToLocal
   return 0;
 }
 
-bool elf::isPPC64SmallCodeModelReloc(RelType Type) {
-  // List is not yet complete, at the very least the got based tls related
-  // relocations need to be added, and we need to determine how the section
-  // sorting interacts with the thread pointer and dynamic thread pointer
-  // relative tls relocations.
-  return Type == R_PPC64_GOT16 || Type == R_PPC64_TOC16 ||
-         Type == R_PPC64_TOC16_DS;
+bool elf::isPPC64SmallCodeModelTocReloc(RelType Type) {
+  // The only small code model relocations that access the .toc section.
+  return Type == R_PPC64_TOC16 || Type == R_PPC64_TOC16_DS;
 }
 
 namespace {

Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=353849&r1=353848&r2=353849&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Tue Feb 12 07:35:49 2019
@@ -114,14 +114,15 @@ public:
   bool JustSymbols = false;
 
   // On PPC64 we need to keep track of which files contain small code model
-  // relocations. To minimize the chance of a relocation overflow files that do
-  // contain small code model relocations should have their .toc sections sorted
-  // closer to the .got section than files that do not contain any small code
-  // model relocations. Thats because the toc-pointer is defined to point at
-  // .got + 0x8000 and the instructions used with small code model relocations
-  // support immediates in the range [-0x8000, 0x7FFC], making the addressable
-  // range relative to the toc pointer [.got, .got + 0xFFFC].
-  bool PPC64SmallCodeModelRelocs = false;
+  // relocations that access the .toc section. To minimize the chance of a
+  // relocation overflow, files that do contain said relocations should have
+  // their .toc sections sorted closer to the .got section than files that do
+  // not contain any small code model relocations. Thats because the toc-pointer
+  // is defined to point at .got + 0x8000 and the instructions used with small
+  // code model relocations support immediates in the range [-0x8000, 0x7FFC],
+  // making the addressable range relative to the toc pointer
+  // [.got, .got + 0xFFFC].
+  bool PPC64SmallCodeModelTocRelocs = false;
 
   // GroupId is used for --warn-backrefs which is an optional error
   // checking feature. All files within the same --{start,end}-group or

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=353849&r1=353848&r2=353849&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Tue Feb 12 07:35:49 2019
@@ -1000,8 +1000,16 @@ static void scanReloc(InputSectionBase &
   if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
     return;
 
-  if (Config->EMachine == EM_PPC64 && isPPC64SmallCodeModelReloc(Type))
-    Sec.File->PPC64SmallCodeModelRelocs = true;
+  // We can separate the small code model relocations into 2 categories:
+  // 1) Those that access the compiler generated .toc sections.
+  // 2) Those that access the linker allocated got entries.
+  // lld allocates got entries to symbols on demand. Since we don't try to sort
+  // the got entries in any way, we don't have to track which objects have
+  // got-based small code model relocs. The .toc sections get placed after the
+  // end of the linker allocated .got section and we do sort those so sections
+  // addressed with small code model relocations come first.
+  if (Config->EMachine == EM_PPC64 && isPPC64SmallCodeModelTocReloc(Type))
+    Sec.File->PPC64SmallCodeModelTocRelocs = true;
 
   // Strengthen or relax relocations.
   //

Modified: lld/trunk/ELF/Target.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.h?rev=353849&r1=353848&r2=353849&view=diff
==============================================================================
--- lld/trunk/ELF/Target.h (original)
+++ lld/trunk/ELF/Target.h Tue Feb 12 07:35:49 2019
@@ -176,7 +176,9 @@ static inline std::string getErrorLocati
 // to the local entry-point.
 unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t StOther);
 
-bool isPPC64SmallCodeModelReloc(RelType Type);
+// Returns true if a relocation is a small code model relocation that accesses
+// the .toc section.
+bool isPPC64SmallCodeModelTocReloc(RelType Type);
 
 uint64_t getPPC64TocBase();
 uint64_t getAArch64Page(uint64_t Expr);

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=353849&r1=353848&r2=353849&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Feb 12 07:35:49 2019
@@ -1262,8 +1262,8 @@ static void sortSection(OutputSection *S
     auto *ISD = cast<InputSectionDescription>(Sec->SectionCommands[0]);
     std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(),
                      [](const InputSection *A, const InputSection *B) -> bool {
-                       return A->File->PPC64SmallCodeModelRelocs &&
-                              !B->File->PPC64SmallCodeModelRelocs;
+                       return A->File->PPC64SmallCodeModelTocRelocs &&
+                              !B->File->PPC64SmallCodeModelTocRelocs;
                      });
     return;
   }




More information about the llvm-commits mailing list