[llvm-nm] - Fix a bug and unbreak ASan BB.
[llvm-complete.git] / tools / llvm-ifs / llvm-ifs.cpp
blob1746357827572ced6dd7bafac1a073189e30b55d
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 //===-----------------------------------------------------------------------===/
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>
30 using namespace llvm;
31 using namespace llvm::yaml;
32 using namespace llvm::MachO;
34 #define DEBUG_TYPE "llvm-ifs"
36 namespace {
37 const VersionTuple IFSVersionCurrent(1, 2);
40 static cl::opt<std::string> Action("action", cl::desc("<llvm-ifs action>"),
41 cl::value_desc("write-ifs | write-bin"),
42 cl::init("write-ifs"));
44 static cl::opt<std::string> ForceFormat("force-format",
45 cl::desc("<force object format>"),
46 cl::value_desc("ELF | TBD"),
47 cl::init(""));
49 static cl::list<std::string> InputFilenames(cl::Positional,
50 cl::desc("<input ifs files>"),
51 cl::ZeroOrMore);
53 static cl::opt<std::string> OutputFilename("o", cl::desc("<output file>"),
54 cl::value_desc("path"));
56 enum class IFSSymbolType {
57 NoType = 0,
58 Object,
59 Func,
60 // Type information is 4 bits, so 16 is safely out of range.
61 Unknown = 16,
64 std::string getTypeName(IFSSymbolType Type) {
65 switch (Type) {
66 case IFSSymbolType::NoType:
67 return "NoType";
68 case IFSSymbolType::Func:
69 return "Func";
70 case IFSSymbolType::Object:
71 return "Object";
72 case IFSSymbolType::Unknown:
73 return "Unknown";
75 llvm_unreachable("Unexpected ifs symbol type.");
78 struct IFSSymbol {
79 IFSSymbol(std::string SymbolName) : Name(SymbolName) {}
80 std::string Name;
81 uint64_t Size;
82 IFSSymbolType Type;
83 bool Weak;
84 Optional<std::string> Warning;
85 bool operator<(const IFSSymbol &RHS) const { return Name < RHS.Name; }
88 namespace llvm {
89 namespace yaml {
90 /// YAML traits for IFSSymbolType.
91 template <> struct ScalarEnumerationTraits<IFSSymbolType> {
92 static void enumeration(IO &IO, IFSSymbolType &SymbolType) {
93 IO.enumCase(SymbolType, "NoType", IFSSymbolType::NoType);
94 IO.enumCase(SymbolType, "Func", IFSSymbolType::Func);
95 IO.enumCase(SymbolType, "Object", IFSSymbolType::Object);
96 IO.enumCase(SymbolType, "Unknown", IFSSymbolType::Unknown);
97 // Treat other symbol types as noise, and map to Unknown.
98 if (!IO.outputting() && IO.matchEnumFallback())
99 SymbolType = IFSSymbolType::Unknown;
103 template <> struct ScalarTraits<VersionTuple> {
104 static void output(const VersionTuple &Value, void *,
105 llvm::raw_ostream &Out) {
106 Out << Value.getAsString();
109 static StringRef input(StringRef Scalar, void *, VersionTuple &Value) {
110 if (Value.tryParse(Scalar))
111 return StringRef("Can't parse version: invalid version format.");
113 if (Value > IFSVersionCurrent)
114 return StringRef("Unsupported IFS version.");
116 // Returning empty StringRef indicates successful parse.
117 return StringRef();
120 // Don't place quotation marks around version value.
121 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
124 /// YAML traits for IFSSymbol.
125 template <> struct MappingTraits<IFSSymbol> {
126 static void mapping(IO &IO, IFSSymbol &Symbol) {
127 IO.mapRequired("Type", Symbol.Type);
128 // The need for symbol size depends on the symbol type.
129 if (Symbol.Type == IFSSymbolType::NoType)
130 IO.mapOptional("Size", Symbol.Size, (uint64_t)0);
131 else if (Symbol.Type == IFSSymbolType::Func)
132 Symbol.Size = 0;
133 else
134 IO.mapRequired("Size", Symbol.Size);
135 IO.mapOptional("Weak", Symbol.Weak, false);
136 IO.mapOptional("Warning", Symbol.Warning);
139 // Compacts symbol information into a single line.
140 static const bool flow = true;
143 /// YAML traits for set of IFSSymbols.
144 template <> struct CustomMappingTraits<std::set<IFSSymbol>> {
145 static void inputOne(IO &IO, StringRef Key, std::set<IFSSymbol> &Set) {
146 std::string Name = Key.str();
147 IFSSymbol Sym(Name);
148 IO.mapRequired(Name.c_str(), Sym);
149 Set.insert(Sym);
152 static void output(IO &IO, std::set<IFSSymbol> &Set) {
153 for (auto &Sym : Set)
154 IO.mapRequired(Sym.Name.c_str(), const_cast<IFSSymbol &>(Sym));
157 } // namespace yaml
158 } // namespace llvm
160 // A cumulative representation of ELF stubs.
161 // Both textual and binary stubs will read into and write from this object.
162 class IFSStub {
163 // TODO: Add support for symbol versioning.
164 public:
165 VersionTuple IfsVersion;
166 std::string Triple;
167 std::string ObjectFileFormat;
168 Optional<std::string> SOName;
169 std::vector<std::string> NeededLibs;
170 std::set<IFSSymbol> Symbols;
172 IFSStub() = default;
173 IFSStub(const IFSStub &Stub)
174 : IfsVersion(Stub.IfsVersion), Triple(Stub.Triple),
175 ObjectFileFormat(Stub.ObjectFileFormat), SOName(Stub.SOName),
176 NeededLibs(Stub.NeededLibs), Symbols(Stub.Symbols) {}
177 IFSStub(IFSStub &&Stub)
178 : IfsVersion(std::move(Stub.IfsVersion)), Triple(std::move(Stub.Triple)),
179 ObjectFileFormat(std::move(Stub.ObjectFileFormat)),
180 SOName(std::move(Stub.SOName)), NeededLibs(std::move(Stub.NeededLibs)),
181 Symbols(std::move(Stub.Symbols)) {}
184 namespace llvm {
185 namespace yaml {
186 /// YAML traits for IFSStub objects.
187 template <> struct MappingTraits<IFSStub> {
188 static void mapping(IO &IO, IFSStub &Stub) {
189 if (!IO.mapTag("!experimental-ifs-v1", true))
190 IO.setError("Not a .ifs YAML file.");
191 IO.mapRequired("IfsVersion", Stub.IfsVersion);
192 IO.mapOptional("Triple", Stub.Triple);
193 IO.mapOptional("ObjectFileFormat", Stub.ObjectFileFormat);
194 IO.mapOptional("SOName", Stub.SOName);
195 IO.mapOptional("NeededLibs", Stub.NeededLibs);
196 IO.mapRequired("Symbols", Stub.Symbols);
199 } // namespace yaml
200 } // namespace llvm
202 static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) {
203 // Read in file.
204 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
205 MemoryBuffer::getFileOrSTDIN(FilePath);
206 if (!BufOrError)
207 return createStringError(BufOrError.getError(), "Could not open `%s`",
208 FilePath.data());
210 std::unique_ptr<MemoryBuffer> FileReadBuffer = std::move(*BufOrError);
211 yaml::Input YamlIn(FileReadBuffer->getBuffer());
212 std::unique_ptr<IFSStub> Stub(new IFSStub());
213 YamlIn >> *Stub;
215 if (std::error_code Err = YamlIn.error())
216 return createStringError(Err, "Failed reading Interface Stub File.");
218 return std::move(Stub);
221 int writeTbdStub(const llvm::Triple &T, const std::set<IFSSymbol> &Symbols,
222 const StringRef Format, raw_ostream &Out) {
223 auto ArchOrError =
224 [](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::Architecture> {
225 switch (T.getArch()) {
226 default:
227 return createStringError(errc::not_supported, "Invalid Architecture.\n");
228 case llvm::Triple::ArchType::x86:
229 return AK_i386;
230 case llvm::Triple::ArchType::x86_64:
231 return AK_x86_64;
232 case llvm::Triple::ArchType::arm:
233 return AK_armv7;
234 case llvm::Triple::ArchType::aarch64:
235 return AK_arm64;
237 }(T);
239 auto PlatformKindOrError =
240 [](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::PlatformKind> {
241 if (T.isMacOSX())
242 return llvm::MachO::PlatformKind::macOS;
243 if (T.isTvOS())
244 return llvm::MachO::PlatformKind::tvOS;
245 if (T.isWatchOS())
246 return llvm::MachO::PlatformKind::watchOS;
247 // Note: put isiOS last because tvOS and watchOS are also iOS according
248 // to the Triple.
249 if (T.isiOS())
250 return llvm::MachO::PlatformKind::iOS;
252 // TODO: Add an option for ForceTriple, but keep ForceFormat for now.
253 if (ForceFormat == "TBD")
254 return llvm::MachO::PlatformKind::macOS;
256 return createStringError(errc::not_supported, "Invalid Platform.\n");
257 }(T);
259 if (!ArchOrError)
260 return -1;
262 if (!PlatformKindOrError)
263 return -1;
265 Architecture Arch = ArchOrError.get();
266 PlatformKind Plat = PlatformKindOrError.get();
268 InterfaceFile File;
269 File.setFileType(FileType::TBD_V3); // Only supporting v3 for now.
270 File.setArchitectures(Arch);
271 File.setPlatform(Plat);
273 for (const auto &Symbol : Symbols) {
274 auto Name = Symbol.Name;
275 auto Kind = SymbolKind::GlobalSymbol;
276 switch (Symbol.Type) {
277 default:
278 case IFSSymbolType::NoType:
279 Kind = SymbolKind::GlobalSymbol;
280 break;
281 case IFSSymbolType::Object:
282 Kind = SymbolKind::GlobalSymbol;
283 break;
284 case IFSSymbolType::Func:
285 Kind = SymbolKind::GlobalSymbol;
286 break;
288 if (Symbol.Weak)
289 File.addSymbol(Kind, Name, Arch, SymbolFlags::WeakDefined);
290 else
291 File.addSymbol(Kind, Name, Arch);
294 SmallString<4096> Buffer;
295 raw_svector_ostream OS(Buffer);
296 if (Error Result = TextAPIWriter::writeToStream(OS, File))
297 return -1;
298 Out << OS.str();
299 return 0;
302 int writeElfStub(const llvm::Triple &T, const std::set<IFSSymbol> &Symbols,
303 const StringRef Format, raw_ostream &Out) {
304 SmallString<0> Storage;
305 Storage.clear();
306 raw_svector_ostream OS(Storage);
308 OS << "--- !ELF\n";
309 OS << "FileHeader:\n";
310 OS << " Class: ELFCLASS";
311 OS << (T.isArch64Bit() ? "64" : "32");
312 OS << "\n";
313 OS << " Data: ELFDATA2";
314 OS << (T.isLittleEndian() ? "LSB" : "MSB");
315 OS << "\n";
316 OS << " Type: ET_DYN\n";
317 OS << " Machine: "
318 << llvm::StringSwitch<llvm::StringRef>(T.getArchName())
319 .Case("x86_64", "EM_X86_64")
320 .Case("i386", "EM_386")
321 .Case("i686", "EM_386")
322 .Case("aarch64", "EM_AARCH64")
323 .Case("amdgcn", "EM_AMDGPU")
324 .Case("r600", "EM_AMDGPU")
325 .Case("arm", "EM_ARM")
326 .Case("thumb", "EM_ARM")
327 .Case("avr", "EM_AVR")
328 .Case("mips", "EM_MIPS")
329 .Case("mipsel", "EM_MIPS")
330 .Case("mips64", "EM_MIPS")
331 .Case("mips64el", "EM_MIPS")
332 .Case("msp430", "EM_MSP430")
333 .Case("ppc", "EM_PPC")
334 .Case("ppc64", "EM_PPC64")
335 .Case("ppc64le", "EM_PPC64")
336 .Case("x86", T.isOSIAMCU() ? "EM_IAMCU" : "EM_386")
337 .Case("x86_64", "EM_X86_64")
338 .Default("EM_NONE")
339 << "\nSections:"
340 << "\n - Name: .text"
341 << "\n Type: SHT_PROGBITS"
342 << "\n - Name: .data"
343 << "\n Type: SHT_PROGBITS"
344 << "\n - Name: .rodata"
345 << "\n Type: SHT_PROGBITS"
346 << "\nSymbols:\n";
347 for (const auto &Symbol : Symbols) {
348 OS << " - Name: " << Symbol.Name << "\n"
349 << " Type: STT_";
350 switch (Symbol.Type) {
351 default:
352 case IFSSymbolType::NoType:
353 OS << "NOTYPE";
354 break;
355 case IFSSymbolType::Object:
356 OS << "OBJECT";
357 break;
358 case IFSSymbolType::Func:
359 OS << "FUNC";
360 break;
362 OS << "\n Section: .text"
363 << "\n Binding: STB_" << (Symbol.Weak ? "WEAK" : "GLOBAL")
364 << "\n";
366 OS << "...\n";
368 std::string YamlStr = OS.str();
370 // Only or debugging. Not an offical format.
371 LLVM_DEBUG({
372 if (ForceFormat == "ELFOBJYAML") {
373 Out << YamlStr;
374 return 0;
378 yaml::Input YIn(YamlStr);
379 if (Error E = convertYAML(YIn, Out)) {
380 logAllUnhandledErrors(std::move(E), WithColor::error(errs(), "llvm-ifs"));
381 return 1;
384 return 0;
387 int writeIfso(const IFSStub &Stub, bool IsWriteIfs, raw_ostream &Out) {
388 if (IsWriteIfs) {
389 yaml::Output YamlOut(Out, NULL, /*WrapColumn =*/0);
390 YamlOut << const_cast<IFSStub &>(Stub);
391 return 0;
394 std::string ObjectFileFormat =
395 ForceFormat.empty() ? Stub.ObjectFileFormat : ForceFormat;
397 if (ObjectFileFormat == "ELF" || ForceFormat == "ELFOBJYAML")
398 return writeElfStub(llvm::Triple(Stub.Triple), Stub.Symbols,
399 Stub.ObjectFileFormat, Out);
400 if (ObjectFileFormat == "TBD")
401 return writeTbdStub(llvm::Triple(Stub.Triple), Stub.Symbols,
402 Stub.ObjectFileFormat, Out);
404 WithColor::error()
405 << "Invalid ObjectFileFormat: Only ELF and TBD are supported.\n";
406 return -1;
409 // New Interface Stubs Yaml Format:
410 // --- !experimental-ifs-v1
411 // IfsVersion: 1.0
412 // Triple: <llvm triple>
413 // ObjectFileFormat: <ELF | others not yet supported>
414 // Symbols:
415 // _ZSymbolName: { Type: <type> }
416 // ...
418 int main(int argc, char *argv[]) {
419 // Parse arguments.
420 cl::ParseCommandLineOptions(argc, argv);
422 if (InputFilenames.empty())
423 InputFilenames.push_back("-");
425 IFSStub Stub;
426 std::map<std::string, IFSSymbol> SymbolMap;
428 std::string PreviousInputFilePath = "";
429 for (const std::string &InputFilePath : InputFilenames) {
430 Expected<std::unique_ptr<IFSStub>> StubOrErr = readInputFile(InputFilePath);
431 if (!StubOrErr) {
432 WithColor::error() << StubOrErr.takeError() << "\n";
433 return -1;
435 std::unique_ptr<IFSStub> TargetStub = std::move(StubOrErr.get());
437 if (Stub.Triple.empty()) {
438 PreviousInputFilePath = InputFilePath;
439 Stub.IfsVersion = TargetStub->IfsVersion;
440 Stub.Triple = TargetStub->Triple;
441 Stub.ObjectFileFormat = TargetStub->ObjectFileFormat;
442 Stub.SOName = TargetStub->SOName;
443 Stub.NeededLibs = TargetStub->NeededLibs;
444 } else {
445 if (Stub.IfsVersion != TargetStub->IfsVersion) {
446 if (Stub.IfsVersion.getMajor() != IFSVersionCurrent.getMajor()) {
447 WithColor::error()
448 << "Interface Stub: IfsVersion Mismatch."
449 << "\nFilenames: " << PreviousInputFilePath << " "
450 << InputFilePath << "\nIfsVersion Values: " << Stub.IfsVersion
451 << " " << TargetStub->IfsVersion << "\n";
452 return -1;
454 if (TargetStub->IfsVersion > Stub.IfsVersion)
455 Stub.IfsVersion = TargetStub->IfsVersion;
457 if (Stub.ObjectFileFormat != TargetStub->ObjectFileFormat) {
458 WithColor::error() << "Interface Stub: ObjectFileFormat Mismatch."
459 << "\nFilenames: " << PreviousInputFilePath << " "
460 << InputFilePath << "\nObjectFileFormat Values: "
461 << Stub.ObjectFileFormat << " "
462 << TargetStub->ObjectFileFormat << "\n";
463 return -1;
465 if (Stub.Triple != TargetStub->Triple) {
466 WithColor::error() << "Interface Stub: Triple Mismatch."
467 << "\nFilenames: " << PreviousInputFilePath << " "
468 << InputFilePath
469 << "\nTriple Values: " << Stub.Triple << " "
470 << TargetStub->Triple << "\n";
471 return -1;
473 if (Stub.SOName != TargetStub->SOName) {
474 WithColor::error() << "Interface Stub: SOName Mismatch."
475 << "\nFilenames: " << PreviousInputFilePath << " "
476 << InputFilePath
477 << "\nSOName Values: " << Stub.SOName << " "
478 << TargetStub->SOName << "\n";
479 return -1;
481 if (Stub.NeededLibs != TargetStub->NeededLibs) {
482 WithColor::error() << "Interface Stub: NeededLibs Mismatch."
483 << "\nFilenames: " << PreviousInputFilePath << " "
484 << InputFilePath << "\n";
485 return -1;
489 for (auto Symbol : TargetStub->Symbols) {
490 auto SI = SymbolMap.find(Symbol.Name);
491 if (SI == SymbolMap.end()) {
492 SymbolMap.insert(
493 std::pair<std::string, IFSSymbol>(Symbol.Name, Symbol));
494 continue;
497 assert(Symbol.Name == SI->second.Name && "Symbol Names Must Match.");
499 // Check conflicts:
500 if (Symbol.Type != SI->second.Type) {
501 WithColor::error() << "Interface Stub: Type Mismatch for "
502 << Symbol.Name << ".\nFilename: " << InputFilePath
503 << "\nType Values: " << getTypeName(SI->second.Type)
504 << " " << getTypeName(Symbol.Type) << "\n";
506 return -1;
508 if (Symbol.Size != SI->second.Size) {
509 WithColor::error() << "Interface Stub: Size Mismatch for "
510 << Symbol.Name << ".\nFilename: " << InputFilePath
511 << "\nSize Values: " << SI->second.Size << " "
512 << Symbol.Size << "\n";
514 return -1;
516 if (Symbol.Weak != SI->second.Weak) {
517 // TODO: Add conflict resolution for Weak vs non-Weak.
518 WithColor::error() << "Interface Stub: Weak Mismatch for "
519 << Symbol.Name << ".\nFilename: " << InputFilePath
520 << "\nWeak Values: " << SI->second.Weak << " "
521 << Symbol.Weak << "\n";
523 return -1;
525 // TODO: Not checking Warning. Will be dropped.
528 PreviousInputFilePath = InputFilePath;
531 if (Stub.IfsVersion != IFSVersionCurrent)
532 if (Stub.IfsVersion.getMajor() != IFSVersionCurrent.getMajor()) {
533 WithColor::error() << "Interface Stub: Bad IfsVersion: "
534 << Stub.IfsVersion << ", llvm-ifs supported version: "
535 << IFSVersionCurrent << ".\n";
536 return -1;
539 for (auto &Entry : SymbolMap)
540 Stub.Symbols.insert(Entry.second);
542 std::error_code SysErr;
544 // Open file for writing.
545 raw_fd_ostream Out(OutputFilename, SysErr);
546 if (SysErr) {
547 WithColor::error() << "Couldn't open " << OutputFilename
548 << " for writing.\n";
549 return -1;
552 return writeIfso(Stub, (Action == "write-ifs"), Out);