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