annotate lwdisasm/symbol.c @ 409:cba03436c720

Checkpoint disassembler
author lost@l-w.ca
date Mon, 02 Aug 2010 18:07:04 -0600
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
409
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
1 /*
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
2 symbol.c
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
3
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2010 William Astle
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
5
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
7
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
11 version.
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
12
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
16 more details.
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
17
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
20 */
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
21
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
22 #include <config.h>
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
23
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
24 #include <stdio.h>
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
25
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
26 #include <lw_alloc.h>
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
27 #include <lw_string.h>
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
28
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
29 #include "lwdisasm.h"
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
30
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
31 symbol_t *find_symbol(disasmstate_t *as, int addr, int section)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
32 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
33 symbol_t *s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
34
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
35 for (s = as -> symbols; s; s = s -> next)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
36 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
37 if (s -> address == addr && s -> section == section)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
38 return s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
39 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
40 return NULL;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
41 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
42
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
43 symbol_t *register_symbol(disasmstate_t *as, int addr, int section, char *symbol)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
44 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
45 char symbuf[200];
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
46 symbol_t *s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
47
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
48 for (s = as -> symbols; s; s = s -> next)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
49 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
50 if (s -> address == addr && s -> section == section)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
51 return s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
52 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
53
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
54 if (!symbol)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
55 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
56 if (section)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
57 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
58 sprintf(symbuf, "L%d_%04X", section, addr);
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
59 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
60 else
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
61 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
62 sprintf(symbuf, "L%04X", addr);
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
63 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
64 symbol = symbuf;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
65 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
66
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
67 s = lw_alloc(sizeof(symbol_t));
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
68 s -> symbol = lw_strdup(symbol);
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
69 s -> address = addr;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
70 s -> section = section;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
71 s -> next = as -> symbols;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
72 as -> symbols = s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
73 return s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
74 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
75
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
76 void attach_symbols(disasmstate_t *as)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
77 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
78 symbol_t *s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
79 linedata_t *l;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
80
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
81 for (s = as -> symbols; s; s = s -> next)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
82 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
83 for (l = as -> lhead; l; l = l -> next)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
84 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
85 if (l -> isref == 0 && l -> address == s -> address && l -> sectionref == s -> section)
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
86 {
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
87 l -> symbol = s;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
88 l -> isref = 1;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
89 break;
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
90 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
91 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
92 }
cba03436c720 Checkpoint disassembler
lost@l-w.ca
parents:
diff changeset
93 }