summaryrefslogtreecommitdiff
path: root/xmake/modules/utils/binary/readsyms.lua
blob: 6058e81216c4d65b7b5f7b7590af98ace21102fc (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
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        readsyms.lua
--

-- imports
import("core.base.binutils")

-- get symbols from binary file (auto-detect format: ELF, COFF, Mach-O)
--
-- @param binaryfile  the binary file path (required)
-- @return            the symbols table
function _get_symbols(binaryfile)
    assert(binaryfile, "usage: xmake l utils.binary.readsyms <binaryfile>")

    binaryfile = path.absolute(binaryfile)
    assert(os.isfile(binaryfile), "%s not found!", binaryfile)

    return binutils.readsyms(binaryfile)
end

-- dump symbols to console
--
-- @param binaryfile  the object file path (required)
function dump(binaryfile)
    local symbols = _get_symbols(binaryfile)
    if symbols and #symbols > 0 then
        -- calculate column widths for alignment
        local max_name_len = 0
        local max_type_len = 0

        for i, sym in ipairs(symbols) do
            if sym.name then
                max_name_len = math.max(max_name_len, #sym.name)
            end
            if sym.type then
                max_type_len = math.max(max_type_len, #sym.type)
            end
        end

        -- calculate column widths
        local type_width = math.max(max_type_len, 4)
        local name_width = math.max(max_name_len, 4)

        -- print header
        print("")
        print("Symbols:")
        local header_format = "  %-" .. type_width .. "s  %s"
        print(string.format(header_format, "TYPE", "NAME"))
        print(string.rep("-", 80))

        -- print symbols
        local format_str = "  %-" .. type_width .. "s  %s"

        for i, sym in ipairs(symbols) do
            local type_str = sym.type or "unknown"
            local name_str = sym.name or ""

            print(string.format(format_str, type_str, name_str))
        end
        print("")
        cprint("${bright}%d symbols found!", #symbols)
    else
        print("")
        cprint("${bright}No symbols found!")
    end
end

-- read symbols from object file(s) (auto-detect format: ELF, COFF, Mach-O)
--
-- @param binaryfiles  the object file path or table of object files (required)
-- @return            the symbols table (all symbols from all files if table is provided)
function main(binaryfiles)
    assert(binaryfiles, "usage: xmake l utils.binary.readsyms <binaryfile> or readsyms(binaryfiles)")

    local all_symbols = {}
    if type(binaryfiles) == "string" then
        return _get_symbols(binaryfiles)
    else
        for _, binaryfile in ipairs(binaryfiles) do
            local symbols = _get_symbols(binaryfile)
            if symbols then
                for _, sym in ipairs(symbols) do
                    table.insert(all_symbols, sym)
                end
            end
        end
        return all_symbols
    end
end