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" |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 14 | #include "BPF.h" |
| 15 | #include "BPFCORE.h" |
| 16 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 17 | #include "llvm/BinaryFormat/ELF.h" |
| 18 | #include "llvm/CodeGen/AsmPrinter.h" |
| 19 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCObjectFileInfo.h" |
| 22 | #include "llvm/MC/MCSectionELF.h" |
| 23 | #include "llvm/MC/MCStreamer.h" |
Fangrui Song | 83db887 | 2019-04-02 16:15:46 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LineIterator.h" |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | static const char *BTFKindStr[] = { |
| 29 | #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME, |
| 30 | #include "BTF.def" |
| 31 | }; |
| 32 | |
| 33 | /// Emit a BTF common type. |
| 34 | void BTFTypeBase::emitType(MCStreamer &OS) { |
| 35 | OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) + |
| 36 | ")"); |
| 37 | OS.EmitIntValue(BTFType.NameOff, 4); |
| 38 | OS.AddComment("0x" + Twine::utohexstr(BTFType.Info)); |
| 39 | OS.EmitIntValue(BTFType.Info, 4); |
| 40 | OS.EmitIntValue(BTFType.Size, 4); |
| 41 | } |
| 42 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 43 | BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag, |
| 44 | bool NeedsFixup) |
| 45 | : DTy(DTy), NeedsFixup(NeedsFixup) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 46 | switch (Tag) { |
| 47 | case dwarf::DW_TAG_pointer_type: |
| 48 | Kind = BTF::BTF_KIND_PTR; |
| 49 | break; |
| 50 | case dwarf::DW_TAG_const_type: |
| 51 | Kind = BTF::BTF_KIND_CONST; |
| 52 | break; |
| 53 | case dwarf::DW_TAG_volatile_type: |
| 54 | Kind = BTF::BTF_KIND_VOLATILE; |
| 55 | break; |
| 56 | case dwarf::DW_TAG_typedef: |
| 57 | Kind = BTF::BTF_KIND_TYPEDEF; |
| 58 | break; |
| 59 | case dwarf::DW_TAG_restrict_type: |
| 60 | Kind = BTF::BTF_KIND_RESTRICT; |
| 61 | break; |
| 62 | default: |
| 63 | llvm_unreachable("Unknown DIDerivedType Tag"); |
| 64 | } |
| 65 | BTFType.Info = Kind << 24; |
| 66 | } |
| 67 | |
| 68 | void BTFTypeDerived::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 69 | if (IsCompleted) |
| 70 | return; |
| 71 | IsCompleted = true; |
| 72 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 73 | BTFType.NameOff = BDebug.addString(DTy->getName()); |
| 74 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 75 | if (NeedsFixup) |
| 76 | return; |
| 77 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 78 | // The base type for PTR/CONST/VOLATILE could be void. |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 79 | const DIType *ResolvedType = DTy->getBaseType(); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 80 | if (!ResolvedType) { |
| 81 | assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST || |
| 82 | Kind == BTF::BTF_KIND_VOLATILE) && |
| 83 | "Invalid null basetype"); |
| 84 | BTFType.Type = 0; |
| 85 | } else { |
| 86 | BTFType.Type = BDebug.getTypeId(ResolvedType); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); } |
| 91 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 92 | void BTFTypeDerived::setPointeeType(uint32_t PointeeType) { |
| 93 | BTFType.Type = PointeeType; |
| 94 | } |
| 95 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 96 | /// Represent a struct/union forward declaration. |
| 97 | BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) { |
| 98 | Kind = BTF::BTF_KIND_FWD; |
| 99 | BTFType.Info = IsUnion << 31 | Kind << 24; |
| 100 | BTFType.Type = 0; |
| 101 | } |
| 102 | |
| 103 | void BTFTypeFwd::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 104 | if (IsCompleted) |
| 105 | return; |
| 106 | IsCompleted = true; |
| 107 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 108 | BTFType.NameOff = BDebug.addString(Name); |
| 109 | } |
| 110 | |
| 111 | void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); } |
| 112 | |
| 113 | BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, |
| 114 | uint32_t OffsetInBits, StringRef TypeName) |
| 115 | : Name(TypeName) { |
| 116 | // Translate IR int encoding to BTF int encoding. |
| 117 | uint8_t BTFEncoding; |
| 118 | switch (Encoding) { |
| 119 | case dwarf::DW_ATE_boolean: |
| 120 | BTFEncoding = BTF::INT_BOOL; |
| 121 | break; |
| 122 | case dwarf::DW_ATE_signed: |
| 123 | case dwarf::DW_ATE_signed_char: |
| 124 | BTFEncoding = BTF::INT_SIGNED; |
| 125 | break; |
| 126 | case dwarf::DW_ATE_unsigned: |
| 127 | case dwarf::DW_ATE_unsigned_char: |
| 128 | BTFEncoding = 0; |
| 129 | break; |
| 130 | default: |
| 131 | llvm_unreachable("Unknown BTFTypeInt Encoding"); |
| 132 | } |
| 133 | |
| 134 | Kind = BTF::BTF_KIND_INT; |
| 135 | BTFType.Info = Kind << 24; |
| 136 | BTFType.Size = roundupToBytes(SizeInBits); |
| 137 | IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits; |
| 138 | } |
| 139 | |
| 140 | void BTFTypeInt::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 141 | if (IsCompleted) |
| 142 | return; |
| 143 | IsCompleted = true; |
| 144 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 145 | BTFType.NameOff = BDebug.addString(Name); |
| 146 | } |
| 147 | |
| 148 | void BTFTypeInt::emitType(MCStreamer &OS) { |
| 149 | BTFTypeBase::emitType(OS); |
| 150 | OS.AddComment("0x" + Twine::utohexstr(IntVal)); |
| 151 | OS.EmitIntValue(IntVal, 4); |
| 152 | } |
| 153 | |
| 154 | BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) { |
| 155 | Kind = BTF::BTF_KIND_ENUM; |
| 156 | BTFType.Info = Kind << 24 | VLen; |
| 157 | BTFType.Size = roundupToBytes(ETy->getSizeInBits()); |
| 158 | } |
| 159 | |
| 160 | void BTFTypeEnum::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 161 | if (IsCompleted) |
| 162 | return; |
| 163 | IsCompleted = true; |
| 164 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 165 | BTFType.NameOff = BDebug.addString(ETy->getName()); |
| 166 | |
| 167 | DINodeArray Elements = ETy->getElements(); |
| 168 | for (const auto Element : Elements) { |
| 169 | const auto *Enum = cast<DIEnumerator>(Element); |
| 170 | |
| 171 | struct BTF::BTFEnum BTFEnum; |
| 172 | BTFEnum.NameOff = BDebug.addString(Enum->getName()); |
| 173 | // BTF enum value is 32bit, enforce it. |
| 174 | BTFEnum.Val = static_cast<uint32_t>(Enum->getValue()); |
| 175 | EnumValues.push_back(BTFEnum); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void BTFTypeEnum::emitType(MCStreamer &OS) { |
| 180 | BTFTypeBase::emitType(OS); |
| 181 | for (const auto &Enum : EnumValues) { |
| 182 | OS.EmitIntValue(Enum.NameOff, 4); |
| 183 | OS.EmitIntValue(Enum.Val, 4); |
| 184 | } |
| 185 | } |
| 186 | |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 187 | BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 188 | Kind = BTF::BTF_KIND_ARRAY; |
Yonghong Song | 360a4e2c | 2019-03-28 21:59:49 +0000 | [diff] [blame] | 189 | BTFType.NameOff = 0; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 190 | BTFType.Info = Kind << 24; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 191 | BTFType.Size = 0; |
| 192 | |
Yonghong Song | 360a4e2c | 2019-03-28 21:59:49 +0000 | [diff] [blame] | 193 | ArrayInfo.ElemType = ElemTypeId; |
| 194 | ArrayInfo.Nelems = NumElems; |
| 195 | } |
| 196 | |
| 197 | /// Represent a BTF array. |
| 198 | void BTFTypeArray::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 199 | if (IsCompleted) |
| 200 | return; |
| 201 | IsCompleted = true; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 202 | |
| 203 | // The IR does not really have a type for the index. |
| 204 | // A special type for array index should have been |
| 205 | // created during initial type traversal. Just |
| 206 | // retrieve that type id. |
| 207 | ArrayInfo.IndexType = BDebug.getArrayIndexTypeId(); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void BTFTypeArray::emitType(MCStreamer &OS) { |
| 211 | BTFTypeBase::emitType(OS); |
| 212 | OS.EmitIntValue(ArrayInfo.ElemType, 4); |
| 213 | OS.EmitIntValue(ArrayInfo.IndexType, 4); |
| 214 | OS.EmitIntValue(ArrayInfo.Nelems, 4); |
| 215 | } |
| 216 | |
| 217 | /// Represent either a struct or a union. |
| 218 | BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct, |
| 219 | bool HasBitField, uint32_t Vlen) |
| 220 | : STy(STy), HasBitField(HasBitField) { |
| 221 | Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION; |
| 222 | BTFType.Size = roundupToBytes(STy->getSizeInBits()); |
| 223 | BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen; |
| 224 | } |
| 225 | |
| 226 | void BTFTypeStruct::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 227 | if (IsCompleted) |
| 228 | return; |
| 229 | IsCompleted = true; |
| 230 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 231 | BTFType.NameOff = BDebug.addString(STy->getName()); |
| 232 | |
| 233 | // Add struct/union members. |
| 234 | const DINodeArray Elements = STy->getElements(); |
| 235 | for (const auto *Element : Elements) { |
| 236 | struct BTF::BTFMember BTFMember; |
| 237 | const auto *DDTy = cast<DIDerivedType>(Element); |
| 238 | |
| 239 | BTFMember.NameOff = BDebug.addString(DDTy->getName()); |
| 240 | if (HasBitField) { |
| 241 | uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0; |
| 242 | BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits(); |
| 243 | } else { |
| 244 | BTFMember.Offset = DDTy->getOffsetInBits(); |
| 245 | } |
Yonghong Song | 329abf2 | 2019-07-25 21:47:27 +0000 | [diff] [blame] | 246 | const auto *BaseTy = DDTy->getBaseType(); |
| 247 | BTFMember.Type = BDebug.getTypeId(BaseTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 248 | Members.push_back(BTFMember); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void BTFTypeStruct::emitType(MCStreamer &OS) { |
| 253 | BTFTypeBase::emitType(OS); |
| 254 | for (const auto &Member : Members) { |
| 255 | OS.EmitIntValue(Member.NameOff, 4); |
| 256 | OS.EmitIntValue(Member.Type, 4); |
| 257 | OS.AddComment("0x" + Twine::utohexstr(Member.Offset)); |
| 258 | OS.EmitIntValue(Member.Offset, 4); |
| 259 | } |
| 260 | } |
| 261 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 262 | std::string BTFTypeStruct::getName() { return std::string(STy->getName()); } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 263 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 264 | /// The Func kind represents both subprogram and pointee of function |
| 265 | /// pointers. If the FuncName is empty, it represents a pointee of function |
| 266 | /// pointer. Otherwise, it represents a subprogram. The func arg names |
| 267 | /// are empty for pointee of function pointer case, and are valid names |
| 268 | /// for subprogram. |
| 269 | BTFTypeFuncProto::BTFTypeFuncProto( |
| 270 | const DISubroutineType *STy, uint32_t VLen, |
| 271 | const std::unordered_map<uint32_t, StringRef> &FuncArgNames) |
| 272 | : STy(STy), FuncArgNames(FuncArgNames) { |
| 273 | Kind = BTF::BTF_KIND_FUNC_PROTO; |
| 274 | BTFType.Info = (Kind << 24) | VLen; |
| 275 | } |
| 276 | |
| 277 | void BTFTypeFuncProto::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 278 | if (IsCompleted) |
| 279 | return; |
| 280 | IsCompleted = true; |
| 281 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 282 | DITypeRefArray Elements = STy->getTypeArray(); |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 283 | auto RetType = Elements[0]; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 284 | BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0; |
| 285 | BTFType.NameOff = 0; |
| 286 | |
| 287 | // For null parameter which is typically the last one |
| 288 | // to represent the vararg, encode the NameOff/Type to be 0. |
| 289 | for (unsigned I = 1, N = Elements.size(); I < N; ++I) { |
| 290 | struct BTF::BTFParam Param; |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 291 | auto Element = Elements[I]; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 292 | if (Element) { |
| 293 | Param.NameOff = BDebug.addString(FuncArgNames[I]); |
| 294 | Param.Type = BDebug.getTypeId(Element); |
| 295 | } else { |
| 296 | Param.NameOff = 0; |
| 297 | Param.Type = 0; |
| 298 | } |
| 299 | Parameters.push_back(Param); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void BTFTypeFuncProto::emitType(MCStreamer &OS) { |
| 304 | BTFTypeBase::emitType(OS); |
| 305 | for (const auto &Param : Parameters) { |
| 306 | OS.EmitIntValue(Param.NameOff, 4); |
| 307 | OS.EmitIntValue(Param.Type, 4); |
| 308 | } |
| 309 | } |
| 310 | |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 311 | BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId, |
| 312 | uint32_t Scope) |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 313 | : Name(FuncName) { |
| 314 | Kind = BTF::BTF_KIND_FUNC; |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 315 | BTFType.Info = (Kind << 24) | Scope; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 316 | BTFType.Type = ProtoTypeId; |
| 317 | } |
| 318 | |
| 319 | void BTFTypeFunc::completeType(BTFDebug &BDebug) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 320 | if (IsCompleted) |
| 321 | return; |
| 322 | IsCompleted = true; |
| 323 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 324 | BTFType.NameOff = BDebug.addString(Name); |
| 325 | } |
| 326 | |
| 327 | void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); } |
| 328 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 329 | BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo) |
| 330 | : Name(VarName) { |
| 331 | Kind = BTF::BTF_KIND_VAR; |
| 332 | BTFType.Info = Kind << 24; |
| 333 | BTFType.Type = TypeId; |
| 334 | Info = VarInfo; |
| 335 | } |
| 336 | |
| 337 | void BTFKindVar::completeType(BTFDebug &BDebug) { |
| 338 | BTFType.NameOff = BDebug.addString(Name); |
| 339 | } |
| 340 | |
| 341 | void BTFKindVar::emitType(MCStreamer &OS) { |
| 342 | BTFTypeBase::emitType(OS); |
| 343 | OS.EmitIntValue(Info, 4); |
| 344 | } |
| 345 | |
| 346 | BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName) |
| 347 | : Asm(AsmPrt), Name(SecName) { |
| 348 | Kind = BTF::BTF_KIND_DATASEC; |
| 349 | BTFType.Info = Kind << 24; |
| 350 | BTFType.Size = 0; |
| 351 | } |
| 352 | |
| 353 | void BTFKindDataSec::completeType(BTFDebug &BDebug) { |
| 354 | BTFType.NameOff = BDebug.addString(Name); |
| 355 | BTFType.Info |= Vars.size(); |
| 356 | } |
| 357 | |
| 358 | void BTFKindDataSec::emitType(MCStreamer &OS) { |
| 359 | BTFTypeBase::emitType(OS); |
| 360 | |
| 361 | for (const auto &V : Vars) { |
| 362 | OS.EmitIntValue(std::get<0>(V), 4); |
Fangrui Song | 0bc77a0 | 2020-02-13 13:26:21 -0800 | [diff] [blame] | 363 | Asm->emitLabelReference(std::get<1>(V), 4); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 364 | OS.EmitIntValue(std::get<2>(V), 4); |
| 365 | } |
| 366 | } |
| 367 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 368 | uint32_t BTFStringTable::addString(StringRef S) { |
| 369 | // Check whether the string already exists. |
| 370 | for (auto &OffsetM : OffsetToIdMap) { |
| 371 | if (Table[OffsetM.second] == S) |
| 372 | return OffsetM.first; |
| 373 | } |
| 374 | // Not find, add to the string table. |
| 375 | uint32_t Offset = Size; |
| 376 | OffsetToIdMap[Offset] = Table.size(); |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 377 | Table.push_back(std::string(S)); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 378 | Size += S.size() + 1; |
| 379 | return Offset; |
| 380 | } |
| 381 | |
| 382 | BTFDebug::BTFDebug(AsmPrinter *AP) |
| 383 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false), |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 384 | LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0), |
| 385 | MapDefNotCollected(true) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 386 | addString("\0"); |
| 387 | } |
| 388 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 389 | uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry, |
| 390 | const DIType *Ty) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 391 | TypeEntry->setId(TypeEntries.size() + 1); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 392 | uint32_t Id = TypeEntry->getId(); |
| 393 | DIToIdMap[Ty] = Id; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 394 | TypeEntries.push_back(std::move(TypeEntry)); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 395 | return Id; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) { |
| 399 | TypeEntry->setId(TypeEntries.size() + 1); |
| 400 | uint32_t Id = TypeEntry->getId(); |
| 401 | TypeEntries.push_back(std::move(TypeEntry)); |
| 402 | return Id; |
| 403 | } |
| 404 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 405 | void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 406 | // Only int types are supported in BTF. |
| 407 | uint32_t Encoding = BTy->getEncoding(); |
| 408 | if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed && |
| 409 | Encoding != dwarf::DW_ATE_signed_char && |
| 410 | Encoding != dwarf::DW_ATE_unsigned && |
| 411 | Encoding != dwarf::DW_ATE_unsigned_char) |
| 412 | return; |
| 413 | |
| 414 | // Create a BTF type instance for this DIBasicType and put it into |
| 415 | // DIToIdMap for cross-type reference check. |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 416 | auto TypeEntry = std::make_unique<BTFTypeInt>( |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 417 | Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName()); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 418 | TypeId = addType(std::move(TypeEntry), BTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /// Handle subprogram or subroutine types. |
| 422 | void BTFDebug::visitSubroutineType( |
| 423 | const DISubroutineType *STy, bool ForSubprog, |
| 424 | const std::unordered_map<uint32_t, StringRef> &FuncArgNames, |
| 425 | uint32_t &TypeId) { |
| 426 | DITypeRefArray Elements = STy->getTypeArray(); |
| 427 | uint32_t VLen = Elements.size() - 1; |
| 428 | if (VLen > BTF::MAX_VLEN) |
| 429 | return; |
| 430 | |
| 431 | // Subprogram has a valid non-zero-length name, and the pointee of |
| 432 | // a function pointer has an empty name. The subprogram type will |
| 433 | // not be added to DIToIdMap as it should not be referenced by |
| 434 | // any other types. |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 435 | auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 436 | if (ForSubprog) |
| 437 | TypeId = addType(std::move(TypeEntry)); // For subprogram |
| 438 | else |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 439 | TypeId = addType(std::move(TypeEntry), STy); // For func ptr |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 440 | |
| 441 | // Visit return type and func arg types. |
| 442 | for (const auto Element : Elements) { |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 443 | visitTypeEntry(Element); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | /// Handle structure/union types. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 448 | void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct, |
| 449 | uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 450 | const DINodeArray Elements = CTy->getElements(); |
| 451 | uint32_t VLen = Elements.size(); |
| 452 | if (VLen > BTF::MAX_VLEN) |
| 453 | return; |
| 454 | |
| 455 | // Check whether we have any bitfield members or not |
| 456 | bool HasBitField = false; |
| 457 | for (const auto *Element : Elements) { |
| 458 | auto E = cast<DIDerivedType>(Element); |
| 459 | if (E->isBitField()) { |
| 460 | HasBitField = true; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | auto TypeEntry = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 466 | std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 467 | StructTypes.push_back(TypeEntry.get()); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 468 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 469 | |
| 470 | // Visit all struct members. |
| 471 | for (const auto *Element : Elements) |
| 472 | visitTypeEntry(cast<DIDerivedType>(Element)); |
| 473 | } |
| 474 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 475 | void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) { |
Yonghong Song | 360a4e2c | 2019-03-28 21:59:49 +0000 | [diff] [blame] | 476 | // Visit array element type. |
Yonghong Song | 1487bf6 | 2019-09-24 22:38:43 +0000 | [diff] [blame] | 477 | uint32_t ElemTypeId; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 478 | const DIType *ElemType = CTy->getBaseType(); |
| 479 | visitTypeEntry(ElemType, ElemTypeId, false, false); |
Yonghong Song | 329abf2 | 2019-07-25 21:47:27 +0000 | [diff] [blame] | 480 | |
Yonghong Song | 1487bf6 | 2019-09-24 22:38:43 +0000 | [diff] [blame] | 481 | // Visit array dimensions. |
| 482 | DINodeArray Elements = CTy->getElements(); |
| 483 | for (int I = Elements.size() - 1; I >= 0; --I) { |
| 484 | if (auto *Element = dyn_cast_or_null<DINode>(Elements[I])) |
| 485 | if (Element->getTag() == dwarf::DW_TAG_subrange_type) { |
| 486 | const DISubrange *SR = cast<DISubrange>(Element); |
| 487 | auto *CI = SR->getCount().dyn_cast<ConstantInt *>(); |
| 488 | int64_t Count = CI->getSExtValue(); |
Yonghong Song | 360a4e2c | 2019-03-28 21:59:49 +0000 | [diff] [blame] | 489 | |
Yonghong Song | 1487bf6 | 2019-09-24 22:38:43 +0000 | [diff] [blame] | 490 | // For struct s { int b; char c[]; }, the c[] will be represented |
| 491 | // as an array with Count = -1. |
| 492 | auto TypeEntry = |
| 493 | std::make_unique<BTFTypeArray>(ElemTypeId, |
| 494 | Count >= 0 ? Count : 0); |
| 495 | if (I == 0) |
| 496 | ElemTypeId = addType(std::move(TypeEntry), CTy); |
| 497 | else |
| 498 | ElemTypeId = addType(std::move(TypeEntry)); |
| 499 | } |
Yonghong Song | 360a4e2c | 2019-03-28 21:59:49 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // The array TypeId is the type id of the outermost dimension. |
| 503 | TypeId = ElemTypeId; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 504 | |
| 505 | // The IR does not have a type for array index while BTF wants one. |
| 506 | // So create an array index type if there is none. |
| 507 | if (!ArrayIndexTypeId) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 508 | auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32, |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 509 | 0, "__ARRAY_SIZE_TYPE__"); |
| 510 | ArrayIndexTypeId = addType(std::move(TypeEntry)); |
| 511 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 514 | void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 515 | DINodeArray Elements = CTy->getElements(); |
| 516 | uint32_t VLen = Elements.size(); |
| 517 | if (VLen > BTF::MAX_VLEN) |
| 518 | return; |
| 519 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 520 | auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 521 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 522 | // No need to visit base type as BTF does not encode it. |
| 523 | } |
| 524 | |
| 525 | /// Handle structure/union forward declarations. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 526 | void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion, |
| 527 | uint32_t &TypeId) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 528 | auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 529 | TypeId = addType(std::move(TypeEntry), CTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /// Handle structure, union, array and enumeration types. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 533 | void BTFDebug::visitCompositeType(const DICompositeType *CTy, |
| 534 | uint32_t &TypeId) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 535 | auto Tag = CTy->getTag(); |
| 536 | if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { |
| 537 | // Handle forward declaration differently as it does not have members. |
| 538 | if (CTy->isForwardDecl()) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 539 | visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 540 | else |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 541 | visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 542 | } else if (Tag == dwarf::DW_TAG_array_type) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 543 | visitArrayType(CTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 544 | else if (Tag == dwarf::DW_TAG_enumeration_type) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 545 | visitEnumType(CTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /// Handle pointer, typedef, const, volatile, restrict and member types. |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 549 | void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId, |
| 550 | bool CheckPointer, bool SeenPointer) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 551 | unsigned Tag = DTy->getTag(); |
| 552 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 553 | /// Try to avoid chasing pointees, esp. structure pointees which may |
| 554 | /// unnecessary bring in a lot of types. |
| 555 | if (CheckPointer && !SeenPointer) { |
| 556 | SeenPointer = Tag == dwarf::DW_TAG_pointer_type; |
| 557 | } |
| 558 | |
| 559 | if (CheckPointer && SeenPointer) { |
| 560 | const DIType *Base = DTy->getBaseType(); |
| 561 | if (Base) { |
| 562 | if (const auto *CTy = dyn_cast<DICompositeType>(Base)) { |
| 563 | auto CTag = CTy->getTag(); |
| 564 | if ((CTag == dwarf::DW_TAG_structure_type || |
| 565 | CTag == dwarf::DW_TAG_union_type) && |
| 566 | !CTy->isForwardDecl()) { |
| 567 | /// Find a candidate, generate a fixup. Later on the struct/union |
| 568 | /// pointee type will be replaced with either a real type or |
| 569 | /// a forward declaration. |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 570 | auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 571 | auto &Fixup = FixupDerivedTypes[CTy->getName()]; |
| 572 | Fixup.first = CTag == dwarf::DW_TAG_union_type; |
| 573 | Fixup.second.push_back(TypeEntry.get()); |
| 574 | TypeId = addType(std::move(TypeEntry), DTy); |
| 575 | return; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 581 | if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef || |
| 582 | Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type || |
| 583 | Tag == dwarf::DW_TAG_restrict_type) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 584 | auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 585 | TypeId = addType(std::move(TypeEntry), DTy); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 586 | } else if (Tag != dwarf::DW_TAG_member) { |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | // Visit base type of pointer, typedef, const, volatile, restrict or |
| 591 | // struct/union member. |
Yonghong Song | ded9a44 | 2019-03-22 01:30:50 +0000 | [diff] [blame] | 592 | uint32_t TempTypeId = 0; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 593 | if (Tag == dwarf::DW_TAG_member) |
| 594 | visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false); |
| 595 | else |
| 596 | visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 599 | void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId, |
| 600 | bool CheckPointer, bool SeenPointer) { |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 601 | if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) { |
| 602 | TypeId = DIToIdMap[Ty]; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 603 | return; |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 604 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 605 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 606 | if (const auto *BTy = dyn_cast<DIBasicType>(Ty)) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 607 | visitBasicType(BTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 608 | else if (const auto *STy = dyn_cast<DISubroutineType>(Ty)) |
| 609 | visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(), |
| 610 | TypeId); |
| 611 | else if (const auto *CTy = dyn_cast<DICompositeType>(Ty)) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 612 | visitCompositeType(CTy, TypeId); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 613 | else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 614 | visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 615 | else |
| 616 | llvm_unreachable("Unknown DIType"); |
| 617 | } |
| 618 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 619 | void BTFDebug::visitTypeEntry(const DIType *Ty) { |
| 620 | uint32_t TypeId; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 621 | visitTypeEntry(Ty, TypeId, false, false); |
| 622 | } |
| 623 | |
| 624 | void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) { |
| 625 | if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) { |
| 626 | TypeId = DIToIdMap[Ty]; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | // MapDef type is a struct type |
| 631 | const auto *CTy = dyn_cast<DICompositeType>(Ty); |
| 632 | if (!CTy) |
| 633 | return; |
| 634 | |
| 635 | auto Tag = CTy->getTag(); |
| 636 | if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl()) |
| 637 | return; |
| 638 | |
| 639 | // Record this type |
| 640 | const DINodeArray Elements = CTy->getElements(); |
| 641 | bool HasBitField = false; |
| 642 | for (const auto *Element : Elements) { |
| 643 | auto E = cast<DIDerivedType>(Element); |
| 644 | if (E->isBitField()) { |
| 645 | HasBitField = true; |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | auto TypeEntry = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 651 | std::make_unique<BTFTypeStruct>(CTy, true, HasBitField, Elements.size()); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 652 | StructTypes.push_back(TypeEntry.get()); |
| 653 | TypeId = addType(std::move(TypeEntry), CTy); |
| 654 | |
| 655 | // Visit all struct members |
| 656 | for (const auto *Element : Elements) { |
| 657 | const auto *MemberType = cast<DIDerivedType>(Element); |
| 658 | visitTypeEntry(MemberType->getBaseType()); |
| 659 | } |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 662 | /// Read file contents from the actual file or from the source |
| 663 | std::string BTFDebug::populateFileContent(const DISubprogram *SP) { |
| 664 | auto File = SP->getFile(); |
| 665 | std::string FileName; |
| 666 | |
Yonghong Song | fa36540 | 2019-02-02 05:54:59 +0000 | [diff] [blame] | 667 | if (!File->getFilename().startswith("/") && File->getDirectory().size()) |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 668 | FileName = File->getDirectory().str() + "/" + File->getFilename().str(); |
| 669 | else |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 670 | FileName = std::string(File->getFilename()); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 671 | |
| 672 | // No need to populate the contends if it has been populated! |
| 673 | if (FileContent.find(FileName) != FileContent.end()) |
| 674 | return FileName; |
| 675 | |
| 676 | std::vector<std::string> Content; |
| 677 | std::string Line; |
| 678 | Content.push_back(Line); // Line 0 for empty string |
| 679 | |
Fangrui Song | 83db887 | 2019-04-02 16:15:46 +0000 | [diff] [blame] | 680 | std::unique_ptr<MemoryBuffer> Buf; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 681 | auto Source = File->getSource(); |
Fangrui Song | 83db887 | 2019-04-02 16:15:46 +0000 | [diff] [blame] | 682 | if (Source) |
| 683 | Buf = MemoryBuffer::getMemBufferCopy(*Source); |
| 684 | else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = |
| 685 | MemoryBuffer::getFile(FileName)) |
| 686 | Buf = std::move(*BufOrErr); |
| 687 | if (Buf) |
| 688 | for (line_iterator I(*Buf, false), E; I != E; ++I) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 689 | Content.push_back(std::string(*I)); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 690 | |
| 691 | FileContent[FileName] = Content; |
| 692 | return FileName; |
| 693 | } |
| 694 | |
| 695 | void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label, |
| 696 | uint32_t Line, uint32_t Column) { |
| 697 | std::string FileName = populateFileContent(SP); |
| 698 | BTFLineInfo LineInfo; |
| 699 | |
| 700 | LineInfo.Label = Label; |
| 701 | LineInfo.FileNameOff = addString(FileName); |
| 702 | // If file content is not available, let LineOff = 0. |
| 703 | if (Line < FileContent[FileName].size()) |
| 704 | LineInfo.LineOff = addString(FileContent[FileName][Line]); |
| 705 | else |
| 706 | LineInfo.LineOff = 0; |
| 707 | LineInfo.LineNum = Line; |
| 708 | LineInfo.ColumnNum = Column; |
| 709 | LineInfoTable[SecNameOff].push_back(LineInfo); |
| 710 | } |
| 711 | |
| 712 | void BTFDebug::emitCommonHeader() { |
| 713 | OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC)); |
| 714 | OS.EmitIntValue(BTF::MAGIC, 2); |
| 715 | OS.EmitIntValue(BTF::VERSION, 1); |
| 716 | OS.EmitIntValue(0, 1); |
| 717 | } |
| 718 | |
| 719 | void BTFDebug::emitBTFSection() { |
Yonghong Song | d82247c | 2019-03-05 01:01:21 +0000 | [diff] [blame] | 720 | // Do not emit section if no types and only "" string. |
| 721 | if (!TypeEntries.size() && StringTable.getSize() == 1) |
| 722 | return; |
| 723 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 724 | MCContext &Ctx = OS.getContext(); |
| 725 | OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0)); |
| 726 | |
| 727 | // Emit header. |
| 728 | emitCommonHeader(); |
| 729 | OS.EmitIntValue(BTF::HeaderSize, 4); |
| 730 | |
| 731 | uint32_t TypeLen = 0, StrLen; |
| 732 | for (const auto &TypeEntry : TypeEntries) |
| 733 | TypeLen += TypeEntry->getSize(); |
| 734 | StrLen = StringTable.getSize(); |
| 735 | |
| 736 | OS.EmitIntValue(0, 4); |
| 737 | OS.EmitIntValue(TypeLen, 4); |
| 738 | OS.EmitIntValue(TypeLen, 4); |
| 739 | OS.EmitIntValue(StrLen, 4); |
| 740 | |
| 741 | // Emit type table. |
| 742 | for (const auto &TypeEntry : TypeEntries) |
| 743 | TypeEntry->emitType(OS); |
| 744 | |
| 745 | // Emit string table. |
| 746 | uint32_t StringOffset = 0; |
| 747 | for (const auto &S : StringTable.getTable()) { |
| 748 | OS.AddComment("string offset=" + std::to_string(StringOffset)); |
Fangrui Song | a55daa1 | 2020-02-14 18:16:24 -0800 | [diff] [blame^] | 749 | OS.emitBytes(S); |
| 750 | OS.emitBytes(StringRef("\0", 1)); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 751 | StringOffset += S.size() + 1; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | void BTFDebug::emitBTFExtSection() { |
Yonghong Song | d46a6a9 | 2019-10-10 15:33:09 +0000 | [diff] [blame] | 756 | // Do not emit section if empty FuncInfoTable and LineInfoTable |
| 757 | // and FieldRelocTable. |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 758 | if (!FuncInfoTable.size() && !LineInfoTable.size() && |
Yonghong Song | d46a6a9 | 2019-10-10 15:33:09 +0000 | [diff] [blame] | 759 | !FieldRelocTable.size()) |
Yonghong Song | d82247c | 2019-03-05 01:01:21 +0000 | [diff] [blame] | 760 | return; |
| 761 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 762 | MCContext &Ctx = OS.getContext(); |
| 763 | OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0)); |
| 764 | |
| 765 | // Emit header. |
| 766 | emitCommonHeader(); |
| 767 | OS.EmitIntValue(BTF::ExtHeaderSize, 4); |
| 768 | |
| 769 | // Account for FuncInfo/LineInfo record size as well. |
| 770 | uint32_t FuncLen = 4, LineLen = 4; |
Yonghong Song | d46a6a9 | 2019-10-10 15:33:09 +0000 | [diff] [blame] | 771 | // Do not account for optional FieldReloc. |
| 772 | uint32_t FieldRelocLen = 0; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 773 | for (const auto &FuncSec : FuncInfoTable) { |
| 774 | FuncLen += BTF::SecFuncInfoSize; |
| 775 | FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize; |
| 776 | } |
| 777 | for (const auto &LineSec : LineInfoTable) { |
| 778 | LineLen += BTF::SecLineInfoSize; |
| 779 | LineLen += LineSec.second.size() * BTF::BPFLineInfoSize; |
| 780 | } |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 781 | for (const auto &FieldRelocSec : FieldRelocTable) { |
| 782 | FieldRelocLen += BTF::SecFieldRelocSize; |
| 783 | FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 784 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 785 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 786 | if (FieldRelocLen) |
| 787 | FieldRelocLen += 4; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 788 | |
| 789 | OS.EmitIntValue(0, 4); |
| 790 | OS.EmitIntValue(FuncLen, 4); |
| 791 | OS.EmitIntValue(FuncLen, 4); |
| 792 | OS.EmitIntValue(LineLen, 4); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 793 | OS.EmitIntValue(FuncLen + LineLen, 4); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 794 | OS.EmitIntValue(FieldRelocLen, 4); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 795 | |
| 796 | // Emit func_info table. |
| 797 | OS.AddComment("FuncInfo"); |
| 798 | OS.EmitIntValue(BTF::BPFFuncInfoSize, 4); |
| 799 | for (const auto &FuncSec : FuncInfoTable) { |
| 800 | OS.AddComment("FuncInfo section string offset=" + |
| 801 | std::to_string(FuncSec.first)); |
| 802 | OS.EmitIntValue(FuncSec.first, 4); |
| 803 | OS.EmitIntValue(FuncSec.second.size(), 4); |
| 804 | for (const auto &FuncInfo : FuncSec.second) { |
Fangrui Song | 0bc77a0 | 2020-02-13 13:26:21 -0800 | [diff] [blame] | 805 | Asm->emitLabelReference(FuncInfo.Label, 4); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 806 | OS.EmitIntValue(FuncInfo.TypeId, 4); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | // Emit line_info table. |
| 811 | OS.AddComment("LineInfo"); |
| 812 | OS.EmitIntValue(BTF::BPFLineInfoSize, 4); |
| 813 | for (const auto &LineSec : LineInfoTable) { |
| 814 | OS.AddComment("LineInfo section string offset=" + |
| 815 | std::to_string(LineSec.first)); |
| 816 | OS.EmitIntValue(LineSec.first, 4); |
| 817 | OS.EmitIntValue(LineSec.second.size(), 4); |
| 818 | for (const auto &LineInfo : LineSec.second) { |
Fangrui Song | 0bc77a0 | 2020-02-13 13:26:21 -0800 | [diff] [blame] | 819 | Asm->emitLabelReference(LineInfo.Label, 4); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 820 | OS.EmitIntValue(LineInfo.FileNameOff, 4); |
| 821 | OS.EmitIntValue(LineInfo.LineOff, 4); |
| 822 | OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " + |
| 823 | std::to_string(LineInfo.ColumnNum)); |
| 824 | OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4); |
| 825 | } |
| 826 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 827 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 828 | // Emit field reloc table. |
| 829 | if (FieldRelocLen) { |
| 830 | OS.AddComment("FieldReloc"); |
| 831 | OS.EmitIntValue(BTF::BPFFieldRelocSize, 4); |
| 832 | for (const auto &FieldRelocSec : FieldRelocTable) { |
| 833 | OS.AddComment("Field reloc section string offset=" + |
| 834 | std::to_string(FieldRelocSec.first)); |
| 835 | OS.EmitIntValue(FieldRelocSec.first, 4); |
| 836 | OS.EmitIntValue(FieldRelocSec.second.size(), 4); |
| 837 | for (const auto &FieldRelocInfo : FieldRelocSec.second) { |
Fangrui Song | 0bc77a0 | 2020-02-13 13:26:21 -0800 | [diff] [blame] | 838 | Asm->emitLabelReference(FieldRelocInfo.Label, 4); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 839 | OS.EmitIntValue(FieldRelocInfo.TypeID, 4); |
| 840 | OS.EmitIntValue(FieldRelocInfo.OffsetNameOff, 4); |
| 841 | OS.EmitIntValue(FieldRelocInfo.RelocKind, 4); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | void BTFDebug::beginFunctionImpl(const MachineFunction *MF) { |
| 848 | auto *SP = MF->getFunction().getSubprogram(); |
| 849 | auto *Unit = SP->getUnit(); |
| 850 | |
| 851 | if (Unit->getEmissionKind() == DICompileUnit::NoDebug) { |
| 852 | SkipInstruction = true; |
| 853 | return; |
| 854 | } |
| 855 | SkipInstruction = false; |
| 856 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 857 | // Collect MapDef types. Map definition needs to collect |
| 858 | // pointee types. Do it first. Otherwise, for the following |
| 859 | // case: |
| 860 | // struct m { ...}; |
| 861 | // struct t { |
| 862 | // struct m *key; |
| 863 | // }; |
| 864 | // foo(struct t *arg); |
| 865 | // |
| 866 | // struct mapdef { |
| 867 | // ... |
| 868 | // struct m *key; |
| 869 | // ... |
| 870 | // } __attribute__((section(".maps"))) hash_map; |
| 871 | // |
| 872 | // If subroutine foo is traversed first, a type chain |
| 873 | // "ptr->struct m(fwd)" will be created and later on |
| 874 | // when traversing mapdef, since "ptr->struct m" exists, |
| 875 | // the traversal of "struct m" will be omitted. |
| 876 | if (MapDefNotCollected) { |
| 877 | processGlobals(true); |
| 878 | MapDefNotCollected = false; |
| 879 | } |
| 880 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 881 | // Collect all types locally referenced in this function. |
| 882 | // Use RetainedNodes so we can collect all argument names |
| 883 | // even if the argument is not used. |
| 884 | std::unordered_map<uint32_t, StringRef> FuncArgNames; |
| 885 | for (const DINode *DN : SP->getRetainedNodes()) { |
| 886 | if (const auto *DV = dyn_cast<DILocalVariable>(DN)) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 887 | // Collect function arguments for subprogram func type. |
| 888 | uint32_t Arg = DV->getArg(); |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 889 | if (Arg) { |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 890 | visitTypeEntry(DV->getType()); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 891 | FuncArgNames[Arg] = DV->getName(); |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 892 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | |
| 896 | // Construct subprogram func proto type. |
| 897 | uint32_t ProtoTypeId; |
| 898 | visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId); |
| 899 | |
| 900 | // Construct subprogram func type |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 901 | uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 902 | auto FuncTypeEntry = |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 903 | std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 904 | uint32_t FuncTypeId = addType(std::move(FuncTypeEntry)); |
| 905 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 906 | for (const auto &TypeEntry : TypeEntries) |
| 907 | TypeEntry->completeType(*this); |
| 908 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 909 | // Construct funcinfo and the first lineinfo for the function. |
| 910 | MCSymbol *FuncLabel = Asm->getFunctionBegin(); |
| 911 | BTFFuncInfo FuncInfo; |
| 912 | FuncInfo.Label = FuncLabel; |
| 913 | FuncInfo.TypeId = FuncTypeId; |
| 914 | if (FuncLabel->isInSection()) { |
| 915 | MCSection &Section = FuncLabel->getSection(); |
| 916 | const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section); |
| 917 | assert(SectionELF && "Null section for Function Label"); |
| 918 | SecNameOff = addString(SectionELF->getSectionName()); |
| 919 | } else { |
| 920 | SecNameOff = addString(".text"); |
| 921 | } |
| 922 | FuncInfoTable[SecNameOff].push_back(FuncInfo); |
| 923 | } |
| 924 | |
| 925 | void BTFDebug::endFunctionImpl(const MachineFunction *MF) { |
| 926 | SkipInstruction = false; |
| 927 | LineInfoGenerated = false; |
| 928 | SecNameOff = 0; |
| 929 | } |
| 930 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 931 | /// On-demand populate struct types as requested from abstract member |
| 932 | /// accessing. |
| 933 | unsigned BTFDebug::populateStructType(const DIType *Ty) { |
| 934 | unsigned Id; |
| 935 | visitTypeEntry(Ty, Id, false, false); |
| 936 | for (const auto &TypeEntry : TypeEntries) |
| 937 | TypeEntry->completeType(*this); |
| 938 | return Id; |
| 939 | } |
| 940 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 941 | /// Generate a struct member field relocation. |
Yonghong Song | ffd5740 | 2019-12-19 15:21:53 -0800 | [diff] [blame] | 942 | void BTFDebug::generateFieldReloc(const MCSymbol *ORSym, DIType *RootTy, |
| 943 | StringRef AccessPattern) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 944 | unsigned RootId = populateStructType(RootTy); |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 945 | size_t FirstDollar = AccessPattern.find_first_of('$'); |
| 946 | size_t FirstColon = AccessPattern.find_first_of(':'); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 947 | size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1); |
Yonghong Song | 37d24a6 | 2019-08-02 23:16:44 +0000 | [diff] [blame] | 948 | StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 949 | StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1, |
| 950 | SecondColon - FirstColon); |
| 951 | StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1, |
| 952 | FirstDollar - SecondColon); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 953 | |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 954 | BTFFieldReloc FieldReloc; |
| 955 | FieldReloc.Label = ORSym; |
| 956 | FieldReloc.OffsetNameOff = addString(IndexPattern); |
| 957 | FieldReloc.TypeID = RootId; |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 958 | FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr)); |
| 959 | PatchImms[AccessPattern.str()] = std::stoul(std::string(PatchImmStr)); |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 960 | FieldRelocTable[SecNameOff].push_back(FieldReloc); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Yonghong Song | ffd5740 | 2019-12-19 15:21:53 -0800 | [diff] [blame] | 963 | void BTFDebug::processReloc(const MachineOperand &MO) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 964 | // check whether this is a candidate or not |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 965 | if (MO.isGlobal()) { |
| 966 | const GlobalValue *GVal = MO.getGlobal(); |
| 967 | auto *GVar = dyn_cast<GlobalVariable>(GVal); |
| 968 | if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) { |
| 969 | MCSymbol *ORSym = OS.getContext().createTempSymbol(); |
| 970 | OS.EmitLabel(ORSym); |
| 971 | |
| 972 | MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index); |
| 973 | DIType *Ty = dyn_cast<DIType>(MDN); |
Yonghong Song | ffd5740 | 2019-12-19 15:21:53 -0800 | [diff] [blame] | 974 | generateFieldReloc(ORSym, Ty, GVar->getName()); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 979 | void BTFDebug::beginInstruction(const MachineInstr *MI) { |
| 980 | DebugHandlerBase::beginInstruction(MI); |
| 981 | |
| 982 | if (SkipInstruction || MI->isMetaInstruction() || |
| 983 | MI->getFlag(MachineInstr::FrameSetup)) |
| 984 | return; |
| 985 | |
| 986 | if (MI->isInlineAsm()) { |
| 987 | // Count the number of register definitions to find the asm string. |
| 988 | unsigned NumDefs = 0; |
| 989 | for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); |
| 990 | ++NumDefs) |
| 991 | ; |
| 992 | |
| 993 | // Skip this inline asm instruction if the asmstr is empty. |
| 994 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
| 995 | if (AsmStr[0] == 0) |
| 996 | return; |
| 997 | } |
| 998 | |
Yonghong Song | ffd5740 | 2019-12-19 15:21:53 -0800 | [diff] [blame] | 999 | if (MI->getOpcode() == BPF::LD_imm64) { |
| 1000 | // If the insn is "r2 = LD_imm64 @<an AmaAttr global>", |
| 1001 | // add this insn into the .BTF.ext FieldReloc subsection. |
| 1002 | // Relocation looks like: |
| 1003 | // . SecName: |
| 1004 | // . InstOffset |
| 1005 | // . TypeID |
| 1006 | // . OffSetNameOff |
| 1007 | // . RelocType |
| 1008 | // Later, the insn is replaced with "r2 = <offset>" |
| 1009 | // where "<offset>" equals to the offset based on current |
| 1010 | // type definitions. |
| 1011 | processReloc(MI->getOperand(1)); |
| 1012 | } else if (MI->getOpcode() == BPF::CORE_MEM || |
| 1013 | MI->getOpcode() == BPF::CORE_ALU32_MEM || |
| 1014 | MI->getOpcode() == BPF::CORE_SHIFT) { |
| 1015 | // relocation insn is a load, store or shift insn. |
| 1016 | processReloc(MI->getOperand(3)); |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 1017 | } else if (MI->getOpcode() == BPF::JAL) { |
| 1018 | // check extern function references |
| 1019 | const MachineOperand &MO = MI->getOperand(0); |
| 1020 | if (MO.isGlobal()) { |
| 1021 | processFuncPrototypes(dyn_cast<Function>(MO.getGlobal())); |
| 1022 | } |
Yonghong Song | ffd5740 | 2019-12-19 15:21:53 -0800 | [diff] [blame] | 1023 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1024 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1025 | // Skip this instruction if no DebugLoc or the DebugLoc |
| 1026 | // is the same as the previous instruction. |
| 1027 | const DebugLoc &DL = MI->getDebugLoc(); |
| 1028 | if (!DL || PrevInstLoc == DL) { |
| 1029 | // This instruction will be skipped, no LineInfo has |
| 1030 | // been generated, construct one based on function signature. |
| 1031 | if (LineInfoGenerated == false) { |
| 1032 | auto *S = MI->getMF()->getFunction().getSubprogram(); |
| 1033 | MCSymbol *FuncLabel = Asm->getFunctionBegin(); |
| 1034 | constructLineInfo(S, FuncLabel, S->getLine(), 0); |
| 1035 | LineInfoGenerated = true; |
| 1036 | } |
| 1037 | |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | // Create a temporary label to remember the insn for lineinfo. |
| 1042 | MCSymbol *LineSym = OS.getContext().createTempSymbol(); |
| 1043 | OS.EmitLabel(LineSym); |
| 1044 | |
| 1045 | // Construct the lineinfo. |
| 1046 | auto SP = DL.get()->getScope()->getSubprogram(); |
| 1047 | constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol()); |
| 1048 | |
| 1049 | LineInfoGenerated = true; |
| 1050 | PrevInstLoc = DL; |
| 1051 | } |
| 1052 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1053 | void BTFDebug::processGlobals(bool ProcessingMapDef) { |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1054 | // Collect all types referenced by globals. |
| 1055 | const Module *M = MMI->getModule(); |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 1056 | for (const GlobalVariable &Global : M->globals()) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1057 | // Decide the section name. |
| 1058 | StringRef SecName; |
| 1059 | if (Global.hasSection()) { |
| 1060 | SecName = Global.getSection(); |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1061 | } else if (Global.hasInitializer()) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1062 | // data, bss, or readonly sections |
| 1063 | if (Global.isConstant()) |
| 1064 | SecName = ".rodata"; |
| 1065 | else |
| 1066 | SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data"; |
Yonghong Song | 7d0e893 | 2019-12-10 11:05:22 -0800 | [diff] [blame] | 1067 | } else { |
| 1068 | // extern variables without explicit section, |
| 1069 | // put them into ".extern" section. |
| 1070 | SecName = ".extern"; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (ProcessingMapDef != SecName.startswith(".maps")) |
| 1074 | continue; |
| 1075 | |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 1076 | SmallVector<DIGlobalVariableExpression *, 1> GVs; |
| 1077 | Global.getDebugInfo(GVs); |
Yonghong Song | 166cdc0 | 2019-11-12 11:31:52 -0800 | [diff] [blame] | 1078 | |
| 1079 | // No type information, mostly internal, skip it. |
| 1080 | if (GVs.size() == 0) |
| 1081 | continue; |
| 1082 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1083 | uint32_t GVTypeId = 0; |
Yonghong Song | cacac05 | 2019-03-15 05:51:25 +0000 | [diff] [blame] | 1084 | for (auto *GVE : GVs) { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1085 | if (SecName.startswith(".maps")) |
| 1086 | visitMapDefType(GVE->getVariable()->getType(), GVTypeId); |
| 1087 | else |
| 1088 | visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1089 | break; |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1090 | } |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1091 | |
| 1092 | // Only support the following globals: |
| 1093 | // . static variables |
Yonghong Song | 5ea611d | 2019-12-06 23:00:41 -0800 | [diff] [blame] | 1094 | // . non-static weak or non-weak global variables |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1095 | // . weak or non-weak extern global variables |
| 1096 | // Whether DataSec is readonly or not can be found from corresponding ELF |
| 1097 | // section flags. Whether a BTF_KIND_VAR is a weak symbol or not |
| 1098 | // can be found from the corresponding ELF symbol table. |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1099 | auto Linkage = Global.getLinkage(); |
| 1100 | if (Linkage != GlobalValue::InternalLinkage && |
Yonghong Song | 5ea611d | 2019-12-06 23:00:41 -0800 | [diff] [blame] | 1101 | Linkage != GlobalValue::ExternalLinkage && |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1102 | Linkage != GlobalValue::WeakAnyLinkage && |
| 1103 | Linkage != GlobalValue::ExternalWeakLinkage) |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1104 | continue; |
| 1105 | |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1106 | uint32_t GVarInfo; |
| 1107 | if (Linkage == GlobalValue::InternalLinkage) { |
| 1108 | GVarInfo = BTF::VAR_STATIC; |
| 1109 | } else if (Global.hasInitializer()) { |
| 1110 | GVarInfo = BTF::VAR_GLOBAL_ALLOCATED; |
| 1111 | } else { |
| 1112 | GVarInfo = BTF::VAR_GLOBAL_EXTERNAL; |
| 1113 | } |
| 1114 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1115 | auto VarEntry = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 1116 | std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1117 | uint32_t VarId = addType(std::move(VarEntry)); |
| 1118 | |
Yonghong Song | 7d0e893 | 2019-12-10 11:05:22 -0800 | [diff] [blame] | 1119 | assert(!SecName.empty()); |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1120 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1121 | // Find or create a DataSec |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 1122 | if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) { |
| 1123 | DataSecEntries[std::string(SecName)] = |
| 1124 | std::make_unique<BTFKindDataSec>(Asm, std::string(SecName)); |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | // Calculate symbol size |
| 1128 | const DataLayout &DL = Global.getParent()->getDataLayout(); |
| 1129 | uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType()); |
| 1130 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 1131 | DataSecEntries[std::string(SecName)]->addVar(VarId, Asm->getSymbol(&Global), |
| 1132 | Size); |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1133 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1134 | } |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1135 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1136 | /// Emit proper patchable instructions. |
| 1137 | bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) { |
| 1138 | if (MI->getOpcode() == BPF::LD_imm64) { |
| 1139 | const MachineOperand &MO = MI->getOperand(1); |
| 1140 | if (MO.isGlobal()) { |
| 1141 | const GlobalValue *GVal = MO.getGlobal(); |
| 1142 | auto *GVar = dyn_cast<GlobalVariable>(GVal); |
| 1143 | if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) { |
Yonghong Song | 05e4697 | 2019-10-08 18:23:17 +0000 | [diff] [blame] | 1144 | // Emit "mov ri, <imm>" for patched immediate. |
| 1145 | uint32_t Imm = PatchImms[GVar->getName().str()]; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1146 | OutMI.setOpcode(BPF::MOV_ri); |
| 1147 | OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); |
| 1148 | OutMI.addOperand(MCOperand::createImm(Imm)); |
| 1149 | return true; |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
Yonghong Song | ffd5740 | 2019-12-19 15:21:53 -0800 | [diff] [blame] | 1152 | } else if (MI->getOpcode() == BPF::CORE_MEM || |
| 1153 | MI->getOpcode() == BPF::CORE_ALU32_MEM || |
| 1154 | MI->getOpcode() == BPF::CORE_SHIFT) { |
| 1155 | const MachineOperand &MO = MI->getOperand(3); |
| 1156 | if (MO.isGlobal()) { |
| 1157 | const GlobalValue *GVal = MO.getGlobal(); |
| 1158 | auto *GVar = dyn_cast<GlobalVariable>(GVal); |
| 1159 | if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) { |
| 1160 | uint32_t Imm = PatchImms[GVar->getName().str()]; |
| 1161 | OutMI.setOpcode(MI->getOperand(1).getImm()); |
| 1162 | if (MI->getOperand(0).isImm()) |
| 1163 | OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm())); |
| 1164 | else |
| 1165 | OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg())); |
| 1166 | OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg())); |
| 1167 | OutMI.addOperand(MCOperand::createImm(Imm)); |
| 1168 | return true; |
| 1169 | } |
| 1170 | } |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1171 | } |
| 1172 | return false; |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 1175 | void BTFDebug::processFuncPrototypes(const Function *F) { |
| 1176 | if (!F) |
| 1177 | return; |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1178 | |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 1179 | const DISubprogram *SP = F->getSubprogram(); |
| 1180 | if (!SP || SP->isDefinition()) |
| 1181 | return; |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1182 | |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 1183 | // Do not emit again if already emitted. |
| 1184 | if (ProtoFunctions.find(F) != ProtoFunctions.end()) |
| 1185 | return; |
| 1186 | ProtoFunctions.insert(F); |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1187 | |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 1188 | uint32_t ProtoTypeId; |
| 1189 | const std::unordered_map<uint32_t, StringRef> FuncArgNames; |
| 1190 | visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId); |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1191 | |
Yonghong Song | fbb64aa | 2019-12-17 16:24:23 -0800 | [diff] [blame] | 1192 | uint8_t Scope = BTF::FUNC_EXTERN; |
| 1193 | auto FuncTypeEntry = |
| 1194 | std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope); |
| 1195 | addType(std::move(FuncTypeEntry)); |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1198 | void BTFDebug::endModule() { |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1199 | // Collect MapDef globals if not collected yet. |
| 1200 | if (MapDefNotCollected) { |
| 1201 | processGlobals(true); |
| 1202 | MapDefNotCollected = false; |
| 1203 | } |
| 1204 | |
| 1205 | // Collect global types/variables except MapDef globals. |
| 1206 | processGlobals(false); |
Yonghong Song | 4448125 | 2019-11-22 08:45:37 -0800 | [diff] [blame] | 1207 | |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1208 | for (auto &DataSec : DataSecEntries) |
| 1209 | addType(std::move(DataSec.second)); |
| 1210 | |
| 1211 | // Fixups |
| 1212 | for (auto &Fixup : FixupDerivedTypes) { |
| 1213 | StringRef TypeName = Fixup.first; |
| 1214 | bool IsUnion = Fixup.second.first; |
| 1215 | |
| 1216 | // Search through struct types |
| 1217 | uint32_t StructTypeId = 0; |
| 1218 | for (const auto &StructType : StructTypes) { |
| 1219 | if (StructType->getName() == TypeName) { |
| 1220 | StructTypeId = StructType->getId(); |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (StructTypeId == 0) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 1226 | auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion); |
Yonghong Song | d3d88d0 | 2019-07-09 15:28:41 +0000 | [diff] [blame] | 1227 | StructTypeId = addType(std::move(FwdTypeEntry)); |
| 1228 | } |
| 1229 | |
| 1230 | for (auto &DType : Fixup.second.second) { |
| 1231 | DType->setPointeeType(StructTypeId); |
| 1232 | } |
| 1233 | } |
Yonghong Song | 6db6b56 | 2019-03-16 15:36:31 +0000 | [diff] [blame] | 1234 | |
Yonghong Song | 7b410ac | 2018-12-19 16:40:25 +0000 | [diff] [blame] | 1235 | // Complete BTF type cross refereences. |
| 1236 | for (const auto &TypeEntry : TypeEntries) |
| 1237 | TypeEntry->completeType(*this); |
| 1238 | |
| 1239 | // Emit BTF sections. |
| 1240 | emitBTFSection(); |
| 1241 | emitBTFExtSection(); |
| 1242 | } |