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