[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"
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/ObjectYAML/ObjectYAML.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
2019-09-14 00:00:16 +08:00
|
|
|
bool convertYAML(yaml::Input &YIn, raw_ostream &Out, ErrorHandler ErrHandler,
|
2020-06-05 20:50:59 +08:00
|
|
|
unsigned DocNum, uint64_t MaxSize) {
|
[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
|
|
|
unsigned CurDocNum = 0;
|
|
|
|
do {
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
}
|
2019-09-14 00:00:16 +08:00
|
|
|
|
2020-10-20 20:12:28 +08:00
|
|
|
if (Doc.Arch)
|
|
|
|
return yaml2archive(*Doc.Arch, Out, ErrHandler);
|
2019-09-14 00:00:16 +08:00
|
|
|
if (Doc.Elf)
|
2020-06-05 20:50:59 +08:00
|
|
|
return yaml2elf(*Doc.Elf, Out, ErrHandler, MaxSize);
|
2019-09-14 00:00:16 +08:00
|
|
|
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 10:44:49 +08:00
|
|
|
} while (YIn.nextDocument());
|
|
|
|
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
}
|
|
|
|
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
Storage.clear();
|
|
|
|
raw_svector_ostream OS(Storage);
|
|
|
|
|
|
|
|
yaml::Input YIn(Yaml);
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
|
2019-09-14 00:00:16 +08: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 10:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace yaml
|
|
|
|
} // namespace llvm
|