From e5960ceb6b0f110790ae0befd1c5089d9f61f315 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Thu, 3 Mar 2016 20:24:14 +0000 Subject: [PATCH] [ELF] - Do not allow .bss to occupy the file space when producing relocatable output When generating relocatable output SHT_NOBITS sections were still occupy the file space. Differential revision: http://reviews.llvm.org/D17857 llvm-svn: 262650 --- lld/ELF/Writer.cpp | 6 ++++-- lld/test/ELF/relocatable-bss.s | 40 ++++++++++++++++++++++++++++++++++++++++ lld/test/ELF/relocatable.s | 2 +- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 lld/test/ELF/relocatable-bss.s diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index d138034..c6c18d3 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1327,9 +1327,11 @@ template void Writer::assignAddressesRelocatable() { Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); uintX_t FileOff = 0; for (OutputSectionBase *Sec : OutputSections) { - FileOff = alignTo(FileOff, Sec->getAlign()); + if (Sec->getType() != SHT_NOBITS) + FileOff = alignTo(FileOff, Sec->getAlign()); Sec->setFileOffset(FileOff); - FileOff += Sec->getSize(); + if (Sec->getType() != SHT_NOBITS) + FileOff += Sec->getSize(); } SectionHeaderOff = alignTo(FileOff, sizeof(uintX_t)); FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr); diff --git a/lld/test/ELF/relocatable-bss.s b/lld/test/ELF/relocatable-bss.s new file mode 100644 index 0000000..e0a0c60 --- /dev/null +++ b/lld/test/ELF/relocatable-bss.s @@ -0,0 +1,40 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: ld.lld -r %t1.o -o %t +# RUN: llvm-readobj -file-headers -sections -program-headers -symbols -r %t | FileCheck %s + +## We check here that .bss does not occupy the space in file. +## If it would, the SectionHeaderOffset would have offset about 5 megabytes. +# CHECK: ElfHeader { +# CHECK-NEXT: Ident { +# CHECK-NEXT: Magic: (7F 45 4C 46) +# CHECK-NEXT: Class: 64-bit +# CHECK-NEXT: DataEncoding: LittleEndian +# CHECK-NEXT: FileVersion: 1 +# CHECK-NEXT: OS/ABI: SystemV +# CHECK-NEXT: ABIVersion: 0 +# CHECK-NEXT: Unused: (00 00 00 00 00 00 00) +# CHECK-NEXT: } +# CHECK-NEXT: Type: Relocatable +# CHECK-NEXT: Machine: EM_X86_64 +# CHECK-NEXT: Version: +# CHECK-NEXT: Entry: +# CHECK-NEXT: ProgramHeaderOffset: +# CHECK-NEXT: SectionHeaderOffset: 0xA8 +# CHECK-NEXT: Flags [ +# CHECK-NEXT: ] +# CHECK-NEXT: HeaderSize: +# CHECK-NEXT: ProgramHeaderEntrySize: +# CHECK-NEXT: ProgramHeaderCount: +# CHECK-NEXT: SectionHeaderEntrySize: +# CHECK-NEXT: SectionHeaderCount: +# CHECK-NEXT: StringTableSectionIndex: +# CHECK-NEXT: } + +.text +.globl _start; +_start: + nop + +.bss + .space 5242880 diff --git a/lld/test/ELF/relocatable.s b/lld/test/ELF/relocatable.s index fd7a4f9..032cb63 100644 --- a/lld/test/ELF/relocatable.s +++ b/lld/test/ELF/relocatable.s @@ -30,7 +30,7 @@ # CHECK-NEXT: Version: 1 # CHECK-NEXT: Entry: 0x0 # CHECK-NEXT: ProgramHeaderOffset: 0x0 -# CHECK-NEXT: SectionHeaderOffset: 0x2D8 +# CHECK-NEXT: SectionHeaderOffset: 0x2C0 # CHECK-NEXT: Flags [ # CHECK-NEXT: ] # CHECK-NEXT: HeaderSize: 64 -- 2.7.4