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