| commit 1db59dc8e28819b1960dae8e7fe6d79ad4b03340 |
| Author: Fangrui Song <i@maskray.me> |
| Date: Mon Mar 28 11:05:52 2022 -0700 |
| |
| [ELF] Fix llvm_unreachable failure when COMMON is placed in SHT_PROGBITS output section |
| |
| Fix a regression in aa27bab5a1a17e9c4168a741a6298ecaa92c1ecb: COMMON in an |
| SHT_PROGBITS output section caused llvm_unreachable failure. |
| |
| diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h |
| index f0f0c4845e6f..ad948e896871 100644 |
| --- a/lld/ELF/SyntheticSections.h |
| +++ b/lld/ELF/SyntheticSections.h |
| @@ -186,9 +186,7 @@ private: |
| class BssSection final : public SyntheticSection { |
| public: |
| BssSection(StringRef name, uint64_t size, uint32_t alignment); |
| - void writeTo(uint8_t *) override { |
| - llvm_unreachable("unexpected writeTo() call for SHT_NOBITS section"); |
| - } |
| + void writeTo(uint8_t *) override {} |
| bool isNeeded() const override { return size != 0; } |
| size_t getSize() const override { return size; } |
| |
| diff --git a/lld/test/ELF/linkerscript/common.s b/lld/test/ELF/linkerscript/common.s |
| index 04b667f65912..69376011656f 100644 |
| --- a/lld/test/ELF/linkerscript/common.s |
| +++ b/lld/test/ELF/linkerscript/common.s |
| @@ -1,47 +1,44 @@ |
| # REQUIRES: x86 |
| -# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t |
| -# RUN: echo "SECTIONS { . = SIZEOF_HEADERS; .common : { *(COMMON) } }" > %t.script |
| -# RUN: ld.lld -o %t1 --script %t.script %t |
| -# RUN: llvm-readobj -S --symbols %t1 | FileCheck %s |
| - |
| -# CHECK: Section { |
| -# CHECK: Index: |
| -# CHECK: Name: .common |
| -# CHECK-NEXT: Type: SHT_NOBITS |
| -# CHECK-NEXT: Flags [ |
| -# CHECK-NEXT: SHF_ALLOC |
| -# CHECK-NEXT: SHF_WRITE |
| -# CHECK-NEXT: ] |
| -# CHECK-NEXT: Address: 0x200 |
| -# CHECK-NEXT: Offset: 0x |
| -# CHECK-NEXT: Size: 384 |
| -# CHECK-NEXT: Link: 0 |
| -# CHECK-NEXT: Info: 0 |
| -# CHECK-NEXT: AddressAlignment: 256 |
| -# CHECK-NEXT: EntrySize: 0 |
| -# CHECK-NEXT: } |
| -# CHECK: Symbol { |
| -# CHECK: Name: q1 |
| -# CHECK-NEXT: Value: 0x200 |
| -# CHECK-NEXT: Size: 128 |
| -# CHECK-NEXT: Binding: Global |
| -# CHECK-NEXT: Type: Object |
| -# CHECK-NEXT: Other: 0 |
| -# CHECK-NEXT: Section: .common |
| -# CHECK-NEXT: } |
| -# CHECK-NEXT: Symbol { |
| -# CHECK-NEXT: Name: q2 |
| -# CHECK-NEXT: Value: 0x300 |
| -# CHECK-NEXT: Size: 128 |
| -# CHECK-NEXT: Binding: Global |
| -# CHECK-NEXT: Type: Object |
| -# CHECK-NEXT: Other: 0 |
| -# CHECK-NEXT: Section: .common |
| -# CHECK-NEXT: } |
| +## Test that COMMON matches common symbols. |
| |
| +# RUN: rm -rf %t && split-file %s %t |
| +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %t/a.s -o %t/a.o |
| +# RUN: ld.lld -T %t/1.t %t/a.o -o %t/a1 |
| +# RUN: llvm-readelf -S -s %t/a1 | FileCheck %s --check-prefix=CHECK1 |
| +# RUN: ld.lld -T %t/2.t %t/a.o -o %t/a2 |
| +# RUN: llvm-readelf -S -s %t/a2 | FileCheck %s --check-prefix=CHECK2 |
| + |
| +# CHECK1: [Nr] Name Type Address Off Size ES Flg Lk Inf Al |
| +# CHECK1-NEXT: [ 0] NULL [[#%x,]] [[#%x,]] 000000 00 0 0 0 |
| +# CHECK1-NEXT: [ 1] .text PROGBITS [[#%x,]] [[#%x,]] 000005 00 AX 0 0 4 |
| +# CHECK1-NEXT: [ 2] .data PROGBITS [[#%x,]] [[#%x,]] 000001 00 WA 0 0 1 |
| +# CHECK1-NEXT: [ 3] .common NOBITS [[#%x,]] [[#%x,]] 000180 00 WA 0 0 256 |
| +# CHECK1: Value Size Type Bind Vis Ndx Name |
| +# CHECK1-DAG: [[#%x,]] 128 OBJECT GLOBAL DEFAULT [[#]] q1 |
| +# CHECK1-DAG: [[#%x,]] 128 OBJECT GLOBAL DEFAULT [[#]] q2 |
| + |
| +# CHECK2: [Nr] Name Type Address Off Size ES Flg Lk Inf Al |
| +# CHECK2-NEXT: [ 0] NULL [[#%x,]] [[#%x,]] 000000 00 0 0 0 |
| +# CHECK2-NEXT: [ 1] .text PROGBITS [[#%x,]] [[#%x,]] 000005 00 AX 0 0 4 |
| +# CHECK2-NEXT: [ 2] .data PROGBITS [[#%x,]] [[#%x,]] 000180 00 WA 0 0 256 |
| +# CHECK2: Value Size Type Bind Vis Ndx Name |
| +# CHECK2-DAG: [[#%x,]] 128 OBJECT GLOBAL DEFAULT [[#]] q1 |
| +# CHECK2-DAG: [[#%x,]] 128 OBJECT GLOBAL DEFAULT [[#]] q2 |
| + |
| +#--- a.s |
| .globl _start |
| _start: |
| jmp _start |
| |
| +.section .data,"aw",@progbits |
| +.byte 0 |
| + |
| .comm q1,128,8 |
| .comm q2,128,256 |
| + |
| +#--- 1.t |
| +SECTIONS { . = SIZEOF_HEADERS; .common : { *(COMMON) } } |
| + |
| +#--- 2.t |
| +## COMMON can be placed in a SHT_PROGBITS section. |
| +SECTIONS { . = SIZEOF_HEADERS; .data : { *(.data) *(COMMON) } } |