scc

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

commit 1542722ccdfba37389ccc09fb382df9f975e70e6
parent 5fb4bf82c962c831624ee7d1c1c1e75bf67291d8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  5 Mar 2024 09:40:57 +0100

as: Create object and attach sections

This is a first step to be able to write out an actual binary
and not the dump that we are doing now.

Diffstat:
Msrc/cmd/as/as.h | 2+-
Msrc/cmd/as/main.c | 2+-
Msrc/cmd/as/symbol.c | 53++++++++++++++++++++++++++++++++++++++++++++---------
3 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/src/cmd/as/as.h b/src/cmd/as/as.h @@ -111,7 +111,7 @@ union yylval { /* symbol.c */ extern void cleansecs(void); -extern void isecs(void); +extern void ibinfmt(void); extern void emit(char *, int); extern Section *defsec(char *, char *); extern Symbol *tmpsym(TUINT); diff --git a/src/cmd/as/main.c b/src/cmd/as/main.c @@ -140,7 +140,7 @@ main(int argc, char *argv[]) atexit(cleanup); iarch(); - isecs(); + ibinfmt(); asm(argv); writeout(outfile); diff --git a/src/cmd/as/symbol.c b/src/cmd/as/symbol.c @@ -37,12 +37,13 @@ struct lsection { struct lsection *next; }; -Map *map; Section *cursec; Section *sabs, *sbss, *sdata, *stext; Symbol *linesym; int pass; +static Obj *obj; +static Map *map; static struct lsection *seclist; static struct lsymbol *hashtbl[HASHSIZ], *symlast, *symlist; @@ -259,18 +260,25 @@ secflags(char *attr) return flags; } +static int +sectype(int flags) +{ + if (flags & SEXEC) + return 'T'; + if ((flags & (SALLOC|SLOAD)) == SALLOC|SLOAD) + return 'D'; + if ((flags & (SALLOC|SLOAD)) == SLOAD) + return 'B'; + return '?'; +} + static Section * newsec(Symbol *sym) { + int idx; Section *sec; struct lsection *lsec; struct lsymbol *lsym; - static int index; - - if (setmap(map, sym->name, NULL, 0, 0, 0) < 0) { - perror("as"); - exit(EXIT_FAILURE); - } lsec = xmalloc(sizeof(*lsec)); lsec->pc = lsec->curpc = 0; @@ -284,12 +292,25 @@ newsec(Symbol *sym) sec->flags = 0; sec->fill = 0; sec->align = 0; - sec->index = index++; setmap(map, sym->name, NULL, 0, 0, 0); lsym = (struct lsymbol *) sym; lsym->sec = sec; + if (setmap(map, sym->name, NULL, 0, 0, 0) < 0) { + fprintf(stderr, + "as: error allocating section mapping '%s'\n", + sym->name); + exit(EXIT_FAILURE); + } + + if (setsec(obj, &sec->index, sec) < 0) { + fprintf(stderr, + "as: error adding section '%s' to output\n", + sym->name); + exit(EXIT_FAILURE); + } + return sec; } @@ -314,13 +335,27 @@ defsec(char *name, char *attr) sym->flags = FSECT; } sec->flags |= secflags(attr); + sec->type = sectype(sec->flags); return cursec = sec; } void -isecs(void) +ibinfmt(void) { + int t; + + if ((t = objtype("coff32-z80")) < 0) { + fprintf(stderr, + "as: invalid binary format %s\n", "coff32-z80"); + exit(EXIT_FAILURE); + } + + if ((obj = newobj(t)) < 0) { + fputs("as: error allocating output\n", stderr); + exit(EXIT_FAILURE); + } + if ((map = newmap(NULL, 4)) == NULL) { perror("as"); exit(EXIT_FAILURE);