scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit c66bb872cd601ff3c80ae698013bfe62371ac5c1
parent e2e71bfb86d012abec03e47f20aaae56e0b84bd3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 30 Jan 2025 16:44:04 +0100

libmach/elf: Add is32 field to elf

This field will help to know what is the output format when
we have to write an output file. At this moment it only helps
to double check the correctness of the input file, that is a
bit silly because the type was derived from the input file too.

Diffstat:
Minclude/bits/scc/elf.h | 2++
Msrc/libmach/elf/elfarchs.c | 1+
Msrc/libmach/elf/elfnew.c | 7+++++++
Msrc/libmach/elf/elfread.c | 10+++++++++-
4 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/include/bits/scc/elf.h b/include/bits/scc/elf.h @@ -81,6 +81,7 @@ struct elfsym { struct elf { Elfhdr hdr; Elfphdr *phdr; + int is32; Elfsec *secs; int nsec; @@ -101,6 +102,7 @@ struct arch { int mach; int endian; int type; + int is32; }; /* globals */ diff --git a/src/libmach/elf/elfarchs.c b/src/libmach/elf/elfarchs.c @@ -14,6 +14,7 @@ struct arch elfarchs[] = { .mach = EM_X86_64, .endian = ELFDATA2LSB, .type = OBJ(ELF, ARCHAMD64, LITTLE_ENDIAN), + .is32 = 0, }, NULL, }; diff --git a/src/libmach/elf/elfnew.c b/src/libmach/elf/elfnew.c @@ -11,9 +11,16 @@ int elfnew(Obj *obj, int type) { struct elf *elf; + struct arch *ap; if ((elf = calloc(1, sizeof(*elf))) == NULL) return -1; + for (ap = elfarchs; ap->name; ++ap) { + if (ap->type == type) + break; + } + + elf->is32 = (ap->name) ? ap->is32 : -1; obj->data = elf; obj->index = "/"; return 0; diff --git a/src/libmach/elf/elfread.c b/src/libmach/elf/elfread.c @@ -712,7 +712,15 @@ elfread(Obj *obj, FILE *fp) fread(buf, sizeof(buf), 1, fp); fsetpos(fp, &pos); - elf->unpack = (buf[EI_CLASS] == ELFCLASS64) ? &unpack64 : &unpack32; + if (buf[EI_CLASS] == ELFCLASS64) { + if (elf->is32) + return 0; + elf->is32 = 0; + elf->unpack = &unpack64; + } else { + elf->is32 = 1; + elf->unpack = &unpack32; + } if (!readhdr(obj, fp)) return -1;