comparison lwlink/map.c @ 0:2c24602be78f

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