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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 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. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 28 | // |
| 29 | // NOTE: This code should eventually be extended to support 64-bit ELF (this |
| 30 | // won't be hard), but we haven't done so yet! |
| 31 | // |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 34 | #include "ELFWriter.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 35 | #include "ELFCodeEmitter.h" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 36 | #include "ELF.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 37 | #include "llvm/Module.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 38 | #include "llvm/PassManager.h" |
Chris Lattner | 02b73ab | 2009-01-04 20:19:20 +0000 | [diff] [blame] | 39 | #include "llvm/DerivedTypes.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/FileWriters.h" |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 42 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
Bill Wendling | 5d73a2a | 2007-01-27 02:55:44 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetELFWriterInfo.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Mangler.h" |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 48 | #include "llvm/Support/OutputBuffer.h" |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Streams.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 50 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 51 | #include <list> |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 54 | char ELFWriter::ID = 0; |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 55 | /// AddELFWriter - Concrete function to add the ELF writer to the function pass |
| 56 | /// manager. |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 57 | MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM, |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 58 | raw_ostream &O, |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 59 | TargetMachine &TM) { |
| 60 | ELFWriter *EW = new ELFWriter(O, TM); |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 61 | PM.add(EW); |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 62 | return &EW->getMachineCodeEmitter(); |
| 63 | } |
| 64 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 65 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 66 | // ELFWriter Implementation |
| 67 | //===----------------------------------------------------------------------===// |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 68 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 69 | ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 70 | : MachineFunctionPass(&ID), O(o), TM(tm) { |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 71 | e_flags = 0; // e_flags defaults to 0, no flags. |
| 72 | e_machine = TM.getELFWriterInfo()->getEMachine(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 73 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 74 | is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64; |
| 75 | isLittleEndian = TM.getTargetData()->isLittleEndian(); |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 76 | |
| 77 | // Create the machine code emitter object for this target. |
Bill Wendling | e911615 | 2007-01-17 09:06:13 +0000 | [diff] [blame] | 78 | MCE = new ELFCodeEmitter(*this); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 79 | NumSections = 0; |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ELFWriter::~ELFWriter() { |
| 83 | delete MCE; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // doInitialization - Emit the file header and all of the global variables for |
| 87 | // the module to the ELF file. |
| 88 | bool ELFWriter::doInitialization(Module &M) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 89 | Mang = new Mangler(M); |
| 90 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 91 | // Local alias to shortenify coming code. |
| 92 | std::vector<unsigned char> &FH = FileHeader; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 93 | OutputBuffer FHOut(FH, is64Bit, isLittleEndian); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 94 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 95 | unsigned ElfClass = is64Bit ? ELFCLASS64 : ELFCLASS32; |
| 96 | unsigned ElfEndian = isLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 97 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 98 | // ELF Header |
| 99 | // ---------- |
| 100 | // Fields e_shnum e_shstrndx are only known after all section have |
| 101 | // been emitted. They locations in the ouput buffer are recorded so |
| 102 | // to be patched up later. |
| 103 | // |
| 104 | // Note |
| 105 | // ---- |
| 106 | // FHOut.outaddr method behaves differently for ELF32 and ELF64 writing |
| 107 | // 4 bytes in the former and 8 in the last for *_off and *_addr elf types |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 108 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 109 | FHOut.outbyte(0x7f); // e_ident[EI_MAG0] |
| 110 | FHOut.outbyte('E'); // e_ident[EI_MAG1] |
| 111 | FHOut.outbyte('L'); // e_ident[EI_MAG2] |
| 112 | FHOut.outbyte('F'); // e_ident[EI_MAG3] |
| 113 | |
| 114 | FHOut.outbyte(ElfClass); // e_ident[EI_CLASS] |
| 115 | FHOut.outbyte(ElfEndian); // e_ident[EI_DATA] |
| 116 | FHOut.outbyte(EV_CURRENT); // e_ident[EI_VERSION] |
| 117 | |
| 118 | FH.resize(16); // e_ident[EI_NIDENT-EI_PAD] |
| 119 | |
| 120 | FHOut.outhalf(ET_REL); // e_type |
| 121 | FHOut.outhalf(e_machine); // e_machine = target |
| 122 | FHOut.outword(EV_CURRENT); // e_version |
| 123 | FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file |
| 124 | FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o |
| 125 | |
| 126 | ELFHdr_e_shoff_Offset = FH.size(); |
| 127 | FHOut.outaddr(0); // e_shoff = sec hdr table off in bytes |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 128 | FHOut.outword(e_flags); // e_flags = whatever the target wants |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 129 | |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 130 | FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size |
| 131 | FHOut.outhalf(0); // e_phentsize = prog header entry size |
| 132 | FHOut.outhalf(0); // e_phnum = # prog header entries = 0 |
| 133 | FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 134 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 135 | // e_shnum = # of section header ents |
| 136 | ELFHdr_e_shnum_Offset = FH.size(); |
| 137 | FHOut.outhalf(0); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 138 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 139 | // e_shstrndx = Section # of '.shstrtab' |
| 140 | ELFHdr_e_shstrndx_Offset = FH.size(); |
| 141 | FHOut.outhalf(0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 143 | // Add the null section, which is required to be first in the file. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 144 | getSection("", ELFSection::SHT_NULL, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 146 | // Start up the symbol table. The first entry in the symtab is the null |
| 147 | // entry. |
| 148 | SymbolTable.push_back(ELFSym(0)); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 153 | void ELFWriter::EmitGlobal(GlobalVariable *GV) { |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 154 | // If this is an external global, emit it now. TODO: Note that it would be |
| 155 | // better to ignore the symbol here and only add it to the symbol table if |
| 156 | // referenced. |
| 157 | if (!GV->hasInitializer()) { |
| 158 | ELFSym ExternalSym(GV); |
| 159 | ExternalSym.SetBind(ELFSym::STB_GLOBAL); |
| 160 | ExternalSym.SetType(ELFSym::STT_NOTYPE); |
| 161 | ExternalSym.SectionIdx = ELFSection::SHN_UNDEF; |
| 162 | SymbolTable.push_back(ExternalSym); |
| 163 | return; |
| 164 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 165 | |
Duncan Sands | d102593 | 2008-01-29 06:23:44 +0000 | [diff] [blame] | 166 | unsigned Align = TM.getTargetData()->getPreferredAlignment(GV); |
Chris Lattner | 02b73ab | 2009-01-04 20:19:20 +0000 | [diff] [blame] | 167 | unsigned Size = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 168 | TM.getTargetData()->getTypeAllocSize(GV->getType()->getElementType()); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 170 | // If this global has a zero initializer, it is part of the .bss or common |
| 171 | // section. |
| 172 | if (GV->getInitializer()->isNullValue()) { |
| 173 | // If this global is part of the common block, add it now. Variables are |
| 174 | // part of the common block if they are zero initialized and allowed to be |
| 175 | // merged with other symbols. |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 176 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
| 177 | GV->hasCommonLinkage()) { |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 178 | ELFSym CommonSym(GV); |
| 179 | // Value for common symbols is the alignment required. |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 180 | CommonSym.Value = Align; |
| 181 | CommonSym.Size = Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 182 | CommonSym.SetBind(ELFSym::STB_GLOBAL); |
| 183 | CommonSym.SetType(ELFSym::STT_OBJECT); |
| 184 | // TODO SOMEDAY: add ELF visibility. |
| 185 | CommonSym.SectionIdx = ELFSection::SHN_COMMON; |
| 186 | SymbolTable.push_back(CommonSym); |
| 187 | return; |
| 188 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 190 | // Otherwise, this symbol is part of the .bss section. Emit it now. |
| 191 | |
| 192 | // Handle alignment. Ensure section is aligned at least as much as required |
| 193 | // by this symbol. |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 194 | ELFSection &BSSSection = getBSSSection(); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 195 | BSSSection.Align = std::max(BSSSection.Align, Align); |
| 196 | |
| 197 | // Within the section, emit enough virtual padding to get us to an alignment |
| 198 | // boundary. |
| 199 | if (Align) |
| 200 | BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1); |
| 201 | |
| 202 | ELFSym BSSSym(GV); |
| 203 | BSSSym.Value = BSSSection.Size; |
| 204 | BSSSym.Size = Size; |
| 205 | BSSSym.SetType(ELFSym::STT_OBJECT); |
| 206 | |
| 207 | switch (GV->getLinkage()) { |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 208 | default: // weak/linkonce/common handled above |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 209 | assert(0 && "Unexpected linkage type!"); |
| 210 | case GlobalValue::AppendingLinkage: // FIXME: This should be improved! |
| 211 | case GlobalValue::ExternalLinkage: |
| 212 | BSSSym.SetBind(ELFSym::STB_GLOBAL); |
| 213 | break; |
| 214 | case GlobalValue::InternalLinkage: |
| 215 | BSSSym.SetBind(ELFSym::STB_LOCAL); |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | // Set the idx of the .bss section |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 220 | BSSSym.SectionIdx = BSSSection.SectionIdx; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 221 | if (!GV->hasPrivateLinkage()) |
| 222 | SymbolTable.push_back(BSSSym); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 223 | |
| 224 | // Reserve space in the .bss section for this symbol. |
| 225 | BSSSection.Size += Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 229 | // FIXME: handle .rodata |
| 230 | //assert(!GV->isConstant() && "unimp"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 232 | // FIXME: handle .data |
| 233 | //assert(0 && "unimp"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | |
| 237 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 238 | // 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] | 239 | return false; |
| 240 | } |
| 241 | |
| 242 | /// doFinalization - Now that the module has been completely processed, emit |
| 243 | /// the ELF file to 'O'. |
| 244 | bool ELFWriter::doFinalization(Module &M) { |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 245 | // Okay, the ELF header and .text sections have been completed, build the |
| 246 | // .data, .bss, and "common" sections next. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 247 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 248 | I != E; ++I) |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 249 | EmitGlobal(I); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 250 | |
| 251 | // Emit the symbol table now, if non-empty. |
| 252 | EmitSymbolTable(); |
| 253 | |
| 254 | // FIXME: Emit the relocations now. |
| 255 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 256 | // Emit the string table for the sections in the ELF file we have. |
| 257 | EmitSectionTableStringTable(); |
| 258 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 259 | // Emit the sections to the .o file, and emit the section table for the file. |
| 260 | OutputSectionsAndSectionTable(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 262 | // We are done with the abstract symbols. |
| 263 | SectionList.clear(); |
| 264 | NumSections = 0; |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 265 | |
| 266 | // Release the name mangler object. |
| 267 | delete Mang; Mang = 0; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 271 | /// EmitSymbolTable - If the current symbol table is non-empty, emit the string |
| 272 | /// table for it and then the symbol table itself. |
| 273 | void ELFWriter::EmitSymbolTable() { |
| 274 | if (SymbolTable.size() == 1) return; // Only the null entry. |
| 275 | |
| 276 | // FIXME: compact all local symbols to the start of the symtab. |
| 277 | unsigned FirstNonLocalSymbol = 1; |
| 278 | |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 279 | ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 280 | StrTab.Align = 1; |
| 281 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 282 | DataBuffer &StrTabBuf = StrTab.SectionData; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 283 | OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 285 | // Set the zero'th symbol to a null byte, as required. |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 286 | StrTabOut.outbyte(0); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 287 | SymbolTable[0].NameIdx = 0; |
| 288 | unsigned Index = 1; |
| 289 | for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 290 | // Use the name mangler to uniquify the LLVM symbol. |
| 291 | std::string Name = Mang->getValueName(SymbolTable[i].GV); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 292 | |
| 293 | if (Name.empty()) { |
| 294 | SymbolTable[i].NameIdx = 0; |
| 295 | } else { |
| 296 | SymbolTable[i].NameIdx = Index; |
| 297 | |
| 298 | // Add the name to the output buffer, including the null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 299 | StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end()); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 300 | |
| 301 | // Add a null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 302 | StrTabBuf.push_back(0); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 303 | |
| 304 | // Keep track of the number of bytes emitted to this section. |
| 305 | Index += Name.size()+1; |
| 306 | } |
| 307 | } |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 308 | assert(Index == StrTabBuf.size()); |
| 309 | StrTab.Size = Index; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 310 | |
| 311 | // Now that we have emitted the string table and know the offset into the |
| 312 | // string table of each symbol, emit the symbol table itself. |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 313 | ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0); |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 314 | SymTab.Align = is64Bit ? 8 : 4; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 315 | SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab. |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 316 | SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol. |
| 317 | SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64 |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 318 | DataBuffer &SymTabBuf = SymTab.SectionData; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 319 | OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 321 | if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit. |
| 322 | for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { |
| 323 | ELFSym &Sym = SymbolTable[i]; |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 324 | SymTabOut.outword(Sym.NameIdx); |
| 325 | SymTabOut.outaddr32(Sym.Value); |
| 326 | SymTabOut.outword(Sym.Size); |
| 327 | SymTabOut.outbyte(Sym.Info); |
| 328 | SymTabOut.outbyte(Sym.Other); |
| 329 | SymTabOut.outhalf(Sym.SectionIdx); |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { |
| 333 | ELFSym &Sym = SymbolTable[i]; |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 334 | SymTabOut.outword(Sym.NameIdx); |
| 335 | SymTabOut.outbyte(Sym.Info); |
| 336 | SymTabOut.outbyte(Sym.Other); |
| 337 | SymTabOut.outhalf(Sym.SectionIdx); |
| 338 | SymTabOut.outaddr64(Sym.Value); |
| 339 | SymTabOut.outxword(Sym.Size); |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 340 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 343 | SymTab.Size = SymTabBuf.size(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 346 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 347 | /// ELF Section Table string table: the string table that holds all of the |
| 348 | /// section names. |
| 349 | void ELFWriter::EmitSectionTableStringTable() { |
| 350 | // First step: add the section for the string table to the list of sections: |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 351 | ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 352 | |
| 353 | // Now that we know which section number is the .shstrtab section, update the |
| 354 | // e_shstrndx entry in the ELF header. |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 355 | OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 356 | FHOut.fixhalf(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 357 | |
| 358 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 359 | // the string table. |
| 360 | unsigned Index = 0; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 361 | DataBuffer &Buf = SHStrTab.SectionData; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 363 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 364 | E = SectionList.end(); I != E; ++I) { |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 365 | // Set the index into the table. Note if we have lots of entries with |
| 366 | // common suffixes, we could memoize them here if we cared. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 367 | I->NameIdx = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 368 | |
| 369 | // Add the name to the output buffer, including the null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 370 | Buf.insert(Buf.end(), I->Name.begin(), I->Name.end()); |
| 371 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 372 | // Add a null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 373 | Buf.push_back(0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 374 | |
| 375 | // Keep track of the number of bytes emitted to this section. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 376 | Index += I->Name.size()+1; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // Set the size of .shstrtab now that we know what it is. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 380 | assert(Index == Buf.size()); |
| 381 | SHStrTab.Size = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 384 | /// OutputSectionsAndSectionTable - Now that we have constructed the file header |
| 385 | /// and all of the sections, emit these to the ostream destination and emit the |
| 386 | /// SectionTable. |
| 387 | void ELFWriter::OutputSectionsAndSectionTable() { |
| 388 | // Pass #1: Compute the file offset for each section. |
| 389 | size_t FileOff = FileHeader.size(); // File header first. |
| 390 | |
| 391 | // Emit all of the section data in order. |
| 392 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 393 | E = SectionList.end(); I != E; ++I) { |
| 394 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 395 | if (I->Align) |
| 396 | FileOff = (FileOff+I->Align-1) & ~(I->Align-1); |
| 397 | I->Offset = FileOff; |
| 398 | FileOff += I->SectionData.size(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 401 | // Align Section Header. |
| 402 | unsigned TableAlign = is64Bit ? 8 : 4; |
| 403 | FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 404 | |
| 405 | // Now that we know where all of the sections will be emitted, set the e_shnum |
| 406 | // entry in the ELF header. |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 407 | OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 408 | FHOut.fixhalf(NumSections, ELFHdr_e_shnum_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 410 | // Now that we know the offset in the file of the section table, update the |
| 411 | // e_shoff address in the ELF header. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame^] | 412 | FHOut.fixaddr(FileOff, ELFHdr_e_shoff_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 414 | // Now that we know all of the data in the file header, emit it and all of the |
| 415 | // sections! |
| 416 | O.write((char*)&FileHeader[0], FileHeader.size()); |
| 417 | FileOff = FileHeader.size(); |
| 418 | DataBuffer().swap(FileHeader); |
| 419 | |
| 420 | DataBuffer Table; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 421 | OutputBuffer TableOut(Table, is64Bit, isLittleEndian); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 422 | |
| 423 | // Emit all of the section data and build the section table itself. |
| 424 | while (!SectionList.empty()) { |
| 425 | const ELFSection &S = *SectionList.begin(); |
| 426 | |
| 427 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 428 | if (S.Align) |
| 429 | for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1); |
| 430 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 431 | O << (char)0xAB; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 432 | O.write((char*)&S.SectionData[0], S.SectionData.size()); |
| 433 | FileOff += S.SectionData.size(); |
| 434 | |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 435 | TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx |
| 436 | TableOut.outword(S.Type); // sh_type - Section contents & semantics |
| 437 | TableOut.outword(S.Flags); // sh_flags - Section flags. |
| 438 | TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in. |
| 439 | TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start. |
| 440 | TableOut.outword(S.Size); // sh_size - The section size. |
| 441 | TableOut.outword(S.Link); // sh_link - Section header table index link. |
| 442 | TableOut.outword(S.Info); // sh_info - Auxillary information. |
| 443 | TableOut.outword(S.Align); // sh_addralign - Alignment of section. |
| 444 | TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 445 | |
| 446 | SectionList.pop_front(); |
| 447 | } |
| 448 | |
| 449 | // Align output for the section table. |
| 450 | for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 451 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 452 | O << (char)0xAB; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 454 | // Emit the section table itself. |
| 455 | O.write((char*)&Table[0], Table.size()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 456 | } |