[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
//===-- yaml2obj.cpp ------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ObjectYAML/yaml2obj.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/ObjectYAML/ObjectYAML.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
Error convertYAML(yaml::Input &YIn, raw_ostream &Out, unsigned DocNum) {
|
2019-09-13 17:12:38 +08:00
|
|
|
auto BoolToErr = [](bool Ret) -> Error {
|
|
|
|
if (!Ret)
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
return createStringError(errc::invalid_argument, "yaml2obj failed");
|
|
|
|
return Error::success();
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned CurDocNum = 0;
|
|
|
|
do {
|
|
|
|
if (++CurDocNum == DocNum) {
|
|
|
|
yaml::YamlObjectFile Doc;
|
|
|
|
YIn >> Doc;
|
|
|
|
if (std::error_code EC = YIn.error())
|
|
|
|
return createStringError(EC, "Failed to parse YAML input!");
|
|
|
|
if (Doc.Elf)
|
2019-09-13 17:12:38 +08:00
|
|
|
return BoolToErr(yaml2elf(*Doc.Elf, Out));
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
if (Doc.Coff)
|
2019-09-13 17:12:38 +08:00
|
|
|
return BoolToErr(yaml2coff(*Doc.Coff, Out));
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
if (Doc.MachO || Doc.FatMachO)
|
2019-09-13 17:12:38 +08:00
|
|
|
return BoolToErr(yaml2macho(Doc, Out));
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
if (Doc.Minidump)
|
2019-09-13 17:12:38 +08:00
|
|
|
return BoolToErr(yaml2minidump(*Doc.Minidump, Out));
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
if (Doc.Wasm)
|
2019-09-13 17:12:38 +08:00
|
|
|
return BoolToErr(yaml2wasm(*Doc.Wasm, Out));
|
[yaml2obj] Move core yaml2obj code into lib and include for use in unit tests
Reviewers: jhenderson, rupprecht, MaskRay, grimar, labath
Reviewed By: rupprecht
Subscribers: gribozavr, mgrang, seiya, mgorny, sbc100, hiraditya, aheejin, jakehehrlich, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65255
llvm-svn: 368119
2019-08-07 10:44:49 +08:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"Unknown document type!");
|
|
|
|
}
|
|
|
|
} while (YIn.nextDocument());
|
|
|
|
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"Cannot find the %u%s document", DocNum,
|
|
|
|
getOrdinalSuffix(DocNum).data());
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<object::ObjectFile>>
|
|
|
|
yaml2ObjectFile(SmallVectorImpl<char> &Storage, StringRef Yaml) {
|
|
|
|
Storage.clear();
|
|
|
|
raw_svector_ostream OS(Storage);
|
|
|
|
|
|
|
|
yaml::Input YIn(Yaml);
|
|
|
|
if (Error E = convertYAML(YIn, OS))
|
|
|
|
return std::move(E);
|
|
|
|
|
|
|
|
return object::ObjectFile::createObjectFile(
|
|
|
|
MemoryBufferRef(OS.str(), "YamlObject"));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace yaml
|
|
|
|
} // namespace llvm
|