blob: 14a44f0240bff2ec2ef65db7baa44d0be42e4aa6 [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 Lopes04066de2009-06-06 03:56:29 +000019#include "ELF.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include <list>
Dan Gohman249ddbf2008-03-21 23:51:57 +000021#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022
23namespace llvm {
24 class GlobalVariable;
25 class Mangler;
26 class MachineCodeEmitter;
27 class ELFCodeEmitter;
Owen Anderson847b99b2008-08-21 00:14:44 +000028 class raw_ostream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029
30 /// ELFWriter - This class implements the common target-independent code for
31 /// writing ELF files. Targets should derive a class from this to
32 /// parameterize the output format.
33 ///
34 class ELFWriter : public MachineFunctionPass {
35 friend class ELFCodeEmitter;
36 public:
37 static char ID;
38
39 MachineCodeEmitter &getMachineCodeEmitter() const {
40 return *(MachineCodeEmitter*)MCE;
41 }
42
Owen Anderson847b99b2008-08-21 00:14:44 +000043 ELFWriter(raw_ostream &O, TargetMachine &TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 ~ELFWriter();
45
46 typedef std::vector<unsigned char> DataBuffer;
47
48 protected:
49 /// Output stream to send the resultant object file to.
Owen Anderson847b99b2008-08-21 00:14:44 +000050 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 /// Target machine description.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 TargetMachine &TM;
54
55 /// Mang - The object used to perform name mangling for this module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 Mangler *Mang;
57
58 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
59 /// code for functions to the .o file.
60 ELFCodeEmitter *MCE;
61
62 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000064 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66 /// is64Bit/isLittleEndian - This information is inferred from the target
67 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
68 bool is64Bit, isLittleEndian;
69
70 /// doInitialization - Emit the file header and all of the global variables
71 /// for the module to the ELF file.
72 bool doInitialization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 bool runOnMachineFunction(MachineFunction &MF);
74
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 /// doFinalization - Now that the module has been completely processed, emit
76 /// the ELF file to 'O'.
77 bool doFinalization(Module &M);
78
79 private:
80 // The buffer we accumulate the file header into. Note that this should be
81 // changed into something much more efficient later (and the bitcode writer
82 // as well!).
83 DataBuffer FileHeader;
84
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000085 /// ElfHdr - Hold information about the ELF Header
86 ELFHeader *ElfHdr;
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 /// SectionList - This is the list of sections that we have emitted to the
89 /// file. Once the file has been completely built, the section header table
90 /// is constructed from this info.
91 std::list<ELFSection> SectionList;
92 unsigned NumSections; // Always = SectionList.size()
93
94 /// SectionLookup - This is a mapping from section name to section number in
95 /// the SectionList.
96 std::map<std::string, ELFSection*> SectionLookup;
97
98 /// getSection - Return the section with the specified name, creating a new
99 /// section if one does not already exist.
100 ELFSection &getSection(const std::string &Name,
101 unsigned Type, unsigned Flags = 0) {
102 ELFSection *&SN = SectionLookup[Name];
103 if (SN) return *SN;
104
105 SectionList.push_back(Name);
106 SN = &SectionList.back();
107 SN->SectionIdx = NumSections++;
108 SN->Type = Type;
109 SN->Flags = Flags;
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000110 SN->Link = ELFSection::SHN_UNDEF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return *SN;
112 }
113
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000114 ELFSection &getTextSection() {
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000115 return getSection(".text", ELFSection::SHT_PROGBITS,
116 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000117 }
118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 ELFSection &getDataSection() {
120 return getSection(".data", ELFSection::SHT_PROGBITS,
121 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
122 }
123 ELFSection &getBSSSection() {
124 return getSection(".bss", ELFSection::SHT_NOBITS,
125 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
126 }
127
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 /// SymbolTable - This is the list of symbols we have emitted to the file.
129 /// This actually gets rearranged before emission to the file (to put the
130 /// local symbols first in the list).
131 std::vector<ELFSym> SymbolTable;
132
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000133 /// PendingSyms - This is a list of externally defined symbols that we have
134 /// been asked to emit, but have not seen a reference to. When a reference
135 /// is seen, the symbol will move from this list to the SymbolTable.
136 SetVector<GlobalValue*> PendingGlobals;
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 // As we complete the ELF file, we need to update fields in the ELF header
139 // (e.g. the location of the section table). These members keep track of
140 // the offset in ELFHeader of these various pieces to update and other
141 // locations in the file.
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000142 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
143 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
144 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 private:
146 void EmitGlobal(GlobalVariable *GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 void EmitSymbolTable();
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000148 void EmitRelocations();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 void EmitSectionTableStringTable();
150 void OutputSectionsAndSectionTable();
151 };
152}
153
154#endif