Compare commits

..

No commits in common. "b47828014e5e8805f4d66f4abb0dcb7e26d1a749" and "dc85955859b7ffb06ce138b726a8fb2f23247a7e" have entirely different histories.

2 changed files with 29 additions and 24 deletions

View File

@ -56,8 +56,7 @@ 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++) { for (i = 0; i < len; i++, data++) {
crc_val = ((crc_val << 8) & crc->crc_mask) ^ crc_val = ((crc_val << 8) & crc->crc_mask) ^ crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data];
crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data];
} }
crc->crc_val = crc_val; crc->crc_val = crc_val;

View File

@ -62,7 +62,7 @@ struct elfpatch {
#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)
@ -218,10 +218,9 @@ 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);
} }
@ -432,13 +431,15 @@ 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]);
@ -453,8 +454,9 @@ 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;
@ -559,13 +561,15 @@ 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)]);
}
/* 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;
} }
@ -707,8 +711,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, needed_space = calculate_needed_space_for_crcs(format, crc_data->elf_bits, check_start_magic, check_end_magic, crc_size_bytes,
check_end_magic, crc_size_bytes, crc_count); 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,
@ -778,11 +782,12 @@ 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.
* This is needed due to the forced memory layout * This is needed due to the forced memory layout
@ -803,10 +808,11 @@ 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));
} }
} }
}
if (ep->elf) if (ep->elf)
elf_end(ep->elf); elf_end(ep->elf);