Bring back ext2 code from branch
[reactos.git] / reactos / lib / rossym / find.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/rossym/find.c
5 * PURPOSE: Find symbol info for an address
6 *
7 * PROGRAMMERS: Ge van Geldorp (gvg@reactos.com)
8 */
9 /*
10 * Parts of this file based on work Copyright (c) 1990, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include <ntddk.h>
39 #include <reactos/rossym.h>
40 #include "rossympriv.h"
41
42 #define NDEBUG
43 #include <debug.h>
44
45 static PROSSYM_ENTRY
46 FindEntry(IN PROSSYM_INFO RosSymInfo, IN ULONG_PTR RelativeAddress)
47 {
48 /*
49 * Perform a binary search.
50 *
51 * The code below is a bit sneaky. After a comparison fails, we
52 * divide the work in half by moving either left or right. If lim
53 * is odd, moving left simply involves halving lim: e.g., when lim
54 * is 5 we look at item 2, so we change lim to 2 so that we will
55 * look at items 0 & 1. If lim is even, the same applies. If lim
56 * is odd, moving right again involes halving lim, this time moving
57 * the base up one item past p: e.g., when lim is 5 we change base
58 * to item 3 and make lim 2 so that we will look at items 3 and 4.
59 * If lim is even, however, we have to shrink it by one before
60 * halving: e.g., when lim is 4, we still looked at item 2, so we
61 * have to make lim 3, then halve, obtaining 1, so that we will only
62 * look at item 3.
63 */
64 PROSSYM_ENTRY Base = RosSymInfo->Symbols;
65 ULONG Lim;
66 PROSSYM_ENTRY Mid, Low;
67
68 if (RelativeAddress < Base->Address)
69 {
70 return NULL;
71 }
72
73 Low = Base;
74 for (Lim = RosSymInfo->SymbolsCount; Lim != 0; Lim >>= 1)
75 {
76 Mid = Base + (Lim >> 1);
77 if (RelativeAddress == Mid->Address)
78 {
79 return Mid;
80 }
81 if (Mid->Address < RelativeAddress) /* key > mid: move right */
82 {
83 Low = Mid;
84 Base = Mid + 1;
85 Lim--;
86 } /* else move left */
87 }
88
89 return Low;
90 }
91
92
93 BOOLEAN
94 RosSymGetAddressInformation(PROSSYM_INFO RosSymInfo,
95 ULONG_PTR RelativeAddress,
96 ULONG *LineNumber,
97 char *FileName,
98 char *FunctionName)
99 {
100 PROSSYM_ENTRY RosSymEntry;
101
102 DPRINT("RelativeAddress = 0x%08x\n", RelativeAddress);
103
104 if (RosSymInfo->Symbols == NULL || RosSymInfo->SymbolsCount == 0 ||
105 RosSymInfo->Strings == NULL || RosSymInfo->StringsLength == 0)
106 {
107 DPRINT1("Uninitialized RosSymInfo\n");
108 return FALSE;
109 }
110
111 ASSERT(LineNumber || FileName || FunctionName);
112
113 /* find symbol entry for function */
114 RosSymEntry = FindEntry(RosSymInfo, RelativeAddress);
115
116 if (NULL == RosSymEntry)
117 {
118 DPRINT("None of the requested information was found!\n");
119 return FALSE;
120 }
121
122 if (LineNumber != NULL)
123 {
124 *LineNumber = RosSymEntry->SourceLine;
125 }
126 if (FileName != NULL)
127 {
128 PCSTR Name = "";
129 if (RosSymEntry->FileOffset != 0)
130 {
131 Name = (PCHAR) RosSymInfo->Strings + RosSymEntry->FileOffset;
132 }
133 strcpy(FileName, Name);
134 }
135 if (FunctionName != NULL)
136 {
137 PCSTR Name = "";
138 if (RosSymEntry->FunctionOffset != 0)
139 {
140 Name = (PCHAR) RosSymInfo->Strings + RosSymEntry->FunctionOffset;
141 }
142 strcpy(FunctionName, Name);
143 }
144
145 return TRUE;
146 }
147
148 /* EOF */