632
|
1 |
#
|
|
2 |
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
# Extract library API from ELF-files
|
|
16 |
#
|
|
17 |
|
|
18 |
import os
|
|
19 |
import sys
|
|
20 |
import re
|
|
21 |
|
|
22 |
from SymbianUtils import SymbianUtilsError
|
|
23 |
|
|
24 |
class Readelf(object):
|
|
25 |
'''
|
|
26 |
Methods for extracting a library API. Blocks.Packaging.ComponentBuilder.Rule
|
|
27 |
and Blocks.Packaging.PackageModel.API make use of Readelf.
|
|
28 |
'''
|
|
29 |
class ConfigurationError(SymbianUtilsError):
|
|
30 |
''' Missing required binaries '''
|
|
31 |
|
|
32 |
class Failed(SymbianUtilsError):
|
|
33 |
''' The external binary returned an error '''
|
|
34 |
|
|
35 |
if sys.platform == "win32":
|
|
36 |
readelf = os.path.normpath(os.path.join(os.path.dirname(__file__), "bin", "readelf.exe"))
|
|
37 |
elif "linux" in sys.platform:
|
|
38 |
readelf = "/usr/bin/readelf"
|
|
39 |
|
|
40 |
if not os.path.isfile(readelf):
|
|
41 |
raise ConfigurationError, "Cannot find readelf command (%s)" % readelf
|
|
42 |
|
|
43 |
@classmethod
|
|
44 |
def getInterface(cls, path):
|
|
45 |
'''
|
|
46 |
|
|
47 |
@param path: The file to inspect
|
|
48 |
@return: The interface - relevant parts from the symbol table as read from file by I{readelf}
|
|
49 |
@rtype: Dictionary
|
|
50 |
'''
|
|
51 |
path = os.path.normpath(path)
|
|
52 |
api = {}
|
|
53 |
binary = cls.readelf + " -Ws " + path + " 2>&1"
|
|
54 |
rex = re.compile(r'^\s*(\d+):\s+\d+\s+\d+\s+FUNC\s+GLOBAL\s+DEFAULT\s+\d+\s+(.*)@@.*')
|
|
55 |
fo = os.popen(binary, "r", -1)
|
|
56 |
for line in fo:
|
|
57 |
match = rex.search(line)
|
|
58 |
if match:
|
|
59 |
api[match.group(1)] = match.group(2)
|
|
60 |
if fo.close():
|
|
61 |
# "Not an ELF file", "No such file", etc.
|
|
62 |
raise cls.Failed("readelf unable to get interface for '%s': %s" % (path, line.strip()))
|
|
63 |
return api |