Skip to content

Commit

Permalink
tools/embed: add ELF type conversion functions
Browse files Browse the repository at this point in the history
Converts the pointers in the ELF embedder state structure
to a pointer to a lone ELF structure type.

Fixes gcc build errors because unsigned char * cannot be converted
implicitly to the structure types. Both GCC and clang reject this
code as invalid when compiled in my laptop and in GitHub actions.
The clang provided by Termux doesn't complain though. Weird...
  • Loading branch information
matheusmoreira committed Aug 21, 2024
1 parent c2edb94 commit 62a141a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions source/tools/lone-embed.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ struct elf {
} data;
};

static struct lone_elf_header *
hdr(struct elf *elf)
{
return (struct lone_elf_header *) elf->header.pointer;
}

static struct lone_elf_segment *
segs(struct elf *elf)
{
return (struct lone_elf_segment *) elf->header.pointer;
}

static size_t pht_size_for(struct elf *elf, size_t entry_count)
{
return elf->segments.table.segment.size * entry_count;
Expand Down Expand Up @@ -199,7 +211,7 @@ static void validate_elf_header(struct elf *elf)
{
elf->class = LONE_ELF_IDENT_CLASS_INVALID;

if (lone_elf_header_is_valid(elf->header.pointer)) {
if (lone_elf_header_is_valid(hdr(elf))) {
elf->class = elf->header.pointer[LONE_ELF_IDENT_INDEX_CLASS];
} else {
invalid_elf();
Expand All @@ -213,13 +225,13 @@ static void load_program_header_table(struct elf *elf)
lone_elf_umax size;
void *address;

offset = lone_elf_header_read_segments_offset(elf->header.pointer);
offset = lone_elf_header_read_segments_offset(hdr(elf));
if (!offset.present) { invalid_elf(); }

entry_size = lone_elf_header_read_segment_size(elf->header.pointer);
entry_size = lone_elf_header_read_segment_size(hdr(elf));
if (!entry_size.present) { invalid_elf(); }

entry_count = lone_elf_header_read_segment_count(elf->header.pointer);
entry_count = lone_elf_header_read_segment_count(hdr(elf));
if (!entry_count.present) { invalid_elf(); }

elf->segments.nulls_count = 0;
Expand Down Expand Up @@ -290,7 +302,7 @@ static void set_start_end(lone_elf_umax *start, lone_elf_umax *end,

static void analyze(struct elf *elf)
{
struct lone_elf_header *header = elf->header.pointer;
struct lone_elf_header *header = hdr(elf);
lone_u16 entry_count = elf->segments.table.segment.count;
struct lone_elf_segment *segment;
struct lone_optional_u32 type;
Expand Down Expand Up @@ -344,8 +356,8 @@ static void adjust_phdr_entry(struct elf *elf)
lone_elf_umax alignment, size, size_aligned, offset, virtual, physical;
lone_u32 type;

header = elf->header.pointer;
segments = elf->segments.table.segments;
header = hdr(elf);
segments = segs(elf);
entry_count = elf->segments.table.segment.count;
phdr = 0;
load = 0;
Expand Down

0 comments on commit 62a141a

Please sign in to comment.