summaryrefslogtreecommitdiffstats
path: root/src/build/linker/gensyms.C
blob: d445deb69c55f5aa4df393c924cf6396552480a0 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/build/linker/gensyms.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2013,2015                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* 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.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <string>
#include <stdint.h>
#include <cstring>
#include <endian.h>
#include <assert.h>
#include <limits.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

#ifndef be64toh
    #include <byteswap.h>
    #if __BYTE_ORDER == __LITTLE_ENDIAN
        #define be64toh(x) __bswap_64(x)
    #else
        #define be64toh(x) (x)
    #endif
#endif


using namespace std;

/** Print tool usage */
void print_usage();

/** Prepend to a path the img/ subdirectory.
 *
 *  @param[in,out] io_path - The path to modify / prepend to.
 */
void add_image_subdir(string& io_path);

/** Parse the image.modinfo file.
 *
 *  @param[in] i_image - The path to the image to parse the corresponding
 *                       modinfo.
 */
void parse_modinfo_file(const string& i_image);

/** Read the symbols from a module.
 *
 *  @param[in] pair<string, uint64_t>* - Pair of <Path of module, offset >.
 *
 *  Parameters are passed as a (void*) to allow this function to be started
 *  as a thread.
 *
 *  @return Unused.
 */
void* read_module_symbols(void*);

    /** Module information parsed from modinfo.  <Module, Offset> */
vector<pair<string, uint64_t> > g_modules;

    /** Name / path of the base image. */
string g_imageName;
    /** Name / path of the extended image. */
string g_extImageName;
    /** Pointer to the mmap of the base image. */
const char* g_imageFile;
    /** Size of the base image file. */
size_t g_imageFileSize;
    /** Pointer to the mmap of the extended image. */
const char* g_extImageFile;
    /** Size of the extended image file. */
size_t g_extImageFileSize;
    /** Offset (in memory) that the extended image is to be loaded at. */
uint64_t g_extImageOffset = ULONG_MAX;

    /** Cached value of the CROSS_PREFIX environment variable, used to
     *  call binutils tools. */
char* g_crossPrefix = NULL;

    /** Resulting symbol addresses and names.
     *
     *  This is a multimap because there are some symbol addresses with
     *  multiple names.  Ex. the data_start_address often collides with
     *  a global symbol in the data section.
     */
multimap<uint64_t, string> g_symbols;
    /** Mutex to protect symbol map. */
pthread_mutex_t g_symbolMutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char** argv)
{
    // Allow one argument (base image) or three arguments (base, extend, offset)
    if ((argc != 2) && (argc != 4))
    {
        print_usage();
    }

    // Get base image name.
    g_imageName = argv[1];
    add_image_subdir(g_imageName);

    // Get extended image name.
    if (argc > 3)
    {
        g_extImageName = argv[2];
        add_image_subdir(g_extImageName);

        // Read extended image offset from options.
        if (1 != sscanf(argv[3], "%lx", &g_extImageOffset))
        {
            print_usage();
        }
    }

    // Open base image.
    int base_fd = open(g_imageName.c_str(), O_RDONLY);
    if (-1 == base_fd)
    {
        fprintf(stderr, "Failed to open image file: %s.\n", g_imageName.c_str());
        exit(-1);
    }
    struct stat base_stat;
    if (0 != fstat(base_fd, &base_stat))
    {
        fprintf(stderr, "Failed to stat image file: %s.\n", g_imageName.c_str());
        exit(-1);
    }
    g_imageFileSize = base_stat.st_size;
    g_imageFile = (const char*) mmap(NULL, base_stat.st_size,
                                     PROT_READ, MAP_PRIVATE,
                                     base_fd, 0);

    // Open extended image.
    if (string() != g_extImageName.c_str())
    {
        int ext_fd = open(g_extImageName.c_str(), O_RDONLY);
        if (-1 == ext_fd)
        {
            fprintf(stderr, "Failed to open image file: %s.\n", g_extImageName.c_str());
            exit(-1);
        }
        struct stat ext_stat;
        if (0 != fstat(ext_fd, &ext_stat))
        {
            fprintf(stderr, "Failed to stat image file: %s.\n", g_extImageName.c_str());
            exit(-1);
        }
        g_extImageFileSize = ext_stat.st_size;
        g_extImageFile = (const char*) mmap(NULL, ext_stat.st_size,
                                            PROT_READ, MAP_PRIVATE,
                                            ext_fd, 0);
    }

    // Read CROSS_PREFIX environment variable.
    g_crossPrefix = getenv("CROSS_PREFIX");
    if (NULL == g_crossPrefix)
    {
        fprintf(stderr, "Environment variable CROSS_PREFIX not set.\n");
        exit(-1);
    }
    g_crossPrefix = strdup(g_crossPrefix);

    // Parse modinfo file for base image.
    parse_modinfo_file(g_imageName);

    // Create threads for each ELF object in the image(s) to get their symbol
    // information.
    vector<pthread_t*> threads;
    for(vector<pair<string, uint64_t> >::const_iterator i = g_modules.begin();
        i != g_modules.end(); ++i)
    {
        const string& m = i->first;
        // Filter out non-ELF files by filename.
        if (strstr(m.c_str(), ".o") || strstr(m.c_str(), ".elf") ||
            strstr(m.c_str(), ".so"))
        {
            pthread_t* thread = new pthread_t;
            pthread_create(thread, NULL, read_module_symbols,
                           new pair<string,uint64_t>(*i));
            threads.push_back(thread);
        }
    }

    // Wait for all threads to finish.
    for(vector<pthread_t*>::const_iterator i = threads.begin();
        i != threads.end(); ++i)
    {
        pthread_join(*(*i), NULL);
    }

    // Output (in order) each symbol information.
    for (multimap<uint64_t, string>::const_iterator i = g_symbols.begin();
         i != g_symbols.end(); ++i)
    {
        printf("%s", i->second.c_str());
    }

    return 0;
}

void print_usage()
{
    fprintf(stderr, "gensyms <image> [<extimage> <extoffset>]\n");
    exit(-1);
}

void add_image_subdir(string& io_path)
{
    // Prepend ./img if the path to the image directory is not already part
    // of the path.
    if (string::npos == io_path.find("img"))
    {
        io_path.insert(0, "./img/");
    }
}

void parse_modinfo_file(const string& i_image)
{
    // Open modinfo file.
    string modinfo_name = i_image + ".modinfo";
    FILE* modinfo_file = fopen(modinfo_name.c_str(), "r");
    if (NULL == modinfo_file)
    {
        fprintf(stderr, "Unable to open modinfo file.\n");
        exit(-1);
    }

    // Parse one line at a time.
    char line[1024];
    do
    {
        // fgets returns NULL when no additional lines are present, break.
        if (NULL == fgets(line, 1024, modinfo_file)) break;

        // Lines should be formatted: "object,offset\n"

        // Skip lines without a comma.
        char* comma = strchr(line, ',');
        if (NULL == comma) continue;

        // Extract module name (everything before comma).
        string mod_name(line, comma - line);

        // Parse module offset (hex integer after comma).
        uint64_t mod_addr;
        if (1 != sscanf(comma+1, "0x%lx", &mod_addr)) continue;

        // Add to the module list.
        g_modules.push_back(make_pair(mod_name, mod_addr));

    } while(1);
}

void* read_module_symbols(void* input)
{
    // Get module name and offset from input parameter.
    pair<string, uint64_t>* mod_info =
        reinterpret_cast<pair<string,uint64_t>*>(input);
    const string& module = mod_info->first;
    uint64_t addr = mod_info->second;

    // Determine the full path to the module based on the base image path.
    // Assumes they are in the same subdirectory.
    string module_path = g_imageName.substr(0, g_imageName.rfind('/') + 1) +
                         module;

    // Create the 'objdump' command for finding all the symbols and start as
    // a sub-process.
    string command = string(g_crossPrefix) + string("objdump --syms -C ") +
                     module_path;
    FILE* pipe = popen(command.c_str(), "r");
    if (NULL == pipe) return NULL;

    // Local symbol map (to reduce contention on the global symbol map).
    //   No need to use the overhead of a map because we don't care about
    //   order at this point.
    vector<pair<uint64_t, string> > l_symbols;

    // Parse each line of the 'objdump' output.
    char line[1024];
    do
    {
        if (NULL == fgets(line, 1024, pipe)) break;

        size_t linelen = strlen(line);

        // Skip absolute values (ex. constants) and undefined symbols.
        if (strstr(line, "*ABS*") || strstr(line, "*UND*")) continue;
        // Skip section symbols (marked by 'd' in the 22nd column).
        if (linelen > 22 && 'd' == line[22]) continue;

        // First part of an objdump line is the symbol address, parse that.
        uint64_t line_address;
        if (1 != sscanf(line, "%16lx", &line_address)) continue;
        line_address += addr;

        // Determine if the symbol is a function and if it is in the .rodata
        // section.  Symbols in the .rodata section have a slightly longer
        // line than those in the .text/.data sections (by 2 characters).
        bool is_function = (linelen > 23 && 'F' == line[23]);
        size_t rodata = (NULL != strstr(line, ".rodata")) ? 2 : 0;

        // Parse the symbol size.
        uint64_t symbol_size;
        if (linelen > 32+rodata &&
            1 != sscanf(&line[32+rodata], "%lx", &symbol_size)) continue;

        // Parse the function name.
        assert(linelen > 48+rodata);
        string function = &line[48+rodata];
        function.resize(function.length() - 1); // remove the newline.

        // Function have two addresses: TOC entry and code address.  Objdump
        // gives the TOC entry, so we need to read the file itself to determine
        // the code address.  The first part of the TOC entry is the code
        // address.
        uint64_t code_addr = 0;
        if (is_function)
        {
            // Module is in the extended image, read from it.
            if (line_address > g_extImageOffset)
            {
                // Read code address.
                assert((line_address - g_extImageOffset) < g_extImageFileSize);
                memcpy(&code_addr,
                       &g_extImageFile[line_address - g_extImageOffset], 8);
            }
            // Module is in the base image.
            else
            {
                // Read code address.
                assert(line_address < g_imageFileSize);
                memcpy(&code_addr, &g_imageFile[line_address], 8);
            }
            // Fix up the endianness.
            code_addr = be64toh(code_addr);

            std::swap(code_addr, line_address);
        }

        // Print all of this into a new line and add to the symbol map.
        sprintf(line, "%c,%08lx,%08lx,%08lx,%s\n",
            is_function ? 'F' : 'V',
            line_address, code_addr, symbol_size,
            function.c_str());

        l_symbols.push_back(make_pair(line_address, line));

    } while(1);

    // Close subprocess (done).
    pclose(pipe);

    // Copy our local symbol list all at once into the global symbol list.
    pthread_mutex_lock(&g_symbolMutex);
    g_symbols.insert(l_symbols.begin(), l_symbols.end());
    pthread_mutex_unlock(&g_symbolMutex);

    return NULL;
}
OpenPOWER on IntegriCloud