2009-01-06 10:41:38 -07:00
|
|
|
// Inferno utils/8l/obj.c
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/8l/obj.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.
|
|
|
|
|
|
|
|
#define EXTERN
|
|
|
|
#include "l.h"
|
2009-10-07 01:11:59 -06:00
|
|
|
#include "../ld/lib.h"
|
2009-09-24 13:53:35 -06:00
|
|
|
#include "../ld/elf.h"
|
2009-09-30 18:33:39 -06:00
|
|
|
#include "../ld/macho.h"
|
2009-12-13 13:39:20 -07:00
|
|
|
#include "../ld/pe.h"
|
2009-01-06 10:41:38 -07:00
|
|
|
#include <ar.h>
|
|
|
|
|
|
|
|
#ifndef DEFAULT
|
|
|
|
#define DEFAULT '9'
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *noname = "<none>";
|
|
|
|
char thechar = '8';
|
|
|
|
char *thestring = "386";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* -H0 -T0x40004C -D0x10000000 is garbage unix
|
|
|
|
* -H1 -T0xd0 -R4 is unix coff
|
|
|
|
* -H2 -T4128 -R4096 is plan9 format
|
|
|
|
* -H3 -Tx -Rx is MS-DOS .COM
|
|
|
|
* -H4 -Tx -Rx is fake MS-DOS .EXE
|
2009-11-18 00:58:51 -07:00
|
|
|
* -H6 -Tx -Rx is Apple Mach-O
|
|
|
|
* -H7 -Tx -Rx is Linux ELF32
|
|
|
|
* -H8 -Tx -Rx is Google Native Client
|
|
|
|
* -H9 -Tx -Rx is FreeBSD ELF32
|
2009-01-06 10:41:38 -07:00
|
|
|
*/
|
|
|
|
|
2009-06-02 23:33:21 -06:00
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
2010-02-26 15:03:52 -07:00
|
|
|
fprint(2, "usage: 8l [-options] [-E entry] [-H head] [-L dir] [-T text] [-R rnd] [-r path] [-o out] main.8\n");
|
2009-06-02 23:33:21 -06:00
|
|
|
exits("usage");
|
|
|
|
}
|
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
void
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i, c;
|
|
|
|
|
|
|
|
Binit(&bso, 1, OWRITE);
|
|
|
|
cout = -1;
|
|
|
|
listinit();
|
|
|
|
memset(debug, 0, sizeof(debug));
|
|
|
|
nerrors = 0;
|
2009-12-13 13:39:20 -07:00
|
|
|
outfile = nil;
|
2009-01-06 10:41:38 -07:00
|
|
|
HEADTYPE = -1;
|
|
|
|
INITTEXT = -1;
|
|
|
|
INITDAT = -1;
|
|
|
|
INITRND = -1;
|
|
|
|
INITENTRY = 0;
|
2009-10-06 16:08:32 -06:00
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
c = ARGC();
|
2010-02-03 23:31:38 -07:00
|
|
|
if(c == 'l')
|
|
|
|
usage();
|
|
|
|
if(c >= 0 && c < sizeof(debug))
|
2009-01-06 10:41:38 -07:00
|
|
|
debug[c]++;
|
|
|
|
break;
|
|
|
|
case 'o': /* output to (next arg) */
|
2009-06-02 23:33:21 -06:00
|
|
|
outfile = EARGF(usage());
|
2009-01-06 10:41:38 -07:00
|
|
|
break;
|
|
|
|
case 'E':
|
2009-06-02 23:33:21 -06:00
|
|
|
INITENTRY = EARGF(usage());
|
2009-01-06 10:41:38 -07:00
|
|
|
break;
|
|
|
|
case 'H':
|
2009-06-02 23:33:21 -06:00
|
|
|
HEADTYPE = atolwhex(EARGF(usage()));
|
|
|
|
break;
|
|
|
|
case 'L':
|
2009-10-07 01:11:59 -06:00
|
|
|
Lflag(EARGF(usage()));
|
2009-01-06 10:41:38 -07:00
|
|
|
break;
|
|
|
|
case 'T':
|
2009-06-02 23:33:21 -06:00
|
|
|
INITTEXT = atolwhex(EARGF(usage()));
|
2009-01-06 10:41:38 -07:00
|
|
|
break;
|
|
|
|
case 'D':
|
2009-06-02 23:33:21 -06:00
|
|
|
INITDAT = atolwhex(EARGF(usage()));
|
2009-01-06 10:41:38 -07:00
|
|
|
break;
|
|
|
|
case 'R':
|
2009-06-02 23:33:21 -06:00
|
|
|
INITRND = atolwhex(EARGF(usage()));
|
|
|
|
break;
|
2010-02-26 15:03:52 -07:00
|
|
|
case 'r':
|
|
|
|
rpath = EARGF(usage());
|
2009-01-06 10:41:38 -07:00
|
|
|
break;
|
2010-02-08 10:46:53 -07:00
|
|
|
case 'V':
|
|
|
|
print("%cl version %s\n", thechar, getgoversion());
|
|
|
|
errorexit();
|
2009-01-06 10:41:38 -07:00
|
|
|
} ARGEND
|
2010-01-25 18:53:43 -07:00
|
|
|
|
|
|
|
if(argc != 1)
|
2009-06-02 23:33:21 -06:00
|
|
|
usage();
|
2009-03-20 15:22:59 -06:00
|
|
|
|
2009-12-13 13:39:20 -07:00
|
|
|
mywhatsys(); // get goos
|
2009-10-06 16:08:32 -06:00
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
if(HEADTYPE == -1) {
|
2009-03-20 15:22:59 -06:00
|
|
|
HEADTYPE = 2;
|
|
|
|
if(strcmp(goos, "linux") == 0)
|
|
|
|
HEADTYPE = 7;
|
|
|
|
else
|
|
|
|
if(strcmp(goos, "darwin") == 0)
|
|
|
|
HEADTYPE = 6;
|
2009-09-18 19:57:15 -06:00
|
|
|
else
|
|
|
|
if(strcmp(goos, "nacl") == 0)
|
|
|
|
HEADTYPE = 8;
|
2009-03-20 15:22:59 -06:00
|
|
|
else
|
2009-11-18 17:51:59 -07:00
|
|
|
if(strcmp(goos, "freebsd") == 0)
|
2009-11-18 00:58:51 -07:00
|
|
|
HEADTYPE = 9;
|
2009-11-18 17:51:59 -07:00
|
|
|
else
|
2010-04-30 00:45:14 -06:00
|
|
|
if(strcmp(goos, "windows") == 0)
|
2009-12-13 13:39:20 -07:00
|
|
|
HEADTYPE = 10;
|
2010-01-13 20:51:59 -07:00
|
|
|
else
|
2010-06-07 15:18:42 -06:00
|
|
|
if(strcmp(goos, "tiny") == 0)
|
2010-01-13 20:51:59 -07:00
|
|
|
HEADTYPE = 11;
|
2009-12-13 13:39:20 -07:00
|
|
|
else
|
|
|
|
print("goos is not known: %s\n", goos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(outfile == nil) {
|
|
|
|
if(HEADTYPE == 10)
|
|
|
|
outfile = "8.out.exe";
|
|
|
|
else
|
|
|
|
outfile = "8.out";
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
2009-03-20 15:22:59 -06:00
|
|
|
|
2009-12-13 13:39:20 -07:00
|
|
|
libinit();
|
2010-07-01 00:31:27 -06:00
|
|
|
if(rpath == nil)
|
|
|
|
rpath = smprint("%s/pkg/%s_%s", goroot, goos, goarch);
|
2009-12-13 13:39:20 -07:00
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
switch(HEADTYPE) {
|
|
|
|
default:
|
|
|
|
diag("unknown -H option");
|
|
|
|
errorexit();
|
|
|
|
|
|
|
|
case 0: /* this is garbage */
|
|
|
|
HEADR = 20L+56L;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0x40004CL;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0x10000000L;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 0;
|
|
|
|
break;
|
|
|
|
case 1: /* is unix coff */
|
|
|
|
HEADR = 0xd0L;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0xd0;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0x400000;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 0;
|
|
|
|
break;
|
|
|
|
case 2: /* plan 9 */
|
|
|
|
HEADR = 32L;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 4096+32;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 4096;
|
|
|
|
break;
|
|
|
|
case 3: /* MS-DOS .COM */
|
|
|
|
HEADR = 0;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0x0100;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 4;
|
|
|
|
break;
|
|
|
|
case 4: /* fake MS-DOS .EXE */
|
|
|
|
HEADR = 0x200;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0x0100;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 4;
|
|
|
|
HEADR += (INITTEXT & 0xFFFF);
|
|
|
|
if(debug['v'])
|
|
|
|
Bprint(&bso, "HEADR = 0x%ld\n", HEADR);
|
|
|
|
break;
|
2009-03-20 15:22:59 -06:00
|
|
|
case 6: /* apple MACH */
|
2009-10-03 11:37:12 -06:00
|
|
|
/*
|
|
|
|
* OS X system constant - offset from %gs to our TLS.
|
|
|
|
* Explained in ../../libcgo/darwin_386.c.
|
|
|
|
*/
|
|
|
|
tlsoffset = 0x468;
|
2009-09-30 18:33:39 -06:00
|
|
|
machoinit();
|
|
|
|
HEADR = MACHORESERVE;
|
2009-03-20 15:22:59 -06:00
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 4096+HEADR;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 4096;
|
|
|
|
break;
|
|
|
|
case 7: /* elf32 executable */
|
2009-11-18 00:58:51 -07:00
|
|
|
case 9:
|
2009-10-03 11:37:12 -06:00
|
|
|
/*
|
|
|
|
* Linux ELF uses TLS offsets negative from %gs.
|
|
|
|
* Translate 0(GS) and 4(GS) into -8(GS) and -4(GS).
|
|
|
|
* Also known to ../../pkg/runtime/linux/386/sys.s
|
|
|
|
* and ../../libcgo/linux_386.c.
|
|
|
|
*/
|
2009-11-18 17:51:59 -07:00
|
|
|
tlsoffset = -8;
|
2009-09-24 13:53:35 -06:00
|
|
|
elfinit();
|
|
|
|
HEADR = ELFRESERVE;
|
2009-03-20 15:22:59 -06:00
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0x08048000+HEADR;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 4096;
|
|
|
|
break;
|
2009-09-18 19:57:15 -06:00
|
|
|
case 8: /* native client elf32 executable */
|
2009-09-24 13:53:35 -06:00
|
|
|
elfinit();
|
2009-09-18 19:57:15 -06:00
|
|
|
HEADR = 4096;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0x20000;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
2010-04-22 18:52:22 -06:00
|
|
|
INITRND = 65536;
|
|
|
|
|
|
|
|
// 512 kB of address space for closures.
|
|
|
|
// (Doesn't take any space in the binary file.)
|
|
|
|
// Closures are 64 bytes each, so this is 8,192 closures.
|
|
|
|
textpad = 512*1024;
|
2009-09-18 19:57:15 -06:00
|
|
|
break;
|
2009-12-13 13:39:20 -07:00
|
|
|
case 10: /* PE executable */
|
|
|
|
peinit();
|
|
|
|
HEADR = PERESERVE;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = PEBASE+0x1000;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = PEALIGN;
|
|
|
|
break;
|
2010-01-13 20:51:59 -07:00
|
|
|
case 11:
|
|
|
|
tlsoffset = 0;
|
|
|
|
elfinit();
|
|
|
|
HEADR = ELFRESERVE;
|
|
|
|
if(INITTEXT == -1)
|
|
|
|
INITTEXT = 0x100000+HEADR;
|
|
|
|
if(INITDAT == -1)
|
|
|
|
INITDAT = 0;
|
|
|
|
if(INITRND == -1)
|
|
|
|
INITRND = 4096;
|
|
|
|
break;
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
|
|
|
if(INITDAT != 0 && INITRND != 0)
|
|
|
|
print("warning: -D0x%lux is ignored because of -R0x%lux\n",
|
|
|
|
INITDAT, INITRND);
|
|
|
|
if(debug['v'])
|
|
|
|
Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n",
|
|
|
|
HEADTYPE, INITTEXT, INITDAT, INITRND);
|
|
|
|
Bflush(&bso);
|
|
|
|
for(i=1; optab[i].as; i++)
|
|
|
|
if(i != optab[i].as) {
|
|
|
|
diag("phase error in optab: %d", i);
|
|
|
|
errorexit();
|
|
|
|
}
|
|
|
|
maxop = i;
|
|
|
|
|
|
|
|
for(i=0; i<Ymax; i++)
|
|
|
|
ycover[i*Ymax + i] = 1;
|
|
|
|
|
|
|
|
ycover[Yi0*Ymax + Yi8] = 1;
|
|
|
|
ycover[Yi1*Ymax + Yi8] = 1;
|
|
|
|
|
|
|
|
ycover[Yi0*Ymax + Yi32] = 1;
|
|
|
|
ycover[Yi1*Ymax + Yi32] = 1;
|
|
|
|
ycover[Yi8*Ymax + Yi32] = 1;
|
|
|
|
|
|
|
|
ycover[Yal*Ymax + Yrb] = 1;
|
|
|
|
ycover[Ycl*Ymax + Yrb] = 1;
|
|
|
|
ycover[Yax*Ymax + Yrb] = 1;
|
|
|
|
ycover[Ycx*Ymax + Yrb] = 1;
|
|
|
|
ycover[Yrx*Ymax + Yrb] = 1;
|
|
|
|
|
|
|
|
ycover[Yax*Ymax + Yrx] = 1;
|
|
|
|
ycover[Ycx*Ymax + Yrx] = 1;
|
|
|
|
|
|
|
|
ycover[Yax*Ymax + Yrl] = 1;
|
|
|
|
ycover[Ycx*Ymax + Yrl] = 1;
|
|
|
|
ycover[Yrx*Ymax + Yrl] = 1;
|
|
|
|
|
|
|
|
ycover[Yf0*Ymax + Yrf] = 1;
|
|
|
|
|
|
|
|
ycover[Yal*Ymax + Ymb] = 1;
|
|
|
|
ycover[Ycl*Ymax + Ymb] = 1;
|
|
|
|
ycover[Yax*Ymax + Ymb] = 1;
|
|
|
|
ycover[Ycx*Ymax + Ymb] = 1;
|
|
|
|
ycover[Yrx*Ymax + Ymb] = 1;
|
|
|
|
ycover[Yrb*Ymax + Ymb] = 1;
|
|
|
|
ycover[Ym*Ymax + Ymb] = 1;
|
|
|
|
|
|
|
|
ycover[Yax*Ymax + Yml] = 1;
|
|
|
|
ycover[Ycx*Ymax + Yml] = 1;
|
|
|
|
ycover[Yrx*Ymax + Yml] = 1;
|
|
|
|
ycover[Yrl*Ymax + Yml] = 1;
|
|
|
|
ycover[Ym*Ymax + Yml] = 1;
|
|
|
|
|
|
|
|
for(i=0; i<D_NONE; i++) {
|
|
|
|
reg[i] = -1;
|
|
|
|
if(i >= D_AL && i <= D_BH)
|
|
|
|
reg[i] = (i-D_AL) & 7;
|
|
|
|
if(i >= D_AX && i <= D_DI)
|
|
|
|
reg[i] = (i-D_AX) & 7;
|
|
|
|
if(i >= D_F0 && i <= D_F0+7)
|
|
|
|
reg[i] = (i-D_F0) & 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
zprg.link = P;
|
|
|
|
zprg.pcond = P;
|
|
|
|
zprg.back = 2;
|
|
|
|
zprg.as = AGOK;
|
|
|
|
zprg.from.type = D_NONE;
|
|
|
|
zprg.from.index = D_NONE;
|
|
|
|
zprg.from.scale = 1;
|
|
|
|
zprg.to = zprg.from;
|
|
|
|
|
|
|
|
pcstr = "%.6lux ";
|
|
|
|
nuxiinit();
|
|
|
|
histgen = 0;
|
|
|
|
textp = P;
|
|
|
|
datap = P;
|
|
|
|
edatap = P;
|
|
|
|
pc = 0;
|
|
|
|
dtype = 4;
|
|
|
|
version = 0;
|
|
|
|
cbp = buf.cbuf;
|
|
|
|
cbc = sizeof(buf.cbuf);
|
|
|
|
firstp = prg();
|
|
|
|
lastp = firstp;
|
|
|
|
|
2010-02-03 17:30:45 -07:00
|
|
|
addlibpath("command line", "command line", argv[0], "main");
|
2010-02-03 23:31:38 -07:00
|
|
|
loadlib();
|
2009-10-07 01:11:59 -06:00
|
|
|
|
2009-03-31 01:20:07 -06:00
|
|
|
deadcode();
|
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
firstp = firstp->link;
|
|
|
|
if(firstp == P)
|
|
|
|
errorexit();
|
|
|
|
if(doexp || dlm){
|
|
|
|
EXPTAB = "_exporttab";
|
|
|
|
zerosig(EXPTAB);
|
|
|
|
zerosig("etext");
|
|
|
|
zerosig("edata");
|
|
|
|
zerosig("end");
|
|
|
|
if(dlm){
|
|
|
|
import();
|
|
|
|
HEADTYPE = 2;
|
|
|
|
INITTEXT = INITDAT = 0;
|
|
|
|
INITRND = 8;
|
|
|
|
INITENTRY = EXPTAB;
|
|
|
|
}
|
|
|
|
export();
|
|
|
|
}
|
|
|
|
patch();
|
|
|
|
follow();
|
2009-09-24 13:53:35 -06:00
|
|
|
doelf();
|
2009-10-03 11:37:12 -06:00
|
|
|
if(HEADTYPE == 6)
|
|
|
|
domacho();
|
2009-01-06 10:41:38 -07:00
|
|
|
dodata();
|
|
|
|
dostkoff();
|
|
|
|
if(debug['p'])
|
|
|
|
if(debug['1'])
|
|
|
|
doprof1();
|
|
|
|
else
|
|
|
|
doprof2();
|
|
|
|
span();
|
|
|
|
doinit();
|
2009-12-13 13:39:20 -07:00
|
|
|
if(HEADTYPE == 10)
|
|
|
|
dope();
|
2009-01-06 10:41:38 -07:00
|
|
|
asmb();
|
|
|
|
undef();
|
|
|
|
if(debug['v']) {
|
|
|
|
Bprint(&bso, "%5.2f cpu time\n", cputime());
|
|
|
|
Bprint(&bso, "%ld symbols\n", nsymbol);
|
|
|
|
Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
|
|
|
|
Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
|
|
|
|
}
|
|
|
|
Bflush(&bso);
|
|
|
|
|
|
|
|
errorexit();
|
|
|
|
}
|
|
|
|
|
2010-07-21 14:44:49 -06:00
|
|
|
Sym*
|
|
|
|
zsym(char *pn, Biobuf *f, Sym *h[])
|
|
|
|
{
|
|
|
|
int o;
|
|
|
|
|
|
|
|
o = Bgetc(f);
|
|
|
|
if(o < 0 || o >= NSYM || h[o] == nil)
|
|
|
|
mangle(pn);
|
|
|
|
return h[o];
|
|
|
|
}
|
|
|
|
|
2009-03-20 17:40:00 -06:00
|
|
|
void
|
2010-07-16 17:16:17 -06:00
|
|
|
zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
|
2009-03-20 17:40:00 -06:00
|
|
|
{
|
2010-07-16 17:16:17 -06:00
|
|
|
int o, t;
|
2009-01-06 12:34:02 -07:00
|
|
|
int32 l;
|
2009-01-06 10:41:38 -07:00
|
|
|
Sym *s;
|
|
|
|
Auto *u;
|
|
|
|
|
2009-03-20 17:40:00 -06:00
|
|
|
t = Bgetc(f);
|
2009-11-05 13:47:22 -07:00
|
|
|
a->index = D_NONE;
|
|
|
|
a->scale = 0;
|
2009-01-06 10:41:38 -07:00
|
|
|
if(t & T_INDEX) {
|
2009-03-20 17:40:00 -06:00
|
|
|
a->index = Bgetc(f);
|
|
|
|
a->scale = Bgetc(f);
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
2009-03-20 17:40:00 -06:00
|
|
|
a->type = D_NONE;
|
2009-01-06 10:41:38 -07:00
|
|
|
a->offset = 0;
|
2009-03-20 17:40:00 -06:00
|
|
|
if(t & T_OFFSET)
|
|
|
|
a->offset = Bget4(f);
|
|
|
|
a->offset2 = 0;
|
|
|
|
if(t & T_OFFSET2) {
|
|
|
|
a->offset2 = Bget4(f);
|
|
|
|
a->type = D_CONST2;
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
|
|
|
a->sym = S;
|
2010-07-21 14:44:49 -06:00
|
|
|
if(t & T_SYM)
|
|
|
|
a->sym = zsym(pn, f, h);
|
2009-01-06 10:41:38 -07:00
|
|
|
if(t & T_FCONST) {
|
2009-03-20 17:40:00 -06:00
|
|
|
a->ieee.l = Bget4(f);
|
|
|
|
a->ieee.h = Bget4(f);
|
2009-01-06 10:41:38 -07:00
|
|
|
a->type = D_FCONST;
|
|
|
|
} else
|
|
|
|
if(t & T_SCONST) {
|
2009-03-20 17:40:00 -06:00
|
|
|
Bread(f, a->scon, NSNAME);
|
2009-01-06 10:41:38 -07:00
|
|
|
a->type = D_SCONST;
|
|
|
|
}
|
2009-03-20 17:40:00 -06:00
|
|
|
if(t & T_TYPE)
|
|
|
|
a->type = Bgetc(f);
|
2009-11-05 13:47:22 -07:00
|
|
|
adrgotype = S;
|
2009-08-13 16:45:58 -06:00
|
|
|
if(t & T_GOTYPE)
|
2010-07-21 14:44:49 -06:00
|
|
|
adrgotype = zsym(pn, f, h);
|
2009-10-03 11:37:12 -06:00
|
|
|
|
|
|
|
t = a->type;
|
|
|
|
if(t == D_INDIR+D_GS)
|
|
|
|
a->offset += tlsoffset;
|
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
s = a->sym;
|
|
|
|
if(s == S)
|
2009-03-20 17:40:00 -06:00
|
|
|
return;
|
2009-08-13 16:45:58 -06:00
|
|
|
if(t != D_AUTO && t != D_PARAM) {
|
2009-11-05 13:47:22 -07:00
|
|
|
if(adrgotype)
|
|
|
|
s->gotype = adrgotype;
|
2009-03-20 17:40:00 -06:00
|
|
|
return;
|
2009-08-13 16:45:58 -06:00
|
|
|
}
|
2009-01-06 10:41:38 -07:00
|
|
|
l = a->offset;
|
|
|
|
for(u=curauto; u; u=u->link) {
|
|
|
|
if(u->asym == s)
|
|
|
|
if(u->type == t) {
|
|
|
|
if(u->aoffset > l)
|
|
|
|
u->aoffset = l;
|
2009-11-05 13:47:22 -07:00
|
|
|
if(adrgotype)
|
|
|
|
u->gotype = adrgotype;
|
2009-03-20 17:40:00 -06:00
|
|
|
return;
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-20 17:40:00 -06:00
|
|
|
u = mal(sizeof(*u));
|
2009-01-06 10:41:38 -07:00
|
|
|
u->link = curauto;
|
|
|
|
curauto = u;
|
|
|
|
u->asym = s;
|
|
|
|
u->aoffset = l;
|
|
|
|
u->type = t;
|
2009-11-05 13:47:22 -07:00
|
|
|
u->gotype = adrgotype;
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nopout(Prog *p)
|
|
|
|
{
|
|
|
|
p->as = ANOP;
|
|
|
|
p->from.type = D_NONE;
|
|
|
|
p->to.type = D_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-22 18:06:20 -07:00
|
|
|
ldobj1(Biobuf *f, char *pkg, int64 len, char *pn)
|
2009-01-06 10:41:38 -07:00
|
|
|
{
|
2009-01-06 12:34:02 -07:00
|
|
|
int32 ipc;
|
2009-01-06 10:41:38 -07:00
|
|
|
Prog *p, *t;
|
|
|
|
int v, o, r, skip;
|
|
|
|
Sym *h[NSYM], *s, *di;
|
2009-01-06 12:34:02 -07:00
|
|
|
uint32 sig;
|
2009-10-07 01:11:59 -06:00
|
|
|
int ntext;
|
2009-03-20 17:40:00 -06:00
|
|
|
int32 eof;
|
2010-01-22 18:06:20 -07:00
|
|
|
char *name, *x;
|
2009-03-20 17:40:00 -06:00
|
|
|
char src[1024];
|
|
|
|
|
|
|
|
ntext = 0;
|
2009-10-07 01:11:59 -06:00
|
|
|
eof = Boffset(f) + len;
|
2009-01-06 10:41:38 -07:00
|
|
|
di = S;
|
2009-10-07 01:11:59 -06:00
|
|
|
src[0] = 0;
|
2009-01-06 10:41:38 -07:00
|
|
|
|
2009-03-20 17:40:00 -06:00
|
|
|
|
2009-01-06 10:41:38 -07:00
|
|
|
newloop:
|
|
|
|
memset(h, 0, sizeof(h));
|
|
|
|
version++;
|
|
|
|
histfrogp = 0;
|
|
|
|
ipc = pc;
|
|
|
|
skip = 0;
|
|
|
|
|
|
|
|
loop:
|
2009-03-20 17:40:00 -06:00
|
|
|
if(f->state == Bracteof || Boffset(f) >= eof)
|
2009-01-06 10:41:38 -07:00
|
|
|
goto eof;
|
2009-03-20 17:40:00 -06:00
|
|
|
o = Bgetc(f);
|
|
|
|
if(o == Beof)
|
|
|
|
goto eof;
|
|
|
|
o |= Bgetc(f) << 8;
|
2009-01-06 10:41:38 -07:00
|
|
|
if(o <= AXXX || o >= ALAST) {
|
|
|
|
if(o < 0)
|
|
|
|
goto eof;
|
2009-03-20 17:40:00 -06:00
|
|
|
diag("%s:#%lld: opcode out of range: %#ux", pn, Boffset(f), o);
|
|
|
|
print(" probably not a .%c file\n", thechar);
|
2009-01-06 10:41:38 -07:00
|
|
|
errorexit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(o == ANAME || o == ASIGNAME) {
|
|
|
|
sig = 0;
|
2009-03-20 17:40:00 -06:00
|
|
|
if(o == ASIGNAME)
|
|
|
|
sig = Bget4(f);
|
|
|
|
v = Bgetc(f); /* type */
|
|
|
|
o = Bgetc(f); /* sym */
|
|
|
|
r = 0;
|
|
|
|
if(v == D_STATIC)
|
|
|
|
r = version;
|
|
|
|
name = Brdline(f, '\0');
|
|
|
|
if(name == nil) {
|
|
|
|
if(Blinelen(f) > 0) {
|
2009-01-06 10:41:38 -07:00
|
|
|
fprint(2, "%s: name too long\n", pn);
|
|
|
|
errorexit();
|
|
|
|
}
|
2009-03-20 17:40:00 -06:00
|
|
|
goto eof;
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
2010-01-22 18:06:20 -07:00
|
|
|
x = expandpkg(name, pkg);
|
|
|
|
s = lookup(x, r);
|
|
|
|
if(x != name)
|
|
|
|
free(x);
|
|
|
|
name = nil;
|
2009-01-06 10:41:38 -07:00
|
|
|
|
|
|
|
if(debug['S'] && r == 0)
|
|
|
|
sig = 1729;
|
|
|
|
if(sig != 0){
|
|
|
|
if(s->sig != 0 && s->sig != sig)
|
2009-03-20 17:40:00 -06:00
|
|
|
diag("incompatible type signatures"
|
|
|
|
"%lux(%s) and %lux(%s) for %s",
|
2009-10-07 01:11:59 -06:00
|
|
|
s->sig, s->file, sig, pn, s->name);
|
2009-01-06 10:41:38 -07:00
|
|
|
s->sig = sig;
|
2009-10-07 01:11:59 -06:00
|
|
|
s->file = pn;
|
2009-01-06 10:41:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if(debug['W'])
|
|
|
|
print(" ANAME %s\n", s->name);
|
2010-07-21 14:44:49 -06:00
|
|
|
if(o < 0 || o >= nelem(h))
|
|
|
|
mangle(pn);
|
2009-01-06 10:41:38 -07:00
|
|
|
h[o] = s;
|
|
|
|
if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
|
|
|
|
s->type = SXREF;
|
|
|
|
if(v == D_FILE) {
|
|
|
|
if(s->type != SFILE) {
|
|
|
|
histgen++;
|
|
|
|
s->type = SFILE;
|
|
|
|
s->value = histgen;
|
|
|
|
}
|
|
|
|
if(histfrogp < MAXHIST) {
|
|
|
|
histfrog[histfrogp] = s;
|
|
|
|
histfrogp++;
|
|
|
|
} else
|
|
|
|
collapsefrog(s);
|
|
|
|
}
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
2009-01-06 12:34:02 -07:00
|
|
|
p = mal(sizeof(*p));
|
2009-01-06 10:41:38 -07:00
|
|
|
p->as = o;
|
2009-03-20 17:40:00 -06:00
|
|
|
p->line = Bget4(f);
|
2009-01-06 10:41:38 -07:00
|
|
|
p->back = 2;
|
2009-11-05 13:47:22 -07:00
|
|
|
p->ft = 0;
|
|
|
|
p->tt = 0;
|
2010-07-16 17:16:17 -06:00
|
|
|
zaddr(pn, f, &p->from, h);
|
2009-11-05 13:47:22 -07:00
|
|
|
fromgotype = adrgotype;
|
2010-07-16 17:16:17 -06:00
|
|
|
zaddr(pn, f, &p->to, h);
|
2009-01-06 10:41:38 -07:00
|
|
|
|
|
|
|
if(debug['W'])
|
|
|
|
print("%P\n", p);
|
|
|
|
|
|
|
|
switch(p->as) {
|
|
|
|
case AHISTORY:
|
|
|
|
if(p->to.offset == -1) {
|
2009-03-20 17:40:00 -06:00
|
|
|
addlib(src, pn);
|
2009-01-06 10:41:38 -07:00
|
|
|
histfrogp = 0;
|
|
|
|
goto loop;
|
|
|
|
}
|
2009-03-20 17:40:00 -06:00
|
|
|
if(src[0] == '\0')
|
|
|
|
copyhistfrog(src, sizeof src);
|
2009-01-06 10:41:38 -07:00
|
|
|
addhist(p->line, D_FILE); /* 'z' */
|
|
|
|
if(p->to.offset)
|
|
|
|
addhist(p->to.offset, D_FILE1); /* 'Z' */
|
|
|
|
histfrogp = 0;
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case AEND:
|
|
|
|
histtoauto();
|
|
|
|
if(curtext != P)
|
|
|
|
curtext->to.autom = curauto;
|
|
|
|
curauto = 0;
|
|
|
|
curtext = P;
|
2009-03-20 17:40:00 -06:00
|
|
|
if(Boffset(f) == eof)
|
|
|
|
return;
|
|
|
|
goto newloop;
|
2009-01-06 10:41:38 -07:00
|
|
|
|
|
|
|
case AGLOBL:
|
|
|
|
s = p->from.sym;
|
|
|
|
if(s->type == 0 || s->type == SXREF) {
|
|
|
|
s->type = SBSS;
|
|
|
|
s->value = 0;
|
|
|
|
}
|
|
|
|
if(s->type != SBSS) {
|
|
|
|
diag("%s: redefinition: %s in %s",
|
|
|
|
pn, s->name, TNAME);
|
|
|
|
s->type = SBSS;
|
|
|
|
s->value = 0;
|
|
|
|
}
|
|
|
|
if(p->to.offset > s->value)
|
|
|
|
s->value = p->to.offset;
|
2009-06-02 23:33:21 -06:00
|
|
|
if(p->from.scale & DUPOK)
|
|
|
|
s->dupok = 1;
|
2009-01-06 10:41:38 -07:00
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case ADYNT:
|
|
|
|
if(p->to.sym == S) {
|
|
|
|
diag("DYNT without a sym\n%P", p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
di = p->to.sym;
|
|
|
|
p->from.scale = 4;
|
|
|
|
if(di->type == SXREF) {
|
|
|
|
if(debug['z'])
|
|
|
|
Bprint(&bso, "%P set to %d\n", p, dtype);
|
|
|
|
di->type = SCONST;
|
|
|
|
di->value = dtype;
|
|
|
|
dtype += 4;
|
|
|
|
}
|
|
|
|
if(p->from.sym == S)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p->from.offset = di->value;
|
|
|
|
p->from.sym->type = SDATA;
|
|
|
|
if(curtext == P) {
|
|
|
|
diag("DYNT not in text: %P", p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->to.sym = curtext->from.sym;
|
|
|
|
p->to.type = D_ADDR;
|
|
|
|
p->to.index = D_EXTERN;
|
|
|
|
goto data;
|
|
|
|
|
|
|
|
case AINIT:
|
|
|
|
if(p->from.sym == S) {
|
|
|
|
diag("INIT without a sym\n%P", p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(di == S) {
|
|
|
|
diag("INIT without previous DYNT\n%P", p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->from.offset = di->value;
|
|
|
|
p->from.sym->type = SDATA;
|
|
|
|
goto data;
|
|
|
|
|
|
|
|
case ADATA:
|
|
|
|
data:
|
2009-03-31 01:20:07 -06:00
|
|
|
// Assume that AGLOBL comes after ADATA.
|
|
|
|
// If we've seen an AGLOBL that said this sym was DUPOK,
|
|
|
|
// ignore any more ADATA we see, which must be
|
|
|
|
// redefinitions.
|
|
|
|
s = p->from.sym;
|
|
|
|
if(s != S && s->dupok) {
|
2009-11-05 13:47:22 -07:00
|
|
|
// if(debug['v'])
|
|
|
|
// Bprint(&bso, "skipping %s in %s: dupok\n", s->name, pn);
|
2009-03-31 01:20:07 -06:00
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
if(s != S) {
|
|
|
|
p->dlink = s->data;
|
|
|
|
s->data = p;
|
2010-01-25 09:53:27 -07:00
|
|
|
if(s->file == nil)
|
|
|
|
s->file = pn;
|
|
|
|
else if(s->file != pn) {
|
|
|
|
diag("multiple initialization for %s: in both %s and %s", s->name, s->file, pn);
|
|
|
|
errorexit();
|
|
|
|
}
|
2009-03-31 01:20:07 -06:00
|
|
|
}
|
2009-01-06 10:41:38 -07:00
|
|
|
if(edatap == P)
|
|
|
|
datap = p;
|
|
|
|
else
|
|
|
|
edatap->link = p;
|
|
|
|
edatap = p;
|
|
|
|
p->link = P;
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case AGOK:
|
|
|
|
diag("%s: GOK opcode in %s", pn, TNAME);
|
|
|
|
pc++;
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case ATEXT:
|
2009-03-20 17:40:00 -06:00
|
|
|
s = p->from.sym;
|
|
|
|
if(s == S) {
|
|
|
|
diag("%s: no TEXT symbol: %P", pn, p);
|
|
|
|
errorexit();
|
|
|
|
}
|
|
|
|
if(ntext++ == 0 && s->type != 0 && s->type != SXREF) {
|
|
|
|
/* redefinition, so file has probably been seen before */
|
|
|
|
if(debug['v'])
|
|
|
|
diag("skipping: %s: redefinition: %s", pn, s->name);
|
|
|
|
return;
|
|
|
|
}
|
2009-01-06 10:41:38 -07:00
|
|
|
if(curtext != P) {
|
|
|
|
histtoauto();
|
|
|
|
curtext->to.autom = curauto;
|
|
|
|
curauto = 0;
|
|
|
|
}
|
|
|
|
skip = 0;
|
|
|
|
curtext = p;
|
|
|
|
if(s->type != 0 && s->type != SXREF) {
|
|
|
|
if(p->from.scale & DUPOK) {
|
|
|
|
skip = 1;
|
|
|
|
goto casdef;
|
|
|
|
}
|
|
|
|
diag("%s: redefinition: %s\n%P", pn, s->name, p);
|
|
|
|
}
|
2009-03-31 01:20:07 -06:00
|
|
|
newtext(p, s);
|
2009-01-06 10:41:38 -07:00
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case AFMOVF:
|
|
|
|
case AFADDF:
|
|
|
|
case AFSUBF:
|
|
|
|
case AFSUBRF:
|
|
|
|
case AFMULF:
|
|
|
|
case AFDIVF:
|
|
|
|
case AFDIVRF:
|
|
|
|
case AFCOMF:
|
|
|
|
case AFCOMFP:
|
|
|
|
if(skip)
|
|
|
|
goto casdef;
|
|
|
|
if(p->from.type == D_FCONST) {
|
|
|
|
/* size sb 9 max */
|
|
|
|
sprint(literal, "$%lux", ieeedtof(&p->from.ieee));
|
|
|
|
s = lookup(literal, 0);
|
|
|
|
if(s->type == 0) {
|
|
|
|
s->type = SBSS;
|
|
|
|
s->value = 4;
|
|
|
|
t = prg();
|
|
|
|
t->as = ADATA;
|
|
|
|
t->line = p->line;
|
|
|
|
t->from.type = D_EXTERN;
|
|
|
|
t->from.sym = s;
|
|
|
|
t->from.scale = 4;
|
|
|
|
t->to = p->from;
|
|
|
|
if(edatap == P)
|
|
|
|
datap = t;
|
|
|
|
else
|
|
|
|
edatap->link = t;
|
|
|
|
edatap = t;
|
|
|
|
t->link = P;
|
|
|
|
}
|
|
|
|
p->from.type = D_EXTERN;
|
|
|
|
p->from.sym = s;
|
|
|
|
p->from.offset = 0;
|
|
|
|
}
|
|
|
|
goto casdef;
|
|
|
|
|
|
|
|
case AFMOVD:
|
|
|
|
case AFADDD:
|
|
|
|
case AFSUBD:
|
|
|
|
case AFSUBRD:
|
|
|
|
case AFMULD:
|
|
|
|
case AFDIVD:
|
|
|
|
case AFDIVRD:
|
|
|
|
case AFCOMD:
|
|
|
|
case AFCOMDP:
|
|
|
|
if(skip)
|
|
|
|
goto casdef;
|
|
|
|
if(p->from.type == D_FCONST) {
|
|
|
|
/* size sb 18 max */
|
|
|
|
sprint(literal, "$%lux.%lux",
|
|
|
|
p->from.ieee.l, p->from.ieee.h);
|
|
|
|
s = lookup(literal, 0);
|
|
|
|
if(s->type == 0) {
|
|
|
|
s->type = SBSS;
|
|
|
|
s->value = 8;
|
|
|
|
t = prg();
|
|
|
|
t->as = ADATA;
|
|
|
|
t->line = p->line;
|
|
|
|
t->from.type = D_EXTERN;
|
|
|
|
t->from.sym = s;
|
|
|
|
t->from.scale = 8;
|
|
|
|
t->to = p->from;
|
|
|
|
if(edatap == P)
|
|
|
|
datap = t;
|
|
|
|
else
|
|
|
|
edatap->link = t;
|
|
|
|
edatap = t;
|
|
|
|
t->link = P;
|
|
|
|
}
|
|
|
|
p->from.type = D_EXTERN;
|
|
|
|
p->from.sym = s;
|
|
|
|
p->from.offset = 0;
|
|
|
|
}
|
|
|
|
goto casdef;
|
|
|
|
|
|
|
|
casdef:
|
|
|
|
default:
|
|
|
|
if(skip)
|
|
|
|
nopout(p);
|
|
|
|
|
|
|
|
if(p->to.type == D_BRANCH)
|
|
|
|
p->to.offset += ipc;
|
|
|
|
lastp->link = p;
|
|
|
|
lastp = p;
|
|
|
|
p->pc = pc;
|
|
|
|
pc++;
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
eof:
|
|
|
|
diag("truncated object file: %s", pn);
|
|
|
|
}
|
|
|
|
|
|
|
|
Prog*
|
|
|
|
prg(void)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
2009-01-06 12:34:02 -07:00
|
|
|
p = mal(sizeof(Prog));
|
2009-01-06 10:41:38 -07:00
|
|
|
*p = zprg;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
Prog*
|
|
|
|
copyp(Prog *q)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
|
|
|
p = prg();
|
|
|
|
*p = *q;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
Prog*
|
|
|
|
appendp(Prog *q)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
|
|
|
|
p = prg();
|
|
|
|
p->link = q->link;
|
|
|
|
q->link = p;
|
|
|
|
p->line = q->line;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
doprof1(void)
|
|
|
|
{
|
|
|
|
Sym *s;
|
2009-01-06 12:34:02 -07:00
|
|
|
int32 n;
|
2009-01-06 10:41:38 -07:00
|
|
|
Prog *p, *q;
|
|
|
|
|
|
|
|
if(debug['v'])
|
|
|
|
Bprint(&bso, "%5.2f profile 1\n", cputime());
|
|
|
|
Bflush(&bso);
|
|
|
|
s = lookup("__mcount", 0);
|
|
|
|
n = 1;
|
|
|
|
for(p = firstp->link; p != P; p = p->link) {
|
|
|
|
if(p->as == ATEXT) {
|
|
|
|
q = prg();
|
|
|
|
q->line = p->line;
|
|
|
|
q->link = datap;
|
|
|
|
datap = q;
|
|
|
|
q->as = ADATA;
|
|
|
|
q->from.type = D_EXTERN;
|
|
|
|
q->from.offset = n*4;
|
|
|
|
q->from.sym = s;
|
|
|
|
q->from.scale = 4;
|
|
|
|
q->to = p->from;
|
|
|
|
q->to.type = D_CONST;
|
|
|
|
|
|
|
|
q = prg();
|
|
|
|
q->line = p->line;
|
|
|
|
q->pc = p->pc;
|
|
|
|
q->link = p->link;
|
|
|
|
p->link = q;
|
|
|
|
p = q;
|
|
|
|
p->as = AADDL;
|
|
|
|
p->from.type = D_CONST;
|
|
|
|
p->from.offset = 1;
|
|
|
|
p->to.type = D_EXTERN;
|
|
|
|
p->to.sym = s;
|
|
|
|
p->to.offset = n*4 + 4;
|
|
|
|
|
|
|
|
n += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
q = prg();
|
|
|
|
q->line = 0;
|
|
|
|
q->link = datap;
|
|
|
|
datap = q;
|
|
|
|
|
|
|
|
q->as = ADATA;
|
|
|
|
q->from.type = D_EXTERN;
|
|
|
|
q->from.sym = s;
|
|
|
|
q->from.scale = 4;
|
|
|
|
q->to.type = D_CONST;
|
|
|
|
q->to.offset = n;
|
|
|
|
|
|
|
|
s->type = SBSS;
|
|
|
|
s->value = n*4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
doprof2(void)
|
|
|
|
{
|
|
|
|
Sym *s2, *s4;
|
|
|
|
Prog *p, *q, *ps2, *ps4;
|
|
|
|
|
|
|
|
if(debug['v'])
|
|
|
|
Bprint(&bso, "%5.2f profile 2\n", cputime());
|
|
|
|
Bflush(&bso);
|
|
|
|
|
|
|
|
s2 = lookup("_profin", 0);
|
|
|
|
s4 = lookup("_profout", 0);
|
|
|
|
if(s2->type != STEXT || s4->type != STEXT) {
|
|
|
|
diag("_profin/_profout not defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ps2 = P;
|
|
|
|
ps4 = P;
|
|
|
|
for(p = firstp; p != P; p = p->link) {
|
|
|
|
if(p->as == ATEXT) {
|
|
|
|
if(p->from.sym == s2) {
|
|
|
|
p->from.scale = 1;
|
|
|
|
ps2 = p;
|
|
|
|
}
|
|
|
|
if(p->from.sym == s4) {
|
|
|
|
p->from.scale = 1;
|
|
|
|
ps4 = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(p = firstp; p != P; p = p->link) {
|
|
|
|
if(p->as == ATEXT) {
|
|
|
|
curtext = p;
|
|
|
|
|
|
|
|
if(p->from.scale & NOPROF) { /* dont profile */
|
|
|
|
for(;;) {
|
|
|
|
q = p->link;
|
|
|
|
if(q == P)
|
|
|
|
break;
|
|
|
|
if(q->as == ATEXT)
|
|
|
|
break;
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JMPL profin
|
|
|
|
*/
|
|
|
|
q = prg();
|
|
|
|
q->line = p->line;
|
|
|
|
q->pc = p->pc;
|
|
|
|
q->link = p->link;
|
|
|
|
p->link = q;
|
|
|
|
p = q;
|
|
|
|
p->as = ACALL;
|
|
|
|
p->to.type = D_BRANCH;
|
|
|
|
p->pcond = ps2;
|
|
|
|
p->to.sym = s2;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(p->as == ARET) {
|
|
|
|
/*
|
|
|
|
* RET
|
|
|
|
*/
|
|
|
|
q = prg();
|
|
|
|
q->as = ARET;
|
|
|
|
q->from = p->from;
|
|
|
|
q->to = p->to;
|
|
|
|
q->link = p->link;
|
|
|
|
p->link = q;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JAL profout
|
|
|
|
*/
|
|
|
|
p->as = ACALL;
|
|
|
|
p->from = zprg.from;
|
|
|
|
p->to = zprg.to;
|
|
|
|
p->to.type = D_BRANCH;
|
|
|
|
p->pcond = ps4;
|
|
|
|
p->to.sym = s4;
|
|
|
|
|
|
|
|
p = q;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|