annotate lwlink/map.c @ 185:b89adfb0d174

Added support for outputting a linkmap
author lost
date Sat, 21 Mar 2009 19:47:45 +0000
parents
children bae1e3ecdce1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
185
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
1 /*
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
2 map.c
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
4
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
5 This file is part of LWLINK.
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
6
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
10 version.
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
11
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
15 more details.
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
16
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
19
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
20
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
21 Output information about the linking process
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
22 */
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
23
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
25 #include "config.h"
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
26 #endif
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
27
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
28 #include <errno.h>
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
29 #include <stdio.h>
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
30 #include <stdlib.h>
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
31
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
32 #include "lwlink.h"
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
33 #include "util.h"
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
34
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
35 struct symliste
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
36 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
37 char *name;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
38 char *fn;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
39 int addr;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
40 int ext;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
41 struct symliste *next;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
42 };
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
43
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
44 void display_map(void)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
45 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
46 FILE *of;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
47 int sn;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
48 int std = 0;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
49 struct symliste *slist = NULL;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
50 struct symliste *ce, *pe, *ne;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
51 symtab_t *sym;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
52 int i;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
53
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
54 if (!strcmp(map_file, "-"))
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
55 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
56 std = 1;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
57 of = stdout;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
58 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
59 else
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
60 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
61 of = fopen(map_file, "w");
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
62 if (!of)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
63 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
64 fprintf(stderr, "Cannot open map file - using stdout\n");
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
65 std = 1;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
66 of = stdout;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
67 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
68 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
69
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
70 // display section list
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
71 for (sn = 0; sn < nsects; sn++)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
72 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
73 fprintf(of, "Section: %s (%s) load at %04X, length %04X\n",
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
74 sectlist[sn].ptr -> name,
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
75 sectlist[sn].ptr -> file -> filename,
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
76 sectlist[sn].ptr -> loadaddress,
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
77 sectlist[sn].ptr -> codesize
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
78 );
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
79 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
80
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
81 // generate a sorted list of symbols and display it
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
82 for (sn = 0; sn < nsects; sn++)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
83 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
84 for (sym = sectlist[sn].ptr -> localsyms; sym; sym = sym -> next)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
85 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
86 for (pe = NULL, ce = slist; ce; ce = ce -> next)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
87 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
88 i = strcmp(ce -> name, sym -> sym);
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
89 if (i == 0)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
90 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
91 i = strcmp(ce -> fn, sectlist[sn].ptr -> file -> filename);
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
92 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
93 if (i > 0)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
94 break;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
95 pe = ce;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
96 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
97 ne = lw_malloc(sizeof(struct symliste));
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
98 ne -> ext = 0;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
99 ne -> addr = sym -> offset + sectlist[sn].ptr -> loadaddress;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
100 ne -> next = ce;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
101 ne -> name = sym -> sym;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
102 ne -> fn = sectlist[sn].ptr -> file -> filename;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
103 if (pe)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
104 pe -> next = ne;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
105 else
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
106 slist = ne;
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
107 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
108 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
109
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
110 for (ce = slist; ce; ce = ce -> next)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
111 {
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
112 fprintf(of, "Symbol: %s (%s) = %04X\n", ce -> name, ce -> fn, ce -> addr);
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
113 }
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
114
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
115 if (!std)
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
116 fclose(of);
b89adfb0d174 Added support for outputting a linkmap
lost
parents:
diff changeset
117 }