-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[yaml2obj][obj2yaml] - Teach tools to work with regular archives.
This teaches obj2yaml to dump valid regular (not thin) archives. This also teaches yaml2obj to recognize archives YAML descriptions, what allows to craft all different kinds of archives (valid and broken ones). Differential revision: https://reviews.llvm.org/D89949
- Loading branch information
Georgii Rymar
committed
Oct 28, 2020
1 parent
f53d7f5
commit 47369e1
Showing
14 changed files
with
628 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//===- ArchiveYAML.h - Archive YAMLIO implementation ------------*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file declares classes for handling the YAML representation of archives. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_OBJECTYAML_ARCHIVEYAML_H | ||
#define LLVM_OBJECTYAML_ARCHIVEYAML_H | ||
|
||
#include "llvm/Support/YAMLTraits.h" | ||
#include "llvm/ObjectYAML/YAML.h" | ||
#include "llvm/ADT/MapVector.h" | ||
|
||
namespace llvm { | ||
namespace ArchYAML { | ||
|
||
struct Archive { | ||
struct Child { | ||
struct Field { | ||
Field() = default; | ||
Field(StringRef Default, unsigned Length) | ||
: DefaultValue(Default), MaxLength(Length) {} | ||
StringRef Value; | ||
StringRef DefaultValue; | ||
unsigned MaxLength; | ||
}; | ||
|
||
Child() { | ||
Fields["Name"] = {"", 16}; | ||
Fields["LastModified"] = {"0", 12}; | ||
Fields["UID"] = {"0", 6}; | ||
Fields["GID"] = {"0", 6}; | ||
Fields["AccessMode"] = {"0", 8}; | ||
Fields["Size"] = {"0", 10}; | ||
Fields["Terminator"] = {"`\n", 2}; | ||
} | ||
|
||
MapVector<StringRef, Field> Fields; | ||
|
||
Optional<yaml::BinaryRef> Content; | ||
Optional<llvm::yaml::Hex8> PaddingByte; | ||
}; | ||
|
||
StringRef Magic; | ||
Optional<std::vector<Child>> Members; | ||
Optional<yaml::BinaryRef> Content; | ||
}; | ||
|
||
} // end namespace ArchYAML | ||
} // end namespace llvm | ||
|
||
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ArchYAML::Archive::Child) | ||
|
||
namespace llvm { | ||
namespace yaml { | ||
|
||
template <> struct MappingTraits<ArchYAML::Archive> { | ||
static void mapping(IO &IO, ArchYAML::Archive &A); | ||
static std::string validate(IO &, ArchYAML::Archive &A); | ||
}; | ||
|
||
template <> struct MappingTraits<ArchYAML::Archive::Child> { | ||
static void mapping(IO &IO, ArchYAML::Archive::Child &C); | ||
static std::string validate(IO &, ArchYAML::Archive::Child &C); | ||
}; | ||
|
||
} // end namespace yaml | ||
} // end namespace llvm | ||
|
||
#endif // LLVM_OBJECTYAML_ARCHIVEYAML_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===- ArchiveEmitter.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/ArchiveYAML.h" | ||
#include "llvm/ObjectYAML/yaml2obj.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
using namespace llvm; | ||
using namespace ArchYAML; | ||
|
||
namespace llvm { | ||
namespace yaml { | ||
|
||
bool yaml2archive(ArchYAML::Archive &Doc, raw_ostream &Out, ErrorHandler EH) { | ||
Out.write(Doc.Magic.data(), Doc.Magic.size()); | ||
|
||
if (Doc.Content) { | ||
Doc.Content->writeAsBinary(Out); | ||
return true; | ||
} | ||
|
||
if (!Doc.Members) | ||
return true; | ||
|
||
auto WriteField = [&](StringRef Field, uint8_t Size) { | ||
Out.write(Field.data(), Field.size()); | ||
for (size_t I = Field.size(); I != Size; ++I) | ||
Out.write(' '); | ||
}; | ||
|
||
for (const Archive::Child &C : *Doc.Members) { | ||
for (auto &P : C.Fields) | ||
WriteField(P.second.Value, P.second.MaxLength); | ||
|
||
if (C.Content) | ||
C.Content->writeAsBinary(Out); | ||
if (C.PaddingByte) | ||
Out.write(*C.PaddingByte); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace yaml | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//===- ArchiveYAML.cpp - ELF YAMLIO implementation -------------------- ----===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines classes for handling the YAML representation of archives. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ObjectYAML/ArchiveYAML.h" | ||
|
||
namespace llvm { | ||
|
||
namespace yaml { | ||
|
||
void MappingTraits<ArchYAML::Archive>::mapping(IO &IO, ArchYAML::Archive &A) { | ||
assert(!IO.getContext() && "The IO context is initialized already"); | ||
IO.setContext(&A); | ||
IO.mapTag("!Arch", true); | ||
IO.mapOptional("Magic", A.Magic, "!<arch>\n"); | ||
IO.mapOptional("Members", A.Members); | ||
IO.mapOptional("Content", A.Content); | ||
IO.setContext(nullptr); | ||
} | ||
|
||
std::string MappingTraits<ArchYAML::Archive>::validate(IO &, | ||
ArchYAML::Archive &A) { | ||
if (A.Members && A.Content) | ||
return "\"Content\" and \"Members\" cannot be used together"; | ||
return ""; | ||
} | ||
|
||
void MappingTraits<ArchYAML::Archive::Child>::mapping( | ||
IO &IO, ArchYAML::Archive::Child &E) { | ||
assert(IO.getContext() && "The IO context is not initialized"); | ||
for (auto &P : E.Fields) | ||
IO.mapOptional(P.first.data(), P.second.Value, P.second.DefaultValue); | ||
IO.mapOptional("Content", E.Content); | ||
IO.mapOptional("PaddingByte", E.PaddingByte); | ||
} | ||
|
||
std::string | ||
MappingTraits<ArchYAML::Archive::Child>::validate(IO &, | ||
ArchYAML::Archive::Child &C) { | ||
for (auto &P : C.Fields) | ||
if (P.second.Value.size() > P.second.MaxLength) | ||
return ("the maximum length of \"" + P.first + "\" field is " + | ||
Twine(P.second.MaxLength)) | ||
.str(); | ||
return ""; | ||
} | ||
|
||
} // end namespace yaml | ||
|
||
} // end namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.