diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -425,7 +425,8 @@ MMI, Streamer); } -static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { +static SectionKind getELFKindForNamedSection(MCContext &Ctx, StringRef Name, + SectionKind K) { // N.B.: The defaults used in here are not the same ones used in MC. // We follow gcc, MC follows gas. For example, given ".section .eh_frame", // both gas and MC will produce a section with no flags. Given @@ -440,8 +441,11 @@ Name == ".llvmbc" || Name == ".llvmcmd") return SectionKind::getMetadata(); + // GNU binutils ld.bfd <2.28 has a bug which causes __start_/__stop_ symbols + // for SHT_NOBITS sections to have incorrect address if (Name == getInstrProfSectionName(IPSK_cnts, Triple::ELF, - /*AddSegmentInfo=*/false)) + /*AddSegmentInfo=*/false) && + Ctx.getAsmInfo()->binutilsIsAtLeast(2, 28)) return SectionKind::getBSS(); if (Name.empty() || Name[0] != '.') return K; @@ -674,7 +678,7 @@ } // Infer section flags from the section name if we can. - Kind = getELFKindForNamedSection(SectionName, Kind); + Kind = getELFKindForNamedSection(getContext(), SectionName, Kind); StringRef Group = ""; bool IsComdat = false; diff --git a/llvm/test/Instrumentation/InstrProfiling/X86/nobits.ll b/llvm/test/Instrumentation/InstrProfiling/X86/nobits.ll --- a/llvm/test/Instrumentation/InstrProfiling/X86/nobits.ll +++ b/llvm/test/Instrumentation/InstrProfiling/X86/nobits.ll @@ -1,5 +1,6 @@ ;; Ensure that SHT_NOBITS section type is set for __llvm_prf_cnts in ELF. -; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -binutils-version=2.27 | FileCheck %s --check-prefix=OLD +; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -binutils-version=2.28 | FileCheck %s --check-prefix=NEW @__profc_foo = hidden global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8 @@ -12,4 +13,5 @@ declare void @llvm.instrprof.increment(i8*, i64, i32, i32) -; CHECK: .section __llvm_prf_cnts,"aw",@nobits +; OLD: .section __llvm_prf_cnts,"aw",@progbits +; NEW: .section __llvm_prf_cnts,"aw",@nobits