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/Object/ObjectFile.h"
12 #include "llvm/ObjectYAML/ObjectYAML.h"
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/YAMLTraits.h"
19 Error
convertYAML(yaml::Input
&YIn
, raw_ostream
&Out
, unsigned DocNum
) {
20 // TODO: make yaml2* functions return Error instead of int.
21 auto IntToErr
= [](int Ret
) -> Error
{
23 return createStringError(errc::invalid_argument
, "yaml2obj failed");
24 return Error::success();
27 unsigned CurDocNum
= 0;
29 if (++CurDocNum
== DocNum
) {
30 yaml::YamlObjectFile Doc
;
32 if (std::error_code EC
= YIn
.error())
33 return createStringError(EC
, "Failed to parse YAML input!");
35 return IntToErr(yaml2elf(*Doc
.Elf
, Out
));
37 return IntToErr(yaml2coff(*Doc
.Coff
, Out
));
38 if (Doc
.MachO
|| Doc
.FatMachO
)
39 return IntToErr(yaml2macho(Doc
, Out
));
41 return IntToErr(yaml2minidump(*Doc
.Minidump
, Out
));
43 return IntToErr(yaml2wasm(*Doc
.Wasm
, Out
));
44 return createStringError(errc::invalid_argument
,
45 "Unknown document type!");
47 } while (YIn
.nextDocument());
49 return createStringError(errc::invalid_argument
,
50 "Cannot find the %u%s document", DocNum
,
51 getOrdinalSuffix(DocNum
).data());
54 Expected
<std::unique_ptr
<object::ObjectFile
>>
55 yaml2ObjectFile(SmallVectorImpl
<char> &Storage
, StringRef Yaml
) {
57 raw_svector_ostream
OS(Storage
);
59 yaml::Input
YIn(Yaml
);
60 if (Error E
= convertYAML(YIn
, OS
))
63 return object::ObjectFile::createObjectFile(
64 MemoryBufferRef(OS
.str(), "YamlObject"));