Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1327,16 +1327,22 @@ if (auto *GA = dyn_cast(&GIS)) { // If the aliasee does not correspond to a symbol in the output, i.e. the - // alias is not of an object or the aliased object is private, then set the + // alias is not of an object, the aliased object is private, the aliasee is + // offset from the object or is smaller then the base object, then set the // size of the alias symbol from the type of the alias. We don't do this in // other situations as the alias and aliasee having differing types but same // size may be intentional. - const GlobalObject *BaseObject = GA->getBaseObject(); - if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized() && - (!BaseObject || BaseObject->hasPrivateLinkage())) { + const GlobalObject *BaseObject = + dyn_cast(GA->stripPointerCasts()); + if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized()) { const DataLayout &DL = M.getDataLayout(); uint64_t Size = DL.getTypeAllocSize(GA->getValueType()); - OutStreamer->emitELFSize(Name, MCConstantExpr::create(Size, OutContext)); + if (!BaseObject || BaseObject->hasPrivateLinkage() || + !BaseObject->getValueType()->isSized() || + Size < DL.getTypeAllocSize(BaseObject->getValueType())) { + OutStreamer->emitELFSize(Name, + MCConstantExpr::create(Size, OutContext)); + } } } } Index: llvm/test/CodeGen/X86/global-alias.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/X86/global-alias.ll @@ -0,0 +1,16 @@ +; RUN: llc < %s -mtriple=i386-unknown-linux-gnu | FileCheck %s + +%global.type = type <{ [4 x i32] }> +@global = linkonce_odr global %global.type zeroinitializer, align 4 +@my_alias = alias [2 x i32], bitcast (i32* getelementptr inbounds (i32, i32* bitcast (%global.type* @global to i32*), i32 2) to [2 x i32]*) +@my_alias2 = alias [4 x i32], bitcast (i32* getelementptr inbounds (i32, i32* bitcast (%global.type* @global to i32*), i32 2) to [4 x i32]*) +@my_alias3 = alias %global.type, %global.type* @global + +;CHECK: .weak global +;CHECK: .size global, 16 +;CHECK: .set my_alias, global+8 +;CHECK: .size my_alias, 8 +;CHECK: .set my_alias2, global+8 +;CHECK: .size my_alias2, 16 +;CHECK: .set my_alias3, global +;CHECK-NOT: .size my_alias3