]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libsframe: correct binary search for SFrame FDE
authorJens Remus <jremus@linux.ibm.com>
Mon, 26 May 2025 18:01:14 +0000 (11:01 -0700)
committerIndu Bhagat <indu.bhagat@oracle.com>
Mon, 26 May 2025 18:01:14 +0000 (11:01 -0700)
sframe_get_funcdesc_with_addr_internal erroneously returns the last FDE,
if its function start address is lower than the searched for address.

Simplify the binary search for a SFrame FDE for a given address.  Only
return an FDE, if the searched for address is within the bounds of the
FDE function start address and function size.

libsframe/
* sframe.c (sframe_get_funcdesc_with_addr_internal): Correct
binary search for SFrame FDE.

libsframe/testsuite/
* libsframe.find/plt-findfre-1.c: Add test for out of range
PLT6.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
libsframe/sframe.c
libsframe/testsuite/libsframe.find/plt-findfre-1.c

index c1bc692829a9c175101c70eef3b505d322d9bf01..d16305d629b469a856a6c57c1eff4c4b1af02f5c 100644 (file)
@@ -1023,7 +1023,7 @@ sframe_get_funcdesc_with_addr_internal (sframe_decoder_ctx *ctx, int32_t addr,
 {
   sframe_header *dhp;
   sframe_func_desc_entry *fdp;
-  int low, high, cnt;
+  int low, high;
 
   if (ctx == NULL)
     return sframe_ret_set_errno (errp, SFRAME_ERR_INVAL);
@@ -1041,22 +1041,19 @@ sframe_get_funcdesc_with_addr_internal (sframe_decoder_ctx *ctx, int32_t addr,
   fdp = (sframe_func_desc_entry *) ctx->sfd_funcdesc;
   low = 0;
   high = dhp->sfh_num_fdes;
-  cnt = high;
   while (low <= high)
     {
       int mid = low + (high - low) / 2;
 
-      if (fdp[mid].sfde_func_start_address == addr)
+      /* Given sfde_func_start_address <= addr,
+        addr - sfde_func_start_address must be positive.  */
+      if (fdp[mid].sfde_func_start_address <= addr
+         && ((uint32_t)(addr - fdp[mid].sfde_func_start_address)
+             < fdp[mid].sfde_func_size))
        return fdp + mid;
 
       if (fdp[mid].sfde_func_start_address < addr)
-       {
-         if (mid == (cnt - 1))         /* Check if it's the last one.  */
-           return fdp + (cnt - 1);
-         else if (fdp[mid+1].sfde_func_start_address > addr)
-           return fdp + mid;
-         low = mid + 1;
-       }
+       low = mid + 1;
       else
        high = mid - 1;
     }
index 60037fd67ea0c01d5dcc575239c52895b32cd70a..89ca4661922ea8d76340f19e420c6ec4c203ecae 100644 (file)
@@ -128,6 +128,10 @@ void test_plt_findfre (uint32_t plt_vaddr, uint32_t sframe_vaddr)
   TEST ("plt-findfre-1: Find last FRE in PLT4",
        (err == 0 && sframe_fre_get_cfa_offset (dctx, &frep, &err) == 0x3));
 
+  /* Find no FRE for out of range PLT6.  */
+  err = sframe_find_fre (dctx, (plt_vaddr + 16*5 + 0x0 - sframe_vaddr), &frep);
+  TEST ("plt-findfre-1: Find no FRE for out of range PLT6", err != 0);
+
   sframe_encoder_free (&ectx);
   sframe_decoder_free (&dctx);
 }