llvm.org GIT mirror llvm / 274264c
[yaml2obj] Initial ELF section support. The current functionality is extremely basic and a bit rough around the edges, but it will flesh out in future commits. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183953 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 7 years ago
5 changed file(s) with 212 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
3535 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
3636 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
3737 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
38 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
39 // Just use 64, since it can hold 32-bit values too.
40 LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
3841
3942 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
4043 // since 64-bit can hold 32-bit values too.
4548 ELF_EM Machine;
4649 llvm::yaml::Hex64 Entry;
4750 };
51 struct Section {
52 StringRef Name;
53 ELF_SHT Type;
54 ELF_SHF Flags;
55 };
4856 struct Object {
4957 FileHeader Header;
58 std::vector
Sections;
5059 };
5160
5261 } // end namespace ELFYAML
5362 } // end namespace llvm
63
64 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Section)
5465
5566 namespace llvm {
5667 namespace yaml {
7687 };
7788
7889 template <>
90 struct ScalarEnumerationTraits {
91 static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
92 };
93
94 template <>
95 struct ScalarBitSetTraits {
96 static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
97 };
98
99 template <>
79100 struct MappingTraits {
80101 static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
102 };
103
104 template <>
105 struct MappingTraits {
106 static void mapping(IO &IO, ELFYAML::Section &Section);
81107 };
82108
83109 template <>
211211 #undef ECase
212212 }
213213
214 void ScalarEnumerationTraits::enumeration(
215 IO &IO, ELFYAML::ELF_SHT &Value) {
216 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
217 ECase(SHT_NULL)
218 ECase(SHT_PROGBITS)
219 ECase(SHT_SYMTAB)
220 ECase(SHT_STRTAB)
221 ECase(SHT_RELA)
222 ECase(SHT_HASH)
223 ECase(SHT_DYNAMIC)
224 ECase(SHT_NOTE)
225 ECase(SHT_NOBITS)
226 ECase(SHT_REL)
227 ECase(SHT_SHLIB)
228 ECase(SHT_DYNSYM)
229 ECase(SHT_INIT_ARRAY)
230 ECase(SHT_FINI_ARRAY)
231 ECase(SHT_PREINIT_ARRAY)
232 ECase(SHT_GROUP)
233 ECase(SHT_SYMTAB_SHNDX)
234 #undef ECase
235 }
236
237 void ScalarBitSetTraits::bitset(IO &IO,
238 ELFYAML::ELF_SHF &Value) {
239 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X);
240 BCase(SHF_WRITE)
241 BCase(SHF_ALLOC)
242 BCase(SHF_EXECINSTR)
243 BCase(SHF_MERGE)
244 BCase(SHF_STRINGS)
245 BCase(SHF_INFO_LINK)
246 BCase(SHF_LINK_ORDER)
247 BCase(SHF_OS_NONCONFORMING)
248 BCase(SHF_GROUP)
249 BCase(SHF_TLS)
250 #undef BCase
251 }
252
214253 void MappingTraits::mapping(IO &IO,
215254 ELFYAML::FileHeader &FileHdr) {
216255 IO.mapRequired("Class", FileHdr.Class);
220259 IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
221260 }
222261
262 void MappingTraits::mapping(IO &IO,
263 ELFYAML::Section &Section) {
264 IO.mapOptional("Name", Section.Name, StringRef());
265 IO.mapRequired("Type", Section.Type);
266 IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
267 }
268
223269 void MappingTraits::mapping(IO &IO, ELFYAML::Object &Object) {
224270 IO.mapRequired("FileHeader", Object.Header);
271 IO.mapOptional("Sections", Object.Sections);
225272 }
226273
227274 } // end namespace yaml
None config.suffixes = ['.test', '.ll']
0 config.suffixes = ['.test', '.ll', '.yaml']
0 # RUN: yaml2obj -format=elf %s | llvm-readobj -sections - | FileCheck %s
1 !ELF
2 FileHeader:
3 Class: ELFCLASS64
4 Data: ELFDATA2LSB
5 Type: ET_REL
6 Machine: EM_X86_64
7 Sections:
8 - Name: .text
9 Type: SHT_PROGBITS
10 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
11
12 # CHECK: Section {
13 # CHECK: Index: 0
14 # CHECK: Type: SHT_NULL (0x0)
15 #
16 # CHECK: Section {
17 # CHECK: Name: .text
18 # CHECK: Type: SHT_PROGBITS (0x1)
19 # CHECK-NEXT: Flags [ (0x6)
20 # CHECK-NEXT: SHF_ALLOC (0x2)
21 # CHECK-NEXT: SHF_EXECINSTR (0x4)
22 # CHECK-NEXT: ]
2121
2222 using namespace llvm;
2323
24 // There is similar code in yaml2coff, but with some slight COFF-specific
25 // variations like different initial state. Might be able to deduplicate
26 // some day, but also want to make sure that the Mach-O use case is served.
27 //
28 // This class has a deliberately small interface, since a lot of
29 // implementation variation is possible.
30 //
31 // TODO: Use an ordered container with a suffix-based comparison in order
32 // to deduplicate suffixes. std::map<> with a custom comparator is likely
33 // to be the simplest implementation, but a suffix trie could be more
34 // suitable for the job.
35 class StringTableBuilder {
36 /// \brief Indices of strings currently present in `Buf`.
37 StringMap StringIndices;
38 /// \brief The contents of the string table as we build it.
39 std::string Buf;
40 public:
41 StringTableBuilder() {
42 Buf.push_back('\0');
43 }
44 /// \returns Index of string in string table.
45 unsigned addString(StringRef S) {
46 StringMapEntry &Entry = StringIndices.GetOrCreateValue(S);
47 unsigned &I = Entry.getValue();
48 if (I != 0)
49 return I;
50 I = Buf.size();
51 Buf.append(S.begin(), S.end());
52 Buf.push_back('\0');
53 return I;
54 }
55 size_t size() const {
56 return Buf.size();
57 }
58 void writeToStream(raw_ostream &OS) {
59 OS.write(Buf.data(), Buf.size());
60 }
61 };
62
63 template
64 static size_t vectorDataSize(const std::vector &Vec) {
65 return Vec.size() * sizeof(T);
66 }
67
68 template
69 static void writeVectorData(raw_ostream &OS, const std::vector &Vec) {
70 OS.write((const char *)Vec.data(), vectorDataSize(Vec));
71 }
72
73 template
74 static void zero(T &Obj) {
75 memset(&Obj, 0, sizeof(Obj));
76 }
77
2478 template
2579 static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
26 const ELFYAML::FileHeader &Hdr = Doc.Header;
2780 using namespace llvm::ELF;
2881 using namespace llvm::object;
29 typename ELFObjectFile::Elf_Ehdr Header;
30 memset(&Header, 0, sizeof(Header));
82 typedef typename ELFObjectFile::Elf_Ehdr Elf_Ehdr;
83 typedef typename ELFObjectFile::Elf_Shdr Elf_Shdr;
84
85 const ELFYAML::FileHeader &Hdr = Doc.Header;
86
87 Elf_Ehdr Header;
88 zero(Header);
3189 Header.e_ident[EI_MAG0] = 0x7f;
3290 Header.e_ident[EI_MAG1] = 'E';
3391 Header.e_ident[EI_MAG2] = 'L';
3593 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
3694 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
3795 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
38
3996 Header.e_ident[EI_VERSION] = EV_CURRENT;
40
4197 // TODO: Implement ELF_ELFOSABI enum.
4298 Header.e_ident[EI_OSABI] = ELFOSABI_NONE;
4399 // TODO: Implement ELF_ABIVERSION enum.
46102 Header.e_machine = Hdr.Machine;
47103 Header.e_version = EV_CURRENT;
48104 Header.e_entry = Hdr.Entry;
49 Header.e_ehsize = sizeof(Header);
105 Header.e_ehsize = sizeof(Elf_Ehdr);
50106
51 // TODO: Section headers and program headers.
107 // TODO: Flesh out section header support.
108 // TODO: Program headers.
109
110 Header.e_shentsize = sizeof(Elf_Shdr);
111 // Immediately following the ELF header.
112 Header.e_shoff = sizeof(Header);
113 std::vector Sections = Doc.Sections;
114 if (Sections.empty() || Sections.front().Type != SHT_NULL) {
115 ELFYAML::Section S;
116 S.Type = SHT_NULL;
117 zero(S.Flags);
118 Sections.insert(Sections.begin(), S);
119 }
120 // "+ 1" for string table.
121 Header.e_shnum = Sections.size() + 1;
122 // Place section header string table last.
123 Header.e_shstrndx = Sections.size();
124
125 StringTableBuilder StrTab;
126 std::vector SHeaders;
127 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
128 const ELFYAML::Section &Sec = Sections[i];
129 Elf_Shdr SHeader;
130 zero(SHeader);
131 SHeader.sh_name = StrTab.addString(Sec.Name);
132 SHeader.sh_type = Sec.Type;
133 SHeader.sh_flags = Sec.Flags;
134 SHeader.sh_addr = 0;
135 SHeader.sh_offset = 0;
136 SHeader.sh_size = 0;
137 SHeader.sh_link = 0;
138 SHeader.sh_info = 0;
139 SHeader.sh_addralign = 1;
140 SHeader.sh_entsize = 0;
141 SHeaders.push_back(SHeader);
142 }
143
144 // String table header.
145 Elf_Shdr StrTabSHeader;
146 zero(StrTabSHeader);
147 StrTabSHeader.sh_name = 0;
148 StrTabSHeader.sh_type = SHT_STRTAB;
149 StrTabSHeader.sh_flags = 0;
150 StrTabSHeader.sh_addr = 0;
151 StrTabSHeader.sh_offset = Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
152 StrTabSHeader.sh_size = StrTab.size();
153 StrTabSHeader.sh_link = 0;
154 StrTabSHeader.sh_info = 0;
155 StrTabSHeader.sh_addralign = 1;
156 StrTabSHeader.sh_entsize = 0;
52157
53158 OS.write((const char *)&Header, sizeof(Header));
159 writeVectorData(OS, SHeaders);
160 OS.write((const char *)&StrTabSHeader, sizeof(StrTabSHeader));
161 StrTab.writeToStream(OS);
54162 }
55163
56164 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {