blob: 14a44f0240bff2ec2ef65db7baa44d0be42e4aa6 [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 Lopesf5b0c5a2009-06-06 03:56:29 +000019#include "ELF.h"
Chris Lattner5f48ff72005-07-16 08:01:13 +000020#include <list>
Dan Gohmanc9235d22008-03-21 23:51:57 +000021#include <map>
Chris Lattner32018152005-06-27 06:28:45 +000022
23namespace llvm {
24 class GlobalVariable;
Chris Lattner75bbdff2005-07-11 03:11:10 +000025 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000026 class MachineCodeEmitter;
27 class ELFCodeEmitter;
Owen Andersoncb371882008-08-21 00:14:44 +000028 class raw_ostream;
Chris Lattner32018152005-06-27 06:28:45 +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 {
Chris Lattner6871aed2005-07-11 05:15:32 +000035 friend class ELFCodeEmitter;
36 public:
Devang Patel19974732007-05-03 01:11:54 +000037 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000038
Chris Lattner6871aed2005-07-11 05:15:32 +000039 MachineCodeEmitter &getMachineCodeEmitter() const {
40 return *(MachineCodeEmitter*)MCE;
41 }
42
Owen Andersoncb371882008-08-21 00:14:44 +000043 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000044 ~ELFWriter();
45
Chris Lattner5f48ff72005-07-16 08:01:13 +000046 typedef std::vector<unsigned char> DataBuffer;
47
Chris Lattner32018152005-06-27 06:28:45 +000048 protected:
Chris Lattner32018152005-06-27 06:28:45 +000049 /// Output stream to send the resultant object file to.
Owen Andersoncb371882008-08-21 00:14:44 +000050 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000051
52 /// Target machine description.
Chris Lattner32018152005-06-27 06:28:45 +000053 TargetMachine &TM;
54
Chris Lattner75bbdff2005-07-11 03:11:10 +000055 /// Mang - The object used to perform name mangling for this module.
Chris Lattner75bbdff2005-07-11 03:11:10 +000056 Mangler *Mang;
57
Chris Lattner6871aed2005-07-11 05:15:32 +000058 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
59 /// code for functions to the .o file.
60 ELFCodeEmitter *MCE;
61
Chris Lattner32018152005-06-27 06:28:45 +000062 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000063 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000064 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +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);
Chris Lattner32018152005-06-27 06:28:45 +000073 bool runOnMachineFunction(MachineFunction &MF);
74
Chris Lattner32018152005-06-27 06:28:45 +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:
Chris Lattner5f48ff72005-07-16 08:01:13 +000080 // The buffer we accumulate the file header into. Note that this should be
Gabor Greifa99be512007-07-05 17:07:56 +000081 // changed into something much more efficient later (and the bitcode writer
Chris Lattner32018152005-06-27 06:28:45 +000082 // as well!).
Chris Lattner5f48ff72005-07-16 08:01:13 +000083 DataBuffer FileHeader;
Chris Lattner32018152005-06-27 06:28:45 +000084
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000085 /// ElfHdr - Hold information about the ELF Header
86 ELFHeader *ElfHdr;
87
Chris Lattner32018152005-06-27 06:28:45 +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.
Chris Lattner5f48ff72005-07-16 08:01:13 +000091 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.
Chris Lattner5f9cb592005-07-16 17:35:26 +0000100 ELFSection &getSection(const std::string &Name,
101 unsigned Type, unsigned Flags = 0) {
Chris Lattner5f48ff72005-07-16 08:01:13 +0000102 ELFSection *&SN = SectionLookup[Name];
103 if (SN) return *SN;
104
105 SectionList.push_back(Name);
106 SN = &SectionList.back();
107 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000108 SN->Type = Type;
109 SN->Flags = Flags;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000110 SN->Link = ELFSection::SHN_UNDEF;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000111 return *SN;
112 }
Chris Lattner32018152005-06-27 06:28:45 +0000113
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000114 ELFSection &getTextSection() {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000115 return getSection(".text", ELFSection::SHT_PROGBITS,
116 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000117 }
118
Chris Lattner56c32612005-07-16 17:40:34 +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
Chris Lattnere578ab92005-07-07 07:00:37 +0000128 /// SymbolTable - This is the list of symbols we have emitted to the file.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000129 /// This actually gets rearranged before emission to the file (to put the
130 /// local symbols first in the list).
Chris Lattnere578ab92005-07-07 07:00:37 +0000131 std::vector<ELFSym> SymbolTable;
132
Bruno Cardoso Lopesa029a272009-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
Chris Lattner5f48ff72005-07-16 08:01:13 +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 Lopesf5b0c5a2009-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.
Chris Lattner32018152005-06-27 06:28:45 +0000145 private:
Chris Lattner56c32612005-07-16 17:40:34 +0000146 void EmitGlobal(GlobalVariable *GV);
Chris Lattnere578ab92005-07-07 07:00:37 +0000147 void EmitSymbolTable();
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000148 void EmitRelocations();
Chris Lattner32018152005-06-27 06:28:45 +0000149 void EmitSectionTableStringTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000150 void OutputSectionsAndSectionTable();
Chris Lattner32018152005-06-27 06:28:45 +0000151 };
152}
153
154#endif