[PATCH] D46200: Mitigate relocation overflow [part 2 of 2]

Han Shen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 16 11:14:24 PDT 2018


shenhan updated this revision to Diff 147135.
shenhan edited the summary of this revision.

Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D46200

Files:
  ELF/Writer.cpp
  test/ELF/pre_init_fini_array_missing.s


Index: test/ELF/pre_init_fini_array_missing.s
===================================================================
--- test/ELF/pre_init_fini_array_missing.s
+++ test/ELF/pre_init_fini_array_missing.s
@@ -14,30 +14,27 @@
   call __fini_array_start
   call __fini_array_end
 
-// With no .init_array section the symbols resolve to 0
-// 0 - (0x201000 + 5) = -2101253
-// 0 - (0x201005 + 5) = -2101258
-// 0 - (0x20100a + 5) = -2101263
-// 0 - (0x20100f + 5) = -2101268
-// 0 - (0x201014 + 5) = -2101273
-// 0 - (0x201019 + 5) = -2101278
+// With no .init_array section the symbols resolve to .text.
+// 0x201000 - (0x201000 + 5) = -5
+// 0x201000 - (0x201005 + 5) = -10
+// ...
 
 // CHECK: Disassembly of section .text:
 // CHECK-NEXT:  _start:
-// CHECK-NEXT:   201000:    e8 fb ef df ff     callq    -2101253
-// CHECK-NEXT:   201005:    e8 f6 ef df ff     callq    -2101258
-// CHECK-NEXT:   20100a:    e8 f1 ef df ff     callq    -2101263
-// CHECK-NEXT:   20100f:    e8 ec ef df ff     callq    -2101268
-// CHECK-NEXT:   201014:    e8 e7 ef df ff     callq    -2101273
-// CHECK-NEXT:   201019:    e8 e2 ef df ff     callq    -2101278
+// CHECK-NEXT:   201000:    e8 fb ff ff ff     callq    -5
+// CHECK-NEXT:   201005:    e8 f6 ff ff ff     callq    -10
+// CHECK-NEXT:   20100a:    e8 f1 ff ff ff     callq    -15
+// CHECK-NEXT:   20100f:    e8 ec ff ff ff     callq    -20
+// CHECK-NEXT:   201014:    e8 e7 ff ff ff     callq    -25
+// CHECK-NEXT:   201019:    e8 e2 ff ff ff     callq    -30
 
 // In position-independent binaries, they resolve to the image base.
 
 // PIE:      Disassembly of section .text:
 // PIE-NEXT: _start:
-// PIE-NEXT:     1000:	e8 fb ef ff ff 	callq	-4101
-// PIE-NEXT:     1005:	e8 f6 ef ff ff 	callq	-4106
-// PIE-NEXT:     100a:	e8 f1 ef ff ff 	callq	-4111
-// PIE-NEXT:     100f:	e8 ec ef ff ff 	callq	-4116
-// PIE-NEXT:     1014:	e8 e7 ef ff ff 	callq	-4121
-// PIE-NEXT:     1019:	e8 e2 ef ff ff 	callq	-4126
+// PIE-NEXT:     1000:  e8 fb ef ff ff  callq   -4101
+// PIE-NEXT:     1005:  e8 f6 ef ff ff  callq   -4106
+// PIE-NEXT:     100a:  e8 f1 ef ff ff  callq   -4111
+// PIE-NEXT:     100f:  e8 ec ef ff ff  callq   -4116
+// PIE-NEXT:     1014:  e8 e7 ef ff ff  callq   -4121
+// PIE-NEXT:     1019:  e8 e2 ef ff ff  callq   -4126
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1701,17 +1701,21 @@
 // The linker is expected to define SECNAME_start and SECNAME_end
 // symbols for a few sections. This function defines them.
 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
-  auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
-    // These symbols resolve to the image base if the section does not exist.
-    // A special value -1 indicates end of the section.
+  // These symbols resolve to the image base or ".text" if the section
+  // does not exist. Set symbol value to ".text" mitigates the
+  // possibilities that an relocation from .text section to these
+  // symbols overflows.  A special value -1 indicates end of the
+  // section.
+  OutputSection *DefaultOutSec;
+  if (Config->Pic || !(DefaultOutSec = findSection(".text")))
+    DefaultOutSec = Out::ElfHeader;
+  auto Define = [=](StringRef Start, StringRef End, OutputSection *OS) {
     if (OS) {
       addOptionalRegular(Start, OS, 0);
       addOptionalRegular(End, OS, -1);
     } else {
-      if (Config->Pic)
-        OS = Out::ElfHeader;
-      addOptionalRegular(Start, OS, 0);
-      addOptionalRegular(End, OS, 0);
+      addOptionalRegular(Start, DefaultOutSec, 0);
+      addOptionalRegular(End, DefaultOutSec, 0);
     }
   };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46200.147135.patch
Type: text/x-patch
Size: 3693 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180516/30403051/attachment.bin>


More information about the llvm-commits mailing list