1 //===- llvm-ifs.cpp -------------------------------------------------------===// 2 // 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 6 // 7 //===-----------------------------------------------------------------------===/ 8 9 #include "llvm/ADT/StringRef.h" 10 #include "llvm/ADT/StringSwitch.h" 11 #include "llvm/ADT/Triple.h" 12 #include "llvm/ObjectYAML/yaml2obj.h" 13 #include "llvm/Support/CommandLine.h" 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/Errc.h" 16 #include "llvm/Support/Error.h" 17 #include "llvm/Support/FileOutputBuffer.h" 18 #include "llvm/Support/MemoryBuffer.h" 19 #include "llvm/Support/Path.h" 20 #include "llvm/Support/VersionTuple.h" 21 #include "llvm/Support/WithColor.h" 22 #include "llvm/Support/YAMLTraits.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include "llvm/TextAPI/MachO/InterfaceFile.h" 25 #include "llvm/TextAPI/MachO/TextAPIReader.h" 26 #include "llvm/TextAPI/MachO/TextAPIWriter.h" 27 #include <set> 28 #include <string> 29 #include <vector> 30 31 using namespace llvm; 32 using namespace llvm::yaml; 33 using namespace llvm::MachO; 34 35 #define DEBUG_TYPE "llvm-ifs" 36 37 namespace { 38 const VersionTuple IFSVersionCurrent(2, 0); 39 } // end anonymous namespace 40 41 static cl::opt<std::string> Action("action", cl::desc("<llvm-ifs action>"), 42 cl::value_desc("write-ifs | write-bin"), 43 cl::init("write-ifs")); 44 45 static cl::opt<std::string> ForceFormat("force-format", 46 cl::desc("<force object format>"), 47 cl::value_desc("ELF | TBD"), 48 cl::init("")); 49 50 static cl::list<std::string> InputFilenames(cl::Positional, 51 cl::desc("<input ifs files>"), 52 cl::ZeroOrMore); 53 54 static cl::opt<std::string> OutputFilename("o", cl::desc("<output file>"), 55 cl::value_desc("path")); 56 57 enum class IFSSymbolType { 58 NoType = 0, 59 Object, 60 Func, 61 // Type information is 4 bits, so 16 is safely out of range. 62 Unknown = 16, 63 }; 64 65 std::string getTypeName(IFSSymbolType Type) { 66 switch (Type) { 67 case IFSSymbolType::NoType: 68 return "NoType"; 69 case IFSSymbolType::Func: 70 return "Func"; 71 case IFSSymbolType::Object: 72 return "Object"; 73 case IFSSymbolType::Unknown: 74 return "Unknown"; 75 } 76 llvm_unreachable("Unexpected ifs symbol type."); 77 } 78 79 struct IFSSymbol { 80 IFSSymbol() = default; 81 IFSSymbol(std::string SymbolName) : Name(SymbolName) {} 82 std::string Name; 83 uint64_t Size; 84 IFSSymbolType Type; 85 bool Weak; 86 Optional<std::string> Warning; 87 bool operator<(const IFSSymbol &RHS) const { return Name < RHS.Name; } 88 }; 89 90 LLVM_YAML_IS_SEQUENCE_VECTOR(IFSSymbol) 91 92 namespace llvm { 93 namespace yaml { 94 /// YAML traits for IFSSymbolType. 95 template <> struct ScalarEnumerationTraits<IFSSymbolType> { 96 static void enumeration(IO &IO, IFSSymbolType &SymbolType) { 97 IO.enumCase(SymbolType, "NoType", IFSSymbolType::NoType); 98 IO.enumCase(SymbolType, "Func", IFSSymbolType::Func); 99 IO.enumCase(SymbolType, "Object", IFSSymbolType::Object); 100 IO.enumCase(SymbolType, "Unknown", IFSSymbolType::Unknown); 101 // Treat other symbol types as noise, and map to Unknown. 102 if (!IO.outputting() && IO.matchEnumFallback()) 103 SymbolType = IFSSymbolType::Unknown; 104 } 105 }; 106 107 template <> struct ScalarTraits<VersionTuple> { 108 static void output(const VersionTuple &Value, void *, 109 llvm::raw_ostream &Out) { 110 Out << Value.getAsString(); 111 } 112 113 static StringRef input(StringRef Scalar, void *, VersionTuple &Value) { 114 if (Value.tryParse(Scalar)) 115 return StringRef("Can't parse version: invalid version format."); 116 117 if (Value > IFSVersionCurrent) 118 return StringRef("Unsupported IFS version."); 119 120 // Returning empty StringRef indicates successful parse. 121 return StringRef(); 122 } 123 124 // Don't place quotation marks around version value. 125 static QuotingType mustQuote(StringRef) { return QuotingType::None; } 126 }; 127 128 /// YAML traits for IFSSymbol. 129 template <> struct MappingTraits<IFSSymbol> { 130 static void mapping(IO &IO, IFSSymbol &Symbol) { 131 IO.mapRequired("Name", Symbol.Name); 132 IO.mapRequired("Type", Symbol.Type); 133 // The need for symbol size depends on the symbol type. 134 if (Symbol.Type == IFSSymbolType::NoType) 135 IO.mapOptional("Size", Symbol.Size, (uint64_t)0); 136 else if (Symbol.Type == IFSSymbolType::Func) 137 Symbol.Size = 0; 138 else 139 IO.mapRequired("Size", Symbol.Size); 140 IO.mapOptional("Weak", Symbol.Weak, false); 141 IO.mapOptional("Warning", Symbol.Warning); 142 } 143 144 // Compacts symbol information into a single line. 145 static const bool flow = true; 146 }; 147 148 } // namespace yaml 149 } // namespace llvm 150 151 // A cumulative representation of ELF stubs. 152 // Both textual and binary stubs will read into and write from this object. 153 class IFSStub { 154 // TODO: Add support for symbol versioning. 155 public: 156 VersionTuple IfsVersion; 157 std::string Triple; 158 std::string ObjectFileFormat; 159 Optional<std::string> SOName; 160 std::vector<std::string> NeededLibs; 161 std::vector<IFSSymbol> Symbols; 162 163 IFSStub() = default; 164 IFSStub(const IFSStub &Stub) 165 : IfsVersion(Stub.IfsVersion), Triple(Stub.Triple), 166 ObjectFileFormat(Stub.ObjectFileFormat), SOName(Stub.SOName), 167 NeededLibs(Stub.NeededLibs), Symbols(Stub.Symbols) {} 168 IFSStub(IFSStub &&Stub) 169 : IfsVersion(std::move(Stub.IfsVersion)), Triple(std::move(Stub.Triple)), 170 ObjectFileFormat(std::move(Stub.ObjectFileFormat)), 171 SOName(std::move(Stub.SOName)), NeededLibs(std::move(Stub.NeededLibs)), 172 Symbols(std::move(Stub.Symbols)) {} 173 }; 174 175 namespace llvm { 176 namespace yaml { 177 /// YAML traits for IFSStub objects. 178 template <> struct MappingTraits<IFSStub> { 179 static void mapping(IO &IO, IFSStub &Stub) { 180 if (!IO.mapTag("!experimental-ifs-v2", true)) 181 IO.setError("Not a .ifs YAML file."); 182 183 auto OldContext = IO.getContext(); 184 IO.setContext(&Stub); 185 IO.mapRequired("IfsVersion", Stub.IfsVersion); 186 IO.mapOptional("Triple", Stub.Triple); 187 IO.mapOptional("ObjectFileFormat", Stub.ObjectFileFormat); 188 IO.mapOptional("SOName", Stub.SOName); 189 IO.mapOptional("NeededLibs", Stub.NeededLibs); 190 IO.mapRequired("Symbols", Stub.Symbols); 191 IO.setContext(&OldContext); 192 } 193 }; 194 } // namespace yaml 195 } // namespace llvm 196 197 static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) { 198 // Read in file. 199 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError = 200 MemoryBuffer::getFileOrSTDIN(FilePath); 201 if (!BufOrError) 202 return createStringError(BufOrError.getError(), "Could not open `%s`", 203 FilePath.data()); 204 205 std::unique_ptr<MemoryBuffer> FileReadBuffer = std::move(*BufOrError); 206 yaml::Input YamlIn(FileReadBuffer->getBuffer()); 207 std::unique_ptr<IFSStub> Stub(new IFSStub()); 208 YamlIn >> *Stub; 209 210 if (std::error_code Err = YamlIn.error()) 211 return createStringError(Err, "Failed reading Interface Stub File."); 212 213 return std::move(Stub); 214 } 215 216 int writeTbdStub(const llvm::Triple &T, const std::vector<IFSSymbol> &Symbols, 217 const StringRef Format, raw_ostream &Out) { 218 219 auto PlatformKindOrError = 220 [](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::PlatformKind> { 221 if (T.isMacOSX()) 222 return llvm::MachO::PlatformKind::macOS; 223 if (T.isTvOS()) 224 return llvm::MachO::PlatformKind::tvOS; 225 if (T.isWatchOS()) 226 return llvm::MachO::PlatformKind::watchOS; 227 // Note: put isiOS last because tvOS and watchOS are also iOS according 228 // to the Triple. 229 if (T.isiOS()) 230 return llvm::MachO::PlatformKind::iOS; 231 232 // TODO: Add an option for ForceTriple, but keep ForceFormat for now. 233 if (ForceFormat == "TBD") 234 return llvm::MachO::PlatformKind::macOS; 235 236 return createStringError(errc::not_supported, "Invalid Platform.\n"); 237 }(T); 238 239 if (!PlatformKindOrError) 240 return -1; 241 242 PlatformKind Plat = PlatformKindOrError.get(); 243 TargetList Targets({Target(llvm::MachO::mapToArchitecture(T), Plat)}); 244 245 InterfaceFile File; 246 File.setFileType(FileType::TBD_V3); // Only supporting v3 for now. 247 File.addTargets(Targets); 248 249 for (const auto &Symbol : Symbols) { 250 auto Name = Symbol.Name; 251 auto Kind = SymbolKind::GlobalSymbol; 252 switch (Symbol.Type) { 253 default: 254 case IFSSymbolType::NoType: 255 Kind = SymbolKind::GlobalSymbol; 256 break; 257 case IFSSymbolType::Object: 258 Kind = SymbolKind::GlobalSymbol; 259 break; 260 case IFSSymbolType::Func: 261 Kind = SymbolKind::GlobalSymbol; 262 break; 263 } 264 if (Symbol.Weak) 265 File.addSymbol(Kind, Name, Targets, SymbolFlags::WeakDefined); 266 else 267 File.addSymbol(Kind, Name, Targets); 268 } 269 270 SmallString<4096> Buffer; 271 raw_svector_ostream OS(Buffer); 272 if (Error Result = TextAPIWriter::writeToStream(OS, File)) 273 return -1; 274 Out << OS.str(); 275 return 0; 276 } 277 278 int writeElfStub(const llvm::Triple &T, const std::vector<IFSSymbol> &Symbols, 279 const StringRef Format, raw_ostream &Out) { 280 SmallString<0> Storage; 281 Storage.clear(); 282 raw_svector_ostream OS(Storage); 283 284 OS << "--- !ELF\n"; 285 OS << "FileHeader:\n"; 286 OS << " Class: ELFCLASS"; 287 OS << (T.isArch64Bit() ? "64" : "32"); 288 OS << "\n"; 289 OS << " Data: ELFDATA2"; 290 OS << (T.isLittleEndian() ? "LSB" : "MSB"); 291 OS << "\n"; 292 OS << " Type: ET_DYN\n"; 293 OS << " Machine: " 294 << llvm::StringSwitch<llvm::StringRef>(T.getArchName()) 295 .Case("x86_64", "EM_X86_64") 296 .Case("i386", "EM_386") 297 .Case("i686", "EM_386") 298 .Case("aarch64", "EM_AARCH64") 299 .Case("amdgcn", "EM_AMDGPU") 300 .Case("r600", "EM_AMDGPU") 301 .Case("arm", "EM_ARM") 302 .Case("thumb", "EM_ARM") 303 .Case("avr", "EM_AVR") 304 .Case("mips", "EM_MIPS") 305 .Case("mipsel", "EM_MIPS") 306 .Case("mips64", "EM_MIPS") 307 .Case("mips64el", "EM_MIPS") 308 .Case("msp430", "EM_MSP430") 309 .Case("ppc", "EM_PPC") 310 .Case("ppc64", "EM_PPC64") 311 .Case("ppc64le", "EM_PPC64") 312 .Case("x86", T.isOSIAMCU() ? "EM_IAMCU" : "EM_386") 313 .Case("x86_64", "EM_X86_64") 314 .Default("EM_NONE") 315 << "\nSections:" 316 << "\n - Name: .text" 317 << "\n Type: SHT_PROGBITS" 318 << "\n - Name: .data" 319 << "\n Type: SHT_PROGBITS" 320 << "\n - Name: .rodata" 321 << "\n Type: SHT_PROGBITS" 322 << "\nSymbols:\n"; 323 for (const auto &Symbol : Symbols) { 324 OS << " - Name: " << Symbol.Name << "\n" 325 << " Type: STT_"; 326 switch (Symbol.Type) { 327 default: 328 case IFSSymbolType::NoType: 329 OS << "NOTYPE"; 330 break; 331 case IFSSymbolType::Object: 332 OS << "OBJECT"; 333 break; 334 case IFSSymbolType::Func: 335 OS << "FUNC"; 336 break; 337 } 338 OS << "\n Section: .text" 339 << "\n Binding: STB_" << (Symbol.Weak ? "WEAK" : "GLOBAL") 340 << "\n"; 341 } 342 OS << "...\n"; 343 344 std::string YamlStr = std::string(OS.str()); 345 346 // Only or debugging. Not an offical format. 347 LLVM_DEBUG({ 348 if (ForceFormat == "ELFOBJYAML") { 349 Out << YamlStr; 350 return 0; 351 } 352 }); 353 354 yaml::Input YIn(YamlStr); 355 auto ErrHandler = [](const Twine &Msg) { 356 WithColor::error(errs(), "llvm-ifs") << Msg << "\n"; 357 }; 358 return convertYAML(YIn, Out, ErrHandler) ? 0 : 1; 359 } 360 361 int writeIfso(const IFSStub &Stub, bool IsWriteIfs, raw_ostream &Out) { 362 if (IsWriteIfs) { 363 yaml::Output YamlOut(Out, NULL, /*WrapColumn =*/0); 364 YamlOut << const_cast<IFSStub &>(Stub); 365 return 0; 366 } 367 368 std::string ObjectFileFormat = 369 ForceFormat.empty() ? Stub.ObjectFileFormat : ForceFormat; 370 371 if (ObjectFileFormat == "ELF" || ForceFormat == "ELFOBJYAML") 372 return writeElfStub(llvm::Triple(Stub.Triple), Stub.Symbols, 373 Stub.ObjectFileFormat, Out); 374 if (ObjectFileFormat == "TBD") 375 return writeTbdStub(llvm::Triple(Stub.Triple), Stub.Symbols, 376 Stub.ObjectFileFormat, Out); 377 378 WithColor::error() 379 << "Invalid ObjectFileFormat: Only ELF and TBD are supported.\n"; 380 return -1; 381 } 382 383 // TODO: Drop ObjectFileFormat, it can be subsumed from the triple. 384 // New Interface Stubs Yaml Format: 385 // --- !experimental-ifs-v2 386 // IfsVersion: 2.0 387 // Triple: <llvm triple> 388 // ObjectFileFormat: <ELF | others not yet supported> 389 // Symbols: 390 // _ZSymbolName: { Type: <type> } 391 // ... 392 393 int main(int argc, char *argv[]) { 394 // Parse arguments. 395 cl::ParseCommandLineOptions(argc, argv); 396 397 if (InputFilenames.empty()) 398 InputFilenames.push_back("-"); 399 400 IFSStub Stub; 401 std::map<std::string, IFSSymbol> SymbolMap; 402 403 std::string PreviousInputFilePath = ""; 404 for (const std::string &InputFilePath : InputFilenames) { 405 Expected<std::unique_ptr<IFSStub>> StubOrErr = readInputFile(InputFilePath); 406 if (!StubOrErr) { 407 WithColor::error() << StubOrErr.takeError() << "\n"; 408 return -1; 409 } 410 std::unique_ptr<IFSStub> TargetStub = std::move(StubOrErr.get()); 411 412 if (Stub.Triple.empty()) { 413 PreviousInputFilePath = InputFilePath; 414 Stub.IfsVersion = TargetStub->IfsVersion; 415 Stub.Triple = TargetStub->Triple; 416 Stub.ObjectFileFormat = TargetStub->ObjectFileFormat; 417 Stub.SOName = TargetStub->SOName; 418 Stub.NeededLibs = TargetStub->NeededLibs; 419 } else { 420 Stub.ObjectFileFormat = !Stub.ObjectFileFormat.empty() 421 ? Stub.ObjectFileFormat 422 : TargetStub->ObjectFileFormat; 423 424 if (Stub.IfsVersion != TargetStub->IfsVersion) { 425 if (Stub.IfsVersion.getMajor() != IFSVersionCurrent.getMajor()) { 426 WithColor::error() 427 << "Interface Stub: IfsVersion Mismatch." 428 << "\nFilenames: " << PreviousInputFilePath << " " 429 << InputFilePath << "\nIfsVersion Values: " << Stub.IfsVersion 430 << " " << TargetStub->IfsVersion << "\n"; 431 return -1; 432 } 433 if (TargetStub->IfsVersion > Stub.IfsVersion) 434 Stub.IfsVersion = TargetStub->IfsVersion; 435 } 436 if (Stub.ObjectFileFormat != TargetStub->ObjectFileFormat && 437 !TargetStub->ObjectFileFormat.empty()) { 438 WithColor::error() << "Interface Stub: ObjectFileFormat Mismatch." 439 << "\nFilenames: " << PreviousInputFilePath << " " 440 << InputFilePath << "\nObjectFileFormat Values: " 441 << Stub.ObjectFileFormat << " " 442 << TargetStub->ObjectFileFormat << "\n"; 443 return -1; 444 } 445 if (Stub.Triple != TargetStub->Triple && !TargetStub->Triple.empty()) { 446 WithColor::error() << "Interface Stub: Triple Mismatch." 447 << "\nFilenames: " << PreviousInputFilePath << " " 448 << InputFilePath 449 << "\nTriple Values: " << Stub.Triple << " " 450 << TargetStub->Triple << "\n"; 451 return -1; 452 } 453 if (Stub.SOName != TargetStub->SOName) { 454 WithColor::error() << "Interface Stub: SOName Mismatch." 455 << "\nFilenames: " << PreviousInputFilePath << " " 456 << InputFilePath 457 << "\nSOName Values: " << Stub.SOName << " " 458 << TargetStub->SOName << "\n"; 459 return -1; 460 } 461 if (Stub.NeededLibs != TargetStub->NeededLibs) { 462 WithColor::error() << "Interface Stub: NeededLibs Mismatch." 463 << "\nFilenames: " << PreviousInputFilePath << " " 464 << InputFilePath << "\n"; 465 return -1; 466 } 467 } 468 469 for (auto Symbol : TargetStub->Symbols) { 470 auto SI = SymbolMap.find(Symbol.Name); 471 if (SI == SymbolMap.end()) { 472 SymbolMap.insert( 473 std::pair<std::string, IFSSymbol>(Symbol.Name, Symbol)); 474 continue; 475 } 476 477 assert(Symbol.Name == SI->second.Name && "Symbol Names Must Match."); 478 479 // Check conflicts: 480 if (Symbol.Type != SI->second.Type) { 481 WithColor::error() << "Interface Stub: Type Mismatch for " 482 << Symbol.Name << ".\nFilename: " << InputFilePath 483 << "\nType Values: " << getTypeName(SI->second.Type) 484 << " " << getTypeName(Symbol.Type) << "\n"; 485 486 return -1; 487 } 488 if (Symbol.Size != SI->second.Size) { 489 WithColor::error() << "Interface Stub: Size Mismatch for " 490 << Symbol.Name << ".\nFilename: " << InputFilePath 491 << "\nSize Values: " << SI->second.Size << " " 492 << Symbol.Size << "\n"; 493 494 return -1; 495 } 496 if (Symbol.Weak != SI->second.Weak) { 497 Symbol.Weak = false; 498 continue; 499 } 500 // TODO: Not checking Warning. Will be dropped. 501 } 502 503 PreviousInputFilePath = InputFilePath; 504 } 505 506 if (Stub.IfsVersion != IFSVersionCurrent) 507 if (Stub.IfsVersion.getMajor() != IFSVersionCurrent.getMajor()) { 508 WithColor::error() << "Interface Stub: Bad IfsVersion: " 509 << Stub.IfsVersion << ", llvm-ifs supported version: " 510 << IFSVersionCurrent << ".\n"; 511 return -1; 512 } 513 514 for (auto &Entry : SymbolMap) 515 Stub.Symbols.push_back(Entry.second); 516 517 std::error_code SysErr; 518 519 // Open file for writing. 520 raw_fd_ostream Out(OutputFilename, SysErr); 521 if (SysErr) { 522 WithColor::error() << "Couldn't open " << OutputFilename 523 << " for writing.\n"; 524 return -1; 525 } 526 527 return writeIfso(Stub, (Action == "write-ifs"), Out); 528 } 529