1 | //===-- yaml2obj.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/ObjectYAML/yaml2obj.h" |
10 | #include "llvm/ADT/StringExtras.h" |
11 | #include "llvm/ADT/Twine.h" |
12 | #include "llvm/Object/ObjectFile.h" |
13 | #include "llvm/ObjectYAML/ObjectYAML.h" |
14 | #include "llvm/Support/Errc.h" |
15 | #include "llvm/Support/WithColor.h" |
16 | #include "llvm/Support/YAMLTraits.h" |
17 | |
18 | namespace llvm { |
19 | namespace yaml { |
20 | |
21 | bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler, |
22 | unsigned DocNum, uint64_t MaxSize) { |
23 | unsigned CurDocNum = 0; |
24 | do { |
25 | if (++CurDocNum != DocNum) |
26 | continue; |
27 | |
28 | yaml::YamlObjectFile Doc; |
29 | YIn >> Doc; |
30 | if (std::error_code EC = YIn.error()) { |
31 | ErrHandler("failed to parse YAML input: " + EC.message()); |
32 | return false; |
33 | } |
34 | |
35 | if (Doc.Arch) |
36 | return yaml2archive(Doc&: *Doc.Arch, Out, EH: ErrHandler); |
37 | if (Doc.Elf) |
38 | return yaml2elf(Doc&: *Doc.Elf, Out, EH: ErrHandler, MaxSize); |
39 | if (Doc.Coff) |
40 | return yaml2coff(Doc&: *Doc.Coff, Out, EH: ErrHandler); |
41 | if (Doc.Goff) |
42 | return yaml2goff(Doc&: *Doc.Goff, Out, EH: ErrHandler); |
43 | if (Doc.MachO || Doc.FatMachO) |
44 | return yaml2macho(Doc, Out, EH: ErrHandler); |
45 | if (Doc.Minidump) |
46 | return yaml2minidump(Doc&: *Doc.Minidump, Out, EH: ErrHandler); |
47 | if (Doc.Offload) |
48 | return yaml2offload(Doc&: *Doc.Offload, Out, EH: ErrHandler); |
49 | if (Doc.Wasm) |
50 | return yaml2wasm(Doc&: *Doc.Wasm, Out, EH: ErrHandler); |
51 | if (Doc.Xcoff) |
52 | return yaml2xcoff(Doc&: *Doc.Xcoff, Out, EH: ErrHandler); |
53 | if (Doc.DXContainer) |
54 | return yaml2dxcontainer(Doc&: *Doc.DXContainer, Out, EH: ErrHandler); |
55 | |
56 | ErrHandler("unknown document type" ); |
57 | return false; |
58 | |
59 | } while (YIn.nextDocument()); |
60 | |
61 | ErrHandler("cannot find the " + Twine(DocNum) + |
62 | getOrdinalSuffix(Val: DocNum).data() + " document" ); |
63 | return false; |
64 | } |
65 | |
66 | std::unique_ptr<object::ObjectFile> |
67 | yaml2ObjectFile(SmallVectorImpl<char> &Storage, StringRef Yaml, |
68 | ErrorHandler ErrHandler) { |
69 | Storage.clear(); |
70 | raw_svector_ostream OS(Storage); |
71 | |
72 | yaml::Input YIn(Yaml); |
73 | if (!convertYAML(YIn, Out&: OS, ErrHandler)) |
74 | return {}; |
75 | |
76 | Expected<std::unique_ptr<object::ObjectFile>> ObjOrErr = |
77 | object::ObjectFile::createObjectFile( |
78 | Object: MemoryBufferRef(OS.str(), "YamlObject" )); |
79 | if (ObjOrErr) |
80 | return std::move(*ObjOrErr); |
81 | |
82 | ErrHandler(toString(E: ObjOrErr.takeError())); |
83 | return {}; |
84 | } |
85 | |
86 | } // namespace yaml |
87 | } // namespace llvm |
88 | |