| OLD | NEW |
| 1 //===- subzero/src/IceELFObjectWriter.h - ELF object writer -----*- C++ -*-===// | 1 //===- subzero/src/IceELFObjectWriter.h - ELF object writer -----*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 /// |
| 10 // Abstraction for a writer that is responsible for writing an ELF file. | 10 /// \file |
| 11 // | 11 /// Abstraction for a writer that is responsible for writing an ELF file. |
| 12 /// |
| 12 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 13 | 14 |
| 14 #ifndef SUBZERO_SRC_ICEELFOBJECTWRITER_H | 15 #ifndef SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| 15 #define SUBZERO_SRC_ICEELFOBJECTWRITER_H | 16 #define SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| 16 | 17 |
| 17 #include "IceDefs.h" | 18 #include "IceDefs.h" |
| 18 #include "IceELFSection.h" | 19 #include "IceELFSection.h" |
| 19 #include "IceELFStreamer.h" | 20 #include "IceELFStreamer.h" |
| 20 #include "IceTypes.h" | 21 #include "IceTypes.h" |
| 21 | 22 |
| 22 using namespace llvm::ELF; | 23 using namespace llvm::ELF; |
| 23 | 24 |
| 24 namespace Ice { | 25 namespace Ice { |
| 25 | 26 |
| 26 // Higher level ELF object writer. Manages section information and writes | 27 /// Higher level ELF object writer. Manages section information and writes |
| 27 // the final ELF object. The object writer will write to file the code | 28 /// the final ELF object. The object writer will write to file the code |
| 28 // and data as it is being defined (rather than keep a copy). | 29 /// and data as it is being defined (rather than keep a copy). |
| 29 // After all definitions are written out, it will finalize the bookkeeping | 30 /// After all definitions are written out, it will finalize the bookkeeping |
| 30 // sections and write them out. Expected usage: | 31 /// sections and write them out. Expected usage: |
| 31 // | 32 /// |
| 32 // (1) writeInitialELFHeader (invoke once) | 33 /// (1) writeInitialELFHeader (invoke once) |
| 33 // (2) writeDataSection (may be invoked multiple times, as long as | 34 /// (2) writeDataSection (may be invoked multiple times, as long as |
| 34 // SectionSuffix is unique) | 35 /// SectionSuffix is unique) |
| 35 // (3) writeFunctionCode (must invoke once per function) | 36 /// (3) writeFunctionCode (must invoke once per function) |
| 36 // (4) writeConstantPool (must invoke once per pooled primitive type) | 37 /// (4) writeConstantPool (must invoke once per pooled primitive type) |
| 37 // (5) setUndefinedSyms (invoke once) | 38 /// (5) setUndefinedSyms (invoke once) |
| 38 // (6) writeNonUserSections (invoke once) | 39 /// (6) writeNonUserSections (invoke once) |
| 39 // | 40 /// |
| 40 // The requirement for writeDataSection to be invoked only once can | 41 /// The requirement for writeDataSection to be invoked only once can |
| 41 // be relaxed if using -fdata-sections. The requirement to invoke only once | 42 /// be relaxed if using -fdata-sections. The requirement to invoke only once |
| 42 // without -fdata-sections is so that variables that belong to each possible | 43 /// without -fdata-sections is so that variables that belong to each possible |
| 43 // SectionType are contiguous in the file. With -fdata-sections, each global | 44 /// SectionType are contiguous in the file. With -fdata-sections, each global |
| 44 // variable is in a separate section and therefore the sections will be | 45 /// variable is in a separate section and therefore the sections will be |
| 45 // trivially contiguous. | 46 /// trivially contiguous. |
| 46 class ELFObjectWriter { | 47 class ELFObjectWriter { |
| 47 ELFObjectWriter() = delete; | 48 ELFObjectWriter() = delete; |
| 48 ELFObjectWriter(const ELFObjectWriter &) = delete; | 49 ELFObjectWriter(const ELFObjectWriter &) = delete; |
| 49 ELFObjectWriter &operator=(const ELFObjectWriter &) = delete; | 50 ELFObjectWriter &operator=(const ELFObjectWriter &) = delete; |
| 50 | 51 |
| 51 public: | 52 public: |
| 52 ELFObjectWriter(GlobalContext &Ctx, ELFStreamer &Out); | 53 ELFObjectWriter(GlobalContext &Ctx, ELFStreamer &Out); |
| 53 | 54 |
| 54 // Write the initial ELF header. This is just to reserve space in the ELF | 55 /// Write the initial ELF header. This is just to reserve space in the ELF |
| 55 // file. Reserving space allows the other functions to write text | 56 /// file. Reserving space allows the other functions to write text |
| 56 // and data directly to the file and get the right file offsets. | 57 /// and data directly to the file and get the right file offsets. |
| 57 void writeInitialELFHeader(); | 58 void writeInitialELFHeader(); |
| 58 | 59 |
| 59 // Copy initializer data for globals to file and note the offset and size | 60 /// Copy initializer data for globals to file and note the offset and size |
| 60 // of each global's definition in the symbol table. | 61 /// of each global's definition in the symbol table. |
| 61 // Use the given target's RelocationKind for any relocations. | 62 /// Use the given target's RelocationKind for any relocations. |
| 62 void writeDataSection(const VariableDeclarationList &Vars, | 63 void writeDataSection(const VariableDeclarationList &Vars, |
| 63 FixupKind RelocationKind, | 64 FixupKind RelocationKind, |
| 64 const IceString &SectionSuffix); | 65 const IceString &SectionSuffix); |
| 65 | 66 |
| 66 // Copy data of a function's text section to file and note the offset of the | 67 /// Copy data of a function's text section to file and note the offset of the |
| 67 // symbol's definition in the symbol table. | 68 /// symbol's definition in the symbol table. |
| 68 // Copy the text fixups for use after all functions are written. | 69 /// Copy the text fixups for use after all functions are written. |
| 69 // The text buffer and fixups are extracted from the Assembler object. | 70 /// The text buffer and fixups are extracted from the Assembler object. |
| 70 void writeFunctionCode(const IceString &FuncName, bool IsInternal, | 71 void writeFunctionCode(const IceString &FuncName, bool IsInternal, |
| 71 const Assembler *Asm); | 72 const Assembler *Asm); |
| 72 | 73 |
| 73 // Queries the GlobalContext for constant pools of the given type | 74 /// Queries the GlobalContext for constant pools of the given type |
| 74 // and writes out read-only data sections for those constants. This also | 75 /// and writes out read-only data sections for those constants. This also |
| 75 // fills the symbol table with labels for each constant pool entry. | 76 /// fills the symbol table with labels for each constant pool entry. |
| 76 template <typename ConstType> void writeConstantPool(Type Ty); | 77 template <typename ConstType> void writeConstantPool(Type Ty); |
| 77 | 78 |
| 78 // Populate the symbol table with a list of external/undefined symbols. | 79 /// Populate the symbol table with a list of external/undefined symbols. |
| 79 void setUndefinedSyms(const ConstantList &UndefSyms); | 80 void setUndefinedSyms(const ConstantList &UndefSyms); |
| 80 | 81 |
| 81 // Do final layout and write out the rest of the object file. | 82 /// Do final layout and write out the rest of the object file. |
| 82 // Finally, patch up the initial ELF header with the final info. | 83 /// Finally, patch up the initial ELF header with the final info. |
| 83 void writeNonUserSections(); | 84 void writeNonUserSections(); |
| 84 | 85 |
| 85 // Which type of ELF section a global variable initializer belongs to. | 86 /// Which type of ELF section a global variable initializer belongs to. |
| 86 // This is used as an array index so should start at 0 and be contiguous. | 87 /// This is used as an array index so should start at 0 and be contiguous. |
| 87 enum SectionType { ROData = 0, Data, BSS, NumSectionTypes }; | 88 enum SectionType { ROData = 0, Data, BSS, NumSectionTypes }; |
| 88 | 89 |
| 89 private: | 90 private: |
| 90 GlobalContext &Ctx; | 91 GlobalContext &Ctx; |
| 91 ELFStreamer &Str; | 92 ELFStreamer &Str; |
| 92 bool SectionNumbersAssigned = false; | 93 bool SectionNumbersAssigned = false; |
| 93 bool ELF64; | 94 bool ELF64; |
| 94 | 95 |
| 95 // All created sections, separated into different pools. | 96 // All created sections, separated into different pools. |
| 96 typedef std::vector<ELFSection *> SectionList; | 97 typedef std::vector<ELFSection *> SectionList; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 109 ELFSection *NullSection; | 110 ELFSection *NullSection; |
| 110 ELFStringTableSection *ShStrTab; | 111 ELFStringTableSection *ShStrTab; |
| 111 ELFSymbolTableSection *SymTab; | 112 ELFSymbolTableSection *SymTab; |
| 112 ELFStringTableSection *StrTab; | 113 ELFStringTableSection *StrTab; |
| 113 | 114 |
| 114 template <typename T> | 115 template <typename T> |
| 115 T *createSection(const IceString &Name, Elf64_Word ShType, | 116 T *createSection(const IceString &Name, Elf64_Word ShType, |
| 116 Elf64_Xword ShFlags, Elf64_Xword ShAddralign, | 117 Elf64_Xword ShFlags, Elf64_Xword ShAddralign, |
| 117 Elf64_Xword ShEntsize); | 118 Elf64_Xword ShEntsize); |
| 118 | 119 |
| 119 // Create a relocation section, given the related section | 120 /// Create a relocation section, given the related section |
| 120 // (e.g., .text, .data., .rodata). | 121 /// (e.g., .text, .data., .rodata). |
| 121 ELFRelocationSection * | 122 ELFRelocationSection * |
| 122 createRelocationSection(const ELFSection *RelatedSection); | 123 createRelocationSection(const ELFSection *RelatedSection); |
| 123 | 124 |
| 124 // Align the file position before writing out a section's data, | 125 /// Align the file position before writing out a section's data, |
| 125 // and return the position of the file. | 126 /// and return the position of the file. |
| 126 Elf64_Off alignFileOffset(Elf64_Xword Align); | 127 Elf64_Off alignFileOffset(Elf64_Xword Align); |
| 127 | 128 |
| 128 // Assign an ordering / section numbers to each section. | 129 /// Assign an ordering / section numbers to each section. |
| 129 // Fill in other information that is only known near the end | 130 /// Fill in other information that is only known near the end |
| 130 // (such as the size, if it wasn't already incrementally updated). | 131 /// (such as the size, if it wasn't already incrementally updated). |
| 131 // This then collects all sections in the decided order, into one vector, | 132 /// This then collects all sections in the decided order, into one vector, |
| 132 // for conveniently writing out all of the section headers. | 133 /// for conveniently writing out all of the section headers. |
| 133 void assignSectionNumbersInfo(SectionList &AllSections); | 134 void assignSectionNumbersInfo(SectionList &AllSections); |
| 134 | 135 |
| 135 // This function assigns .foo and .rel.foo consecutive section numbers. | 136 /// This function assigns .foo and .rel.foo consecutive section numbers. |
| 136 // It also sets the relocation section's sh_info field to the related | 137 /// It also sets the relocation section's sh_info field to the related |
| 137 // section's number. | 138 /// section's number. |
| 138 template <typename UserSectionList> | 139 template <typename UserSectionList> |
| 139 void assignRelSectionNumInPairs(SizeT &CurSectionNumber, | 140 void assignRelSectionNumInPairs(SizeT &CurSectionNumber, |
| 140 UserSectionList &UserSections, | 141 UserSectionList &UserSections, |
| 141 RelSectionList &RelSections, | 142 RelSectionList &RelSections, |
| 142 SectionList &AllSections); | 143 SectionList &AllSections); |
| 143 | 144 |
| 144 // Link the relocation sections to the symbol table. | 145 /// Link the relocation sections to the symbol table. |
| 145 void assignRelLinkNum(SizeT SymTabNumber, RelSectionList &RelSections); | 146 void assignRelLinkNum(SizeT SymTabNumber, RelSectionList &RelSections); |
| 146 | 147 |
| 147 // Helper function for writeDataSection. Writes a data section of type | 148 /// Helper function for writeDataSection. Writes a data section of type |
| 148 // SectionType, given the global variables Vars belonging to that SectionType. | 149 /// SectionType, given the global variables Vars belonging to that SectionType
. |
| 149 void writeDataOfType(SectionType SectionType, | 150 void writeDataOfType(SectionType SectionType, |
| 150 const VariableDeclarationList &Vars, | 151 const VariableDeclarationList &Vars, |
| 151 FixupKind RelocationKind, | 152 FixupKind RelocationKind, |
| 152 const IceString &SectionSuffix); | 153 const IceString &SectionSuffix); |
| 153 | 154 |
| 154 // Write the final relocation sections given the final symbol table. | 155 /// Write the final relocation sections given the final symbol table. |
| 155 // May also be able to seek around the file and resolve function calls | 156 /// May also be able to seek around the file and resolve function calls |
| 156 // that are for functions within the same section. | 157 /// that are for functions within the same section. |
| 157 void writeAllRelocationSections(); | 158 void writeAllRelocationSections(); |
| 158 void writeRelocationSections(RelSectionList &RelSections); | 159 void writeRelocationSections(RelSectionList &RelSections); |
| 159 | 160 |
| 160 // Write the ELF file header with the given information about sections. | 161 /// Write the ELF file header with the given information about sections. |
| 161 template <bool IsELF64> | 162 template <bool IsELF64> |
| 162 void writeELFHeaderInternal(Elf64_Off SectionHeaderOffset, | 163 void writeELFHeaderInternal(Elf64_Off SectionHeaderOffset, |
| 163 SizeT SectHeaderStrIndex, SizeT NumSections); | 164 SizeT SectHeaderStrIndex, SizeT NumSections); |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 } // end of namespace Ice | 167 } // end of namespace Ice |
| 167 | 168 |
| 168 #endif // SUBZERO_SRC_ICEELFOBJECTWRITER_H | 169 #endif // SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| OLD | NEW |