[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 04:44:49 +02: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"
|
2019-09-13 18:00:16 +02:00
|
|
|
#include "llvm/ADT/Twine.h"
|
[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 04:44:49 +02:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/ObjectYAML/ObjectYAML.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
2019-09-13 18:00:16 +02:00
|
|
|
#include "llvm/Support/WithColor.h"
|
[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 04:44:49 +02:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
2019-09-13 18:00:16 +02:00
|
|
|
bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
|
|
|
|
unsigned DocNum) {
|
[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 04:44:49 +02:00
|
|
|
unsigned CurDocNum = 0;
|
|
|
|
do {
|
2019-09-13 18:00:16 +02:00
|
|
|
if (++CurDocNum != DocNum)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
yaml::YamlObjectFile Doc;
|
|
|
|
YIn >> Doc;
|
|
|
|
if (std::error_code EC = YIn.error()) {
|
|
|
|
ErrHandler("failed to parse YAML input: " + EC.message());
|
|
|
|
return false;
|
[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 04:44:49 +02:00
|
|
|
}
|
2019-09-13 18:00:16 +02:00
|
|
|
|
|
|
|
if (Doc.Elf)
|
|
|
|
return yaml2elf(*Doc.Elf, Out, ErrHandler);
|
|
|
|
if (Doc.Coff)
|
|
|
|
return yaml2coff(*Doc.Coff, Out, ErrHandler);
|
|
|
|
if (Doc.MachO || Doc.FatMachO)
|
|
|
|
return yaml2macho(Doc, Out, ErrHandler);
|
|
|
|
if (Doc.Minidump)
|
|
|
|
return yaml2minidump(*Doc.Minidump, Out, ErrHandler);
|
|
|
|
if (Doc.Wasm)
|
|
|
|
return yaml2wasm(*Doc.Wasm, Out, ErrHandler);
|
|
|
|
|
|
|
|
ErrHandler("unknown document type");
|
|
|
|
return false;
|
|
|
|
|
[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 04:44:49 +02:00
|
|
|
} while (YIn.nextDocument());
|
|
|
|
|
2019-09-13 18:00:16 +02:00
|
|
|
ErrHandler("cannot find the " + Twine(DocNum) +
|
|
|
|
getOrdinalSuffix(DocNum).data() + " document");
|
|
|
|
return false;
|
[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 04:44:49 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 18:00:16 +02:00
|
|
|
std::unique_ptr<object::ObjectFile>
|
|
|
|
yaml2ObjectFile(SmallVectorImpl<char> &Storage, StringRef Yaml,
|
|
|
|
ErrorHandler ErrHandler) {
|
[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 04:44:49 +02:00
|
|
|
Storage.clear();
|
|
|
|
raw_svector_ostream OS(Storage);
|
|
|
|
|
|
|
|
yaml::Input YIn(Yaml);
|
2019-09-13 18:00:16 +02:00
|
|
|
if (!convertYAML(YIn, OS, ErrHandler))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<object::ObjectFile>> ObjOrErr =
|
|
|
|
object::ObjectFile::createObjectFile(
|
|
|
|
MemoryBufferRef(OS.str(), "YamlObject"));
|
|
|
|
if (ObjOrErr)
|
|
|
|
return std::move(*ObjOrErr);
|
[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 04:44:49 +02:00
|
|
|
|
2019-09-13 18:00:16 +02:00
|
|
|
ErrHandler(toString(ObjOrErr.takeError()));
|
|
|
|
return {};
|
[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 04:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace yaml
|
|
|
|
} // namespace llvm
|