xref: /haiku/src/kits/debug/Image.h (revision c302a243e15e640fae0f689e32cdf0c18749afee)
1 /*
2  * Copyright 2005-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef IMAGE_H
7 #define IMAGE_H
8 
9 #include <stdio.h>
10 
11 #include <elf_private.h>
12 #include <image.h>
13 #include <OS.h>
14 
15 #include <util/DoublyLinkedList.h>
16 
17 
18 struct image_t;
19 struct runtime_loader_debug_area;
20 
21 
22 namespace BPrivate {
23 namespace Debug {
24 
25 
26 class Image : public DoublyLinkedListLinkImpl<Image> {
27 public:
28 								Image();
29 	virtual						~Image();
30 
Info()31 			const image_info&	Info() const		{ return fInfo; }
ID()32 			image_id			ID() const			{ return fInfo.id; }
Name()33 			const char*			Name() const		{ return fInfo.name; }
TextAddress()34 			addr_t				TextAddress() const
35 				{ return (addr_t)fInfo.text; }
TextSize()36 			size_t				TextSize() const	{ return fInfo.text_size; }
37 
38 	virtual	const elf_sym*		LookupSymbol(addr_t address,
39 									addr_t* _baseAddress,
40 									const char** _symbolName,
41 									size_t *_symbolNameLen,
42 									bool *_exactMatch) const = 0;
43 	virtual	status_t			NextSymbol(int32& iterator,
44 									const char** _symbolName,
45 									size_t* _symbolNameLen,
46 									addr_t* _symbolAddress, size_t* _symbolSize,
47 									int32* _symbolType) const = 0;
48 
49 	virtual	status_t			GetSymbol(const char* name, int32 symbolType,
50 									void** _symbolLocation, size_t* _symbolSize,
51 									int32* _symbolType) const;
52 
53 protected:
54 			image_info			fInfo;
55 };
56 
57 
58 class SymbolTableBasedImage : public Image {
59 public:
60 								SymbolTableBasedImage();
61 	virtual						~SymbolTableBasedImage();
62 
63 	virtual	const elf_sym*		LookupSymbol(addr_t address,
64 									addr_t* _baseAddress,
65 									const char** _symbolName,
66 									size_t *_symbolNameLen,
67 									bool *_exactMatch) const;
68 	virtual	status_t			NextSymbol(int32& iterator,
69 									const char** _symbolName,
70 									size_t* _symbolNameLen,
71 									addr_t* _symbolAddress, size_t* _symbolSize,
72 									int32* _symbolType) const;
73 
74 protected:
75 			size_t				_SymbolNameLen(const char* symbolName) const;
76 
77 protected:
78 			addr_t				fLoadDelta;
79 			elf_sym*			fSymbolTable;
80 			char*				fStringTable;
81 			int32				fSymbolCount;
82 			size_t				fStringTableSize;
83 };
84 
85 
86 class ImageFile : public SymbolTableBasedImage {
87 public:
88 								ImageFile();
89 	virtual						~ImageFile();
90 
91 			status_t			Init(const image_info& info);
92 			status_t			Init(const char* path);
93 
94 private:
95 			status_t			_LoadFile(const char* path,
96 									addr_t* _textAddress, size_t* _textSize,
97 									addr_t* _dataAddress, size_t* _dataSize);
98 
99 			status_t			_FindTableInSection(elf_ehdr* elfHeader,
100 									uint16 sectionType);
101 
102 private:
103 			int					fFD;
104 			off_t				fFileSize;
105 			uint8*				fMappedFile;
106 };
107 
108 
109 class KernelImage : public SymbolTableBasedImage {
110 public:
111 								KernelImage();
112 	virtual						~KernelImage();
113 
114 			status_t			Init(const image_info& info);
115 };
116 
117 
118 class CommPageImage : public SymbolTableBasedImage {
119 public:
120 								CommPageImage();
121 	virtual						~CommPageImage();
122 
123 			status_t			Init(const image_info& info);
124 };
125 
126 }	// namespace Debug
127 }	// namespace BPrivate
128 
129 
130 using BPrivate::Debug::ImageFile;
131 
132 
133 #endif	// IMAGE_H
134