OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file defines the ElfObject which decodes the elf data and calls | |
6 // the appropriate reader callbacks. The reader interface allows the | |
7 // libary user to examine the ELF file without ever needing to know | |
8 // machine class (32/64 bit). | |
9 | |
10 #ifndef ELF_READER_ELF_OBJECT_H_ | |
11 #define ELF_READER_ELF_OBJECT_H_ | |
12 | |
13 #include <stdio.h> | |
14 | |
15 #include "common/types.h" | |
16 #include "elf_reader/elf_structs.h" | |
17 | |
18 | |
19 namespace elf_reader { | |
20 | |
21 class IElfReader; | |
22 | |
23 // The ElfObject class is used to load and store an ELF based binary. The | |
24 // entire file is read in on a successfull load. During load, precomputed | |
25 // pointers are set to important points in the file. | |
26 class ElfObject { | |
27 public: | |
28 enum ClassSize { | |
29 ELFCLASSERR= 0, | |
30 ELFCLASS32 = 1, | |
31 ELFCLASS64 = 2 | |
32 }; | |
33 | |
34 enum Encoding { | |
35 ELFDATAERR = 0, | |
36 ELFDATA2LSB = 1, | |
37 ELFDATA2MSB = 2 | |
38 }; | |
39 | |
40 enum ObjectType { | |
41 ET_NONE = 0, | |
42 ET_REL = 1, | |
43 ET_EXEC = 2, | |
44 ET_DYN = 3 | |
45 }; | |
46 | |
47 // These pointers are stored as a union since we sometimes need a byte | |
48 // relative offset, and sometimes need to access either 32b or 64b versions | |
49 // of the structure. A call to GetClassSize determines if the 32b or 64b | |
50 // version should be used. These pointers are never provided to the user. | |
51 union ElfHdrPtr { | |
52 uint8_t* raw_; | |
53 ElfHdrDef* def_; | |
54 ElfHdr32* hdr32_; | |
55 ElfHdr64* hdr64_; | |
56 }; | |
57 | |
58 union ElfShdrPtr { | |
59 uint8_t* raw_; | |
60 ElfShdr32* shdr32_; | |
61 ElfShdr64* shdr64_; | |
62 }; | |
63 | |
64 union ElfPhdrPtr { | |
65 uint8_t* raw_; | |
66 ElfPhdr32* phdr32_; | |
67 ElfPhdr64* phdr64_; | |
68 }; | |
69 | |
70 public: | |
71 ElfObject(); | |
72 ~ElfObject(); | |
73 | |
74 // Loading a ELF file will cause the entire file to be placed in memory. A | |
75 // call to Unload will free all data associated with the ELF file. The load | |
76 // function will automatically unload, so it is safe to reuse an ElfObject. | |
77 // The act of loading verifies that the load file containes the expected | |
78 // values and precomputes interesting offsets within the file which are then | |
79 // used by other members. | |
80 bool Load(const char *path); | |
81 void Unload(); | |
82 void CleanUp(FILE * fp); | |
83 | |
84 // Retrieve information about the loaded file. | |
85 const char *GetPath() const; | |
86 const uint8_t *GetData() const; | |
87 uint64_t GetLength() const; | |
88 | |
89 // Retrieve ELF header information | |
90 ClassSize GetClassSize() const; | |
91 Encoding GetEncoding() const; | |
92 ObjectType GetObjectType() const; | |
93 uint32_t GetObjectFlags() const; | |
94 | |
95 // Process the ELF data, using the reader callbacks | |
96 void Parse(IElfReader* reader) const; | |
97 | |
98 private: | |
99 uint16_t GetProgramHeaderCount() const; | |
100 uint16_t GetSectionHeaderCount() const; | |
101 const char *GetSectionStrings() const; | |
102 | |
103 char *path_; | |
104 uint64_t length_; | |
105 | |
106 ElfHdrPtr data_; | |
107 ElfObjDef* obj_; | |
108 char *strings_; | |
109 | |
110 ElfShdrPtr shdr_; | |
111 ElfPhdrPtr phdr_; | |
112 }; | |
113 | |
114 } // namespace elf_reader | |
115 | |
116 #endif // ELF_READER_ELF_OBJECT_H_ | |
117 | |
OLD | NEW |