summaryrefslogtreecommitdiffstats
path: root/coff.h
blob: 1175c04bd2d43626125bac61447515cfa70e1bb4 (plain) (blame)
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
// COFF file parser
// COFF file structure:
// file header
// optional header
// sections(name, address, size, raw data pointer, reloc entry, ptr to first line, number of lines)
// relocation entries
// symbols (name, value, section, type, class) + optional info
// lines (source file symbol, line number, address of code)
// symbol table
// string table

#define LMAX 0x2200
#define DATA_MAX 0x2200
#define ULMAX 0x400

typedef struct filehdr
{
    unsigned short f_magic;
    unsigned short f_nscns;
    unsigned long f_timdat;
    unsigned long f_symptr;
    unsigned long f_nsyms;
    unsigned short f_opthdr;
    unsigned short f_flags;
} filehdr_t;

typedef struct opthdr
{
    unsigned short magic;
    unsigned long vstamp;
    unsigned long proc_type;
    unsigned long rom_width_bits;
    unsigned long ram_width_bits;
} opthdr_t;

typedef struct scnhdr
{
    union
    {
      char _s_name[8]; /* section name is a string */
      struct
        {
          unsigned long _s_zeroes;
          unsigned long _s_offset;
        }_s_s;
    }_s;
	unsigned long s_paddr;
    unsigned long s_vaddr;
    unsigned long s_size;
    unsigned long s_scnptr;
    unsigned long s_relptr;
    unsigned long s_lnnoptr;
    unsigned short s_nreloc;
    unsigned short s_nlnno;
    unsigned long s_flags;
} scnhdr_t;

typedef struct reloc
{
    unsigned long r_vaddr;
    unsigned long r_symndx;
    short r_offset;
    unsigned short r_type;
} reloc_t;

#define SYMNMLEN 8
typedef struct syment
{
    union
    {
      char _n_name[SYMNMLEN];
      struct
      {
        unsigned long _n_zeroes;
        unsigned long _n_offset;
      } _n_n;
      char *_n_nptr[2];
    } _n;
    unsigned long n_value;
    short n_scnum;
    unsigned long n_type;
    char n_sclass;
    unsigned char n_numaux;
} syment_t;

typedef struct coff_lineno
{
    unsigned long l_srcndx;
    unsigned short l_lnno;
    unsigned long l_paddr;
    unsigned short l_flags;
    unsigned long l_fcnndx;
} coff_lineno_t;

typedef struct aux_file
{
    unsigned long x_offset;
    unsigned long x_incline;
    unsigned char x_flags;
    char _unused[11];
} aux_file_t;

typedef struct aux_scn
{
    unsigned long x_scnlen;
    unsigned short x_nreloc;
    unsigned short x_nlinno;
    char _unused[12];
} aux_scn_t;

typedef struct aux_tag
{
    char _unused[6];
    unsigned short x_size;
    char _unused2[4];
    unsigned long x_endndx;
    char _unused3[4];
} aux_tag_t;

typedef struct aux_eos
{
    unsigned long x_tagndx;
    char _unused[2];
    unsigned short x_size;
    char _unused2[12];
} aux_eos_t;

typedef struct aux_fcn
{
    unsigned long x_tagndx;
    unsigned long x_size;
    unsigned long x_lnnoptr;
    unsigned long x_endndx;
    short x_actscnum;
    char _unused[2];
} aux_fcn_t;

typedef struct aux_fcn_calls
{
    unsigned long x_calleendx;
    unsigned long x_is_interrupt;
    char _unused[12];
} aux_fcn_calls_t;

#define X_DIMNUM 4
typedef struct aux_arr
{
    unsigned long x_tagndx;
    unsigned short x_lnno;
    unsigned short x_size;
    unsigned short x_dimen[X_DIMNUM];
    char _unused[4];
} aux_arr_t;

typedef struct aux_eobf
{
    char _unused[4];
    unsigned short x_lnno;
    char _unused2[14];
} aux_eobf_t;

typedef struct aux_bobf
{
    char _unused[4];
    unsigned short x_lnno;
    char _unused2[6];
    unsigned long x_endndx;
    char _unused3[4];
} aux_bobf_t;

typedef struct aux_var
{
    unsigned long x_tagndx;
    char _unused[2];
    unsigned short x_size;
    char _unused2[12];
} aux_var_t;

typedef struct aux_field
{
    char _unused[6];
    unsigned short x_size;
    char _unused2[12];
} aux_field_t;

struct src_i
{
	char* label;
	int src_file;
	int src_line;
};

struct srcfile {
	unsigned long l_srcndx;
	char* name;
	FILE* ptr;
	int nlines;
	long int *lineptr;	//line pointer array
};

struct symbol {
	char* name;
	int value;
};

FILE* scanSourceFile(struct srcfile *s_files_p);
int analyzeCOFF(char *filename,char* label[LMAX],char* ulabel[ULMAX],struct src_i source_info[LMAX],\
					struct srcfile **s_files_p,unsigned short data[0x2200],struct symbol **sym_p, int* nsym);