File: binfile.c

package info (click to toggle)
mspdebug 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,700 kB
  • sloc: ansic: 67,821; makefile: 201; xml: 19
file content (125 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download | duplicates (3)
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
/* MSPDebug - debugging tool for MSP430 MCUs
 * Copyright (C) 2009, 2010 Daniel Beer
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "util.h"
#include "binfile.h"
#include "ihex.h"
#include "elf32.h"
#include "symmap.h"
#include "titext.h"
#include "srec.h"
#include "coff.h"
#include "output.h"

struct file_format {
	int (*check)(FILE *in);
	int (*extract)(FILE *in, binfile_imgcb_t cb, void *user_data);
	int (*syms)(FILE *in);
};

static const struct file_format formats[] = {
	{
		.check = elf32_check,
		.extract = elf32_extract,
		.syms = elf32_syms
	},
	{
		.check = ihex_check,
		.extract = ihex_extract
	},
	{
		.check = symmap_check,
		.syms = symmap_syms
	},
	{
		.check = titext_check,
		.extract = titext_extract
	},
	{
		.check = srec_check,
		.extract = srec_extract
	},
	{
		.check = coff_check,
		.extract = coff_extract,
		.syms = coff_syms
	}
};

static const struct file_format *identify(FILE *in)
{
	int i;

	for (i = 0; i < ARRAY_LEN(formats); i++) {
		const struct file_format *f = &formats[i];

		if (f->check(in) > 0)
			return f;
	}

	return NULL;
}

int binfile_info(FILE *in)
{
	const struct file_format *fmt = identify(in);
	int flags = 0;

	if (fmt) {
		if (fmt->extract)
			flags |= BINFILE_HAS_TEXT;
		if (fmt->syms)
			flags |= BINFILE_HAS_SYMS;
	}

	return flags;
}

int binfile_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
{
	const struct file_format *fmt = identify(in);

	if (!fmt) {
		printc_err("binfile: unknown file format\n");
		return -1;
	}

	if (!fmt->extract) {
		printc_err("binfile: this format contains no code\n");
		return -1;
	}

	return fmt->extract(in, cb, user_data);
}

int binfile_syms(FILE *in)
{
	const struct file_format *fmt = identify(in);

	if (!fmt) {
		printc_err("binfile: unknown file format\n");
		return -1;
	}

	if (!fmt->syms) {
		printc_err("binfile: this format contains no symbols\n");
		return -1;
	}

	return fmt->syms(in);
}