1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 356519 - ELF header reader makeShort() bug

This commit is contained in:
John Cortell 2012-02-24 13:16:17 -06:00
parent 117792fba6
commit b4da3bb8f0
3 changed files with 270 additions and 45 deletions

View file

@ -0,0 +1,148 @@
/*******************************************************************************
* Copyright (c) 2012 Freescale Semiconductor and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Freescale Semiconductor - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.utils;
import static org.eclipse.cdt.internal.core.ByteUtils.makeInt;
import static org.eclipse.cdt.internal.core.ByteUtils.makeLong;
import static org.eclipse.cdt.internal.core.ByteUtils.makeShort;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class ByteUtilsTest extends TestCase {
public static Test suite() {
return new TestSuite(ByteUtilsTest.class);
}
// Allows us to avoid ugly misalignment in the source
private static byte Ox80 = (byte)0x80;
private static byte Oxff = (byte)0xff;
public void testMakeShort() throws Exception {
Assert.assertEquals((short)0x0000, makeShort(new byte[]{0x00,0x00}, 0, false));
Assert.assertEquals((short)0x7f00, makeShort(new byte[]{0x7f,0x00}, 0, false));
Assert.assertEquals((short)0x007f, makeShort(new byte[]{0x00,0x7f}, 0, false));
Assert.assertEquals((short)0x8000, makeShort(new byte[]{Ox80,0x00}, 0, false));
Assert.assertEquals((short)0x0080, makeShort(new byte[]{0x00,Ox80}, 0, false));
Assert.assertEquals((short)0xff00, makeShort(new byte[]{Oxff,0x00}, 0, false));
Assert.assertEquals((short)0x00ff, makeShort(new byte[]{0x00,Oxff}, 0, false));
Assert.assertEquals((short)0xffff, makeShort(new byte[]{Oxff,Oxff}, 0, false));
Assert.assertEquals((short)0x0000, makeShort(new byte[]{0x00,0x00}, 0, true));
Assert.assertEquals((short)0x007f, makeShort(new byte[]{0x7f,0x00}, 0, true));
Assert.assertEquals((short)0x7f00, makeShort(new byte[]{0x00,0x7f}, 0, true));
Assert.assertEquals((short)0x0080, makeShort(new byte[]{Ox80,0x00}, 0, true));
Assert.assertEquals((short)0x8000, makeShort(new byte[]{0x00,Ox80}, 0, true));
Assert.assertEquals((short)0x00ff, makeShort(new byte[]{Oxff,0x00}, 0, true));
Assert.assertEquals((short)0xff00, makeShort(new byte[]{0x00,Oxff}, 0, true));
Assert.assertEquals((short)0xffff, makeShort(new byte[]{Oxff,Oxff}, 0, true));
Assert.assertEquals(0x0102, makeShort(new byte[]{0,0,0,0x01,0x02}, 3, false));
Assert.assertEquals(0x0201, makeShort(new byte[]{0,0,0,0x01,0x02}, 3, true));
}
public void testMakeInt() throws Exception {
Assert.assertEquals(0x00000000, makeInt(new byte[]{0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x7f000000, makeInt(new byte[]{0x7f,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x007f0000, makeInt(new byte[]{0x00,0x7f,0x00,0x00}, 0, false));
Assert.assertEquals(0x00007f00, makeInt(new byte[]{0x00,0x00,0x7f,0x00}, 0, false));
Assert.assertEquals(0x0000007f, makeInt(new byte[]{0x00,0x00,0x00,0x7f}, 0, false));
Assert.assertEquals(0x80000000, makeInt(new byte[]{Ox80,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x00800000, makeInt(new byte[]{0x00,Ox80,0x00,0x00}, 0, false));
Assert.assertEquals(0x00008000, makeInt(new byte[]{0x00,0x00,Ox80,0x00}, 0, false));
Assert.assertEquals(0x00000080, makeInt(new byte[]{0x00,0x00,0x00,Ox80}, 0, false));
Assert.assertEquals(0xff000000, makeInt(new byte[]{Oxff,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x00ff0000, makeInt(new byte[]{0x00,Oxff,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000ff00, makeInt(new byte[]{0x00,0x00,Oxff,0x00}, 0, false));
Assert.assertEquals(0x000000ff, makeInt(new byte[]{0x00,0x00,0x00,Oxff}, 0, false));
Assert.assertEquals(0xffffffff, makeInt(new byte[]{Oxff,Oxff,Oxff,Oxff}, 0, false));
Assert.assertEquals(0x00000000, makeInt(new byte[]{0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000007f, makeInt(new byte[]{0x7f,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x00007f00, makeInt(new byte[]{0x00,0x7f,0x00,0x00}, 0, true));
Assert.assertEquals(0x007f0000, makeInt(new byte[]{0x00,0x00,0x7f,0x00}, 0, true));
Assert.assertEquals(0x7f000000, makeInt(new byte[]{0x00,0x00,0x00,0x7f}, 0, true));
Assert.assertEquals(0x00000080, makeInt(new byte[]{Ox80,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x00008000, makeInt(new byte[]{0x00,Ox80,0x00,0x00}, 0, true));
Assert.assertEquals(0x00800000, makeInt(new byte[]{0x00,0x00,Ox80,0x00}, 0, true));
Assert.assertEquals(0x80000000, makeInt(new byte[]{0x00,0x00,0x00,Ox80}, 0, true));
Assert.assertEquals(0x000000ff, makeInt(new byte[]{Oxff,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000ff00, makeInt(new byte[]{0x00,Oxff,0x00,0x00}, 0, true));
Assert.assertEquals(0x00ff0000, makeInt(new byte[]{0x00,0x00,Oxff,0x00}, 0, true));
Assert.assertEquals(0xff000000, makeInt(new byte[]{0x00,0x00,0x00,Oxff}, 0, true));
Assert.assertEquals(0xffffffff, makeInt(new byte[]{Oxff,Oxff,Oxff,Oxff}, 0, true));
Assert.assertEquals(0x01020304, makeInt(new byte[]{0,0,0,0x01,0x02,0x03,0x04}, 3, false));
Assert.assertEquals(0x04030201, makeInt(new byte[]{0,0,0,0x01,0x02,0x03,0x04}, 3, true));
}
public void testMakeLong() throws Exception {
Assert.assertEquals(0x0000000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x7f00000000000000L, makeLong(new byte[]{0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x007f000000000000L, makeLong(new byte[]{0x00,0x7f,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x00007f0000000000L, makeLong(new byte[]{0x00,0x00,0x7f,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000007f00000000L, makeLong(new byte[]{0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x000000007f000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x7f,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x00000000007f0000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000000000007f00L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00}, 0, false));
Assert.assertEquals(0x000000000000007fL, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f}, 0, false));
Assert.assertEquals(0x8000000000000000L, makeLong(new byte[]{Ox80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0080000000000000L, makeLong(new byte[]{0x00,Ox80,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000800000000000L, makeLong(new byte[]{0x00,0x00,Ox80,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000008000000000L, makeLong(new byte[]{0x00,0x00,0x00,Ox80,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000000080000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,Ox80,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000000000800000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,Ox80,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000000000008000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,Ox80,0x00}, 0, false));
Assert.assertEquals(0x0000000000000080L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,Ox80}, 0, false));
Assert.assertEquals(0xff00000000000000L, makeLong(new byte[]{Oxff,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x00ff000000000000L, makeLong(new byte[]{0x00,Oxff,0x00,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000ff0000000000L, makeLong(new byte[]{0x00,0x00,Oxff,0x00,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x000000ff00000000L, makeLong(new byte[]{0x00,0x00,0x00,Oxff,0x00,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x00000000ff000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,Oxff,0x00,0x00,0x00}, 0, false));
Assert.assertEquals(0x0000000000ff0000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,Oxff,0x00,0x00}, 0, false));
Assert.assertEquals(0x000000000000ff00L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,Oxff,0x00}, 0, false));
Assert.assertEquals(0x00000000000000ffL, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,Oxff}, 0, false));
Assert.assertEquals(0xffffffffffffffffL, makeLong(new byte[]{Oxff,Oxff,Oxff,Oxff,Oxff,Oxff,Oxff,Oxff}, 0, false));
Assert.assertEquals(0x0000000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x000000000000007fL, makeLong(new byte[]{0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000000000007f00L, makeLong(new byte[]{0x00,0x7f,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x00000000007f0000L, makeLong(new byte[]{0x00,0x00,0x7f,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x000000007f000000L, makeLong(new byte[]{0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000007f00000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x7f,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x00007f0000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00}, 0, true));
Assert.assertEquals(0x007f000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00}, 0, true));
Assert.assertEquals(0x7f00000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f}, 0, true));
Assert.assertEquals(0x0000000000000080L, makeLong(new byte[]{Ox80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000000000008000L, makeLong(new byte[]{0x00,Ox80,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000000000800000L, makeLong(new byte[]{0x00,0x00,Ox80,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000000080000000L, makeLong(new byte[]{0x00,0x00,0x00,Ox80,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000008000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,Ox80,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000800000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,Ox80,0x00,0x00}, 0, true));
Assert.assertEquals(0x0080000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,Ox80,0x00}, 0, true));
Assert.assertEquals(0x8000000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,Ox80}, 0, true));
Assert.assertEquals(0x00000000000000ffL, makeLong(new byte[]{Oxff,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x000000000000ff00L, makeLong(new byte[]{0x00,Oxff,0x00,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000000000ff0000L, makeLong(new byte[]{0x00,0x00,Oxff,0x00,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x00000000ff000000L, makeLong(new byte[]{0x00,0x00,0x00,Oxff,0x00,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x000000ff00000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,Oxff,0x00,0x00,0x00}, 0, true));
Assert.assertEquals(0x0000ff0000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,Oxff,0x00,0x00}, 0, true));
Assert.assertEquals(0x00ff000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,Oxff,0x00}, 0, true));
Assert.assertEquals(0xff00000000000000L, makeLong(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,Oxff}, 0, true));
Assert.assertEquals(0xffffffffffffffffL, makeLong(new byte[]{Oxff,Oxff,Oxff,Oxff,Oxff,Oxff,Oxff,Oxff}, 0, true));
Assert.assertEquals(0x0102030405060708L, makeLong(new byte[]{0,0,0,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}, 3, false));
Assert.assertEquals(0x0807060504030201L, makeLong(new byte[]{0,0,0,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}, 3, true));
}
}

View file

@ -0,0 +1,95 @@
/*******************************************************************************
* Copyright (c) 2000, 2012 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* QNX Software Systems - initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core;
import java.io.IOException;
public class ByteUtils {
/**
* Concatenates two bytes to make a short
*
* @param bytes
* collection of bytes; must provide a minimum of two bytes starting at [offset]
* @param offset
* zero-based index into [bytes], identifying the first input byte
* @param isle
* if true, bytes are concatenated in little-endian order (the first input byte in the
* collection represents the least-significant byte of the resulting short); otherwise
* big-endian
* @return the resulting short
* @throws IOException
* if an insufficient number of bytes are supplied
*/
public static short makeShort(byte[] bytes, int offset, boolean isle) throws IOException {
if (bytes.length < offset + 2)
throw new IOException();
return isle ?
(short) ((bytes[offset + 1] << 8) | (bytes[offset + 0] & 0xff)) :
(short) ((bytes[offset + 0] << 8) | (bytes[offset + 1] & 0xff));
}
/**
* Concatenates four bytes to make an int
*
* @param bytes
* collection of bytes; must provide a minimum of four bytes starting at [offset]
* @param offset
* zero-based index into [bytes], identifying the first input byte
* @param isle
* if true, bytes are concatenated in little-endian order (the first input byte in the
* collection represents the least-significant byte of the resulting short); otherwise
* big-endian
* @return the resulting int
* @throws IOException
* if an insufficient number of bytes are supplied
*/
public static long makeInt(byte[] bytes, int offset, boolean isle) throws IOException {
if (bytes.length < offset + 4)
throw new IOException();
return isle ?
(bytes[offset + 3] << 24) + ((bytes[offset + 2] & 0xff) << 16) + ((bytes[offset + 1] & 0xff) << 8) + (bytes[offset + 0] & 0xff) :
(bytes[offset + 0] << 24) + ((bytes[offset + 1] & 0xff) << 16) + ((bytes[offset + 2] & 0xff) << 8) + (bytes[offset + 3] & 0xff);
}
/**
* Concatenates eight bytes to make a long
*
* @param bytes
* collection of bytes; must provide a minimum of eight bytes starting at [offset]
* @param offset
* zero-based index into [bytes], identifying the first input byte
* @param isle
* if true, bytes are concatenated in little-endian order (the first input byte in the
* collection represents the least-significant byte of the resulting short); otherwise
* big-endian
* @return the resulting int
* @throws IOException
* if an insufficient number of bytes are supplied
*/
public static long makeLong(byte[] bytes, int offset, boolean isle) throws IOException {
long result = 0;
int shift = 0;
if (isle)
for (int i = 7; i >= 0; i--) {
shift = i * 8;
result += ( ((long)bytes[offset + i]) << shift) & (0xffL << shift);
}
else
for (int i = 0; i <= 7; i++) {
shift = (7 - i) * 8;
result += ( ((long)bytes[offset + i]) << shift) & (0xffL << shift);
}
return result;
}
}

View file

@ -12,6 +12,11 @@
package org.eclipse.cdt.utils.elf;
import java.io.EOFException;
import static org.eclipse.cdt.internal.core.ByteUtils.makeShort;
import static org.eclipse.cdt.internal.core.ByteUtils.makeInt;
import static org.eclipse.cdt.internal.core.ByteUtils.makeLong;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel.MapMode;
@ -136,6 +141,13 @@ public class Elf {
public final static int EM_RS08 = 132; /* Freescale RS08 embedded processor */
public final static int EM_MMDSP = 160;
/** @since 5.4 */
public final static int EM_RX = 173; /* Renesas RX Microcontroller */
/** @since 5.4 */
public final static int EM_RL78 = 197; /* Renesas RL78 Microcontroller */
public final static int EM_NIOS = 0xFEBB;
public final static int EM_CYGNUS_POWERPC = 0x9025;
public final static int EM_CYGNUS_M32R = 0x9041;
@ -263,51 +275,6 @@ public class Elf {
e_shstrndx = makeShort(bytes, offset, isle);
offset += 2;
}
private final short makeShort(byte[] val, int offset, boolean isle) throws IOException {
if (val.length < offset + 2)
throw new IOException();
if (isle) {
return (short) ( (val[offset + 1] << 8) + val[offset + 0]);
}
return (short) ( (val[offset + 0] << 8) + val[offset + 1]);
}
private final long makeInt(byte[] val, int offset, boolean isle) throws IOException {
if (val.length < offset + 4)
throw new IOException();
if (isle) {
return ( (val[offset + 3] << 24) + (val[offset + 2] << 16) + (val[offset + 1] << 8) + val[offset + 0]);
}
return ( (val[offset + 0] << 24) + (val[offset + 1] << 16) + (val[offset + 2] << 8) + val[offset + 3]);
}
private final long makeLong(byte[] val, int offset, boolean isle) throws IOException {
long result = 0;
int shift = 0;
if (isle)
for (int i = 7; i >= 0; i--) {
shift = i * 8;
result += ( ((long)val[offset + i]) << shift) & (0xffL << shift);
}
else
for (int i = 0; i <= 7; i++) {
shift = (7 - i) * 8;
result += ( ((long)val[offset + i]) << shift) & (0xffL << shift);
}
return result;
}
private final long makeUnsignedLong(byte[] val, int offset, boolean isle) throws IOException {
long result = makeLong(val, offset, isle);
if (result < 0) {
throw new IOException("Maximal file offset is " + Long.toHexString(Long.MAX_VALUE) + //$NON-NLS-1$
" given offset is " + Long.toHexString(result)); //$NON-NLS-1$
}
return result;
}
}
public class Section {
@ -893,6 +860,12 @@ public class Elf {
case Elf.ELFhdr.EM_MMDSP:
attrib.cpu = "mmdsp"; //$NON-NLS-1$
break;
case Elf.ELFhdr.EM_RX:
attrib.cpu = "rx"; //$NON-NLS-1$
break;
case Elf.ELFhdr.EM_RL78:
attrib.cpu = "rl78"; //$NON-NLS-1$
break;
case Elf.ELFhdr.EM_68HC08:
attrib.cpu = "hc08"; //$NON-NLS-1$
break;
@ -1226,4 +1199,13 @@ public class Elf {
return reader;
}
/** @since 5.4 */
public static long makeUnsignedLong(byte[] val, int offset, boolean isle) throws IOException {
long result = makeLong(val, offset, isle);
if (result < 0) {
throw new IOException("Maximal file offset is " + Long.toHexString(Long.MAX_VALUE) + //$NON-NLS-1$
" given offset is " + Long.toHexString(result)); //$NON-NLS-1$
}
return result;
}
}