libmach/elf: Add loadmap() - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e2e71bfb86d012abec03e47f20aaae56e0b84bd3
 (DIR) parent f485fa123db6f14395f034c90705edde591e759a
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
       Date:   Tue, 28 Jan 2025 14:50:25 +0100
       
       libmach/elf: Add loadmap()
       
       Diffstat:
         M src/libmach/elf/Makefile            |       1 +
         A src/libmach/elf/elfloadmap.c        |      42 +++++++++++++++++++++++++++++++
         M src/libmach/loadmap.c               |       1 +
       
       3 files changed, 44 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/src/libmach/elf/Makefile b/src/libmach/elf/Makefile
       @@ -8,6 +8,7 @@ OBJS =\
                elfdel.o\
                elfgetsec.o\
                elfgetsym.o\
       +        elfloadmap.o\
                elfnew.o\
                elfprobe.o\
                elfread.o\
 (DIR) diff --git a/src/libmach/elf/elfloadmap.c b/src/libmach/elf/elfloadmap.c
       @@ -0,0 +1,42 @@
       +#include <stdio.h>
       +
       +#include <scc/mach.h>
       +#include <scc/elf.h>
       +
       +#include "../libmach.h"
       +#include "fun.h"
       +
       +Map *
       +elfloadmap(Obj *obj, FILE *fp)
       +{
       +        long i;
       +        Map *map;
       +        char *name;
       +        long nsec;
       +        FILE *src;
       +        Elfsec *shdr;
       +        Elf *elf = obj->data;
       +        Elfhdr *hdr = &elf->hdr;
       +
       +        nsec = hdr->shnum;
       +        if ((map = newmap(NULL, nsec)) == NULL)
       +                return NULL;
       +
       +        for (shdr = elf->secs; nsec--; ++shdr) {
       +                unsigned long o;
       +                unsigned long long b = shdr->addralign;
       +                unsigned long long e = b + shdr->size;
       +
       +                if (shdr->offset != 0) {
       +                        o = obj->pos + shdr->offset;
       +                        src = fp;
       +                } else {
       +                        o = 0;
       +                        src = NULL;
       +                }
       +
       +                setmap(map, name, src, b, e, o);
       +        }
       +
       +        return map;
       +}
 (DIR) diff --git a/src/libmach/loadmap.c b/src/libmach/loadmap.c
       @@ -9,6 +9,7 @@
        
        static Map *(*ops[NFORMATS])(Obj *, FILE *) = {
                [COFF32] = coff32loadmap,
       +        [ELF] = elfloadmap,
        };
        
        Map *