diff --git a/src/core/operations/ELFInfo.mjs b/src/core/operations/ELFInfo.mjs index 6fa46c54..9153e24f 100644 --- a/src/core/operations/ELFInfo.mjs +++ b/src/core/operations/ELFInfo.mjs @@ -125,7 +125,7 @@ class ELFInfo extends Operation { ehResult.push("Format:".padEnd(align) + `${format === 1 ? "32-bit" : "64-bit"}`); endianness = stream.readInt(1) === 1 ? "le" : "be"; - ehResult.push("Endianness:".padEnd(align) + `${endianness === 1 ? "Little" : "Big"}`); + ehResult.push("Endianness:".padEnd(align) + `${endianness === "le" ? "Little" : "Big"}`); ehResult.push("Version:".padEnd(align) + `${stream.readInt(1).toString()}`); diff --git a/tests/operations/tests/ELFInfo.mjs b/tests/operations/tests/ELFInfo.mjs index cabf1344..fad9a05c 100644 --- a/tests/operations/tests/ELFInfo.mjs +++ b/tests/operations/tests/ELFInfo.mjs @@ -4,6 +4,12 @@ * @license Apache-2.0 */ import TestRegister from "../../lib/TestRegister.mjs"; +import {ELF32_LE, ELF32_BE, ELF64_LE, ELF64_BE} from "../../samples/Executables.mjs"; + +const ELF32_LE_OUTPUT = "============================== ELF Header ==============================\nMagic: \x7fELF\nFormat: 32-bit\nEndianness: Little\nVersion: 1\nABI: System V\nABI Version: 0\nType: Executable File\nInstruction Set Architecture: x86\nELF Version: 1\nEntry Point: 0x8062150\nEntry PHOFF: 0x34\nEntry SHOFF: 0x54\nFlags: 00000000\nELF Header Size: 52 bytes\nProgram Header Size: 32 bytes\nProgram Header Entries: 1\nSection Header Size: 40 bytes\nSection Header Entries: 3\nSection Header Names: 0\n\n============================== Program Header ==============================\nProgram Header Type: Program Header Table\nOffset Of Segment: 52\nVirtual Address of Segment: 134512692\nPhysical Address of Segment: 134512692\nSize of Segment: 256 bytes\nSize of Segment in Memory: 256 bytes\nFlags: Execute,Read\n\n============================== Section Header ==============================\nType: String Table\nSection Name: .shstrab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 204\nSection Size: 28\nAssociated Section: 0\nSection Extra Information: 0\n\nType: Symbol Table\nSection Name: .symtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 230\nSection Size: 16\nAssociated Section: 0\nSection Extra Information: 0\n\nType: String Table\nSection Name: .strtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 245\nSection Size: 4\nAssociated Section: 0\nSection Extra Information: 0\n\n============================== Symbol Table ==============================\nSymbol Name: test"; +const ELF32_BE_OUTPUT = "============================== ELF Header ==============================\nMagic: \x7fELF\nFormat: 32-bit\nEndianness: Big\nVersion: 1\nABI: System V\nABI Version: 0\nType: Executable File\nInstruction Set Architecture: x86\nELF Version: 1\nEntry Point: 0x8062150\nEntry PHOFF: 0x34\nEntry SHOFF: 0x54\nFlags: 00000000\nELF Header Size: 52 bytes\nProgram Header Size: 32 bytes\nProgram Header Entries: 1\nSection Header Size: 40 bytes\nSection Header Entries: 3\nSection Header Names: 0\n\n============================== Program Header ==============================\nProgram Header Type: Program Header Table\nOffset Of Segment: 52\nVirtual Address of Segment: 134512692\nPhysical Address of Segment: 134512692\nSize of Segment: 256 bytes\nSize of Segment in Memory: 256 bytes\nFlags: Execute,Read\n\n============================== Section Header ==============================\nType: String Table\nSection Name: .shstrab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 204\nSection Size: 28\nAssociated Section: 0\nSection Extra Information: 0\n\nType: Symbol Table\nSection Name: .symtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 230\nSection Size: 16\nAssociated Section: 0\nSection Extra Information: 0\n\nType: String Table\nSection Name: .strtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 245\nSection Size: 4\nAssociated Section: 0\nSection Extra Information: 0\n\n============================== Symbol Table ==============================\nSymbol Name: test"; +const ELF64_LE_OUTPUT = "============================== ELF Header ==============================\nMagic: \x7fELF\nFormat: 64-bit\nEndianness: Little\nVersion: 1\nABI: System V\nABI Version: 0\nType: Executable File\nInstruction Set Architecture: AMD x86-64\nELF Version: 1\nEntry Point: 0x8062150\nEntry PHOFF: 0x40\nEntry SHOFF: 0x78\nFlags: 00000000\nELF Header Size: 64 bytes\nProgram Header Size: 56 bytes\nProgram Header Entries: 1\nSection Header Size: 64 bytes\nSection Header Entries: 3\nSection Header Names: 0\n\n============================== Program Header ==============================\nProgram Header Type: Program Header Table\nFlags: Execute,Read\nOffset Of Segment: 52\nVirtual Address of Segment: 134512692\nPhysical Address of Segment: 134512692\nSize of Segment: 256 bytes\nSize of Segment in Memory: 256 bytes\n\n============================== Section Header ==============================\nType: String Table\nSection Name: .shstrab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 312\nSection Size: 28\nAssociated Section: 0\nSection Extra Information: 0\n\nType: Symbol Table\nSection Name: .symtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 336\nSection Size: 16\nAssociated Section: 0\nSection Extra Information: 0\n\nType: String Table\nSection Name: .strtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 361\nSection Size: 4\nAssociated Section: 0\nSection Extra Information: 0\n\n============================== Symbol Table ==============================\nSymbol Name: test"; +const ELF64_BE_OUTPUT = "============================== ELF Header ==============================\nMagic: \x7fELF\nFormat: 64-bit\nEndianness: Big\nVersion: 1\nABI: System V\nABI Version: 0\nType: Executable File\nInstruction Set Architecture: AMD x86-64\nELF Version: 1\nEntry Point: 0x8062150\nEntry PHOFF: 0x40\nEntry SHOFF: 0x78\nFlags: 00000000\nELF Header Size: 64 bytes\nProgram Header Size: 56 bytes\nProgram Header Entries: 1\nSection Header Size: 64 bytes\nSection Header Entries: 3\nSection Header Names: 0\n\n============================== Program Header ==============================\nProgram Header Type: Program Header Table\nFlags: Execute,Read\nOffset Of Segment: 52\nVirtual Address of Segment: 134512692\nPhysical Address of Segment: 134512692\nSize of Segment: 256 bytes\nSize of Segment in Memory: 256 bytes\n\n============================== Section Header ==============================\nType: String Table\nSection Name: .shstrab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 312\nSection Size: 28\nAssociated Section: 0\nSection Extra Information: 0\n\nType: Symbol Table\nSection Name: .symtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 336\nSection Size: 16\nAssociated Section: 0\nSection Extra Information: 0\n\nType: String Table\nSection Name: .strtab\nFlags: \nSection Vaddr in memory: 0\nOffset of the section: 361\nSection Size: 4\nAssociated Section: 0\nSection Extra Information: 0\n\n============================== Symbol Table ==============================\nSymbol Name: test"; TestRegister.addTests([ { @@ -17,4 +23,64 @@ TestRegister.addTests([ }, ], }, + { + name: "ELF Info 32-bit ELF Little Endian.", + input: ELF32_LE, + expectedOutput: ELF32_LE_OUTPUT, + recipeConfig: [ + { + op: "From Hex", + args: ["None"], + }, + { + op: "ELF Info", + args: [], + }, + ], + }, + { + name: "ELF Info 32-bit ELF Big Endian.", + input: ELF32_BE, + expectedOutput: ELF32_BE_OUTPUT, + recipeConfig: [ + { + op: "From Hex", + args: ["None"], + }, + { + op: "ELF Info", + args: [], + }, + ], + }, + { + name: "ELF Info 64-bit ELF Little Endian.", + input: ELF64_LE, + expectedOutput: ELF64_LE_OUTPUT, + recipeConfig: [ + { + op: "From Hex", + args: ["None"], + }, + { + op: "ELF Info", + args: [], + }, + ], + }, + { + name: "ELF Info 64-bit ELF Big Endian.", + input: ELF64_BE, + expectedOutput: ELF64_BE_OUTPUT, + recipeConfig: [ + { + op: "From Hex", + args: ["None"], + }, + { + op: "ELF Info", + args: [], + }, + ], + }, ]); diff --git a/tests/samples/Executables.mjs b/tests/samples/Executables.mjs new file mode 100644 index 00000000..389836e2 --- /dev/null +++ b/tests/samples/Executables.mjs @@ -0,0 +1,375 @@ +/** + * Executables in various formats for use in tests. + * + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2022 + * @license Apache-2.0 + */ + +/** + * ---------------ELF Header--------------- + * 7f454c46 - Magic: .ELF + * 01 - Format: 32 bit + * 01 - Endianness: Little + * 01 - ELF Version: 1 + * 00 - Target ABI: System V + * 00 - ABI Version: 0 + * 00000000000000 - Padding + * 0200 - File Type: Executable File + * 0300 - ISA: x86 + * 01000000 - ELF Version: 1 + * 50210608 - Entry Point: 0x8062150 + * 34000000 - PH Offset: 0x34 + * 54000000 - SH Offset: 0x54 + * 00000000 - Flags: 0 + * 3400 - ELF Header Size: 0x34 + * 2000 - PH Header Size: 0x20 + * 0100 - PH Entries: 1 + * 2800 - SH Size: 0x28 + * 0300 - SH Entries: 0x3 + * 0000 - SH Names Offset: 0x0 + * + * -------------Program Header------------- + * 06000000 - PH Type: Program Header Table + * 34000000 - Segment Offset: 0x34 + * 34800408 - VAddr of segment: 134512692 + * 34800408 - PAddr of segment: 134512692 + * 00010000 - Size of segment: 256 + * 00010000 - Size of segment: 256 + * 05000000 - Flags: Execute,Read + * 04000000 - Alignment + * + * --------------SH .shstrtab-------------- + * 00000000 - SH Name Offset: 0 + * 03000000 - SH Type: String Table + * 00000000 - Flags: + * 00000000 - VAddr of section: 0 + * cc000000 - Section offset: 204 + * 1c000000 - Section size: 28 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 00000000 - Alignment + * 00000000 - Entry Size: 0 + * + * ---------------SH .symtab--------------- + * 09000000 - SH Name Offset: 9 + * 02000000 - SH Type: Symbol Table + * 00000000 - Flags: + * 00000000 - VAddr of section: 0 + * e6000000 - Section offset: 230 + * 10000000 - Section size: 16 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 00000000 - Alignment + * 10000000 - Entry Size: 16 + * + * ---------------SH .strtab--------------- + * 11000000 - SH Name Offset: 17 + * 03000000 - SH Type: String Table + * 00000000 - Flags: + * 00000000 - VAddr of section: 0 + * f5000000 - Section offset: 245 + * 04000000 - Section size: 4 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 00000000 - Alignment + * 00000000 - Entry Size: 0 + * + * ---------------.shstrtab--------------- + * 2e73687374726162002e73796d746162002e73747274616200 - .shstrab\0.symtab\0.strtab\0 + * + * ----------------.symtab---------------- + * 00000000 - Name Offset: 0 + * 00000000 - Value: 0 + * 00000000 - Size: 0 + * 00 - Info + * 00 - other + * 0000 - shdx + * + * ----------------.strtab---------------- + * 74657374 - test + */ +export const ELF32_LE = "7f454c46010101000000000000000000020003000100000050210608340000005400000000000000340020000100280003000000" + + "0600000034000000348004083480040800010000000100000500000004000000" + + "00000000030000000000000000000000cc0000001c00000000000000000000000000000000000000" + + "09000000020000000000000000000000e60000001000000000000000000000000000000010000000" + + "11000000030000000000000000000000f50000000400000000000000000000000000000000000000" + + "2e73687374726162002e73796d746162002e73747274616200" + + "00000000000000000000000000000000" + + "74657374"; + +/** + * ---------------ELF Header--------------- + * 7f454c46 - Magic: .ELF + * 01 - Format: 32 bit + * 02 - Endianness: Big + * 01 - ELF Version: 1 + * 00 - Target ABI: System V + * 00 - ABI Version: 0 + * 00000000000000 - Padding + * 0002 - File Type: Executable File + * 0003 - ISA: x86 + * 00000001 - ELF Version: 1 + * 08062150 - Entry Point: 0x8062150 + * 00000034 - PH Offset: 0x34 + * 00000054 - SH Offset: 0x54 + * 00000000 - Flags: 0 + * 0034 - ELF Header Size: 0x34 + * 0020 - PH Header Size: 0x20 + * 0001 - PH Entries: 1 + * 0028 - SH Size: 0x28 + * 0003 - SH Entries: 0x3 + * 0000 - SH Names Offset: 0x0 + * + * -------------Program Header------------- + * 00000006 - PH Type: Program Header Table + * 00000034 - Segment Offset: 0x34 + * 08048034 - VAddr of segment: 134512692 + * 08048034 - PAddr of segment: 134512692 + * 00000100 - Size of segment: 256 + * 00000100 - Size of segment: 256 + * 00000005 - Flags: Execute,Read + * 00000004 - Alignment + * + * --------------SH .shstrtab-------------- + * 00000000 - SH Name Offset: 0 + * 00000003 - SH Type: String Table + * 00000000 - Flags: + * 00000000 - VAddr of section: 0 + * 000000cc - Section offset: 204 + * 0000001c - Section size: 28 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 00000000 - Alignment + * 00000000 - Entry Size: 0 + * + * ---------------SH .symtab--------------- + * 00000009 - SH Name Offset: 9 + * 00000002 - SH Type: Symbol Table + * 00000000 - Flags: + * 00000000 - VAddr of section: 0 + * 000000e6 - Section offset: 230 + * 00000010 - Section size: 16 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 00000000 - Alignment + * 00000010 - Entry Size: 16 + * + * ---------------SH .strtab--------------- + * 00000011 - SH Name Offset: 17 + * 00000003 - SH Type: String Table + * 00000000 - Flags: + * 00000000 - VAddr of section: 0 + * 000000f5 - Section offset: 245 + * 00000004 - Section size: 4 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 00000000 - Alignment + * 00000000 - Entry Size: 0 + * + * ---------------.shstrtab--------------- + * 2e73687374726162002e73796d746162002e73747274616200 - .shstrab\0.symtab\0.strtab\0 + * + * ----------------.symtab---------------- + * 00000000 - Name Offset: 0 + * 00000000 - Value: 0 + * 00000000 - Size: 0 + * 00 - Info + * 00 - other + * 0000 - shdx + * + * ----------------.strtab---------------- + * 74657374 - test + */ +export const ELF32_BE = "7f454c46010201000000000000000000000200030000000108062150000000340000005400000000003400200001002800030000" + + "0000000600000034080480340804803400000100000001000000000500000004" + + "00000000000000030000000000000000000000cc0000001c00000000000000000000000000000000" + + "00000009000000020000000000000000000000e60000001000000000000000000000000000000010" + + "00000011000000030000000000000000000000f50000000400000000000000000000000000000000" + + "2e73687374726162002e73796d746162002e73747274616200" + + "00000000000000000000000000000000" + + "74657374"; + +/** + * ---------------ELF Header--------------- + * 7f454c46 - Magic: .ELF + * 02 - Format: 64 bit + * 01 - Endianness: Little + * 01 - ELF Version: 1 + * 00 - Target ABI: System V + * 00 - ABI Version: 0 + * 00000000000000 - Padding + * 0200 - File Type: Executable File + * 3e00 - ISA: AMD x86-64 + * 01000000 - ELF Version: 1 + * 5021060800000000 - Entry Point: 0x8062150 + * 4000000000000000 - PH Offset: 0x40 + * 7800000000000000 - SH Offset: 0x78 + * 00000000 - Flags: 0 + * 4000 - ELF Header Size: 0x40 + * 3800 - PH Header Size: 0x38 + * 0100 - PH Entries: 1 + * 4000 - SH Size: 0x40 + * 0300 - SH Entries: 0x3 + * 0000 - SH Names Offset: 0x0 + * + * -------------Program Header------------- + * 06000000 - PH Type: Program Header Table + * 05000000 - Flags: Execute,Read + * 3400000000000000 - Segment Offset: 0x34 + * 3480040800000000 - VAddr of segment: 134512692 + * 3480040800000000 - PAddr of segment: 134512692 + * 0001000000000000 - Size of segment: 256 + * 0001000000000000 - Size of segment: 256 + * 0400000000000000 - Alignment + * + * --------------SH .shstrtab-------------- + * 00000000 - SH Name Offset: 0 + * 03000000 - SH Type: String Table + * 0000000000000000 - Flags: + * 0000000000000000 - VAddr of section: 0 + * 3801000000000000 - Section offset: 312 + * 1c00000000000000 - Section size: 28 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 0000000000000000 - Alignment + * 0000000000000000 - Entry Size: 0 + * + * ---------------SH .symtab--------------- + * 09000000 - SH Name Offset: 9 + * 02000000 - SH Type: Symbol Table + * 0000000000000000 - Flags: + * 0000000000000000 - VAddr of section: 0 + * 5001000000000000 - Section offset: 336 + * 1000000000000000 - Section size: 16 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 0000000000000000 - Alignment + * 1800000000000000 - Entry Size: 24 + * + * ---------------SH .strtab--------------- + * 11000000 - SH Name Offset: 17 + * 03000000 - SH Type: String Table + * 0000000000000000 - Flags: + * 0000000000000000 - VAddr of section: 0 + * 6901000000000000 - Section offset: 361 + * 0400000000000000 - Section size: 4 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 0000000000000000 - Alignment + * 0000000000000000 - Entry Size: 0 + * + * ---------------.shstrtab--------------- + * 2e73687374726162002e73796d746162002e73747274616200 - .shstrab\0.symtab\0.strtab\0 + * + * ----------------.symtab---------------- + * 00000000 - Name Offset: 0 + * 00 - Info + * 00 - other + * 0000 - shdx + * 0000000000000000 - Value: 0 + * 0000000000000000 - Size: 0 + * + * ----------------.strtab---------------- + * 74657374 - test + */ +export const ELF64_LE = "7f454c4602010100000000000000000002003e000100000050210608000000004000000000000000780000000000000000000000400038000100400003000000" + + "0600000005000000340000000000000034800408000000003480040800000000000100000000000000010000000000000400000000000000" + + "00000000030000000000000000000000000000000000000038010000000000001c00000000000000000000000000000000000000000000000000000000000000" + + "09000000020000000000000000000000000000000000000050010000000000001000000000000000000000000000000000000000000000001800000000000000" + + "11000000030000000000000000000000000000000000000069010000000000000400000000000000000000000000000000000000000000000000000000000000" + + "2e73687374726162002e73796d746162002e73747274616200" + + "000000000000000000000000000000000000000000000000" + + "74657374"; + +/** + * ---------------ELF Header--------------- + * 7f454c46 - Magic: .ELF + * 02 - Format: 64 bit + * 02 - Endianness: Big + * 01 - ELF Version: 1 + * 00 - Target ABI: System V + * 00 - ABI Version: 0 + * 00000000000000 - Padding + * 0002 - File Type: Executable File + * 003e - ISA: AMD x86-64 + * 00000001 - ELF Version: 1 + * 0000000008062150 - Entry Point: 0x8062150 + * 0000000000000040 - PH Offset: 0x40 + * 0000000000000078 - SH Offset: 0x78 + * 00000000 - Flags: 0 + * 0040 - ELF Header Size: 0x40 + * 0038 - PH Header Size: 0x38 + * 0001 - PH Entries: 1 + * 0040 - SH Size: 0x40 + * 0003 - SH Entries: 0x3 + * 0000 - SH Names Offset: 0x0 + * + * -------------Program Header------------- + * 00000006 - PH Type: Program Header Table + * 00000005 - Flags: Execute,Read + * 0000000000000034 - Segment Offset: 0x34 + * 0000000008048034 - VAddr of segment: 134512692 + * 0000000008048034 - PAddr of segment: 134512692 + * 0000000000000100 - Size of segment: 256 + * 0000000000000100 - Size of segment: 256 + * 0400000000000000 - Alignment + * + * --------------SH .shstrtab-------------- + * 00000000 - SH Name Offset: 0 + * 00000003 - SH Type: String Table + * 0000000000000000 - Flags: + * 0000000000000000 - VAddr of section: 0 + * 0000000000000138 - Section offset: 312 + * 000000000000001c - Section size: 28 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 0000000000000000 - Alignment + * 0000000000000000 - Entry Size: 0 + * + * ---------------SH .symtab--------------- + * 00000009 - SH Name Offset: 9 + * 00000002 - SH Type: Symbol Table + * 0000000000000000 - Flags: + * 0000000000000000 - VAddr of section: 0 + * 0000000000000150 - Section offset: 336 + * 0000000000000010 - Section size: 16 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 0000000000000000 - Alignment + * 0000000000000018 - Entry Size: 24 + * + * ---------------SH .strtab--------------- + * 00000011 - SH Name Offset: 17 + * 00000003 - SH Type: String Table + * 0000000000000000 - Flags: + * 0000000000000000 - VAddr of section: 0 + * 0000000000000169 - Section offset: 361 + * 0000000000000004 - Section size: 4 + * 00000000 - Associated: 0 + * 00000000 - Extra Info: 0 + * 0000000000000000 - Alignment + * 0000000000000000 - Entry Size: 0 + * + * ---------------.shstrtab--------------- + * 2e73687374726162002e73796d746162002e73747274616200 - .shstrab\0.symtab\0.strtab\0 + * + * ----------------.symtab---------------- + * 00000000 - Name Offset: 0 + * 00 - Info + * 00 - other + * 0000 - shdx + * 0000000000000000 - Value: 0 + * 0000000000000000 - Size: 0 + * + * ----------------.strtab---------------- + * 74657374 - test + */ +export const ELF64_BE = "7f454c460202010000000000000000000002003e0000000100000000080621500000000000000040000000000000007800000000004000380001004000030000" + + "0000000600000005000000000000003400000000080480340000000008048034000000000000010000000000000001000400000000000000" + + "0000000000000003000000000000000000000000000000000000000000000138000000000000001c000000000000000000000000000000000000000000000000" + + "00000009000000020000000000000000000000000000000000000000000001500000000000000010000000000000000000000000000000000000000000000018" + + "00000011000000030000000000000000000000000000000000000000000001690000000000000004000000000000000000000000000000000000000000000000" + + "2e73687374726162002e73796d746162002e73747274616200" + + "000000000000000000000000000000000000000000000000" + + "74657374";