Bug Summary

File:src/gnu/usr.bin/binutils-2.17/ld/ldexp.c
Warning:line 755, column 16
Access to field 'type' results in a dereference of a null pointer (loaded from variable 'h')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ldexp.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/binutils-2.17/obj/ld -resource-dir /usr/local/lib/clang/13.0.0 -D HAVE_CONFIG_H -I . -I /usr/src/gnu/usr.bin/binutils-2.17/ld -I . -D _GNU_SOURCE -I . -I /usr/src/gnu/usr.bin/binutils-2.17/ld -I ../bfd -I /usr/src/gnu/usr.bin/binutils-2.17/ld/../bfd -I /usr/src/gnu/usr.bin/binutils-2.17/ld/../include -I /usr/src/gnu/usr.bin/binutils-2.17/ld/../intl -I ../intl -D PIE_DEFAULT=1 -D LOCALEDIR="/usr/share/locale" -D PIE_DEFAULT=1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-null-pointer-arithmetic -fdebug-compilation-dir=/usr/src/gnu/usr.bin/binutils-2.17/obj/ld -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/gnu/usr.bin/binutils-2.17/ld/ldexp.c
1/* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
6
7 This file is part of GLD, the Gnu Linker.
8
9 GLD is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GLD is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GLD; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23
24/* This module is in charge of working out the contents of expressions.
25
26 It has to keep track of the relative/absness of a symbol etc. This
27 is done by keeping all values in a struct (an etree_value_type)
28 which contains a value, a section to which it is relative and a
29 valid bit. */
30
31#include "bfd.h"
32#include "sysdep.h"
33#include "bfdlink.h"
34
35#include "ld.h"
36#include "ldmain.h"
37#include "ldmisc.h"
38#include "ldexp.h"
39#include <ldgram.h>
40#include "ldlang.h"
41#include "libiberty.h"
42#include "safe-ctype.h"
43
44static void exp_fold_tree_1 (etree_type *);
45static void exp_fold_tree_no_dot (etree_type *);
46static bfd_vma align_n (bfd_vma, bfd_vma);
47
48segment_type *segments;
49
50struct ldexp_control expld;
51
52/* Print the string representation of the given token. Surround it
53 with spaces if INFIX_P is TRUE. */
54
55static void
56exp_print_token (token_code_type code, int infix_p)
57{
58 static const struct
59 {
60 token_code_type code;
61 char * name;
62 }
63 table[] =
64 {
65 { INT257, "int" },
66 { NAME258, "NAME" },
67 { PLUSEQ260, "+=" },
68 { MINUSEQ261, "-=" },
69 { MULTEQ262, "*=" },
70 { DIVEQ263, "/=" },
71 { LSHIFTEQ264, "<<=" },
72 { RSHIFTEQ265, ">>=" },
73 { ANDEQ266, "&=" },
74 { OREQ267, "|=" },
75 { OROR268, "||" },
76 { ANDAND269, "&&" },
77 { EQ270, "==" },
78 { NE271, "!=" },
79 { LE272, "<=" },
80 { GE273, ">=" },
81 { LSHIFT274, "<<" },
82 { RSHIFT275, ">>" },
83 { ALIGN_K278, "ALIGN" },
84 { BLOCK279, "BLOCK" },
85 { QUAD281, "QUAD" },
86 { SQUAD282, "SQUAD" },
87 { LONG283, "LONG" },
88 { SHORT284, "SHORT" },
89 { BYTE285, "BYTE" },
90 { SECTIONS286, "SECTIONS" },
91 { SIZEOF_HEADERS293, "SIZEOF_HEADERS" },
92 { MEMORY300, "MEMORY" },
93 { DEFINED307, "DEFINED" },
94 { TARGET_K308, "TARGET" },
95 { SEARCH_DIR309, "SEARCH_DIR" },
96 { MAP310, "MAP" },
97 { ENTRY311, "ENTRY" },
98 { NEXT312, "NEXT" },
99 { SIZEOF313, "SIZEOF" },
100 { ADDR314, "ADDR" },
101 { LOADADDR315, "LOADADDR" },
102 { MAX_K316, "MAX_K" },
103 { REL353, "relocatable" },
104 { DATA_SEGMENT_ALIGN288, "DATA_SEGMENT_ALIGN" },
105 { DATA_SEGMENT_RELRO_END289, "DATA_SEGMENT_RELRO_END" },
106 { DATA_SEGMENT_END290, "DATA_SEGMENT_END" },
107 { ORIGIN324, "ORIGIN" },
108 { LENGTH326, "LENGTH" },
109 { SEGMENT_START298, "SEGMENT_START" }
110 };
111 unsigned int idx;
112
113 for (idx = 0; idx < ARRAY_SIZE (table)(sizeof (table) / sizeof ((table)[0])); idx++)
114 if (table[idx].code == code)
115 break;
116
117 if (infix_p)
118 fputc (' ', config.map_file);
119
120 if (idx < ARRAY_SIZE (table)(sizeof (table) / sizeof ((table)[0])))
121 fputs (table[idx].name, config.map_file);
122 else if (code < 127)
123 fputc (code, config.map_file);
124 else
125 fprintf (config.map_file, "<code %d>", code);
126
127 if (infix_p)
128 fputc (' ', config.map_file);
129}
130
131static void
132make_abs (void)
133{
134 expld.result.value += expld.result.section->vma;
135 expld.result.section = bfd_abs_section_ptr((asection *) &bfd_abs_section);
136}
137
138static void
139new_abs (bfd_vma value)
140{
141 expld.result.valid_p = TRUE1;
142 expld.result.section = bfd_abs_section_ptr((asection *) &bfd_abs_section);
143 expld.result.value = value;
144 expld.result.str = NULL((void*)0);
145}
146
147etree_type *
148exp_intop (bfd_vma value)
149{
150 etree_type *new = stat_alloc (sizeof (new->value));
151 new->type.node_code = INT257;
152 new->value.value = value;
153 new->value.str = NULL((void*)0);
154 new->type.node_class = etree_value;
155 return new;
156}
157
158etree_type *
159exp_bigintop (bfd_vma value, char *str)
160{
161 etree_type *new = stat_alloc (sizeof (new->value));
162 new->type.node_code = INT257;
163 new->value.value = value;
164 new->value.str = str;
165 new->type.node_class = etree_value;
166 return new;
167}
168
169/* Build an expression representing an unnamed relocatable value. */
170
171etree_type *
172exp_relop (asection *section, bfd_vma value)
173{
174 etree_type *new = stat_alloc (sizeof (new->rel));
175 new->type.node_code = REL353;
176 new->type.node_class = etree_rel;
177 new->rel.section = section;
178 new->rel.value = value;
179 return new;
180}
181
182static void
183new_rel (bfd_vma value, char *str, asection *section)
184{
185 expld.result.valid_p = TRUE1;
186 expld.result.value = value;
187 expld.result.str = str;
188 expld.result.section = section;
189}
190
191static void
192new_rel_from_abs (bfd_vma value)
193{
194 expld.result.valid_p = TRUE1;
195 expld.result.value = value - expld.section->vma;
196 expld.result.str = NULL((void*)0);
197 expld.result.section = expld.section;
198}
199
200static void
201fold_unary (etree_type *tree)
202{
203 exp_fold_tree_1 (tree->unary.child);
204 if (expld.result.valid_p)
205 {
206 switch (tree->type.node_code)
207 {
208 case ALIGN_K278:
209 if (expld.phase != lang_first_phase_enum)
210 new_rel_from_abs (align_n (expld.dot, expld.result.value));
211 else
212 expld.result.valid_p = FALSE0;
213 break;
214
215 case ABSOLUTE341:
216 make_abs ();
217 break;
218
219 case '~':
220 make_abs ();
221 expld.result.value = ~expld.result.value;
222 break;
223
224 case '!':
225 make_abs ();
226 expld.result.value = !expld.result.value;
227 break;
228
229 case '-':
230 make_abs ();
231 expld.result.value = -expld.result.value;
232 break;
233
234 case NEXT312:
235 /* Return next place aligned to value. */
236 if (expld.phase != lang_first_phase_enum)
237 {
238 make_abs ();
239 expld.result.value = align_n (expld.dot, expld.result.value);
240 }
241 else
242 expld.result.valid_p = FALSE0;
243 break;
244
245 case DATA_SEGMENT_END290:
246 if (expld.phase != lang_first_phase_enum
247 && expld.section == bfd_abs_section_ptr((asection *) &bfd_abs_section)
248 && (expld.dataseg.phase == exp_dataseg_align_seen
249 || expld.dataseg.phase == exp_dataseg_relro_seen
250 || expld.dataseg.phase == exp_dataseg_adjust
251 || expld.dataseg.phase == exp_dataseg_relro_adjust
252 || expld.phase == lang_final_phase_enum))
253 {
254 if (expld.dataseg.phase == exp_dataseg_align_seen
255 || expld.dataseg.phase == exp_dataseg_relro_seen)
256 {
257 expld.dataseg.phase = exp_dataseg_end_seen;
258 expld.dataseg.end = expld.result.value;
259 }
260 }
261 else
262 expld.result.valid_p = FALSE0;
263 break;
264
265 default:
266 FAIL ()do { info_assert("/usr/src/gnu/usr.bin/binutils-2.17/ld/ldexp.c"
,266); } while (0)
;
267 break;
268 }
269 }
270}
271
272static void
273fold_binary (etree_type *tree)
274{
275 exp_fold_tree_1 (tree->binary.lhs);
276
277 /* The SEGMENT_START operator is special because its first
278 operand is a string, not the name of a symbol. */
279 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START298)
280 {
281 const char *segment_name;
282 segment_type *seg;
283 /* Check to see if the user has overridden the default
284 value. */
285 segment_name = tree->binary.rhs->name.name;
286 for (seg = segments; seg; seg = seg->next)
287 if (strcmp (seg->name, segment_name) == 0)
288 {
289 seg->used = TRUE1;
290 expld.result.value = seg->value;
291 expld.result.str = NULL((void*)0);
292 expld.result.section = NULL((void*)0);
293 break;
294 }
295 }
296 else if (expld.result.valid_p)
297 {
298 etree_value_type lhs = expld.result;
299
300 exp_fold_tree_1 (tree->binary.rhs);
301 if (expld.result.valid_p)
302 {
303 /* If the values are from different sections, or this is an
304 absolute expression, make both the source arguments
305 absolute. However, adding or subtracting an absolute
306 value from a relative value is meaningful, and is an
307 exception. */
308 if (expld.section != bfd_abs_section_ptr((asection *) &bfd_abs_section)
309 && lhs.section == bfd_abs_section_ptr((asection *) &bfd_abs_section)
310 && tree->type.node_code == '+')
311 {
312 /* Keep the section of the rhs term. */
313 expld.result.value = lhs.value + expld.result.value;
314 return;
315 }
316 else if (expld.section != bfd_abs_section_ptr((asection *) &bfd_abs_section)
317 && expld.result.section == bfd_abs_section_ptr((asection *) &bfd_abs_section)
318 && (tree->type.node_code == '+'
319 || tree->type.node_code == '-'))
320 {
321 /* Keep the section of the lhs term. */
322 expld.result.section = lhs.section;
323 }
324 else if (expld.result.section != lhs.section
325 || expld.section == bfd_abs_section_ptr((asection *) &bfd_abs_section))
326 {
327 make_abs ();
328 lhs.value += lhs.section->vma;
329 }
330
331 switch (tree->type.node_code)
332 {
333 case '%':
334 if (expld.result.value != 0)
335 expld.result.value = ((bfd_signed_vma) lhs.value
336 % (bfd_signed_vma) expld.result.value);
337 else if (expld.phase != lang_mark_phase_enum)
338 einfo (_("%F%S %% by zero\n")("%F%S %% by zero\n"));
339 break;
340
341 case '/':
342 if (expld.result.value != 0)
343 expld.result.value = ((bfd_signed_vma) lhs.value
344 / (bfd_signed_vma) expld.result.value);
345 else if (expld.phase != lang_mark_phase_enum)
346 einfo (_("%F%S / by zero\n")("%F%S / by zero\n"));
347 break;
348
349#define BOP(x, y)case x: expld.result.value = lhs.value y expld.result.value; break
;
\
350 case x: \
351 expld.result.value = lhs.value y expld.result.value; \
352 break;
353
354 BOP ('+', +)case '+': expld.result.value = lhs.value + expld.result.value
; break;
;
355 BOP ('*', *)case '*': expld.result.value = lhs.value * expld.result.value
; break;
;
356 BOP ('-', -)case '-': expld.result.value = lhs.value - expld.result.value
; break;
;
357 BOP (LSHIFT, <<)case 274: expld.result.value = lhs.value << expld.result
.value; break;
;
358 BOP (RSHIFT, >>)case 275: expld.result.value = lhs.value >> expld.result
.value; break;
;
359 BOP (EQ, ==)case 270: expld.result.value = lhs.value == expld.result.value
; break;
;
360 BOP (NE, !=)case 271: expld.result.value = lhs.value != expld.result.value
; break;
;
361 BOP ('<', <)case '<': expld.result.value = lhs.value < expld.result
.value; break;
;
362 BOP ('>', >)case '>': expld.result.value = lhs.value > expld.result
.value; break;
;
363 BOP (LE, <=)case 272: expld.result.value = lhs.value <= expld.result.value
; break;
;
364 BOP (GE, >=)case 273: expld.result.value = lhs.value >= expld.result.value
; break;
;
365 BOP ('&', &)case '&': expld.result.value = lhs.value & expld.result
.value; break;
;
366 BOP ('^', ^)case '^': expld.result.value = lhs.value ^ expld.result.value
; break;
;
367 BOP ('|', |)case '|': expld.result.value = lhs.value | expld.result.value
; break;
;
368 BOP (ANDAND, &&)case 269: expld.result.value = lhs.value && expld.result
.value; break;
;
369 BOP (OROR, ||)case 268: expld.result.value = lhs.value || expld.result.value
; break;
;
370
371 case MAX_K316:
372 if (lhs.value > expld.result.value)
373 expld.result.value = lhs.value;
374 break;
375
376 case MIN_K317:
377 if (lhs.value < expld.result.value)
378 expld.result.value = lhs.value;
379 break;
380
381 case ALIGN_K278:
382 expld.result.value = align_n (lhs.value, expld.result.value);
383 break;
384
385 case DATA_SEGMENT_ALIGN288:
386 if (expld.phase != lang_first_phase_enum
387 && expld.section == bfd_abs_section_ptr((asection *) &bfd_abs_section)
388 && (expld.dataseg.phase == exp_dataseg_none
389 || expld.dataseg.phase == exp_dataseg_adjust
390 || expld.dataseg.phase == exp_dataseg_relro_adjust
391 || expld.phase == lang_final_phase_enum))
392 {
393 bfd_vma maxpage = lhs.value;
394 bfd_vma commonpage = expld.result.value;
395
396 expld.result.value = align_n (expld.dot, maxpage);
397 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
398 expld.result.value = expld.dataseg.base;
399 else if (expld.dataseg.phase != exp_dataseg_adjust)
400 {
401 expld.result.value += expld.dot & (maxpage - 1);
402 if (expld.phase == lang_allocating_phase_enum)
403 {
404 expld.dataseg.phase = exp_dataseg_align_seen;
405 expld.dataseg.min_base = align_n (expld.dot, maxpage);
406 expld.dataseg.base = expld.result.value;
407 expld.dataseg.pagesize = commonpage;
408 expld.dataseg.maxpagesize = maxpage;
409 expld.dataseg.relro_end = 0;
410 }
411 }
412 else if (commonpage < maxpage)
413 expld.result.value += ((expld.dot + commonpage - 1)
414 & (maxpage - commonpage));
415 }
416 else
417 expld.result.valid_p = FALSE0;
418 break;
419
420 case DATA_SEGMENT_RELRO_END289:
421 if (expld.phase != lang_first_phase_enum
422 && (expld.dataseg.phase == exp_dataseg_align_seen
423 || expld.dataseg.phase == exp_dataseg_adjust
424 || expld.dataseg.phase == exp_dataseg_relro_adjust
425 || expld.phase == lang_final_phase_enum))
426 {
427 if (expld.dataseg.phase == exp_dataseg_align_seen
428 || expld.dataseg.phase == exp_dataseg_relro_adjust)
429 expld.dataseg.relro_end = lhs.value + expld.result.value;
430
431 if (expld.dataseg.phase == exp_dataseg_relro_adjust
432 && (expld.dataseg.relro_end
433 & (expld.dataseg.pagesize - 1)))
434 {
435 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
436 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
437 expld.result.value = (expld.dataseg.relro_end
438 - expld.result.value);
439 }
440 else
441 expld.result.value = lhs.value;
442
443 if (expld.dataseg.phase == exp_dataseg_align_seen)
444 expld.dataseg.phase = exp_dataseg_relro_seen;
445 }
446 else
447 expld.result.valid_p = FALSE0;
448 break;
449
450 default:
451 FAIL ()do { info_assert("/usr/src/gnu/usr.bin/binutils-2.17/ld/ldexp.c"
,451); } while (0)
;
452 }
453 }
454 else
455 expld.result.valid_p = FALSE0;
456 }
457}
458
459static void
460fold_trinary (etree_type *tree)
461{
462 exp_fold_tree_1 (tree->trinary.cond);
463 if (expld.result.valid_p)
464 exp_fold_tree_1 (expld.result.value
465 ? tree->trinary.lhs
466 : tree->trinary.rhs);
467}
468
469static void
470fold_name (etree_type *tree)
471{
472 memset (&expld.result, 0, sizeof (expld.result));
14
Value assigned to 'expld.result.valid_p', which participates in a condition later
473
474 switch (tree->type.node_code)
15
Control jumps to 'case 315:' at line 570
475 {
476 case SIZEOF_HEADERS293:
477 if (expld.phase != lang_first_phase_enum)
478 {
479 bfd_vma hdr_size = 0;
480 /* Don't find the real header size if only marking sections;
481 The bfd function may cache incorrect data. */
482 if (expld.phase != lang_mark_phase_enum)
483 hdr_size = bfd_sizeof_headers (output_bfd, link_info.relocatable)((*((output_bfd)->xvec->_bfd_sizeof_headers)) (output_bfd
, link_info.relocatable))
;
484 new_abs (hdr_size);
485 }
486 break;
487 case DEFINED307:
488 if (expld.phase == lang_first_phase_enum)
489 lang_track_definedness (tree->name.name);
490 else
491 {
492 struct bfd_link_hash_entry *h;
493 int def_iteration
494 = lang_symbol_definition_iteration (tree->name.name);
495
496 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
497 tree->name.name,
498 FALSE0, FALSE0, TRUE1);
499 expld.result.value = (h != NULL((void*)0)
500 && (h->type == bfd_link_hash_defined
501 || h->type == bfd_link_hash_defweak
502 || h->type == bfd_link_hash_common)
503 && (def_iteration == lang_statement_iteration
504 || def_iteration == -1));
505 expld.result.section = bfd_abs_section_ptr((asection *) &bfd_abs_section);
506 expld.result.valid_p = TRUE1;
507 }
508 break;
509 case NAME258:
510 if (expld.phase == lang_first_phase_enum)
511 ;
512 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
513 new_rel_from_abs (expld.dot);
514 else
515 {
516 struct bfd_link_hash_entry *h;
517
518 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
519 tree->name.name,
520 TRUE1, FALSE0, TRUE1);
521 if (!h)
522 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")("%P%F: bfd_link_hash_lookup failed: %E\n"));
523 else if (h->type == bfd_link_hash_defined
524 || h->type == bfd_link_hash_defweak)
525 {
526 if (bfd_is_abs_section (h->u.def.section)((h->u.def.section) == ((asection *) &bfd_abs_section)
)
)
527 new_abs (h->u.def.value);
528 else
529 {
530 asection *output_section;
531
532 output_section = h->u.def.section->output_section;
533 if (output_section == NULL((void*)0))
534 {
535 if (expld.phase != lang_mark_phase_enum)
536 einfo (_("%X%S: unresolvable symbol `%s'"("%X%S: unresolvable symbol `%s'" " referenced in expression\n"
)
537 " referenced in expression\n")("%X%S: unresolvable symbol `%s'" " referenced in expression\n"
)
,
538 tree->name.name);
539 }
540 else
541 new_rel (h->u.def.value + h->u.def.section->output_offset,
542 NULL((void*)0), output_section);
543 }
544 }
545 else if (expld.phase == lang_final_phase_enum
546 || expld.assigning_to_dot)
547 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n")("%F%S: undefined symbol `%s' referenced in expression\n"),
548 tree->name.name);
549 else if (h->type == bfd_link_hash_new)
550 {
551 h->type = bfd_link_hash_undefined;
552 h->u.undef.abfd = NULL((void*)0);
553 if (h->u.undef.next == NULL((void*)0) && h != link_info.hash->undefs_tail)
554 bfd_link_add_undef (link_info.hash, h);
555 }
556 }
557 break;
558
559 case ADDR314:
560 if (expld.phase != lang_first_phase_enum)
561 {
562 lang_output_section_statement_type *os;
563
564 os = lang_output_section_find (tree->name.name);
565 if (os != NULL((void*)0) && os->processed)
566 new_rel (0, NULL((void*)0), os->bfd_section);
567 }
568 break;
569
570 case LOADADDR315:
571 if (expld.phase != lang_first_phase_enum)
16
Assuming field 'phase' is equal to lang_first_phase_enum
17
Taking false branch
572 {
573 lang_output_section_statement_type *os;
574
575 os = lang_output_section_find (tree->name.name);
576 if (os != NULL((void*)0) && os->processed)
577 {
578 if (os->load_base == NULL((void*)0))
579 new_rel (0, NULL((void*)0), os->bfd_section);
580 else
581 exp_fold_tree_1 (os->load_base);
582 }
583 }
584 break;
18
Execution continues on line 571
585
586 case SIZEOF313:
587 if (expld.phase != lang_first_phase_enum)
588 {
589 int opb = bfd_octets_per_byte (output_bfd);
590 lang_output_section_statement_type *os;
591
592 os = lang_output_section_find (tree->name.name);
593 if (os == NULL((void*)0))
594 new_abs (0);
595 else if (os->processed)
596 new_abs (os->bfd_section->size / opb);
597 }
598 break;
599
600 case LENGTH326:
601 {
602 lang_memory_region_type *mem;
603
604 mem = lang_memory_region_lookup (tree->name.name, FALSE0);
605 if (mem != NULL((void*)0))
606 new_abs (mem->length);
607 else
608 einfo (_("%F%S: undefined MEMORY region `%s'"("%F%S: undefined MEMORY region `%s'" " referenced in expression\n"
)
609 " referenced in expression\n")("%F%S: undefined MEMORY region `%s'" " referenced in expression\n"
)
, tree->name.name);
610 }
611 break;
612
613 case ORIGIN324:
614 {
615 lang_memory_region_type *mem;
616
617 mem = lang_memory_region_lookup (tree->name.name, FALSE0);
618 if (mem != NULL((void*)0))
619 new_abs (mem->origin);
620 else
621 einfo (_("%F%S: undefined MEMORY region `%s'"("%F%S: undefined MEMORY region `%s'" " referenced in expression\n"
)
622 " referenced in expression\n")("%F%S: undefined MEMORY region `%s'" " referenced in expression\n"
)
, tree->name.name);
623 }
624 break;
625
626 default:
627 FAIL ()do { info_assert("/usr/src/gnu/usr.bin/binutils-2.17/ld/ldexp.c"
,627); } while (0)
;
628 break;
629 }
630}
631
632static void
633exp_fold_tree_1 (etree_type *tree)
634{
635 if (tree
4.1
'tree' is not equal to NULL
== NULL((void*)0)
)
5
Taking false branch
10
Assuming 'tree' is not equal to NULL
11
Taking false branch
636 {
637 memset (&expld.result, 0, sizeof (expld.result));
638 return;
639 }
640
641 switch (tree->type.node_class)
6
Control jumps to 'case etree_assign:' at line 676
12
Control jumps to 'case etree_name:' at line 764
642 {
643 case etree_value:
644 new_rel (tree->value.value, tree->value.str, expld.section);
645 break;
646
647 case etree_rel:
648 if (expld.phase != lang_first_phase_enum)
649 {
650 asection *output_section = tree->rel.section->output_section;
651 new_rel (tree->rel.value + tree->rel.section->output_offset,
652 NULL((void*)0), output_section);
653 }
654 else
655 memset (&expld.result, 0, sizeof (expld.result));
656 break;
657
658 case etree_assert:
659 exp_fold_tree_1 (tree->assert_s.child);
660 if (expld.phase == lang_final_phase_enum && !expld.result.value)
661 einfo ("%X%P: %s\n", tree->assert_s.message);
662 break;
663
664 case etree_unary:
665 fold_unary (tree);
666 break;
667
668 case etree_binary:
669 fold_binary (tree);
670 break;
671
672 case etree_trinary:
673 fold_trinary (tree);
674 break;
675
676 case etree_assign:
677 case etree_provide:
678 case etree_provided:
679 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
7
Assuming the condition is false
680 {
681 /* Assignment to dot can only be done during allocation. */
682 if (tree->type.node_class != etree_assign)
683 einfo (_("%F%S can not PROVIDE assignment to location counter\n")("%F%S can not PROVIDE assignment to location counter\n"));
684 if (expld.phase == lang_mark_phase_enum
685 || expld.phase == lang_allocating_phase_enum
686 || (expld.phase == lang_final_phase_enum
687 && expld.section == bfd_abs_section_ptr((asection *) &bfd_abs_section)))
688 {
689 /* Notify the folder that this is an assignment to dot. */
690 expld.assigning_to_dot = TRUE1;
691 exp_fold_tree_1 (tree->assign.src);
692 expld.assigning_to_dot = FALSE0;
693
694 if (!expld.result.valid_p)
695 {
696 if (expld.phase != lang_mark_phase_enum)
697 einfo (_("%F%S invalid assignment to location counter\n")("%F%S invalid assignment to location counter\n"));
698 }
699 else if (expld.dotp == NULL((void*)0))
700 einfo (_("%F%S assignment to location counter"("%F%S assignment to location counter" " invalid outside of SECTION\n"
)
701 " invalid outside of SECTION\n")("%F%S assignment to location counter" " invalid outside of SECTION\n"
)
);
702 else
703 {
704 bfd_vma nextdot;
705
706 nextdot = expld.result.value + expld.section->vma;
707 if (nextdot < expld.dot
708 && expld.section != bfd_abs_section_ptr((asection *) &bfd_abs_section))
709 einfo (_("%F%S cannot move location counter backwards"("%F%S cannot move location counter backwards" " (from %V to %V)\n"
)
710 " (from %V to %V)\n")("%F%S cannot move location counter backwards" " (from %V to %V)\n"
)
, expld.dot, nextdot);
711 else
712 {
713 expld.dot = nextdot;
714 *expld.dotp = nextdot;
715 }
716 }
717 }
718 else
719 memset (&expld.result, 0, sizeof (expld.result));
720 }
721 else
722 {
723 struct bfd_link_hash_entry *h = NULL((void*)0);
724
725 if (tree->type.node_class
7.1
Field 'node_class' is not equal to etree_provide
== etree_provide)
8
Taking false branch
726 {
727 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
728 FALSE0, FALSE0, TRUE1);
729 if (h == NULL((void*)0)
730 || (h->type != bfd_link_hash_new
731 && h->type != bfd_link_hash_undefined
732 && h->type != bfd_link_hash_common))
733 {
734 /* Do nothing. The symbol was never referenced, or was
735 defined by some object. */
736 break;
737 }
738 }
739
740 exp_fold_tree_1 (tree->assign.src);
9
Calling 'exp_fold_tree_1'
21
Returning from 'exp_fold_tree_1'
741 if (expld.result.valid_p)
22
Assuming field 'valid_p' is not equal to 0
23
Taking true branch
742 {
743 if (h
23.1
'h' is equal to NULL
== NULL((void*)0))
24
Taking true branch
744 {
745 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
25
Value assigned to 'h'
746 TRUE1, FALSE0, TRUE1);
747 if (h == NULL((void*)0))
26
Assuming 'h' is equal to NULL
27
Assuming pointer value is null
28
Taking true branch
748 einfo (_("%P%F:%s: hash creation failed\n")("%P%F:%s: hash creation failed\n"),
749 tree->assign.dst);
750 }
751
752 /* FIXME: Should we worry if the symbol is already
753 defined? */
754 lang_update_definedness (tree->assign.dst, h);
755 h->type = bfd_link_hash_defined;
29
Access to field 'type' results in a dereference of a null pointer (loaded from variable 'h')
756 h->u.def.value = expld.result.value;
757 h->u.def.section = expld.result.section;
758 if (tree->type.node_class == etree_provide)
759 tree->type.node_class = etree_provided;
760 }
761 }
762 break;
763
764 case etree_name:
765 fold_name (tree);
13
Calling 'fold_name'
19
Returning from 'fold_name'
766 break;
20
Execution continues on line 765
767
768 default:
769 FAIL ()do { info_assert("/usr/src/gnu/usr.bin/binutils-2.17/ld/ldexp.c"
,769); } while (0)
;
770 memset (&expld.result, 0, sizeof (expld.result));
771 break;
772 }
773}
774
775void
776exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
777{
778 expld.dot = *dotp;
779 expld.dotp = dotp;
780 expld.section = current_section;
781 exp_fold_tree_1 (tree);
782}
783
784static void
785exp_fold_tree_no_dot (etree_type *tree)
786{
787 expld.dot = 0;
788 expld.dotp = NULL((void*)0);
789 expld.section = bfd_abs_section_ptr((asection *) &bfd_abs_section);
790 exp_fold_tree_1 (tree);
4
Calling 'exp_fold_tree_1'
791}
792
793etree_type *
794exp_binop (int code, etree_type *lhs, etree_type *rhs)
795{
796 etree_type value, *new;
797
798 value.type.node_code = code;
799 value.binary.lhs = lhs;
800 value.binary.rhs = rhs;
801 value.type.node_class = etree_binary;
802 exp_fold_tree_no_dot (&value);
803 if (expld.result.valid_p)
804 return exp_intop (expld.result.value);
805
806 new = stat_alloc (sizeof (new->binary));
807 memcpy (new, &value, sizeof (new->binary));
808 return new;
809}
810
811etree_type *
812exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
813{
814 etree_type value, *new;
815
816 value.type.node_code = code;
817 value.trinary.lhs = lhs;
818 value.trinary.cond = cond;
819 value.trinary.rhs = rhs;
820 value.type.node_class = etree_trinary;
821 exp_fold_tree_no_dot (&value);
822 if (expld.result.valid_p)
823 return exp_intop (expld.result.value);
824
825 new = stat_alloc (sizeof (new->trinary));
826 memcpy (new, &value, sizeof (new->trinary));
827 return new;
828}
829
830etree_type *
831exp_unop (int code, etree_type *child)
832{
833 etree_type value, *new;
834
835 value.unary.type.node_code = code;
836 value.unary.child = child;
837 value.unary.type.node_class = etree_unary;
838 exp_fold_tree_no_dot (&value);
839 if (expld.result.valid_p)
840 return exp_intop (expld.result.value);
841
842 new = stat_alloc (sizeof (new->unary));
843 memcpy (new, &value, sizeof (new->unary));
844 return new;
845}
846
847etree_type *
848exp_nameop (int code, const char *name)
849{
850 etree_type value, *new;
851
852 value.name.type.node_code = code;
853 value.name.name = name;
854 value.name.type.node_class = etree_name;
855
856 exp_fold_tree_no_dot (&value);
857 if (expld.result.valid_p)
858 return exp_intop (expld.result.value);
859
860 new = stat_alloc (sizeof (new->name));
861 memcpy (new, &value, sizeof (new->name));
862 return new;
863
864}
865
866etree_type *
867exp_assop (int code, const char *dst, etree_type *src)
868{
869 etree_type *new;
870
871 new = stat_alloc (sizeof (new->assign));
872 new->type.node_code = code;
873 new->type.node_class = etree_assign;
874 new->assign.src = src;
875 new->assign.dst = dst;
876 return new;
877}
878
879/* Handle PROVIDE. */
880
881etree_type *
882exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
883{
884 etree_type *n;
885
886 n = stat_alloc (sizeof (n->assign));
887 n->assign.type.node_code = '=';
888 n->assign.type.node_class = etree_provide;
889 n->assign.src = src;
890 n->assign.dst = dst;
891 n->assign.hidden = hidden;
892 return n;
893}
894
895/* Handle ASSERT. */
896
897etree_type *
898exp_assert (etree_type *exp, const char *message)
899{
900 etree_type *n;
901
902 n = stat_alloc (sizeof (n->assert_s));
903 n->assert_s.type.node_code = '!';
904 n->assert_s.type.node_class = etree_assert;
905 n->assert_s.child = exp;
906 n->assert_s.message = message;
907 return n;
908}
909
910void
911exp_print_tree (etree_type *tree)
912{
913 if (config.map_file == NULL((void*)0))
914 config.map_file = stderr(&__sF[2]);
915
916 if (tree == NULL((void*)0))
917 {
918 minfo ("NULL TREE\n");
919 return;
920 }
921
922 switch (tree->type.node_class)
923 {
924 case etree_value:
925 minfo ("0x%v", tree->value.value);
926 return;
927 case etree_rel:
928 if (tree->rel.section->owner != NULL((void*)0))
929 minfo ("%B:", tree->rel.section->owner);
930 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
931 return;
932 case etree_assign:
933 fprintf (config.map_file, "%s", tree->assign.dst);
934 exp_print_token (tree->type.node_code, TRUE1);
935 exp_print_tree (tree->assign.src);
936 break;
937 case etree_provide:
938 case etree_provided:
939 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
940 exp_print_tree (tree->assign.src);
941 fprintf (config.map_file, ")");
942 break;
943 case etree_binary:
944 fprintf (config.map_file, "(");
945 exp_print_tree (tree->binary.lhs);
946 exp_print_token (tree->type.node_code, TRUE1);
947 exp_print_tree (tree->binary.rhs);
948 fprintf (config.map_file, ")");
949 break;
950 case etree_trinary:
951 exp_print_tree (tree->trinary.cond);
952 fprintf (config.map_file, "?");
953 exp_print_tree (tree->trinary.lhs);
954 fprintf (config.map_file, ":");
955 exp_print_tree (tree->trinary.rhs);
956 break;
957 case etree_unary:
958 exp_print_token (tree->unary.type.node_code, FALSE0);
959 if (tree->unary.child)
960 {
961 fprintf (config.map_file, " (");
962 exp_print_tree (tree->unary.child);
963 fprintf (config.map_file, ")");
964 }
965 break;
966
967 case etree_assert:
968 fprintf (config.map_file, "ASSERT (");
969 exp_print_tree (tree->assert_s.child);
970 fprintf (config.map_file, ", %s)", tree->assert_s.message);
971 break;
972
973 case etree_name:
974 if (tree->type.node_code == NAME258)
975 {
976 fprintf (config.map_file, "%s", tree->name.name);
977 }
978 else
979 {
980 exp_print_token (tree->type.node_code, FALSE0);
981 if (tree->name.name)
982 fprintf (config.map_file, " (%s)", tree->name.name);
983 }
984 break;
985 default:
986 FAIL ()do { info_assert("/usr/src/gnu/usr.bin/binutils-2.17/ld/ldexp.c"
,986); } while (0)
;
987 break;
988 }
989}
990
991bfd_vma
992exp_get_vma (etree_type *tree, bfd_vma def, char *name)
993{
994 if (tree != NULL((void*)0))
995 {
996 exp_fold_tree_no_dot (tree);
997 if (expld.result.valid_p)
998 return expld.result.value;
999 else if (name != NULL((void*)0) && expld.phase != lang_mark_phase_enum)
1000 einfo (_("%F%S nonconstant expression for %s\n")("%F%S nonconstant expression for %s\n"), name);
1001 }
1002 return def;
1003}
1004
1005int
1006exp_get_value_int (etree_type *tree, int def, char *name)
1007{
1008 return exp_get_vma (tree, def, name);
1009}
1010
1011fill_type *
1012exp_get_fill (etree_type *tree, fill_type *def, char *name)
1013{
1014 fill_type *fill;
1015 size_t len;
1016 unsigned int val;
1017
1018 if (tree == NULL((void*)0))
1019 return def;
1020
1021 exp_fold_tree_no_dot (tree);
1022 if (!expld.result.valid_p)
1023 {
1024 if (name != NULL((void*)0) && expld.phase != lang_mark_phase_enum)
1025 einfo (_("%F%S nonconstant expression for %s\n")("%F%S nonconstant expression for %s\n"), name);
1026 return def;
1027 }
1028
1029 if (expld.result.str != NULL((void*)0) && (len = strlen (expld.result.str)) != 0)
1030 {
1031 unsigned char *dst;
1032 unsigned char *s;
1033 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1034 fill->size = (len + 1) / 2;
1035 dst = fill->data;
1036 s = (unsigned char *) expld.result.str;
1037 val = 0;
1038 do
1039 {
1040 unsigned int digit;
1041
1042 digit = *s++ - '0';
1043 if (digit > 9)
1044 digit = (digit - 'A' + '0' + 10) & 0xf;
1045 val <<= 4;
1046 val += digit;
1047 --len;
1048 if ((len & 1) == 0)
1049 {
1050 *dst++ = val;
1051 val = 0;
1052 }
1053 }
1054 while (len != 0);
1055 }
1056 else
1057 {
1058 fill = xmalloc (4 + sizeof (*fill) - 1);
1059 val = expld.result.value;
1060 fill->data[0] = (val >> 24) & 0xff;
1061 fill->data[1] = (val >> 16) & 0xff;
1062 fill->data[2] = (val >> 8) & 0xff;
1063 fill->data[3] = (val >> 0) & 0xff;
1064 fill->size = 4;
1065 }
1066 return fill;
1067}
1068
1069bfd_vma
1070exp_get_abs_int (etree_type *tree, int def, char *name)
1071{
1072 if (tree != NULL((void*)0))
1
Assuming 'tree' is not equal to NULL
2
Taking true branch
1073 {
1074 exp_fold_tree_no_dot (tree);
3
Calling 'exp_fold_tree_no_dot'
1075
1076 if (expld.result.valid_p)
1077 {
1078 expld.result.value += expld.result.section->vma;
1079 return expld.result.value;
1080 }
1081 else if (name != NULL((void*)0) && expld.phase != lang_mark_phase_enum)
1082 einfo (_("%F%S non constant expression for %s\n")("%F%S non constant expression for %s\n"), name);
1083 }
1084 return def;
1085}
1086
1087static bfd_vma
1088align_n (bfd_vma value, bfd_vma align)
1089{
1090 if (align <= 1)
1091 return value;
1092
1093 value = (value + align - 1) / align;
1094 return value * align;
1095}