blob: 0389185f1db4f7cbc316d0b6c17e5d5d99c45a3b [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
17#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000018#include "ELF.h"
Chris Lattner5f48ff72005-07-16 08:01:13 +000019#include <list>
Dan Gohmanc9235d22008-03-21 23:51:57 +000020#include <map>
Chris Lattner32018152005-06-27 06:28:45 +000021
22namespace llvm {
23 class GlobalVariable;
Chris Lattner75bbdff2005-07-11 03:11:10 +000024 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000025 class MachineCodeEmitter;
26 class ELFCodeEmitter;
Owen Andersoncb371882008-08-21 00:14:44 +000027 class raw_ostream;
Chris Lattner32018152005-06-27 06:28:45 +000028
29 /// ELFWriter - This class implements the common target-independent code for
30 /// writing ELF files. Targets should derive a class from this to
31 /// parameterize the output format.
32 ///
33 class ELFWriter : public MachineFunctionPass {
Chris Lattner6871aed2005-07-11 05:15:32 +000034 friend class ELFCodeEmitter;
35 public:
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000037
Chris Lattner6871aed2005-07-11 05:15:32 +000038 MachineCodeEmitter &getMachineCodeEmitter() const {
39 return *(MachineCodeEmitter*)MCE;
40 }
41
Owen Andersoncb371882008-08-21 00:14:44 +000042 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000043 ~ELFWriter();
44
Chris Lattner5f48ff72005-07-16 08:01:13 +000045 typedef std::vector<unsigned char> DataBuffer;
46
Chris Lattner32018152005-06-27 06:28:45 +000047 protected:
Chris Lattner32018152005-06-27 06:28:45 +000048 /// Output stream to send the resultant object file to.
49 ///
Owen Andersoncb371882008-08-21 00:14:44 +000050 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000051
52 /// Target machine description.
53 ///
54 TargetMachine &TM;
55
Chris Lattner75bbdff2005-07-11 03:11:10 +000056 /// Mang - The object used to perform name mangling for this module.
57 ///
58 Mangler *Mang;
59
Chris Lattner6871aed2005-07-11 05:15:32 +000060 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
61 /// code for functions to the .o file.
62 ELFCodeEmitter *MCE;
63
Chris Lattner32018152005-06-27 06:28:45 +000064 //===------------------------------------------------------------------===//
65 // Properties to be set by the derived class ctor, used to configure the
66 // ELFWriter.
67
68 // e_machine - This field is the target specific value to emit as the
69 // e_machine member of the ELF header.
70 unsigned short e_machine;
71
72 // e_flags - The machine flags for the target. This defaults to zero.
73 unsigned e_flags;
74
75 //===------------------------------------------------------------------===//
76 // Properties inferred automatically from the target machine.
77 //
78
79 /// is64Bit/isLittleEndian - This information is inferred from the target
80 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
81 bool is64Bit, isLittleEndian;
82
83 /// doInitialization - Emit the file header and all of the global variables
84 /// for the module to the ELF file.
85 bool doInitialization(Module &M);
Chris Lattner32018152005-06-27 06:28:45 +000086 bool runOnMachineFunction(MachineFunction &MF);
87
Chris Lattner32018152005-06-27 06:28:45 +000088 /// doFinalization - Now that the module has been completely processed, emit
89 /// the ELF file to 'O'.
90 bool doFinalization(Module &M);
91
92 private:
Chris Lattner5f48ff72005-07-16 08:01:13 +000093 // The buffer we accumulate the file header into. Note that this should be
Gabor Greifa99be512007-07-05 17:07:56 +000094 // changed into something much more efficient later (and the bitcode writer
Chris Lattner32018152005-06-27 06:28:45 +000095 // as well!).
Chris Lattner5f48ff72005-07-16 08:01:13 +000096 DataBuffer FileHeader;
Chris Lattner32018152005-06-27 06:28:45 +000097
Chris Lattner32018152005-06-27 06:28:45 +000098 /// SectionList - This is the list of sections that we have emitted to the
99 /// file. Once the file has been completely built, the section header table
100 /// is constructed from this info.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000101 std::list<ELFSection> SectionList;
102 unsigned NumSections; // Always = SectionList.size()
103
104 /// SectionLookup - This is a mapping from section name to section number in
105 /// the SectionList.
106 std::map<std::string, ELFSection*> SectionLookup;
107
108 /// getSection - Return the section with the specified name, creating a new
109 /// section if one does not already exist.
Chris Lattner5f9cb592005-07-16 17:35:26 +0000110 ELFSection &getSection(const std::string &Name,
111 unsigned Type, unsigned Flags = 0) {
Chris Lattner5f48ff72005-07-16 08:01:13 +0000112 ELFSection *&SN = SectionLookup[Name];
113 if (SN) return *SN;
114
115 SectionList.push_back(Name);
116 SN = &SectionList.back();
117 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000118 SN->Type = Type;
119 SN->Flags = Flags;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000120 SN->Link = ELFSection::SHN_UNDEF;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000121 return *SN;
122 }
Chris Lattner32018152005-06-27 06:28:45 +0000123
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000124 ELFSection &getTextSection() {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000125 return getSection(".text", ELFSection::SHT_PROGBITS,
126 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000127 }
128
Chris Lattner56c32612005-07-16 17:40:34 +0000129 ELFSection &getDataSection() {
130 return getSection(".data", ELFSection::SHT_PROGBITS,
131 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
132 }
133 ELFSection &getBSSSection() {
134 return getSection(".bss", ELFSection::SHT_NOBITS,
135 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
136 }
137
Chris Lattnere578ab92005-07-07 07:00:37 +0000138 /// SymbolTable - This is the list of symbols we have emitted to the file.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000139 /// This actually gets rearranged before emission to the file (to put the
140 /// local symbols first in the list).
Chris Lattnere578ab92005-07-07 07:00:37 +0000141 std::vector<ELFSym> SymbolTable;
142
Chris Lattner5f48ff72005-07-16 08:01:13 +0000143 // As we complete the ELF file, we need to update fields in the ELF header
144 // (e.g. the location of the section table). These members keep track of
145 // the offset in ELFHeader of these various pieces to update and other
146 // locations in the file.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000147 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
148 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
149 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Chris Lattner32018152005-06-27 06:28:45 +0000150 private:
Chris Lattner56c32612005-07-16 17:40:34 +0000151 void EmitGlobal(GlobalVariable *GV);
Chris Lattnere578ab92005-07-07 07:00:37 +0000152
153 void EmitSymbolTable();
Chris Lattner32018152005-06-27 06:28:45 +0000154
155 void EmitSectionTableStringTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000156 void OutputSectionsAndSectionTable();
Chris Lattner32018152005-06-27 06:28:45 +0000157 };
158}
159
160#endif