scc

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

commit 8d23e114fea69c8652fc1482b1d7b4af032f223a
parent 6bbca83d59e09b38fbcce458581616d8ec92789e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 18 Jan 2025 23:40:16 +0100

libmach: Add hack to know if a section has relocations

Copying back the number of relocations works only in coff,
because it is not easy to get that data in other formats
like a.out or elf. As getsec() is used in many different
contexts it does not make sense to make it more complex
and slow only to satisfy objdump. This is just a fast
way to get some output in the case of coff, but we do
know that it will have to be changed in the future.

Diffstat:
Minclude/bits/scc/mach.h | 1+
Msrc/libmach/coff32/coff32getsec.c | 3++-
Msrc/libmach/elf64/elf64getsec.c | 6++++++
3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/include/bits/scc/mach.h b/include/bits/scc/mach.h @@ -98,6 +98,7 @@ struct section { unsigned long long size; unsigned long long offset; + unsigned long nreloc; unsigned flags; int index; int align; diff --git a/src/libmach/coff32/coff32getsec.c b/src/libmach/coff32/coff32getsec.c @@ -85,9 +85,10 @@ coff32getsec(Obj *obj, int *idx, Section *sec) sec->base = scn->s_vaddr; sec->load = scn->s_paddr; sec->offset = scn->s_scnptr; + sec->nreloc = scn->s_nrelloc; sec->type = type; sec->flags = sflags; - sec->align = 16; + sec->align = 4; return sec; } diff --git a/src/libmach/elf64/elf64getsec.c b/src/libmach/elf64/elf64getsec.c @@ -49,6 +49,11 @@ elf64getsec(Obj *obj, int *idx, Section *sec) * We cannot differentiate between load and base address * in a section, while we can use the physical address * for that when dealing with segments. + * Also, we don't have an easy way to know the number of + * relocations affecting one section. To know that, we + * have to run over the relocation sections and find one + * with the sh_link pointing to this section. Maybe, + * we should just remove the nrelloc field. */ sec->name = elf64str(obj, SEC_STRTBL, shdr->sh_name); sec->index = n; @@ -56,6 +61,7 @@ elf64getsec(Obj *obj, int *idx, Section *sec) sec->base = shdr->sh_addr; sec->load = shdr->sh_addr; sec->offset = shdr->sh_offset; + sec->nreloc = 0; sec->type = stype; sec->flags = sflags; sec->align = shdr->sh_addralign;