OLD | NEW |
(Empty) | |
| 1 //===- subzero/src/IceELFObjectWriter.h - ELF object writer -----*- C++ -*-===// |
| 2 // |
| 3 // The Subzero Code Generator |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // Abstraction for a writer that is responsible for writing an ELF file. |
| 11 // |
| 12 //===----------------------------------------------------------------------===// |
| 13 |
| 14 #ifndef SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| 15 #define SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| 16 |
| 17 #include "IceDefs.h" |
| 18 #include "IceELFSection.h" |
| 19 #include "IceELFStreamer.h" |
| 20 |
| 21 using namespace llvm::ELF; |
| 22 |
| 23 namespace Ice { |
| 24 |
| 25 // Higher level ELF object writer. Manages section information and writes |
| 26 // the final ELF object. The object writer will write to file the code |
| 27 // and data as it is being defined (rather than keep a copy). |
| 28 // After all definitions are written out, it will finalize the bookkeeping |
| 29 // sections and write them out. Expected usage: |
| 30 // |
| 31 // (1) writeInitialELFHeader |
| 32 // (2) writeDataInitializer* |
| 33 // (3) writeFunctionCode* |
| 34 // (4) writeNonUserSections |
| 35 class ELFObjectWriter { |
| 36 ELFObjectWriter(const ELFObjectWriter &) = delete; |
| 37 ELFObjectWriter &operator=(const ELFObjectWriter &) = delete; |
| 38 |
| 39 public: |
| 40 ELFObjectWriter(GlobalContext &Ctx, ELFStreamer &Out); |
| 41 |
| 42 // Write the initial ELF header. This is just to reserve space in the ELF |
| 43 // file. Reserving space allows the other functions to write text |
| 44 // and data directly to the file and get the right file offsets. |
| 45 void writeInitialELFHeader(); |
| 46 |
| 47 // Copy data of a function's text section to file and note the offset of the |
| 48 // symbol's definition in the symbol table. |
| 49 // TODO(jvoung): This also needs the relocations to adjust the |
| 50 // section-relative offsets and hook them up to the symbol table references. |
| 51 void writeFunctionCode(const IceString &FuncName, bool IsInternal, |
| 52 const llvm::StringRef Data); |
| 53 |
| 54 // Copy initializer data for a global to file and note the offset and |
| 55 // size of the global's definition in the symbol table. |
| 56 // TODO(jvoung): This needs to know which section. This also needs the |
| 57 // relocations to hook them up to the symbol table references. Also |
| 58 // TODO is handling BSS (which just needs to note the size). |
| 59 void writeDataInitializer(const IceString &VarName, |
| 60 const llvm::StringRef Data); |
| 61 |
| 62 // Do final layout and write out the rest of the object file, then |
| 63 // patch up the initial ELF header with the final info. |
| 64 void writeNonUserSections(); |
| 65 |
| 66 private: |
| 67 GlobalContext &Ctx; |
| 68 ELFStreamer &Str; |
| 69 bool SectionNumbersAssigned; |
| 70 |
| 71 // All created sections, separated into different pools. |
| 72 typedef std::vector<ELFSection *> SectionList; |
| 73 typedef std::vector<ELFTextSection *> TextSectionList; |
| 74 typedef std::vector<ELFDataSection *> DataSectionList; |
| 75 typedef std::vector<ELFRelocationSectionBase *> RelSectionList; |
| 76 TextSectionList TextSections; |
| 77 RelSectionList RelTextSections; |
| 78 DataSectionList DataSections; |
| 79 RelSectionList RelDataSections; |
| 80 DataSectionList RoDataSections; |
| 81 RelSectionList RelRoDataSections; |
| 82 |
| 83 // Handles to special sections that need incremental bookkeeping. |
| 84 ELFSection *NullSection; |
| 85 ELFStringTableSection *ShStrTab; |
| 86 ELFSymbolTableSection *SymTab; |
| 87 ELFStringTableSection *StrTab; |
| 88 |
| 89 template <typename T> |
| 90 T *createSection(const IceString &Name, Elf64_Word ShType, |
| 91 Elf64_Xword ShFlags, Elf64_Xword ShAddralign, |
| 92 Elf64_Xword ShEntsize); |
| 93 |
| 94 // Align the file position before writing out a section's data, |
| 95 // and return the position of the file. |
| 96 Elf64_Off alignFileOffset(Elf64_Xword Align); |
| 97 |
| 98 // Assign an ordering / section numbers to each section. |
| 99 // Fill in other information that is only known near the end |
| 100 // (such as the size, if it wasn't already incrementally updated). |
| 101 // This then collects all sections in the decided order, into one vector, |
| 102 // for conveniently writing out all of the section headers. |
| 103 void assignSectionNumbersInfo(SectionList &AllSections); |
| 104 |
| 105 // This function assigns .foo and .rel.foo consecutive section numbers. |
| 106 // It also sets the relocation section's sh_info field to the related |
| 107 // section's number. |
| 108 template <typename UserSectionList> |
| 109 void assignRelSectionNumInPairs(SizeT &CurSectionNumber, |
| 110 UserSectionList &UserSections, |
| 111 RelSectionList &RelSections, |
| 112 SectionList &AllSections); |
| 113 |
| 114 // Link the relocation sections to the symbol table. |
| 115 void assignRelLinkNum(SizeT SymTabNumber, RelSectionList &RelSections); |
| 116 |
| 117 // Write the ELF file header with the given information about sections. |
| 118 template <bool IsELF64> |
| 119 void writeELFHeaderInternal(uint64_t SectionHeaderOffset, |
| 120 SizeT SectHeaderStrIndex, SizeT NumSections); |
| 121 }; |
| 122 |
| 123 } // end of namespace Ice |
| 124 |
| 125 #endif // SUBZERO_SRC_ICEELFOBJECTWRITER_H |
OLD | NEW |