Chris Lattner | 35f0a4f | 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 | 80ed8fa | 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 | 35f0a4f | 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 | 80ed8fa | 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 | 35f0a4f | 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" |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 38 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 43 | namespace llvm { |
| 44 | class ELFCodeEmitter : public MachineCodeEmitter { |
| 45 | ELFWriter &EW; |
| 46 | std::vector<unsigned char> &OutputBuffer; |
| 47 | size_t FnStart; |
| 48 | public: |
| 49 | ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutputBuffer(EW.OutputBuffer) {} |
| 50 | |
| 51 | void startFunction(MachineFunction &F) { |
| 52 | // Align the output buffer to the appropriate alignment. |
| 53 | unsigned Align = 16; // FIXME: GENERICIZE!! |
| 54 | ELFWriter::ELFSection &TextSection = EW.SectionList.back(); |
| 55 | |
| 56 | // Upgrade the section alignment if required. |
| 57 | if (TextSection.Align < Align) TextSection.Align = Align; |
| 58 | |
| 59 | // Add padding zeros to the end of the buffer to make sure that the |
| 60 | // function will start on the correct byte alignment within the section. |
| 61 | size_t SectionOff = OutputBuffer.size()-TextSection.Offset; |
| 62 | if (SectionOff & (Align-1)) { |
| 63 | // Add padding to get alignment to the correct place. |
| 64 | size_t Pad = Align-(SectionOff & (Align-1)); |
| 65 | OutputBuffer.resize(OutputBuffer.size()+Pad); |
| 66 | } |
| 67 | |
| 68 | FnStart = OutputBuffer.size(); |
| 69 | } |
Chris Lattner | 5fe7b6e | 2005-07-11 06:17:35 +0000 | [diff] [blame^] | 70 | void finishFunction(MachineFunction &F) { |
| 71 | // We now know the size of the function, add a symbol to represent it. |
| 72 | ELFWriter::ELFSym FnSym(F.getFunction()); |
| 73 | |
| 74 | // Figure out the binding (linkage) of the symbol. |
| 75 | switch (F.getFunction()->getLinkage()) { |
| 76 | default: |
| 77 | // appending linkage is illegal for functions. |
| 78 | assert(0 && "Unknown linkage type!"); |
| 79 | case GlobalValue::ExternalLinkage: |
| 80 | FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL); |
| 81 | break; |
| 82 | case GlobalValue::LinkOnceLinkage: |
| 83 | case GlobalValue::WeakLinkage: |
| 84 | FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK); |
| 85 | break; |
| 86 | case GlobalValue::InternalLinkage: |
| 87 | FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | FnSym.SetType(ELFWriter::ELFSym::STT_FUNC); |
| 92 | FnSym.SectionIdx = EW.SectionList.size()-1; // .text section. |
| 93 | // Value = Offset from start of .text |
| 94 | FnSym.Value = FnStart - EW.SectionList.back().Offset; |
| 95 | FnSym.Size = OutputBuffer.size()-FnStart; |
| 96 | |
| 97 | // Finally, add it to the symtab. |
| 98 | EW.SymbolTable.push_back(FnSym); |
| 99 | } |
| 100 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 101 | void emitConstantPool(MachineConstantPool *MCP) { |
| 102 | if (MCP->isEmpty()) return; |
| 103 | assert(0 && "unimp"); |
| 104 | } |
| 105 | virtual void emitByte(unsigned char B) { |
| 106 | OutputBuffer.push_back(B); |
| 107 | } |
| 108 | virtual void emitWordAt(unsigned W, unsigned *Ptr) { |
| 109 | assert(0 && "ni"); |
| 110 | } |
| 111 | virtual void emitWord(unsigned W) { |
| 112 | assert(0 && "ni"); |
| 113 | } |
| 114 | virtual uint64_t getCurrentPCValue() { |
| 115 | return OutputBuffer.size(); |
| 116 | } |
| 117 | virtual uint64_t getCurrentPCOffset() { |
| 118 | return OutputBuffer.size()-FnStart; |
| 119 | } |
| 120 | void addRelocation(const MachineRelocation &MR) { |
| 121 | assert(0 && "relo not handled yet!"); |
| 122 | } |
| 123 | virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { |
| 124 | assert(0 && "CP not implementated yet!"); |
| 125 | } |
| 126 | /// JIT SPECIFIC FUNCTIONS |
| 127 | void startFunctionStub(unsigned StubSize) { |
| 128 | assert(0 && "JIT specific function called!"); |
| 129 | abort(); |
| 130 | } |
| 131 | void *finishFunctionStub(const Function *F) { |
| 132 | assert(0 && "JIT specific function called!"); |
| 133 | abort(); |
| 134 | return 0; |
| 135 | } |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 140 | ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { |
| 141 | e_machine = 0; // e_machine defaults to 'No Machine' |
| 142 | e_flags = 0; // e_flags defaults to 0, no flags. |
| 143 | |
| 144 | is64Bit = TM.getTargetData().getPointerSizeInBits() == 64; |
| 145 | isLittleEndian = TM.getTargetData().isLittleEndian(); |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 146 | |
| 147 | // Create the machine code emitter object for this target. |
| 148 | MCE = new ELFCodeEmitter(*this); |
| 149 | } |
| 150 | |
| 151 | ELFWriter::~ELFWriter() { |
| 152 | delete MCE; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // doInitialization - Emit the file header and all of the global variables for |
| 156 | // the module to the ELF file. |
| 157 | bool ELFWriter::doInitialization(Module &M) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 158 | Mang = new Mangler(M); |
| 159 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 160 | outbyte(0x7F); // EI_MAG0 |
| 161 | outbyte('E'); // EI_MAG1 |
| 162 | outbyte('L'); // EI_MAG2 |
| 163 | outbyte('F'); // EI_MAG3 |
| 164 | outbyte(is64Bit ? 2 : 1); // EI_CLASS |
| 165 | outbyte(isLittleEndian ? 1 : 2); // EI_DATA |
| 166 | outbyte(1); // EI_VERSION |
| 167 | for (unsigned i = OutputBuffer.size(); i != 16; ++i) |
| 168 | outbyte(0); // EI_PAD up to 16 bytes. |
| 169 | |
| 170 | // This should change for shared objects. |
| 171 | outhalf(1); // e_type = ET_REL |
| 172 | outhalf(e_machine); // e_machine = whatever the target wants |
| 173 | outword(1); // e_version = 1 |
| 174 | outaddr(0); // e_entry = 0 -> no entry point in .o file |
| 175 | outaddr(0); // e_phoff = 0 -> no program header for .o |
| 176 | |
| 177 | ELFHeader_e_shoff_Offset = OutputBuffer.size(); |
| 178 | outaddr(0); // e_shoff |
| 179 | outword(e_flags); // e_flags = whatever the target wants |
| 180 | |
| 181 | assert(!is64Bit && "These sizes need to be adjusted for 64-bit!"); |
| 182 | outhalf(52); // e_ehsize = ELF header size |
| 183 | outhalf(0); // e_phentsize = prog header entry size |
| 184 | outhalf(0); // e_phnum = # prog header entries = 0 |
| 185 | outhalf(40); // e_shentsize = sect header entry size |
| 186 | |
| 187 | |
| 188 | ELFHeader_e_shnum_Offset = OutputBuffer.size(); |
| 189 | outhalf(0); // e_shnum = # of section header ents |
| 190 | ELFHeader_e_shstrndx_Offset = OutputBuffer.size(); |
| 191 | outhalf(0); // e_shstrndx = Section # of '.shstrtab' |
| 192 | |
| 193 | // Add the null section. |
| 194 | SectionList.push_back(ELFSection()); |
| 195 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 196 | // Start up the symbol table. The first entry in the symtab is the null |
| 197 | // entry. |
| 198 | SymbolTable.push_back(ELFSym(0)); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 199 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 200 | SectionList.push_back(ELFSection(".text", OutputBuffer.size())); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 205 | void ELFWriter::EmitGlobal(GlobalVariable *GV, ELFSection &DataSection, |
| 206 | ELFSection &BSSSection) { |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 207 | // If this is an external global, emit it now. TODO: Note that it would be |
| 208 | // better to ignore the symbol here and only add it to the symbol table if |
| 209 | // referenced. |
| 210 | if (!GV->hasInitializer()) { |
| 211 | ELFSym ExternalSym(GV); |
| 212 | ExternalSym.SetBind(ELFSym::STB_GLOBAL); |
| 213 | ExternalSym.SetType(ELFSym::STT_NOTYPE); |
| 214 | ExternalSym.SectionIdx = ELFSection::SHN_UNDEF; |
| 215 | SymbolTable.push_back(ExternalSym); |
| 216 | return; |
| 217 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 219 | const Type *GVType = (const Type*)GV->getType(); |
| 220 | unsigned Align = TM.getTargetData().getTypeAlignment(GVType); |
| 221 | unsigned Size = TM.getTargetData().getTypeSize(GVType); |
| 222 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 223 | // If this global has a zero initializer, it is part of the .bss or common |
| 224 | // section. |
| 225 | if (GV->getInitializer()->isNullValue()) { |
| 226 | // If this global is part of the common block, add it now. Variables are |
| 227 | // part of the common block if they are zero initialized and allowed to be |
| 228 | // merged with other symbols. |
| 229 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) { |
| 230 | ELFSym CommonSym(GV); |
| 231 | // Value for common symbols is the alignment required. |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 232 | CommonSym.Value = Align; |
| 233 | CommonSym.Size = Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 234 | CommonSym.SetBind(ELFSym::STB_GLOBAL); |
| 235 | CommonSym.SetType(ELFSym::STT_OBJECT); |
| 236 | // TODO SOMEDAY: add ELF visibility. |
| 237 | CommonSym.SectionIdx = ELFSection::SHN_COMMON; |
| 238 | SymbolTable.push_back(CommonSym); |
| 239 | return; |
| 240 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 242 | // Otherwise, this symbol is part of the .bss section. Emit it now. |
| 243 | |
| 244 | // Handle alignment. Ensure section is aligned at least as much as required |
| 245 | // by this symbol. |
| 246 | BSSSection.Align = std::max(BSSSection.Align, Align); |
| 247 | |
| 248 | // Within the section, emit enough virtual padding to get us to an alignment |
| 249 | // boundary. |
| 250 | if (Align) |
| 251 | BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1); |
| 252 | |
| 253 | ELFSym BSSSym(GV); |
| 254 | BSSSym.Value = BSSSection.Size; |
| 255 | BSSSym.Size = Size; |
| 256 | BSSSym.SetType(ELFSym::STT_OBJECT); |
| 257 | |
| 258 | switch (GV->getLinkage()) { |
| 259 | default: // weak/linkonce handled above |
| 260 | assert(0 && "Unexpected linkage type!"); |
| 261 | case GlobalValue::AppendingLinkage: // FIXME: This should be improved! |
| 262 | case GlobalValue::ExternalLinkage: |
| 263 | BSSSym.SetBind(ELFSym::STB_GLOBAL); |
| 264 | break; |
| 265 | case GlobalValue::InternalLinkage: |
| 266 | BSSSym.SetBind(ELFSym::STB_LOCAL); |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | // Set the idx of the .bss section |
| 271 | BSSSym.SectionIdx = &BSSSection-&SectionList[0]; |
| 272 | SymbolTable.push_back(BSSSym); |
| 273 | |
| 274 | // Reserve space in the .bss section for this symbol. |
| 275 | BSSSection.Size += Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 279 | // FIXME: handle .rodata |
| 280 | //assert(!GV->isConstant() && "unimp"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 282 | // FIXME: handle .data |
| 283 | //assert(0 && "unimp"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
| 287 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 288 | // Nothing to do here, this is all done through the MCE object above. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
| 292 | /// doFinalization - Now that the module has been completely processed, emit |
| 293 | /// the ELF file to 'O'. |
| 294 | bool ELFWriter::doFinalization(Module &M) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 295 | // Okay, the .text section has now been finalized. If it contains nothing, do |
| 296 | // not emit it. |
| 297 | uint64_t TextSize = OutputBuffer.size() - SectionList.back().Offset; |
| 298 | if (TextSize == 0) { |
| 299 | SectionList.pop_back(); |
| 300 | } else { |
| 301 | ELFSection &Text = SectionList.back(); |
| 302 | Text.Size = TextSize; |
| 303 | Text.Type = ELFSection::SHT_PROGBITS; |
| 304 | Text.Flags = ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC; |
| 305 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 306 | |
| 307 | // Okay, the ELF header and .text sections have been completed, build the |
| 308 | // .data, .bss, and "common" sections next. |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 309 | SectionList.push_back(ELFSection(".data", OutputBuffer.size())); |
| 310 | SectionList.push_back(ELFSection(".bss")); |
| 311 | ELFSection &DataSection = *(SectionList.end()-2); |
| 312 | ELFSection &BSSSection = SectionList.back(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 313 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 314 | I != E; ++I) |
| 315 | EmitGlobal(I, DataSection, BSSSection); |
| 316 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 317 | // Finish up the data section. |
| 318 | DataSection.Type = ELFSection::SHT_PROGBITS; |
| 319 | DataSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 321 | // The BSS Section logically starts at the end of the Data Section (adjusted |
| 322 | // to the required alignment of the BSSSection). |
| 323 | BSSSection.Offset = DataSection.Offset+DataSection.Size; |
| 324 | BSSSection.Type = ELFSection::SHT_NOBITS; |
| 325 | BSSSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC; |
| 326 | if (BSSSection.Align) |
| 327 | BSSSection.Offset = (BSSSection.Offset+BSSSection.Align-1) & |
| 328 | ~(BSSSection.Align-1); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 329 | |
| 330 | // Emit the symbol table now, if non-empty. |
| 331 | EmitSymbolTable(); |
| 332 | |
| 333 | // FIXME: Emit the relocations now. |
| 334 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 335 | // Emit the string table for the sections in the ELF file we have. |
| 336 | EmitSectionTableStringTable(); |
| 337 | |
| 338 | // Emit the .o file section table. |
| 339 | EmitSectionTable(); |
| 340 | |
| 341 | // Emit the .o file to the specified stream. |
| 342 | O.write((char*)&OutputBuffer[0], OutputBuffer.size()); |
| 343 | |
| 344 | // Free the output buffer. |
| 345 | std::vector<unsigned char>().swap(OutputBuffer); |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 346 | |
| 347 | // Release the name mangler object. |
| 348 | delete Mang; Mang = 0; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 352 | /// EmitSymbolTable - If the current symbol table is non-empty, emit the string |
| 353 | /// table for it and then the symbol table itself. |
| 354 | void ELFWriter::EmitSymbolTable() { |
| 355 | if (SymbolTable.size() == 1) return; // Only the null entry. |
| 356 | |
| 357 | // FIXME: compact all local symbols to the start of the symtab. |
| 358 | unsigned FirstNonLocalSymbol = 1; |
| 359 | |
| 360 | SectionList.push_back(ELFSection(".strtab", OutputBuffer.size())); |
| 361 | ELFSection &StrTab = SectionList.back(); |
| 362 | StrTab.Type = ELFSection::SHT_STRTAB; |
| 363 | StrTab.Align = 1; |
| 364 | |
| 365 | // Set the zero'th symbol to a null byte, as required. |
| 366 | outbyte(0); |
| 367 | SymbolTable[0].NameIdx = 0; |
| 368 | unsigned Index = 1; |
| 369 | for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 370 | // Use the name mangler to uniquify the LLVM symbol. |
| 371 | std::string Name = Mang->getValueName(SymbolTable[i].GV); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 372 | |
| 373 | if (Name.empty()) { |
| 374 | SymbolTable[i].NameIdx = 0; |
| 375 | } else { |
| 376 | SymbolTable[i].NameIdx = Index; |
| 377 | |
| 378 | // Add the name to the output buffer, including the null terminator. |
| 379 | OutputBuffer.insert(OutputBuffer.end(), Name.begin(), Name.end()); |
| 380 | |
| 381 | // Add a null terminator. |
| 382 | OutputBuffer.push_back(0); |
| 383 | |
| 384 | // Keep track of the number of bytes emitted to this section. |
| 385 | Index += Name.size()+1; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | StrTab.Size = OutputBuffer.size()-StrTab.Offset; |
| 390 | |
| 391 | // Now that we have emitted the string table and know the offset into the |
| 392 | // string table of each symbol, emit the symbol table itself. |
| 393 | assert(!is64Bit && "Should this be 8 byte aligned for 64-bit?" |
| 394 | " (check .Align below also)"); |
| 395 | align(4); |
| 396 | |
| 397 | SectionList.push_back(ELFSection(".symtab", OutputBuffer.size())); |
| 398 | ELFSection &SymTab = SectionList.back(); |
| 399 | SymTab.Type = ELFSection::SHT_SYMTAB; |
| 400 | SymTab.Align = 4; // FIXME: check for ELF64 |
| 401 | SymTab.Link = SectionList.size()-2; // Section Index of .strtab. |
| 402 | SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol. |
| 403 | SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64 |
| 404 | |
| 405 | assert(!is64Bit && "check this!"); |
| 406 | for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { |
| 407 | ELFSym &Sym = SymbolTable[i]; |
| 408 | outword(Sym.NameIdx); |
| 409 | outaddr(Sym.Value); |
| 410 | outword(Sym.Size); |
| 411 | outbyte(Sym.Info); |
| 412 | outbyte(Sym.Other); |
| 413 | outhalf(Sym.SectionIdx); |
| 414 | } |
| 415 | |
| 416 | SymTab.Size = OutputBuffer.size()-SymTab.Offset; |
| 417 | } |
| 418 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 419 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 420 | /// ELF Section Table string table: the string table that holds all of the |
| 421 | /// section names. |
| 422 | void ELFWriter::EmitSectionTableStringTable() { |
| 423 | // First step: add the section for the string table to the list of sections: |
| 424 | SectionList.push_back(ELFSection(".shstrtab", OutputBuffer.size())); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 425 | SectionList.back().Type = ELFSection::SHT_STRTAB; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 426 | |
| 427 | // Now that we know which section number is the .shstrtab section, update the |
| 428 | // e_shstrndx entry in the ELF header. |
| 429 | fixhalf(SectionList.size()-1, ELFHeader_e_shstrndx_Offset); |
| 430 | |
| 431 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 432 | // the string table. |
| 433 | unsigned Index = 0; |
| 434 | |
| 435 | for (unsigned i = 0, e = SectionList.size(); i != e; ++i) { |
| 436 | // Set the index into the table. Note if we have lots of entries with |
| 437 | // common suffixes, we could memoize them here if we cared. |
| 438 | SectionList[i].NameIdx = Index; |
| 439 | |
| 440 | // Add the name to the output buffer, including the null terminator. |
| 441 | OutputBuffer.insert(OutputBuffer.end(), SectionList[i].Name.begin(), |
| 442 | SectionList[i].Name.end()); |
| 443 | // Add a null terminator. |
| 444 | OutputBuffer.push_back(0); |
| 445 | |
| 446 | // Keep track of the number of bytes emitted to this section. |
| 447 | Index += SectionList[i].Name.size()+1; |
| 448 | } |
| 449 | |
| 450 | // Set the size of .shstrtab now that we know what it is. |
| 451 | SectionList.back().Size = Index; |
| 452 | } |
| 453 | |
| 454 | /// EmitSectionTable - Now that we have emitted the entire contents of the file |
| 455 | /// (all of the sections), emit the section table which informs the reader where |
| 456 | /// the boundaries are. |
| 457 | void ELFWriter::EmitSectionTable() { |
| 458 | // Now that all of the sections have been emitted, set the e_shnum entry in |
| 459 | // the ELF header. |
| 460 | fixhalf(SectionList.size(), ELFHeader_e_shnum_Offset); |
| 461 | |
| 462 | // Now that we know the offset in the file of the section table (which we emit |
| 463 | // next), update the e_shoff address in the ELF header. |
| 464 | fixaddr(OutputBuffer.size(), ELFHeader_e_shoff_Offset); |
| 465 | |
| 466 | // Emit all of the section table entries. |
| 467 | for (unsigned i = 0, e = SectionList.size(); i != e; ++i) { |
| 468 | const ELFSection &S = SectionList[i]; |
| 469 | outword(S.NameIdx); // sh_name - Symbol table name idx |
| 470 | outword(S.Type); // sh_type - Section contents & semantics |
| 471 | outword(S.Flags); // sh_flags - Section flags. |
| 472 | outaddr(S.Addr); // sh_addr - The mem address this section appears in. |
| 473 | outaddr(S.Offset); // sh_offset - The offset from the start of the file. |
| 474 | outword(S.Size); // sh_size - The section size. |
| 475 | outword(S.Link); // sh_link - Section header table index link. |
| 476 | outword(S.Info); // sh_info - Auxillary information. |
| 477 | outword(S.Align); // sh_addralign - Alignment of section. |
| 478 | outword(S.EntSize); // sh_entsize - Size of each entry in the section. |
| 479 | } |
| 480 | |
| 481 | // Release the memory allocated for the section list. |
| 482 | std::vector<ELFSection>().swap(SectionList); |
| 483 | } |