Skip to content

Commit

Permalink
[dev.cc] liblink: invoke 'go tool objwriter' to implement writeobj, i…
Browse files Browse the repository at this point in the history
…f directed

This CL enables moving the bulk of the object writing code
out of liblink and into translated Go libraries in cmd/internal/obj,
but it does not do the move.

This CL introduces two new environment variables,
$GOOBJ and $GOOBJWRITER, but both will be deleted along with
the rest of the liblink C code.

The default behavior of a build is unchanged by this CL:
the C version of liblink uses the C object layout and writing code.

If $GOOBJ=1, liblink invokes go tool objwriter instead.

If $GOOBJ=2, liblink does its own layout and then invokes
go tool objwriter, which checks that it gets the same answer.

That is, in $GOOBJ=2 mode, both the C and the Go version of
the code run, and the operation fails if the two produce different
answers. This provides a very strong check that the translation
is working correctly.

Change-Id: I56ab49b07ccb2c7b81085f1d6950131047c6aa3c
Reviewed-on: https://go-review.googlesource.com/3048
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
rsc committed Jan 21, 2015
1 parent 35d3987 commit be81836
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 18 deletions.
3 changes: 3 additions & 0 deletions include/link.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct Prog
uchar ft; /* 6l, 8l oclass cache */
uchar tt; // 6l, 8l
uchar isize; // 6l, 8l
uchar printed;

char width; /* fake for DATA */
char mode; /* 16, 32, or 64 in 6l, 8l; internal use in 5g, 6g, 8g */
Expand Down Expand Up @@ -145,6 +146,7 @@ struct LSym
uchar localentry; // ppc64: instrs between global & local entry
uchar seenglobl;
uchar onlist; // on the textp or datap lists
uchar printed;
int16 symid; // for writing .5/.6/.8 files
int32 dynid;
int32 sig;
Expand Down Expand Up @@ -300,6 +302,7 @@ struct Hist
char* name;
int32 line;
int32 offset;
uchar printed;
};

struct Plist
Expand Down
52 changes: 34 additions & 18 deletions src/liblink/objfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,45 @@ static char *rdstring(Biobuf*);
static void rddata(Biobuf*, uchar**, int*);
static LSym *rdsym(Link*, Biobuf*, char*);

void writeobjdirect(Link*, Biobuf*);
void writeobjdirect(Link *ctxt, Biobuf *b);

void writeobjgo1(Link*, char*);
void writeobjgo2(Link*, char*, int64);

extern char *outfile;

void
writeobj(Link *ctxt, Biobuf *b)
{
char *cmd[3];

// TODO(rsc): Use 'go tool objwriter' to write object file,
// allowing the bulk of liblink to be moved into Go.
// As a first step, we check that we can invoke objwriter at all
// (it is an empty program for now).
// This tests the cmd/dist bootstrap process, making sure
// that objwriter is available when it needs to be.
// Once the support mechanisms are there, we can put the
// real code in.

cmd[0] = smprint("%s/pkg/tool/%s_%s/objwriter", getgoroot(), getgohostos(), getgohostarch());
cmd[1] = "ping";
cmd[2] = nil;
if(runcmd(cmd) < 0)
sysfatal("cannot run objwriter: %r");
vlong start;
char *env;

// If $GOOBJ > 0, invoke the Go version of the liblink
// output routines via a subprocess.
// If $GOOBJ == 1, copy that subprocess's output to
// the actual output file.
// If $GOOBJ >= 2, generate output using the usual C version
// but then check that the subprocess wrote the same bytes.
// $GOOBJ is a temporary setting for the transition to a
// Go liblink back end. Once the C liblink back ends are deleted,
// we will hard code the GOOBJ=1 behavior.
env = getenv("GOOBJ");
if(env == nil)
env = "2";
if(atoi(env) == 0) {
writeobjdirect(ctxt, b);
return;
}

writeobjdirect(ctxt, b);
Bflush(b);
start = Boffset(b);
writeobjgo1(ctxt, outfile);
if(atoi(env) > 1) {
writeobjdirect(ctxt, b);
Bflush(b);
}
writeobjgo2(ctxt, outfile, start);
Bseek(b, 0, 2);
}

// The Go and C compilers, and the assembler, call writeobj to write
Expand Down
Loading

0 comments on commit be81836

Please sign in to comment.