Skip to content

Commit

Permalink
[ELF] Fix llvm_unreachable failure when COMMON is placed in SHT_PROGB…
Browse files Browse the repository at this point in the history
…ITS output section

Fix a regression in aa27bab: COMMON in an
SHT_PROGBITS output section caused llvm_unreachable failure.
  • Loading branch information
MaskRay committed Mar 28, 2022
1 parent cd331af commit 1db59dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
4 changes: 1 addition & 3 deletions lld/ELF/SyntheticSections.h
Expand Up @@ -186,9 +186,7 @@ class BuildIdSection : public SyntheticSection {
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; }

Expand Down
75 changes: 36 additions & 39 deletions 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) } }

0 comments on commit 1db59dc

Please sign in to comment.