13 Commits

7 changed files with 179 additions and 78 deletions

View File

@@ -1,14 +1,14 @@
# Maintainer: Mario Hüttel <mario (dot) huettel (!) gmx (dot) net> # Maintainer: Mario Hüttel <mario (dot) huettel (!) gmx (dot) net>
pkgname=patchelfcrc pkgname=patchelfcrc
pkgver=5e7f697 pkgver=v1.0.0_rc1
pkgrel=1 pkgrel=1
pkgdesc="Tool for patching CRC checksums of sections into ELF binaries" pkgdesc="Tool for patching CRC checksums of sections into ELF binaries"
arch=('i686' 'x86_64') arch=('i686' 'x86_64')
url="https://git.shimatta.de/mhu/patchelfcrc" url="https://git.shimatta.de/mhu/patchelfcrc"
licence=('GPLv2') licence=('GPLv2')
depends=('libelf' 'libxml2') depends=('libelf' 'libxml2')
makedepends=('cmake' 'pandoc' 'git' 'gvim') makedepends=('cmake' 'pandoc' 'git' 'gvim' 'bash')
provides=('patchelfcrc') provides=('patchelfcrc')
source=("${pkgname}-git"::"git+https://git.shimatta.de/mhu/patchelfcrc" "git+https://git.shimatta.de/3rd-party/libfort.git" "git+https://git.shimatta.de/mhu/linklist-lib") source=("${pkgname}-git"::"git+https://git.shimatta.de/mhu/patchelfcrc" "git+https://git.shimatta.de/3rd-party/libfort.git" "git+https://git.shimatta.de/mhu/linklist-lib")
sha1sums=('SKIP' 'SKIP' 'SKIP') sha1sums=('SKIP' 'SKIP' 'SKIP')

View File

@@ -10,10 +10,10 @@ add_custom_command(
OUTPUT OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME} ${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}
COMMAND COMMAND
bash -c "pandoc \"${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md\" -s -t man | gzip > \"${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}\"" bash -c "cat \"${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md\" | sed \"s/!version!/`git describe --tags --always --dirty`/\" | pandoc -s -t man | gzip > \"${CMAKE_CURRENT_BINARY_DIR}/${MAN_PAGE_NAME}\""
VERBATIM VERBATIM
WORKING_DIRECTORY WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
MAIN_DEPENDENCY MAIN_DEPENDENCY
${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md ${CMAKE_CURRENT_SOURCE_DIR}/patchelfcrc.1.md
) )

View File

@@ -1,4 +1,4 @@
% patchelfcrc(1) 0.0.2 % patchelfcrc(1) !version!
% Mario Huettel % Mario Huettel
% October 2022 % October 2022
@@ -136,4 +136,4 @@ The output sections start and end are checked for the given magic numbers in ord
The memory is interpreted as *little endian* and the CRC calculation granularity is a 32 bit *word*. The memory is interpreted as *little endian* and the CRC calculation granularity is a 32 bit *word*.
# BUGS # BUGS
Currently, reversed CRC algorithms are not implemented. None

View File

@@ -34,13 +34,26 @@ int crc_len_from_poly(uint64_t polynomial)
return pos; return pos;
} }
static uint32_t reverse_short_poly(uint32_t poly, uint8_t len)
{
uint8_t i;
uint32_t ret = 0ul;
for (i = 0; i < len; i++) {
ret <<= 1;
ret |= (poly & 1u);
poly >>= 1;
}
return ret;
}
static uint64_t shorten_polynomial(uint64_t poly) static uint64_t shorten_polynomial(uint64_t poly)
{ {
int i; int i;
for (i = 32; i >= 0; i--) {
for (i = 31; i <= 0; i--) { if (poly & ((uint64_t)1ull << i)) {
if (poly & (1 << i)) { poly &= ~((uint64_t)1ull<<i);
poly &= ~(1<<i);
break; break;
} }
} }
@@ -55,14 +68,22 @@ static void internal_push_byte(struct crc_calc *crc, const uint8_t *data, size_t
crc_val = crc->crc_val; crc_val = crc->crc_val;
for (i = 0; i < len; i++, data++) { if (crc->settings.rev) {
crc_val = ((crc_val << 8) & crc->crc_mask) ^ crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data]; for (i = 0; i < len; i++, data++) {
crc_val = (crc_val >> 8) ^ crc->table[((crc_val & 0xFF) ^ *data)];
}
} else {
/* Non reversed algo */
for (i = 0; i < len; i++, data++) {
crc_val = ((crc_val << 8) & crc->crc_mask) ^
crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data];
}
} }
crc->crc_val = crc_val; crc->crc_val = crc_val;
} }
static void fill_crc_table(struct crc_calc *crc) static void fill_crc_table_non_reversed(struct crc_calc *crc)
{ {
uint32_t input; uint32_t input;
uint32_t crc_reg; uint32_t crc_reg;
@@ -86,10 +107,44 @@ static void fill_crc_table(struct crc_calc *crc)
crc_reg <<= 1; crc_reg <<= 1;
} }
} }
crc->table[input] = crc_reg; crc->table[input] = crc_reg & crc->crc_mask;
} }
} }
static void fill_crc_table_reversed(struct crc_calc *crc)
{
uint32_t input;
uint32_t crc_reg;
uint32_t short_poly;
int i;
short_poly = (uint32_t)shorten_polynomial(crc->settings.polynomial);
short_poly = reverse_short_poly(short_poly, crc->crc_length);
for (input = 0; input <= 255u; input++) {
crc_reg = (uint32_t)input;
for (i = 0; i < 8; i++) {
/* Check LSB for reversed CRC shifting */
if (crc_reg & 1u) {
crc_reg >>= 1;
crc_reg ^= short_poly;
} else {
crc_reg >>= 1;
}
}
crc->table[input] = crc_reg & crc->crc_mask;
}
}
static void fill_crc_table(struct crc_calc *crc)
{
if (crc->settings.rev)
fill_crc_table_reversed(crc);
else
fill_crc_table_non_reversed(crc);
}
void crc_init(struct crc_calc *crc, const struct crc_settings *settings) void crc_init(struct crc_calc *crc, const struct crc_settings *settings)
{ {
uint32_t i; uint32_t i;

View File

@@ -30,6 +30,19 @@
#include <fort.h> #include <fort.h>
#include <inttypes.h> #include <inttypes.h>
#include <patchelfcrc/crc-output-struct.h> #include <patchelfcrc/crc-output-struct.h>
#include <byteswap.h>
static const union {
uint8_t data[4];
uint32_t val;
} _endianess_check_union = {{1u, 2u, 3u, 4u}};
enum endianess {
END_LITTLE = 0x04030201ul,
END_BIG = 0x01020304ul,
};
#define HOST_ENDIANESS (_endianess_check_union.val)
struct elf_section { struct elf_section {
GElf_Shdr section_header; GElf_Shdr section_header;
@@ -55,16 +68,16 @@ struct elfpatch {
#define is_elfpatch_struct(x) ((x) && (x)->magic == (ELFPATCH_MAGIC)) #define is_elfpatch_struct(x) ((x) && (x)->magic == (ELFPATCH_MAGIC))
#define ret_if_ep_err(ep) do { \ #define ret_if_ep_err(ep) do { \
if (!is_elfpatch_struct((ep))) { \ if (!is_elfpatch_struct((ep))) { \
return; \ return; \
} \ } \
} while(0) } while (0)
#define ret_val_if_ep_err(ep, val) do { \ #define ret_val_if_ep_err(ep, val) do { \
if (!is_elfpatch_struct((ep))) { \ if (!is_elfpatch_struct((ep))) { \
return (val); \ return val; \
} \ } \
} while(0) } while (0)
/** /**
* @brief Convert a series of 4 bytes into a uint32_t dpending on endianess * @brief Convert a series of 4 bytes into a uint32_t dpending on endianess
@@ -77,15 +90,16 @@ static uint32_t get_uint32_from_byte_string(const uint8_t *data, bool little_end
uint32_t out = 0ul; uint32_t out = 0ul;
int i; int i;
/* Always shift in in big endian format */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
if (little_endian)
out >>= 8u;
else
out <<= 8u; out <<= 8u;
out |= (uint32_t)data[i];
out |= (((uint32_t)data[i]) << (little_endian ? 24u : 0u));
} }
/* Swap bytes if little endian */
if (little_endian)
out = bswap_32(out);
return out; return out;
} }
@@ -96,14 +110,12 @@ static void write_crc_to_byte_array(uint8_t *byte_array, uint32_t crc, uint8_t c
if (!byte_array) if (!byte_array)
return; return;
if (!little_endian)
crc = bswap_32(crc);
for (i = 0; i < crc_size_bytes; i++) { for (i = 0; i < crc_size_bytes; i++) {
if (little_endian) { byte_array[i] = (uint8_t)(crc & 0xFFul);
byte_array[i] = (uint8_t)(crc & 0xFFul); crc >>= 8u;
crc >>= 8u;
} else {
byte_array[i] = (uint8_t)((crc & 0xFF000000ul) >> 24u);
crc <<= 8u;
}
} }
} }
@@ -136,6 +148,14 @@ static const char *section_type_to_str(Elf64_Word type)
return "INIT_ARRAY"; return "INIT_ARRAY";
case SHT_FINI_ARRAY: case SHT_FINI_ARRAY:
return "FINI_ARRAY"; return "FINI_ARRAY";
case SHT_PREINIT_ARRAY:
return "PREINIT_ARRAY";
case SHT_DYNAMIC:
return "DYNAMIC";
case SHT_ARM_ATTRIBUTES:
return "ARM_ATTRIBUTES";
case SHT_ARM_PREEMPTMAP:
return "ARM_PREEMPTMAP";
default: default:
break; break;
} }
@@ -147,6 +167,7 @@ static void print_sections(elfpatch_handle_t *ep)
SlList *iter; SlList *iter;
ft_table_t *table; ft_table_t *table;
const struct elf_section *section; const struct elf_section *section;
bool alloc, write, exec;
ret_if_ep_err(ep); ret_if_ep_err(ep);
@@ -162,15 +183,23 @@ static void print_sections(elfpatch_handle_t *ep)
/* Write header */ /* Write header */
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "Section", "Type", "Size", "VMA", "LMA", "File Offset"); ft_write_ln(table, "Section", "Type", "ALLOC", "WRITE", "EXEC", "Size", "VMA", "LMA", "File Offset");
for (iter = ep->sections; iter; iter = sl_list_next(iter)) { for (iter = ep->sections; iter; iter = sl_list_next(iter)) {
section = (const struct elf_section *)iter->data; section = (const struct elf_section *)iter->data;
if (!section) if (!section)
continue; continue;
ft_printf_ln(table, "%s|%s|%lu|%p|%p|%p",
alloc = !!(section->section_header.sh_flags & SHF_ALLOC);
write = !!(section->section_header.sh_flags & SHF_WRITE);
exec = !!(section->section_header.sh_flags & SHF_EXECINSTR);
ft_printf_ln(table, "%s|%s|%s|%s|%s|%lu|%p|%p|%p",
section->name, section->name,
section_type_to_str(section->section_header.sh_type), section_type_to_str(section->section_header.sh_type),
alloc ? "x" : "",
write ? "x" : "",
exec ? "x" : "",
section->section_header.sh_size, section->section_header.sh_size,
(void *)section->section_header.sh_addr, (void *)section->section_header.sh_addr,
(void *)section->lma, (void *)section->lma,
@@ -197,7 +226,7 @@ static SlList *elf_patch_get_sections(elfpatch_handle_t *ep)
sl_list_free_full(ret, (void (*)(void *))free_elf_section_element); sl_list_free_full(ret, (void (*)(void *))free_elf_section_element);
ep->sections = NULL; ep->sections = NULL;
if (elf_getshdrstrndx (ep->elf , &shstrndx) != 0) { if (elf_getshdrstrndx(ep->elf, &shstrndx) != 0) {
print_err("ELF error: %s\n", elf_errmsg(-1)); print_err("ELF error: %s\n", elf_errmsg(-1));
goto ret_free_section_list; goto ret_free_section_list;
} }
@@ -218,9 +247,10 @@ static SlList *elf_patch_get_sections(elfpatch_handle_t *ep)
sec->lma = (uint64_t)sec->section_header.sh_addr; sec->lma = (uint64_t)sec->section_header.sh_addr;
name = elf_strptr(ep->elf, shstrndx, sec->section_header.sh_name); name = elf_strptr(ep->elf, shstrndx, sec->section_header.sh_name);
if (name) {
if (name)
sec->name = strdup(name); sec->name = strdup(name);
}
ret = sl_list_append(ret, sec); ret = sl_list_append(ret, sec);
} }
@@ -260,6 +290,12 @@ static int elf_patch_read_program_headers(elfpatch_handle_t *ep)
return -1; return -1;
} }
if (header_count == 0) {
/* No program headers found. This ELF file is probably not linked */
ep->program_headers_count = 0;
return 0;
}
ep->program_headers = (GElf_Phdr *)malloc(header_count * sizeof(GElf_Phdr)); ep->program_headers = (GElf_Phdr *)malloc(header_count * sizeof(GElf_Phdr));
if (!ep->program_headers) { if (!ep->program_headers) {
/* Mem error. Abort. Program will crash eventually */ /* Mem error. Abort. Program will crash eventually */
@@ -305,9 +341,11 @@ static void resolve_section_lmas(elfpatch_handle_t *ep)
if (!sec) if (!sec)
continue; continue;
/* By default each sections LMA is assumed to be its LMA as well */
sec->lma = (uint64_t)sec->section_header.sh_addr;
if (sec->section_header.sh_type == SHT_NOBITS) { if (sec->section_header.sh_type == SHT_NOBITS) {
/* Section does not contain data. It may be allocated but is not loaded. Therefore, LMA=VMA. */ /* Section does not contain data. It may be allocated but is not loaded. Therefore, LMA=VMA. */
sec->lma = (uint64_t)sec->section_header.sh_addr;
continue; continue;
} }
@@ -431,15 +469,13 @@ elfpatch_handle_t *elf_patch_open(const char *path, bool readonly, bool expect_l
switch (ident[5]) { switch (ident[5]) {
case 1: case 1:
print_debug("ELF Endianess: little\n"); print_debug("ELF Endianess: little\n");
if (!expect_little_endian) { if (!expect_little_endian)
print_err("Big endian format expected. File is little endian. Double check settings!\n"); print_err("Big endian format expected. File is little endian. Double check settings!\n");
}
break; break;
case 2: case 2:
print_debug("ELF Endianess: big\n"); print_debug("ELF Endianess: big\n");
if (expect_little_endian) { if (expect_little_endian)
print_err("Little endian format expected. File is big endian. Double check settings!\n"); print_err("Little endian format expected. File is big endian. Double check settings!\n");
}
break; break;
default: default:
print_err("Cannot determine endianess of ELF file. EI_DATA is: %d\n", ident[5]); print_err("Cannot determine endianess of ELF file. EI_DATA is: %d\n", ident[5]);
@@ -454,9 +490,8 @@ close_elf:
ep->elf = NULL; ep->elf = NULL;
} }
close_fd: close_fd:
if (ep->fd > 0) { if (ep->fd > 0)
close(ep->fd); close(ep->fd);
}
free_struct: free_struct:
free(ep); free(ep);
ep = NULL; ep = NULL;
@@ -491,14 +526,14 @@ int elf_patch_check_for_section(elfpatch_handle_t *ep, const char *section)
return ret; return ret;
} }
static size_t translate_index(size_t index, enum granularity granularity, bool little_endian) static size_t translate_index(size_t index, enum granularity granularity, bool little_endian, bool reversed)
{ {
size_t word_idx; size_t word_idx;
size_t part_idx; size_t part_idx;
size_t d_index; size_t d_index;
size_t gran_in_bytes; size_t gran_in_bytes;
if (!little_endian || granularity == GRANULARITY_BYTE) if ((!little_endian && !reversed) || (little_endian && reversed) || granularity == GRANULARITY_BYTE)
return index; return index;
gran_in_bytes = (size_t)granularity / 8u; gran_in_bytes = (size_t)granularity / 8u;
@@ -548,8 +583,9 @@ int elf_patch_compute_crc_over_section(elfpatch_handle_t *ep, const char *sectio
return -2; return -2;
} }
/* If big endian or granularity is byte, simply compute CRC. No reordering is necessary */ /* If big endian for non reversed / little endian for reversed or granularity is byte, simply compute CRC. No reordering is necessary */
if (!little_endian || granularity == GRANULARITY_BYTE) { if ((!little_endian && !crc->settings.rev) || (little_endian && crc->settings.rev) ||
granularity == GRANULARITY_BYTE) {
crc_push_bytes(crc, data->d_buf, data->d_size); crc_push_bytes(crc, data->d_buf, data->d_size);
} else { } else {
/* Little endian case with > byte sized chunks */ /* Little endian case with > byte sized chunks */
@@ -561,14 +597,17 @@ int elf_patch_compute_crc_over_section(elfpatch_handle_t *ep, const char *sectio
section, padding_count); section, padding_count);
} }
for (idx = 0; idx < data->d_size; idx++) { for (idx = 0; idx < data->d_size; idx++)
crc_push_byte(crc, ((char *)data->d_buf)[translate_index(idx, granularity, little_endian)]); crc_push_byte(crc,
} ((char *)data->d_buf)[
translate_index(idx, granularity,
little_endian,
crc->settings.rev)
]);
/* Pad with zeroes */ /* Pad with zeroes */
for (idx = 0; idx < padding_count; idx++) { for (idx = 0; idx < padding_count; idx++)
crc_push_byte(crc, 0x00); crc_push_byte(crc, 0x00);
}
} }
return 0; return 0;
@@ -649,6 +688,7 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
struct crc_out_struct_32bit crc_32bit; struct crc_out_struct_32bit crc_32bit;
struct crc_out_struct_64bit crc_64bit; struct crc_out_struct_64bit crc_64bit;
uint64_t in_sec_addr, in_sec_len; uint64_t in_sec_addr, in_sec_len;
bool needs_byteswap;
ret_val_if_ep_err(ep, -1000); ret_val_if_ep_err(ep, -1000);
@@ -711,8 +751,8 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
print_debug("Single CRC requires %u bytes.\n", (unsigned int)crc_size_bytes); print_debug("Single CRC requires %u bytes.\n", (unsigned int)crc_size_bytes);
needed_space = calculate_needed_space_for_crcs(format, crc_data->elf_bits, check_start_magic, check_end_magic, crc_size_bytes, needed_space = calculate_needed_space_for_crcs(format, crc_data->elf_bits, check_start_magic,
crc_count); check_end_magic, crc_size_bytes, crc_count);
print_debug("Required space for %zu CRCs%s: %zu (available: %zu)\n", print_debug("Required space for %zu CRCs%s: %zu (available: %zu)\n",
crc_count, crc_count,
@@ -746,6 +786,12 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
if (check_start_magic && crc_data->elf_bits == 64) if (check_start_magic && crc_data->elf_bits == 64)
sec_bytes += 4u; sec_bytes += 4u;
needs_byteswap = false;
if ((HOST_ENDIANESS != END_LITTLE && little_endian) ||
(HOST_ENDIANESS == END_LITTLE && !little_endian)) {
needs_byteswap = true;
}
for (iter = crc_data->crc_entries, idx = 0; iter; iter = sl_list_next(iter), idx++) { for (iter = crc_data->crc_entries, idx = 0; iter; iter = sl_list_next(iter), idx++) {
crc_entry = (struct crc_entry *)iter->data; crc_entry = (struct crc_entry *)iter->data;
in_sec_addr = use_vma ? crc_entry->vma : crc_entry->lma; in_sec_addr = use_vma ? crc_entry->vma : crc_entry->lma;
@@ -756,18 +802,19 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
print_debug("Corresponding input section at 0x%"PRIx64", length: %"PRIu64"\n", print_debug("Corresponding input section at 0x%"PRIx64", length: %"PRIu64"\n",
in_sec_addr, in_sec_addr,
in_sec_len); in_sec_len);
if (crc_data->elf_bits == 32) { if (crc_data->elf_bits == 32) {
crc_32bit.crc = crc_entry->crc; crc_32bit.crc = needs_byteswap ? bswap_32(crc_entry->crc) : crc_entry->crc;
crc_32bit.length = (uint32_t)in_sec_len; crc_32bit.length = needs_byteswap ? bswap_32((uint32_t)in_sec_len) : (uint32_t)in_sec_len;
crc_32bit.start_address = (uint32_t)in_sec_addr; crc_32bit.start_address = needs_byteswap ? bswap_32((uint32_t)in_sec_addr) : (uint32_t)in_sec_addr;
memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit)); memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit));
sec_bytes += sizeof(crc_32bit); sec_bytes += sizeof(crc_32bit);
} else { } else {
/* 64 bit case */ /* 64 bit case */
crc_64bit.crc = crc_entry->crc; crc_64bit.crc = needs_byteswap ? bswap_32(crc_entry->crc) : crc_entry->crc;
crc_64bit._unused_dummy = 0ul; crc_64bit._unused_dummy = 0ul;
crc_64bit.length = in_sec_len; crc_64bit.length = needs_byteswap ? bswap_64(in_sec_len) : in_sec_len;
crc_64bit.start_address = in_sec_addr; crc_64bit.start_address = needs_byteswap ? bswap_64(in_sec_addr) : in_sec_addr;
memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit)); memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit));
sec_bytes += sizeof(crc_64bit); sec_bytes += sizeof(crc_64bit);
} }
@@ -782,11 +829,10 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
crc_64bit.length = 0ull; crc_64bit.length = 0ull;
crc_64bit.start_address = 0ull; crc_64bit.start_address = 0ull;
if (crc_data->elf_bits == 32) { if (crc_data->elf_bits == 32)
memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit)); memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit));
} else { else
memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit)); memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit));
}
} }
/* Flag section data as invalid to trigger rewrite. /* Flag section data as invalid to trigger rewrite.
@@ -808,9 +854,8 @@ void elf_patch_close_and_free(elfpatch_handle_t *ep)
if (ep->readonly) { if (ep->readonly) {
print_debug("DRY RUN: File will not be updated\n"); print_debug("DRY RUN: File will not be updated\n");
} else { } else {
if (elf_update(ep->elf, ELF_C_WRITE) < 0) { if (elf_update(ep->elf, ELF_C_WRITE) < 0)
print_err("Error writing ELF file: %s\n", elf_errmsg(-1)); print_err("Error writing ELF file: %s\n", elf_errmsg(-1));
}
} }
} }

View File

@@ -452,12 +452,6 @@ int main(int argc, char **argv)
if (!cmd_opts.output_section && cmd_opts.export_xml == NULL) if (!cmd_opts.output_section && cmd_opts.export_xml == NULL)
print_err("No output section / XML export specified. Will continue but not create any output\n"); print_err("No output section / XML export specified. Will continue but not create any output\n");
/* Do error printing if using a reversed polynomial. It is not implemented yet! */
if (cmd_opts.crc.rev) {
print_err("Reversed polynomials are not supported yet\nExiting...\n");
goto free_cmds;
}
/* Prepare libelf for use with the latest ELF version */ /* Prepare libelf for use with the latest ELF version */
elf_version(EV_CURRENT); elf_version(EV_CURRENT);

View File

@@ -110,19 +110,26 @@ void list_predefined_crcs(void)
{ {
ft_table_t *table; ft_table_t *table;
const struct named_crc *iter; const struct named_crc *iter;
struct crc_calc crc;
table = ft_create_table(); table = ft_create_table();
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "Name", "Polynomial", "Reversed", "Start Value", "Output XOR"); ft_write_ln(table, "Name", "Polynomial", "Reversed", "Start Value", "Output XOR", "Test Value");
for (iter = predefined_crc_table; iter->name; iter++) { for (iter = predefined_crc_table; iter->name; iter++) {
ft_printf_ln(table, "%s|0x%lx|%s|0x%x|0x%x", crc_init(&crc, &iter->settings);
/* Calculate the test value */
crc_push_bytes(&crc, (const uint8_t *)"123456789", 9);
crc_finish_calc(&crc);
ft_printf_ln(table, "%s|0x%lx|%s|0x%x|0x%x|0x%x",
iter->name, iter->name,
iter->settings.polynomial, iter->settings.polynomial,
iter->settings.rev ? "yes" : "no", iter->settings.rev ? "yes" : "no",
iter->settings.start_value, iter->settings.start_value,
iter->settings.xor); iter->settings.xor,
crc_get_value(&crc));
crc_destroy(&crc);
} }
printf("%s\n", ft_to_string(table)); printf("%s\n", ft_to_string(table));