summaryrefslogtreecommitdiff
path: root/src/minibfd/binfile.c
blob: dd6aef1f3b8a608a5ef5c53a7ad61356e674bcb9 (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
/*
   This file is part of odin, a memory profiler with fragmentation analysis.

   Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>

   Based on:
   MemProf -- memory profiler and leak detector
   Copyright 1999, 2000, 2001, Red Hat, Inc.
   Copyright 2002, Kristian Rietveld

   Sysprof -- Sampling, systemwide CPU profiler
   Copyright 2004, 2005, 2006, 2007, Soeren Sandmann


   odin is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   odin is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with odin. If not, see <http://www.gnu.org/licenses/>/

   The GNU General Public License is contained in the file COPYING.
*/

/* Most interesting code in this file is lifted from bfdutils.c
 * and process.c from Memprof,
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "binfile.h"
#include "elfparser.h"

#define DEBUGDIR "/usr/lib/debug"

static const char * const vdso = "[vdso]";

static ino_t
read_inode (const char *filename)
{
    struct stat statbuf;

    if (strcmp (filename, vdso) == 0)
	return (ino_t)0;

    if (stat (filename, &statbuf) < 0)
	return (ino_t)-1;

    return statbuf.st_ino;
}

static ElfParser *
separate_debug_file_exists (const char *name, guint32 crc, gboolean force)
{
    ElfParser *parser;

    parser = elf_parser_new (name, NULL);
    if (parser == NULL)
	return NULL;

    if (elf_parser_get_crc32 (parser) != crc) {
	if (force) {
	    g_warning ("debug crc mismatch for '%s' (forced load)", name);
	} else {
	    elf_parser_free (parser);
	    return NULL;
	}
    }

    return parser;
}

static ElfParser *
get_debug_file (ElfParser *elf, const char *filename, char **new_name)
{
#define N_TRIES 4
    const char *basename;
    char *dir;
    guint32 crc32;
    char *tries[N_TRIES];
    int i;
    ElfParser *result;

    if (!elf)
	return NULL;

    basename = elf_parser_get_debug_link (elf, &crc32);
    if (basename == NULL)
	return NULL;

    dir = g_path_get_dirname (filename);

    tries[0] = g_build_filename (dir, basename, NULL);
    tries[1] = g_build_filename (dir, ".debug", basename, NULL);
    tries[2] = g_build_filename ("/usr" G_DIR_SEPARATOR_S "lib" G_DIR_SEPARATOR_S "debug", dir, basename, NULL);
    tries[3] = g_build_filename (DEBUGDIR, dir, basename, NULL);

    for (i = 0; i < N_TRIES; ++i) {
	result = separate_debug_file_exists (tries[i], crc32, FALSE);
	if (result) {
	    if (new_name)
		*new_name = g_strdup (tries[i]);
	    break;
	}
    }

    for (i = 1; i < N_TRIES; ++i) {
	result = separate_debug_file_exists (tries[i], crc32, TRUE);
	if (result) {
	    if (new_name)
		*new_name = g_strdup (tries[i]);
	    break;
	}
    }

    g_free (dir);

    for (i = 0; i < N_TRIES; ++i)
	g_free (tries[i]);

    return result;
}

static ElfParser *
find_separate_debug_file (ElfParser *elf, const char *filename, char **real_filename)
{
    ElfParser *debug, *ret = NULL;
    char *debug_name = NULL;
    char *fname;
    GHashTable *seen_names = g_hash_table_new_full (g_str_hash,
	                                            g_str_equal,
						    g_free,
						    NULL);

    fname = g_strdup (filename);
    do {
	if (g_hash_table_lookup (seen_names, fname)) {
	    /* cycle detected, just return the original elf file itself */
	    break;
	}

	debug = get_debug_file (elf, fname, &debug_name);
	if (debug != NULL) {
	    elf_parser_free (elf);
	    elf = ret = debug;

	    g_hash_table_insert (seen_names, fname, fname);
	    fname = debug_name;
	}
    } while (debug);

    *real_filename = fname;
    g_hash_table_destroy (seen_names);

    return ret;
}

void
bin_file_init (BinFile *bf, const char *filename)
{
    bf->elf = NULL;
    bf->real_filename = NULL;
    bf->inode_check = FALSE;

    if (strcmp (filename, vdso) == 0) {
	bf->filename = (char *) vdso;
#if 0
	gsize length;
	const guint8 *vdso_bytes = NULL;

	vdso_bytes = get_vdso_bytes (&length);
	if (vdso_bytes != NULL)
	    bf->elf = elf_parser_new_from_data (vdso_bytes, length);
	else
	    bf->elf = NULL;
#endif
    } else {
	bf->filename = g_strdup (filename);
	bf->elf = elf_parser_new (filename, NULL);
    }

    /* We need the text offset of the actual binary, not the
     * (potential) debug binary
     */
    if (bf->elf != NULL) {
	bf->text_offset = elf_parser_get_text_offset (bf->elf);
	bf->elf = find_separate_debug_file (bf->elf, filename,
					    &bf->real_filename);

	bf->inode = read_inode (filename);
    }
}

void
bin_file_open (BinFile *bf)
{
    if (bf->elf != NULL)
	return;

    if (bf->filename == vdso) {
	/* permanent */
    } else if (bf->real_filename != NULL) {
	bf->elf = elf_parser_new (bf->real_filename, NULL);
	bf->inode = read_inode (bf->filename);
    }
}

void
bin_file_close (BinFile *bf)
{
    if (bf->elf == NULL)
	return;

    if (bf->filename == vdso) {
	elf_parser_free (bf->elf);
	bf->elf = NULL;
    }
}

void
bin_file_fini (BinFile *bf)
{
    if (bf->elf != NULL)
	elf_parser_free (bf->elf);

    g_free (bf->real_filename);
    if (bf->filename != vdso)
	g_free (bf->filename);
}

gboolean
bin_file_lookup_symbol (BinFile    *bf,
	                gulong      address,
			const char **function,
			const char **filename,
			const char **path,
			guint *line)
{
    g_assert (bf->elf != NULL);

    return elf_parser_lookup_symbol (bf->elf, address - bf->text_offset,
				     function,
				     filename,
				     path,
				     line);
}

gboolean
bin_file_check_inode (BinFile *bf,
	              ino_t    inode)
{
    if (bf->inode == inode)
	return TRUE;

    if (bf->elf == NULL)
	return FALSE;

    if (! bf->inode_check) {
	g_print ("warning: Inode mismatch for %s (disk: %" G_GUINT64_FORMAT ", memory: %" G_GUINT64_FORMAT ")\n",
		bf->filename,
		(guint64) bf->inode,
		(guint64) inode);

	bf->inode_check = TRUE;
    }

    return FALSE;
}