xref: /haiku/src/tools/elfsymbolpatcher/ElfFile.h (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2003, Ingo Weinhold
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		ElfFile.h
23 //	Author:			Ingo Weinhold (bonefish@users.sf.net)
24 //	Description:	Interface declarations of classes for accessing ELF file,
25 //					or more precisely for iterating through their relocation
26 //					sections.
27 //------------------------------------------------------------------------------
28 
29 #ifndef ELF_FILE_H
30 #define ELF_FILE_H
31 
32 #include <File.h>
33 #include <image.h>
34 
35 #include "Elf.h"
36 
37 class ElfFile;
38 class ElfSection;
39 
40 // ElfSymbol
41 class ElfSymbol {
42 public:
43 								ElfSymbol(ElfSection* section = NULL,
44 										  int32 index = -1);
45 								~ElfSymbol();
46 
47 			void				SetTo(ElfSection* section, int32 index);
48 			void				Unset();
49 
50 			const Elf32_Sym*	GetSymbolStruct();
51 			const char*			GetName();
52 			uint32				GetBinding();
53 			uint32				GetType();
54 			uint32				GetTargetSectionIndex();
55 
56 private:
57 			ElfSection*			fSection;
58 			int32				fIndex;
59 			Elf32_Sym*			fSymbol;
60 };
61 
62 // ElfRelocation
63 class ElfRelocation {
64 public:
65 								ElfRelocation(ElfSection* section = NULL,
66 											  int32 index = -1);
67 								~ElfRelocation();
68 
69 			void				SetTo(ElfSection* section, int32 index);
70 			void				Unset();
71 
72 			Elf32_Rel*			GetRelocationStruct();
73 			uint32				GetType();
74 			uint32				GetSymbolIndex();
75 			Elf32_Addr			GetOffset();
76 			status_t			GetSymbol(ElfSymbol* symbol);
77 
78 private:
79 			ElfSection*			fSection;
80 			int32				fIndex;
81 			Elf32_Rel*			fRelocation;
82 };
83 
84 // ElfRelocationIterator
85 class ElfRelocationIterator {
86 public:
87 								ElfRelocationIterator(ElfFile* file);
88 								~ElfRelocationIterator();
89 
90 			bool				GetNext(ElfRelocation* relocation);
91 
92 private:
93 			ElfSection*			_FindNextSection();
94 
95 private:
96 			ElfFile*			fFile;
97 			int32				fSectionIndex;
98 			int32				fEntryIndex;
99 };
100 
101 // ElfFile
102 class ElfFile {
103 public:
104 								ElfFile();
105 								~ElfFile();
106 			status_t			SetTo(const char *filename);
107 			void				Unset();
108 			void				Unload();
109 
110 			BFile*				GetFile()	{ return &fFile; }
111 
112 			const char*			GetSectionHeaderStrings(size_t* size);
113 			const char*			GetStringSectionStrings(int32 index,
114 														size_t* size);
115 
116 			int32				CountSections() const { return fSectionCount; }
117 			ElfSection*			SectionAt(int32 index, bool load = false);
118 
119 			void				Dump();
120 
121 private:
122 			status_t			_SetTo(const char *filename);
123 
124 			Elf32_Shdr*			_SectionHeaderAt(int32 index);
125 
126 			status_t			_LoadSection(int32 index);
127 
128 private:
129 			BFile				fFile;
130 			Elf32_Ehdr			fHeader;
131 			uint8*				fSectionHeaders;
132 			ElfSection*			fSections;
133 			int32				fSectionCount;
134 			size_t				fSectionHeaderSize;
135 };
136 
137 #endif	// ELF_FILE_H
138