[ELF] Consider that NOLOAD sections should be placed in a PT_LOAD segment
authorKonstantin Schwarz <konstantin.schwarz@hightec-rt.com>
Mon, 7 Jun 2021 10:40:44 +0000 (12:40 +0200)
committerKonstantin Schwarz <konstantin.schwarz@hightec-rt.com>
Wed, 16 Jun 2021 10:36:45 +0000 (12:36 +0200)
During PHDR creation, the case where an output section does not require a
PT_LOAD header but still occupies memory in the current VMA region was not handled.

If such an output section interleaves two output sections that have the same
VMA and LMA regions set, we would previously re-use the existing PT_LOAD header
for the second output section.
However, since the memory region is not contiguous, we need to start a new PT_LOAD
segment.

This fixes https://bugs.llvm.org/show_bug.cgi?id=50558

Reviewed By: MaskRay

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

lld/ELF/Writer.cpp
lld/test/ELF/linkerscript/nobits-offset.s
lld/test/ELF/linkerscript/noload.s

index 75ddd2d..167df09 100644 (file)
@@ -2296,7 +2296,7 @@ void Writer<ELFT>::addStartStopSymbols(OutputSection *sec) {
 }
 
 static bool needsPtLoad(OutputSection *sec) {
-  if (!(sec->flags & SHF_ALLOC) || sec->noload)
+  if (!(sec->flags & SHF_ALLOC))
     return false;
 
   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
index 35397ec..387eaed 100644 (file)
 
 # CHECK:      Name  Type     Address          Off     Size
 # CHECK-NEXT:       NULL     0000000000000000 000000  000000
-# CHECK-NEXT: .text PROGBITS 0000000000000000 000158  000000
-# CHECK-NEXT: .sec1 NOBITS   0000000000000000 000158  000001
-# CHECK-NEXT: .bss  NOBITS   0000000000000400 000400  000001
+# CHECK-NEXT: .text PROGBITS 0000000000000000 000190  000000
+# CHECK-NEXT: .sec1 NOBITS   0000000000000000 001000  000001
+# CHECK-NEXT: .bss  NOBITS   0000000000000400 001400  000001
 
 # CHECK:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
-# CHECK-NEXT: LOAD 0x000400 0x0000000000000400 0x0000000000000400 0x000000 0x000001 RW  0x1000
+# CHECK-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000000 0x000001 R   0x1000
+# CHECK-NEXT: LOAD 0x001400 0x0000000000000400 0x0000000000000400 0x000000 0x000001 RW  0x1000
 
-# CHECK:      00 .bss {{$}}
+# CHECK:      00 .sec1 {{$}}
+# CHECK:      01 .bss {{$}}
 
 .bss
 .p2align 10
index 20b07b2..76007e9 100644 (file)
 # CHECK-NEXT: .no_input_sec_noload NOBITS 0000000000011000 [[OFF]]           000001
 
 # CHECK:      Type Offset   VirtAddr           PhysAddr
+# CHECK-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000
 # CHECK-NEXT: LOAD 0x001000 0x0000000000020000 0x0000000000020000
 
+# CHECK:      00 .data_noload_a .data_noload_b .no_input_sec_noload {{$}}
+# CHECK:      01 .text {{$}}
+
 #--- asm
 .section .text,"ax",@progbits
   nop