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