1 //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the target-independent ELF writer. This file writes out
11 // the ELF file in the following order:
14 // #2. '.data' section
17 // #X. '.shstrtab' section
20 // The entries in the section table are laid out as:
21 // #0. Null entry [required]
22 // #1. ".data" entry - global variables with initializers. [ if needed ]
23 // #2. ".bss" entry - global variables without initializers. [ if needed ]
24 // #3. ".text" entry - the program code
26 // #N. ".shstrtab" entry - String table for the section names.
29 // NOTE: This code should eventually be extended to support 64-bit ELF (this
30 // won't be hard), but we haven't done so yet!
32 //===----------------------------------------------------------------------===//
34 #include "llvm/CodeGen/ELFWriter.h"
35 #include "llvm/Module.h"
36 #include "llvm/Target/TargetMachine.h"
39 ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
40 e_machine = 0; // e_machine defaults to 'No Machine'
41 e_flags = 0; // e_flags defaults to 0, no flags.
43 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
44 isLittleEndian = TM.getTargetData().isLittleEndian();
47 // doInitialization - Emit the file header and all of the global variables for
48 // the module to the ELF file.
49 bool ELFWriter::doInitialization(Module &M) {
50 outbyte(0x7F); // EI_MAG0
51 outbyte('E'); // EI_MAG1
52 outbyte('L'); // EI_MAG2
53 outbyte('F'); // EI_MAG3
54 outbyte(is64Bit ? 2 : 1); // EI_CLASS
55 outbyte(isLittleEndian ? 1 : 2); // EI_DATA
56 outbyte(1); // EI_VERSION
57 for (unsigned i = OutputBuffer.size(); i != 16; ++i)
58 outbyte(0); // EI_PAD up to 16 bytes.
60 // This should change for shared objects.
61 outhalf(1); // e_type = ET_REL
62 outhalf(e_machine); // e_machine = whatever the target wants
63 outword(1); // e_version = 1
64 outaddr(0); // e_entry = 0 -> no entry point in .o file
65 outaddr(0); // e_phoff = 0 -> no program header for .o
67 ELFHeader_e_shoff_Offset = OutputBuffer.size();
68 outaddr(0); // e_shoff
69 outword(e_flags); // e_flags = whatever the target wants
71 assert(!is64Bit && "These sizes need to be adjusted for 64-bit!");
72 outhalf(52); // e_ehsize = ELF header size
73 outhalf(0); // e_phentsize = prog header entry size
74 outhalf(0); // e_phnum = # prog header entries = 0
75 outhalf(40); // e_shentsize = sect header entry size
78 ELFHeader_e_shnum_Offset = OutputBuffer.size();
79 outhalf(0); // e_shnum = # of section header ents
80 ELFHeader_e_shstrndx_Offset = OutputBuffer.size();
81 outhalf(0); // e_shstrndx = Section # of '.shstrtab'
83 // Add the null section.
84 SectionList.push_back(ELFSection());
86 // Okay, the ELF header has been completed, emit the .data section next.
87 ELFSection DataSection(".data", OutputBuffer.size());
88 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
90 EmitDATASectionGlobal(I);
92 // If the .data section is nonempty, add it to our list.
93 if ((DataSection.Size = OutputBuffer.size()-DataSection.Offset)) {
94 DataSection.Align = 4; // FIXME: Compute!
95 SectionList.push_back(DataSection);
98 // Okay, emit the .bss section next.
99 ELFSection BSSSection(".bss", OutputBuffer.size());
100 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
102 EmitBSSSectionGlobal(I);
104 // If the .bss section is nonempty, add it to our list.
105 if ((BSSSection.Size = OutputBuffer.size()-BSSSection.Offset)) {
106 BSSSection.Align = 4; // FIXME: Compute!
107 SectionList.push_back(BSSSection);
113 // isCOMM - A global variable should be emitted to the common area if it is zero
114 // initialized and has linkage that permits it to be merged with other globals.
115 static bool isCOMM(GlobalVariable *GV) {
116 return GV->getInitializer()->isNullValue() &&
117 (GV->hasLinkOnceLinkage() || GV->hasInternalLinkage() ||
118 GV->hasWeakLinkage());
121 // EmitDATASectionGlobal - Emit a global variable to the .data section if it
123 void ELFWriter::EmitDATASectionGlobal(GlobalVariable *GV) {
124 if (!GV->hasInitializer()) return;
126 // Do not emit a symbol here if it should be emitted to the common area.
127 if (isCOMM(GV)) return;
132 void ELFWriter::EmitBSSSectionGlobal(GlobalVariable *GV) {
133 if (!GV->hasInitializer()) return;
135 // FIXME: We don't support BSS yet!
141 void ELFWriter::EmitGlobal(GlobalVariable *GV) {
145 bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
149 /// doFinalization - Now that the module has been completely processed, emit
150 /// the ELF file to 'O'.
151 bool ELFWriter::doFinalization(Module &M) {
152 // Emit the string table for the sections in the ELF file we have.
153 EmitSectionTableStringTable();
155 // Emit the .o file section table.
158 // Emit the .o file to the specified stream.
159 O.write((char*)&OutputBuffer[0], OutputBuffer.size());
161 // Free the output buffer.
162 std::vector<unsigned char>().swap(OutputBuffer);
166 /// EmitSectionTableStringTable - This method adds and emits a section for the
167 /// ELF Section Table string table: the string table that holds all of the
169 void ELFWriter::EmitSectionTableStringTable() {
170 // First step: add the section for the string table to the list of sections:
171 SectionList.push_back(ELFSection(".shstrtab", OutputBuffer.size()));
172 SectionList.back().Type = 3; // SHT_STRTAB
174 // Now that we know which section number is the .shstrtab section, update the
175 // e_shstrndx entry in the ELF header.
176 fixhalf(SectionList.size()-1, ELFHeader_e_shstrndx_Offset);
178 // Set the NameIdx of each section in the string table and emit the bytes for
182 for (unsigned i = 0, e = SectionList.size(); i != e; ++i) {
183 // Set the index into the table. Note if we have lots of entries with
184 // common suffixes, we could memoize them here if we cared.
185 SectionList[i].NameIdx = Index;
187 // Add the name to the output buffer, including the null terminator.
188 OutputBuffer.insert(OutputBuffer.end(), SectionList[i].Name.begin(),
189 SectionList[i].Name.end());
190 // Add a null terminator.
191 OutputBuffer.push_back(0);
193 // Keep track of the number of bytes emitted to this section.
194 Index += SectionList[i].Name.size()+1;
197 // Set the size of .shstrtab now that we know what it is.
198 SectionList.back().Size = Index;
201 /// EmitSectionTable - Now that we have emitted the entire contents of the file
202 /// (all of the sections), emit the section table which informs the reader where
203 /// the boundaries are.
204 void ELFWriter::EmitSectionTable() {
205 // Now that all of the sections have been emitted, set the e_shnum entry in
207 fixhalf(SectionList.size(), ELFHeader_e_shnum_Offset);
209 // Now that we know the offset in the file of the section table (which we emit
210 // next), update the e_shoff address in the ELF header.
211 fixaddr(OutputBuffer.size(), ELFHeader_e_shoff_Offset);
213 // Emit all of the section table entries.
214 for (unsigned i = 0, e = SectionList.size(); i != e; ++i) {
215 const ELFSection &S = SectionList[i];
216 outword(S.NameIdx); // sh_name - Symbol table name idx
217 outword(S.Type); // sh_type - Section contents & semantics
218 outword(S.Flags); // sh_flags - Section flags.
219 outaddr(S.Addr); // sh_addr - The mem address this section appears in.
220 outaddr(S.Offset); // sh_offset - The offset from the start of the file.
221 outword(S.Size); // sh_size - The section size.
222 outword(S.Link); // sh_link - Section header table index link.
223 outword(S.Info); // sh_info - Auxillary information.
224 outword(S.Align); // sh_addralign - Alignment of section.
225 outword(S.EntSize); // sh_entsize - Size of each entry in the section.
228 // Release the memory allocated for the section list.
229 std::vector<ELFSection>().swap(SectionList);