1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:44:40 -07:00

5l, 6l, 8l: link pclntab and symtab as ordinary rodata symbols

That is, move the pc/ln table and the symbol table
into the read-only data segment.  This eliminates
the need for a special load command to map the
symbol table into memory, which makes the
information available on systems that couldn't handle
the magic load to 0x99000000, like NaCl and ARM QEMU
and Linux without config_highmem=y.  It also
eliminates an #ifdef and some clumsy code to
find the symbol table on Windows.

The bad news is that the binary appears to be bigger
than it used to be.  This is not actually the case, though:
the same amount of data is being mapped into memory
as before, and the tables are still read-only, so they're
still shared across multiple instances of the binary as
they were before.  The difference is just that the tables
aren't squirreled away in some section that "size" doesn't
know to look at.

This is a checkpoint.
It probably breaks Windows and breaks NaCl more
than it used to be broken, but those will be fixed.
The logic involving -s needs to be revisited too.

Fixes #871.

R=ken2
CC=golang-dev
https://golang.org/cl/2587041
This commit is contained in:
Russ Cox 2010-10-19 18:07:19 -04:00
parent 01df088f8d
commit 19fd5c787f
26 changed files with 420 additions and 735 deletions

View File

@ -293,8 +293,7 @@ asmb(void)
{ {
int32 t; int32 t;
int a, dynsym; int a, dynsym;
uint32 va, fo, w, symo, startva; uint32 va, fo, w, startva;
uint32 symdatva = SYMDATVA;
int strtabsize; int strtabsize;
ElfEhdr *eh; ElfEhdr *eh;
ElfPhdr *ph, *pph; ElfPhdr *ph, *pph;
@ -302,7 +301,6 @@ asmb(void)
Section *sect; Section *sect;
strtabsize = 0; strtabsize = 0;
symo = 0;
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f asmb\n", cputime()); Bprint(&bso, "%5.2f asmb\n", cputime());
@ -333,6 +331,7 @@ asmb(void)
symsize = 0; symsize = 0;
lcsize = 0; lcsize = 0;
if(!debug['s']) { if(!debug['s']) {
// TODO: rationalize
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f sym\n", cputime()); Bprint(&bso, "%5.2f sym\n", cputime());
Bflush(&bso); Bflush(&bso);
@ -353,25 +352,11 @@ asmb(void)
break; break;
case 6: case 6:
OFFSET += segdata.filelen; OFFSET += segdata.filelen;
symo = rnd(OFFSET, INITRND); seek(cout, rnd(OFFSET, INITRND), 0);
seek(cout, symo + 8, 0);
break; break;
} }
if(!debug['s'])
asmsym();
if(debug['v'])
Bprint(&bso, "%5.2f pc\n", cputime());
Bflush(&bso);
if(!debug['s'])
asmlc();
if(!debug['s']) if(!debug['s'])
asmthumbmap(); asmthumbmap();
if(!debug['s'])
strnput("", INITRND-(8+symsize+lcsize)%INITRND);
cflush();
seek(cout, symo, 0);
lputl(symsize);
lputl(lcsize);
cflush(); cflush();
} }
@ -479,15 +464,6 @@ asmb(void)
elfphload(&segtext); elfphload(&segtext);
elfphload(&segdata); elfphload(&segdata);
if(!debug['s']) {
segsym.rwx = 04;
segsym.vaddr = symdatva;
segsym.len = rnd(8+symsize+lcsize, INITRND);
segsym.fileoff = symo;
segsym.filelen = segsym.len;
elfphload(&segsym);
}
/* Dynamic linking sections */ /* Dynamic linking sections */
if (!debug['d']) { /* -d suppresses dynamic loader format */ if (!debug['d']) { /* -d suppresses dynamic loader format */
/* S headers for dynamic linking */ /* S headers for dynamic linking */
@ -576,38 +552,17 @@ asmb(void)
elfshbits(sect); elfshbits(sect);
if (!debug['s']) { if (!debug['s']) {
fo = symo;
w = 8;
sh = newElfShdr(elfstr[ElfStrGosymtab]); sh = newElfShdr(elfstr[ElfStrGosymtab]);
sh->type = SHT_PROGBITS; sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC; sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = INITRND;
sh->addr = symdatva;
fo += w;
w = symsize;
sh = newElfShdr(elfstr[ElfStrGosymtab]);
sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1; sh->addralign = 1;
sh->addr = symdatva + 8; shsym(sh, lookup("symtab", 0));
fo += w;
w = lcsize;
sh = newElfShdr(elfstr[ElfStrGopclntab]); sh = newElfShdr(elfstr[ElfStrGopclntab]);
sh->type = SHT_PROGBITS; sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC; sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1; sh->addralign = 1;
sh->addr = symdatva + 8 + lcsize; shsym(sh, lookup("pclntab", 0));
} }
sh = newElfShstrtab(elfstr[ElfStrShstrtab]); sh = newElfShstrtab(elfstr[ElfStrShstrtab]);
@ -833,7 +788,7 @@ if(debug['P']) print("%ux: %P type %d\n", (uint32)(p->pc), p, o->type);
break; break;
case 0: /* pseudo ops */ case 0: /* pseudo ops */
if(debug['G']) print("%ux: %s: arm %d %d %d %d\n", (uint32)(p->pc), p->from.sym->name, p->from.sym->thumb, p->from.sym->foreign, p->from.sym->fnptr, p->from.sym->used); if(debug['G']) print("%ux: %s: arm %d %d %d\n", (uint32)(p->pc), p->from.sym->name, p->from.sym->thumb, p->from.sym->foreign, p->from.sym->fnptr);
break; break;
case 1: /* op R,[R],R */ case 1: /* op R,[R],R */
@ -1914,3 +1869,65 @@ chipfloat(Ieee *e)
return -1; return -1;
} }
void
genasmsym(void (*put)(Sym*, char*, int, vlong, vlong, int, Sym*))
{
Auto *a;
Sym *s;
int h;
s = lookup("etext", 0);
if(s->type == STEXT)
put(s, s->name, 'T', s->value, s->size, s->version, 0);
for(h=0; h<NHASH; h++) {
for(s=hash[h]; s!=S; s=s->hash) {
switch(s->type) {
case SCONST:
case SRODATA:
case SDATA:
case SELFDATA:
if(!s->reachable)
continue;
put(s, s->name, 'D', s->value, s->size, s->version, s->gotype);
continue;
case SBSS:
if(!s->reachable)
continue;
put(s, s->name, 'B', s->value, s->size, s->version, s->gotype);
continue;
case SFILE:
put(nil, s->name, 'f', s->value, 0, s->version, 0);
continue;
}
}
}
for(s = textp; s != nil; s = s->next) {
/* filenames first */
for(a=s->autom; a; a=a->link)
if(a->type == D_FILE)
put(nil, a->asym->name, 'z', a->aoffset, 0, 0, 0);
else
if(a->type == D_FILE1)
put(nil, a->asym->name, 'Z', a->aoffset, 0, 0, 0);
put(s, s->name, 'T', s->value, s->size, s->version, s->gotype);
/* frame, auto and param after */
put(nil, ".frame", 'm', s->text->to.offset+4, 0, 0, 0);
for(a=s->autom; a; a=a->link)
if(a->type == D_AUTO)
put(nil, a->asym->name, 'a', -a->aoffset, 0, 0, a->gotype);
else
if(a->type == D_PARAM)
put(nil, a->asym->name, 'p', a->aoffset, 0, 0, a->gotype);
}
if(debug['v'] || debug['n'])
Bprint(&bso, "symsize = %ud\n", symsize);
Bflush(&bso);
}

View File

@ -135,7 +135,7 @@ struct Sym
int32 value; int32 value;
int32 sig; int32 sig;
int32 size; int32 size;
uchar used; uchar special;
uchar thumb; // thumb code uchar thumb; // thumb code
uchar foreign; // called by arm if thumb, by thumb if arm uchar foreign; // called by arm if thumb, by thumb if arm
uchar fnptr; // used as fn ptr uchar fnptr; // used as fn ptr
@ -212,7 +212,6 @@ enum
SXREF, SXREF,
SFILE, SFILE,
SCONST, SCONST,
SFIXED,
LFROM = 1<<0, LFROM = 1<<0,
LTO = 1<<1, LTO = 1<<1,
@ -384,7 +383,6 @@ void asmb(void);
void asmthumbmap(void); void asmthumbmap(void);
void asmout(Prog*, Optab*, int32*); void asmout(Prog*, Optab*, int32*);
void thumbasmout(Prog*, Optab*); void thumbasmout(Prog*, Optab*);
void asmsym(void);
int32 atolwhex(char*); int32 atolwhex(char*);
Prog* brloop(Prog*); Prog* brloop(Prog*);
void buildop(void); void buildop(void);
@ -433,7 +431,6 @@ void prasm(Prog*);
void prepend(Prog*, Prog*); void prepend(Prog*, Prog*);
Prog* prg(void); Prog* prg(void);
int pseudo(Prog*); int pseudo(Prog*);
void putsymb(char*, int, int32, int);
int32 regoff(Adr*); int32 regoff(Adr*);
int relinv(int); int relinv(int);
int32 rnd(int32, int32); int32 rnd(int32, int32);

View File

@ -70,7 +70,6 @@ main(int argc, char *argv[])
{ {
int c, i; int c, i;
//debug['s'] = 1; // qemu cannot handle symdat load
Binit(&bso, 1, OWRITE); Binit(&bso, 1, OWRITE);
cout = -1; cout = -1;
listinit(); listinit();
@ -259,6 +258,8 @@ main(int argc, char *argv[])
softfloat(); softfloat();
noops(); noops();
span(); span();
pclntab();
symtab();
dodata(); dodata();
address(); address();
reloc(); reloc();

View File

@ -102,7 +102,7 @@ fnpinc(Sym *s)
if(!debug['f']) if(!debug['f'])
diag("fnptr == 0 in fnpinc"); diag("fnptr == 0 in fnpinc");
if(s->foreign) if(s->foreign)
diag("bad usage in fnpinc %s %d %d %d", s->name, s->used, s->foreign, s->thumb); diag("bad usage in fnpinc %s %d %d", s->name, s->foreign, s->thumb);
return 0; return 0;
} }
/* 0, 1, 2, 3 squared */ /* 0, 1, 2, 3 squared */
@ -524,6 +524,7 @@ xdefine(char *p, int t, int32 v)
s->type = t; s->type = t;
s->value = v; s->value = v;
s->reachable = 1; s->reachable = 1;
s->special = 1;
} }
int32 int32
@ -592,19 +593,20 @@ symaddr(Sym *s)
return 0; return 0;
case STEXT: case STEXT:
/* TODO(rsc): what is this for?
#ifdef CALLEEBX #ifdef CALLEEBX
v += fnpinc(s); v += fnpinc(s);
#else #else
if(s->thumb) if(s->thumb)
v++; // T bit v++; // T bit
#endif #endif
*/
break; break;
case SELFDATA: case SELFDATA:
case SRODATA: case SRODATA:
case SDATA: case SDATA:
case SBSS: case SBSS:
case SFIXED:
case SCONST: case SCONST:
break; break;
} }

View File

@ -1,153 +0,0 @@
// Inferno utils/5l/asm.c
// http://code.google.com/p/inferno-os/source/browse/utils/5l/asm.c
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Symbol table.
#include "l.h"
#include "../ld/lib.h"
void
asmsym(void)
{
Prog *p;
Auto *a;
Sym *s;
int h;
s = lookup("etext", 0);
if(s->type == STEXT)
putsymb(s->name, 'T', s->value, s->version);
for(h=0; h<NHASH; h++)
for(s=hash[h]; s!=S; s=s->hash) {
if(!s->reachable)
continue;
switch(s->type) {
case SCONST:
case SDATA:
case SELFDATA:
putsymb(s->name, 'D', s->value, s->version);
continue;
case SBSS:
case SFIXED:
putsymb(s->name, 'B', s->value, s->version);
continue;
case SFILE:
putsymb(s->name, 'f', s->value, s->version);
continue;
}
}
for(s=textp; s!=nil; s=s->next) {
p = s->text;
if(s->type != STEXT)
continue;
/* filenames first */
for(a=s->autom; a; a=a->link)
if(a->type == D_FILE)
putsymb(a->asym->name, 'z', a->aoffset, 0);
else
if(a->type == D_FILE1)
putsymb(a->asym->name, 'Z', a->aoffset, 0);
if(!s->reachable)
continue;
if(s->leaf == 0)
putsymb(s->name, 'T', s->value, s->version);
else
putsymb(s->name, 'L', s->value, s->version);
/* frame, auto and param after */
putsymb(".frame", 'm', p->to.offset+4, 0);
for(a=s->autom; a; a=a->link)
if(a->type == D_AUTO)
putsymb(a->asym->name, 'a', -a->aoffset, 0);
else
if(a->type == D_PARAM)
putsymb(a->asym->name, 'p', a->aoffset, 0);
}
if(debug['v'] || debug['n'])
Bprint(&bso, "symsize = %ud\n", symsize);
Bflush(&bso);
}
void
putsymb(char *s, int t, int32 v, int ver)
{
int i, f;
if(t == 'f')
s++;
lput(v);
if(ver)
t += 'a' - 'A';
cput(t+0x80); /* 0x80 is variable length */
if(t == 'Z' || t == 'z') {
cput(s[0]);
for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) {
cput(s[i]);
cput(s[i+1]);
}
cput(0);
cput(0);
i++;
}
else {
for(i=0; s[i]; i++)
cput(s[i]);
cput(0);
}
// TODO(rsc): handle go parameter
lput(0);
symsize += 4 + 1 + i + 1 + 4;
if(debug['n']) {
if(t == 'z' || t == 'Z') {
Bprint(&bso, "%c %.8ux ", t, v);
for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) {
f = ((s[i]&0xff) << 8) | (s[i+1]&0xff);
Bprint(&bso, "/%x", f);
}
Bprint(&bso, "\n");
return;
}
if(ver)
Bprint(&bso, "%c %.8ux %s<%d>\n", t, v, s, ver);
else
Bprint(&bso, "%c %.8ux %s\n", t, v, s);
}
}

View File

@ -347,7 +347,6 @@ asmb(void)
int32 magic; int32 magic;
int a, dynsym; int a, dynsym;
vlong vl, va, startva, fo, w, symo, elfsymo, elfstro, elfsymsize, machlink; vlong vl, va, startva, fo, w, symo, elfsymo, elfstro, elfsymsize, machlink;
vlong symdatva = SYMDATVA;
ElfEhdr *eh; ElfEhdr *eh;
ElfPhdr *ph, *pph; ElfPhdr *ph, *pph;
ElfShdr *sh; ElfShdr *sh;
@ -438,16 +437,12 @@ asmb(void)
* line number table * line number table
*/ */
seek(cout, symo+8, 0); seek(cout, symo+8, 0);
if(!debug['s'])
asmsym();
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f sp\n", cputime()); Bprint(&bso, "%5.2f sp\n", cputime());
Bflush(&bso); Bflush(&bso);
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f pc\n", cputime()); Bprint(&bso, "%5.2f pc\n", cputime());
Bflush(&bso); Bflush(&bso);
if(!debug['s'])
asmlc();
if(!debug['s']) if(!debug['s'])
strnput("", INITRND-(8+symsize+lcsize)%INITRND); strnput("", INITRND-(8+symsize+lcsize)%INITRND);
cflush(); cflush();
@ -458,7 +453,7 @@ asmb(void)
if(!debug['s']) { if(!debug['s']) {
elfsymo = symo+8+symsize+lcsize; elfsymo = symo+8+symsize+lcsize;
seek(cout, elfsymo, 0); seek(cout, elfsymo, 0);
asmelfsym(); asmelfsym64();
cflush(); cflush();
elfstro = seek(cout, 0, 1); elfstro = seek(cout, 0, 1);
elfsymsize = elfstro - elfsymo; elfsymsize = elfstro - elfsymo;
@ -503,7 +498,7 @@ asmb(void)
lputb(lcsize); /* line offsets */ lputb(lcsize); /* line offsets */
break; break;
case 6: case 6:
asmbmacho(symdatva, symo); asmbmacho();
break; break;
case 7: case 7:
case 9: case 9:
@ -551,15 +546,6 @@ asmb(void)
elfphload(&segtext); elfphload(&segtext);
elfphload(&segdata); elfphload(&segdata);
if(!debug['s']) {
segsym.rwx = 04;
segsym.vaddr = symdatva;
segsym.len = rnd(8+symsize+lcsize, INITRND);
segsym.fileoff = symo;
segsym.filelen = segsym.len;
elfphload(&segsym);
}
/* Dynamic linking sections */ /* Dynamic linking sections */
if (!debug['d']) { /* -d suppresses dynamic loader format */ if (!debug['d']) { /* -d suppresses dynamic loader format */
/* S headers for dynamic linking */ /* S headers for dynamic linking */
@ -649,38 +635,17 @@ asmb(void)
elfshbits(sect); elfshbits(sect);
if (!debug['s']) { if (!debug['s']) {
fo = symo;
w = 8;
sh = newElfShdr(elfstr[ElfStrGosymcounts]);
sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1;
sh->addr = symdatva;
fo += w;
w = symsize;
sh = newElfShdr(elfstr[ElfStrGosymtab]); sh = newElfShdr(elfstr[ElfStrGosymtab]);
sh->type = SHT_PROGBITS; sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC; sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1; sh->addralign = 1;
sh->addr = symdatva + 8; shsym(sh, lookup("symtab", 0));
fo += w;
w = lcsize;
sh = newElfShdr(elfstr[ElfStrGopclntab]); sh = newElfShdr(elfstr[ElfStrGopclntab]);
sh->type = SHT_PROGBITS; sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC; sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1; sh->addralign = 1;
sh->addr = symdatva + 8 + symsize; shsym(sh, lookup("pclntab", 0));
sh = newElfShdr(elfstr[ElfStrSymtab]); sh = newElfShdr(elfstr[ElfStrSymtab]);
sh->type = SHT_SYMTAB; sh->type = SHT_SYMTAB;
@ -769,3 +734,62 @@ rnd(vlong v, vlong r)
v -= c; v -= c;
return v; return v;
} }
void
genasmsym(void (*put)(Sym*, char*, int, vlong, vlong, int, Sym*))
{
Auto *a;
Sym *s;
int h;
for(h=0; h<NHASH; h++) {
for(s=hash[h]; s!=S; s=s->hash) {
switch(s->type) {
case SCONST:
case SRODATA:
case SDATA:
case SELFDATA:
case SMACHO:
if(!s->reachable)
continue;
put(s, s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
continue;
case SBSS:
if(!s->reachable)
continue;
put(s, s->name, 'B', symaddr(s), s->size, s->version, s->gotype);
continue;
case SFILE:
put(nil, s->name, 'f', s->value, 0, s->version, 0);
continue;
}
}
}
for(s = textp; s != nil; s = s->next) {
/* filenames first */
for(a=s->autom; a; a=a->link)
if(a->type == D_FILE)
put(nil, a->asym->name, 'z', a->aoffset, 0, 0, 0);
else
if(a->type == D_FILE1)
put(nil, a->asym->name, 'Z', a->aoffset, 0, 0, 0);
put(s, s->name, 'T', 0, s->size, s->version, s->gotype);
/* frame, auto and param after */
put(nil, ".frame", 'm', s->text->to.offset+8, 0, 0, 0);
for(a=s->autom; a; a=a->link)
if(a->type == D_AUTO)
put(nil, a->asym->name, 'a', -a->aoffset, 0, 0, a->gotype);
else
if(a->type == D_PARAM)
put(nil, a->asym->name, 'p', a->aoffset, 0, 0, a->gotype);
}
if(debug['v'] || debug['n'])
Bprint(&bso, "symsize = %ud\n", symsize);
Bflush(&bso);
}

View File

@ -127,6 +127,7 @@ struct Sym
uchar dupok; uchar dupok;
uchar reachable; uchar reachable;
uchar dynexport; uchar dynexport;
uchar special;
int32 sig; int32 sig;
Sym* hash; // in hash table Sym* hash; // in hash table
Sym* next; // in text or data list Sym* next; // in text or data list
@ -180,7 +181,6 @@ enum
SMACHO, SMACHO,
SFILE, SFILE,
SCONST, SCONST,
SFIXED,
NHASH = 10007, NHASH = 10007,
NHUNK = 100000, NHUNK = 100000,
@ -397,7 +397,6 @@ void doprof2(void);
void dostkoff(void); void dostkoff(void);
vlong entryvalue(void); vlong entryvalue(void);
void follow(void); void follow(void);
void genasmsym(void (*put)(char*, int, vlong, vlong, int, Sym*));
void gethunk(void); void gethunk(void);
void gotypestrings(void); void gotypestrings(void);
void listinit(void); void listinit(void);

View File

@ -413,7 +413,7 @@ Iconv(Fmt *fp)
// call fmtstrcpy to handle padding. // call fmtstrcpy to handle padding.
fmtstrinit(&fmt); fmtstrinit(&fmt);
for(i=0; i<n; i++) for(i=0; i<n; i++)
fmtprint(&fmt, "%.8ux", *p++); fmtprint(&fmt, "%.2ux", *p++);
s = fmtstrflush(&fmt); s = fmtstrflush(&fmt);
fmtstrcpy(fp, s); fmtstrcpy(fp, s);
free(s); free(s);

View File

@ -245,6 +245,8 @@ main(int argc, char *argv[])
else else
doprof2(); doprof2();
span(); span();
pclntab();
symtab();
dodata(); dodata();
address(); address();
reloc(); reloc();

View File

@ -196,6 +196,7 @@ xdefine(char *p, int t, vlong v)
s->type = t; s->type = t;
s->value = v; s->value = v;
s->reachable = 1; s->reachable = 1;
s->special = 1;
} }
void void
@ -671,9 +672,6 @@ vlong
symaddr(Sym *s) symaddr(Sym *s)
{ {
switch(s->type) { switch(s->type) {
case SFIXED:
return s->value;
case SMACHO: case SMACHO:
return segdata.vaddr + segdata.filelen - dynptrsize + s->value; return segdata.vaddr + segdata.filelen - dynptrsize + s->value;
@ -702,25 +700,18 @@ vaddr(Adr *a, Reloc *r)
case D_STATIC: case D_STATIC:
case D_EXTERN: case D_EXTERN:
s = a->sym; s = a->sym;
switch(s->type) { if(!s->reachable)
case SFIXED: diag("unreachable symbol in vaddr - %s", s->name);
v += s->value; if(r == nil) {
break; diag("need reloc for %D", a);
default: errorexit();
if(!s->reachable)
diag("unreachable symbol in vaddr - %s", s->name);
if(r == nil) {
diag("need reloc for %D", a);
errorexit();
}
r->type = D_ADDR;
r->siz = 4; // TODO: 8 for external symbols
r->off = -1; // caller must fill in
r->sym = s;
r->add = v;
v = 0;
break;
} }
r->type = D_ADDR;
r->siz = 4; // TODO: 8 for external symbols
r->off = -1; // caller must fill in
r->sym = s;
r->add = v;
v = 0;
} }
return v; return v;
} }

View File

@ -41,7 +41,6 @@
char linuxdynld[] = "/lib/ld-linux.so.2"; char linuxdynld[] = "/lib/ld-linux.so.2";
char freebsddynld[] = "/usr/libexec/ld-elf.so.1"; char freebsddynld[] = "/usr/libexec/ld-elf.so.1";
uint32 symdatva = SYMDATVA;
int32 int32
entryvalue(void) entryvalue(void)
@ -371,6 +370,7 @@ asmb(void)
lcsize = 0; lcsize = 0;
symo = 0; symo = 0;
if(!debug['s']) { if(!debug['s']) {
// TODO: rationalize
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f sym\n", cputime()); Bprint(&bso, "%5.2f sym\n", cputime());
Bflush(&bso); Bflush(&bso);
@ -402,26 +402,8 @@ asmb(void)
symo = rnd(symo, INITRND); symo = rnd(symo, INITRND);
break; break;
} }
seek(cout, symo+8, 0);
if(!debug['s'])
asmsym();
if(debug['v'])
Bprint(&bso, "%5.2f sp\n", cputime());
Bflush(&bso);
if(debug['v'])
Bprint(&bso, "%5.2f pc\n", cputime());
Bflush(&bso);
if(!debug['s'])
asmlc();
if(HEADTYPE == 10 || (iself && !debug['s']))
strnput("", INITRND-(8+symsize+lcsize)%INITRND);
cflush();
seek(cout, symo, 0);
lputl(symsize);
lputl(lcsize);
cflush();
if(HEADTYPE != 10 && !debug['s']) { if(HEADTYPE != 10 && !debug['s']) {
seek(cout, symo+8+symsize+lcsize, 0); seek(cout, symo, 0);
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f dwarf\n", cputime()); Bprint(&bso, "%5.2f dwarf\n", cputime());
dwarfemitdebugsections(); dwarfemitdebugsections();
@ -563,7 +545,7 @@ asmb(void)
break; break;
case 6: case 6:
asmbmacho(symdatva, symo); asmbmacho();
break; break;
Elfput: Elfput:
@ -618,15 +600,6 @@ asmb(void)
elfphload(&segrodata); elfphload(&segrodata);
elfphload(&segdata); elfphload(&segdata);
if(!debug['s'] && HEADTYPE != 8 && HEADTYPE != 11) {
segsym.rwx = 04;
segsym.vaddr = symdatva;
segsym.len = rnd(8+symsize+lcsize, INITRND);
segsym.fileoff = symo;
segsym.filelen = segsym.len;
elfphload(&segsym);
}
/* Dynamic linking sections */ /* Dynamic linking sections */
if (!debug['d']) { /* -d suppresses dynamic loader format */ if (!debug['d']) { /* -d suppresses dynamic loader format */
/* S headers for dynamic linking */ /* S headers for dynamic linking */
@ -714,38 +687,17 @@ asmb(void)
elfshbits(sect); elfshbits(sect);
if (!debug['s']) { if (!debug['s']) {
fo = symo;
w = 8;
sh = newElfShdr(elfstr[ElfStrGosymcounts]);
sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1;
sh->addr = symdatva;
fo += w;
w = symsize;
sh = newElfShdr(elfstr[ElfStrGosymtab]); sh = newElfShdr(elfstr[ElfStrGosymtab]);
sh->type = SHT_PROGBITS; sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC; sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1; sh->addralign = 1;
sh->addr = symdatva + 8; shsym(sh, lookup("symtab", 0));
fo += w;
w = lcsize;
sh = newElfShdr(elfstr[ElfStrGopclntab]); sh = newElfShdr(elfstr[ElfStrGopclntab]);
sh->type = SHT_PROGBITS; sh->type = SHT_PROGBITS;
sh->flags = SHF_ALLOC; sh->flags = SHF_ALLOC;
sh->off = fo;
sh->size = w;
sh->addralign = 1; sh->addralign = 1;
sh->addr = symdatva + 8 + symsize; shsym(sh, lookup("pclntab", 0));
dwarfaddelfheaders(); dwarfaddelfheaders();
} }
@ -845,3 +797,66 @@ rnd(int32 v, int32 r)
v -= c; v -= c;
return v; return v;
} }
void
genasmsym(void (*put)(Sym*, char*, int, vlong, vlong, int, Sym*))
{
Auto *a;
Sym *s;
int h;
s = lookup("etext", 0);
if(s->type == STEXT)
put(s, s->name, 'T', s->value, s->size, s->version, 0);
for(h=0; h<NHASH; h++) {
for(s=hash[h]; s!=S; s=s->hash) {
switch(s->type) {
case SCONST:
case SRODATA:
case SDATA:
case SELFDATA:
case SMACHO:
if(!s->reachable)
continue;
put(s, s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
continue;
case SBSS:
if(!s->reachable)
continue;
put(s, s->name, 'B', symaddr(s), s->size, s->version, s->gotype);
continue;
case SFILE:
put(nil, s->name, 'f', s->value, 0, s->version, 0);
continue;
}
}
}
for(s = textp; s != nil; s = s->next) {
/* filenames first */
for(a=s->autom; a; a=a->link)
if(a->type == D_FILE)
put(nil, a->asym->name, 'z', a->aoffset, 0, 0, 0);
else
if(a->type == D_FILE1)
put(nil, a->asym->name, 'Z', a->aoffset, 0, 0, 0);
put(s, s->name, 'T', s->value, s->size, s->version, s->gotype);
/* frame, auto and param after */
put(nil, ".frame", 'm', s->text->to.offset+4, 0, 0, 0);
for(a=s->autom; a; a=a->link)
if(a->type == D_AUTO)
put(nil, a->asym->name, 'a', -a->aoffset, 0, 0, a->gotype);
else
if(a->type == D_PARAM)
put(nil, a->asym->name, 'p', a->aoffset, 0, 0, a->gotype);
}
if(debug['v'] || debug['n'])
Bprint(&bso, "symsize = %ud\n", symsize);
Bflush(&bso);
}

View File

@ -126,6 +126,7 @@ struct Sym
uchar dupok; uchar dupok;
uchar reachable; uchar reachable;
uchar dynexport; uchar dynexport;
uchar special;
int32 value; int32 value;
int32 size; int32 size;
int32 sig; int32 sig;
@ -171,7 +172,6 @@ enum
SMACHO, // TODO(rsc): maybe move between DATA1 and BSS? SMACHO, // TODO(rsc): maybe move between DATA1 and BSS?
SFILE, SFILE,
SCONST, SCONST,
SFIXED,
NHASH = 10007, NHASH = 10007,
NHUNK = 100000, NHUNK = 100000,
@ -347,7 +347,6 @@ void doprof2(void);
void dostkoff(void); void dostkoff(void);
int32 entryvalue(void); int32 entryvalue(void);
void follow(void); void follow(void);
void genasmsym(void (*put)(char*, int, vlong, vlong, int, Sym*));
void instinit(void); void instinit(void);
void listinit(void); void listinit(void);
Sym* lookup(char*, int); Sym* lookup(char*, int);

View File

@ -337,7 +337,7 @@ Iconv(Fmt *fp)
// call fmtstrcpy to handle padding. // call fmtstrcpy to handle padding.
fmtstrinit(&fmt); fmtstrinit(&fmt);
for(i=0; i<n; i++) for(i=0; i<n; i++)
fmtprint(&fmt, "%.8ux", *p++); fmtprint(&fmt, "%.2ux", *p++);
s = fmtstrflush(&fmt); s = fmtstrflush(&fmt);
fmtstrcpy(fp, s); fmtstrcpy(fp, s);
free(s); free(s);

View File

@ -327,6 +327,8 @@ main(int argc, char *argv[])
else else
doprof2(); doprof2();
span(); span();
pclntab();
symtab();
dodata(); dodata();
address(); address();
reloc(); reloc();

View File

@ -193,6 +193,7 @@ xdefine(char *p, int t, int32 v)
s->type = t; s->type = t;
s->value = v; s->value = v;
s->reachable = 1; s->reachable = 1;
s->special = 1;
} }
void void
@ -503,9 +504,6 @@ int32
symaddr(Sym *s) symaddr(Sym *s)
{ {
switch(s->type) { switch(s->type) {
case SFIXED:
return s->value;
case SMACHO: case SMACHO:
return segdata.vaddr + segdata.filelen - dynptrsize + s->value; return segdata.vaddr + segdata.filelen - dynptrsize + s->value;
@ -535,25 +533,18 @@ vaddr(Adr *a, Reloc *r)
case D_EXTERN: case D_EXTERN:
s = a->sym; s = a->sym;
if(s != nil) { if(s != nil) {
switch(s->type) { if(!s->reachable)
case SFIXED: sysfatal("unreachable symbol in vaddr - %s", s->name);
v += s->value; if(r == nil) {
break; diag("need reloc for %D", a);
default: errorexit();
if(!s->reachable)
sysfatal("unreachable symbol in vaddr - %s", s->name);
if(r == nil) {
diag("need reloc for %D", a);
errorexit();
}
r->type = D_ADDR;
r->siz = 4;
r->off = -1;
r->sym = s;
r->add = v;
v = 0;
break;
} }
r->type = D_ADDR;
r->siz = 4;
r->off = -1;
r->sym = s;
r->add = v;
v = 0;
} }
} }
return v; return v;

View File

@ -1,167 +0,0 @@
// Inferno utils/8l/span.c
// http://code.google.com/p/inferno-os/source/browse/utils/8l/span.c
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Symbol table.
#include "l.h"
#include "../ld/lib.h"
void
putsymb(char *s, int t, vlong v, vlong size, int ver, Sym *go)
{
int i, f;
vlong gv;
if(t == 'f')
s++;
lput(v);
if(ver)
t += 'a' - 'A';
cput(t+0x80); /* 0x80 is variable length */
if(t == 'Z' || t == 'z') {
cput(s[0]);
for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) {
cput(s[i]);
cput(s[i+1]);
}
cput(0);
cput(0);
i++;
}
else {
for(i=0; s[i]; i++)
cput(s[i]);
cput(0);
}
gv = 0;
if(go) {
if(!go->reachable)
sysfatal("unreachable type %s", go->name);
gv = symaddr(go);
}
lput(gv);
symsize += 4 + 1 + i+1 + 4;
if(debug['n']) {
if(t == 'z' || t == 'Z') {
Bprint(&bso, "%c %.8ux ", t, v);
for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) {
f = ((s[i]&0xff) << 8) | (s[i+1]&0xff);
Bprint(&bso, "/%x", f);
}
Bprint(&bso, "\n");
return;
}
if(ver)
Bprint(&bso, "%c %.8ux %s<%d> %s (%.8llux)\n", t, v, s, ver, go ? go->name : "", gv);
else
Bprint(&bso, "%c %.8ux %s\n", t, v, s, go ? go->name : "", gv);
}
}
void
genasmsym(void (*put)(char*, int, vlong, vlong, int, Sym*))
{
Auto *a;
Sym *s;
int h;
s = lookup("etext", 0);
if(s->type == STEXT)
put(s->name, 'T', s->value, s->size, s->version, 0);
for(h=0; h<NHASH; h++) {
for(s=hash[h]; s!=S; s=s->hash) {
switch(s->type) {
case SCONST:
case SRODATA:
case SDATA:
case SELFDATA:
if(!s->reachable)
continue;
put(s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
continue;
case SMACHO:
if(!s->reachable)
continue;
put(s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
continue;
case SBSS:
if(!s->reachable)
continue;
put(s->name, 'B', symaddr(s), s->size, s->version, s->gotype);
continue;
case SFIXED:
put(s->name, 'B', s->value, s->size, s->version, s->gotype);
continue;
case SFILE:
put(s->name, 'f', s->value, 0, s->version, 0);
continue;
}
}
}
for(s = textp; s != nil; s = s->next) {
/* filenames first */
for(a=s->autom; a; a=a->link)
if(a->type == D_FILE)
put(a->asym->name, 'z', a->aoffset, 0, 0, 0);
else
if(a->type == D_FILE1)
put(a->asym->name, 'Z', a->aoffset, 0, 0, 0);
put(s->name, 'T', s->value, s->size, s->version, s->gotype);
/* frame, auto and param after */
put(".frame", 'm', s->text->to.offset+4, 0, 0, 0);
for(a=s->autom; a; a=a->link)
if(a->type == D_AUTO)
put(a->asym->name, 'a', -a->aoffset, 0, 0, a->gotype);
else
if(a->type == D_PARAM)
put(a->asym->name, 'p', a->aoffset, 0, 0, a->gotype);
}
if(debug['v'] || debug['n'])
Bprint(&bso, "symsize = %ud\n", symsize);
Bflush(&bso);
}
void
asmsym(void)
{
genasmsym(putsymb);
}

View File

@ -607,7 +607,7 @@ dodata(void)
datap = nil; datap = nil;
for(h=0; h<NHASH; h++) { for(h=0; h<NHASH; h++) {
for(s=hash[h]; s!=S; s=s->hash){ for(s=hash[h]; s!=S; s=s->hash){
if(!s->reachable) if(!s->reachable || s->special)
continue; continue;
if(STEXT < s->type && s->type < SXREF) { if(STEXT < s->type && s->type < SXREF) {
if(last == nil) if(last == nil)
@ -723,6 +723,8 @@ address(void)
for(s=segdata.sect; s != nil; s=s->next) { for(s=segdata.sect; s != nil; s=s->next) {
s->vaddr = va; s->vaddr = va;
va += s->len; va += s->len;
if(s == segdata.sect)
va += dynptrsize;
segdata.len = va - segdata.vaddr; segdata.len = va - segdata.vaddr;
} }
segdata.filelen = segdata.sect->len + dynptrsize; // assume .data is first segdata.filelen = segdata.sect->len + dynptrsize; // assume .data is first
@ -748,8 +750,8 @@ address(void)
xdefine("edata", SBSS, data->vaddr + data->len); xdefine("edata", SBSS, data->vaddr + data->len);
xdefine("end", SBSS, segdata.vaddr + segdata.len); xdefine("end", SBSS, segdata.vaddr + segdata.len);
if(debug['s']) sym = lookup("pclntab", 0);
xdefine("symdat", SFIXED, 0); xdefine("epclntab", SRODATA, sym->value + sym->size);
else sym = lookup("symtab", 0);
xdefine("symdat", SFIXED, SYMDATVA); xdefine("esymtab", SRODATA, sym->value + sym->size);
} }

View File

@ -889,11 +889,19 @@ ewrite(int fd, void *buf, int n)
} }
void void
asmlc(void) pclntab(void)
{ {
vlong oldpc; vlong oldpc;
Prog *p; Prog *p;
int32 oldlc, v, s; int32 oldlc, v, s;
Sym *sym;
uchar *bp;
sym = lookup("pclntab", 0);
sym->type = SRODATA;
sym->reachable = 1;
if(debug['s'])
return;
oldpc = INITTEXT; oldpc = INITTEXT;
oldlc = 0; oldlc = 0;
@ -912,7 +920,9 @@ asmlc(void)
s = 127; s = 127;
if(v < 127) if(v < 127)
s = v; s = v;
cput(s+128); /* 129-255 +pc */ symgrow(sym, lcsize+1);
bp = sym->p + lcsize;
*bp = s+128; /* 129-255 +pc */
if(debug['O']) if(debug['O'])
Bprint(&bso, " pc+%d*%d(%d)", s, MINLC, s+128); Bprint(&bso, " pc+%d*%d(%d)", s, MINLC, s+128);
v -= s; v -= s;
@ -922,11 +932,13 @@ asmlc(void)
oldlc = p->line; oldlc = p->line;
oldpc = p->pc + MINLC; oldpc = p->pc + MINLC;
if(s > 64 || s < -64) { if(s > 64 || s < -64) {
cput(0); /* 0 vv +lc */ symgrow(sym, lcsize+5);
cput(s>>24); bp = sym->p + lcsize;
cput(s>>16); *bp++ = 0; /* 0 vv +lc */
cput(s>>8); *bp++ = s>>24;
cput(s); *bp++ = s>>16;
*bp++ = s>>8;
*bp = s;
if(debug['O']) { if(debug['O']) {
if(s > 0) if(s > 0)
Bprint(&bso, " lc+%d(%d,%d)\n", Bprint(&bso, " lc+%d(%d,%d)\n",
@ -940,15 +952,17 @@ asmlc(void)
lcsize += 5; lcsize += 5;
continue; continue;
} }
symgrow(sym, lcsize+1);
bp = sym->p + lcsize;
if(s > 0) { if(s > 0) {
cput(0+s); /* 1-64 +lc */ *bp = 0+s; /* 1-64 +lc */
if(debug['O']) { if(debug['O']) {
Bprint(&bso, " lc+%d(%d)\n", s, 0+s); Bprint(&bso, " lc+%d(%d)\n", s, 0+s);
Bprint(&bso, "%6llux %P\n", Bprint(&bso, "%6llux %P\n",
p->pc, p); p->pc, p);
} }
} else { } else {
cput(64-s); /* 65-128 -lc */ *bp = 64-s; /* 65-128 -lc */
if(debug['O']) { if(debug['O']) {
Bprint(&bso, " lc%d(%d)\n", s, 64-s); Bprint(&bso, " lc%d(%d)\n", s, 64-s);
Bprint(&bso, "%6llux %P\n", Bprint(&bso, "%6llux %P\n",
@ -958,11 +972,14 @@ asmlc(void)
lcsize++; lcsize++;
} }
} }
while(lcsize & 1) { if(lcsize & 1) {
s = 129; symgrow(sym, lcsize+1);
cput(s); sym->p[lcsize] = 129;
lcsize++; lcsize++;
} }
sym->size = lcsize;
lcsize = 0;
if(debug['v'] || debug['O']) if(debug['v'] || debug['O'])
Bprint(&bso, "lcsize = %d\n", lcsize); Bprint(&bso, "lcsize = %d\n", lcsize);
Bflush(&bso); Bflush(&bso);

View File

@ -121,6 +121,8 @@ void errorexit(void);
void mangle(char*); void mangle(char*);
void objfile(char *file, char *pkg); void objfile(char *file, char *pkg);
void libinit(void); void libinit(void);
void pclntab(void);
void symtab(void);
void Lflag(char *arg); void Lflag(char *arg);
void usage(void); void usage(void);
void ldobj1(Biobuf *f, char*, int64 len, char *pn); void ldobj1(Biobuf *f, char*, int64 len, char *pn);
@ -145,9 +147,13 @@ vlong addaddr(Sym*, Sym*);
vlong addsize(Sym*, Sym*); vlong addsize(Sym*, Sym*);
vlong adduint8(Sym*, uint8); vlong adduint8(Sym*, uint8);
vlong adduint16(Sym*, uint16); vlong adduint16(Sym*, uint16);
void asmsym(void);
void asmelfsym64(void);
void strnput(char*, int); void strnput(char*, int);
void dodata(void); void dodata(void);
void address(void); void address(void);
void genasmsym(void (*put)(Sym*, char*, int, vlong, vlong, int, Sym*));
vlong datoff(vlong);
int pathchar(void); int pathchar(void);
void* mal(uint32); void* mal(uint32);

View File

@ -441,7 +441,7 @@ domacholink(void)
} }
void void
asmbmacho(vlong symdatva, vlong symo) asmbmacho(void)
{ {
vlong v, w; vlong v, w;
vlong va; vlong va;
@ -608,21 +608,17 @@ asmbmacho(vlong symdatva, vlong symo)
} }
if(!debug['s']) { if(!debug['s']) {
ms = newMachoSeg("__SYMDAT", 1); Sym *s;
ms->vaddr = symdatva;
ms->vsize = 8+symsize+lcsize;
ms->fileoffset = symo;
ms->filesize = 8+symsize+lcsize;
ms->prot1 = 7;
ms->prot2 = 5;
md = newMachoDebug(); md = newMachoDebug();
md->fileoffset = symo+8; s = lookup("symtab", 0);
md->filesize = symsize; md->fileoffset = datoff(s->value);
md->filesize = s->size;
md = newMachoDebug(); md = newMachoDebug();
md->fileoffset = symo+8+symsize; s = lookup("pclntab", 0);
md->filesize = lcsize; md->fileoffset = datoff(s->value);
md->filesize = s->size;
dwarfaddmachoheaders(); dwarfaddmachoheaders();
} }

View File

@ -74,4 +74,4 @@ enum {
void domacho(void); void domacho(void);
vlong domacholink(void); vlong domacholink(void);
void asmbmacho(vlong, vlong); void asmbmacho(void);

View File

@ -205,13 +205,6 @@ asmbpe(void)
break; break;
} }
if(!debug['s']) {
IMAGE_SECTION_HEADER *symsect;
symsect = new_section(".symdat", 8+symsize+lcsize, 0);
symsect->Characteristics = IMAGE_SCN_MEM_READ|
IMAGE_SCN_CNT_INITIALIZED_DATA;
}
add_import_table(); add_import_table();
fh.NumberOfSections = nsect; fh.NumberOfSections = nsect;

View File

@ -37,8 +37,7 @@
char *elfstrdat; char *elfstrdat;
int elfstrsize; int elfstrsize;
int maxelfstr; int maxelfstr;
int elftextsh;
void genasmsym(void (*put)(char*, int, vlong, vlong, int, Sym*));
int int
putelfstr(char *s) putelfstr(char *s)
@ -85,151 +84,137 @@ putelfsymb(char *s, int t, vlong addr, vlong size, int ver, Sym *go)
} }
stroff = putelfstr(s); stroff = putelfstr(s);
lputl(stroff); // string LPUT(stroff); // string
cput((bind<<4)|(type&0xF)); cput((bind<<4)|(type&0xF));
cput(0); cput(0);
wputl(shndx); WPUT(shndx);
vputl(addr); VPUT(addr);
vputl(size); VPUT(size);
} }
void void
asmelfsym(void) asmelfsym64(void)
{ {
genasmsym(putelfsymb); // genasmsym(putelfsymb64);
} }
static Sym *symt;
static void
scput(int b)
{
uchar *p;
symgrow(symt, symt->size+1);
p = symt->p + symt->size;
*p = b;
symt->size++;
}
static void
slputb(int32 v)
{
uchar *p;
symgrow(symt, symt->size+4);
p = symt->p + symt->size;
*p++ = v>>24;
*p++ = v>>16;
*p++ = v>>8;
*p = v;
symt->size += 4;
}
void void
putsymb(char *s, int t, vlong v, vlong size, int ver, Sym *go) putsymb(Sym *s, char *name, int t, vlong v, vlong size, int ver, Sym *typ)
{ {
int i, f, l; int i, f, l;
vlong gv; Reloc *rel;
if(t == 'f') if(t == 'f')
s++; name++;
l = 4; l = 4;
if(!debug['8']){ // if(!debug['8'])
lputb(v>>32); // l = 8;
l = 8; if(s != nil) {
} rel = addrel(symt);
lputb(v); rel->siz = l + Rbig;
rel->sym = s;
rel->type = D_ADDR;
rel->off = symt->size;
v = 0;
}
if(l == 8)
slputb(v>>32);
slputb(v);
if(ver) if(ver)
t += 'a' - 'A'; t += 'a' - 'A';
cput(t+0x80); /* 0x80 is variable length */ scput(t+0x80); /* 0x80 is variable length */
if(t == 'Z' || t == 'z') { if(t == 'Z' || t == 'z') {
cput(s[0]); scput(name[0]);
for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) { for(i=1; name[i] != 0 || name[i+1] != 0; i += 2) {
cput(s[i]); scput(name[i]);
cput(s[i+1]); scput(name[i+1]);
} }
cput(0); scput(0);
cput(0); scput(0);
i++; i++;
} }
else { else {
for(i=0; s[i]; i++) for(i=0; name[i]; i++)
cput(s[i]); scput(name[i]);
cput(0); scput(0);
} }
gv = 0; if(typ) {
if(go) { if(!typ->reachable)
if(!go->reachable) diag("unreachable type %s", typ->name);
diag("unreachable type %s", go->name); rel = addrel(symt);
gv = go->value+INITDAT; rel->siz = l;
rel->sym = typ;
rel->type = D_ADDR;
rel->off = symt->size;
} }
if(l == 8) if(l == 8)
lputb(gv>>32); slputb(0);
lputb(gv); slputb(0);
symsize += l + 1 + i+1 + l;
if(debug['n']) { if(debug['n']) {
if(t == 'z' || t == 'Z') { if(t == 'z' || t == 'Z') {
Bprint(&bso, "%c %.8llux ", t, v); Bprint(&bso, "%c %.8llux ", t, v);
for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) { for(i=1; name[i] != 0 || name[i+1] != 0; i+=2) {
f = ((s[i]&0xff) << 8) | (s[i+1]&0xff); f = ((name[i]&0xff) << 8) | (name[i+1]&0xff);
Bprint(&bso, "/%x", f); Bprint(&bso, "/%x", f);
} }
Bprint(&bso, "\n"); Bprint(&bso, "\n");
return; return;
} }
if(ver) if(ver)
Bprint(&bso, "%c %.8llux %s<%d> %s (%.8llux)\n", t, v, s, ver, go ? go->name : "", gv); Bprint(&bso, "%c %.8llux %s<%d> %s\n", t, v, s, ver, typ ? typ->name : "");
else else
Bprint(&bso, "%c %.8llux %s %s (%.8llux)\n", t, v, s, go ? go->name : "", gv); Bprint(&bso, "%c %.8llux %s %s\n", t, v, s, typ ? typ->name : "");
} }
} }
void void
genasmsym(void (*put)(char*, int, vlong, vlong, int, Sym*)) symtab(void)
{ {
Auto *a; // Define these so that they'll get put into the symbol table.
Sym *s; // data.c:/^address will provide the actual values.
int h; xdefine("text", STEXT, 0);
xdefine("etext", STEXT, 0);
xdefine("rodata", SRODATA, 0);
xdefine("erodata", SRODATA, 0);
xdefine("data", SBSS, 0);
xdefine("edata", SBSS, 0);
xdefine("end", SBSS, 0);
xdefine("epclntab", SRODATA, 0);
xdefine("esymtab", SRODATA, 0);
s = lookup("etext", 0); symt = lookup("symtab", 0);
if(s->type == STEXT) symt->type = SRODATA;
put(s->name, 'T', s->value, s->size, s->version, 0); symt->size = 0;
symt->reachable = 1;
for(h=0; h<NHASH; h++) {
for(s=hash[h]; s!=S; s=s->hash) {
switch(s->type) {
case SCONST:
case SRODATA:
case SDATA:
case SELFDATA:
if(!s->reachable)
continue;
put(s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
continue;
case SBSS:
if(!s->reachable)
continue;
put(s->name, 'B', symaddr(s), s->size, s->version, s->gotype);
continue;
case SFIXED:
put(s->name, 'B', s->value, s->size, s->version, s->gotype);
continue;
case SFILE:
put(s->name, 'f', s->value, 0, s->version, 0);
continue;
}
}
}
for(s = textp; s != nil; s = s->next) {
/* filenames first */
for(a=s->autom; a; a=a->link)
if(a->type == D_FILE)
put(a->asym->name, 'z', a->aoffset, 0, 0, 0);
else
if(a->type == D_FILE1)
put(a->asym->name, 'Z', a->aoffset, 0, 0, 0);
put(s->name, 'T', s->value, s->size, s->version, s->gotype);
/* frame, auto and param after */
put(".frame", 'm', s->text->to.offset+8, 0, 0, 0);
for(a=s->autom; a; a=a->link)
if(a->type == D_AUTO)
put(a->asym->name, 'a', -a->aoffset, 0, 0, a->gotype);
else
if(a->type == D_PARAM)
put(a->asym->name, 'p', a->aoffset, 0, 0, a->gotype);
}
if(debug['v'] || debug['n'])
Bprint(&bso, "symsize = %ud\n", symsize);
Bflush(&bso);
}
void
asmsym(void)
{
genasmsym(putsymb); genasmsym(putsymb);
} }

View File

@ -66,7 +66,7 @@ static int adotout(int, Fhdr*, ExecHdr*);
static int elfdotout(int, Fhdr*, ExecHdr*); static int elfdotout(int, Fhdr*, ExecHdr*);
static int machdotout(int, Fhdr*, ExecHdr*); static int machdotout(int, Fhdr*, ExecHdr*);
static int armdotout(int, Fhdr*, ExecHdr*); static int armdotout(int, Fhdr*, ExecHdr*);
static void setsym(Fhdr*, int32, int32, int32, vlong); static void setsym(Fhdr*, vlong, int32, vlong, int32, vlong, int32);
static void setdata(Fhdr*, uvlong, int32, vlong, int32); static void setdata(Fhdr*, uvlong, int32, vlong, int32);
static void settext(Fhdr*, uvlong, uvlong, int32, vlong); static void settext(Fhdr*, uvlong, uvlong, int32, vlong);
static void hswal(void*, int, uint32(*)(uint32)); static void hswal(void*, int, uint32(*)(uint32));
@ -427,7 +427,7 @@ adotout(int fd, Fhdr *fp, ExecHdr *hp)
hp->e.exechdr.text, sizeof(Exec)); hp->e.exechdr.text, sizeof(Exec));
setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize), setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
hp->e.exechdr.data, fp->txtsz+sizeof(Exec), hp->e.exechdr.bss); hp->e.exechdr.data, fp->txtsz+sizeof(Exec), hp->e.exechdr.bss);
setsym(fp, hp->e.exechdr.syms, hp->e.exechdr.spsz, hp->e.exechdr.pcsz, fp->datoff+fp->datsz); setsym(fp, fp->datoff+fp->datsz, hp->e.exechdr.syms, 0, hp->e.exechdr.spsz, 0, hp->e.exechdr.pcsz);
return 1; return 1;
} }
@ -525,7 +525,7 @@ commonllp64(int unused, Fhdr *fp, ExecHdr *hp)
settext(fp, entry, pgsize+fp->hdrsz, hp->e.exechdr.text, fp->hdrsz); settext(fp, entry, pgsize+fp->hdrsz, hp->e.exechdr.text, fp->hdrsz);
setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize), setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
hp->e.exechdr.data, fp->txtsz+fp->hdrsz, hp->e.exechdr.bss); hp->e.exechdr.data, fp->txtsz+fp->hdrsz, hp->e.exechdr.bss);
setsym(fp, hp->e.exechdr.syms, hp->e.exechdr.spsz, hp->e.exechdr.pcsz, fp->datoff+fp->datsz); setsym(fp, fp->datoff+fp->datsz, hp->e.exechdr.syms, 0, hp->e.exechdr.spsz, 0, hp->e.exechdr.pcsz);
if(hp->e.exechdr.magic & DYN_MAGIC) { if(hp->e.exechdr.magic & DYN_MAGIC) {
fp->txtaddr = 0; fp->txtaddr = 0;
@ -758,13 +758,12 @@ elf64dotout(int fd, Fhdr *fp, ExecHdr *hp)
bsssz = ph[0].memsz - ph[0].filesz; bsssz = ph[0].memsz - ph[0].filesz;
settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset); settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz); setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset); setsym(fp, ph[1].offset, ph[1].filesz, 0, 0, 0, ph[1].memsz);
free(ph); free(ph);
return 1; return 1;
} }
werrstr("No TEXT or DATA sections"); werrstr("No TEXT or DATA sections");
error:
free(ph); free(ph);
free(sh); free(sh);
return 0; return 0;
@ -773,12 +772,13 @@ error:
settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset); settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz); setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
if(is != -1) if(is != -1)
setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset); setsym(fp, ph[is].offset, ph[is].filesz, 0, 0, 0, ph[is].memsz);
else if(sh != 0){ else if(sh != 0){
char *buf; char *buf;
uvlong symsize = 0; uvlong symsize = 0;
uvlong symoff = 0; uvlong symoff = 0;
uvlong pclnsz = 0; uvlong pclnsz = 0;
uvlong pclnoff = 0;
/* load shstrtab names */ /* load shstrtab names */
buf = malloc(sh[ep->shstrndx].size); buf = malloc(sh[ep->shstrndx].size);
@ -795,15 +795,11 @@ error:
symoff = sh[i].offset; symoff = sh[i].offset;
} }
if (strcmp(&buf[sh[i].name], ".gopclntab") == 0) { if (strcmp(&buf[sh[i].name], ".gopclntab") == 0) {
if (sh[i].offset != symoff+symsize) {
werrstr("pc line table not contiguous with symbol table");
free(buf);
goto error;
}
pclnsz = sh[i].size; pclnsz = sh[i].size;
pclnoff = sh[i].offset;
} }
} }
setsym(fp, symsize, 0, pclnsz, symoff); setsym(fp, symoff, symsize, 0, 0, pclnoff, pclnsz);
free(buf); free(buf);
} }
done: done:
@ -940,13 +936,12 @@ elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
bsssz = ph[0].memsz - ph[0].filesz; bsssz = ph[0].memsz - ph[0].filesz;
settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset); settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz); setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset); setsym(fp, ph[1].offset, ph[1].filesz, 0, 0, 0, ph[1].memsz);
free(ph); free(ph);
return 1; return 1;
} }
werrstr("No TEXT or DATA sections"); werrstr("No TEXT or DATA sections");
error:
free(sh); free(sh);
free(ph); free(ph);
return 0; return 0;
@ -955,12 +950,13 @@ error:
settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset); settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz); setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
if(is != -1) if(is != -1)
setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset); setsym(fp, ph[is].offset, ph[is].filesz, 0, 0, 0, ph[is].memsz);
else if(sh != 0){ else if(sh != 0){
char *buf; char *buf;
uvlong symsize = 0; uvlong symsize = 0;
uvlong symoff = 0; uvlong symoff = 0;
uvlong pclnsz = 0; uvlong pclnsize = 0;
uvlong pclnoff = 0;
/* load shstrtab names */ /* load shstrtab names */
buf = malloc(sh[ep->shstrndx].size); buf = malloc(sh[ep->shstrndx].size);
@ -977,15 +973,11 @@ error:
symoff = sh[i].offset; symoff = sh[i].offset;
} }
if (strcmp(&buf[sh[i].name], ".gopclntab") == 0) { if (strcmp(&buf[sh[i].name], ".gopclntab") == 0) {
if (sh[i].offset != symoff+symsize) { pclnsize = sh[i].size;
werrstr("pc line table not contiguous with symbol table"); pclnoff = sh[i].offset;
free(buf);
goto error;
}
pclnsz = sh[i].size;
} }
} }
setsym(fp, symsize, 0, pclnsz, symoff); setsym(fp, symoff, symsize, 0, 0, pclnoff, pclnsize);
free(buf); free(buf);
} }
done: done:
@ -1209,7 +1201,7 @@ machdotout(int fd, Fhdr *fp, ExecHdr *hp)
settext(fp, textva+sizeof(Machhdr) + mp->sizeofcmds, textva, textsize, textoff); settext(fp, textva+sizeof(Machhdr) + mp->sizeofcmds, textva, textsize, textoff);
setdata(fp, datava, datasize, dataoff, bsssize); setdata(fp, datava, datasize, dataoff, bsssize);
if(symtab != 0) if(symtab != 0)
setsym(fp, symtab->filesize, 0, pclntab? pclntab->filesize : 0, symtab->fileoff); setsym(fp, symtab->fileoff, symtab->filesize, 0, 0, 0, pclntab? pclntab->filesize : 0);
free(cmd); free(cmd);
free(cmdbuf); free(cmdbuf);
return 1; return 1;
@ -1230,7 +1222,7 @@ armdotout(int fd, Fhdr *fp, ExecHdr *hp)
USED(fd); USED(fd);
settext(fp, hp->e.exechdr.entry, sizeof(Exec), hp->e.exechdr.text, sizeof(Exec)); settext(fp, hp->e.exechdr.entry, sizeof(Exec), hp->e.exechdr.text, sizeof(Exec));
setdata(fp, fp->txtsz, hp->e.exechdr.data, fp->txtsz, hp->e.exechdr.bss); setdata(fp, fp->txtsz, hp->e.exechdr.data, fp->txtsz, hp->e.exechdr.bss);
setsym(fp, hp->e.exechdr.syms, hp->e.exechdr.spsz, hp->e.exechdr.pcsz, fp->datoff+fp->datsz); setsym(fp, fp->datoff+fp->datsz, hp->e.exechdr.syms, 0, hp->e.exechdr.spsz, 0, hp->e.exechdr.pcsz);
kbase = 0xF0000000; kbase = 0xF0000000;
if ((fp->entry & kbase) == kbase) { /* Boot image */ if ((fp->entry & kbase) == kbase) { /* Boot image */
@ -1261,14 +1253,20 @@ setdata(Fhdr *fp, uvlong a, int32 s, vlong off, int32 bss)
} }
static void static void
setsym(Fhdr *fp, int32 symsz, int32 sppcsz, int32 lnpcsz, vlong symoff) setsym(Fhdr *fp, vlong symoff, int32 symsz, vlong sppcoff, int32 sppcsz, vlong lnpcoff, int32 lnpcsz)
{ {
fp->symsz = symsz;
fp->symoff = symoff; fp->symoff = symoff;
fp->symsz = symsz;
if(sppcoff == 0)
sppcoff = symoff+symsz;
fp->sppcoff = symoff;
fp->sppcsz = sppcsz; fp->sppcsz = sppcsz;
fp->sppcoff = fp->symoff+fp->symsz;
if(lnpcoff == 0)
lnpcoff = sppcoff + sppcsz;
fp->lnpcoff = lnpcoff;
fp->lnpcsz = lnpcsz; fp->lnpcsz = lnpcsz;
fp->lnpcoff = fp->sppcoff+fp->sppcsz;
} }

View File

@ -17,7 +17,7 @@
#include "os.h" #include "os.h"
#include "arch.h" #include "arch.h"
extern int32 symdat[]; extern byte pclntab[], epclntab[], symtab[], esymtab[];
typedef struct Sym Sym; typedef struct Sym Sym;
struct Sym struct Sym
@ -32,25 +32,16 @@ struct Sym
static void static void
walksymtab(void (*fn)(Sym*)) walksymtab(void (*fn)(Sym*))
{ {
int32 *v;
byte *p, *ep, *q; byte *p, *ep, *q;
Sym s; Sym s;
if(symdat == nil) p = symtab;
return; ep = esymtab;
#ifdef __WINDOWS__
v = get_symdat_addr();
p = (byte*)v+8;
#else
v = symdat;
p = (byte*)(symdat+2);
#endif
ep = p + v[0];
while(p < ep) { while(p < ep) {
if(p + 7 > ep) if(p + 7 > ep)
break; break;
s.value = ((uint32)p[0]<<24) | ((uint32)p[1]<<16) | ((uint32)p[2]<<8) | ((uint32)p[3]); s.value = ((uint32)p[0]<<24) | ((uint32)p[1]<<16) | ((uint32)p[2]<<8) | ((uint32)p[3]);
if(!(p[4]&0x80)) if(!(p[4]&0x80))
break; break;
s.symtype = p[4] & ~0x80; s.symtype = p[4] & ~0x80;
@ -127,8 +118,13 @@ dofunc(Sym *sym)
break; break;
case 'f': case 'f':
if(fname == nil) { if(fname == nil) {
if(sym->value >= nfname) if(sym->value >= nfname) {
if(sym->value >= 0x10000) {
printf("invalid symbol file index %p\n", sym->value);
throw("mangled symbol table");
}
nfname = sym->value+1; nfname = sym->value+1;
}
break; break;
} }
fname[sym->value] = sym->name; fname[sym->value] = sym->name;
@ -240,9 +236,11 @@ splitpcln(void)
uintptr pc; uintptr pc;
byte *p, *ep; byte *p, *ep;
Func *f, *ef; Func *f, *ef;
int32 *v;
int32 pcquant; int32 pcquant;
if(pclntab == epclntab || nfunc == 0)
return;
switch(thechar) { switch(thechar) {
case '5': case '5':
pcquant = 4; pcquant = 4;
@ -252,19 +250,9 @@ splitpcln(void)
break; break;
} }
if(symdat == nil)
return;
// pc/ln table bounds // pc/ln table bounds
#ifdef __WINDOWS__ p = pclntab;
v = get_symdat_addr(); ep = epclntab;
p = (byte*)v+8;
#else
v = symdat;
p = (byte*)(symdat+2);
#endif
p += v[0];
ep = p+v[1];
f = func; f = func;
ef = func + nfunc; ef = func + nfunc;

View File

@ -22,7 +22,6 @@ void *SetLastError;
static void *CreateEvent; static void *CreateEvent;
static void *CreateThread; static void *CreateThread;
static void *GetModuleHandle;
static void *WaitForSingleObject; static void *WaitForSingleObject;
static void* static void*
@ -60,7 +59,6 @@ osinit(void)
CreateEvent = get_proc_addr("kernel32.dll", "CreateEventA"); CreateEvent = get_proc_addr("kernel32.dll", "CreateEventA");
CreateThread = get_proc_addr("kernel32.dll", "CreateThread"); CreateThread = get_proc_addr("kernel32.dll", "CreateThread");
ExitProcess = get_proc_addr("kernel32.dll", "ExitProcess"); ExitProcess = get_proc_addr("kernel32.dll", "ExitProcess");
GetModuleHandle = get_proc_addr("kernel32.dll", "GetModuleHandleA");
GetStdHandle = get_proc_addr("kernel32.dll", "GetStdHandle"); GetStdHandle = get_proc_addr("kernel32.dll", "GetStdHandle");
SetEvent = get_proc_addr("kernel32.dll", "SetEvent"); SetEvent = get_proc_addr("kernel32.dll", "SetEvent");
VirtualAlloc = get_proc_addr("kernel32.dll", "VirtualAlloc"); VirtualAlloc = get_proc_addr("kernel32.dll", "VirtualAlloc");
@ -155,24 +153,6 @@ write(int32 fd, void *buf, int32 n)
return written; return written;
} }
void*
get_symdat_addr(void)
{
byte *mod, *p;
uint32 peh, add;
uint16 oph;
mod = stdcall(GetModuleHandle, 1, 0);
peh = *(uint32*)(mod+0x3c);
p = mod+peh+4;
oph = *(uint16*)(p+0x10);
p += 0x14+oph;
while(strcmp(p, (byte*)".symdat"))
p += 40;
add = *(uint32*)(p+0x0c);
return mod+add;
}
// Thread-safe allocation of an event. // Thread-safe allocation of an event.
static void static void
initevent(void **pevent) initevent(void **pevent)