Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -340,6 +340,9 @@ // The content for __ehdr_start symbol. static DefinedSynthetic *EhdrStart; + // The content for __bss_start symbol. + static DefinedSynthetic *BssStart; + // The content for _etext and etext symbols. static DefinedSynthetic *Etext; static DefinedSynthetic *Etext2; @@ -358,6 +361,7 @@ static DefinedRegular *MipsGp; }; +template DefinedSynthetic *ElfSym::BssStart; template DefinedSynthetic *ElfSym::EhdrStart; template DefinedSynthetic *ElfSym::Etext; template DefinedSynthetic *ElfSym::Etext2; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -840,6 +840,10 @@ ElfSym::EhdrStart = addOptionalSynthetic("__ehdr_start", Out::ElfHeader, 0); + // __bss_start is the location of .bss.* sections. + ElfSym::BssStart = + addOptionalSynthetic("__bss_start", nullptr, 0, STV_DEFAULT); + auto Define = [](StringRef S, DefinedSynthetic *&Sym1, DefinedSynthetic *&Sym2) { Sym1 = addOptionalSynthetic(S, nullptr, 0, STV_DEFAULT); @@ -1661,6 +1665,16 @@ Set(ElfSym::Edata, ElfSym::Edata2, LastRW->First, LastRW->p_filesz); + // __bss_start should point to the first .bss.* output section. + for (OutputSection *OS : OutputSections) { + bool IsBSS = (In::Common && In::Common->OutSec == OS) || + OS == Out::Bss || OS == Out::BssRelRo; + if (!IsBSS) + continue; + Set(ElfSym::BssStart, nullptr, OS, 0); + break; + } + // Setup MIPS _gp_disp/__gnu_local_gp symbols which should // be equal to the _gp symbol's value. if (Config->EMachine == EM_MIPS) { Index: test/ELF/Inputs/bss-start.s =================================================================== --- test/ELF/Inputs/bss-start.s +++ test/ELF/Inputs/bss-start.s @@ -0,0 +1,13 @@ +.rodata +.globl a +.size a, 4 +.type a, @object +a: +.word 1 + +.section .data,"aw",%progbits +.globl b +.size b, 4 +.type b, @object +b: +.word 2 Index: test/ELF/bss-start.s =================================================================== --- test/ELF/bss-start.s +++ test/ELF/bss-start.s @@ -0,0 +1,22 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/bss-start.s -o %t2.o +# RUN: ld.lld -shared %t2.o -o %t.so +# RUN: ld.lld %t %t.so -o %t2 +# RUN: llvm-objdump -t -section-headers %t2 | FileCheck %s + +# CHECK: Sections: +# CHECK: Idx Name Size Address Type +# CHECK: 7 .bss.rel.ro 00000004 00000000002020b0 BSS +# CHECK: 8 .bss 00000004 0000000000203000 BSS +# CHECK: 9 .bss 00000004 0000000000203004 BSS +# CHECK: SYMBOL TABLE: +# CHECK: 00000000002020b0 .bss.rel.ro 00000000 __bss_start + +.global __bss_start +.text +_start: +.comm sym1,4,4 + + movl $1, a + movl $1, b Index: test/ELF/bss-start2.s =================================================================== --- test/ELF/bss-start2.s +++ test/ELF/bss-start2.s @@ -0,0 +1,17 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/bss-start.s -o %t2.o +# RUN: ld.lld -shared %t2.o -o %t.so +# RUN: ld.lld %t %t.so -o %t2 +# RUN: llvm-objdump -t -section-headers %t2 | FileCheck %s + +# CHECK: Sections: +# CHECK: Idx Name Size Address Type +# CHECK: 7 .bss 00000004 0000000000203000 BSS +# CHECK: SYMBOL TABLE: +# CHECK: 0000000000203000 .bss 00000000 __bss_start + +.global __bss_start +.text +_start: + movl $1, b Index: test/ELF/bss-start3.s =================================================================== --- test/ELF/bss-start3.s +++ test/ELF/bss-start3.s @@ -0,0 +1,15 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: ld.lld %t -o %t2 +# RUN: llvm-objdump -t -section-headers %t2 | FileCheck %s + +# CHECK: Sections: +# CHECK: Idx Name Size Address Type +# CHECK: 2 .bss 00000004 0000000000201000 BSS +# CHECK: SYMBOL TABLE: +# CHECK: 0000000000201000 .bss 00000000 __bss_start + +.global __bss_start +.text +_start: +.comm sym1,4,4