Bprint(&bso, "%5.2f sym\n", cputime());
Bflush(&bso);
switch(HEADTYPE) {
- case 0:
- case 1:
- case 4:
- case 5:
+ case Hnoheader:
+ case Hrisc:
+ case Hixp1200:
+ case Hipaq:
debug['s'] = 1;
break;
- case 2:
+ case Hplan9x32:
OFFSET = HEADR+textsize+segdata.filelen;
seek(cout, OFFSET, 0);
break;
- case 3:
+ case Hnetbsd:
OFFSET += rnd(segdata.filelen, 4096);
seek(cout, OFFSET, 0);
break;
- case 6:
+ case Hlinux:
OFFSET += segdata.filelen;
seek(cout, rnd(OFFSET, INITRND), 0);
break;
OFFSET = 0;
seek(cout, OFFSET, 0);
switch(HEADTYPE) {
- case 0: /* no header */
+ case Hnoheader: /* no header */
break;
- case 1: /* aif for risc os */
+ case Hrisc: /* aif for risc os */
lputl(0xe1a00000); /* NOP - decompress code */
lputl(0xe1a00000); /* NOP - relocation code */
lputl(0xeb000000 + 12); /* BL - zero init code */
lputl(0xe1a00000); /* NOP - zero init code */
lputl(0xe1a0f00e); /* B (R14) - zero init return */
break;
- case 2: /* plan 9 */
+ case Hplan9x32: /* plan 9 */
lput(0x647); /* magic */
lput(textsize); /* sizes */
lput(segdata.filelen);
lput(0L);
lput(lcsize);
break;
- case 3: /* boot for NetBSD */
+ case Hnetbsd: /* boot for NetBSD */
lput((143<<16)|0413); /* magic */
lputl(rnd(HEADR+textsize, 4096));
lputl(rnd(segdata.filelen, 4096));
lputl(0L);
lputl(0L);
break;
- case 4: /* boot for IXP1200 */
+ case Hixp1200: /* boot for IXP1200 */
break;
- case 5: /* boot for ipaq */
+ case Hipaq: /* boot for ipaq */
lputl(0xe3300000); /* nop */
lputl(0xe3300000); /* nop */
lputl(0xe3300000); /* nop */
lputl(0xe3300000); /* nop */
break;
- case 6:
+ case Hlinux:
/* elf arm */
eh = getElfEhdr();
fo = HEADR;
-F
Force use of software floating point.
Also implied by setting GOARM=5 in the environment.
+-Hlinux
+ Write Linux ELF binaries (default when $GOOS is linux)
-I interpreter
Set the ELF dynamic linker to use.
-L dir1 -L dir2
char *noname = "<none>";
char *thestring = "arm";
+Header headers[] = {
+ "noheader", Hnoheader,
+ "risc", Hrisc,
+ "plan9", Hplan9x32,
+ "netbsd", Hnetbsd,
+ "ixp1200", Hixp1200,
+ "ipaq", Hipaq,
+ "linux", Hlinux,
+ 0, 0
+};
+
/*
- * -H1 -T0x10005000 -R4 is aif for risc os
- * -H2 -T4128 -R4096 is plan9 format
- * -H3 -T0xF0000020 -R4 is NetBSD format
- * -H4 is IXP1200 (raw)
- * -H5 -T0xC0008010 -R1024 is ipaq
+ * -Hrisc -T0x10005000 -R4 is aif for risc os
+ * -Hplan9 -T4128 -R4096 is plan9 format
+ * -Hnetbsd -T0xF0000020 -R4 is NetBSD format
+ * -Hixp1200 is IXP1200 (raw)
+ * -Hipaq -T0xC0008010 -R1024 is ipaq
+ * -Hlinux -Tx -Rx is linux elf
*/
static char*
rpath = EARGF(usage());
break;
case 'H':
- HEADTYPE = atolwhex(EARGF(usage()));
+ HEADTYPE = headtype(EARGF(usage()));
/* do something about setting INITTEXT */
break;
case 'V':
debug[DEFAULT] = 1;
if(HEADTYPE == -1) {
if(debug['U'])
- HEADTYPE = 0;
+ HEADTYPE = Hnoheader;
if(debug['B'])
- HEADTYPE = 1;
+ HEADTYPE = Hrisc;
if(debug['9'])
- HEADTYPE = 2;
- HEADTYPE = 6;
+ HEADTYPE = Hplan9x32;
+ HEADTYPE = Hlinux;
}
switch(HEADTYPE) {
default:
diag("unknown -H option");
errorexit();
- case 0: /* no header */
+ case Hnoheader: /* no header */
HEADR = 0L;
if(INITTEXT == -1)
INITTEXT = 0;
if(INITRND == -1)
INITRND = 4;
break;
- case 1: /* aif for risc os */
+ case Hrisc: /* aif for risc os */
HEADR = 128L;
if(INITTEXT == -1)
INITTEXT = 0x10005000 + HEADR;
if(INITRND == -1)
INITRND = 4;
break;
- case 2: /* plan 9 */
+ case Hplan9x32: /* plan 9 */
HEADR = 32L;
if(INITTEXT == -1)
INITTEXT = 4128;
if(INITRND == -1)
INITRND = 4096;
break;
- case 3: /* boot for NetBSD */
+ case Hnetbsd: /* boot for NetBSD */
HEADR = 32L;
if(INITTEXT == -1)
INITTEXT = 0xF0000020L;
if(INITRND == -1)
INITRND = 4096;
break;
- case 4: /* boot for IXP1200 */
+ case Hixp1200: /* boot for IXP1200 */
HEADR = 0L;
if(INITTEXT == -1)
INITTEXT = 0x0;
if(INITRND == -1)
INITRND = 4;
break;
- case 5: /* boot for ipaq */
+ case Hipaq: /* boot for ipaq */
HEADR = 16L;
if(INITTEXT == -1)
INITTEXT = 0xC0008010;
if(INITRND == -1)
INITRND = 1024;
break;
- case 6: /* arm elf */
+ case Hlinux: /* arm elf */
debug['d'] = 1; // no dynamic linking
elfinit();
HEADR = ELFRESERVE;
r->type = 256; // ignore during relocsym
return;
}
- if(HEADTYPE == 6 && s->size == PtrSize && r->off == 0) {
+ if(HEADTYPE == Hdarwin && s->size == PtrSize && r->off == 0) {
// Mach-O relocations are a royal pain to lay out.
// They use a compact stateful bytecode representation
// that is too much bother to deal with.
adduint64(rela, 0);
s->plt = plt->size - 16;
- } else if(HEADTYPE == 6) { // Mach-O
+ } else if(HEADTYPE == Hdarwin) {
// To do lazy symbol lookup right, we're supposed
// to tell the dynamic loader which library each
// symbol comes from and format the link info
addaddrplus(rela, got, s->got);
adduint64(rela, ELF64_R_INFO(s->dynid, R_X86_64_GLOB_DAT));
adduint64(rela, 0);
- } else if(HEADTYPE == 6) { // Mach-O
+ } else if(HEADTYPE == Hdarwin) {
adduint32(lookup(".linkedit.got", 0), s->dynid);
} else {
diag("addgotsym: unsupported binary format");
elfwritedynent(lookup(".dynamic", 0), DT_NEEDED,
addstring(lookup(".dynstr", 0), s->dynimplib));
}
- } else if(HEADTYPE == 6) {
+ } else if(HEADTYPE == Hdarwin) {
// Mach-o symbol nlist64
d = lookup(".dynsym", 0);
name = s->dynimpname;
if(s->size == 0)
addstring(s, "");
elfwritedynent(lookup(".dynamic", 0), DT_NEEDED, addstring(s, lib));
- } else if(HEADTYPE == 6) { // Mach-O
+ } else if(HEADTYPE == Hdarwin) {
machoadddynlib(lib);
} else {
diag("adddynlib: unsupported binary format");
{
Sym *s, *shstrtab, *dynstr;
- if(HEADTYPE != 7 && HEADTYPE != 9)
+ if(HEADTYPE != Hlinux && HEADTYPE != Hfreebsd)
return;
/* predefine strings we need for section headers */
datblk(segdata.vaddr, segdata.filelen);
machlink = 0;
- if(HEADTYPE == 6)
+ if(HEADTYPE == Hdarwin)
machlink = domacholink();
switch(HEADTYPE) {
default:
diag("unknown header type %d", HEADTYPE);
- case 2:
- case 5:
+ case Hplan9x32:
+ case Helf:
break;
- case 6:
+ case Hdarwin:
debug['8'] = 1; /* 64-bit addresses */
break;
- case 7:
- case 9:
+ case Hlinux:
+ case Hfreebsd:
debug['8'] = 1; /* 64-bit addresses */
/* index of elf text section; needed by asmelfsym, double-checked below */
/* !debug['d'] causes extra sections before the .text section */
if(!debug['d'])
elftextsh += 10;
break;
- case 10:
+ case Hwindows:
break;
}
Bflush(&bso);
switch(HEADTYPE) {
default:
- case 2:
- case 5:
+ case Hplan9x32:
+ case Helf:
debug['s'] = 1;
symo = HEADR+segtext.len+segdata.filelen;
break;
- case 6:
+ case Hdarwin:
symo = rnd(HEADR+segtext.len, INITRND)+rnd(segdata.filelen, INITRND)+machlink;
break;
- case 7:
- case 9:
+ case Hlinux:
+ case Hfreebsd:
symo = rnd(HEADR+segtext.len, INITRND)+segdata.filelen;
symo = rnd(symo, INITRND);
break;
- case 10:
+ case Hwindows:
symo = rnd(HEADR+segtext.filelen, PEFILEALIGN)+segdata.filelen;
symo = rnd(symo, PEFILEALIGN);
break;
lputl(symsize);
lputl(lcsize);
cflush();
- if(HEADTYPE != 10 && !debug['s']) {
+ if(HEADTYPE != Hwindows && !debug['s']) {
elfsymo = symo+8+symsize+lcsize;
seek(cout, elfsymo, 0);
asmelfsym64();
seek(cout, 0L, 0);
switch(HEADTYPE) {
default:
- case 2: /* plan9 */
+ case Hplan9x32: /* plan9 */
magic = 4*26*26+7;
magic |= 0x00008000; /* fat header */
lputb(magic); /* magic */
lputb(lcsize); /* line offsets */
vputb(vl); /* va of entry */
break;
- case 3: /* plan9 */
+ case Hplan9x64: /* plan9 */
magic = 4*26*26+7;
lputb(magic); /* magic */
lputb(segtext.filelen); /* sizes */
lputb(spsize); /* sp offsets */
lputb(lcsize); /* line offsets */
break;
- case 6:
+ case Hdarwin:
asmbmacho();
break;
- case 7:
- case 9:
+ case Hlinux:
+ case Hfreebsd:
/* elf amd-64 */
eh = getElfEhdr();
sh->addralign = 1;
if(interpreter == nil) {
switch(HEADTYPE) {
- case 7:
+ case Hlinux:
interpreter = linuxdynld;
break;
- case 9:
+ case Hfreebsd:
interpreter = freebsddynld;
break;
}
eh->ident[EI_MAG1] = 'E';
eh->ident[EI_MAG2] = 'L';
eh->ident[EI_MAG3] = 'F';
- if(HEADTYPE == 9)
+ if(HEADTYPE == Hfreebsd)
eh->ident[EI_OSABI] = 9;
eh->ident[EI_CLASS] = ELFCLASS64;
eh->ident[EI_DATA] = ELFDATA2LSB;
if(a+elfwriteinterp() > ELFRESERVE)
diag("ELFRESERVE too small: %d > %d", a, ELFRESERVE);
break;
- case 10:
+ case Hwindows:
asmbpe();
break;
}
-e
Emit an extra ELF-compatible symbol table useful with tools such as
nm, gdb, and oprofile. This option makes the binary file considerably larger.
--H6
+-Hdarwin
Write Apple Mach-O binaries (default when $GOOS is darwin)
--H7
+-Hlinux
Write Linux ELF binaries (default when $GOOS is linux)
+-Hfreebsd
+ Write FreeBSD ELF binaries (default when $GOOS is freebsd)
+-Hwindows
+ Write Windows PE32+ binaries (default when $GOOS is windows)
-I interpreter
Set the ELF dynamic linker to use.
-L dir1 -L dir2
char* thestring = "amd64";
char* paramspace = "FP";
+Header headers[] = {
+ "plan9x32", Hplan9x32,
+ "plan9", Hplan9x64,
+ "elf", Helf,
+ "darwin", Hdarwin,
+ "linux", Hlinux,
+ "freebsd", Hfreebsd,
+ "windows", Hwindows,
+ 0, 0
+};
+
/*
- * -H2 -T4136 -R4096 is plan9 64-bit format
- * -H3 -T4128 -R4096 is plan9 32-bit format
- * -H5 -T0x80110000 -R4096 is ELF32
- * -H6 -Tx -Rx is apple MH-exec
- * -H7 -Tx -Rx is linux elf-exec
- * -H9 -Tx -Rx is FreeBSD elf-exec
+ * -Hplan9x32 -T4136 -R4096 is plan9 64-bit format
+ * -Hplan9 -T4128 -R4096 is plan9 32-bit format
+ * -Helf -T0x80110000 -R4096 is ELF32
+ * -Hdarwin -Tx -Rx is apple MH-exec
+ * -Hlinux -Tx -Rx is linux elf-exec
+ * -Hfreebsd -Tx -Rx is FreeBSD elf-exec
+ * -Hwindows -Tx -Rx is MS Windows PE32+
*
* options used: 189BLQSWabcjlnpsvz
*/
INITENTRY = EARGF(usage());
break;
case 'H':
- HEADTYPE = atolwhex(EARGF(usage()));
+ HEADTYPE = headtype(EARGF(usage()));
break;
case 'I':
interpreter = EARGF(usage());
libinit();
- if(HEADTYPE == -1) {
- HEADTYPE = 2;
- if(strcmp(goos, "linux") == 0)
- HEADTYPE = 7;
- else
- if(strcmp(goos, "darwin") == 0)
- HEADTYPE = 6;
- else
- if(strcmp(goos, "freebsd") == 0)
- HEADTYPE = 9;
- else
- if(strcmp(goos, "windows") == 0)
- HEADTYPE = 10;
- else
- print("goos is not known: %s\n", goos);
- }
+ if(HEADTYPE == -1)
+ HEADTYPE = headtype(goos);
switch(HEADTYPE) {
default:
diag("unknown -H option");
errorexit();
- case 2: /* plan 9 */
+ case Hplan9x32: /* plan 9 */
HEADR = 32L+8L;
if(INITTEXT == -1)
INITTEXT = 4096+HEADR;
if(INITRND == -1)
INITRND = 4096;
break;
- case 3: /* plan 9 */
+ case Hplan9x64: /* plan 9 */
HEADR = 32L;
if(INITTEXT == -1)
INITTEXT = 4096+32;
if(INITRND == -1)
INITRND = 4096;
break;
- case 5: /* elf32 executable */
+ case Helf: /* elf32 executable */
HEADR = rnd(52L+3*32L, 16);
if(INITTEXT == -1)
INITTEXT = 0x80110000L;
if(INITRND == -1)
INITRND = 4096;
break;
- case 6: /* apple MACH */
+ case Hdarwin: /* apple MACH */
/*
* OS X system constant - offset from 0(GS) to our TLS.
* Explained in ../../libcgo/darwin_amd64.c.
if(INITDAT == -1)
INITDAT = 0;
break;
- case 7: /* elf64 executable */
- case 9: /* freebsd */
+ case Hlinux: /* elf64 executable */
+ case Hfreebsd: /* freebsd */
/*
* ELF uses TLS offset negative from FS.
* Translate 0(FS) and 8(FS) into -16(FS) and -8(FS).
if(INITRND == -1)
INITRND = 4096;
break;
- case 10: /* PE executable */
+ case Hwindows: /* PE executable */
peinit();
HEADR = PEFILEHEADR;
if(INITTEXT == -1)
patch();
follow();
doelf();
- if(HEADTYPE == 6)
+ if(HEADTYPE == Hdarwin)
domacho();
dostkoff();
dostkcheck();
else
doprof2();
span();
- if(HEADTYPE == 10)
+ if(HEADTYPE == Hwindows)
dope();
addexport();
textaddress();
vexit = s->value;
for(cursym = textp; cursym != nil; cursym = cursym->next)
for(p = cursym->text; p != P; p = p->link) {
- if(HEADTYPE == 10) {
+ if(HEADTYPE == Hwindows) {
// Windows
// Convert
// op n(GS), reg
p->from.offset = 0x58;
}
}
- if(HEADTYPE == 7 || HEADTYPE == 9) {
+ if(HEADTYPE == Hlinux || HEADTYPE == Hfreebsd) {
// ELF uses FS instead of GS.
if(p->from.type == D_INDIR+D_GS)
p->from.type = D_INDIR+D_FS;
if(!(p->from.scale & NOSPLIT)) {
p = appendp(p); // load g into CX
p->as = AMOVQ;
- if(HEADTYPE == 7 || HEADTYPE == 9) // ELF uses FS
+ if(HEADTYPE == Hlinux || HEADTYPE == Hfreebsd) // ELF uses FS
p->from.type = D_INDIR+D_FS;
else
p->from.type = D_INDIR+D_GS;
p->from.offset = tlsoffset+0;
p->to.type = D_CX;
- if(HEADTYPE == 10) { // Windows
+ if(HEADTYPE == Hwindows) {
// movq %gs:0x58, %rcx
// movq (%rcx), %rcx
p->as = AMOVQ;
r->sym = S;
return;
}
- if(HEADTYPE == 6 && s->size == PtrSize && r->off == 0) {
+ if(HEADTYPE == Hdarwin && s->size == PtrSize && r->off == 0) {
// Mach-O relocations are a royal pain to lay out.
// They use a compact stateful bytecode representation
// that is too much bother to deal with.
adduint32(rel, ELF32_R_INFO(s->dynid, R_386_JMP_SLOT));
s->plt = plt->size - 16;
- } else if(HEADTYPE == 6) { // Mach-O
+ } else if(HEADTYPE == Hdarwin) {
// Same laziness as in 6l.
Sym *plt;
rel = lookup(".rel", 0);
addaddrplus(rel, got, s->got);
adduint32(rel, ELF32_R_INFO(s->dynid, R_386_GLOB_DAT));
- } else if(HEADTYPE == 6) { // Mach-O
+ } else if(HEADTYPE == Hdarwin) {
adduint32(lookup(".linkedit.got", 0), s->dynid);
} else {
diag("addgotsym: unsupported binary format");
}
adduint16(d, t);
}
- } else if(HEADTYPE == 6) {
+ } else if(HEADTYPE == Hdarwin) {
// Mach-O symbol nlist32
d = lookup(".dynsym", 0);
name = s->dynimpname;
adduint8(d, 0); // section
adduint16(d, 0); // desc
adduint32(d, 0); // value
- } else if(HEADTYPE != 10) {
+ } else if(HEADTYPE != Hwindows) {
diag("adddynsym: unsupported binary format");
}
}
if(s->size == 0)
addstring(s, "");
elfwritedynent(lookup(".dynamic", 0), DT_NEEDED, addstring(s, lib));
- } else if(HEADTYPE == 6) { // Mach-O
+ } else if(HEADTYPE == Hdarwin) {
machoadddynlib(lib);
- } else if(HEADTYPE != 10) {
+ } else if(HEADTYPE != Hwindows) {
diag("adddynlib: unsupported binary format");
}
}
datblk(segdata.vaddr, segdata.filelen);
machlink = 0;
- if(HEADTYPE == 6)
+ if(HEADTYPE == Hdarwin)
machlink = domacholink();
if(iself) {
default:
if(iself)
goto Elfsym;
- case 0:
+ case Hgarbunix:
seek(cout, rnd(HEADR+segtext.filelen, 8192)+segdata.filelen, 0);
break;
- case 1:
+ case Hunixcoff:
seek(cout, rnd(HEADR+segtext.filelen, INITRND)+segdata.filelen, 0);
break;
- case 2:
+ case Hplan9x32:
symo = HEADR+segtext.filelen+segdata.filelen;
break;
- case 3:
- case 4:
+ case Hmsdoscom:
+ case Hmsdosexe:
debug['s'] = 1;
symo = HEADR+segtext.filelen+segdata.filelen;
break;
- case 6:
+ case Hdarwin:
symo = rnd(HEADR+segtext.filelen, INITRND)+rnd(segdata.filelen, INITRND)+machlink;
break;
Elfsym:
symo = rnd(HEADR+segtext.filelen, INITRND)+segdata.filelen;
symo = rnd(symo, INITRND);
break;
- case 10:
+ case Hwindows:
// TODO(brainman): not sure what symo meant to be, but it is not used for Windows PE for now anyway
symo = rnd(HEADR+segtext.filelen, PEFILEALIGN)+segdata.filelen;
symo = rnd(symo, PEFILEALIGN);
if(!debug['s']) {
seek(cout, symo, 0);
- if(HEADTYPE == 2) {
+ if(HEADTYPE == Hplan9x32) {
asmplan9sym();
cflush();
cflush();
}
- } else if(HEADTYPE != 10) {
+ } else if(HEADTYPE != Hwindows) {
if(debug['v'])
Bprint(&bso, "%5.2f dwarf\n", cputime());
dwarfemitdebugsections();
default:
if(iself)
goto Elfput;
- case 0: /* garbage */
+ case Hgarbunix: /* garbage */
lputb(0x160L<<16); /* magic and sections */
lputb(0L); /* time and date */
lputb(rnd(HEADR+segtext.filelen, 4096)+segdata.filelen);
lputb(~0L); /* gp value ?? */
break;
lputl(0); /* x */
- case 1: /* unix coff */
+ case Hunixcoff: /* unix coff */
/*
* file header
*/
lputl(0); /* relocation, line numbers */
lputl(0x200); /* flags comment only */
break;
- case 2: /* plan9 */
+ case Hplan9x32: /* plan9 */
magic = 4*11*11+7;
lputb(magic); /* magic */
lputb(segtext.filelen); /* sizes */
lputb(spsize); /* sp offsets */
lputb(lcsize); /* line offsets */
break;
- case 3:
+ case Hmsdoscom:
/* MS-DOS .COM */
break;
- case 4:
+ case Hmsdosexe:
/* fake MS-DOS .EXE */
v = rnd(HEADR+segtext.filelen, INITRND)+segdata.filelen;
wputl(0x5A4D); /* 'MZ' */
wputl(0x0000); /* overlay number */
break;
- case 6:
+ case Hdarwin:
asmbmacho();
break;
Elfput:
/* elf 386 */
- if(HEADTYPE == 11)
+ if(HEADTYPE == Htiny)
debug['d'] = 1;
eh = getElfEhdr();
sh->addralign = 1;
if(interpreter == nil) {
switch(HEADTYPE) {
- case 7:
+ case Hlinux:
interpreter = linuxdynld;
break;
- case 9:
+ case Hfreebsd:
interpreter = freebsddynld;
break;
}
eh->ident[EI_DATA] = ELFDATA2LSB;
eh->ident[EI_VERSION] = EV_CURRENT;
switch(HEADTYPE) {
- case 9:
+ case Hfreebsd:
eh->ident[EI_OSABI] = 9;
break;
}
diag("ELFRESERVE too small: %d > %d", a, ELFRESERVE);
break;
- case 10:
+ case Hwindows:
asmbpe();
break;
}
Elide the dynamic linking header. With this option, the binary
is statically linked and does not refer to dynld. Without this option
(the default), the binary's contents are identical but it is loaded with dynld.
--H6
+-Hplan9
+ Write Plan 9 32-bit format binaries (default when $GOOS is plan9)
+-Hdarwin
Write Apple Mach-O binaries (default when $GOOS is darwin)
--H7
+-Hlinux
Write Linux ELF binaries (default when $GOOS is linux)
+-Hfreebsd
+ Write FreeBSD ELF binaries (default when $GOOS is freebsd)
+-Hwindows
+ Write Windows PE32 binaries (default when $GOOS is windows)
-I interpreter
Set the ELF dynamic linker to use.
-L dir1 -L dir2
char *noname = "<none>";
char *thestring = "386";
+Header headers[] = {
+ "garbunix", Hgarbunix,
+ "unixcoff", Hunixcoff,
+ "plan9", Hplan9x32,
+ "msdoscom", Hmsdoscom,
+ "msdosexe", Hmsdosexe,
+ "darwin", Hdarwin,
+ "linux", Hlinux,
+ "nacl", Hnacl,
+ "freebsd", Hfreebsd,
+ "windows", Hwindows,
+ "tiny", Htiny,
+ 0, 0
+};
+
/*
- * -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
- * -H6 -Tx -Rx is Apple Mach-O
- * -H7 -Tx -Rx is Linux ELF32
- * -H8 -Tx -Rx was Google Native Client
- * -H9 -Tx -Rx is FreeBSD ELF32
- * -H10 -Tx -Rx is MS Windows PE
- * -H11 -Tx -Rx is tiny (os image)
+ * -Hgarbunix -T0x40004C -D0x10000000 is garbage unix
+ * -Hunixcoff -T0xd0 -R4 is unix coff
+ * -Hplan9 -T4128 -R4096 is plan9 format
+ * -Hmsdoscom -Tx -Rx is MS-DOS .COM
+ * -Hmsdosexe -Tx -Rx is fake MS-DOS .EXE
+ * -Hdarwin -Tx -Rx is Apple Mach-O
+ * -Hlinux -Tx -Rx is Linux ELF32
+ * -Hnacl -Tx -Rx was Google Native Client
+ * -Hfreebsd -Tx -Rx is FreeBSD ELF32
+ * -Hwindows -Tx -Rx is MS Windows PE32
+ * -Htiny -Tx -Rx is tiny (os image)
*/
void
INITENTRY = EARGF(usage());
break;
case 'H':
- HEADTYPE = atolwhex(EARGF(usage()));
+ HEADTYPE = headtype(EARGF(usage()));
break;
case 'I':
interpreter = EARGF(usage());
mywhatsys(); // get goos
- if(HEADTYPE == -1) {
- HEADTYPE = 2;
- if(strcmp(goos, "linux") == 0)
- HEADTYPE = 7;
- else
- if(strcmp(goos, "darwin") == 0)
- HEADTYPE = 6;
- else
- if(strcmp(goos, "freebsd") == 0)
- HEADTYPE = 9;
- else
- if(strcmp(goos, "windows") == 0)
- HEADTYPE = 10;
- else
- if(strcmp(goos, "tiny") == 0)
- HEADTYPE = 11;
- else
- if(strcmp(goos, "plan9") == 0)
- HEADTYPE = 2;
- else
- print("goos is not known: %s\n", goos);
- }
+ if(HEADTYPE == -1)
+ HEADTYPE = headtype(goos);
if(outfile == nil) {
- if(HEADTYPE == 10)
+ if(HEADTYPE == Hwindows)
outfile = "8.out.exe";
else
outfile = "8.out";
diag("unknown -H option");
errorexit();
- case 0: /* this is garbage */
+ case Hgarbunix: /* this is garbage */
HEADR = 20L+56L;
if(INITTEXT == -1)
INITTEXT = 0x40004CL;
if(INITRND == -1)
INITRND = 0;
break;
- case 1: /* is unix coff */
+ case Hunixcoff: /* is unix coff */
HEADR = 0xd0L;
if(INITTEXT == -1)
INITTEXT = 0xd0;
if(INITRND == -1)
INITRND = 0;
break;
- case 2: /* plan 9 */
+ case Hplan9x32: /* plan 9 */
tlsoffset = -8;
HEADR = 32L;
if(INITTEXT == -1)
if(INITRND == -1)
INITRND = 1;
break;
- case 3: /* MS-DOS .COM */
+ case Hmsdoscom: /* MS-DOS .COM */
HEADR = 0;
if(INITTEXT == -1)
INITTEXT = 0x0100;
if(INITRND == -1)
INITRND = 4;
break;
- case 4: /* fake MS-DOS .EXE */
+ case Hmsdosexe: /* fake MS-DOS .EXE */
HEADR = 0x200;
if(INITTEXT == -1)
INITTEXT = 0x0100;
if(debug['v'])
Bprint(&bso, "HEADR = 0x%d\n", HEADR);
break;
- case 6: /* apple MACH */
+ case Hdarwin: /* apple MACH */
/*
* OS X system constant - offset from %gs to our TLS.
* Explained in ../../libcgo/darwin_386.c.
if(INITRND == -1)
INITRND = 4096;
break;
- case 7: /* elf32 executable */
- case 9:
+ case Hlinux: /* elf32 executable */
+ case Hfreebsd:
/*
* ELF uses TLS offsets negative from %gs.
* Translate 0(GS) and 4(GS) into -8(GS) and -4(GS).
if(INITRND == -1)
INITRND = 4096;
break;
- case 10: /* PE executable */
+ case Hwindows: /* PE executable */
peinit();
HEADR = PEFILEHEADR;
if(INITTEXT == -1)
if(INITRND == -1)
INITRND = PESECTALIGN;
break;
- case 11:
+ case Htiny:
tlsoffset = 0;
elfinit();
HEADR = ELFRESERVE;
patch();
follow();
doelf();
- if(HEADTYPE == 6)
+ if(HEADTYPE == Hdarwin)
domacho();
- if(HEADTYPE == 10)
+ if(HEADTYPE == Hwindows)
dope();
dostkoff();
if(debug['p'])
vexit = s->value;
plan9_tos = S;
- if(HEADTYPE == 2)
+ if(HEADTYPE == Hplan9x32)
plan9_tos = lookup("_tos", 0);
for(cursym = textp; cursym != nil; cursym = cursym->next) {
for(p = cursym->text; p != P; p = p->link) {
- if(HEADTYPE == 10) { // Windows
+ if(HEADTYPE == Hwindows) {
// Convert
// op n(GS), reg
// to
p->from.offset = 0x2C;
}
}
- if(HEADTYPE == 7) { // Linux
+ if(HEADTYPE == Hlinux) {
// Running binaries under Xen requires using
// MOVL 0(GS), reg
// and then off(reg) instead of saying off(GS) directly
p->from.offset = 0;
}
}
- if(HEADTYPE == 2) { // Plan 9
+ if(HEADTYPE == Hplan9x32) {
if(p->from.type == D_INDIR+D_GS
&& p->to.type >= D_AX && p->to.type <= D_DI) {
q = appendp(p);
}
plan9_tos = S;
- if(HEADTYPE == 2)
+ if(HEADTYPE == Hplan9x32)
plan9_tos = lookup("_tos", 0);
for(cursym = textp; cursym != nil; cursym = cursym->next) {
if(!(p->from.scale & NOSPLIT)) {
p = appendp(p); // load g into CX
switch(HEADTYPE) {
- case 10: // Windows
+ case Hwindows:
p->as = AMOVL;
p->from.type = D_INDIR+D_FS;
p->from.offset = 0x2c;
p->to.type = D_CX;
break;
- case 7: // Linux
+ case Hlinux:
p->as = AMOVL;
p->from.type = D_INDIR+D_GS;
p->from.offset = 0;
p->to.type = D_CX;
break;
- case 2: // Plan 9
+ case Hplan9x32:
p->as = AMOVL;
p->from.type = D_EXTERN;
p->from.sym = plan9_tos;
{
Reloc *r;
- if(thechar == '8' && HEADTYPE == 10) { // Windows PE
+ if(HEADTYPE == Hwindows) {
Sym *rel, *targ;
rel = lookup(".rel", 0);
segdata.rwx = 06;
segdata.vaddr = va;
segdata.fileoff = va - segtext.vaddr + segtext.fileoff;
- if((thechar == '6' || thechar == '8') && HEADTYPE == 10) // Windows PE
+ if(HEADTYPE == Hwindows)
segdata.fileoff = segtext.fileoff + rnd(segtext.len, PEFILEALIGN);
- if(thechar == '8' && HEADTYPE == 2) { // Plan 9
+ if(HEADTYPE == Hplan9x32) {
segdata.vaddr = va = rnd(va, 4096);
segdata.fileoff = segtext.fileoff + segtext.filelen;
}
static void
align(vlong size)
{
- if((thechar == '6' || thechar == '8') && HEADTYPE == 10) // Only Windows PE need section align.
+ if(HEADTYPE == Hwindows) // Only Windows PE need section align.
strnput("", rnd(size, PEFILEALIGN) - size);
}
if(ch->limit != limit)
print("\t%d\tafter %s uses %d\n", limit, name, ch->limit - limit);
}
+
+int
+headtype(char *name)
+{
+ int i;
+
+ for(i=0; headers[i].name; i++)
+ if(strcmp(name, headers[i].name) == 0)
+ return headers[i].val;
+ fprint(2, "unknown header type -H %s\n", name);
+ errorexit();
+ return -1; // not reached
+}
ArchiveObj,
Pkgdef
};
+
+/* executable header types */
+enum {
+ Hgarbunix = 0, // garbage unix
+ Hnoheader, // no header
+ Hunixcoff, // unix coff
+ Hrisc, // aif for risc os
+ Hplan9x32, // plan 9 32-bit format
+ Hplan9x64, // plan 9 64-bit format
+ Hmsdoscom, // MS-DOS .COM
+ Hnetbsd, // NetBSD
+ Hmsdosexe, // fake MS-DOS .EXE
+ Hixp1200, // IXP1200 (raw)
+ Helf, // ELF32
+ Hipaq, // ipaq
+ Hdarwin, // Apple Mach-O
+ Hlinux, // Linux ELF
+ Hnacl, // Google Native Client
+ Hfreebsd, // FreeBSD ELF
+ Hwindows, // MS Windows PE
+ Htiny // tiny (os image)
+};
+
+typedef struct Header Header;
+struct Header {
+ char *name;
+ int val;
+};
+
+extern Header headers[];
+int headtype(char*);