blob: 8a380f03401035365a78cf04bd2dd0abe6bcb04f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ELFWriter class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ELFWRITER_H
15#define ELFWRITER_H
16
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000017#include "llvm/ADT/SetVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000019#include "llvm/Support/OutputBuffer.h"
20#include "llvm/Target/TargetAsmInfo.h"
21#include "llvm/Target/TargetELFWriterInfo.h"
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +000022#include "ELF.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include <list>
Dan Gohman249ddbf2008-03-21 23:51:57 +000024#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025
26namespace llvm {
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000027 class BinaryObject;
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000028 class ConstantStruct;
29 class ELFCodeEmitter;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 class GlobalVariable;
31 class Mangler;
32 class MachineCodeEmitter;
Owen Anderson847b99b2008-08-21 00:14:44 +000033 class raw_ostream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034
35 /// ELFWriter - This class implements the common target-independent code for
36 /// writing ELF files. Targets should derive a class from this to
37 /// parameterize the output format.
38 ///
39 class ELFWriter : public MachineFunctionPass {
40 friend class ELFCodeEmitter;
41 public:
42 static char ID;
43
44 MachineCodeEmitter &getMachineCodeEmitter() const {
45 return *(MachineCodeEmitter*)MCE;
46 }
47
Owen Anderson847b99b2008-08-21 00:14:44 +000048 ELFWriter(raw_ostream &O, TargetMachine &TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 ~ELFWriter();
50
51 typedef std::vector<unsigned char> DataBuffer;
52
53 protected:
54 /// Output stream to send the resultant object file to.
Owen Anderson847b99b2008-08-21 00:14:44 +000055 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
57 /// Target machine description.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 TargetMachine &TM;
59
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000060 /// Target Elf Writer description.
61 const TargetELFWriterInfo *TEW;
62
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 /// Mang - The object used to perform name mangling for this module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 Mangler *Mang;
65
66 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
67 /// code for functions to the .o file.
68 ELFCodeEmitter *MCE;
69
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000070 /// TAI - Target Asm Info, provide information about section names for
71 /// globals and other target specific stuff.
72 const TargetAsmInfo *TAI;
73
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000076 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
78 /// is64Bit/isLittleEndian - This information is inferred from the target
79 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
80 bool is64Bit, isLittleEndian;
81
82 /// doInitialization - Emit the file header and all of the global variables
83 /// for the module to the ELF file.
84 bool doInitialization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 bool runOnMachineFunction(MachineFunction &MF);
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 /// doFinalization - Now that the module has been completely processed, emit
88 /// the ELF file to 'O'.
89 bool doFinalization(Module &M);
90
91 private:
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000092 // Blob containing the Elf header
93 BinaryObject ElfHdr;
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000094
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 /// SectionList - This is the list of sections that we have emitted to the
96 /// file. Once the file has been completely built, the section header table
97 /// is constructed from this info.
98 std::list<ELFSection> SectionList;
99 unsigned NumSections; // Always = SectionList.size()
100
101 /// SectionLookup - This is a mapping from section name to section number in
102 /// the SectionList.
103 std::map<std::string, ELFSection*> SectionLookup;
104
105 /// getSection - Return the section with the specified name, creating a new
106 /// section if one does not already exist.
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000107 ELFSection &getSection(const std::string &Name, unsigned Type,
108 unsigned Flags = 0, unsigned Align = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 ELFSection *&SN = SectionLookup[Name];
110 if (SN) return *SN;
111
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000112 SectionList.push_back(ELFSection(Name, isLittleEndian, is64Bit));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 SN = &SectionList.back();
114 SN->SectionIdx = NumSections++;
115 SN->Type = Type;
116 SN->Flags = Flags;
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000117 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000118 SN->Align = Align;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 return *SN;
120 }
121
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000122 ELFSection &getTextSection() {
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000123 return getSection(".text", ELFSection::SHT_PROGBITS,
124 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000125 }
126
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000127 ELFSection &getNonExecStackSection() {
128 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
129 }
130
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000131 ELFSection &getSymbolTableSection() {
132 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
133 }
134
135 ELFSection &getStringTableSection() {
136 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
137 }
138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 ELFSection &getDataSection() {
140 return getSection(".data", ELFSection::SHT_PROGBITS,
141 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
142 }
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 ELFSection &getBSSSection() {
145 return getSection(".bss", ELFSection::SHT_NOBITS,
146 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
147 }
148
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000149 /// SymbolList - This is the list of symbols we have emitted to the file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 /// This actually gets rearranged before emission to the file (to put the
151 /// local symbols first in the list).
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000152 std::vector<ELFSym> SymbolList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000154 /// PendingGlobals - List of externally defined symbols that we have been
155 /// asked to emit, but have not seen a reference to. When a reference
156 /// is seen, the symbol will move from this list to the SymbolList.
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000157 SetVector<GlobalValue*> PendingGlobals;
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 // As we complete the ELF file, we need to update fields in the ELF header
160 // (e.g. the location of the section table). These members keep track of
161 // the offset in ELFHeader of these various pieces to update and other
162 // locations in the file.
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000163 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
164 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
165 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 private:
168 void EmitGlobal(GlobalVariable *GV);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000169 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000170 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000171 ELFSection &GblS);
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000172 void EmitRelocations();
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000173 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 void EmitSectionTableStringTable();
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000175 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000176 void EmitSymbolTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 void OutputSectionsAndSectionTable();
178 };
179}
180
181#endif