From 9bf449a90ed81f0058d96613a7ecaec0ca68061e Mon Sep 17 00:00:00 2001 From: Henny Sipma Date: Wed, 25 Feb 2026 22:19:38 -0800 Subject: [PATCH] CHB:ELF: enable retrieval of last n bytes from section --- CodeHawk/CHB/bchlib/bCHVersion.ml | 4 ++-- CodeHawk/CHB/bchlibelf/bCHELFHeader.ml | 10 ++++++++-- CodeHawk/CHB/bchlibelf/bCHELFTypes.mli | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CodeHawk/CHB/bchlib/bCHVersion.ml b/CodeHawk/CHB/bchlib/bCHVersion.ml index 383958b1..fef02570 100644 --- a/CodeHawk/CHB/bchlib/bCHVersion.ml +++ b/CodeHawk/CHB/bchlib/bCHVersion.ml @@ -95,8 +95,8 @@ end let version = new version_info_t - ~version:"0.6.0_20260122" - ~date:"2026-01-22" + ~version:"0.6.0_20260225" + ~date:"2026-02-25" ~licensee: None ~maxfilesize: None () diff --git a/CodeHawk/CHB/bchlibelf/bCHELFHeader.ml b/CodeHawk/CHB/bchlibelf/bCHELFHeader.ml index b244eff2..b7b3c1fc 100644 --- a/CodeHawk/CHB/bchlibelf/bCHELFHeader.ml +++ b/CodeHawk/CHB/bchlibelf/bCHELFHeader.ml @@ -6,7 +6,7 @@ Copyright (c) 2005-2020 Kestrel Technology LLC Copyright (c) 2020 Henny Sipma - Copyright (c) 2021-2025 Aarno Labs LLC + Copyright (c) 2021-2026 Aarno Labs LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -808,12 +808,18 @@ object(self) match self#get_containing_section a with | Some s -> let diff = a#subtract_to_int s#get_vaddr in + (* In some cases the <= check may be problematic. If the section is the + last one in the file, or perhaps if there is a gap between this + section and the next, the check may succeed, but the retrieval of + the corresponding bytes may fail. However, a < check may result in + missing the last library stub in a plt section.*) if Result.is_ok diff then - (TR.tget_ok diff) + size < s#get_size + (TR.tget_ok diff) + size <= s#get_size else raise (BCH_failure (LBLOCK [ + STR __FILE__; STR ":"; INT __LINE__; STR "ELFHeader:has_xsubstring: interal error: "; STR "subtraction"])) | _ -> false diff --git a/CodeHawk/CHB/bchlibelf/bCHELFTypes.mli b/CodeHawk/CHB/bchlibelf/bCHELFTypes.mli index 71d69580..907bfc40 100644 --- a/CodeHawk/CHB/bchlibelf/bCHELFTypes.mli +++ b/CodeHawk/CHB/bchlibelf/bCHELFTypes.mli @@ -6,7 +6,7 @@ Copyright (c) 2005-2020 Kestrel Technology LLC Copyright (c) 2020 Henny Sipma - Copyright (c) 2021-2024 Aarno Labs LLC + Copyright (c) 2021-2026 Aarno Labs LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -807,6 +807,11 @@ object method get_executable_sections: (elf_section_header_int * string) list method get_executable_segments: (elf_program_header_int * string) list method get_string_at_address: doubleword_int -> string option + + (** [get_xsubstring addr size] returns the string of bytes starting at + virtual address [addr] with length size bytes. + Note: see comment for has_xsubstring below + *) method get_xsubstring: doubleword_int -> int -> string method get_relocation: doubleword_int -> string option method get_containing_section: doubleword_int -> elf_raw_section_int option @@ -832,6 +837,21 @@ object method is_uninitialized_data_address: doubleword_int -> bool method is_global_offset_table_address: doubleword_int -> bool + + (** [has_xsubstring addr size] returns true if the section that contains + virtual address [addr] has at least [size] bytes starting from address + [addr]. + + Note: the check performed is + [addr - section_start_address) + size <= section_size] + In some cases the less than equal check may be problematic: if the + section is the last section of the file, or perhaps if there is a + gap between this section and the next, the equal sign may make the + check succeed, but the retrieval of the corresponding bytes, by calling + [get_xsubstring] with the same arguments, may fail. + However, if a less-than check is used the last library stub in a plt + may be missed. + *) method has_xsubstring: doubleword_int -> int -> bool method has_debug_info: bool method has_debug_abbrev: bool