diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 173437e231b3e8d4d6a9edf3cb7bab0a061abeb9..8eb08b174d7bafd261b06923962762c712f06842 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2266,8 +2266,17 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, if (const auto *CSA = D->getAttr()) GO->setSection(CSA->getName()); + #ifdef __OHOS_FAMILY__ + else if (const auto *SA = D->getAttr()) { + GO->setSection(SA->getName()); + if ("_hilog_" == SA->getName()) + cast(GO)->setUnnamedAddr( + llvm::GlobalValue::UnnamedAddr::Global); + } + #else else if (const auto *SA = D->getAttr()) GO->setSection(SA->getName()); + #endif } getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); diff --git a/clang/test/CodeGen/attributes.c b/clang/test/CodeGen/attributes.c index 95c6e67f1fead75b2c1121e74e14fe521b42edc4..8f4512c5298fd248375da56281ba63e742541689 100644 --- a/clang/test/CodeGen/attributes.c +++ b/clang/test/CodeGen/attributes.c @@ -3,6 +3,9 @@ // CHECK: @t5 = weak{{.*}} global i32 2 int t5 __attribute__((weak)) = 2; +// CHECK: unnamed_addr +__attribute__((section("_hilog_"))) const char hilog_string1[] = "Hello"; + // CHECK: @t13 ={{.*}} global %struct.s0 zeroinitializer, section "SECT" struct s0 { int x; }; struct s0 t13 __attribute__((section("SECT"))) = { 0 }; diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 407ad034c18edcdfb537db705fa2b161b8fe5f11..12ff73f68a32a9f44e3b4da53756c2e62210b6ac 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -450,6 +450,11 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { // // .section .eh_frame,"a",@progbits + #ifdef __OHOS_FAMILY__ + if (Name == "_hilog_") + return SectionKind::getMergeable1ByteCString(); + #endif + if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, /*AddSegmentInfo=*/false) || Name == getInstrProfSectionName(IPSK_covfun, Triple::ELF, diff --git a/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll b/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll index 0ca08d4b3feade0047b6d773b8e6c558cd3f93a0..142f2316c22f97b9b15755e5757560322fab2cf5 100644 --- a/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll +++ b/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll @@ -53,6 +53,10 @@ define i32 @fn2_s2() section "s2" { ; CHECK: .globl fn2_s2 ; CHECK: fn2_s2: +; Check if the "_hilog_" section is set as mergeable +@hilog_var = global [14 x i8] c"Hello world!\0A\00", section "_hilog_" +; CHECK: .section _hilog_,"aMS",@progbits,1,unique,1 + ; Values that share a section name with a function are placed in different sections without executable flag @rw_s1 = global i32 10, section "s1", align 4 @ro_s2 = constant i32 10, section "s2", align 4