ref: 7f6552d4e83fdb7ad2f27df890459261d1a45492
parent: 552e89a27a4cb01785604e7aa049184eb940a996
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Jan 19 10:21:10 EST 2020
ld: Add mkfile() This function simplifies the creation of all the temporary files.
--- a/src/cmd/ld/pass3.c
+++ b/src/cmd/ld/pass3.c
@@ -44,8 +44,9 @@
void
pass3(int argc, char *argv[])
{
+ int i;
Obj *obj;
- Objsec *sp;
+ Section *sec;
Segment *seg;
/*
@@ -57,8 +58,9 @@
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 @@
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);
}
}
--- a/src/cmd/ld/section.c
+++ b/src/cmd/ld/section.c
@@ -84,6 +84,8 @@
}
p[n++] = sec;
seg->sections = p;
+
+ /* rebase(obj, sec->index, seg->size); */
seg->size += sec->size;
}
@@ -90,15 +92,16 @@
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));
@@ -105,35 +108,31 @@
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;
+ FILE *fp;
- 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);
- }
-
+ fp = mkfile(sec, nbytes);
while (nbytes-- > 0)
- putc(0, sp->tmpfp);
-
- sec->size += nbytes;
+ putc(0, fp);
}
#ifndef NDEBUG