Skip to content

Commit

Permalink
[elf2] Don't allocate VA space for TLS NOBITS sections.
Browse files Browse the repository at this point in the history
Differential Revision: http://reviews.llvm.org/D13838

llvm-svn: 251454
  • Loading branch information
Bigcheese committed Oct 27, 2015
1 parent 94c4943 commit 4633a37
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lld/ELF/Writer.cpp
Expand Up @@ -611,6 +611,15 @@ static uint32_t toPhdrFlags(uint64_t Flags) {
return Ret;
}

template <class ELFT>
static bool consumesVirtualAddressSpace(OutputSectionBase<ELFT> *Sec) {
return (Sec->getFlags() & SHF_ALLOC) &&
// Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
// responsible for allocating space for them, not the PT_LOAD that
// contains the TLS initialization image.
!((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS);
}

// Visits all sections to create PHDRs and to assign incremental,
// non-overlapping addresses to output sections.
template <class ELFT> void Writer<ELFT>::assignAddresses() {
Expand Down Expand Up @@ -659,7 +668,7 @@ template <class ELFT> void Writer<ELFT>::assignAddresses() {
}
}

if (Sec->getFlags() & SHF_ALLOC) {
if (consumesVirtualAddressSpace<ELFT>(Sec)) {
VA = RoundUpToAlignment(VA, Sec->getAlign());
Sec->setVA(VA);
VA += Sec->getSize();
Expand Down
93 changes: 93 additions & 0 deletions lld/test/elf2/tls.s
@@ -0,0 +1,93 @@
// REQUIRES: x86
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
// RUN: ld.lld2 %t -o %tout
// RUN: llvm-readobj -sections -program-headers %tout | FileCheck %s

.global _start
_start:

.section .tbss,"awT",@nobits
.long 0

.section .tdata,"awT",@progbits
.long 1

.section .thread_bss,"awT",@nobits
.long 0

.section .thread_data,"awT",@progbits
.long 2

// CHECK: Name: .tdata
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_TLS
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Link:
// CHECK-NEXT: Info:
// CHECK-NEXT: AddressAlignment:
// CHECK-NEXT: EntrySize:
// CHECK-NEXT: }
// CHECK-NEXT: Section {
// CHECK-NEXT: Index:
// CHECK-NEXT: Name: .thread_data
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_TLS
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Link:
// CHECK-NEXT: Info:
// CHECK-NEXT: AddressAlignment:
// CHECK-NEXT: EntrySize:
// CHECK-NEXT: }
// CHECK-NEXT: Section {
// CHECK-NEXT: Index:
// CHECK-NEXT: Name: .tbss
// CHECK-NEXT: Type: SHT_NOBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_TLS
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Link:
// CHECK-NEXT: Info:
// CHECK-NEXT: AddressAlignment:
// CHECK-NEXT: EntrySize:
// CHECK-NEXT: }
// CHECK-NEXT: Section {
// CHECK-NEXT: Index:
// CHECK-NEXT: Name: .thread_bss
// CHECK-NEXT: Type: SHT_NOBITS
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_TLS
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 4

// Check that the TLS NOBITS sections weren't added to the R/W PT_LOAD's size.

// CHECK: ProgramHeaders [
// CHECK: Type: PT_LOAD
// CHECK: Type: PT_LOAD
// CHECK: FileSize: 8
// CHECK-NEXT: MemSize: 8
// CHECK-NEXT: Flags [
// CHECK-NEXT: PF_R
// CHECK-NEXT: PF_W
// CHECK-NEXT: ]

0 comments on commit 4633a37

Please sign in to comment.