static int init_udp(struct qtsession* session) { char* envval; fprintf(stderr, "Initializing UDP socket...\n"); struct addrinfo *ai_local = NULL, *ai_remote = NULL; unsigned short af = 0; int ret; if (envval = getconf("LOCAL_ADDRESS")) { if (ret = getaddrinfo(envval, NULL, NULL, &ai_local)) return errorexit2("getaddrinfo(LOCAL_ADDRESS)", gai_strerror(ret)); if (!ai_local) return errorexit("LOCAL_ADDRESS lookup failed"); if (ai_local->ai_addrlen > sizeof(sockaddr_any)) return errorexit("Resolved LOCAL_ADDRESS is too big"); af = ai_local->ai_family; } if (envval = getconf("REMOTE_ADDRESS")) { if (ret = getaddrinfo(envval, NULL, NULL, &ai_remote)) return errorexit2("getaddrinfo(REMOTE_ADDRESS)", gai_strerror(ret)); if (!ai_remote) return errorexit("REMOTE_ADDRESS lookup failed"); if (ai_remote->ai_addrlen > sizeof(sockaddr_any)) return errorexit("Resolved REMOTE_ADDRESS is too big"); if (af && af != ai_remote->ai_family) return errorexit("Address families do not match"); af = ai_remote->ai_family; } if (!af) af = AF_INET; int sfd = socket(af, SOCK_DGRAM, IPPROTO_UDP); if (sfd < 0) return errorexitp("Could not create UDP socket"); sockaddr_any udpaddr; memset(&udpaddr, 0, sizeof(udpaddr)); udpaddr.any.sa_family = af; if (ai_local) memcpy(&udpaddr, ai_local->ai_addr, ai_local->ai_addrlen); int port = 2998; if (envval = getconf("LOCAL_PORT")) port = atoi(envval); if (sockaddr_set_port(&udpaddr, port)) return -1; if (bind(sfd, (struct sockaddr*)&udpaddr, sizeof(udpaddr))) return errorexitp("Could not bind socket"); memset(&udpaddr, 0, sizeof(udpaddr)); udpaddr.any.sa_family = af; if (ai_remote) memcpy(&udpaddr, ai_remote->ai_addr, ai_remote->ai_addrlen); if (!ai_remote || sockaddr_is_zero_address(&udpaddr)) { session->remote_float = 1; } else { session->remote_float = getconf("REMOTE_FLOAT") ? 1 : 0; port = 2998; if (envval = getconf("REMOTE_PORT")) port = atoi(envval); if (sockaddr_set_port(&udpaddr, port)) return -1; session->remote_addr = udpaddr; if (session->remote_float) { session->remote_float = 2; } else { if (connect(sfd, (struct sockaddr*)&udpaddr, sizeof(udpaddr))) return errorexitp("Could not connect socket"); } } if (ai_local) freeaddrinfo(ai_local); if (ai_remote) freeaddrinfo(ai_remote); session->fd_socket = sfd; return sfd; }
void main(int argc, char *argv[]) { char *p; int c; thechar = '6'; thestring = "amd64"; ensuresymb(NSYMB); memset(debug, 0, sizeof(debug)); cinit(); outfile = 0; setinclude("."); ARGBEGIN { default: c = ARGC(); if(c >= 0 || c < sizeof(debug)) debug[c] = 1; break; case 'o': outfile = ARGF(); break; case 'D': p = ARGF(); if(p) { if (nDlist%8 == 0) Dlist = allocn(Dlist, nDlist*sizeof(char *), 8*sizeof(char *)); Dlist[nDlist++] = p; } break; case 'I': p = ARGF(); setinclude(p); break; } ARGEND if(*argv == 0) { print("usage: %ca [-options] file.s\n", thechar); errorexit(); } if(argc > 1){ print("can't assemble multiple files\n"); errorexit(); } if(assemble(argv[0])) errorexit(); exits(0); }
void main(int argc, char *argv[]) { char *p; thechar = '6'; thestring = "amd64"; // Allow GOARCH=thestring or GOARCH=thestringsuffix, // but not other values. p = getgoarch(); if(strncmp(p, thestring, strlen(thestring)) != 0) sysfatal("cannot use %cc with GOARCH=%s", thechar, p); if(strcmp(p, "amd64p32") == 0) thelinkarch = &linkamd64p32; ctxt = linknew(thelinkarch); ctxt->diag = yyerror; ctxt->bso = &bstdout; ctxt->enforce_data_order = 1; Binit(&bstdout, 1, OWRITE); listinit6(); fmtinstall('L', Lconv); ensuresymb(NSYMB); memset(debug, 0, sizeof(debug)); cinit(); outfile = 0; setinclude("."); flagfn1("D", "name[=value]: add #define", dodef); flagfn1("I", "dir: add dir to include path", setinclude); flagcount("S", "print assembly and machine code", &debug['S']); flagcount("m", "debug preprocessor macros", &debug['m']); flagstr("o", "file: set output file", &outfile); flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath); flagparse(&argc, &argv, usage); ctxt->debugasm = debug['S']; if(argc < 1) usage(); if(argc > 1){ print("can't assemble multiple files\n"); errorexit(); } if(assemble(argv[0])) errorexit(); Bflush(&bstdout); exits(0); }
void preamble() /* process preamble, use random access */ { mseek(DVIfile, 0L, absolute); /* read the dvifile from the start */ if ((opcode = skipnops()) != PRE) errorexit(nopre); opcode = (int) get1(); /* check id in preamble, ignore rest of it */ if (opcode != VERSIONID) errorexit(badid); mseek(DVIfile, 12L, relative); /* numerator, denominator, magnification */ mseek(DVIfile, get1(), relative); /* skip job identification */ } /* preamble */
int relinv(int a) { switch(a) { case AJEQ: return AJNE; case AJNE: return AJEQ; case AJLE: return AJGT; case AJLS: return AJHI; case AJLT: return AJGE; case AJMI: return AJPL; case AJGE: return AJLT; case AJPL: return AJMI; case AJGT: return AJLE; case AJHI: return AJLS; case AJCS: return AJCC; case AJCC: return AJCS; case AJPS: return AJPC; case AJPC: return AJPS; case AJOS: return AJOC; case AJOC: return AJOS; } diag("unknown relation: %s in %s", anames[a], TNAME); errorexit(); return a; }
void dumpobj(void) { bout = Bopen(outfile, OWRITE); if(bout == nil) { flusherrors(); print("can't create %s: %r\n", outfile); errorexit(); } Bprint(bout, "go object %s %s %s %s\n", getgoos(), thestring, getgoversion(), expstring()); Bprint(bout, " exports automatically generated from\n"); Bprint(bout, " %s in package \"%s\"\n", curio.infile, localpkg->name); dumpexport(); Bprint(bout, "\n!\n"); outhist(bout); dumpglobls(); dumptypestructs(); dumpdata(); dumpfuncs(); Bterm(bout); }
void usage(void) { print("usage: %ca [options] file.c...\n", thechar); flagprint(1); errorexit(); }
void emit_1_or_2_operandsl_vartext (char *str0, char *str1) { int j; if (tokennum == 2) { printf (" %s ", str0); emitanoperand (1, "ds:dword ptr", 1); } else if (tokennum == 3) { if (!_stricmp (tokens[2], "%st(0)")) printf (" %s ", str0); else printf (" %s ", str1); emitanoperand (2, "ds:dword ptr", 1); printf (","); emitanoperand (1, "ds:dword ptr", 1); } else { fprintf (stderr, "Error: too many operands\n"); for (j=0 ; j<tokennum ; j++) fprintf (stderr, "%s\n", tokens[j]); fprintf (stderr, "\n"); errorexit (); } }
void walkpages() /* process the pages in the DVI-file */ { register bool wantpage; pagecounter = 0L; while ((opcode = skipnops()) != POST) { if (opcode != BOP) /* should be at start of page now */ errorexit(nobop); else { pagecounter++; pagenr = sget4(); /* get TeX page number */ mseek(DVIfile, 36L, relative); /* skip page header */ backpointer = sget4(); /* get previous page offset */ if (pageswitchon) if (sequenceon) wantpage = inlist(pagecounter); else wantpage = inlist(pagenr); else wantpage = TRUE; if (wantpage) { initpage(); dopage(); printpage(); } else { skippage(); } } } } /* walkpages */
ElfShdr* elfshname(char *name) { int i, off; ElfShdr *sh; for(i=0; i<nelfstr; i++) { if(strcmp(name, elfstr[i].s) == 0) { off = elfstr[i].off; goto found; } } diag("cannot find elf name %s", name); errorexit(); return nil; found: for(i=0; i<hdr.shnum; i++) { sh = shdr[i]; if(sh->name == off) return sh; } sh = newElfShdr(off); return sh; }
// replace all "". with pkg. char* expandpkg(char *t0, char *pkg) { int n; char *p; char *w, *w0, *t; n = 0; for(p=t0; (p=strstr(p, "\"\".")) != nil; p+=3) n++; if(n == 0) return t0; // use malloc, not mal, so that caller can free w0 = malloc(strlen(t0) + strlen(pkg)*n); if(w0 == nil) { diag("out of memory"); errorexit(); } w = w0; for(p=t=t0; (p=strstr(p, "\"\".")) != nil; p=t) { memmove(w, t, p - t); w += p-t; strcpy(w, pkg); w += strlen(pkg); t = p+2; } strcpy(w, t); return w0; }
/* * For more than 8 characters section names, name contains a slash (/) that is * followed by an ASCII representation of a decimal number that is an offset into * the string table. * reference: pecoff_v8.docx Page 24. * <http://www.microsoft.com/whdc/system/platform/firmware/PECOFFdwn.mspx> */ IMAGE_SECTION_HEADER* newPEDWARFSection(char *name, vlong size) { IMAGE_SECTION_HEADER *h; char s[8]; if(size == 0) return nil; if(nextsymoff+strlen(name)+1 > sizeof(symnames)) { diag("pe string table is full"); errorexit(); } strcpy(&symnames[nextsymoff], name); sprint(s, "/%d\0", nextsymoff+4); nextsymoff += strlen(name); symnames[nextsymoff] = 0; nextsymoff ++; h = addpesection(s, size, size); h->Characteristics = IMAGE_SCN_MEM_READ| IMAGE_SCN_MEM_DISCARDABLE; return h; }
int assemble(char *file) { char *ofile, *p; int i, of; ofile = alloc(strlen(file)+3); // +3 for .x\0 (x=thechar) strcpy(ofile, file); p = utfrrune(ofile, pathchar()); if(p) { include[0] = ofile; *p++ = 0; } else p = ofile; if(outfile == 0) { outfile = p; if(outfile){ p = utfrrune(outfile, '.'); if(p) if(p[1] == 's' && p[2] == 0) p[0] = 0; p = utfrune(outfile, 0); p[0] = '.'; p[1] = thechar; p[2] = 0; } else outfile = "/dev/null"; } of = create(outfile, OWRITE, 0664); if(of < 0) { yyerror("%ca: cannot create %s", thechar, outfile); errorexit(); } Binit(&obuf, of, OWRITE); pass = 1; pinit(file); Bprint(&obuf, "go object %s %s %s\n", getgoos(), thestring, getgoversion()); for(i=0; i<nDlist; i++) dodefine(Dlist[i]); yyparse(); if(nerrors) { cclean(); return nerrors; } Bprint(&obuf, "\n!\n"); pass = 2; outhist(); pinit(file); for(i=0; i<nDlist; i++) dodefine(Dlist[i]); yyparse(); cclean(); return nerrors; }
void emit_1_or_2_operandsl (void) { int j; if (tokennum == 2) { printf (" "); emitanoperand (1, "ds:dword ptr", 1); } else if (tokennum == 3) { printf (" "); emitanoperand (2, "ds:dword ptr", 1); printf (","); emitanoperand (1, "ds:dword ptr", 1); } else { fprintf (stderr, "Error: too many operands\n"); for (j=0 ; j<tokennum ; j++) fprintf (stderr, "%s\n", tokens[j]); fprintf (stderr, "\n"); errorexit (); } }
int assemble(char *file) { char ofile[100], incfile[20], *p; int i, of; strcpy(ofile, file); if(p = strrchr(ofile, pathchar())) { include[0] = ofile; *p++ = 0; } else p = ofile; if(outfile == 0) { outfile = p; if(p = strrchr(outfile, '.')) if(p[1] == 's' && p[2] == 0) p[0] = 0; p = strrchr(outfile, 0); p[0] = '.'; p[1] = thechar; p[2] = 0; } p = getenv("INCLUDE"); if(p) { setinclude(p); } else { if(systemtype(Plan9)) { sprint(incfile,"/%s/include", thestring); setinclude(strdup(incfile)); } } of = mycreat(outfile, 0664); if(of < 0) { yyerror("%ca: cannot create %s", thechar, outfile); errorexit(); } Binit(&obuf, of, OWRITE); pass = 1; nosched = 0; pinit(file); for(i=0; i<nDlist; i++) dodefine(Dlist[i]); yyparse(); if(nerrors) { cclean(); return nerrors; } pass = 2; nosched = 0; outhist(); pinit(file); for(i=0; i<nDlist; i++) dodefine(Dlist[i]); yyparse(); cclean(); return nerrors; }
static int32 vaddr(Adr *a, Reloc *r) { int t; int32 v; Sym *s; if(r != nil) memset(r, 0, sizeof *r); t = a->type; v = a->offset; if(t == D_ADDR) t = a->index; switch(t) { case D_STATIC: case D_EXTERN: s = a->sym; if(s != nil) { 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; } } return v; }
static int sockaddr_set_port(sockaddr_any* sa, int port) { port = htons(port); int af = sa->any.sa_family; if (af == AF_INET) sa->ip4.sin_port = port; else if (af == AF_INET6) sa->ip6.sin6_port = port; else return errorexit("Unknown address family"); return 0; }
static void chksectoff(IMAGE_SECTION_HEADER *h, vlong off) { if(off != h->PointerToRawData) { diag("%s.PointerToRawData = %#llux, want %#llux", (char *)h->Name, (vlong)h->PointerToRawData, off); errorexit(); } }
void archinit(void) { // getgoextlinkenabled is based on GO_EXTLINK_ENABLED when // Go was built; see ../../make.bash. if(linkmode == LinkAuto && strcmp(getgoextlinkenabled(), "0") == 0) linkmode = LinkInternal; switch(HEADTYPE) { default: if(linkmode == LinkAuto) linkmode = LinkInternal; if(linkmode == LinkExternal && strcmp(getgoextlinkenabled(), "1") != 0) sysfatal("cannot use -linkmode=external with -H %s", headstr(HEADTYPE)); break; } switch(HEADTYPE) { default: diag("unknown -H option"); errorexit(); case Hplan9: /* plan 9 */ HEADR = 32L; if(INITTEXT == -1) INITTEXT = 4128; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4096; break; case Hlinux: /* ppc64 elf */ debug['d'] = 1; // TODO(minux): dynamic linking is not supported yet. elfinit(); HEADR = ELFRESERVE; if(INITTEXT == -1) INITTEXT = 0x10000 + HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 0x10000; break; case Hnacl: elfinit(); HEADR = 0x10000; funcalign = 16; if(INITTEXT == -1) INITTEXT = 0x20000; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 0x10000; break; } if(INITDAT != 0 && INITRND != 0) print("warning: -D0x%ux is ignored because of -R0x%ux\n", INITDAT, INITRND); }
void elfsetstring(char *s, int off) { if(nelfstr >= nelem(elfstr)) { diag("too many elf strings"); errorexit(); } elfstr[nelfstr].s = s; elfstr[nelfstr].off = off; nelfstr++; }
static void blk(Sym *start, int32 addr, int32 size) { Sym *sym; int32 eaddr; uchar *p, *ep; for(sym = start; sym != nil; sym = sym->next) if(!(sym->type&SSUB) && sym->value >= addr) break; eaddr = addr+size; for(; sym != nil; sym = sym->next) { if(sym->type&SSUB) continue; if(sym->value >= eaddr) break; if(sym->value < addr) { diag("phase error: addr=%#llx but sym=%#llx type=%d", (vlong)addr, (vlong)sym->value, sym->type); errorexit(); } cursym = sym; for(; addr < sym->value; addr++) cput(0); p = sym->p; ep = p + sym->np; while(p < ep) cput(*p++); addr += sym->np; for(; addr < sym->value+sym->size; addr++) cput(0); if(addr != sym->value+sym->size) { diag("phase error: addr=%#llx value+size=%#llx", (vlong)addr, (vlong)sym->value+sym->size); errorexit(); } } for(; addr < eaddr; addr++) cput(0); cflush(); }
void postamble() /* find and process postamble, use random access */ { register long size; register int count; struct stat st; fstat (fileno(DVIfile), &st); size = (long) st.st_size; /* get size of file */ count = -1; do { /* back file up past signature bytes (223), to id-byte */ if (size == 0) errorexit(nopst); size--; mseek(DVIfile, size, absolute); opcode = (int) get1(); count++; } while (opcode == TRAILER); if (count < 4) { /* must have 4 trailer bytes */ foo = count; errorexit(fwsgn); } if (opcode != VERSIONID) errorexit(badid); mseek(DVIfile, size-4, absolute); /* back up to back-pointer */ mseek(DVIfile, sget4(), absolute); /* and to start of postamble */ if (get1() != POST) errorexit(nopst); mseek(DVIfile, 20L, relative); /* lastpageoffset, numerator, denominator */ /* magnification, maxpageheight */ maxpagewidth = sget4(); charwidth = maxpagewidth / (ttywidth + espace); stackmax = (int) get2(); if ((stack = (stackitem *) malloc(stackmax * sizeof(stackitem))) == NULL) errorexit(stkrq); /* get2() -- totalpages */ /* fontdefs do fontdefs in flight ... */ } /* postamble */
void setlinkmode(char *arg) { if(strcmp(arg, "internal") == 0) linkmode = LinkInternal; else if(strcmp(arg, "external") == 0) linkmode = LinkExternal; else if(strcmp(arg, "auto") == 0) linkmode = LinkAuto; else { fprint(2, "unknown link mode -linkmode %s\n", arg); errorexit(); } }
int main(int argc, char **argv) { HFILE dHandle; char *drive = "a:"; int choice; copyr(); if ((argc > 2) || ((argc == 2) && (argv[1][1] != ':'))) { fputs("usage: dskcpy2 drive_letter:", stderr); exit(1); } if (argc == 2) drive = argv[1]; DosError(HARDERROR_DISABLE); do { choice = dskcpy_menu(gotSource, drive); switch (choice) { case READ_SOURCE: query("Place SOURCE disk in drive %s and strike any key when ready..", drive); if ((dHandle = opendrive(drive)) == 0) errorexit(dHandle); if (lockdrive(dHandle)) errorexit(dHandle); if (readsource(dHandle)) errorexit(dHandle); if (unlockdrive(dHandle)) errorexit(dHandle); DosClose(dHandle); break; case COPY_TARGET: query("Place TARGET disk in drive %s and strike any key when ready..", drive); if ((dHandle = opendrive(drive)) == 0) errorexit(dHandle); if (lockdrive(dHandle)) errorexit(dHandle); if (writetarget(dHandle)) errorexit(dHandle); if (unlockdrive(dHandle)) errorexit(dHandle); DosClose(dHandle); break; default: break; } } while (choice != EXIT_DSKCPY); return _DosError; }
static void dowrite(int fd, char *p, int n) { int m; while(n > 0) { m = write(fd, p, n); if(m <= 0) { ctxt->cursym = S; diag("write error: %r"); errorexit(); } n -= m; p += m; } }
Reloc* addrel(Sym *s) { if(s->nr >= s->maxr) { if(s->maxr == 0) s->maxr = 4; else s->maxr <<= 1; s->r = realloc(s->r, s->maxr*sizeof s->r[0]); if(s->r == 0) { diag("out of memory"); errorexit(); } memset(s->r+s->nr, 0, (s->maxr-s->nr)*sizeof s->r[0]); } return &s->r[s->nr++]; }
void libinit(void) { char *suffix, *suffixsep; fmtinstall('i', iconv); fmtinstall('Y', Yconv); fmtinstall('Z', Zconv); mywhatsys(); // get goroot, goarch, goos if(strcmp(goarch, thestring) != 0) print("goarch is not known: %s\n", goarch); // add goroot to the end of the libdir list. suffix = ""; suffixsep = ""; if(flag_installsuffix != nil) { suffixsep = "_"; suffix = flag_installsuffix; } else if(flag_race) { suffixsep = "_"; suffix = "race"; } Lflag(smprint("%s/pkg/%s_%s%s%s", goroot, goos, goarch, suffixsep, suffix)); // Unix doesn't like it when we write to a running (or, sometimes, // recently run) binary, so remove the output file before writing it. // On Windows 7, remove() can force the following create() to fail. #ifndef _WIN32 remove(outfile); #endif cout = create(outfile, 1, 0775); if(cout < 0) { diag("cannot create %s: %r", outfile); errorexit(); } if(INITENTRY == nil) { INITENTRY = mal(strlen(goarch)+strlen(goos)+20); if(!flag_shared) { sprint(INITENTRY, "_rt0_%s_%s", goarch, goos); } else { sprint(INITENTRY, "_rt0_%s_%s_lib", goarch, goos); } } linklookup(ctxt, INITENTRY, 0)->type = SXREF; }
void symgrow(Sym *s, int32 siz) { if(s->np >= siz) return; if(s->maxp < siz) { if(s->maxp == 0) s->maxp = 8; while(s->maxp < siz) s->maxp <<= 1; s->p = realloc(s->p, s->maxp); if(s->p == nil) { diag("out of memory"); errorexit(); } memset(s->p+s->np, 0, s->maxp-s->np); } s->np = siz; }
void hostobjs(void) { int i; Biobuf *f; Hostobj *h; for(i=0; i<nhostobj; i++) { h = &hostobj[i]; f = Bopen(h->file, OREAD); if(f == nil) { ctxt->cursym = S; diag("cannot reopen %s: %r", h->pn); errorexit(); } Bseek(f, h->off, 0); h->ld(f, h->pkg, h->len, h->pn); Bterm(f); } }
void diag(char *fmt, ...) { char buf[STRINGSZ], *tn; va_list arg; tn = "??none??"; if(curtext != P && curtext->from.sym != S) tn = curtext->from.sym->name; va_start(arg, fmt); vseprint(buf, buf+sizeof(buf), fmt, arg); va_end(arg); print("%s: %s\n", tn, buf); nerrors++; if(nerrors > 10) { print("too many errors\n"); errorexit(); } }