ref: 266132e9d24058d85928f324cbfa7d0b0f46ba63
parent: 25bac1a306eed889cd22ac809776bf29fdef9078
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Aug 19 13:02:14 EDT 2019
[ld] Add pass4 This pass is combining every section in a temporary file. This is only a skeleton because it doesn't care about alignment or rellocations.
--- a/src/cmd/ld/deps.mk
+++ b/src/cmd/ld/deps.mk
@@ -9,6 +9,8 @@
./pass2.o: ./ld.h
./pass3.o: $(INCDIR)/scc/scc/mach.h
./pass3.o: ./ld.h
+./pass4.o: $(INCDIR)/scc/scc/mach.h
+./pass4.o: ./ld.h
./symbol.o: $(INCDIR)/scc/scc/mach.h
./symbol.o: $(INCDIR)/scc/scc/scc.h
./symbol.o: ./ld.h
--- a/src/cmd/ld/ld.h
+++ b/src/cmd/ld/ld.h
@@ -8,8 +8,8 @@
struct section {
char *name;
- unsigned long base;
- unsigned long long size;
+ unsigned long long base;
+ unsigned long size;
unsigned flags;
int type;
FILE *fp;
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -8,10 +8,6 @@
#include "ld.h"
-char *output = "a.out", *entry = "start";
-
-char *filename, *membname;
-
int sflag; /* discard all the symbols */
int xflag; /* discard local symbols */
int Xflag; /* discard locals starting with 'L' */
@@ -20,6 +16,14 @@
int gflag; /* preserve debug symbols */
char *Dflag; /* size of data */
+char *filename, *membname;
+
+Segment text = {.type = 'T'};
+Segment rodata = {.type = 'R'};
+Segment data = {.type = 'D'};
+Segment bss = {.type = 'B'};
+
+static char *output = "a.out", *entry = "start";
static int status;
char *
@@ -55,6 +59,7 @@
* pass1: Get the list of object files that are going to be linked.
* pass2: Calculate the size of every segment.
* pass3: Rebase all symbols in sections
+ * pass4: Create the temporary files per section
*/
static void
ld(int argc, char*argv[])
@@ -62,6 +67,7 @@
pass1(argc, argv);
pass2(argc, argv);
pass3(argc, argv);
+ pass4(argc, argv);
debugsym();
debugsec();
}
--- a/src/cmd/ld/pass2.c
+++ b/src/cmd/ld/pass2.c
@@ -6,12 +6,6 @@
#include "ld.h"
-Segment text = {.type = 'T'};
-Segment rodata = {.type = 'R'};
-Segment data = {.type = 'D'};
-Segment bss = {.type = 'B'};
-Segment debug = {.type = 'N'};
-
static void
mksecs(void)
{
@@ -22,7 +16,6 @@
for (lp = objhead; lp; lp = lp->next) {
for (sp = lp->obj->secs; sp; sp = sp->next) {
sec = section(sp->name);
-
if (sec->type == '?') {
sec->type = sp->type;
sec->flags = sp->flags;
@@ -33,6 +26,7 @@
sec->name);
}
+ /* TODO: use add symbol aligment */
sec->size += sp->size;
}
}
@@ -67,7 +61,6 @@
merge(&rodata);
merge(&data);
merge(&bss);
- merge(&debug);
}
void
--- /dev/null
+++ b/src/cmd/ld/pass4.c
@@ -1,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <scc/mach.h>
+
+#include "ld.h"
+
+void
+pass4(int argc, char *argv[])
+{
+ Objlst *lp;
+ Objsect *sp;
+ Section *sec;
+ unsigned long i;
+ FILE *fp;
+
+ for (lp = objhead; lp; lp = lp->next) {
+ fp = lp->obj->fp;
+ for (sp = lp->obj->secs; sp; sp = sp->next) {
+ fseek(fp, sp->seek, SEEK_SET);
+
+ sec = section(sp->name);
+ if (!sec->fp) {
+ sec->fp = tmpfile();
+ if (!sec->fp) {
+ error("out of memory");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ /* TODO: add symbol alignment */
+
+ for (i = 0; i < sp->size; i++)
+ putc(getc(fp), sec->fp);
+
+ /* TODO: Apply relocations */
+ }
+ }
+}