[PATCH] D114103: [ELF] Ensure output section is also not empty in addStartEndSymbols()

Andrew Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 18 11:07:40 PST 2021


andrewng updated this revision to Diff 388269.
andrewng added a comment.

Updated to address review comments.

However, I have also uncovered a situation where an empty `.preinit_array` section can be emitted and in this case, this patch would result in the value of `__preinit_array_start` and `__preinit_array_end` not matching the address of the `.preinit_array` section. I have created D114179 <https://reviews.llvm.org/D114179> as an alternative to this patch which copes with this scenario.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114103/new/

https://reviews.llvm.org/D114103

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/linkerscript/preinit-array-empty.test


Index: lld/test/ELF/linkerscript/preinit-array-empty.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/preinit-array-empty.test
@@ -0,0 +1,24 @@
+# REQUIRES: x86
+## PR52534: https://bugs.llvm.org/show_bug.cgi?id=52534
+## Link should succeed without causing an out of range relocation error.
+
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/t.s -o %t.o
+# RUN: ld.lld --script %t/t.script %t.o -o %t.out --image-base=0x80000000
+
+# RUN: llvm-readelf -s %t.out | FileCheck %s
+# CHECK:      [[#%x,ADDR:]] 0 NOTYPE  LOCAL  HIDDEN      [[#]] __preinit_array_start
+# CHECK-NEXT: [[#ADDR]]     0 NOTYPE  LOCAL  HIDDEN      [[#]] __preinit_array_end
+# CHECK-NEXT: [[#ADDR]]     0 NOTYPE  GLOBAL DEFAULT     [[#]] _start
+
+#--- t.s
+.global _start
+_start:
+ movq __preinit_array_start at GOTPCREL(%rip),%rax
+ movq __preinit_array_end at GOTPCREL(%rip),%rax
+
+#--- t.script
+SECTIONS {
+  .text : { *(.text); }
+  .preinit_array : { *(.preinit_array); }
+}
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -2276,7 +2276,7 @@
     Default = Out::elfHeader;
 
   auto define = [=](StringRef start, StringRef end, OutputSection *os) {
-    if (os) {
+    if (os && getFirstInputSection(os)) {
       addOptionalRegular(start, os, 0);
       addOptionalRegular(end, os, -1);
     } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114103.388269.patch
Type: text/x-patch
Size: 1466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211118/13eea6b9/attachment.bin>


More information about the llvm-commits mailing list