diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 2084fcfd4d651..c335e93186734 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1726,13 +1726,21 @@ static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym, sym = ctx.symtab->insert(objSym.getName()); } - int c = objSym.getComdatIndex(); - if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) { + if (objSym.isUndefined()) { Undefined newSym(&f, StringRef(), binding, visibility, type); sym->resolve(ctx, newSym); sym->referenced = true; return; } + int c = objSym.getComdatIndex(); + if (c != -1 && !keptComdats[c]) { + Defined newSym(ctx, &f, StringRef(), binding, visibility, type, 0, 0, + nullptr); + sym->ltoCanOmit = objSym.canBeOmittedFromSymbolTable() && + (!sym->isDefined() || sym->ltoCanOmit); + sym->resolve(ctx, newSym); + return; + } if (objSym.isCommon()) { sym->resolve(ctx, CommonSymbol{ctx, &f, StringRef(), binding, visibility, diff --git a/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll b/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll index 585b99ae5a513..3166166a65856 100644 --- a/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll +++ b/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll @@ -1,6 +1,16 @@ target triple = "x86_64-unknown-linux-gnu" target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +$f1 = comdat any +$f2 = comdat any + define weak_odr void @bah() { ret void } + +define linkonce_odr void @f1() local_unnamed_addr comdat { + ret void +} +define weak_odr void @f2() local_unnamed_addr comdat { + ret void +} \ No newline at end of file diff --git a/lld/test/ELF/lto/internalize-exportdyn.ll b/lld/test/ELF/lto/internalize-exportdyn.ll index f02d3b375dad5..ceaf31b4c8d2c 100644 --- a/lld/test/ELF/lto/internalize-exportdyn.ll +++ b/lld/test/ELF/lto/internalize-exportdyn.ll @@ -9,6 +9,9 @@ target triple = "x86_64-unknown-linux-gnu" target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +$f1 = comdat any +$f2 = comdat any + @c = linkonce_odr constant i32 1 @g = linkonce_odr global i32 1 @u_c = linkonce_odr unnamed_addr constant i32 1 @@ -46,6 +49,14 @@ define linkonce_odr void @baz() { @use_baz = global ptr @baz +define weak_odr void @f1() local_unnamed_addr comdat { + ret void +} + +define linkonce_odr void @f2() local_unnamed_addr comdat { + ret void +} + ; Check what gets internalized. ; CHECK: @c = weak_odr dso_local constant i32 1 ; CHECK: @g = weak_odr dso_local global i32 1 @@ -60,6 +71,8 @@ define linkonce_odr void @baz() { ; CHECK: define internal void @zed2() ; CHECK: define weak_odr dso_local void @bah() ; CHECK: define weak_odr dso_local void @baz() +; CHECK: define weak_odr dso_local void @f1() comdat +; CHECK: define weak_odr dso_local void @f2() comdat ; DSO: @c = weak_odr constant i32 1 ; DSO: @g = weak_odr global i32 1 @@ -74,3 +87,5 @@ define linkonce_odr void @baz() { ; DSO: define internal void @zed2() ; DSO: define weak_odr void @bah() ; DSO: define weak_odr void @baz() +; DSO: define weak_odr void @f1() comdat +; DSO: define weak_odr void @f2() comdat