annotate src/link.c @ 301:376cc55361c7

section order and address resolution done
author lost
date Wed, 21 Jan 2009 05:45:25 +0000
parents
children eff969272fb9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
301
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
1 /*
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
2 link.c
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
4
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
5 This file is part of LWLINK.
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
6
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
10 version.
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
11
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
15 more details.
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
16
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
19
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
20
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
21 Resolve section and symbol addresses; handle incomplete references
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
22 */
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
23
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
25 #include "config.h"
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
26 #endif
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
27
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
28 #include <stdlib.h>
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
29
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
30 #include "lwlink.h"
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
31 #include "util.h"
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
32
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
33 struct section_list
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
34 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
35 section_t *ptr; // ptr to section structure
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
36 int forceaddr; // was this force to an address by the link script?
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
37 };
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
38
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
39 static struct section_list *sectlist = NULL;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
40 static int nsects = 0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
41
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
42 // work out section load order and resolve base addresses for each section
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
43 // make a list of sections to load in order
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
44 void resolve_sections(void)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
45 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
46 int laddr = 0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
47 int ln, sn, fn;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
48
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
49 for (ln = 0; ln < linkscript.nlines; ln++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
50 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
51 if (linkscript.lines[ln].sectname)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
52 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
53 int f = 0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
54 // named section
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
55 // look for all instances of a section by the specified name
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
56 // and resolve base addresses and add to the list
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
57 for (fn = 0; fn < ninputfiles; fn++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
58 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
59 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
60 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
61 if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
62 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
63 // we have a match
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
64 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
65 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
66
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
67 inputfiles[fn] -> sections[sn].processed = 1;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
68 if (!f && linkscript.lines[ln].loadat >= 0)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
69 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
70 f = 1;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
71 sectlist[nsects].forceaddr = 1;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
72 laddr = linkscript.lines[ln].loadat;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
73 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
74 else
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
75 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
76 sectlist[nsects].forceaddr = 0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
77 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
78 inputfiles[fn] -> sections[sn].loadaddress = laddr;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
79 nsects++;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
80 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
81 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
82 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
83 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
84 else
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
85 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
86 // wildcard section
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
87 // look for all sections not yet processed that match flags
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
88
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
89 int f = 0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
90 int fn0, sn0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
91 char *sname;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
92
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
93 // named section
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
94 // look for all instances of a section by the specified name
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
95 // and resolve base addresses and add to the list
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
96 for (fn0 = 0; fn0 < ninputfiles; fn0++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
97 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
98 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
99 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
100 // ignore if the "no flags" bit says to
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
101 if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
102 continue;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
103 // ignore unless the yes flags tell us not to
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
104 if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
105 continue;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
106 if (inputfiles[fn0] -> sections[sn0].processed == 0)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
107 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
108 sname = inputfiles[fn0] -> sections[sn0].name;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
109 for (fn = 0; fn < ninputfiles; fn++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
110 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
111 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
112 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
113 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
114 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
115 // we have a match
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
116 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
117 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
118
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
119 inputfiles[fn] -> sections[sn].processed = 1;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
120 if (!f && linkscript.lines[ln].loadat >= 0)
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
121 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
122 f = 1;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
123 sectlist[nsects].forceaddr = 1;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
124 laddr = linkscript.lines[ln].loadat;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
125 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
126 else
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
127 {
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
128 sectlist[nsects].forceaddr = 0;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
129 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
130 inputfiles[fn] -> sections[sn].loadaddress = laddr;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
131 nsects++;
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
132 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
133 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
134 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
135 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
136 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
137 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
138 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
139 }
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
140
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
141 // theoretically, all the base addresses are set now
376cc55361c7 section order and address resolution done
lost
parents:
diff changeset
142 }