[PATCH] D48924: [ELF] - Prevent relocation overflow against .bss in some cases.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 4 02:27:39 PDT 2018


grimar created this revision.
grimar added a reviewer: ruiu.
Herald added subscribers: arichardson, emaste.
Herald added a reviewer: espindola.

This is https://bugs.llvm.org/show_bug.cgi?id=38037

When we add files:
https://github.com/llvm-mirror/lld/blob/master/ELF/Driver.cpp#L1267

We might add "COMMON" input sections, which are later
changes their name to ".bss" (in getOutputSectionName):
https://github.com/llvm-mirror/lld/blob/master/ELF/SymbolTable.cpp#L461
and placed to ".bss" output section.

Since we are adding the files first,
it results in such ".bss" created out of order and placed
at the start. Itself it is not a huge issue, perhaps, but the problem
appears when such "COMMON" sections (common symbols) are huge.

In PR, usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o is the first
in the command line and it has a R_X86_64_PC32 relocation against its own ".bss".
But because of LLD, which places huge input ".bss" created for user's SHN_COMMON symbol
at the beginning of the output section, relocation overflows.

The patch fixes the issue by moving all early created synthetics to the end of the input sections list.


https://reviews.llvm.org/D48924

Files:
  ELF/Driver.cpp
  test/ELF/bss-reloc-overflow.s


Index: test/ELF/bss-reloc-overflow.s
===================================================================
--- test/ELF/bss-reloc-overflow.s
+++ test/ELF/bss-reloc-overflow.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: ld.lld %t -o %t2
+
+## Previously we were not able to link this code.
+## It has R_X86_64_PC32 relocation which overflowed.
+
+.text
+.long .bss - .
+
+.bss
+.byte 0x0
+
+.type arr, at object
+.comm arr,0xffffffff,4
Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -1342,16 +1342,27 @@
   // Apply symbol renames for -wrap.
   Symtab->applySymbolWrap();
 
-  // Now that we have a complete list of input files.
-  // Beyond this point, no new files are added.
-  // Aggregate all input sections into one place.
+  // Now that we have a complete list of input files. Beyond this point, no new
+  // files are added. Aggregate all input sections into one place.
+  //
+  // At this point, the list might be not empty and contain synthetic sections
+  // created for common symbols. We want to move them to the end of input
+  // sections list. That way they will be grouped with other synthetics we will
+  // add. This helps sometimes to resolve relocations properly. For example,
+  // gcc/x86_64-linux-gnu/5.4.0/crtbegin.o has 32bit relocation against it's
+  // own .bss. If user's code has huge common symbol, it would be placed at the
+  // start of ".bss" and may cause overflow.
+  std::vector<InputSectionBase *> Commons;
+  Commons.swap(InputSections);
   for (InputFile *F : ObjectFiles)
     for (InputSectionBase *S : F->getSections())
       if (S && S != &InputSection::Discarded)
         InputSections.push_back(S);
   for (BinaryFile *F : BinaryFiles)
     for (InputSectionBase *S : F->getSections())
       InputSections.push_back(cast<InputSection>(S));
+  for (InputSectionBase *S : Commons)
+    InputSections.push_back(S);
 
   // We do not want to emit debug sections if --strip-all
   // or -strip-debug are given.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48924.154072.patch
Type: text/x-patch
Size: 2101 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180704/dd55ca6f/attachment.bin>


More information about the llvm-commits mailing list