annotate llvm/lib/ObjectYAML/yaml2obj.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents c4bab56944e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===-- yaml2obj.cpp ------------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "llvm/ObjectYAML/yaml2obj.h"
anatofuz
parents:
diff changeset
10 #include "llvm/ADT/StringExtras.h"
anatofuz
parents:
diff changeset
11 #include "llvm/ADT/Twine.h"
anatofuz
parents:
diff changeset
12 #include "llvm/Object/ObjectFile.h"
anatofuz
parents:
diff changeset
13 #include "llvm/ObjectYAML/ObjectYAML.h"
anatofuz
parents:
diff changeset
14 #include "llvm/Support/Errc.h"
anatofuz
parents:
diff changeset
15 #include "llvm/Support/WithColor.h"
anatofuz
parents:
diff changeset
16 #include "llvm/Support/YAMLTraits.h"
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 namespace llvm {
anatofuz
parents:
diff changeset
19 namespace yaml {
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
22 unsigned DocNum, uint64_t MaxSize) {
150
anatofuz
parents:
diff changeset
23 unsigned CurDocNum = 0;
anatofuz
parents:
diff changeset
24 do {
anatofuz
parents:
diff changeset
25 if (++CurDocNum != DocNum)
anatofuz
parents:
diff changeset
26 continue;
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 yaml::YamlObjectFile Doc;
anatofuz
parents:
diff changeset
29 YIn >> Doc;
anatofuz
parents:
diff changeset
30 if (std::error_code EC = YIn.error()) {
anatofuz
parents:
diff changeset
31 ErrHandler("failed to parse YAML input: " + EC.message());
anatofuz
parents:
diff changeset
32 return false;
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
35 if (Doc.Arch)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
36 return yaml2archive(*Doc.Arch, Out, ErrHandler);
150
anatofuz
parents:
diff changeset
37 if (Doc.Elf)
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
38 return yaml2elf(*Doc.Elf, Out, ErrHandler, MaxSize);
150
anatofuz
parents:
diff changeset
39 if (Doc.Coff)
anatofuz
parents:
diff changeset
40 return yaml2coff(*Doc.Coff, Out, ErrHandler);
anatofuz
parents:
diff changeset
41 if (Doc.MachO || Doc.FatMachO)
anatofuz
parents:
diff changeset
42 return yaml2macho(Doc, Out, ErrHandler);
anatofuz
parents:
diff changeset
43 if (Doc.Minidump)
anatofuz
parents:
diff changeset
44 return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
45 if (Doc.Offload)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
46 return yaml2offload(*Doc.Offload, Out, ErrHandler);
150
anatofuz
parents:
diff changeset
47 if (Doc.Wasm)
anatofuz
parents:
diff changeset
48 return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
49 if (Doc.Xcoff)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
50 return yaml2xcoff(*Doc.Xcoff, Out, ErrHandler);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
51 if (Doc.DXContainer)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
52 return yaml2dxcontainer(*Doc.DXContainer, Out, ErrHandler);
150
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 ErrHandler("unknown document type");
anatofuz
parents:
diff changeset
55 return false;
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 } while (YIn.nextDocument());
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 ErrHandler("cannot find the " + Twine(DocNum) +
anatofuz
parents:
diff changeset
60 getOrdinalSuffix(DocNum).data() + " document");
anatofuz
parents:
diff changeset
61 return false;
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 std::unique_ptr<object::ObjectFile>
anatofuz
parents:
diff changeset
65 yaml2ObjectFile(SmallVectorImpl<char> &Storage, StringRef Yaml,
anatofuz
parents:
diff changeset
66 ErrorHandler ErrHandler) {
anatofuz
parents:
diff changeset
67 Storage.clear();
anatofuz
parents:
diff changeset
68 raw_svector_ostream OS(Storage);
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 yaml::Input YIn(Yaml);
anatofuz
parents:
diff changeset
71 if (!convertYAML(YIn, OS, ErrHandler))
anatofuz
parents:
diff changeset
72 return {};
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 Expected<std::unique_ptr<object::ObjectFile>> ObjOrErr =
anatofuz
parents:
diff changeset
75 object::ObjectFile::createObjectFile(
anatofuz
parents:
diff changeset
76 MemoryBufferRef(OS.str(), "YamlObject"));
anatofuz
parents:
diff changeset
77 if (ObjOrErr)
anatofuz
parents:
diff changeset
78 return std::move(*ObjOrErr);
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 ErrHandler(toString(ObjOrErr.takeError()));
anatofuz
parents:
diff changeset
81 return {};
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 } // namespace yaml
anatofuz
parents:
diff changeset
85 } // namespace llvm