Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 1 | //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the target-independent ELF writer. This file writes out |
| 11 | // the ELF file in the following order: |
| 12 | // |
| 13 | // #1. ELF Header |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 14 | // #2. '.text' section |
| 15 | // #3. '.data' section |
| 16 | // #4. '.bss' section (conceptual position in file) |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 17 | // ... |
| 18 | // #X. '.shstrtab' section |
| 19 | // #Y. Section Table |
| 20 | // |
| 21 | // The entries in the section table are laid out as: |
| 22 | // #0. Null entry [required] |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 23 | // #1. ".text" entry - the program code |
| 24 | // #2. ".data" entry - global variables with initializers. [ if needed ] |
| 25 | // #3. ".bss" entry - global variables without initializers. [ if needed ] |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 26 | // ... |
| 27 | // #N. ".shstrtab" entry - String table for the section names. |
| 28 | |
| 29 | // |
| 30 | // NOTE: This code should eventually be extended to support 64-bit ELF (this |
| 31 | // won't be hard), but we haven't done so yet! |
| 32 | // |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | #include "llvm/CodeGen/ELFWriter.h" |
| 36 | #include "llvm/Module.h" |
| 37 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | dfe33bc | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { |
| 42 | e_machine = 0; // e_machine defaults to 'No Machine' |
| 43 | e_flags = 0; // e_flags defaults to 0, no flags. |
| 44 | |
| 45 | is64Bit = TM.getTargetData().getPointerSizeInBits() == 64; |
| 46 | isLittleEndian = TM.getTargetData().isLittleEndian(); |
| 47 | } |
| 48 | |
| 49 | // doInitialization - Emit the file header and all of the global variables for |
| 50 | // the module to the ELF file. |
| 51 | bool ELFWriter::doInitialization(Module &M) { |
Chris Lattner | dfe33bc | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 52 | Mang = new Mangler(M); |
| 53 | |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 54 | outbyte(0x7F); // EI_MAG0 |
| 55 | outbyte('E'); // EI_MAG1 |
| 56 | outbyte('L'); // EI_MAG2 |
| 57 | outbyte('F'); // EI_MAG3 |
| 58 | outbyte(is64Bit ? 2 : 1); // EI_CLASS |
| 59 | outbyte(isLittleEndian ? 1 : 2); // EI_DATA |
| 60 | outbyte(1); // EI_VERSION |
| 61 | for (unsigned i = OutputBuffer.size(); i != 16; ++i) |
| 62 | outbyte(0); // EI_PAD up to 16 bytes. |
| 63 | |
| 64 | // This should change for shared objects. |
| 65 | outhalf(1); // e_type = ET_REL |
| 66 | outhalf(e_machine); // e_machine = whatever the target wants |
| 67 | outword(1); // e_version = 1 |
| 68 | outaddr(0); // e_entry = 0 -> no entry point in .o file |
| 69 | outaddr(0); // e_phoff = 0 -> no program header for .o |
| 70 | |
| 71 | ELFHeader_e_shoff_Offset = OutputBuffer.size(); |
| 72 | outaddr(0); // e_shoff |
| 73 | outword(e_flags); // e_flags = whatever the target wants |
| 74 | |
| 75 | assert(!is64Bit && "These sizes need to be adjusted for 64-bit!"); |
| 76 | outhalf(52); // e_ehsize = ELF header size |
| 77 | outhalf(0); // e_phentsize = prog header entry size |
| 78 | outhalf(0); // e_phnum = # prog header entries = 0 |
| 79 | outhalf(40); // e_shentsize = sect header entry size |
| 80 | |
| 81 | |
| 82 | ELFHeader_e_shnum_Offset = OutputBuffer.size(); |
| 83 | outhalf(0); // e_shnum = # of section header ents |
| 84 | ELFHeader_e_shstrndx_Offset = OutputBuffer.size(); |
| 85 | outhalf(0); // e_shstrndx = Section # of '.shstrtab' |
| 86 | |
| 87 | // Add the null section. |
| 88 | SectionList.push_back(ELFSection()); |
| 89 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 90 | // Start up the symbol table. The first entry in the symtab is the null |
| 91 | // entry. |
| 92 | SymbolTable.push_back(ELFSym(0)); |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 96 | // FIXME: Should start the .text section. |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 100 | void ELFWriter::EmitGlobal(GlobalVariable *GV, ELFSection &DataSection, |
| 101 | ELFSection &BSSSection) { |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 102 | // If this is an external global, emit it now. TODO: Note that it would be |
| 103 | // better to ignore the symbol here and only add it to the symbol table if |
| 104 | // referenced. |
| 105 | if (!GV->hasInitializer()) { |
| 106 | ELFSym ExternalSym(GV); |
| 107 | ExternalSym.SetBind(ELFSym::STB_GLOBAL); |
| 108 | ExternalSym.SetType(ELFSym::STT_NOTYPE); |
| 109 | ExternalSym.SectionIdx = ELFSection::SHN_UNDEF; |
| 110 | SymbolTable.push_back(ExternalSym); |
| 111 | return; |
| 112 | } |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 114 | const Type *GVType = (const Type*)GV->getType(); |
| 115 | unsigned Align = TM.getTargetData().getTypeAlignment(GVType); |
| 116 | unsigned Size = TM.getTargetData().getTypeSize(GVType); |
| 117 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 118 | // If this global has a zero initializer, it is part of the .bss or common |
| 119 | // section. |
| 120 | if (GV->getInitializer()->isNullValue()) { |
| 121 | // If this global is part of the common block, add it now. Variables are |
| 122 | // part of the common block if they are zero initialized and allowed to be |
| 123 | // merged with other symbols. |
| 124 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) { |
| 125 | ELFSym CommonSym(GV); |
| 126 | // Value for common symbols is the alignment required. |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 127 | CommonSym.Value = Align; |
| 128 | CommonSym.Size = Size; |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 129 | CommonSym.SetBind(ELFSym::STB_GLOBAL); |
| 130 | CommonSym.SetType(ELFSym::STT_OBJECT); |
| 131 | // TODO SOMEDAY: add ELF visibility. |
| 132 | CommonSym.SectionIdx = ELFSection::SHN_COMMON; |
| 133 | SymbolTable.push_back(CommonSym); |
| 134 | return; |
| 135 | } |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 137 | // Otherwise, this symbol is part of the .bss section. Emit it now. |
| 138 | |
| 139 | // Handle alignment. Ensure section is aligned at least as much as required |
| 140 | // by this symbol. |
| 141 | BSSSection.Align = std::max(BSSSection.Align, Align); |
| 142 | |
| 143 | // Within the section, emit enough virtual padding to get us to an alignment |
| 144 | // boundary. |
| 145 | if (Align) |
| 146 | BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1); |
| 147 | |
| 148 | ELFSym BSSSym(GV); |
| 149 | BSSSym.Value = BSSSection.Size; |
| 150 | BSSSym.Size = Size; |
| 151 | BSSSym.SetType(ELFSym::STT_OBJECT); |
| 152 | |
| 153 | switch (GV->getLinkage()) { |
| 154 | default: // weak/linkonce handled above |
| 155 | assert(0 && "Unexpected linkage type!"); |
| 156 | case GlobalValue::AppendingLinkage: // FIXME: This should be improved! |
| 157 | case GlobalValue::ExternalLinkage: |
| 158 | BSSSym.SetBind(ELFSym::STB_GLOBAL); |
| 159 | break; |
| 160 | case GlobalValue::InternalLinkage: |
| 161 | BSSSym.SetBind(ELFSym::STB_LOCAL); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | // Set the idx of the .bss section |
| 166 | BSSSym.SectionIdx = &BSSSection-&SectionList[0]; |
| 167 | SymbolTable.push_back(BSSSym); |
| 168 | |
| 169 | // Reserve space in the .bss section for this symbol. |
| 170 | BSSSection.Size += Size; |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 174 | // FIXME: handle .rodata |
| 175 | //assert(!GV->isConstant() && "unimp"); |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 177 | // FIXME: handle .data |
| 178 | //assert(0 && "unimp"); |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /// doFinalization - Now that the module has been completely processed, emit |
| 187 | /// the ELF file to 'O'. |
| 188 | bool ELFWriter::doFinalization(Module &M) { |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 189 | // Okay, the .text section has now been finalized. |
| 190 | // FIXME: finalize the .text section. |
| 191 | |
| 192 | // Okay, the ELF header and .text sections have been completed, build the |
| 193 | // .data, .bss, and "common" sections next. |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 194 | SectionList.push_back(ELFSection(".data", OutputBuffer.size())); |
| 195 | SectionList.push_back(ELFSection(".bss")); |
| 196 | ELFSection &DataSection = *(SectionList.end()-2); |
| 197 | ELFSection &BSSSection = SectionList.back(); |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 198 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 199 | I != E; ++I) |
| 200 | EmitGlobal(I, DataSection, BSSSection); |
| 201 | |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 202 | // Finish up the data section. |
| 203 | DataSection.Type = ELFSection::SHT_PROGBITS; |
| 204 | DataSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC; |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 748de6e | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 206 | // The BSS Section logically starts at the end of the Data Section (adjusted |
| 207 | // to the required alignment of the BSSSection). |
| 208 | BSSSection.Offset = DataSection.Offset+DataSection.Size; |
| 209 | BSSSection.Type = ELFSection::SHT_NOBITS; |
| 210 | BSSSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC; |
| 211 | if (BSSSection.Align) |
| 212 | BSSSection.Offset = (BSSSection.Offset+BSSSection.Align-1) & |
| 213 | ~(BSSSection.Align-1); |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 214 | |
| 215 | // Emit the symbol table now, if non-empty. |
| 216 | EmitSymbolTable(); |
| 217 | |
| 218 | // FIXME: Emit the relocations now. |
| 219 | |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 220 | // Emit the string table for the sections in the ELF file we have. |
| 221 | EmitSectionTableStringTable(); |
| 222 | |
| 223 | // Emit the .o file section table. |
| 224 | EmitSectionTable(); |
| 225 | |
| 226 | // Emit the .o file to the specified stream. |
| 227 | O.write((char*)&OutputBuffer[0], OutputBuffer.size()); |
| 228 | |
| 229 | // Free the output buffer. |
| 230 | std::vector<unsigned char>().swap(OutputBuffer); |
Chris Lattner | dfe33bc | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 231 | |
| 232 | // Release the name mangler object. |
| 233 | delete Mang; Mang = 0; |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 237 | /// EmitSymbolTable - If the current symbol table is non-empty, emit the string |
| 238 | /// table for it and then the symbol table itself. |
| 239 | void ELFWriter::EmitSymbolTable() { |
| 240 | if (SymbolTable.size() == 1) return; // Only the null entry. |
| 241 | |
| 242 | // FIXME: compact all local symbols to the start of the symtab. |
| 243 | unsigned FirstNonLocalSymbol = 1; |
| 244 | |
| 245 | SectionList.push_back(ELFSection(".strtab", OutputBuffer.size())); |
| 246 | ELFSection &StrTab = SectionList.back(); |
| 247 | StrTab.Type = ELFSection::SHT_STRTAB; |
| 248 | StrTab.Align = 1; |
| 249 | |
| 250 | // Set the zero'th symbol to a null byte, as required. |
| 251 | outbyte(0); |
| 252 | SymbolTable[0].NameIdx = 0; |
| 253 | unsigned Index = 1; |
| 254 | for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) { |
Chris Lattner | dfe33bc | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 255 | // Use the name mangler to uniquify the LLVM symbol. |
| 256 | std::string Name = Mang->getValueName(SymbolTable[i].GV); |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 257 | |
| 258 | if (Name.empty()) { |
| 259 | SymbolTable[i].NameIdx = 0; |
| 260 | } else { |
| 261 | SymbolTable[i].NameIdx = Index; |
| 262 | |
| 263 | // Add the name to the output buffer, including the null terminator. |
| 264 | OutputBuffer.insert(OutputBuffer.end(), Name.begin(), Name.end()); |
| 265 | |
| 266 | // Add a null terminator. |
| 267 | OutputBuffer.push_back(0); |
| 268 | |
| 269 | // Keep track of the number of bytes emitted to this section. |
| 270 | Index += Name.size()+1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | StrTab.Size = OutputBuffer.size()-StrTab.Offset; |
| 275 | |
| 276 | // Now that we have emitted the string table and know the offset into the |
| 277 | // string table of each symbol, emit the symbol table itself. |
| 278 | assert(!is64Bit && "Should this be 8 byte aligned for 64-bit?" |
| 279 | " (check .Align below also)"); |
| 280 | align(4); |
| 281 | |
| 282 | SectionList.push_back(ELFSection(".symtab", OutputBuffer.size())); |
| 283 | ELFSection &SymTab = SectionList.back(); |
| 284 | SymTab.Type = ELFSection::SHT_SYMTAB; |
| 285 | SymTab.Align = 4; // FIXME: check for ELF64 |
| 286 | SymTab.Link = SectionList.size()-2; // Section Index of .strtab. |
| 287 | SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol. |
| 288 | SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64 |
| 289 | |
| 290 | assert(!is64Bit && "check this!"); |
| 291 | for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { |
| 292 | ELFSym &Sym = SymbolTable[i]; |
| 293 | outword(Sym.NameIdx); |
| 294 | outaddr(Sym.Value); |
| 295 | outword(Sym.Size); |
| 296 | outbyte(Sym.Info); |
| 297 | outbyte(Sym.Other); |
| 298 | outhalf(Sym.SectionIdx); |
| 299 | } |
| 300 | |
| 301 | SymTab.Size = OutputBuffer.size()-SymTab.Offset; |
| 302 | } |
| 303 | |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 304 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 305 | /// ELF Section Table string table: the string table that holds all of the |
| 306 | /// section names. |
| 307 | void ELFWriter::EmitSectionTableStringTable() { |
| 308 | // First step: add the section for the string table to the list of sections: |
| 309 | SectionList.push_back(ELFSection(".shstrtab", OutputBuffer.size())); |
Chris Lattner | 1932f5c | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 310 | SectionList.back().Type = ELFSection::SHT_STRTAB; |
Chris Lattner | 386b151 | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 311 | |
| 312 | // Now that we know which section number is the .shstrtab section, update the |
| 313 | // e_shstrndx entry in the ELF header. |
| 314 | fixhalf(SectionList.size()-1, ELFHeader_e_shstrndx_Offset); |
| 315 | |
| 316 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 317 | // the string table. |
| 318 | unsigned Index = 0; |
| 319 | |
| 320 | for (unsigned i = 0, e = SectionList.size(); i != e; ++i) { |
| 321 | // Set the index into the table. Note if we have lots of entries with |
| 322 | // common suffixes, we could memoize them here if we cared. |
| 323 | SectionList[i].NameIdx = Index; |
| 324 | |
| 325 | // Add the name to the output buffer, including the null terminator. |
| 326 | OutputBuffer.insert(OutputBuffer.end(), SectionList[i].Name.begin(), |
| 327 | SectionList[i].Name.end()); |
| 328 | // Add a null terminator. |
| 329 | OutputBuffer.push_back(0); |
| 330 | |
| 331 | // Keep track of the number of bytes emitted to this section. |
| 332 | Index += SectionList[i].Name.size()+1; |
| 333 | } |
| 334 | |
| 335 | // Set the size of .shstrtab now that we know what it is. |
| 336 | SectionList.back().Size = Index; |
| 337 | } |
| 338 | |
| 339 | /// EmitSectionTable - Now that we have emitted the entire contents of the file |
| 340 | /// (all of the sections), emit the section table which informs the reader where |
| 341 | /// the boundaries are. |
| 342 | void ELFWriter::EmitSectionTable() { |
| 343 | // Now that all of the sections have been emitted, set the e_shnum entry in |
| 344 | // the ELF header. |
| 345 | fixhalf(SectionList.size(), ELFHeader_e_shnum_Offset); |
| 346 | |
| 347 | // Now that we know the offset in the file of the section table (which we emit |
| 348 | // next), update the e_shoff address in the ELF header. |
| 349 | fixaddr(OutputBuffer.size(), ELFHeader_e_shoff_Offset); |
| 350 | |
| 351 | // Emit all of the section table entries. |
| 352 | for (unsigned i = 0, e = SectionList.size(); i != e; ++i) { |
| 353 | const ELFSection &S = SectionList[i]; |
| 354 | outword(S.NameIdx); // sh_name - Symbol table name idx |
| 355 | outword(S.Type); // sh_type - Section contents & semantics |
| 356 | outword(S.Flags); // sh_flags - Section flags. |
| 357 | outaddr(S.Addr); // sh_addr - The mem address this section appears in. |
| 358 | outaddr(S.Offset); // sh_offset - The offset from the start of the file. |
| 359 | outword(S.Size); // sh_size - The section size. |
| 360 | outword(S.Link); // sh_link - Section header table index link. |
| 361 | outword(S.Info); // sh_info - Auxillary information. |
| 362 | outword(S.Align); // sh_addralign - Alignment of section. |
| 363 | outword(S.EntSize); // sh_entsize - Size of each entry in the section. |
| 364 | } |
| 365 | |
| 366 | // Release the memory allocated for the section list. |
| 367 | std::vector<ELFSection>().swap(SectionList); |
| 368 | } |