1 //------------------------------------------------------------------------------
2 // Copyright (c) 2003, Ingo Weinhold
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
22 // File Name: ElfSymbolPatcher.h
23 // Author: Ingo Weinhold (bonefish@users.sf.net)
24 // Description: Interface declaration of classes used for patching ELF
25 // symbols. Central class is ElfSymbolPatcher. It is a kind of
26 // roster, managing all necessary infos (loaded images) and
27 // being able to fill ElfSymbolPatchInfos with life.
28 // An ElfSymbolPatchInfo represents a symbol and is able to
29 // patch/restore it. An ElfSymbolPatchGroup bundles several
30 // ElfSymbolPatchInfos and can update their data, e.g.
31 // when images are loaded/unloaded. It uses a
32 // ElfSymbolPatcher internally and provides a more convenient
34 //------------------------------------------------------------------------------
36 #ifndef ELF_SYMBOL_PATCHER_H
37 #define ELF_SYMBOL_PATCHER_H
43 namespace SymbolPatcher
{
46 class ElfSymbolPatcher
;
47 class ElfSymbolPatchGroup
;
50 class ElfSymbolPatchInfo
{
53 ~ElfSymbolPatchInfo();
55 status_t
InitCheck() const;
57 const char* GetSymbolName() const;
58 void* GetOriginalAddress() const;
59 image_id
GetOriginalAddressImage() const;
61 status_t
Patch(void* newAddress
);
69 status_t
SetSymbolName(const char* name
);
70 void SetOriginalAddress(void* address
,
72 status_t
CreateEntry(image_id image
, BList
* targets
);
73 bool DeleteEntry(image_id image
);
74 Entry
* EntryAt(int32 index
);
75 Entry
* EntryFor(image_id image
);
78 friend class ElfSymbolPatcher
;
79 friend class ElfSymbolPatchGroup
;
82 void* fOriginalAddress
;
83 image_id fOriginalAddressImage
;
88 class ElfSymbolPatcher
{
96 status_t
InitCheck() const;
97 status_t
Update(UpdateAdapter
* updateAdapter
= NULL
);
100 status_t
GetSymbolPatchInfo(const char* symbolName
,
101 ElfSymbolPatchInfo
* info
);
102 status_t
UpdateSymbolPatchInfo(ElfSymbolPatchInfo
* info
,
109 ElfImage
* _ImageAt(int32 index
) const;
110 ElfImage
* _ImageForID(image_id id
) const;
114 status_t fInitStatus
;
118 class ElfSymbolPatcher::UpdateAdapter
{
121 virtual ~UpdateAdapter();
123 virtual void ImageAdded(ElfImage
* image
);
124 virtual void ImageRemoved(ElfImage
* image
);
128 // ElfSymbolPatchGroup
129 class ElfSymbolPatchGroup
: private ElfSymbolPatcher::UpdateAdapter
{
132 ElfSymbolPatcher
* patcher
= NULL
);
133 ~ElfSymbolPatchGroup();
135 ElfSymbolPatcher
* GetPatcher() const { return fPatcher
; }
137 status_t
AddPatch(const char* symbolName
,
139 void** originalAddress
);
141 void RemoveAllPatches();
149 class PatchInfo
: public ElfSymbolPatchInfo
{
155 virtual void ImageAdded(ElfImage
* image
);
156 virtual void ImageRemoved(ElfImage
* image
);
159 ElfSymbolPatcher
* fPatcher
;
165 } // namespace SymbolPatcher
167 using namespace SymbolPatcher
;
170 #endif // ELF_SYMBOL_PATCHER_H