aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.7/libjava/gnu/gcj/tools/gc_analyze/SymbolTable.java
blob: eb5df7641b78cfb273ae5c9443dc1eb79deb2e83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* SymbolTable.java -- Maintains a mapping of addresses to names.
   Copyright (C) 2007  Free Software Foundation

   This file is part of libgcj.

   This software is copyrighted work licensed under the terms of the
   Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   details.  */

package gnu.gcj.tools.gc_analyze;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class SymbolTable
{
  // Long address->String name
  private HashMap<Long, String> map = new HashMap<Long, String>();

  // Reverse
  // String name -> Long address
  // used for RelocateImage
  private HashMap<String, Long> reverse = new HashMap<String, Long>();
  
  long loadAddr;
  long relocation;

  static Matcher interestingSymbol =
    Pattern.compile("^([0-9a-fA-F]+)\\s+\\S+\\s+(_Z\\S+)").matcher("");
  static Matcher readelfLoadMatcher =
    Pattern.compile("^\\s+LOAD\\s+(\\S+)\\s+(\\S+)\\s.*").matcher("");
 
  public SymbolTable(String filename) throws IOException
  {
    Process p = Runtime.getRuntime().exec(ToolPrefix.toolPrefix
                                          + "nm " + filename);
    InputStream es = p.getErrorStream();
    InputStream is = p.getInputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    int count = 0;

    String line;
    while ((line = reader.readLine()) != null)
      {
        interestingSymbol.reset(line);
        if (interestingSymbol.matches())
          {
            try
              {
                String name = interestingSymbol.group(2);
                String addr = interestingSymbol.group(1);
                if (name.startsWith("_ZTVN") || name.endsWith("6class$E"))
                  {
                    long address = MemoryMap.parseHexLong(addr);
                    Long l = new Long(address);
                    map.put(l, name);
                    count++;
                    reverse.put(name, l);
                  }
              }
            catch (NumberFormatException e)
              {
                // ignore it
              }
          }
      }
    es.close();
    is.close();
    p.destroy();
    
    if (count > 0)
      {
        // Assume nm read some symbols from it and that
        // readelf can tell us something about how it is loaded.
        p = Runtime.getRuntime().exec(ToolPrefix.toolPrefix
                                      + "readelf -l " + filename);
        es = p.getErrorStream();
        is = p.getInputStream();

        reader = new BufferedReader(new InputStreamReader(is));
        while ((line = reader.readLine()) != null)
          {
            readelfLoadMatcher.reset(line);
            if (readelfLoadMatcher.matches())
              {
                loadAddr
                  = Long.decode(readelfLoadMatcher.group(2)).longValue();
                break;
              }
          }
        es.close();
        is.close();
        p.destroy();
      }
    
    System.out.println(ToolPrefix.toolPrefix + "nm " + filename
                       + " -> " + count + " symbols");
  }

  public static void main(String args[])
  {
    try
      {
        SymbolTable st = new SymbolTable(args[0]);
        st.dump();
      }
    catch (Exception ex)
      {
        ex.printStackTrace();
      }
  }

  public static String demangleVTName(String n)
  {
    if (n.startsWith("_ZTVN") && n.endsWith("E"))
      return demangle(n.substring(5, n.length() - 1));
    else
      return null;
  }

  public void dump()
  {
    for (Map.Entry<Long, String> me : map.entrySet())
      {
        long address = me.getKey();
        String symbol = me.getValue();
        System.out.println(Long.toHexString(address) + " -> " + symbol);
        if (symbol.startsWith("_ZN") && symbol.endsWith("6class$E"))
          {
            System.out.println("  Class: "
                               + demangle(symbol.substring(3, symbol.length()
                                                           - 8)));
          }
        else if (symbol.startsWith("_ZTVN") && symbol.endsWith("E"))
          {
            System.out.println("  VT: "
                               + demangle(symbol.substring(5, symbol.length()
                                                           - 1)));
          }
      }
  }

  private static String demangle(String symbol)
  {
    StringBuilder sb = new StringBuilder();
    for (int i=0; i<symbol.length(); )
      {
        int l = 0;
        while (i < symbol.length())
          {
            int d = symbol.charAt(i);
            if (d < '0' || d > '9')
              break;
            l = 10 * l + (d - '0');
            i++;
          }
        if (l == 0)
          break; 
        // copy
        if (sb.length() > 0)
          sb.append('.');
        while (l > 0 && i < symbol.length())
          {
            sb.append(symbol.charAt(i));
            l--;
            i++;
          }
      }
    return sb.toString();
  }

  public String getSymbol(long address)
  {
    String symbol = map.get(address);
    if (symbol == null)
      return null;

    if (symbol.startsWith("_ZN") && symbol.endsWith("6class$E"))
      symbol = demangle(symbol.substring(3, symbol.length() - 8));
    return symbol;
  }

  // will return -1 if not found
  public long getAddress(String symbol)
  {
    Long address = reverse.get(symbol);
    if (address == null)
      return -1;
    return address.longValue();
  }
}