blob: 04918f7acbd3afe086e55f5b5088d86365cfd3fe [file] [log] [blame]
Chris Lattner32018152005-06-27 06:28:45 +00001//===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner32018152005-06-27 06:28:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ELFWriter class.
11//
12//===----------------------------------------------------------------------===//
13
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000014#ifndef ELFWRITER_H
15#define ELFWRITER_H
Chris Lattner32018152005-06-27 06:28:45 +000016
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000017#include "llvm/ADT/SetVector.h"
Chris Lattner32018152005-06-27 06:28:45 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopesc997d452009-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 Lopesf5b0c5a2009-06-06 03:56:29 +000022#include "ELF.h"
Chris Lattner5f48ff72005-07-16 08:01:13 +000023#include <list>
Dan Gohmanc9235d22008-03-21 23:51:57 +000024#include <map>
Chris Lattner32018152005-06-27 06:28:45 +000025
26namespace llvm {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000027 class ConstantStruct;
28 class ELFCodeEmitter;
Chris Lattner32018152005-06-27 06:28:45 +000029 class GlobalVariable;
Chris Lattner75bbdff2005-07-11 03:11:10 +000030 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000031 class MachineCodeEmitter;
Owen Andersoncb371882008-08-21 00:14:44 +000032 class raw_ostream;
Chris Lattner32018152005-06-27 06:28:45 +000033
34 /// ELFWriter - This class implements the common target-independent code for
35 /// writing ELF files. Targets should derive a class from this to
36 /// parameterize the output format.
37 ///
38 class ELFWriter : public MachineFunctionPass {
Chris Lattner6871aed2005-07-11 05:15:32 +000039 friend class ELFCodeEmitter;
40 public:
Devang Patel19974732007-05-03 01:11:54 +000041 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000042
Chris Lattner6871aed2005-07-11 05:15:32 +000043 MachineCodeEmitter &getMachineCodeEmitter() const {
44 return *(MachineCodeEmitter*)MCE;
45 }
46
Owen Andersoncb371882008-08-21 00:14:44 +000047 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000048 ~ELFWriter();
49
Chris Lattner5f48ff72005-07-16 08:01:13 +000050 typedef std::vector<unsigned char> DataBuffer;
51
Chris Lattner32018152005-06-27 06:28:45 +000052 protected:
Chris Lattner32018152005-06-27 06:28:45 +000053 /// Output stream to send the resultant object file to.
Owen Andersoncb371882008-08-21 00:14:44 +000054 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000055
56 /// Target machine description.
Chris Lattner32018152005-06-27 06:28:45 +000057 TargetMachine &TM;
58
Chris Lattner75bbdff2005-07-11 03:11:10 +000059 /// Mang - The object used to perform name mangling for this module.
Chris Lattner75bbdff2005-07-11 03:11:10 +000060 Mangler *Mang;
61
Chris Lattner6871aed2005-07-11 05:15:32 +000062 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
63 /// code for functions to the .o file.
64 ELFCodeEmitter *MCE;
65
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000066 /// TAI - Target Asm Info, provide information about section names for
67 /// globals and other target specific stuff.
68 const TargetAsmInfo *TAI;
69
Chris Lattner32018152005-06-27 06:28:45 +000070 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000071 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000072 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000073
74 /// is64Bit/isLittleEndian - This information is inferred from the target
75 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
76 bool is64Bit, isLittleEndian;
77
78 /// doInitialization - Emit the file header and all of the global variables
79 /// for the module to the ELF file.
80 bool doInitialization(Module &M);
Chris Lattner32018152005-06-27 06:28:45 +000081 bool runOnMachineFunction(MachineFunction &MF);
82
Chris Lattner32018152005-06-27 06:28:45 +000083 /// doFinalization - Now that the module has been completely processed, emit
84 /// the ELF file to 'O'.
85 bool doFinalization(Module &M);
86
87 private:
Chris Lattner5f48ff72005-07-16 08:01:13 +000088 // The buffer we accumulate the file header into. Note that this should be
Gabor Greifa99be512007-07-05 17:07:56 +000089 // changed into something much more efficient later (and the bitcode writer
Chris Lattner32018152005-06-27 06:28:45 +000090 // as well!).
Chris Lattner5f48ff72005-07-16 08:01:13 +000091 DataBuffer FileHeader;
Chris Lattner32018152005-06-27 06:28:45 +000092
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000093 /// ElfHdr - Hold information about the ELF Header
94 ELFHeader *ElfHdr;
95
Chris Lattner32018152005-06-27 06:28:45 +000096 /// SectionList - This is the list of sections that we have emitted to the
97 /// file. Once the file has been completely built, the section header table
98 /// is constructed from this info.
Chris Lattner5f48ff72005-07-16 08:01:13 +000099 std::list<ELFSection> SectionList;
100 unsigned NumSections; // Always = SectionList.size()
101
102 /// SectionLookup - This is a mapping from section name to section number in
103 /// the SectionList.
104 std::map<std::string, ELFSection*> SectionLookup;
105
106 /// getSection - Return the section with the specified name, creating a new
107 /// section if one does not already exist.
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000108 ELFSection &getSection(const std::string &Name, unsigned Type,
109 unsigned Flags = 0, unsigned Align = 0) {
Chris Lattner5f48ff72005-07-16 08:01:13 +0000110 ELFSection *&SN = SectionLookup[Name];
111 if (SN) return *SN;
112
113 SectionList.push_back(Name);
114 SN = &SectionList.back();
115 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000116 SN->Type = Type;
117 SN->Flags = Flags;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000118 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000119 SN->Align = Align;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000120 return *SN;
121 }
Chris Lattner32018152005-06-27 06:28:45 +0000122
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000123 ELFSection &getTextSection() {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000124 return getSection(".text", ELFSection::SHT_PROGBITS,
125 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000126 }
127
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000128 ELFSection &getSymbolTableSection() {
129 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
130 }
131
132 ELFSection &getStringTableSection() {
133 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
134 }
135
Chris Lattner56c32612005-07-16 17:40:34 +0000136 ELFSection &getDataSection() {
137 return getSection(".data", ELFSection::SHT_PROGBITS,
138 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
139 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000140
Chris Lattner56c32612005-07-16 17:40:34 +0000141 ELFSection &getBSSSection() {
142 return getSection(".bss", ELFSection::SHT_NOBITS,
143 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
144 }
145
Chris Lattnere578ab92005-07-07 07:00:37 +0000146 /// SymbolTable - This is the list of symbols we have emitted to the file.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000147 /// This actually gets rearranged before emission to the file (to put the
148 /// local symbols first in the list).
Chris Lattnere578ab92005-07-07 07:00:37 +0000149 std::vector<ELFSym> SymbolTable;
150
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000151 /// PendingSyms - This is a list of externally defined symbols that we have
152 /// been asked to emit, but have not seen a reference to. When a reference
153 /// is seen, the symbol will move from this list to the SymbolTable.
154 SetVector<GlobalValue*> PendingGlobals;
155
Chris Lattner5f48ff72005-07-16 08:01:13 +0000156 // As we complete the ELF file, we need to update fields in the ELF header
157 // (e.g. the location of the section table). These members keep track of
158 // the offset in ELFHeader of these various pieces to update and other
159 // locations in the file.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000160 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
161 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
162 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Chris Lattner32018152005-06-27 06:28:45 +0000163 private:
Chris Lattner56c32612005-07-16 17:40:34 +0000164 void EmitGlobal(GlobalVariable *GV);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000165 void EmitGlobalConstant(const Constant *C, OutputBuffer &GblCstTab);
166 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
167 OutputBuffer &GblCstTab);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000168 void EmitRelocations();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000169 void EmitSectionHeader(OutputBuffer &TableOut, const ELFSection &Section);
Chris Lattner32018152005-06-27 06:28:45 +0000170 void EmitSectionTableStringTable();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000171 void EmitSymbol(OutputBuffer &SymTabOut, ELFSym &Sym);
172 void EmitSymbolTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000173 void OutputSectionsAndSectionTable();
Chris Lattner32018152005-06-27 06:28:45 +0000174 };
175}
176
177#endif