• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- COFF.h - COFF object file implementation -----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the COFFObjectFile class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_OBJECT_COFF_H
15 #define LLVM_OBJECT_COFF_H
16 
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Support/COFF.h"
19 #include "llvm/Support/Endian.h"
20 
21 namespace llvm {
22 namespace object {
23 
24 struct coff_file_header {
25   support::ulittle16_t Machine;
26   support::ulittle16_t NumberOfSections;
27   support::ulittle32_t TimeDateStamp;
28   support::ulittle32_t PointerToSymbolTable;
29   support::ulittle32_t NumberOfSymbols;
30   support::ulittle16_t SizeOfOptionalHeader;
31   support::ulittle16_t Characteristics;
32 };
33 
34 struct coff_symbol {
35   struct StringTableOffset {
36     support::ulittle32_t Zeroes;
37     support::ulittle32_t Offset;
38   };
39 
40   union {
41     char ShortName[8];
42     StringTableOffset Offset;
43   } Name;
44 
45   support::ulittle32_t Value;
46   support::little16_t SectionNumber;
47 
48   struct {
49     support::ulittle8_t BaseType;
50     support::ulittle8_t ComplexType;
51   } Type;
52 
53   support::ulittle8_t  StorageClass;
54   support::ulittle8_t  NumberOfAuxSymbols;
55 };
56 
57 struct coff_section {
58   char Name[8];
59   support::ulittle32_t VirtualSize;
60   support::ulittle32_t VirtualAddress;
61   support::ulittle32_t SizeOfRawData;
62   support::ulittle32_t PointerToRawData;
63   support::ulittle32_t PointerToRelocations;
64   support::ulittle32_t PointerToLinenumbers;
65   support::ulittle16_t NumberOfRelocations;
66   support::ulittle16_t NumberOfLinenumbers;
67   support::ulittle32_t Characteristics;
68 };
69 
70 struct coff_relocation {
71   support::ulittle32_t VirtualAddress;
72   support::ulittle32_t SymbolTableIndex;
73   support::ulittle16_t Type;
74 };
75 
76 class COFFObjectFile : public ObjectFile {
77 private:
78   const coff_file_header *Header;
79   const coff_section     *SectionTable;
80   const coff_symbol      *SymbolTable;
81   const char             *StringTable;
82         uint32_t          StringTableSize;
83 
84         error_code        getSection(int32_t index,
85                                      const coff_section *&Res) const;
86         error_code        getString(uint32_t offset, StringRef &Res) const;
87         error_code        getSymbol(uint32_t index,
88                                     const coff_symbol *&Res) const;
89 
90   const coff_symbol      *toSymb(DataRefImpl Symb) const;
91   const coff_section     *toSec(DataRefImpl Sec) const;
92   const coff_relocation  *toRel(DataRefImpl Rel) const;
93 
94 protected:
95   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
96   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
97   virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
98   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
99   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
100   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
101   virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
102   virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
103   virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
104 
105   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
106   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
107   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
108   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
109   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
110   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
111   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
112   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
113   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
114   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
115                                            bool &Result) const;
116   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
117   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
118 
119   virtual error_code getRelocationNext(DataRefImpl Rel,
120                                        RelocationRef &Res) const;
121   virtual error_code getRelocationAddress(DataRefImpl Rel,
122                                           uint64_t &Res) const;
123   virtual error_code getRelocationSymbol(DataRefImpl Rel,
124                                          SymbolRef &Res) const;
125   virtual error_code getRelocationType(DataRefImpl Rel,
126                                        uint32_t &Res) const;
127   virtual error_code getRelocationTypeName(DataRefImpl Rel,
128                                            SmallVectorImpl<char> &Result) const;
129   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
130                                                  int64_t &Res) const;
131   virtual error_code getRelocationValueString(DataRefImpl Rel,
132                                            SmallVectorImpl<char> &Result) const;
133 
134 public:
135   COFFObjectFile(MemoryBuffer *Object, error_code &ec);
136   virtual symbol_iterator begin_symbols() const;
137   virtual symbol_iterator end_symbols() const;
138   virtual section_iterator begin_sections() const;
139   virtual section_iterator end_sections() const;
140 
141   virtual uint8_t getBytesInAddress() const;
142   virtual StringRef getFileFormatName() const;
143   virtual unsigned getArch() const;
144 };
145 
146 }
147 }
148 
149 #endif
150