1 /* Find a variable's value in memory, for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "user-regs.h"
34 #include "gdbsupport/selftest.h"
36 /* Basic byte-swapping routines. All 'extract' functions return a
37 host-format integer from a target-format integer at ADDR which is
40 #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
41 /* 8 bit characters are a pretty safe assumption these days, so we
42 assume it throughout all these swapping routines. If we had to deal with
43 9 bit characters, we would need to make len be in bits and would have
44 to re-write these routines... */
48 template<typename T
, typename
>
50 extract_integer (gdb::array_view
<const gdb_byte
> buf
, enum bfd_endian byte_order
)
52 typename
std::make_unsigned
<T
>::type retval
= 0;
54 if (buf
.size () > (int) sizeof (T
))
56 That operation is not available on integers of more than %d bytes."),
59 /* Start at the most significant end of the integer, and work towards
60 the least significant. */
61 if (byte_order
== BFD_ENDIAN_BIG
)
65 if (std::is_signed
<T
>::value
)
67 /* Do the sign extension once at the start. */
68 retval
= ((LONGEST
) buf
[i
] ^ 0x80) - 0x80;
71 for (; i
< buf
.size (); ++i
)
72 retval
= (retval
<< 8) | buf
[i
];
76 ssize_t i
= buf
.size () - 1;
78 if (std::is_signed
<T
>::value
)
80 /* Do the sign extension once at the start. */
81 retval
= ((LONGEST
) buf
[i
] ^ 0x80) - 0x80;
85 retval
= (retval
<< 8) | buf
[i
];
90 /* Explicit instantiations. */
91 template LONGEST extract_integer
<LONGEST
> (gdb::array_view
<const gdb_byte
> buf
,
92 enum bfd_endian byte_order
);
93 template ULONGEST extract_integer
<ULONGEST
>
94 (gdb::array_view
<const gdb_byte
> buf
, enum bfd_endian byte_order
);
96 /* Sometimes a long long unsigned integer can be extracted as a
97 LONGEST value. This is done so that we can print these values
98 better. If this integer can be converted to a LONGEST, this
99 function returns 1 and sets *PVAL. Otherwise it returns 0. */
102 extract_long_unsigned_integer (const gdb_byte
*addr
, int orig_len
,
103 enum bfd_endian byte_order
, LONGEST
*pval
)
106 const gdb_byte
*first_addr
;
110 if (byte_order
== BFD_ENDIAN_BIG
)
113 len
> (int) sizeof (LONGEST
) && p
< addr
+ orig_len
;
126 for (p
= addr
+ orig_len
- 1;
127 len
> (int) sizeof (LONGEST
) && p
>= addr
;
137 if (len
<= (int) sizeof (LONGEST
))
139 *pval
= (LONGEST
) extract_unsigned_integer (first_addr
,
149 /* Treat the bytes at BUF as a pointer of type TYPE, and return the
150 address it represents. */
152 extract_typed_address (const gdb_byte
*buf
, struct type
*type
)
154 gdb_assert (type
->is_pointer_or_reference ());
155 return gdbarch_pointer_to_address (type
->arch (), type
, buf
);
158 /* All 'store' functions accept a host-format integer and store a
159 target-format integer at ADDR which is LEN bytes long. */
160 template<typename T
, typename
>
162 store_integer (gdb::array_view
<gdb_byte
> dst
, enum bfd_endian byte_order
,
166 gdb_byte
*startaddr
= dst
.data ();
167 gdb_byte
*endaddr
= startaddr
+ dst
.size ();
169 /* Start at the least significant end of the integer, and work towards
170 the most significant. */
171 if (byte_order
== BFD_ENDIAN_BIG
)
173 for (p
= endaddr
- 1; p
>= startaddr
; --p
)
181 for (p
= startaddr
; p
< endaddr
; ++p
)
189 /* Explicit instantiations. */
190 template void store_integer (gdb::array_view
<gdb_byte
> dst
,
191 bfd_endian byte_order
, LONGEST val
);
193 template void store_integer (gdb::array_view
<gdb_byte
> dst
,
194 bfd_endian byte_order
, ULONGEST val
);
196 /* Store the address ADDR as a pointer of type TYPE at BUF, in target
199 store_typed_address (gdb_byte
*buf
, struct type
*type
, CORE_ADDR addr
)
201 gdb_assert (type
->is_pointer_or_reference ());
202 gdbarch_address_to_pointer (type
->arch (), type
, buf
, addr
);
205 /* Copy a value from SOURCE of size SOURCE_SIZE bytes to DEST of size DEST_SIZE
206 bytes. If SOURCE_SIZE is greater than DEST_SIZE, then truncate the most
207 significant bytes. If SOURCE_SIZE is less than DEST_SIZE then either sign
208 or zero extended according to IS_SIGNED. Values are stored in memory with
209 endianness BYTE_ORDER. */
212 copy_integer_to_size (gdb_byte
*dest
, int dest_size
, const gdb_byte
*source
,
213 int source_size
, bool is_signed
,
214 enum bfd_endian byte_order
)
216 signed int size_diff
= dest_size
- source_size
;
218 /* Copy across everything from SOURCE that can fit into DEST. */
220 if (byte_order
== BFD_ENDIAN_BIG
&& size_diff
> 0)
221 memcpy (dest
+ size_diff
, source
, source_size
);
222 else if (byte_order
== BFD_ENDIAN_BIG
&& size_diff
< 0)
223 memcpy (dest
, source
- size_diff
, dest_size
);
225 memcpy (dest
, source
, std::min (source_size
, dest_size
));
227 /* Fill the remaining space in DEST by either zero extending or sign
232 gdb_byte extension
= 0;
234 && ((byte_order
!= BFD_ENDIAN_BIG
&& source
[source_size
- 1] & 0x80)
235 || (byte_order
== BFD_ENDIAN_BIG
&& source
[0] & 0x80)))
238 /* Extend into MSBs of SOURCE. */
239 if (byte_order
== BFD_ENDIAN_BIG
)
240 memset (dest
, extension
, size_diff
);
242 memset (dest
+ source_size
, extension
, size_diff
);
249 value_of_register (int regnum
, const frame_info_ptr
&next_frame
)
251 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
253 /* User registers lie completely outside of the range of normal
254 registers. Catch them early so that the target never sees them. */
255 if (regnum
>= gdbarch_num_cooked_regs (gdbarch
))
256 return value_of_user_reg (regnum
, get_prev_frame_always (next_frame
));
258 value
*reg_val
= value_of_register_lazy (next_frame
, regnum
);
259 reg_val
->fetch_lazy ();
266 value_of_register_lazy (const frame_info_ptr
&next_frame
, int regnum
)
268 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
270 gdb_assert (regnum
< gdbarch_num_cooked_regs (gdbarch
));
271 gdb_assert (next_frame
!= nullptr);
273 return value::allocate_register_lazy (next_frame
, regnum
);
276 /* Given a pointer of type TYPE in target form in BUF, return the
277 address it represents. */
279 unsigned_pointer_to_address (struct gdbarch
*gdbarch
,
280 struct type
*type
, const gdb_byte
*buf
)
282 enum bfd_endian byte_order
= type_byte_order (type
);
284 return extract_unsigned_integer (buf
, type
->length (), byte_order
);
288 signed_pointer_to_address (struct gdbarch
*gdbarch
,
289 struct type
*type
, const gdb_byte
*buf
)
291 enum bfd_endian byte_order
= type_byte_order (type
);
293 return extract_signed_integer (buf
, type
->length (), byte_order
);
296 /* Given an address, store it as a pointer of type TYPE in target
299 unsigned_address_to_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
300 gdb_byte
*buf
, CORE_ADDR addr
)
302 enum bfd_endian byte_order
= type_byte_order (type
);
304 store_unsigned_integer (buf
, type
->length (), byte_order
, addr
);
308 address_to_signed_pointer (struct gdbarch
*gdbarch
, struct type
*type
,
309 gdb_byte
*buf
, CORE_ADDR addr
)
311 enum bfd_endian byte_order
= type_byte_order (type
);
313 store_signed_integer (buf
, type
->length (), byte_order
, addr
);
318 enum symbol_needs_kind
319 symbol_read_needs (struct symbol
*sym
)
321 if (const symbol_computed_ops
*computed_ops
= sym
->computed_ops ();
322 computed_ops
!= nullptr)
323 return computed_ops
->get_symbol_read_needs (sym
);
325 switch (sym
->aclass ())
327 /* All cases listed explicitly so that gcc -Wall will detect it if
328 we failed to consider one. */
330 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
335 case LOC_REGPARM_ADDR
:
337 return SYMBOL_NEEDS_FRAME
;
345 /* Getting the address of a label can be done independently of the block,
346 even if some *uses* of that address wouldn't work so well without
350 case LOC_CONST_BYTES
:
352 case LOC_OPTIMIZED_OUT
:
353 return SYMBOL_NEEDS_NONE
;
355 return SYMBOL_NEEDS_FRAME
;
361 symbol_read_needs_frame (struct symbol
*sym
)
363 return symbol_read_needs (sym
) == SYMBOL_NEEDS_FRAME
;
366 /* Assuming VAR is a symbol that can be reached from FRAME thanks to lexical
367 rules, look for the frame that is actually hosting VAR and return it. If,
368 for some reason, we found no such frame, return NULL.
370 This kind of computation is necessary to correctly handle lexically nested
373 Note that in some cases, we know what scope VAR comes from but we cannot
374 reach the specific frame that hosts the instance of VAR we are looking for.
375 For backward compatibility purposes (with old compilers), we then look for
376 the first frame that can host it. */
378 static frame_info_ptr
379 get_hosting_frame (struct symbol
*var
, const struct block
*var_block
,
380 const frame_info_ptr
&initial_frame
)
382 const struct block
*frame_block
= NULL
;
384 if (!symbol_read_needs_frame (var
))
387 /* Some symbols for local variables have no block: this happens when they are
388 not produced by a debug information reader, for instance when GDB creates
389 synthetic symbols. Without block information, we must assume they are
390 local to FRAME. In this case, there is nothing to do. */
391 else if (var_block
== NULL
)
392 return initial_frame
;
394 /* We currently assume that all symbols with a location list need a frame.
395 This is true in practice because selecting the location description
396 requires to compute the CFA, hence requires a frame. However we have
397 tests that embed global/static symbols with null location lists.
398 We want to get <optimized out> instead of <frame required> when evaluating
399 them so return a frame instead of raising an error. */
400 else if (var_block
->is_global_block () || var_block
->is_static_block ())
401 return initial_frame
;
403 /* We have to handle the "my_func::my_local_var" notation. This requires us
404 to look for upper frames when we find no block for the current frame: here
405 and below, handle when frame_block == NULL. */
406 if (initial_frame
!= nullptr)
407 frame_block
= get_frame_block (initial_frame
, NULL
);
409 /* Climb up the call stack until reaching the frame we are looking for. */
410 frame_info_ptr frame
= initial_frame
;
411 while (frame
!= NULL
&& frame_block
!= var_block
)
413 /* Stacks can be quite deep: give the user a chance to stop this. */
416 if (frame_block
== NULL
)
418 frame
= get_prev_frame (frame
);
421 frame_block
= get_frame_block (frame
, NULL
);
424 /* If we failed to find the proper frame, fallback to the heuristic
426 else if (frame_block
->is_global_block ())
432 /* Assuming we have a block for this frame: if we are at the function
433 level, the immediate upper lexical block is in an outer function:
434 follow the static link. */
435 else if (frame_block
->function () != nullptr)
437 frame
= frame_follow_static_link (frame
);
438 if (frame
!= nullptr)
440 frame_block
= get_frame_block (frame
, nullptr);
441 if (frame_block
== nullptr)
447 /* We must be in some function nested lexical block. Just get the
448 outer block: both must share the same frame. */
449 frame_block
= frame_block
->superblock ();
452 /* Old compilers may not provide a static link, or they may provide an
453 invalid one. For such cases, fallback on the old way to evaluate
454 non-local references: just climb up the call stack and pick the first
455 frame that contains the variable we are looking for. */
458 frame
= block_innermost_frame (var_block
);
461 if (var_block
->function ()
462 && !var_block
->inlined_p ()
463 && var_block
->function ()->print_name ())
464 error (_("No frame is currently executing in block %s."),
465 var_block
->function ()->print_name ());
467 error (_("No frame is currently executing in specified"
475 /* See language.h. */
478 language_defn::read_var_value (struct symbol
*var
,
479 const struct block
*var_block
,
480 const frame_info_ptr
&frame_param
) const
483 struct type
*type
= var
->type ();
485 enum symbol_needs_kind sym_need
;
486 frame_info_ptr frame
= frame_param
;
488 /* Call check_typedef on our type to make sure that, if TYPE is
489 a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
490 instead of zero. However, we do not replace the typedef type by the
491 target type, because we want to keep the typedef in order to be able to
492 set the returned value type description correctly. */
493 check_typedef (type
);
495 sym_need
= symbol_read_needs (var
);
496 if (sym_need
== SYMBOL_NEEDS_FRAME
)
497 gdb_assert (frame
!= NULL
);
498 else if (sym_need
== SYMBOL_NEEDS_REGISTERS
&& !target_has_registers ())
499 error (_("Cannot read `%s' without registers"), var
->print_name ());
502 frame
= get_hosting_frame (var
, var_block
, frame
);
504 if (const symbol_computed_ops
*computed_ops
= var
->computed_ops ())
505 return computed_ops
->read_variable (var
, frame
);
507 switch (var
->aclass ())
510 if (is_dynamic_type (type
))
512 gdb_byte bytes
[sizeof (LONGEST
)];
514 size_t len
= std::min (sizeof (LONGEST
), (size_t) type
->length ());
515 store_unsigned_integer (bytes
, len
,
516 type_byte_order (type
),
517 var
->value_longest ());
518 gdb::array_view
<const gdb_byte
> view (bytes
, len
);
520 /* Value is a constant byte-sequence. */
521 type
= resolve_dynamic_type (type
, view
, /* Unused address. */ 0);
523 /* Put the constant back in target format. */
524 v
= value::allocate (type
);
525 store_signed_integer (v
->contents_raw ().data (), type
->length (),
526 type_byte_order (type
), var
->value_longest ());
527 v
->set_lval (not_lval
);
532 /* Put the constant back in target format. */
533 if (overlay_debugging
)
535 struct objfile
*var_objfile
= var
->objfile ();
536 addr
= symbol_overlayed_address (var
->value_address (),
537 var
->obj_section (var_objfile
));
540 addr
= var
->value_address ();
542 /* First convert the CORE_ADDR to a function pointer type, this
543 ensures the gdbarch knows what type of pointer we are
544 manipulating when value_from_pointer is called. */
545 type
= builtin_type (var
->arch ())->builtin_func_ptr
;
546 v
= value_from_pointer (type
, addr
);
548 /* But we want to present the value as 'void *', so cast it to the
549 required type now, this will not change the values bit
551 struct type
*void_ptr_type
552 = builtin_type (var
->arch ())->builtin_data_ptr
;
553 v
= value_cast_pointers (void_ptr_type
, v
, 0);
554 v
->set_lval (not_lval
);
558 case LOC_CONST_BYTES
:
559 if (is_dynamic_type (type
))
561 gdb::array_view
<const gdb_byte
> view (var
->value_bytes (),
564 /* Value is a constant byte-sequence. */
565 type
= resolve_dynamic_type (type
, view
, /* Unused address. */ 0);
567 v
= value::allocate (type
);
568 memcpy (v
->contents_raw ().data (), var
->value_bytes (),
570 v
->set_lval (not_lval
);
574 if (overlay_debugging
)
576 = symbol_overlayed_address (var
->value_address (),
577 var
->obj_section (var
->objfile ()));
579 addr
= var
->value_address ();
583 addr
= get_frame_args_address (frame
);
585 error (_("Unknown argument list address for `%s'."),
587 addr
+= var
->value_longest ();
595 argref
= get_frame_args_address (frame
);
597 error (_("Unknown argument list address for `%s'."),
599 argref
+= var
->value_longest ();
600 ref
= value_at (lookup_pointer_type (type
), argref
);
601 addr
= value_as_address (ref
);
606 addr
= get_frame_locals_address (frame
);
607 addr
+= var
->value_longest ();
611 error (_("Cannot look up value of a typedef `%s'."),
616 if (overlay_debugging
)
617 addr
= symbol_overlayed_address
618 (var
->value_block ()->entry_pc (),
619 var
->obj_section (var
->objfile ()));
621 addr
= var
->value_block ()->entry_pc ();
625 case LOC_REGPARM_ADDR
:
627 const symbol_register_ops
*reg_ops
= var
->register_ops ();
628 int regno
= reg_ops
->register_number (var
, get_frame_arch (frame
));
630 if (var
->aclass () == LOC_REGPARM_ADDR
)
631 addr
= value_as_address
632 (value_from_register (lookup_pointer_type (type
), regno
, frame
));
634 return value_from_register (type
, regno
, frame
);
639 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
643 struct obj_section
*obj_section
;
644 bound_minimal_symbol bmsym
;
646 gdbarch_iterate_over_objfiles_in_search_order
648 [var
, &bmsym
] (objfile
*objfile
)
650 bmsym
= lookup_minimal_symbol (var
->linkage_name (), nullptr,
653 /* Stop if a match is found. */
654 return bmsym
.minsym
!= nullptr;
658 /* If we can't find the minsym there's a problem in the symbol info.
659 The symbol exists in the debug info, but it's missing in the minsym
661 if (bmsym
.minsym
== nullptr)
663 const char *flavour_name
664 = objfile_flavour_name (var
->objfile ());
666 /* We can't get here unless we've opened the file, so flavour_name
668 gdb_assert (flavour_name
!= NULL
);
669 error (_("Missing %s symbol \"%s\"."),
670 flavour_name
, var
->linkage_name ());
673 obj_section
= bmsym
.minsym
->obj_section (bmsym
.objfile
);
674 /* Relocate address, unless there is no section or the variable is
676 if (obj_section
== NULL
677 || (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
678 addr
= CORE_ADDR (bmsym
.minsym
->unrelocated_address ());
680 addr
= bmsym
.value_address ();
681 if (overlay_debugging
)
682 addr
= symbol_overlayed_address (addr
, obj_section
);
683 /* Determine address of TLS variable. */
685 && (obj_section
->the_bfd_section
->flags
& SEC_THREAD_LOCAL
) != 0)
686 addr
= target_translate_tls_address (obj_section
->objfile
, addr
);
690 case LOC_OPTIMIZED_OUT
:
691 if (is_dynamic_type (type
))
692 type
= resolve_dynamic_type (type
, {}, /* Unused address. */ 0);
693 return value::allocate_optimized_out (type
);
696 error (_("Cannot look up value of a botched symbol `%s'."),
701 v
= value_at_lazy (type
, addr
);
705 /* Calls VAR's language read_var_value hook with the given arguments. */
708 read_var_value (struct symbol
*var
, const struct block
*var_block
,
709 const frame_info_ptr
&frame
)
711 const struct language_defn
*lang
= language_def (var
->language ());
713 gdb_assert (lang
!= NULL
);
715 return lang
->read_var_value (var
, var_block
, frame
);
718 /* Install default attributes for register values. */
721 default_value_from_register (gdbarch
*gdbarch
, type
*type
, int regnum
,
722 const frame_info_ptr
&this_frame
)
725 = value::allocate_register (get_next_frame_sentinel_okay (this_frame
),
728 /* Any structure stored in more than one register will always be
729 an integral number of registers. Otherwise, you need to do
730 some fiddling with the last register copied here for little
732 if (type_byte_order (type
) == BFD_ENDIAN_BIG
733 && type
->length () < register_size (gdbarch
, regnum
))
734 /* Big-endian, and we want less than full size. */
735 value
->set_offset (register_size (gdbarch
, regnum
) - type
->length ());
737 value
->set_offset (0);
742 /* VALUE must be an lval_register value. If regnum is the value's
743 associated register number, and len the length of the value's type,
744 read one or more registers in VALUE's frame, starting with register REGNUM,
745 until we've read LEN bytes.
747 If any of the registers we try to read are optimized out, then mark the
748 complete resulting value as optimized out. */
751 read_frame_register_value (value
*value
)
753 gdb_assert (value
->lval () == lval_register
);
755 frame_info_ptr next_frame
= frame_find_by_id (value
->next_frame_id ());
756 gdb_assert (next_frame
!= nullptr);
758 gdbarch
*gdbarch
= frame_unwind_arch (next_frame
);
760 LONGEST reg_offset
= value
->offset ();
761 int regnum
= value
->regnum ();
762 int len
= type_length_units (check_typedef (value
->type ()));
764 /* Skip registers wholly inside of REG_OFFSET. */
765 while (reg_offset
>= register_size (gdbarch
, regnum
))
767 reg_offset
-= register_size (gdbarch
, regnum
);
774 struct value
*regval
= frame_unwind_register_value (next_frame
, regnum
);
775 int reg_len
= type_length_units (regval
->type ()) - reg_offset
;
777 /* If the register length is larger than the number of bytes
778 remaining to copy, then only copy the appropriate bytes. */
782 regval
->contents_copy (value
, offset
, reg_offset
, reg_len
);
791 /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME. */
794 value_from_register (struct type
*type
, int regnum
, const frame_info_ptr
&frame
)
796 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
797 struct type
*type1
= check_typedef (type
);
800 if (gdbarch_convert_register_p (gdbarch
, regnum
, type1
))
802 int optim
, unavail
, ok
;
804 /* The ISA/ABI need to something weird when obtaining the
805 specified value from this register. It might need to
806 re-order non-adjacent, starting with REGNUM (see MIPS and
807 i386). It might need to convert the [float] register into
808 the corresponding [integer] type (see Alpha). The assumption
809 is that gdbarch_register_to_value populates the entire value
810 including the location. */
811 v
= value::allocate_register (get_next_frame_sentinel_okay (frame
),
813 ok
= gdbarch_register_to_value (gdbarch
, frame
, regnum
, type1
,
814 v
->contents_raw ().data (), &optim
,
820 v
->mark_bytes_optimized_out (0, type
->length ());
822 v
->mark_bytes_unavailable (0, type
->length ());
827 /* Construct the value. */
828 v
= gdbarch_value_from_register (gdbarch
, type
, regnum
, frame
);
831 read_frame_register_value (v
);
837 /* Return contents of register REGNUM in frame FRAME as address.
838 Will abort if register value is not available. */
841 address_from_register (int regnum
, const frame_info_ptr
&frame
)
843 type
*type
= builtin_type (get_frame_arch (frame
))->builtin_data_ptr
;
844 value_ref_ptr v
= release_value (value_from_register (type
, regnum
, frame
));
846 if (v
->optimized_out ())
848 /* This function is used while computing a location expression.
849 Complain about the value being optimized out, rather than
850 letting value_as_address complain about some random register
851 the expression depends on not being saved. */
852 error_value_optimized_out ();
855 return value_as_address (v
.get ());
859 namespace selftests
{
860 namespace findvar_tests
{
862 /* Function to test copy_integer_to_size. Store SOURCE_VAL with size
863 SOURCE_SIZE to a buffer, making sure no sign extending happens at this
864 stage. Copy buffer to a new buffer using copy_integer_to_size. Extract
865 copied value and compare to DEST_VALU. Copy again with a signed
866 copy_integer_to_size and compare to DEST_VALS. Do everything for both
867 LITTLE and BIG target endians. Use unsigned values throughout to make
868 sure there are no implicit sign extensions. */
871 do_cint_test (ULONGEST dest_valu
, ULONGEST dest_vals
, int dest_size
,
872 ULONGEST src_val
, int src_size
)
874 for (int i
= 0; i
< 2 ; i
++)
876 gdb_byte srcbuf
[sizeof (ULONGEST
)] = {};
877 gdb_byte destbuf
[sizeof (ULONGEST
)] = {};
878 enum bfd_endian byte_order
= i
? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
880 /* Fill the src buffer (and later the dest buffer) with non-zero junk,
881 to ensure zero extensions aren't hidden. */
882 memset (srcbuf
, 0xaa, sizeof (srcbuf
));
884 /* Store (and later extract) using unsigned to ensure there are no sign
886 store_unsigned_integer (srcbuf
, src_size
, byte_order
, src_val
);
889 memset (destbuf
, 0xaa, sizeof (destbuf
));
890 copy_integer_to_size (destbuf
, dest_size
, srcbuf
, src_size
, false,
892 SELF_CHECK (dest_valu
== extract_unsigned_integer (destbuf
, dest_size
,
896 memset (destbuf
, 0xaa, sizeof (destbuf
));
897 copy_integer_to_size (destbuf
, dest_size
, srcbuf
, src_size
, true,
899 SELF_CHECK (dest_vals
== extract_unsigned_integer (destbuf
, dest_size
,
905 copy_integer_to_size_test ()
907 /* Destination is bigger than the source, which has the signed bit unset. */
908 do_cint_test (0x12345678, 0x12345678, 8, 0x12345678, 4);
909 do_cint_test (0x345678, 0x345678, 8, 0x12345678, 3);
911 /* Destination is bigger than the source, which has the signed bit set. */
912 do_cint_test (0xdeadbeef, 0xffffffffdeadbeef, 8, 0xdeadbeef, 4);
913 do_cint_test (0xadbeef, 0xffffffffffadbeef, 8, 0xdeadbeef, 3);
915 /* Destination is smaller than the source. */
916 do_cint_test (0x5678, 0x5678, 2, 0x12345678, 3);
917 do_cint_test (0xbeef, 0xbeef, 2, 0xdeadbeef, 3);
919 /* Destination and source are the same size. */
920 do_cint_test (0x8765432112345678, 0x8765432112345678, 8, 0x8765432112345678,
922 do_cint_test (0x432112345678, 0x432112345678, 6, 0x8765432112345678, 6);
923 do_cint_test (0xfeedbeaddeadbeef, 0xfeedbeaddeadbeef, 8, 0xfeedbeaddeadbeef,
925 do_cint_test (0xbeaddeadbeef, 0xbeaddeadbeef, 6, 0xfeedbeaddeadbeef, 6);
927 /* Destination is bigger than the source. Source is bigger than 32bits. */
928 do_cint_test (0x3412345678, 0x3412345678, 8, 0x3412345678, 6);
929 do_cint_test (0xff12345678, 0xff12345678, 8, 0xff12345678, 6);
930 do_cint_test (0x432112345678, 0x432112345678, 8, 0x8765432112345678, 6);
931 do_cint_test (0xff2112345678, 0xffffff2112345678, 8, 0xffffff2112345678, 6);
934 } // namespace findvar_test
935 } // namespace selftests
939 void _initialize_findvar ();
941 _initialize_findvar ()
944 selftests::register_test
945 ("copy_integer_to_size",
946 selftests::findvar_tests::copy_integer_to_size_test
);