Skip to content

Commit 8b4acb0

Browse files
committed
[elfabi] Prepare elfabi/ifs merging.
This change implements unified text stub format and command line interface proposed in the elfabi/ifs merge plan. Differential Revision: https://reviews.llvm.org/D99399
1 parent 6c0e689 commit 8b4acb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1398
-300
lines changed

llvm/include/llvm/BinaryFormat/ELF.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#ifndef LLVM_BINARYFORMAT_ELF_H
2020
#define LLVM_BINARYFORMAT_ELF_H
2121

22+
#include "llvm/ADT/StringRef.h"
2223
#include <cstdint>
2324
#include <cstring>
25+
#include <string>
2426

2527
namespace llvm {
2628
namespace ELF {
@@ -1673,6 +1675,12 @@ enum {
16731675
ELFCOMPRESS_HIPROC = 0x7fffffff // End of processor-specific.
16741676
};
16751677

1678+
/// Convert an architecture name into ELF's e_machine value.
1679+
uint16_t convertArchNameToEMachine(StringRef Arch);
1680+
1681+
/// Convert an ELF's e_machine value into an architecture name.
1682+
StringRef convertEMachineToArchName(uint16_t EMachine);
1683+
16761684
} // end namespace ELF
16771685
} // end namespace llvm
16781686

llvm/include/llvm/InterfaceStub/ELFObjHandler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf);
3535
///
3636
/// @param FilePath File path for writing the ELF binary.
3737
/// @param Stub Source ELFStub to generate a binary ELF stub from.
38-
/// @param OutputFormat Target ELFType to write binary as.
3938
/// @param WriteIfChanged Whether or not to preserve timestamp if
4039
/// the output stays the same.
4140
Error writeBinaryStub(StringRef FilePath, const ELFStub &Stub,
42-
ELFTarget OutputFormat, bool WriteIfChanged = false);
41+
bool WriteIfChanged = false);
4342

4443
} // end namespace elfabi
4544
} // end namespace llvm

llvm/include/llvm/InterfaceStub/ELFStub.h

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_INTERFACESTUB_ELFSTUB_H
1616

1717
#include "llvm/BinaryFormat/ELF.h"
18+
#include "llvm/Support/Error.h"
1819
#include "llvm/Support/VersionTuple.h"
1920
#include <set>
2021
#include <vector>
@@ -34,8 +35,25 @@ enum class ELFSymbolType {
3435
Unknown = 16,
3536
};
3637

38+
enum class ELFEndiannessType {
39+
Little = ELF::ELFDATA2LSB,
40+
Big = ELF::ELFDATA2MSB,
41+
42+
// Endianness info is 1 bytes, 256 is safely out of rance.
43+
Unknown = 256,
44+
};
45+
46+
enum class ELFBitWidthType {
47+
ELF32 = ELF::ELFCLASS32,
48+
ELF64 = ELF::ELFCLASS64,
49+
50+
// Bit width info is 1 bytes, 256 is safely out of rance.
51+
Unknown = 256,
52+
};
53+
3754
struct ELFSymbol {
38-
ELFSymbol(std::string SymbolName) : Name(SymbolName) {}
55+
ELFSymbol() = default;
56+
explicit ELFSymbol(std::string SymbolName) : Name(std::move(SymbolName)) {}
3957
std::string Name;
4058
uint64_t Size;
4159
ELFSymbolType Type;
@@ -45,21 +63,42 @@ struct ELFSymbol {
4563
bool operator<(const ELFSymbol &RHS) const { return Name < RHS.Name; }
4664
};
4765

66+
struct IFSTarget {
67+
Optional<std::string> Triple;
68+
Optional<std::string> ObjectFormat;
69+
Optional<ELFArch> Arch;
70+
Optional<std::string> ArchString;
71+
Optional<ELFEndiannessType> Endianness;
72+
Optional<ELFBitWidthType> BitWidth;
73+
};
74+
4875
// A cumulative representation of ELF stubs.
4976
// Both textual and binary stubs will read into and write from this object.
50-
class ELFStub {
77+
struct ELFStub {
5178
// TODO: Add support for symbol versioning.
52-
public:
5379
VersionTuple TbeVersion;
5480
Optional<std::string> SoName;
55-
ELFArch Arch;
81+
IFSTarget Target;
5682
std::vector<std::string> NeededLibs;
57-
std::set<ELFSymbol> Symbols;
83+
std::vector<ELFSymbol> Symbols;
5884

5985
ELFStub() {}
6086
ELFStub(const ELFStub &Stub);
6187
ELFStub(ELFStub &&Stub);
6288
};
89+
90+
// Create a alias class for ELFStub.
91+
// LLVM's YAML library does not allow mapping a class with 2 traits,
92+
// which prevents us using 'Target:' field with different definitions.
93+
// This class makes it possible to map a second traits so the same data
94+
// structure can be used for 2 different yaml schema.
95+
struct ELFStubTriple : ELFStub {
96+
ELFStubTriple() {}
97+
ELFStubTriple(const ELFStub &Stub);
98+
ELFStubTriple(const ELFStubTriple &Stub);
99+
ELFStubTriple(ELFStubTriple &&Stub);
100+
};
101+
63102
} // end namespace elfabi
64103
} // end namespace llvm
65104

llvm/include/llvm/InterfaceStub/TBEHandler.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_INTERFACESTUB_TBEHANDLER_H
1616
#define LLVM_INTERFACESTUB_TBEHANDLER_H
1717

18+
#include "ELFStub.h"
1819
#include "llvm/Support/Error.h"
1920
#include "llvm/Support/VersionTuple.h"
2021
#include <memory>
@@ -27,7 +28,7 @@ class StringRef;
2728

2829
namespace elfabi {
2930

30-
class ELFStub;
31+
struct ELFStub;
3132

3233
const VersionTuple TBEVersionCurrent(1, 0);
3334

@@ -37,6 +38,22 @@ Expected<std::unique_ptr<ELFStub>> readTBEFromBuffer(StringRef Buf);
3738
/// Attempts to write an ELF interface file to a raw_ostream.
3839
Error writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub);
3940

41+
/// Override the target platform inforation in the text stub.
42+
Error overrideTBETarget(ELFStub &Stub, Optional<ELFArch> OverrideArch,
43+
Optional<ELFEndiannessType> OverrideEndianness,
44+
Optional<ELFBitWidthType> OverrideBitWidth,
45+
Optional<std::string> OverrideTriple);
46+
47+
/// Validate the target platform inforation in the text stub.
48+
Error validateTBETarget(ELFStub &Stub, bool ParseTriple);
49+
50+
/// Strips target platform information from the text stub.
51+
void stripTBETarget(ELFStub &Stub, bool StripTriple, bool StripArch,
52+
bool StripEndianness, bool StripBitWidth);
53+
54+
/// Parse llvm triple string into a IFSTarget struct.
55+
IFSTarget parseTriple(StringRef TripleStr);
56+
4057
} // end namespace elfabi
4158
} // end namespace llvm
4259

llvm/lib/BinaryFormat/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_llvm_component_library(LLVMBinaryFormat
22
AMDGPUMetadataVerifier.cpp
33
Dwarf.cpp
4+
ELF.cpp
45
MachO.cpp
56
Magic.cpp
67
Minidump.cpp

0 commit comments

Comments
 (0)