[yaml2obj][ELF] Remove unused ELFState class field.
[oota-llvm.git] / tools / yaml2obj / yaml2elf.cpp
1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief The ELF component of yaml2obj.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "yaml2obj.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/Object/ELFObjectFile.h"
18 #include "llvm/Object/ELFYAML.h"
19 #include "llvm/Support/ELF.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/YAMLTraits.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 using namespace llvm;
25
26 // There is similar code in yaml2coff, but with some slight COFF-specific
27 // variations like different initial state. Might be able to deduplicate
28 // some day, but also want to make sure that the Mach-O use case is served.
29 //
30 // This class has a deliberately small interface, since a lot of
31 // implementation variation is possible.
32 //
33 // TODO: Use an ordered container with a suffix-based comparison in order
34 // to deduplicate suffixes. std::map<> with a custom comparator is likely
35 // to be the simplest implementation, but a suffix trie could be more
36 // suitable for the job.
37 namespace {
38 class StringTableBuilder {
39   /// \brief Indices of strings currently present in `Buf`.
40   StringMap<unsigned> StringIndices;
41   /// \brief The contents of the string table as we build it.
42   std::string Buf;
43 public:
44   StringTableBuilder() {
45     Buf.push_back('\0');
46   }
47   /// \returns Index of string in string table.
48   unsigned addString(StringRef S) {
49     StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S);
50     unsigned &I = Entry.getValue();
51     if (I != 0)
52       return I;
53     I = Buf.size();
54     Buf.append(S.begin(), S.end());
55     Buf.push_back('\0');
56     return I;
57   }
58   size_t size() const {
59     return Buf.size();
60   }
61   void writeToStream(raw_ostream &OS) {
62     OS.write(Buf.data(), Buf.size());
63   }
64 };
65 } // end anonymous namespace
66
67 // This class is used to build up a contiguous binary blob while keeping
68 // track of an offset in the output (which notionally begins at
69 // `InitialOffset`).
70 namespace {
71 class ContiguousBlobAccumulator {
72   const uint64_t InitialOffset;
73   SmallVector<char, 128> Buf;
74   raw_svector_ostream OS;
75
76   /// \returns The new offset.
77   uint64_t padToAlignment(unsigned Align) {
78     uint64_t CurrentOffset = InitialOffset + OS.tell();
79     uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
80     for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
81       OS.write('\0');
82     return AlignedOffset; // == CurrentOffset;
83   }
84
85 public:
86   ContiguousBlobAccumulator(uint64_t InitialOffset_)
87       : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
88   template <class Integer>
89   raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
90     Offset = padToAlignment(Align);
91     return OS;
92   }
93   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
94 };
95 } // end anonymous namespace
96
97 // Used to keep track of section names, so that in the YAML file sections
98 // can be referenced by name instead of by index.
99 namespace {
100 class SectionNameToIdxMap {
101   StringMap<int> Map;
102 public:
103   /// \returns true if name is already present in the map.
104   bool addName(StringRef SecName, unsigned i) {
105     StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1);
106     if (Entry.getValue() != -1)
107       return true;
108     Entry.setValue((int)i);
109     return false;
110   }
111   /// \returns true if name is not present in the map
112   bool lookupSection(StringRef SecName, unsigned &Idx) const {
113     StringMap<int>::const_iterator I = Map.find(SecName);
114     if (I == Map.end())
115       return true;
116     Idx = I->getValue();
117     return false;
118   }
119 };
120 } // end anonymous namespace
121
122 template <class T>
123 static size_t arrayDataSize(ArrayRef<T> A) {
124   return A.size() * sizeof(T);
125 }
126
127 template <class T>
128 static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
129   OS.write((const char *)A.data(), arrayDataSize(A));
130 }
131
132 template <class T>
133 static void zero(T &Obj) {
134   memset(&Obj, 0, sizeof(Obj));
135 }
136
137 /// \brief Create a string table in `SHeader`, which we assume is already
138 /// zero'd.
139 template <class Elf_Shdr>
140 static void createStringTableSectionHeader(Elf_Shdr &SHeader,
141                                            StringTableBuilder &STB,
142                                            ContiguousBlobAccumulator &CBA) {
143   SHeader.sh_type = ELF::SHT_STRTAB;
144   STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
145   SHeader.sh_size = STB.size();
146   SHeader.sh_addralign = 1;
147 }
148
149 namespace {
150 /// \brief "Single point of truth" for the ELF file construction.
151 /// TODO: This class still has a ways to go before it is truly a "single
152 /// point of truth".
153 template <class ELFT>
154 class ELFState {
155   /// \brief The future ".strtab" section.
156   StringTableBuilder DotStrtab;
157   /// \brief The section number of the ".strtab" section.
158   unsigned DotStrtabSecNo;
159   /// \brief The accumulated contents of all sections so far.
160   ContiguousBlobAccumulator &SectionContentAccum;
161   typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
162
163   SectionNameToIdxMap &SN2I;
164
165 public:
166   ELFState(ContiguousBlobAccumulator &Accum, unsigned DotStrtabSecNo_,
167            SectionNameToIdxMap &SN2I_)
168       : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_),
169         SectionContentAccum(Accum), SN2I(SN2I_) {}
170
171   unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; }
172   StringTableBuilder &getStringTable() { return DotStrtab; }
173   ContiguousBlobAccumulator &getSectionContentAccum() {
174     return SectionContentAccum;
175   }
176   SectionNameToIdxMap &getSN2I() { return SN2I; }
177 };
178 } // end anonymous namespace
179
180 // FIXME: At this point it is fairly clear that we need to refactor these
181 // static functions into methods of a class sharing some typedefs. These
182 // ELF type names are insane.
183 template <class ELFT>
184 static void
185 addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, ELFState<ELFT> &State,
186            std::vector<typename object::ELFFile<ELFT>::Elf_Sym> &Syms,
187            unsigned SymbolBinding) {
188   typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
189   for (unsigned i = 0, e = Symbols.size(); i != e; ++i) {
190     const ELFYAML::Symbol &Sym = Symbols[i];
191     Elf_Sym Symbol;
192     zero(Symbol);
193     if (!Sym.Name.empty())
194       Symbol.st_name = State.getStringTable().addString(Sym.Name);
195     Symbol.setBindingAndType(SymbolBinding, Sym.Type);
196     if (!Sym.Section.empty()) {
197       unsigned Index;
198       if (State.getSN2I().lookupSection(Sym.Section, Index)) {
199         errs() << "error: Unknown section referenced: '" << Sym.Section
200                << "' by YAML symbol " << Sym.Name << ".\n";
201         exit(1);
202       }
203       Symbol.st_shndx = Index;
204     } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
205     Symbol.st_value = Sym.Value;
206     Symbol.st_size = Sym.Size;
207     Syms.push_back(Symbol);
208   }
209 }
210
211 template <class ELFT>
212 static void
213 handleSymtabSectionHeader(const ELFYAML::LocalGlobalWeakSymbols &Symbols,
214                           ELFState<ELFT> &State,
215                           typename object::ELFFile<ELFT>::Elf_Shdr &SHeader) {
216
217   typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
218   SHeader.sh_type = ELF::SHT_SYMTAB;
219   SHeader.sh_link = State.getDotStrTabSecNo();
220   // One greater than symbol table index of the last local symbol.
221   SHeader.sh_info = Symbols.Local.size() + 1;
222   SHeader.sh_entsize = sizeof(Elf_Sym);
223
224   std::vector<Elf_Sym> Syms;
225   {
226     // Ensure STN_UNDEF is present
227     Elf_Sym Sym;
228     zero(Sym);
229     Syms.push_back(Sym);
230   }
231   addSymbols(Symbols.Local, State, Syms, ELF::STB_LOCAL);
232   addSymbols(Symbols.Global, State, Syms, ELF::STB_GLOBAL);
233   addSymbols(Symbols.Weak, State, Syms, ELF::STB_WEAK);
234
235   ContiguousBlobAccumulator &CBA = State.getSectionContentAccum();
236   writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
237                  makeArrayRef(Syms));
238   SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
239 }
240
241 template <class ELFT>
242 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
243   using namespace llvm::ELF;
244   typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
245   typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
246
247   const ELFYAML::FileHeader &Hdr = Doc.Header;
248
249   Elf_Ehdr Header;
250   zero(Header);
251   Header.e_ident[EI_MAG0] = 0x7f;
252   Header.e_ident[EI_MAG1] = 'E';
253   Header.e_ident[EI_MAG2] = 'L';
254   Header.e_ident[EI_MAG3] = 'F';
255   Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
256   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
257   Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
258   Header.e_ident[EI_VERSION] = EV_CURRENT;
259   Header.e_ident[EI_OSABI] = Hdr.OSABI;
260   Header.e_ident[EI_ABIVERSION] = 0;
261   Header.e_type = Hdr.Type;
262   Header.e_machine = Hdr.Machine;
263   Header.e_version = EV_CURRENT;
264   Header.e_entry = Hdr.Entry;
265   Header.e_ehsize = sizeof(Elf_Ehdr);
266
267   // TODO: Flesh out section header support.
268   // TODO: Program headers.
269
270   Header.e_shentsize = sizeof(Elf_Shdr);
271   // Immediately following the ELF header.
272   Header.e_shoff = sizeof(Header);
273   const std::vector<ELFYAML::Section> &Sections = Doc.Sections;
274   // "+ 4" for
275   // - SHT_NULL entry (placed first, i.e. 0'th entry)
276   // - symbol table (.symtab) (placed third to last)
277   // - string table (.strtab) (placed second to last)
278   // - section header string table. (placed last)
279   Header.e_shnum = Sections.size() + 4;
280   // Place section header string table last.
281   Header.e_shstrndx = Header.e_shnum - 1;
282   const unsigned DotStrtabSecNo = Header.e_shnum - 2;
283
284   // XXX: This offset is tightly coupled with the order that we write
285   // things to `OS`.
286   const size_t SectionContentBeginOffset =
287       Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
288   ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
289   SectionNameToIdxMap SN2I;
290   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
291     StringRef Name = Sections[i].Name;
292     if (Name.empty())
293       continue;
294     // "+ 1" to take into account the SHT_NULL entry.
295     if (SN2I.addName(Name, i + 1)) {
296       errs() << "error: Repeated section name: '" << Name
297              << "' at YAML section number " << i << ".\n";
298       return 1;
299     }
300   }
301
302   ELFState<ELFT> State(CBA, DotStrtabSecNo, SN2I);
303
304   StringTableBuilder SHStrTab;
305   std::vector<Elf_Shdr> SHeaders;
306   {
307     // Ensure SHN_UNDEF entry is present. An all-zero section header is a
308     // valid SHN_UNDEF entry since SHT_NULL == 0.
309     Elf_Shdr SHdr;
310     zero(SHdr);
311     SHeaders.push_back(SHdr);
312   }
313   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
314     const ELFYAML::Section &Sec = Sections[i];
315     Elf_Shdr SHeader;
316     zero(SHeader);
317     SHeader.sh_name = SHStrTab.addString(Sec.Name);
318     SHeader.sh_type = Sec.Type;
319     SHeader.sh_flags = Sec.Flags;
320     SHeader.sh_addr = Sec.Address;
321
322     Sec.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
323     SHeader.sh_size = Sec.Content.binary_size();
324
325     if (!Sec.Link.empty()) {
326       unsigned Index;
327       if (SN2I.lookupSection(Sec.Link, Index)) {
328         errs() << "error: Unknown section referenced: '" << Sec.Link
329                << "' at YAML section number " << i << ".\n";
330         return 1;
331       }
332       SHeader.sh_link = Index;
333     }
334     SHeader.sh_info = 0;
335     SHeader.sh_addralign = Sec.AddressAlign;
336     SHeader.sh_entsize = 0;
337     SHeaders.push_back(SHeader);
338   }
339
340   // .symtab section.
341   Elf_Shdr SymtabSHeader;
342   zero(SymtabSHeader);
343   SymtabSHeader.sh_name = SHStrTab.addString(StringRef(".symtab"));
344   handleSymtabSectionHeader<ELFT>(Doc.Symbols, State, SymtabSHeader);
345   SHeaders.push_back(SymtabSHeader);
346
347   // .strtab string table header.
348   Elf_Shdr DotStrTabSHeader;
349   zero(DotStrTabSHeader);
350   DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab"));
351   createStringTableSectionHeader(DotStrTabSHeader, State.getStringTable(), CBA);
352   SHeaders.push_back(DotStrTabSHeader);
353
354   // Section header string table header.
355   Elf_Shdr SHStrTabSHeader;
356   zero(SHStrTabSHeader);
357   SHStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".shstrtab"));
358   createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA);
359   SHeaders.push_back(SHStrTabSHeader);
360
361   OS.write((const char *)&Header, sizeof(Header));
362   writeArrayData(OS, makeArrayRef(SHeaders));
363   CBA.writeBlobToStream(OS);
364   return 0;
365 }
366
367 static bool is64Bit(const ELFYAML::Object &Doc) {
368   return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
369 }
370
371 static bool isLittleEndian(const ELFYAML::Object &Doc) {
372   return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
373 }
374
375 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
376   yaml::Input YIn(Buf->getBuffer());
377   ELFYAML::Object Doc;
378   YIn >> Doc;
379   if (YIn.error()) {
380     errs() << "yaml2obj: Failed to parse YAML file!\n";
381     return 1;
382   }
383   using object::ELFType;
384   typedef ELFType<support::little, 8, true> LE64;
385   typedef ELFType<support::big, 8, true> BE64;
386   typedef ELFType<support::little, 4, false> LE32;
387   typedef ELFType<support::big, 4, false> BE32;
388   if (is64Bit(Doc)) {
389     if (isLittleEndian(Doc))
390       return writeELF<LE64>(outs(), Doc);
391     else
392       return writeELF<BE64>(outs(), Doc);
393   } else {
394     if (isLittleEndian(Doc))
395       return writeELF<LE32>(outs(), Doc);
396     else
397       return writeELF<BE32>(outs(), Doc);
398   }
399 }