OLD | NEW |
| (Empty) |
1 // Copyright 2010 Google, Inc. All Rights reserved | |
2 // | |
3 // Redistribution and use in source and binary forms, with or without | |
4 // modification, are permitted provided that the following conditions are | |
5 // met: | |
6 // | |
7 // * Redistributions of source code must retain the above copyright | |
8 // notice, this list of conditions and the following disclaimer. | |
9 // * Redistributions in binary form must reproduce the above | |
10 // copyright notice, this list of conditions and the following disclaimer | |
11 // in the documentation and/or other materials provided with the | |
12 // distribution. | |
13 // * Neither the name of Google Inc. nor the names of its | |
14 // contributors may be used to endorse or promote products derived from | |
15 // this software without specific prior written permission. | |
16 // | |
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 | |
30 // This file defines the ElfObject which decodes the elf data and calls | |
31 // the appropriate reader callbacks. The reader interface allows the | |
32 // libary user to examine the ELF file without ever needing to know | |
33 // machine class (32/64 bit). | |
34 | |
35 #ifndef ELF_READER_ELF_OBJECT_H_ | |
36 #define ELF_READER_ELF_OBJECT_H_ | |
37 | |
38 #include "common/types.h" | |
39 #include "elf_reader/elf_structs.h" | |
40 | |
41 | |
42 namespace elf_reader { | |
43 | |
44 class IElfReader; | |
45 | |
46 // The ElfObject class is used to load and store an ELF based binary. The | |
47 // entire file is read in on a successfull load. During load, precomputed | |
48 // pointers are set to important points in the file. | |
49 class ElfObject { | |
50 public: | |
51 enum ClassSize { | |
52 ELFCLASSERR= 0, | |
53 ELFCLASS32 = 1, | |
54 ELFCLASS64 = 2 | |
55 }; | |
56 | |
57 enum Encoding { | |
58 ELFDATAERR = 0, | |
59 ELFDATA2LSB = 1, | |
60 ELFDATA2MSB = 2 | |
61 }; | |
62 | |
63 enum ObjectType { | |
64 ET_NONE = 0, | |
65 ET_REL = 1, | |
66 ET_EXEC = 2, | |
67 ET_DYN = 3 | |
68 }; | |
69 | |
70 // These pointers are stored as a union since we sometimes need a byte | |
71 // relative offset, and sometimes need to access either 32b or 64b versions | |
72 // of the structure. A call to GetClassSize determines if the 32b or 64b | |
73 // version should be used. These pointers are never provided to the user. | |
74 union ElfHdrPtr { | |
75 uint8_t* raw_; | |
76 ElfHdrDef* def_; | |
77 ElfHdr32* hdr32_; | |
78 ElfHdr64* hdr64_; | |
79 }; | |
80 | |
81 union ElfShdrPtr { | |
82 uint8_t* raw_; | |
83 ElfShdr32* shdr32_; | |
84 ElfShdr64* shdr64_; | |
85 }; | |
86 | |
87 union ElfPhdrPtr { | |
88 uint8_t* raw_; | |
89 ElfPhdr32* phdr32_; | |
90 ElfPhdr64* phdr64_; | |
91 }; | |
92 | |
93 public: | |
94 ElfObject(); | |
95 ~ElfObject(); | |
96 | |
97 // Loading a ELF file will cause the entire file to be placed in memory. A | |
98 // call to Unload will free all data associated with the ELF file. The load | |
99 // function will automatically unload, so it is safe to reuse an ElfObject. | |
100 // The act of loading verifies that the load file containes the expected | |
101 // values and precomputes interesting offsets within the file which are then | |
102 // used by other members. | |
103 bool Load(const char *path); | |
104 void Unload(); | |
105 | |
106 // Retreive information about the loaded file. | |
107 const char *GetPath() const; | |
108 const uint8_t *GetData() const; | |
109 uint64_t GetLength() const; | |
110 | |
111 // Retrieve ELF header information | |
112 ClassSize GetClassSize() const; | |
113 Encoding GetEncoding() const; | |
114 ObjectType GetObjectType() const; | |
115 uint32_t GetObjectFlags() const; | |
116 | |
117 // Process the ELF data, using the reader callbacks | |
118 void Parse(IElfReader* reader) const; | |
119 | |
120 private: | |
121 uint16_t GetProgramHeaderCount() const; | |
122 uint16_t GetSectionHeaderCount() const; | |
123 const char *GetSectionStrings() const; | |
124 | |
125 private: | |
126 char *path_; | |
127 uint64_t length_; | |
128 | |
129 ElfHdrPtr data_; | |
130 ElfObjDef* obj_; | |
131 char *strings_; | |
132 | |
133 ElfShdrPtr shdr_; | |
134 ElfPhdrPtr phdr_; | |
135 }; | |
136 | |
137 } // namespace elf_reader | |
138 | |
139 #endif // ELF_READER_ELF_OBJECT_H_ | |
140 | |
OLD | NEW |