Mercurial > hg > index.cgi
comparison lwlink/link.c @ 510:3425005b9980
Fix segfault if no instances of a section are in the final result
If no instances of a section are found to be included in the final linked
binary, print a warning instead of crashing with a segfault.
author | William Astle <lost@l-w.ca> |
---|---|
date | Tue, 02 Jun 2020 21:28:47 -0600 |
parents | 9c6204534ab8 |
children | 4f6b2c615818 |
comparison
equal
deleted
inserted
replaced
509:bab891d85a53 | 510:3425005b9980 |
---|---|
19 | 19 |
20 | 20 |
21 Resolve section and symbol addresses; handle incomplete references | 21 Resolve section and symbol addresses; handle incomplete references |
22 */ | 22 */ |
23 | 23 |
24 #define __link_c_seen__ | |
25 | |
24 #include <stdio.h> | 26 #include <stdio.h> |
25 #include <stdlib.h> | 27 #include <stdlib.h> |
26 #include <string.h> | 28 #include <string.h> |
27 | 29 |
28 #include <lw_alloc.h> | 30 #include <lw_alloc.h> |
51 void check_section_name(char *name, int *base, fileinfo_t *fn, int down) | 53 void check_section_name(char *name, int *base, fileinfo_t *fn, int down) |
52 { | 54 { |
53 int sn; | 55 int sn; |
54 sectopt_t *so; | 56 sectopt_t *so; |
55 | 57 |
56 // fprintf(stderr, "Considering sections in %s (%d) for %s\n", fn -> filename, fn -> forced, name); | 58 //fprintf(stderr, "Considering sections in %s (%d) for %s\n", fn -> filename, fn -> forced, name); |
57 if (fn -> forced == 0) | 59 if (fn -> forced == 0) |
58 return; | 60 return; |
59 | 61 |
60 for (so = section_opts; so; so = so -> next) | 62 for (so = section_opts; so; so = so -> next) |
61 if (!strcmp(so -> name, name)) | 63 if (!strcmp(so -> name, name)) |
62 break; | 64 break; |
63 | 65 |
64 for (sn = 0; sn < fn -> nsections; sn++) | 66 for (sn = 0; sn < fn -> nsections; sn++) |
65 { | 67 { |
66 // fprintf(stderr, " Considering section %s\n", fn -> sections[sn].name); | 68 //fprintf(stderr, " Considering section %s\n", fn -> sections[sn].name); |
67 if (!strcmp(name, (char *)(fn -> sections[sn].name))) | 69 if (!strcmp(name, (char *)(fn -> sections[sn].name))) |
68 { | 70 { |
69 if (fn -> sections[sn].flags & SECTION_CONST) | 71 if (fn -> sections[sn].flags & SECTION_CONST) |
70 continue; | 72 continue; |
71 // we have a match | 73 // we have a match |
72 // fprintf(stderr, " Found\n"); | 74 //fprintf(stderr, " Found\n"); |
73 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1)); | 75 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1)); |
74 sectlist[nsects].ptr = &(fn -> sections[sn]); | 76 sectlist[nsects].ptr = &(fn -> sections[sn]); |
75 | 77 |
76 | 78 |
77 fn -> sections[sn].processed = 1; | 79 fn -> sections[sn].processed = 1; |
92 sectlist[nsects].ptr -> loadaddress -= so -> aftersize; | 94 sectlist[nsects].ptr -> loadaddress -= so -> aftersize; |
93 *base -= so -> aftersize; | 95 *base -= so -> aftersize; |
94 so -> aftersize = 0; | 96 so -> aftersize = 0; |
95 } | 97 } |
96 nsects++; | 98 nsects++; |
97 // fprintf(stderr, "Adding section %s (%s)\n",fn -> sections[sn].name, fn -> filename); | 99 //fprintf(stderr, "Adding section %s (%s) %p %d\n",fn -> sections[sn].name, fn -> filename, sectlist, nsects); |
98 } | 100 } |
99 } | 101 } |
100 for (sn = 0; sn < fn -> nsubs; sn++) | 102 for (sn = 0; sn < fn -> nsubs; sn++) |
101 { | 103 { |
102 check_section_name(name, base, fn -> subs[sn], down); | 104 check_section_name(name, base, fn -> subs[sn], down); |
205 if (linkscript.lines[ln].loadat >= 0) | 207 if (linkscript.lines[ln].loadat >= 0) |
206 { | 208 { |
207 laddr = linkscript.lines[ln].loadat; | 209 laddr = linkscript.lines[ln].loadat; |
208 growdown = linkscript.lines[ln].growsdown; | 210 growdown = linkscript.lines[ln].growsdown; |
209 } | 211 } |
210 //fprintf(stderr, "Adding section %s\n", linkscript.lines[ln].sectname); | 212 fprintf(stderr, "Adding section %s\n", linkscript.lines[ln].sectname); |
211 add_matching_sections(linkscript.lines[ln].sectname, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags, &laddr, growdown); | 213 add_matching_sections(linkscript.lines[ln].sectname, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags, &laddr, growdown); |
212 | 214 |
213 if (linkscript.lines[ln].sectname) | 215 if (linkscript.lines[ln].sectname) |
214 { | 216 { |
215 char *sname = linkscript.lines[ln].sectname; | 217 char *sname = linkscript.lines[ln].sectname; |
219 break; | 221 break; |
220 if (so) | 222 if (so) |
221 { | 223 { |
222 if (so -> aftersize) | 224 if (so -> aftersize) |
223 { | 225 { |
224 sectlist[nsects - 1].ptr -> afterbytes = so -> afterbytes; | 226 if (nsects == 0) |
225 sectlist[nsects - 1].ptr -> aftersize = so -> aftersize; | |
226 if (growdown) | |
227 { | 227 { |
228 sectlist[nsects-1].ptr -> loadaddress -= so -> aftersize; | 228 fprintf(stderr, "Warning: no instances of section listed in link script: %s\n", sname); |
229 laddr -= so -> aftersize; | |
230 } | 229 } |
231 else | 230 else |
232 { | 231 { |
233 laddr += so -> aftersize; | 232 sectlist[nsects - 1].ptr -> afterbytes = so -> afterbytes; |
233 sectlist[nsects - 1].ptr -> aftersize = so -> aftersize; | |
234 if (growdown) | |
235 { | |
236 if (sectlist) sectlist[nsects-1].ptr -> loadaddress -= so -> aftersize; | |
237 laddr -= so -> aftersize; | |
238 } | |
239 else | |
240 { | |
241 laddr += so -> aftersize; | |
242 } | |
234 } | 243 } |
235 } | 244 } |
236 } | 245 } |
237 } | 246 } |
238 else | 247 else |