[clang] [llvm] [DebugInfo] Fix endianness in DW_AT_const_value for constexpr arrays (PR #184804)
Shivam Kunwar via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 5 07:07:00 PST 2026
================
@@ -2036,8 +2036,31 @@ DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
addConstantValue(StaticMemberDIE, CI, Ty);
- if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
+ else if (const ConstantFP *CFP =
+ dyn_cast_or_null<ConstantFP>(DT->getConstant()))
addConstantFPValue(StaticMemberDIE, CFP);
+ else if (auto *CDS =
+ dyn_cast_or_null<ConstantDataSequential>(DT->getConstant())) {
+ // Emit each element byte-by-byte in target byte order. We avoid
+ // getRawDataValues() because it exposes host endianness, which
+ // produces incorrect DWARF on big-endian hosts cross-compiling
+ // for little-endian targets (and vice versa).
+ auto *Block = new (DIEValueAllocator) DIEBlock;
+ unsigned NumElts = CDS->getNumElements();
+ unsigned ElemByteSize = CDS->getElementType()->getPrimitiveSizeInBits() / 8;
+ bool IsLE = Asm->getDataLayout().isLittleEndian();
+ for (unsigned I = 0; I < NumElts; ++I) {
+ APInt Val = CDS->getElementAsAPInt(I);
+ for (unsigned J = 0; J < ElemByteSize; ++J) {
+ unsigned BitOff = IsLE ? (J * 8) : ((ElemByteSize - 1 - J) * 8);
+ addUInt(*Block, dwarf::DW_FORM_data1,
+ Val.extractBitsAsZExtValue(8, BitOff));
+ }
+ }
----------------
phyBrackets wrote:
Good call, I concatenate the elements into a single APInt with insertBits and pass it to addIntAsBlock, which already handles endianness. Much cleaner, thanks!
https://github.com/llvm/llvm-project/pull/184804
More information about the cfe-commits
mailing list