This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
fix 2 readelf bugs
- From: Richard Henderson <rth at twiddle dot net>
- To: binutils at sources dot redhat dot com
- Date: Sat, 31 May 2003 12:31:50 -0700
- Subject: fix 2 readelf bugs
First, signed pc-relative encodings were not being properly handled.
This lead to strange results on 64-bit systems. (Well, presumably
elsewhere as well if you had a 16-bit encoding, which no one did.)
Second, readelf was crashing because it was writing beyond the end
of allocated memory.
r~
* readelf.c (byte_get_signed): New.
(get_encoded_value): New.
(display_debug_frames): Use it. Always pre-process opcodes.
Index: readelf.c
===================================================================
RCS file: /cvs/src/src/binutils/readelf.c,v
retrieving revision 1.207
diff -c -p -d -u -r1.207 readelf.c
--- readelf.c 20 May 2003 14:37:46 -0000 1.207
+++ readelf.c 31 May 2003 19:28:00 -0000
@@ -173,6 +173,8 @@ static bfd_vma byte_get_little_endian
PARAMS ((unsigned char *, int));
static bfd_vma byte_get_big_endian
PARAMS ((unsigned char *, int));
+static bfd_vma byte_get_signed
+ PARAMS ((unsigned char *, int));
static void (*byte_put)
PARAMS ((unsigned char *, bfd_vma, int));
static void byte_put_little_endian
@@ -556,6 +558,29 @@ byte_get_little_endian (field, size)
}
}
+static bfd_vma
+byte_get_signed (field, size)
+ unsigned char *field;
+ int size;
+{
+ bfd_vma x = byte_get (field, size);
+
+ switch (size)
+ {
+ case 1:
+ return (x ^ 0x80) - 0x80;
+ case 2:
+ return (x ^ 0x8000) - 0x8000;
+ case 4:
+ return (x ^ 0x80000000) - 0x80000000;
+ case 8:
+ case -8:
+ return x;
+ default:
+ abort ();
+ }
+}
+
static void
byte_put_little_endian (field, value, size)
unsigned char * field;
@@ -8668,6 +8693,7 @@ Frame_Chunk;
static void frame_need_space PARAMS ((Frame_Chunk *, int));
static void frame_display_row PARAMS ((Frame_Chunk *, int *, int *));
static int size_of_encoded_value PARAMS ((int));
+static bfd_vma get_encoded_value PARAMS ((unsigned char *, int));
static void
frame_need_space (fc, reg)
@@ -8775,6 +8801,18 @@ size_of_encoded_value (encoding)
}
}
+static bfd_vma
+get_encoded_value (data, encoding)
+ unsigned char *data;
+ int encoding;
+{
+ int size = size_of_encoded_value (encoding);
+ if (encoding & DW_EH_PE_signed)
+ return byte_get_signed (data, size);
+ else
+ return byte_get (data, size);
+}
+
#define GET(N) byte_get (start, N); start += N
#define LEB() read_leb128 (start, & length_return, 0); start += length_return
#define SLEB() read_leb128 (start, & length_return, 1); start += length_return
@@ -8980,7 +9018,7 @@ display_debug_frames (section, start, fi
if (fc->fde_encoding)
encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
- fc->pc_begin = byte_get (start, encoded_ptr_size);
+ fc->pc_begin = get_encoded_value (start, fc->fde_encoding);
if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
fc->pc_begin += section->sh_addr + (start - section_start);
start += encoded_ptr_size;
@@ -9011,8 +9049,12 @@ display_debug_frames (section, start, fi
/* At this point, fc is the current chunk, cie (if any) is set, and we're
about to interpret instructions for the chunk. */
-
- if (do_debug_frames_interp)
+ /* ??? At present we need to do this always, since this sizes the
+ fc->col_type and fc->col_offset arrays, which we write into always.
+ We should probably split the interpreted and non-interpreted bits
+ into two different routines, since there's so much that doesn't
+ really overlap between them. */
+ if (1 || do_debug_frames_interp)
{
/* Start by making a pass over the chunk, allocating storage
and taking note of what registers are used. */
@@ -9175,7 +9217,7 @@ display_debug_frames (section, start, fi
break;
case DW_CFA_set_loc:
- vma = byte_get (start, encoded_ptr_size);
+ vma = get_encoded_value (start, fc->fde_encoding);
if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
vma += section->sh_addr + (start - section_start);
start += encoded_ptr_size;