scc

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

commit e42a0145f6bd89a95cc420f703f42cfb7bcd25bd
parent 3703ba5fe4d255ac4918021108f8d6a339131bde
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 19 Jan 2020 15:21:10 +0100

ld: Add mkfile()

This function simplifies the creation of all the temporary
files.

Diffstat:
Msrc/cmd/ld/pass3.c | 15++++++++-------
Msrc/cmd/ld/section.c | 41++++++++++++++++++++---------------------
2 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/src/cmd/ld/pass3.c b/src/cmd/ld/pass3.c @@ -44,8 +44,9 @@ rebase(Obj *obj) void pass3(int argc, char *argv[]) { + int i; Obj *obj; - Objsec *sp; + Section *sec; Segment *seg; /* @@ -57,8 +58,9 @@ pass3(int argc, char *argv[]) bss.base = data.base + data.size; for (obj = objhead; obj; obj = obj->next) { - for (sp = obj->secs; sp; sp = sp->next) { - switch (sp->type) { + for (i = 0; getsec(obj, &i, &sec); i++) { + /* TODO: deal with symbol aligment */ + switch (sec->type) { case 'T': seg = &text; break; @@ -72,10 +74,9 @@ pass3(int argc, char *argv[]) default: abort(); } - sp->base = seg->base + seg->size; - /* TODO: deal with symbol aligment */ - seg->size += sp->size; + + rebase(obj, i, seg->size); + seg->size += sec.size; } - rebase(obj); } } diff --git a/src/cmd/ld/section.c b/src/cmd/ld/section.c @@ -84,56 +84,55 @@ merge(Segment *seg) } p[n++] = sec; seg->sections = p; + + /* rebase(obj, sec->index, seg->size); */ seg->size += sec->size; } seg->nsec = n; } -void -copy(Obj *obj, Section *osec, Section *sec) +static FILE * +mkfile(Section *sec, unsigned long long size) { struct sectab *sp = (struct sectab *) sec; - if (sec->size > ULLONG_MAX - osec->size) { + if (sec->size > ULLONG_MAX - size) { error("%s: section too long", sec->name); - return; + exit(EXIT_FAILURE); } + sec->size += size; if (!sp->tmpfp && (sp->tmpfp = tmpfile()) == NULL) { error(strerror(errno)); exit(EXIT_FAILURE); } + return sp->tmpfp; +} + +void +copy(Obj *obj, Section *osec, Section *sec) +{ + FILE *fp; + + fp = mkfile(sec, osec->size); /* - if (mapsec(obj, osec->index, sp->tmpfp) < 0) { + if (mapsec(obj, osec->index, fp) < 0) { error(strerror(errno)); return; } */ - - sec->size += osec->size; } void grow(Section *sec, int nbytes) { - struct sectab *sp = (struct sectab *) sec; - - if (sec->size > ULLONG_MAX - nbytes) { - error("%s: section too long", sec->name); - return; - } - - if (!sp->tmpfp && (sp->tmpfp = tmpfile()) == NULL) { - error(strerror(errno)); - exit(EXIT_FAILURE); - } + FILE *fp; + fp = mkfile(sec, nbytes); while (nbytes-- > 0) - putc(0, sp->tmpfp); - - sec->size += nbytes; + putc(0, fp); } #ifndef NDEBUG