Compare commits
4 Commits
7be1d6a967
...
faeab33375
Author | SHA1 | Date | |
---|---|---|---|
faeab33375 | |||
7e414f8576 | |||
933680c80d | |||
891df1803e |
@ -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.value)
|
||||||
|
|
||||||
struct elf_section {
|
struct elf_section {
|
||||||
GElf_Shdr section_header;
|
GElf_Shdr section_header;
|
||||||
@ -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,
|
||||||
@ -261,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 */
|
||||||
@ -306,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user