Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1 | //===- BTFDebug.cpp - BTF Generator ---------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains support for writing BTF debug info. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "BTFDebug.h" |
| 14 | #include "llvm/BinaryFormat/ELF.h" |
| 15 | #include "llvm/CodeGen/AsmPrinter.h" |
| 16 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 17 | #include "llvm/MC/MCContext.h" |
| 18 | #include "llvm/MC/MCObjectFileInfo.h" |
| 19 | #include "llvm/MC/MCSectionELF.h" |
| 20 | #include "llvm/MC/MCStreamer.h" |
| 21 | #include <fstream> |
| 22 | #include <sstream> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | static const char *BTFKindStr[] = { |
| 27 | #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME, |
| 28 | #include "BTF.def" |
| 29 | }; |
| 30 | |
| 31 | /// Emit a BTF common type. |
| 32 | void BTFTypeBase::emitType(MCStreamer &OS) { |
| 33 | OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) + |
| 34 | ")"); |
| 35 | OS.EmitIntValue(BTFType.NameOff, 4); |
| 36 | OS.AddComment("0x" + Twine::utohexstr(BTFType.Info)); |
| 37 | OS.EmitIntValue(BTFType.Info, 4); |
| 38 | OS.EmitIntValue(BTFType.Size, 4); |
| 39 | } |
| 40 | |
| 41 | BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag) |
| 42 | : DTy(DTy) { |
| 43 | switch (Tag) { |
| 44 | case dwarf::DW_TAG_pointer_type: |
| 45 | Kind = BTF::BTF_KIND_PTR; |
| 46 | break; |
| 47 | case dwarf::DW_TAG_const_type: |
| 48 | Kind = BTF::BTF_KIND_CONST; |
| 49 | break; |
| 50 | case dwarf::DW_TAG_volatile_type: |
| 51 | Kind = BTF::BTF_KIND_VOLATILE; |
| 52 | break; |
| 53 | case dwarf::DW_TAG_typedef: |
| 54 | Kind = BTF::BTF_KIND_TYPEDEF; |
| 55 | break; |
| 56 | case dwarf::DW_TAG_restrict_type: |
| 57 | Kind = BTF::BTF_KIND_RESTRICT; |
| 58 | break; |
| 59 | default: |
| 60 | llvm_unreachable("Unknown DIDerivedType Tag"); |
| 61 | } |
| 62 | BTFType.Info = Kind << 24; |
| 63 | } |
| 64 | |
| 65 | void BTFTypeDerived::completeType(BTFDebug &BDebug) { |
| 66 | BTFType.NameOff = BDebug.addString(DTy->getName()); |
| 67 | |
| 68 | // The base type for PTR/CONST/VOLATILE could be void. |
| 69 | const DIType *ResolvedType = DTy->getBaseType().resolve(); |
| 70 | if (!ResolvedType) { |
| 71 | assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST || |
| 72 | Kind == BTF::BTF_KIND_VOLATILE) && |
| 73 | "Invalid null basetype"); |
| 74 | BTFType.Type = 0; |
| 75 | } else { |
| 76 | BTFType.Type = BDebug.getTypeId(ResolvedType); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); } |
| 81 | |
| 82 | /// Represent a struct/union forward declaration. |
| 83 | BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) { |
| 84 | Kind = BTF::BTF_KIND_FWD; |
| 85 | BTFType.Info = IsUnion << 31 | Kind << 24; |
| 86 | BTFType.Type = 0; |
| 87 | } |
| 88 | |
| 89 | void BTFTypeFwd::completeType(BTFDebug &BDebug) { |
| 90 | BTFType.NameOff = BDebug.addString(Name); |
| 91 | } |
| 92 | |
| 93 | void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); } |
| 94 | |
| 95 | BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, |
| 96 | uint32_t OffsetInBits, StringRef TypeName) |
| 97 | : Name(TypeName) { |
| 98 | // Translate IR int encoding to BTF int encoding. |
| 99 | uint8_t BTFEncoding; |
| 100 | switch (Encoding) { |
| 101 | case dwarf::DW_ATE_boolean: |
| 102 | BTFEncoding = BTF::INT_BOOL; |
| 103 | break; |
| 104 | case dwarf::DW_ATE_signed: |
| 105 | case dwarf::DW_ATE_signed_char: |
| 106 | BTFEncoding = BTF::INT_SIGNED; |
| 107 | break; |
| 108 | case dwarf::DW_ATE_unsigned: |
| 109 | case dwarf::DW_ATE_unsigned_char: |
| 110 | BTFEncoding = 0; |
| 111 | break; |
| 112 | default: |
| 113 | llvm_unreachable("Unknown BTFTypeInt Encoding"); |
| 114 | } |
| 115 | |
| 116 | Kind = BTF::BTF_KIND_INT; |
| 117 | BTFType.Info = Kind << 24; |
| 118 | BTFType.Size = roundupToBytes(SizeInBits); |
| 119 | IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits; |
| 120 | } |
| 121 | |
| 122 | void BTFTypeInt::completeType(BTFDebug &BDebug) { |
| 123 | BTFType.NameOff = BDebug.addString(Name); |
| 124 | } |
| 125 | |
| 126 | void BTFTypeInt::emitType(MCStreamer &OS) { |
| 127 | BTFTypeBase::emitType(OS); |
| 128 | OS.AddComment("0x" + Twine::utohexstr(IntVal)); |
| 129 | OS.EmitIntValue(IntVal, 4); |
| 130 | } |
| 131 | |
| 132 | BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) { |
| 133 | Kind = BTF::BTF_KIND_ENUM; |
| 134 | BTFType.Info = Kind << 24 | VLen; |
| 135 | BTFType.Size = roundupToBytes(ETy->getSizeInBits()); |
| 136 | } |
| 137 | |
| 138 | void BTFTypeEnum::completeType(BTFDebug &BDebug) { |
| 139 | BTFType.NameOff = BDebug.addString(ETy->getName()); |
| 140 | |
| 141 | DINodeArray Elements = ETy->getElements(); |
| 142 | for (const auto Element : Elements) { |
| 143 | const auto *Enum = cast<DIEnumerator>(Element); |
| 144 | |
| 145 | struct BTF::BTFEnum BTFEnum; |
| 146 | BTFEnum.NameOff = BDebug.addString(Enum->getName()); |
| 147 | // BTF enum value is 32bit, enforce it. |
| 148 | BTFEnum.Val = static_cast<uint32_t>(Enum->getValue()); |
| 149 | EnumValues.push_back(BTFEnum); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void BTFTypeEnum::emitType(MCStreamer &OS) { |
| 154 | BTFTypeBase::emitType(OS); |
| 155 | for (const auto &Enum : EnumValues) { |
| 156 | OS.EmitIntValue(Enum.NameOff, 4); |
| 157 | OS.EmitIntValue(Enum.Val, 4); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | BTFTypeArray::BTFTypeArray(const DICompositeType *ATy) : ATy(ATy) { |
| 162 | Kind = BTF::BTF_KIND_ARRAY; |
| 163 | BTFType.Info = Kind << 24; |
| 164 | } |
| 165 | |
| 166 | /// Represent a BTF array. BTF does not record array dimensions, |
| 167 | /// so conceptually a BTF array is a one-dimensional array. |
| 168 | void BTFTypeArray::completeType(BTFDebug &BDebug) { |
| 169 | BTFType.NameOff = BDebug.addString(ATy->getName()); |
| 170 | BTFType.Size = 0; |
| 171 | |
| 172 | auto *BaseType = ATy->getBaseType().resolve(); |
| 173 | ArrayInfo.ElemType = BDebug.getTypeId(BaseType); |
| 174 | |
| 175 | // The IR does not really have a type for the index. |
| 176 | // A special type for array index should have been |
| 177 | // created during initial type traversal. Just |
| 178 | // retrieve that type id. |
| 179 | ArrayInfo.IndexType = BDebug.getArrayIndexTypeId(); |
| 180 | |
| 181 | // Get the number of array elements. |
| 182 | // If the array size is 0, set the number of elements as 0. |
| 183 | // Otherwise, recursively traverse the base types to |
| 184 | // find the element size. The number of elements is |
| 185 | // the totoal array size in bits divided by |
| 186 | // element size in bits. |
| 187 | uint64_t ArraySizeInBits = ATy->getSizeInBits(); |
| 188 | if (!ArraySizeInBits) { |
| 189 | ArrayInfo.Nelems = 0; |
| 190 | } else { |
| 191 | uint32_t BaseTypeSize = BaseType->getSizeInBits(); |
| 192 | while (!BaseTypeSize) { |
| 193 | const auto *DDTy = cast<DIDerivedType>(BaseType); |
| 194 | BaseType = DDTy->getBaseType().resolve(); |
| 195 | assert(BaseType); |
| 196 | BaseTypeSize = BaseType->getSizeInBits(); |
| 197 | } |
| 198 | ArrayInfo.Nelems = ATy->getSizeInBits() / BaseTypeSize; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void BTFTypeArray::emitType(MCStreamer &OS) { |
| 203 | BTFTypeBase::emitType(OS); |
| 204 | OS.EmitIntValue(ArrayInfo.ElemType, 4); |
| 205 | OS.EmitIntValue(ArrayInfo.IndexType, 4); |
| 206 | OS.EmitIntValue(ArrayInfo.Nelems, 4); |
| 207 | } |
| 208 | |
| 209 | /// Represent either a struct or a union. |
| 210 | BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct, |
| 211 | bool HasBitField, uint32_t Vlen) |
| 212 | : STy(STy), HasBitField(HasBitField) { |
| 213 | Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION; |
| 214 | BTFType.Size = roundupToBytes(STy->getSizeInBits()); |
| 215 | BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen; |
| 216 | } |
| 217 | |
| 218 | void BTFTypeStruct::completeType(BTFDebug &BDebug) { |
| 219 | BTFType.NameOff = BDebug.addString(STy->getName()); |
| 220 | |
| 221 | // Add struct/union members. |
| 222 | const DINodeArray Elements = STy->getElements(); |
| 223 | for (const auto *Element : Elements) { |
| 224 | struct BTF::BTFMember BTFMember; |
| 225 | const auto *DDTy = cast<DIDerivedType>(Element); |
| 226 | |
| 227 | BTFMember.NameOff = BDebug.addString(DDTy->getName()); |
| 228 | if (HasBitField) { |
| 229 | uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0; |
| 230 | BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits(); |
| 231 | } else { |
| 232 | BTFMember.Offset = DDTy->getOffsetInBits(); |
| 233 | } |
| 234 | BTFMember.Type = BDebug.getTypeId(DDTy->getBaseType().resolve()); |
| 235 | Members.push_back(BTFMember); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void BTFTypeStruct::emitType(MCStreamer &OS) { |
| 240 | BTFTypeBase::emitType(OS); |
| 241 | for (const auto &Member : Members) { |
| 242 | OS.EmitIntValue(Member.NameOff, 4); |
| 243 | OS.EmitIntValue(Member.Type, 4); |
| 244 | OS.AddComment("0x" + Twine::utohexstr(Member.Offset)); |
| 245 | OS.EmitIntValue(Member.Offset, 4); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /// The Func kind represents both subprogram and pointee of function |
| 250 | /// pointers. If the FuncName is empty, it represents a pointee of function |
| 251 | /// pointer. Otherwise, it represents a subprogram. The func arg names |
| 252 | /// are empty for pointee of function pointer case, and are valid names |
| 253 | /// for subprogram. |
| 254 | BTFTypeFuncProto::BTFTypeFuncProto( |
| 255 | const DISubroutineType *STy, uint32_t VLen, |
| 256 | const std::unordered_map<uint32_t, StringRef> &FuncArgNames) |
| 257 | : STy(STy), FuncArgNames(FuncArgNames) { |
| 258 | Kind = BTF::BTF_KIND_FUNC_PROTO; |
| 259 | BTFType.Info = (Kind << 24) | VLen; |
| 260 | } |
| 261 | |
| 262 | void BTFTypeFuncProto::completeType(BTFDebug &BDebug) { |
| 263 | DITypeRefArray Elements = STy->getTypeArray(); |
| 264 | auto RetType = Elements[0].resolve(); |
| 265 | BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0; |
| 266 | BTFType.NameOff = 0; |
| 267 | |
| 268 | // For null parameter which is typically the last one |
| 269 | // to represent the vararg, encode the NameOff/Type to be 0. |
| 270 | for (unsigned I = 1, N = Elements.size(); I < N; ++I) { |
| 271 | struct BTF::BTFParam Param; |
| 272 | auto Element = Elements[I].resolve(); |
| 273 | if (Element) { |
| 274 | Param.NameOff = BDebug.addString(FuncArgNames[I]); |
| 275 | Param.Type = BDebug.getTypeId(Element); |
| 276 | } else { |
| 277 | Param.NameOff = 0; |
| 278 | Param.Type = 0; |
| 279 | } |
| 280 | Parameters.push_back(Param); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void BTFTypeFuncProto::emitType(MCStreamer &OS) { |
| 285 | BTFTypeBase::emitType(OS); |
| 286 | for (const auto &Param : Parameters) { |
| 287 | OS.EmitIntValue(Param.NameOff, 4); |
| 288 | OS.EmitIntValue(Param.Type, 4); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId) |
| 293 | : Name(FuncName) { |
| 294 | Kind = BTF::BTF_KIND_FUNC; |
| 295 | BTFType.Info = Kind << 24; |
| 296 | BTFType.Type = ProtoTypeId; |
| 297 | } |
| 298 | |
| 299 | void BTFTypeFunc::completeType(BTFDebug &BDebug) { |
| 300 | BTFType.NameOff = BDebug.addString(Name); |
| 301 | } |
| 302 | |
| 303 | void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); } |
| 304 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 305 | BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo) |
| 306 | : Name(VarName) { |
| 307 | Kind = BTF::BTF_KIND_VAR; |
| 308 | BTFType.Info = Kind << 24; |
| 309 | BTFType.Type = TypeId; |
| 310 | Info = VarInfo; |
| 311 | } |
| 312 | |
| 313 | void BTFKindVar::completeType(BTFDebug &BDebug) { |
| 314 | BTFType.NameOff = BDebug.addString(Name); |
| 315 | } |
| 316 | |
| 317 | void BTFKindVar::emitType(MCStreamer &OS) { |
| 318 | BTFTypeBase::emitType(OS); |
| 319 | OS.EmitIntValue(Info, 4); |
| 320 | } |
| 321 | |
| 322 | BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName) |
| 323 | : Asm(AsmPrt), Name(SecName) { |
| 324 | Kind = BTF::BTF_KIND_DATASEC; |
| 325 | BTFType.Info = Kind << 24; |
| 326 | BTFType.Size = 0; |
| 327 | } |
| 328 | |
| 329 | void BTFKindDataSec::completeType(BTFDebug &BDebug) { |
| 330 | BTFType.NameOff = BDebug.addString(Name); |
| 331 | BTFType.Info |= Vars.size(); |
| 332 | } |
| 333 | |
| 334 | void BTFKindDataSec::emitType(MCStreamer &OS) { |
| 335 | BTFTypeBase::emitType(OS); |
| 336 | |
| 337 | for (const auto &V : Vars) { |
| 338 | OS.EmitIntValue(std::get<0>(V), 4); |
| 339 | Asm->EmitLabelReference(std::get<1>(V), 4); |
| 340 | OS.EmitIntValue(std::get<2>(V), 4); |
| 341 | } |
| 342 | } |
| 343 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 344 | uint32_t BTFStringTable::addString(StringRef S) { |
| 345 | // Check whether the string already exists. |
| 346 | for (auto &OffsetM : OffsetToIdMap) { |
| 347 | if (Table[OffsetM.second] == S) |
| 348 | return OffsetM.first; |
| 349 | } |
| 350 | // Not find, add to the string table. |
| 351 | uint32_t Offset = Size; |
| 352 | OffsetToIdMap[Offset] = Table.size(); |
| 353 | Table.push_back(S); |
| 354 | Size += S.size() + 1; |
| 355 | return Offset; |
| 356 | } |
| 357 | |
| 358 | BTFDebug::BTFDebug(AsmPrinter *AP) |
| 359 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false), |
| 360 | LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0) { |
| 361 | addString("\0"); |
| 362 | } |
| 363 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 364 | uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry, |
| 365 | const DIType *Ty) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 366 | TypeEntry->setId(TypeEntries.size() + 1); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 367 | uint32_t Id = TypeEntry->getId(); |
| 368 | DIToIdMap[Ty] = Id; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 369 | TypeEntries.push_back(std::move(TypeEntry)); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 370 | return Id; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) { |
| 374 | TypeEntry->setId(TypeEntries.size() + 1); |
| 375 | uint32_t Id = TypeEntry->getId(); |
| 376 | TypeEntries.push_back(std::move(TypeEntry)); |
| 377 | return Id; |
| 378 | } |
| 379 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 380 | void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 381 | // Only int types are supported in BTF. |
| 382 | uint32_t Encoding = BTy->getEncoding(); |
| 383 | if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed && |
| 384 | Encoding != dwarf::DW_ATE_signed_char && |
| 385 | Encoding != dwarf::DW_ATE_unsigned && |
| 386 | Encoding != dwarf::DW_ATE_unsigned_char) |
| 387 | return; |
| 388 | |
| 389 | // Create a BTF type instance for this DIBasicType and put it into |
| 390 | // DIToIdMap for cross-type reference check. |
| 391 | auto TypeEntry = llvm::make_unique<BTFTypeInt>( |
| 392 | Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName()); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 393 | TypeId = addType(std::move(TypeEntry), BTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /// Handle subprogram or subroutine types. |
| 397 | void BTFDebug::visitSubroutineType( |
| 398 | const DISubroutineType *STy, bool ForSubprog, |
| 399 | const std::unordered_map<uint32_t, StringRef> &FuncArgNames, |
| 400 | uint32_t &TypeId) { |
| 401 | DITypeRefArray Elements = STy->getTypeArray(); |
| 402 | uint32_t VLen = Elements.size() - 1; |
| 403 | if (VLen > BTF::MAX_VLEN) |
| 404 | return; |
| 405 | |
| 406 | // Subprogram has a valid non-zero-length name, and the pointee of |
| 407 | // a function pointer has an empty name. The subprogram type will |
| 408 | // not be added to DIToIdMap as it should not be referenced by |
| 409 | // any other types. |
| 410 | auto TypeEntry = llvm::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames); |
| 411 | if (ForSubprog) |
| 412 | TypeId = addType(std::move(TypeEntry)); // For subprogram |
| 413 | else |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 414 | TypeId = addType(std::move(TypeEntry), STy); // For func ptr |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 415 | |
| 416 | // Visit return type and func arg types. |
| 417 | for (const auto Element : Elements) { |
| 418 | visitTypeEntry(Element.resolve()); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /// Handle structure/union types. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 423 | void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct, |
| 424 | uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 425 | const DINodeArray Elements = CTy->getElements(); |
| 426 | uint32_t VLen = Elements.size(); |
| 427 | if (VLen > BTF::MAX_VLEN) |
| 428 | return; |
| 429 | |
| 430 | // Check whether we have any bitfield members or not |
| 431 | bool HasBitField = false; |
| 432 | for (const auto *Element : Elements) { |
| 433 | auto E = cast<DIDerivedType>(Element); |
| 434 | if (E->isBitField()) { |
| 435 | HasBitField = true; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | auto TypeEntry = |
| 441 | llvm::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 442 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 443 | |
| 444 | // Visit all struct members. |
| 445 | for (const auto *Element : Elements) |
| 446 | visitTypeEntry(cast<DIDerivedType>(Element)); |
| 447 | } |
| 448 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 449 | void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 450 | auto TypeEntry = llvm::make_unique<BTFTypeArray>(CTy); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 451 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 452 | |
| 453 | // The IR does not have a type for array index while BTF wants one. |
| 454 | // So create an array index type if there is none. |
| 455 | if (!ArrayIndexTypeId) { |
| 456 | auto TypeEntry = llvm::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32, |
| 457 | 0, "__ARRAY_SIZE_TYPE__"); |
| 458 | ArrayIndexTypeId = addType(std::move(TypeEntry)); |
| 459 | } |
| 460 | |
| 461 | // Visit array element type. |
| 462 | visitTypeEntry(CTy->getBaseType().resolve()); |
| 463 | } |
| 464 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 465 | void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 466 | DINodeArray Elements = CTy->getElements(); |
| 467 | uint32_t VLen = Elements.size(); |
| 468 | if (VLen > BTF::MAX_VLEN) |
| 469 | return; |
| 470 | |
| 471 | auto TypeEntry = llvm::make_unique<BTFTypeEnum>(CTy, VLen); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 472 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 473 | // No need to visit base type as BTF does not encode it. |
| 474 | } |
| 475 | |
| 476 | /// Handle structure/union forward declarations. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 477 | void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion, |
| 478 | uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 479 | auto TypeEntry = llvm::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 480 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /// Handle structure, union, array and enumeration types. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 484 | void BTFDebug::visitCompositeType(const DICompositeType *CTy, |
| 485 | uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 486 | auto Tag = CTy->getTag(); |
| 487 | if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { |
| 488 | // Handle forward declaration differently as it does not have members. |
| 489 | if (CTy->isForwardDecl()) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 490 | visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 491 | else |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 492 | visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 493 | } else if (Tag == dwarf::DW_TAG_array_type) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 494 | visitArrayType(CTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 495 | else if (Tag == dwarf::DW_TAG_enumeration_type) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 496 | visitEnumType(CTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /// Handle pointer, typedef, const, volatile, restrict and member types. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 500 | void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 501 | unsigned Tag = DTy->getTag(); |
| 502 | |
| 503 | if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef || |
| 504 | Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type || |
| 505 | Tag == dwarf::DW_TAG_restrict_type) { |
| 506 | auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 507 | TypeId = addType(std::move(TypeEntry), DTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 508 | } else if (Tag != dwarf::DW_TAG_member) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | // Visit base type of pointer, typedef, const, volatile, restrict or |
| 513 | // struct/union member. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 514 | visitTypeEntry(DTy->getBaseType().resolve(), TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 517 | void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId) { |
| 518 | if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) { |
| 519 | TypeId = DIToIdMap[Ty]; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 520 | return; |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 521 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 522 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 523 | if (const auto *BTy = dyn_cast<DIBasicType>(Ty)) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 524 | visitBasicType(BTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 525 | else if (const auto *STy = dyn_cast<DISubroutineType>(Ty)) |
| 526 | visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(), |
| 527 | TypeId); |
| 528 | else if (const auto *CTy = dyn_cast<DICompositeType>(Ty)) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 529 | visitCompositeType(CTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 530 | else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 531 | visitDerivedType(DTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 532 | else |
| 533 | llvm_unreachable("Unknown DIType"); |
| 534 | } |
| 535 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 536 | void BTFDebug::visitTypeEntry(const DIType *Ty) { |
| 537 | uint32_t TypeId; |
| 538 | visitTypeEntry(Ty, TypeId); |
| 539 | } |
| 540 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 541 | /// Read file contents from the actual file or from the source |
| 542 | std::string BTFDebug::populateFileContent(const DISubprogram *SP) { |
| 543 | auto File = SP->getFile(); |
| 544 | std::string FileName; |
| 545 | |
Yonghong Song | fa36540 | 2019-02-02 05:54:59 +0000 | [diff] [blame] | 546 | if (!File->getFilename().startswith("/") && File->getDirectory().size()) |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 547 | FileName = File->getDirectory().str() + "/" + File->getFilename().str(); |
| 548 | else |
| 549 | FileName = File->getFilename(); |
| 550 | |
| 551 | // No need to populate the contends if it has been populated! |
| 552 | if (FileContent.find(FileName) != FileContent.end()) |
| 553 | return FileName; |
| 554 | |
| 555 | std::vector<std::string> Content; |
| 556 | std::string Line; |
| 557 | Content.push_back(Line); // Line 0 for empty string |
| 558 | |
| 559 | auto Source = File->getSource(); |
| 560 | if (Source) { |
| 561 | std::istringstream InputString(Source.getValue()); |
| 562 | while (std::getline(InputString, Line)) |
| 563 | Content.push_back(Line); |
| 564 | } else { |
| 565 | std::ifstream InputFile(FileName); |
| 566 | while (std::getline(InputFile, Line)) |
| 567 | Content.push_back(Line); |
| 568 | } |
| 569 | |
| 570 | FileContent[FileName] = Content; |
| 571 | return FileName; |
| 572 | } |
| 573 | |
| 574 | void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label, |
| 575 | uint32_t Line, uint32_t Column) { |
| 576 | std::string FileName = populateFileContent(SP); |
| 577 | BTFLineInfo LineInfo; |
| 578 | |
| 579 | LineInfo.Label = Label; |
| 580 | LineInfo.FileNameOff = addString(FileName); |
| 581 | // If file content is not available, let LineOff = 0. |
| 582 | if (Line < FileContent[FileName].size()) |
| 583 | LineInfo.LineOff = addString(FileContent[FileName][Line]); |
| 584 | else |
| 585 | LineInfo.LineOff = 0; |
| 586 | LineInfo.LineNum = Line; |
| 587 | LineInfo.ColumnNum = Column; |
| 588 | LineInfoTable[SecNameOff].push_back(LineInfo); |
| 589 | } |
| 590 | |
| 591 | void BTFDebug::emitCommonHeader() { |
| 592 | OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC)); |
| 593 | OS.EmitIntValue(BTF::MAGIC, 2); |
| 594 | OS.EmitIntValue(BTF::VERSION, 1); |
| 595 | OS.EmitIntValue(0, 1); |
| 596 | } |
| 597 | |
| 598 | void BTFDebug::emitBTFSection() { |
Yonghong Song | d82247c | 2019-03-05 01:01:21 +0000 | [diff] [blame] | 599 | // Do not emit section if no types and only "" string. |
| 600 | if (!TypeEntries.size() && StringTable.getSize() == 1) |
| 601 | return; |
| 602 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 603 | MCContext &Ctx = OS.getContext(); |
| 604 | OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0)); |
| 605 | |
| 606 | // Emit header. |
| 607 | emitCommonHeader(); |
| 608 | OS.EmitIntValue(BTF::HeaderSize, 4); |
| 609 | |
| 610 | uint32_t TypeLen = 0, StrLen; |
| 611 | for (const auto &TypeEntry : TypeEntries) |
| 612 | TypeLen += TypeEntry->getSize(); |
| 613 | StrLen = StringTable.getSize(); |
| 614 | |
| 615 | OS.EmitIntValue(0, 4); |
| 616 | OS.EmitIntValue(TypeLen, 4); |
| 617 | OS.EmitIntValue(TypeLen, 4); |
| 618 | OS.EmitIntValue(StrLen, 4); |
| 619 | |
| 620 | // Emit type table. |
| 621 | for (const auto &TypeEntry : TypeEntries) |
| 622 | TypeEntry->emitType(OS); |
| 623 | |
| 624 | // Emit string table. |
| 625 | uint32_t StringOffset = 0; |
| 626 | for (const auto &S : StringTable.getTable()) { |
| 627 | OS.AddComment("string offset=" + std::to_string(StringOffset)); |
| 628 | OS.EmitBytes(S); |
| 629 | OS.EmitBytes(StringRef("\0", 1)); |
| 630 | StringOffset += S.size() + 1; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | void BTFDebug::emitBTFExtSection() { |
Yonghong Song | d82247c | 2019-03-05 01:01:21 +0000 | [diff] [blame] | 635 | // Do not emit section if empty FuncInfoTable and LineInfoTable. |
| 636 | if (!FuncInfoTable.size() && !LineInfoTable.size()) |
| 637 | return; |
| 638 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 639 | MCContext &Ctx = OS.getContext(); |
| 640 | OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0)); |
| 641 | |
| 642 | // Emit header. |
| 643 | emitCommonHeader(); |
| 644 | OS.EmitIntValue(BTF::ExtHeaderSize, 4); |
| 645 | |
| 646 | // Account for FuncInfo/LineInfo record size as well. |
| 647 | uint32_t FuncLen = 4, LineLen = 4; |
| 648 | for (const auto &FuncSec : FuncInfoTable) { |
| 649 | FuncLen += BTF::SecFuncInfoSize; |
| 650 | FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize; |
| 651 | } |
| 652 | for (const auto &LineSec : LineInfoTable) { |
| 653 | LineLen += BTF::SecLineInfoSize; |
| 654 | LineLen += LineSec.second.size() * BTF::BPFLineInfoSize; |
| 655 | } |
| 656 | |
| 657 | OS.EmitIntValue(0, 4); |
| 658 | OS.EmitIntValue(FuncLen, 4); |
| 659 | OS.EmitIntValue(FuncLen, 4); |
| 660 | OS.EmitIntValue(LineLen, 4); |
| 661 | |
| 662 | // Emit func_info table. |
| 663 | OS.AddComment("FuncInfo"); |
| 664 | OS.EmitIntValue(BTF::BPFFuncInfoSize, 4); |
| 665 | for (const auto &FuncSec : FuncInfoTable) { |
| 666 | OS.AddComment("FuncInfo section string offset=" + |
| 667 | std::to_string(FuncSec.first)); |
| 668 | OS.EmitIntValue(FuncSec.first, 4); |
| 669 | OS.EmitIntValue(FuncSec.second.size(), 4); |
| 670 | for (const auto &FuncInfo : FuncSec.second) { |
| 671 | Asm->EmitLabelReference(FuncInfo.Label, 4); |
| 672 | OS.EmitIntValue(FuncInfo.TypeId, 4); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Emit line_info table. |
| 677 | OS.AddComment("LineInfo"); |
| 678 | OS.EmitIntValue(BTF::BPFLineInfoSize, 4); |
| 679 | for (const auto &LineSec : LineInfoTable) { |
| 680 | OS.AddComment("LineInfo section string offset=" + |
| 681 | std::to_string(LineSec.first)); |
| 682 | OS.EmitIntValue(LineSec.first, 4); |
| 683 | OS.EmitIntValue(LineSec.second.size(), 4); |
| 684 | for (const auto &LineInfo : LineSec.second) { |
| 685 | Asm->EmitLabelReference(LineInfo.Label, 4); |
| 686 | OS.EmitIntValue(LineInfo.FileNameOff, 4); |
| 687 | OS.EmitIntValue(LineInfo.LineOff, 4); |
| 688 | OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " + |
| 689 | std::to_string(LineInfo.ColumnNum)); |
| 690 | OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | void BTFDebug::beginFunctionImpl(const MachineFunction *MF) { |
| 696 | auto *SP = MF->getFunction().getSubprogram(); |
| 697 | auto *Unit = SP->getUnit(); |
| 698 | |
| 699 | if (Unit->getEmissionKind() == DICompileUnit::NoDebug) { |
| 700 | SkipInstruction = true; |
| 701 | return; |
| 702 | } |
| 703 | SkipInstruction = false; |
| 704 | |
| 705 | // Collect all types locally referenced in this function. |
| 706 | // Use RetainedNodes so we can collect all argument names |
| 707 | // even if the argument is not used. |
| 708 | std::unordered_map<uint32_t, StringRef> FuncArgNames; |
| 709 | for (const DINode *DN : SP->getRetainedNodes()) { |
| 710 | if (const auto *DV = dyn_cast<DILocalVariable>(DN)) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 711 | // Collect function arguments for subprogram func type. |
| 712 | uint32_t Arg = DV->getArg(); |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 713 | if (Arg) { |
| 714 | visitTypeEntry(DV->getType().resolve()); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 715 | FuncArgNames[Arg] = DV->getName(); |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 716 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
| 720 | // Construct subprogram func proto type. |
| 721 | uint32_t ProtoTypeId; |
| 722 | visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId); |
| 723 | |
| 724 | // Construct subprogram func type |
| 725 | auto FuncTypeEntry = |
| 726 | llvm::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId); |
| 727 | uint32_t FuncTypeId = addType(std::move(FuncTypeEntry)); |
| 728 | |
| 729 | // Construct funcinfo and the first lineinfo for the function. |
| 730 | MCSymbol *FuncLabel = Asm->getFunctionBegin(); |
| 731 | BTFFuncInfo FuncInfo; |
| 732 | FuncInfo.Label = FuncLabel; |
| 733 | FuncInfo.TypeId = FuncTypeId; |
| 734 | if (FuncLabel->isInSection()) { |
| 735 | MCSection &Section = FuncLabel->getSection(); |
| 736 | const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section); |
| 737 | assert(SectionELF && "Null section for Function Label"); |
| 738 | SecNameOff = addString(SectionELF->getSectionName()); |
| 739 | } else { |
| 740 | SecNameOff = addString(".text"); |
| 741 | } |
| 742 | FuncInfoTable[SecNameOff].push_back(FuncInfo); |
| 743 | } |
| 744 | |
| 745 | void BTFDebug::endFunctionImpl(const MachineFunction *MF) { |
| 746 | SkipInstruction = false; |
| 747 | LineInfoGenerated = false; |
| 748 | SecNameOff = 0; |
| 749 | } |
| 750 | |
| 751 | void BTFDebug::beginInstruction(const MachineInstr *MI) { |
| 752 | DebugHandlerBase::beginInstruction(MI); |
| 753 | |
| 754 | if (SkipInstruction || MI->isMetaInstruction() || |
| 755 | MI->getFlag(MachineInstr::FrameSetup)) |
| 756 | return; |
| 757 | |
| 758 | if (MI->isInlineAsm()) { |
| 759 | // Count the number of register definitions to find the asm string. |
| 760 | unsigned NumDefs = 0; |
| 761 | for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); |
| 762 | ++NumDefs) |
| 763 | ; |
| 764 | |
| 765 | // Skip this inline asm instruction if the asmstr is empty. |
| 766 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
| 767 | if (AsmStr[0] == 0) |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | // Skip this instruction if no DebugLoc or the DebugLoc |
| 772 | // is the same as the previous instruction. |
| 773 | const DebugLoc &DL = MI->getDebugLoc(); |
| 774 | if (!DL || PrevInstLoc == DL) { |
| 775 | // This instruction will be skipped, no LineInfo has |
| 776 | // been generated, construct one based on function signature. |
| 777 | if (LineInfoGenerated == false) { |
| 778 | auto *S = MI->getMF()->getFunction().getSubprogram(); |
| 779 | MCSymbol *FuncLabel = Asm->getFunctionBegin(); |
| 780 | constructLineInfo(S, FuncLabel, S->getLine(), 0); |
| 781 | LineInfoGenerated = true; |
| 782 | } |
| 783 | |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | // Create a temporary label to remember the insn for lineinfo. |
| 788 | MCSymbol *LineSym = OS.getContext().createTempSymbol(); |
| 789 | OS.EmitLabel(LineSym); |
| 790 | |
| 791 | // Construct the lineinfo. |
| 792 | auto SP = DL.get()->getScope()->getSubprogram(); |
| 793 | constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol()); |
| 794 | |
| 795 | LineInfoGenerated = true; |
| 796 | PrevInstLoc = DL; |
| 797 | } |
| 798 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 799 | void BTFDebug::processGlobals() { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 800 | // Collect all types referenced by globals. |
| 801 | const Module *M = MMI->getModule(); |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 802 | for (const GlobalVariable &Global : M->globals()) { |
| 803 | // Ignore external globals for now. |
Yonghong Song | 44ed286 | 2019-03-15 17:39:10 +0000 | [diff] [blame] | 804 | if (!Global.hasInitializer() && Global.hasExternalLinkage()) |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 805 | continue; |
| 806 | |
| 807 | SmallVector<DIGlobalVariableExpression *, 1> GVs; |
| 808 | Global.getDebugInfo(GVs); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 809 | uint32_t GVTypeId = 0; |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 810 | for (auto *GVE : GVs) { |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 811 | visitTypeEntry(GVE->getVariable()->getType().resolve(), GVTypeId); |
| 812 | break; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 813 | } |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 814 | |
| 815 | // Only support the following globals: |
| 816 | // . static variables |
| 817 | // . non-static global variables with section attributes |
| 818 | // Essentially means: |
| 819 | // . .bcc/.data/.rodata DataSec entities only contain static data |
| 820 | // . Other DataSec entities contain static or initialized global data. |
| 821 | // Initialized global data are mostly used for finding map key/value type |
| 822 | // id's. Whether DataSec is readonly or not can be found from |
| 823 | // corresponding ELF section flags. |
| 824 | auto Linkage = Global.getLinkage(); |
| 825 | if (Linkage != GlobalValue::InternalLinkage && |
| 826 | (Linkage != GlobalValue::ExternalLinkage || !Global.hasSection())) |
| 827 | continue; |
| 828 | |
| 829 | uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage |
| 830 | ? BTF::VAR_GLOBAL_ALLOCATED |
| 831 | : BTF::VAR_STATIC; |
| 832 | auto VarEntry = |
| 833 | llvm::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo); |
| 834 | uint32_t VarId = addType(std::move(VarEntry)); |
| 835 | |
| 836 | // Decide the section name. |
| 837 | std::string SecName; |
| 838 | if (Global.hasSection()) { |
| 839 | SecName = Global.getSection().str(); |
| 840 | } else { |
| 841 | // data, bss, or readonly sections |
| 842 | if (Global.isConstant()) |
| 843 | SecName += ".rodata"; |
| 844 | else |
| 845 | SecName += Global.getInitializer()->isZeroValue() ? ".bss" : ".data"; |
| 846 | } |
| 847 | |
| 848 | // Find or create a DataSec |
| 849 | if (DataSecEntries.find(SecName) == DataSecEntries.end()) { |
| 850 | DataSecEntries[SecName] = llvm::make_unique<BTFKindDataSec>(Asm, SecName); |
| 851 | } |
| 852 | |
| 853 | // Calculate symbol size |
| 854 | const DataLayout &DL = Global.getParent()->getDataLayout(); |
| 855 | uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType()); |
| 856 | |
| 857 | DataSecEntries[SecName]->addVar(VarId, Asm->getSymbol(&Global), Size); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame^] | 860 | for (auto &DataSec : DataSecEntries) |
| 861 | addType(std::move(DataSec.second)); |
| 862 | } |
| 863 | |
| 864 | void BTFDebug::endModule() { |
| 865 | // Collect all global types/variables. |
| 866 | processGlobals(); |
| 867 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 868 | // Complete BTF type cross refereences. |
| 869 | for (const auto &TypeEntry : TypeEntries) |
| 870 | TypeEntry->completeType(*this); |
| 871 | |
| 872 | // Emit BTF sections. |
| 873 | emitBTFSection(); |
| 874 | emitBTFExtSection(); |
| 875 | } |