1 //===-- yaml2obj.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
21 bool convertYAML(yaml::Input
&YIn
, raw_ostream
&Out
, ErrorHandler ErrHandler
,
22 unsigned DocNum
, uint64_t MaxSize
) {
23 unsigned CurDocNum
= 0;
25 if (++CurDocNum
!= DocNum
)
28 yaml::YamlObjectFile Doc
;
30 if (std::error_code EC
= YIn
.error()) {
31 ErrHandler("failed to parse YAML input: " + EC
.message());
36 return yaml2archive(*Doc
.Arch
, Out
, ErrHandler
);
38 return yaml2elf(*Doc
.Elf
, Out
, ErrHandler
, MaxSize
);
40 return yaml2coff(*Doc
.Coff
, Out
, ErrHandler
);
42 return yaml2goff(*Doc
.Goff
, Out
, ErrHandler
);
43 if (Doc
.MachO
|| Doc
.FatMachO
)
44 return yaml2macho(Doc
, Out
, ErrHandler
);
46 return yaml2minidump(*Doc
.Minidump
, Out
, ErrHandler
);
48 return yaml2offload(*Doc
.Offload
, Out
, ErrHandler
);
50 return yaml2wasm(*Doc
.Wasm
, Out
, ErrHandler
);
52 return yaml2xcoff(*Doc
.Xcoff
, Out
, ErrHandler
);
54 return yaml2dxcontainer(*Doc
.DXContainer
, Out
, ErrHandler
);
56 ErrHandler("unknown document type");
59 } while (YIn
.nextDocument());
61 ErrHandler("cannot find the " + Twine(DocNum
) +
62 getOrdinalSuffix(DocNum
).data() + " document");
66 std::unique_ptr
<object::ObjectFile
>
67 yaml2ObjectFile(SmallVectorImpl
<char> &Storage
, StringRef Yaml
,
68 ErrorHandler ErrHandler
) {
70 raw_svector_ostream
OS(Storage
);
72 yaml::Input
YIn(Yaml
);
73 if (!convertYAML(YIn
, OS
, ErrHandler
))
76 Expected
<std::unique_ptr
<object::ObjectFile
>> ObjOrErr
=
77 object::ObjectFile::createObjectFile(
78 MemoryBufferRef(OS
.str(), "YamlObject"));
80 return std::move(*ObjOrErr
);
82 ErrHandler(toString(ObjOrErr
.takeError()));