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 | // |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "elfwriter" |
| 32 | |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 33 | #include "ELFWriter.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 34 | #include "ELFCodeEmitter.h" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 35 | #include "ELF.h" |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 36 | #include "llvm/Constants.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" |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/BinaryObject.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/FileWriters.h" |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 43 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetData.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 | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Streams.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.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) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 70 | : MachineFunctionPass(&ID), O(o), TM(tm), |
| 71 | is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64), |
| 72 | isLittleEndian(TM.getTargetData()->isLittleEndian()), |
| 73 | ElfHdr(isLittleEndian, is64Bit) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 74 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 75 | TAI = TM.getTargetAsmInfo(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 76 | TEW = TM.getELFWriterInfo(); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 77 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 78 | // Create the machine code emitter object for this target. |
Bill Wendling | e911615 | 2007-01-17 09:06:13 +0000 | [diff] [blame] | 79 | MCE = new ELFCodeEmitter(*this); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 80 | |
| 81 | // Inital number of sections |
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; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // doInitialization - Emit the file header and all of the global variables for |
| 90 | // the module to the ELF file. |
| 91 | bool ELFWriter::doInitialization(Module &M) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 92 | Mang = new Mangler(M); |
| 93 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 94 | // ELF Header |
| 95 | // ---------- |
| 96 | // Fields e_shnum e_shstrndx are only known after all section have |
| 97 | // been emitted. They locations in the ouput buffer are recorded so |
| 98 | // to be patched up later. |
| 99 | // |
| 100 | // Note |
| 101 | // ---- |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 102 | // emitWord method behaves differently for ELF32 and ELF64, writing |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 103 | // 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] | 104 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 105 | ElfHdr.emitByte(0x7f); // e_ident[EI_MAG0] |
| 106 | ElfHdr.emitByte('E'); // e_ident[EI_MAG1] |
| 107 | ElfHdr.emitByte('L'); // e_ident[EI_MAG2] |
| 108 | ElfHdr.emitByte('F'); // e_ident[EI_MAG3] |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 109 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 110 | ElfHdr.emitByte(TEW->getEIClass()); // e_ident[EI_CLASS] |
| 111 | ElfHdr.emitByte(TEW->getEIData()); // e_ident[EI_DATA] |
| 112 | ElfHdr.emitByte(EV_CURRENT); // e_ident[EI_VERSION] |
| 113 | ElfHdr.emitAlignment(16); // e_ident[EI_NIDENT-EI_PAD] |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 114 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 115 | ElfHdr.emitWord16(ET_REL); // e_type |
| 116 | ElfHdr.emitWord16(TEW->getEMachine()); // e_machine = target |
| 117 | ElfHdr.emitWord32(EV_CURRENT); // e_version |
| 118 | ElfHdr.emitWord(0); // e_entry, no entry point in .o file |
| 119 | ElfHdr.emitWord(0); // e_phoff, no program header for .o |
| 120 | ELFHdr_e_shoff_Offset = ElfHdr.size(); |
| 121 | ElfHdr.emitWord(0); // e_shoff = sec hdr table off in bytes |
| 122 | ElfHdr.emitWord32(TEW->getEFlags()); // e_flags = whatever the target wants |
| 123 | ElfHdr.emitWord16(TEW->getHdrSize()); // e_ehsize = ELF header size |
| 124 | ElfHdr.emitWord16(0); // e_phentsize = prog header entry size |
| 125 | ElfHdr.emitWord16(0); // e_phnum = # prog header entries = 0 |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 126 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 127 | // e_shentsize = Section header entry size |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 128 | ElfHdr.emitWord16(TEW->getSHdrSize()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 129 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 130 | // e_shnum = # of section header ents |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 131 | ELFHdr_e_shnum_Offset = ElfHdr.size(); |
| 132 | ElfHdr.emitWord16(0); // Placeholder |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 133 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 134 | // e_shstrndx = Section # of '.shstrtab' |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 135 | ELFHdr_e_shstrndx_Offset = ElfHdr.size(); |
| 136 | ElfHdr.emitWord16(0); // Placeholder |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 138 | // Add the null section, which is required to be first in the file. |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 139 | getNullSection(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 144 | unsigned ELFWriter::getGlobalELFLinkage(const GlobalVariable *GV) { |
| 145 | if (GV->hasInternalLinkage()) |
| 146 | return ELFSym::STB_LOCAL; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 147 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 148 | if (GV->hasWeakLinkage()) |
| 149 | return ELFSym::STB_WEAK; |
| 150 | |
| 151 | return ELFSym::STB_GLOBAL; |
| 152 | } |
| 153 | |
| 154 | // For global symbols without a section, return the Null section as a |
| 155 | // placeholder |
| 156 | ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV, |
| 157 | ELFSym &Sym) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 158 | const Section *S = TAI->SectionForGlobal(GV); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 159 | unsigned Flags = S->getFlags(); |
| 160 | unsigned SectionType = ELFSection::SHT_PROGBITS; |
| 161 | unsigned SHdrFlags = ELFSection::SHF_ALLOC; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 162 | const TargetData *TD = TM.getTargetData(); |
| 163 | unsigned Align = TD->getPreferredAlignment(GV); |
| 164 | Constant *CV = GV->getInitializer(); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 165 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 166 | DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n"; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 167 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 168 | // If this is an external global, the symbol does not have a section. |
| 169 | if (!GV->hasInitializer()) { |
| 170 | Sym.SectionIdx = ELFSection::SHN_UNDEF; |
| 171 | return getNullSection(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 173 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 174 | if (Flags & SectionFlags::Code) |
| 175 | SHdrFlags |= ELFSection::SHF_EXECINSTR; |
| 176 | if (Flags & SectionFlags::Writeable) |
| 177 | SHdrFlags |= ELFSection::SHF_WRITE; |
| 178 | if (Flags & SectionFlags::Mergeable) |
| 179 | SHdrFlags |= ELFSection::SHF_MERGE; |
| 180 | if (Flags & SectionFlags::TLS) |
| 181 | SHdrFlags |= ELFSection::SHF_TLS; |
| 182 | if (Flags & SectionFlags::Strings) |
| 183 | SHdrFlags |= ELFSection::SHF_STRINGS; |
| 184 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 185 | // If this global has a zero initializer, go to .bss or common section. |
| 186 | // Variables are part of the common block if they are zero initialized |
| 187 | // and allowed to be merged with other symbols. |
| 188 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
| 189 | SectionType = ELFSection::SHT_NOBITS; |
| 190 | ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags); |
| 191 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
| 192 | GV->hasCommonLinkage()) { |
| 193 | Sym.SectionIdx = ELFSection::SHN_COMMON; |
| 194 | Sym.IsCommon = true; |
| 195 | return ElfS; |
| 196 | } |
| 197 | Sym.IsBss = true; |
| 198 | Sym.SectionIdx = ElfS.SectionIdx; |
| 199 | if (Align) ElfS.Size = (ElfS.Size + Align-1) & ~(Align-1); |
| 200 | ElfS.Align = std::max(ElfS.Align, Align); |
| 201 | return ElfS; |
| 202 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 203 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 204 | Sym.IsConstant = true; |
| 205 | ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags); |
| 206 | Sym.SectionIdx = ElfS.SectionIdx; |
| 207 | ElfS.Align = std::max(ElfS.Align, Align); |
| 208 | return ElfS; |
| 209 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 210 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 211 | void ELFWriter::EmitFunctionDeclaration(const Function *F) { |
| 212 | ELFSym GblSym(F); |
| 213 | GblSym.setBind(ELFSym::STB_GLOBAL); |
| 214 | GblSym.setType(ELFSym::STT_NOTYPE); |
| 215 | GblSym.SectionIdx = ELFSection::SHN_UNDEF; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 216 | SymbolList.push_back(GblSym); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 219 | void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) { |
| 220 | unsigned SymBind = getGlobalELFLinkage(GV); |
| 221 | ELFSym GblSym(GV); |
| 222 | GblSym.setBind(SymBind); |
| 223 | |
| 224 | if (GV->hasInitializer()) |
| 225 | GblSym.setType(ELFSym::STT_OBJECT); |
| 226 | else |
| 227 | GblSym.setType(ELFSym::STT_NOTYPE); |
| 228 | |
| 229 | ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym); |
| 230 | const TargetData *TD = TM.getTargetData(); |
| 231 | unsigned Align = TD->getPreferredAlignment(GV); |
| 232 | unsigned Size = TD->getTypeAllocSize(GV->getInitializer()->getType()); |
| 233 | GblSym.Size = Size; |
| 234 | |
| 235 | if (GblSym.IsCommon) { |
| 236 | GblSym.Value = Align; |
| 237 | } else if (GblSym.IsBss) { |
| 238 | GblSym.Value = GblSection.Size; |
| 239 | GblSection.Size += Size; |
| 240 | } else if (GblSym.IsConstant){ |
| 241 | // GblSym.Value should contain the symbol index inside the section, |
| 242 | // and all symbols should start on their required alignment boundary |
| 243 | GblSym.Value = (GblSection.size() + (Align-1)) & (-Align); |
| 244 | GblSection.emitAlignment(Align); |
| 245 | EmitGlobalConstant(GV->getInitializer(), GblSection); |
| 246 | } |
| 247 | |
| 248 | // Local symbols should come first on the symbol table. |
| 249 | if (!GV->hasPrivateLinkage()) { |
| 250 | if (SymBind == ELFSym::STB_LOCAL) |
| 251 | SymbolList.push_front(GblSym); |
| 252 | else |
| 253 | SymbolList.push_back(GblSym); |
| 254 | } |
| 255 | } |
| 256 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 257 | void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS, |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 258 | ELFSection &GblS) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 259 | |
| 260 | // Print the fields in successive locations. Pad to align if needed! |
| 261 | const TargetData *TD = TM.getTargetData(); |
| 262 | unsigned Size = TD->getTypeAllocSize(CVS->getType()); |
| 263 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
| 264 | uint64_t sizeSoFar = 0; |
| 265 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 266 | const Constant* field = CVS->getOperand(i); |
| 267 | |
| 268 | // Check if padding is needed and insert one or more 0s. |
| 269 | uint64_t fieldSize = TD->getTypeAllocSize(field->getType()); |
| 270 | uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) |
| 271 | - cvsLayout->getElementOffset(i)) - fieldSize; |
| 272 | sizeSoFar += fieldSize + padSize; |
| 273 | |
| 274 | // Now print the actual field value. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 275 | EmitGlobalConstant(field, GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 276 | |
| 277 | // Insert padding - this may include padding to increase the size of the |
| 278 | // current field up to the ABI size (if the struct is not packed) as well |
| 279 | // as padding to ensure that the next field starts at the right offset. |
| 280 | for (unsigned p=0; p < padSize; p++) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 281 | GblS.emitByte(0); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 282 | } |
| 283 | assert(sizeSoFar == cvsLayout->getSizeInBytes() && |
| 284 | "Layout of constant struct may be incorrect!"); |
| 285 | } |
| 286 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 287 | void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 288 | const TargetData *TD = TM.getTargetData(); |
| 289 | unsigned Size = TD->getTypeAllocSize(CV->getType()); |
| 290 | |
| 291 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 292 | if (CVA->isString()) { |
| 293 | std::string GblStr = CVA->getAsString(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 294 | GblStr.resize(GblStr.size()-1); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 295 | GblS.emitString(GblStr); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 296 | } else { // Not a string. Print the values in successive locations |
| 297 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 298 | EmitGlobalConstant(CVA->getOperand(i), GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 299 | } |
| 300 | return; |
| 301 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 302 | EmitGlobalConstantStruct(CVS, GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 303 | return; |
| 304 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 305 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 306 | if (CFP->getType() == Type::DoubleTy) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 307 | GblS.emitWord64(Val); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 308 | else if (CFP->getType() == Type::FloatTy) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 309 | GblS.emitWord32(Val); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 310 | else if (CFP->getType() == Type::X86_FP80Ty) { |
| 311 | assert(0 && "X86_FP80Ty global emission not implemented"); |
| 312 | } else if (CFP->getType() == Type::PPC_FP128Ty) |
| 313 | assert(0 && "PPC_FP128Ty global emission not implemented"); |
| 314 | return; |
| 315 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 316 | if (Size == 4) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 317 | GblS.emitWord32(CI->getZExtValue()); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 318 | else if (Size == 8) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 319 | GblS.emitWord64(CI->getZExtValue()); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 320 | else |
| 321 | assert(0 && "LargeInt global emission not implemented"); |
| 322 | return; |
| 323 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
| 324 | const VectorType *PTy = CP->getType(); |
| 325 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 326 | EmitGlobalConstant(CP->getOperand(I), GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | assert(0 && "unknown global constant"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 334 | // 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] | 335 | return false; |
| 336 | } |
| 337 | |
| 338 | /// doFinalization - Now that the module has been completely processed, emit |
| 339 | /// the ELF file to 'O'. |
| 340 | bool ELFWriter::doFinalization(Module &M) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 341 | /// FIXME: This should be removed when moving to ObjectCodeEmiter. Since the |
| 342 | /// current ELFCodeEmiter uses CurrBuff, ... it doesn't update S.Data |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 343 | /// vector size for .text sections, so this is a quick dirty fix |
| 344 | ELFSection &TS = getTextSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 345 | if (TS.Size) { |
| 346 | BinaryData &BD = TS.getData(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 347 | for (unsigned e=0; e<TS.Size; ++e) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 348 | BD.push_back(BD[e]); |
| 349 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 350 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 351 | // Emit .data section placeholder |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 352 | getDataSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 353 | |
| 354 | // Emit .bss section placeholder |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 355 | getBSSSection(); |
| 356 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 357 | // Build and emit data, bss and "common" sections. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 358 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 359 | I != E; ++I) { |
| 360 | EmitGlobalVar(I); |
| 361 | GblSymLookup[I] = 0; |
| 362 | } |
| 363 | |
| 364 | // Emit all pending globals |
| 365 | // TODO: this should be done only for referenced symbols |
| 366 | for (SetVector<GlobalValue*>::const_iterator I = PendingGlobals.begin(), |
| 367 | E = PendingGlobals.end(); I != E; ++I) { |
| 368 | |
| 369 | // No need to emit the symbol again |
| 370 | if (GblSymLookup.find(*I) != GblSymLookup.end()) |
| 371 | continue; |
| 372 | |
| 373 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) { |
| 374 | EmitGlobalVar(GV); |
| 375 | } else if (Function *F = dyn_cast<Function>(*I)) { |
| 376 | // If function is not in GblSymLookup, it doesn't have a body, |
| 377 | // so emit the symbol as a function declaration (no section associated) |
| 378 | EmitFunctionDeclaration(F); |
| 379 | } else { |
| 380 | assert("unknown howto handle pending global"); |
| 381 | } |
| 382 | GblSymLookup[*I] = 0; |
| 383 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 384 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 385 | // Emit non-executable stack note |
| 386 | if (TAI->getNonexecutableStackDirective()) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 387 | getNonExecStackSection(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 389 | // Emit the symbol table now, if non-empty. |
| 390 | EmitSymbolTable(); |
| 391 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 392 | // Emit the relocation sections. |
| 393 | EmitRelocations(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 394 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 395 | // Emit the sections string table. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 396 | EmitSectionTableStringTable(); |
| 397 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 398 | // Dump the sections and section table to the .o file. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 399 | OutputSectionsAndSectionTable(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 401 | // We are done with the abstract symbols. |
| 402 | SectionList.clear(); |
| 403 | NumSections = 0; |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 404 | |
| 405 | // Release the name mangler object. |
| 406 | delete Mang; Mang = 0; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 410 | /// EmitRelocations - Emit relocations |
| 411 | void ELFWriter::EmitRelocations() { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 412 | |
| 413 | // Create Relocation sections for each section which needs it. |
| 414 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 415 | E = SectionList.end(); I != E; ++I) { |
| 416 | |
| 417 | // This section does not have relocations |
| 418 | if (!I->hasRelocations()) continue; |
| 419 | |
| 420 | // Get the relocation section for section 'I' |
| 421 | bool HasRelA = TEW->hasRelocationAddend(); |
| 422 | ELFSection &RelSec = getRelocSection(I->getName(), HasRelA); |
| 423 | |
| 424 | // 'Link' - Section hdr idx of the associated symbol table |
| 425 | // 'Info' - Section hdr idx of the section to which the relocation applies |
| 426 | ELFSection &SymTab = getSymbolTableSection(); |
| 427 | RelSec.Link = SymTab.SectionIdx; |
| 428 | RelSec.Info = I->SectionIdx; |
| 429 | RelSec.EntSize = TEW->getRelocationEntrySize(); |
| 430 | |
| 431 | // Get the relocations from Section |
| 432 | std::vector<MachineRelocation> Relos = I->getRelocations(); |
| 433 | for (std::vector<MachineRelocation>::iterator MRI = Relos.begin(), |
| 434 | MRE = Relos.end(); MRI != MRE; ++MRI) { |
| 435 | MachineRelocation &MR = *MRI; |
| 436 | |
| 437 | // Offset from the start of the section containing the symbol |
| 438 | unsigned Offset = MR.getMachineCodeOffset(); |
| 439 | |
| 440 | // Symbol index in the symbol table |
| 441 | unsigned SymIdx = 0; |
| 442 | |
| 443 | // Target specific ELF relocation type |
| 444 | unsigned RelType = TEW->getRelocationType(MR.getRelocationType()); |
| 445 | |
| 446 | // Constant addend used to compute the value to be stored |
| 447 | // into the relocatable field |
| 448 | int64_t Addend = TEW->getAddendForRelTy(RelType); |
| 449 | |
| 450 | // There are several machine relocations types, and each one of |
| 451 | // them needs a different approach to retrieve the symbol table index. |
| 452 | if (MR.isGlobalValue()) { |
| 453 | const GlobalValue *G = MR.getGlobalValue(); |
| 454 | SymIdx = GblSymLookup[G]; |
| 455 | } else { |
| 456 | assert(0 && "dunno how to handle other relocation types"); |
| 457 | } |
| 458 | |
| 459 | // Get the relocation entry and emit to the relocation section |
| 460 | ELFRelocation Rel(Offset, SymIdx, RelType, HasRelA, Addend); |
| 461 | EmitRelocation(RelSec, Rel, HasRelA); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel' |
| 467 | void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, |
| 468 | bool HasRelA) { |
| 469 | RelSec.emitWord(Rel.getOffset()); |
| 470 | RelSec.emitWord(Rel.getInfo(is64Bit)); |
| 471 | if (HasRelA) |
| 472 | RelSec.emitWord(Rel.getAddend()); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 475 | /// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable' |
| 476 | void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 477 | if (is64Bit) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 478 | SymbolTable.emitWord32(Sym.NameIdx); |
| 479 | SymbolTable.emitByte(Sym.Info); |
| 480 | SymbolTable.emitByte(Sym.Other); |
| 481 | SymbolTable.emitWord16(Sym.SectionIdx); |
| 482 | SymbolTable.emitWord64(Sym.Value); |
| 483 | SymbolTable.emitWord64(Sym.Size); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 484 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 485 | SymbolTable.emitWord32(Sym.NameIdx); |
| 486 | SymbolTable.emitWord32(Sym.Value); |
| 487 | SymbolTable.emitWord32(Sym.Size); |
| 488 | SymbolTable.emitByte(Sym.Info); |
| 489 | SymbolTable.emitByte(Sym.Other); |
| 490 | SymbolTable.emitWord16(Sym.SectionIdx); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 494 | /// EmitSectionHeader - Write section 'Section' header in 'SHdrTab' |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 495 | /// Section Header Table |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 496 | void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab, |
| 497 | const ELFSection &SHdr) { |
| 498 | SHdrTab.emitWord32(SHdr.NameIdx); |
| 499 | SHdrTab.emitWord32(SHdr.Type); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 500 | if (is64Bit) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 501 | SHdrTab.emitWord64(SHdr.Flags); |
| 502 | SHdrTab.emitWord(SHdr.Addr); |
| 503 | SHdrTab.emitWord(SHdr.Offset); |
| 504 | SHdrTab.emitWord64(SHdr.Size); |
| 505 | SHdrTab.emitWord32(SHdr.Link); |
| 506 | SHdrTab.emitWord32(SHdr.Info); |
| 507 | SHdrTab.emitWord64(SHdr.Align); |
| 508 | SHdrTab.emitWord64(SHdr.EntSize); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 509 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 510 | SHdrTab.emitWord32(SHdr.Flags); |
| 511 | SHdrTab.emitWord(SHdr.Addr); |
| 512 | SHdrTab.emitWord(SHdr.Offset); |
| 513 | SHdrTab.emitWord32(SHdr.Size); |
| 514 | SHdrTab.emitWord32(SHdr.Link); |
| 515 | SHdrTab.emitWord32(SHdr.Info); |
| 516 | SHdrTab.emitWord32(SHdr.Align); |
| 517 | SHdrTab.emitWord32(SHdr.EntSize); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 521 | /// EmitSymbolTable - If the current symbol table is non-empty, emit the string |
| 522 | /// table for it and then the symbol table itself. |
| 523 | void ELFWriter::EmitSymbolTable() { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 524 | if (!SymbolList.size()) return; // Empty symbol table. |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 526 | unsigned FirstNonLocalSymbol = 1; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 527 | ELFSection &StrTab = getStringTableSection(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 529 | // Set the zero'th symbol to a null byte, as required. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 530 | StrTab.emitByte(0); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 531 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 532 | // Walk on the symbol list and write symbol names into the |
| 533 | // string table. |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 534 | unsigned Index = 1; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 535 | for (std::list<ELFSym>::iterator I = SymbolList.begin(), |
| 536 | E = SymbolList.end(); I != E; ++I) { |
| 537 | |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 538 | // Use the name mangler to uniquify the LLVM symbol. |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 539 | std::string Name = Mang->getValueName(I->GV); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 540 | |
| 541 | if (Name.empty()) { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 542 | I->NameIdx = 0; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 543 | } else { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 544 | I->NameIdx = Index; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 545 | StrTab.emitString(Name); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 546 | |
| 547 | // Keep track of the number of bytes emitted to this section. |
| 548 | Index += Name.size()+1; |
| 549 | } |
| 550 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 551 | assert(Index == StrTab.size()); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 552 | StrTab.Size = Index; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 553 | |
| 554 | // Now that we have emitted the string table and know the offset into the |
| 555 | // string table of each symbol, emit the symbol table itself. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 556 | ELFSection &SymTab = getSymbolTableSection(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 557 | SymTab.Align = TEW->getPrefELFAlignment(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 558 | SymTab.Link = StrTab.SectionIdx; // Section Index of .strtab. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 559 | |
| 560 | // Size of each symtab entry. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 561 | SymTab.EntSize = TEW->getSymTabEntrySize(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 562 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 563 | // The first entry in the symtab is the null symbol |
| 564 | ELFSym NullSym = ELFSym(0); |
| 565 | EmitSymbol(SymTab, NullSym); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 566 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 567 | // Emit all the symbols to the symbol table. Skip the null |
| 568 | // symbol, cause it's emitted already |
| 569 | Index = 1; |
| 570 | for (std::list<ELFSym>::iterator I = SymbolList.begin(), |
| 571 | E = SymbolList.end(); I != E; ++I, ++Index) { |
| 572 | // Keep track of the first non-local symbol |
| 573 | if (I->getBind() == ELFSym::STB_LOCAL) |
| 574 | FirstNonLocalSymbol++; |
| 575 | |
| 576 | // Emit symbol to the symbol table |
| 577 | EmitSymbol(SymTab, *I); |
| 578 | |
| 579 | // Record the symbol table index for each global value |
| 580 | GblSymLookup[I->GV] = Index; |
| 581 | } |
| 582 | |
| 583 | SymTab.Info = FirstNonLocalSymbol; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 584 | SymTab.Size = SymTab.size(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 587 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 588 | /// ELF Section Table string table: the string table that holds all of the |
| 589 | /// section names. |
| 590 | void ELFWriter::EmitSectionTableStringTable() { |
| 591 | // 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] | 592 | ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 593 | |
| 594 | // Now that we know which section number is the .shstrtab section, update the |
| 595 | // e_shstrndx entry in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 596 | ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 597 | |
| 598 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 599 | // the string table. |
| 600 | unsigned Index = 0; |
| 601 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 602 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 603 | E = SectionList.end(); I != E; ++I) { |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 604 | // Set the index into the table. Note if we have lots of entries with |
| 605 | // common suffixes, we could memoize them here if we cared. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 606 | I->NameIdx = Index; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 607 | SHStrTab.emitString(I->getName()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 608 | |
| 609 | // Keep track of the number of bytes emitted to this section. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 610 | Index += I->getName().size()+1; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // Set the size of .shstrtab now that we know what it is. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 614 | assert(Index == SHStrTab.size()); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 615 | SHStrTab.Size = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 618 | /// OutputSectionsAndSectionTable - Now that we have constructed the file header |
| 619 | /// and all of the sections, emit these to the ostream destination and emit the |
| 620 | /// SectionTable. |
| 621 | void ELFWriter::OutputSectionsAndSectionTable() { |
| 622 | // Pass #1: Compute the file offset for each section. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 623 | size_t FileOff = ElfHdr.size(); // File header first. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 624 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 625 | // Adjust alignment of all section if needed. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 626 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 627 | E = SectionList.end(); I != E; ++I) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 628 | |
| 629 | // Section idx 0 has 0 offset |
| 630 | if (!I->SectionIdx) |
| 631 | continue; |
| 632 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 633 | if (!I->size()) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 634 | I->Offset = FileOff; |
| 635 | continue; |
| 636 | } |
| 637 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 638 | // Update Section size |
| 639 | if (!I->Size) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 640 | I->Size = I->size(); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 642 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 643 | if (I->Align) |
| 644 | FileOff = (FileOff+I->Align-1) & ~(I->Align-1); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 646 | I->Offset = FileOff; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 647 | FileOff += I->Size; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 650 | // Align Section Header. |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame^] | 651 | unsigned TableAlign = TEW->getPrefELFAlignment(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 652 | FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 653 | |
| 654 | // Now that we know where all of the sections will be emitted, set the e_shnum |
| 655 | // entry in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 656 | ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 658 | // Now that we know the offset in the file of the section table, update the |
| 659 | // e_shoff address in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 660 | ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 662 | // Now that we know all of the data in the file header, emit it and all of the |
| 663 | // sections! |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 664 | O.write((char *)&ElfHdr.getData()[0], ElfHdr.size()); |
| 665 | FileOff = ElfHdr.size(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 666 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 667 | // Section Header Table blob |
| 668 | BinaryObject SHdrTable(isLittleEndian, is64Bit); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 669 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 670 | // Emit all of sections to the file and build the section header table. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 671 | while (!SectionList.empty()) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 672 | ELFSection &S = *SectionList.begin(); |
| 673 | DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName() |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 674 | << ", Size: " << S.Size << ", Offset: " << S.Offset |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 675 | << ", SectionData Size: " << S.size() << "\n"; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 676 | |
| 677 | // Align FileOff to whatever the alignment restrictions of the section are. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 678 | if (S.Align) { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 679 | for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 680 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 681 | O << (char)0xAB; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 682 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 683 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 684 | if (S.size()) { |
| 685 | O.write((char *)&S.getData()[0], S.Size); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 686 | FileOff += S.Size; |
| 687 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 688 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 689 | EmitSectionHeader(SHdrTable, S); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 690 | SectionList.pop_front(); |
| 691 | } |
| 692 | |
| 693 | // Align output for the section table. |
| 694 | for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 695 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 696 | O << (char)0xAB; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 697 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 698 | // Emit the section table itself. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 699 | O.write((char *)&SHdrTable.getData()[0], SHdrTable.size()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 700 | } |