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/Object/ELF.h"
17#include "llvm/Object/ELFYAML.h"
18#include "llvm/Support/ELF.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/YAMLTraits.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace llvm;
24
25// There is similar code in yaml2coff, but with some slight COFF-specific
26// variations like different initial state. Might be able to deduplicate
27// some day, but also want to make sure that the Mach-O use case is served.
28//
29// This class has a deliberately small interface, since a lot of
30// implementation variation is possible.
31//
32// TODO: Use an ordered container with a suffix-based comparison in order
33// to deduplicate suffixes. std::map<> with a custom comparator is likely
34// to be the simplest implementation, but a suffix trie could be more
35// suitable for the job.
36namespace {
37class StringTableBuilder {
38  /// \brief Indices of strings currently present in `Buf`.
39  StringMap<unsigned> StringIndices;
40  /// \brief The contents of the string table as we build it.
41  std::string Buf;
42public:
43  StringTableBuilder() {
44    Buf.push_back('\0');
45  }
46  /// \returns Index of string in string table.
47  unsigned addString(StringRef S) {
48    StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S);
49    unsigned &I = Entry.getValue();
50    if (I != 0)
51      return I;
52    I = Buf.size();
53    Buf.append(S.begin(), S.end());
54    Buf.push_back('\0');
55    return I;
56  }
57  size_t size() const {
58    return Buf.size();
59  }
60  void writeToStream(raw_ostream &OS) {
61    OS.write(Buf.data(), Buf.size());
62  }
63};
64} // end anonymous namespace
65
66// This class is used to build up a contiguous binary blob while keeping
67// track of an offset in the output (which notionally begins at
68// `InitialOffset`).
69namespace {
70class ContiguousBlobAccumulator {
71  const uint64_t InitialOffset;
72  SmallVector<char, 128> Buf;
73  raw_svector_ostream OS;
74
75  /// \returns The new offset.
76  uint64_t padToAlignment(unsigned Align) {
77    uint64_t CurrentOffset = InitialOffset + OS.tell();
78    uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align);
79    for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
80      OS.write('\0');
81    return AlignedOffset; // == CurrentOffset;
82  }
83
84public:
85  ContiguousBlobAccumulator(uint64_t InitialOffset_)
86      : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
87  template <class Integer>
88  raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) {
89    Offset = padToAlignment(Align);
90    return OS;
91  }
92  void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
93};
94} // end anonymous namespace
95
96// Used to keep track of section names, so that in the YAML file sections
97// can be referenced by name instead of by index.
98namespace {
99class SectionNameToIdxMap {
100  StringMap<int> Map;
101public:
102  /// \returns true if name is already present in the map.
103  bool addName(StringRef SecName, unsigned i) {
104    StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1);
105    if (Entry.getValue() != -1)
106      return true;
107    Entry.setValue((int)i);
108    return false;
109  }
110  /// \returns true if name is not present in the map
111  bool lookupSection(StringRef SecName, unsigned &Idx) const {
112    StringMap<int>::const_iterator I = Map.find(SecName);
113    if (I == Map.end())
114      return true;
115    Idx = I->getValue();
116    return false;
117  }
118};
119} // end anonymous namespace
120
121template <class T>
122static size_t vectorDataSize(const std::vector<T> &Vec) {
123  return Vec.size() * sizeof(T);
124}
125
126template <class T>
127static void writeVectorData(raw_ostream &OS, const std::vector<T> &Vec) {
128  OS.write((const char *)Vec.data(), vectorDataSize(Vec));
129}
130
131template <class T>
132static void zero(T &Obj) {
133  memset(&Obj, 0, sizeof(Obj));
134}
135
136/// \brief Create a string table in `SHeader`, which we assume is already
137/// zero'd.
138template <class Elf_Shdr>
139static void createStringTableSectionHeader(Elf_Shdr &SHeader,
140                                           StringTableBuilder &STB,
141                                           ContiguousBlobAccumulator &CBA) {
142  SHeader.sh_type = ELF::SHT_STRTAB;
143  STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
144  SHeader.sh_size = STB.size();
145  SHeader.sh_addralign = 1;
146}
147
148namespace {
149/// \brief "Single point of truth" for the ELF file construction.
150/// TODO: This class still has a ways to go before it is truly a "single
151/// point of truth".
152template <class ELFT>
153class ELFState {
154  /// \brief The future ".strtab" section.
155  StringTableBuilder DotStrtab;
156  /// \brief The section number of the ".strtab" section.
157  unsigned DotStrtabSecNo;
158  /// \brief The accumulated contents of all sections so far.
159  ContiguousBlobAccumulator &SectionContentAccum;
160  typedef typename object::ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
161  /// \brief The ELF file header.
162  Elf_Ehdr &Header;
163
164  SectionNameToIdxMap &SN2I;
165
166public:
167
168  ELFState(Elf_Ehdr &Header_, ContiguousBlobAccumulator &Accum,
169           unsigned DotStrtabSecNo_, SectionNameToIdxMap &SN2I_)
170      : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_),
171        SectionContentAccum(Accum), Header(Header_), SN2I(SN2I_) {}
172
173  unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; }
174  StringTableBuilder &getStringTable() { return DotStrtab; }
175  ContiguousBlobAccumulator &getSectionContentAccum() {
176    return SectionContentAccum;
177  }
178  SectionNameToIdxMap &getSN2I() { return SN2I; }
179};
180} // end anonymous namespace
181
182// FIXME: At this point it is fairly clear that we need to refactor these
183// static functions into methods of a class sharing some typedefs. These
184// ELF type names are insane.
185template <class ELFT>
186static void
187addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, ELFState<ELFT> &State,
188           std::vector<typename object::ELFObjectFile<ELFT>::Elf_Sym> &Syms,
189           unsigned SymbolBinding) {
190  typedef typename object::ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
191  for (unsigned i = 0, e = Symbols.size(); i != e; ++i) {
192    const ELFYAML::Symbol &Sym = Symbols[i];
193    Elf_Sym Symbol;
194    zero(Symbol);
195    if (!Sym.Name.empty())
196      Symbol.st_name = State.getStringTable().addString(Sym.Name);
197    Symbol.setBindingAndType(SymbolBinding, Sym.Type);
198    if (!Sym.Section.empty()) {
199      unsigned Index;
200      if (State.getSN2I().lookupSection(Sym.Section, Index)) {
201        errs() << "error: Unknown section referenced: '" << Sym.Section
202               << "' by YAML symbol " << Sym.Name << ".\n";
203        exit(1);
204      }
205      Symbol.st_shndx = Index;
206    } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
207    Symbol.st_value = Sym.Value;
208    Symbol.st_size = Sym.Size;
209    Syms.push_back(Symbol);
210  }
211}
212
213template <class ELFT>
214static void handleSymtabSectionHeader(
215    const ELFYAML::LocalGlobalWeakSymbols &Symbols, ELFState<ELFT> &State,
216    typename object::ELFObjectFile<ELFT>::Elf_Shdr &SHeader) {
217
218  typedef typename object::ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
219  SHeader.sh_type = ELF::SHT_SYMTAB;
220  SHeader.sh_link = State.getDotStrTabSecNo();
221  // One greater than symbol table index of the last local symbol.
222  SHeader.sh_info = Symbols.Local.size() + 1;
223  SHeader.sh_entsize = sizeof(Elf_Sym);
224
225  std::vector<Elf_Sym> Syms;
226  {
227    // Ensure STN_UNDEF is present
228    Elf_Sym Sym;
229    zero(Sym);
230    Syms.push_back(Sym);
231  }
232  addSymbols(Symbols.Local, State, Syms, ELF::STB_LOCAL);
233  addSymbols(Symbols.Global, State, Syms, ELF::STB_GLOBAL);
234  addSymbols(Symbols.Weak, State, Syms, ELF::STB_WEAK);
235
236  ContiguousBlobAccumulator &CBA = State.getSectionContentAccum();
237  writeVectorData(CBA.getOSAndAlignedOffset(SHeader.sh_offset), Syms);
238  SHeader.sh_size = vectorDataSize(Syms);
239}
240
241template <class ELFT>
242static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
243  using namespace llvm::ELF;
244  typedef typename object::ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
245  typedef typename object::ELFObjectFile<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(Header, 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  createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA);
358  SHeaders.push_back(SHStrTabSHeader);
359
360  OS.write((const char *)&Header, sizeof(Header));
361  writeVectorData(OS, SHeaders);
362  CBA.writeBlobToStream(OS);
363  return 0;
364}
365
366static bool is64Bit(const ELFYAML::Object &Doc) {
367  return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
368}
369
370static bool isLittleEndian(const ELFYAML::Object &Doc) {
371  return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
372}
373
374int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
375  yaml::Input YIn(Buf->getBuffer());
376  ELFYAML::Object Doc;
377  YIn >> Doc;
378  if (YIn.error()) {
379    errs() << "yaml2obj: Failed to parse YAML file!\n";
380    return 1;
381  }
382  using object::ELFType;
383  typedef ELFType<support::little, 8, true> LE64;
384  typedef ELFType<support::big, 8, true> BE64;
385  typedef ELFType<support::little, 4, false> LE32;
386  typedef ELFType<support::big, 4, false> BE32;
387  if (is64Bit(Doc)) {
388    if (isLittleEndian(Doc))
389      return writeELF<LE64>(outs(), Doc);
390    else
391      return writeELF<BE64>(outs(), Doc);
392  } else {
393    if (isLittleEndian(Doc))
394      return writeELF<LE32>(outs(), Doc);
395    else
396      return writeELF<BE32>(outs(), Doc);
397  }
398}
399