Commit 420fe229 authored by Christopher Nielsen's avatar Christopher Nielsen Committed by Russ Cox

ld/6l/8l: First pass at changes to the linker to support NetBSD binaries.

This will not currently create valid NetBSD binaries because NetBSD requires
an ELF note section to run, otherwise the kernel will throw ENOEXEC. I was
unable to determine an elegant way to add the section, so I am submitting
what I have.

References:
http://www.netbsd.org/docs/kernel/elf-notes.html
http://mail-index.netbsd.org/netbsd-bugs/2001/08/03/0012.html

R=rsc
CC=golang-dev
https://golang.org/cl/5472049
parent 728c16cf
......@@ -44,6 +44,7 @@
char linuxdynld[] = "/lib64/ld-linux-x86-64.so.2";
char freebsddynld[] = "/libexec/ld-elf.so.1";
char openbsddynld[] = "/usr/libexec/ld.so";
char netbsddynld[] = "/libexec/ld.elf_so";
char zeroes[32];
......@@ -95,6 +96,7 @@ enum {
ElfStrPlt,
ElfStrGnuVersion,
ElfStrGnuVersionR,
ElfStrNoteNetbsdIdent,
NElfStr
};
......@@ -558,7 +560,7 @@ doelf(void)
{
Sym *s, *shstrtab, *dynstr;
if(HEADTYPE != Hlinux && HEADTYPE != Hfreebsd && HEADTYPE != Hopenbsd)
if(HEADTYPE != Hlinux && HEADTYPE != Hfreebsd && HEADTYPE != Hopenbsd && HEADTYPE != Hnetbsd)
return;
/* predefine strings we need for section headers */
......@@ -570,6 +572,8 @@ doelf(void)
elfstr[ElfStrText] = addstring(shstrtab, ".text");
elfstr[ElfStrData] = addstring(shstrtab, ".data");
elfstr[ElfStrBss] = addstring(shstrtab, ".bss");
if(HEADTYPE == Hnetbsd)
elfstr[ElfStrNoteNetbsdIdent] = addstring(shstrtab, ".note.netbsd.ident");
addstring(shstrtab, ".elfdata");
addstring(shstrtab, ".rodata");
addstring(shstrtab, ".gosymtab");
......@@ -763,6 +767,7 @@ asmb(void)
break;
case Hlinux:
case Hfreebsd:
case Hnetbsd:
case Hopenbsd:
debug['8'] = 1; /* 64-bit addresses */
/* index of elf text section; needed by asmelfsym, double-checked below */
......@@ -798,6 +803,7 @@ asmb(void)
break;
case Hlinux:
case Hfreebsd:
case Hnetbsd:
case Hopenbsd:
symo = rnd(HEADR+segtext.len, INITRND)+segdata.filelen;
symo = rnd(symo, INITRND);
......@@ -867,6 +873,7 @@ asmb(void)
break;
case Hlinux:
case Hfreebsd:
case Hnetbsd:
case Hopenbsd:
/* elf amd-64 */
......@@ -910,6 +917,9 @@ asmb(void)
case Hfreebsd:
interpreter = freebsddynld;
break;
case Hnetbsd:
interpreter = netbsddynld;
break;
case Hopenbsd:
interpreter = openbsddynld;
break;
......@@ -1076,6 +1086,8 @@ asmb(void)
eh->ident[EI_MAG3] = 'F';
if(HEADTYPE == Hfreebsd)
eh->ident[EI_OSABI] = ELFOSABI_FREEBSD;
else if(HEADTYPE == Hnetbsd)
eh->ident[EI_OSABI] = ELFOSABI_NETBSD;
else if(HEADTYPE == Hopenbsd)
eh->ident[EI_OSABI] = ELFOSABI_OPENBSD;
eh->ident[EI_CLASS] = ELFCLASS64;
......
......@@ -31,6 +31,8 @@ Options new in this version:
Write Linux ELF binaries (default when $GOOS is linux)
-Hfreebsd
Write FreeBSD ELF binaries (default when $GOOS is freebsd)
-Hnetbsd
Write NetBSD ELF binaries (default when $GOOS is netbsd)
-Hopenbsd
Write OpenBSD ELF binaries (default when $GOOS is openbsd)
-Hwindows
......
......@@ -44,16 +44,17 @@ char* thestring = "amd64";
char* paramspace = "FP";
Header headers[] = {
"plan9x32", Hplan9x32,
"plan9", Hplan9x64,
"elf", Helf,
"darwin", Hdarwin,
"linux", Hlinux,
"freebsd", Hfreebsd,
"openbsd", Hopenbsd,
"windows", Hwindows,
"windowsgui", Hwindows,
0, 0
"plan9x32", Hplan9x32,
"plan9", Hplan9x64,
"elf", Helf,
"darwin", Hdarwin,
"linux", Hlinux,
"freebsd", Hfreebsd,
"netbsd", Hnetbsd,
"openbsd", Hopenbsd,
"windows", Hwindows,
"windowsgui", Hwindows,
0, 0
};
/*
......@@ -63,6 +64,7 @@ Header headers[] = {
* -Hdarwin -Tx -Rx is apple MH-exec
* -Hlinux -Tx -Rx is linux elf-exec
* -Hfreebsd -Tx -Rx is FreeBSD elf-exec
* -Hnetbsd -Tx -Rx is NetBSD elf-exec
* -Hopenbsd -Tx -Rx is OpenBSD elf-exec
* -Hwindows -Tx -Rx is MS Windows PE32+
*
......@@ -197,6 +199,7 @@ main(int argc, char *argv[])
break;
case Hlinux: /* elf64 executable */
case Hfreebsd: /* freebsd */
case Hnetbsd: /* netbsd */
case Hopenbsd: /* openbsd */
/*
* ELF uses TLS offset negative from FS.
......
......@@ -295,7 +295,7 @@ patch(void)
}
}
if(HEADTYPE == Hlinux || HEADTYPE == Hfreebsd
|| HEADTYPE == Hopenbsd) {
|| HEADTYPE == Hopenbsd || HEADTYPE == Hnetbsd) {
// ELF uses FS instead of GS.
if(p->from.type == D_INDIR+D_GS)
p->from.type = D_INDIR+D_FS;
......@@ -421,7 +421,7 @@ dostkoff(void)
p = appendp(p); // load g into CX
p->as = AMOVQ;
if(HEADTYPE == Hlinux || HEADTYPE == Hfreebsd
|| HEADTYPE == Hopenbsd) // ELF uses FS
|| HEADTYPE == Hopenbsd || HEADTYPE == Hnetbsd) // ELF uses FS
p->from.type = D_INDIR+D_FS;
else
p->from.type = D_INDIR+D_GS;
......
......@@ -42,6 +42,7 @@
char linuxdynld[] = "/lib/ld-linux.so.2";
char freebsddynld[] = "/usr/libexec/ld-elf.so.1";
char openbsddynld[] = "/usr/libexec/ld.so";
char netbsddynld[] = "/usr/libexec/ld.elf_so";
int32
entryvalue(void)
......@@ -969,6 +970,9 @@ asmb(void)
case Hfreebsd:
interpreter = freebsddynld;
break;
case Hnetbsd:
interpreter = netbsddynld;
break;
case Hopenbsd:
interpreter = openbsddynld;
break;
......@@ -1140,6 +1144,9 @@ asmb(void)
case Hfreebsd:
eh->ident[EI_OSABI] = ELFOSABI_FREEBSD;
break;
case Hnetbsd:
eh->ident[EI_OSABI] = ELFOSABI_NETBSD;
break;
case Hopenbsd:
eh->ident[EI_OSABI] = ELFOSABI_OPENBSD;
break;
......
......@@ -33,6 +33,8 @@ Options new in this version:
Write Linux ELF binaries (default when $GOOS is linux)
-Hfreebsd
Write FreeBSD ELF binaries (default when $GOOS is freebsd)
-Hnetbsd
Write NetBSD ELF binaries (default when $GOOS is netbsd)
-Hopenbsd
Write OpenBSD ELF binaries (default when $GOOS is openbsd)
-Hwindows
......
......@@ -47,18 +47,19 @@ char *noname = "<none>";
char *thestring = "386";
Header headers[] = {
"garbunix", Hgarbunix,
"unixcoff", Hunixcoff,
"plan9", Hplan9x32,
"msdoscom", Hmsdoscom,
"msdosexe", Hmsdosexe,
"darwin", Hdarwin,
"linux", Hlinux,
"freebsd", Hfreebsd,
"openbsd", Hopenbsd,
"windows", Hwindows,
"windowsgui", Hwindows,
0, 0
"garbunix", Hgarbunix,
"unixcoff", Hunixcoff,
"plan9", Hplan9x32,
"msdoscom", Hmsdoscom,
"msdosexe", Hmsdosexe,
"darwin", Hdarwin,
"linux", Hlinux,
"freebsd", Hfreebsd,
"netbsd", Hnetbsd,
"openbsd", Hopenbsd,
"windows", Hwindows,
"windowsgui", Hwindows,
0, 0
};
/*
......@@ -70,6 +71,7 @@ Header headers[] = {
* -Hdarwin -Tx -Rx is Apple Mach-O
* -Hlinux -Tx -Rx is Linux ELF32
* -Hfreebsd -Tx -Rx is FreeBSD ELF32
* -Hnetbsd -Tx -Rx is NetBSD ELF32
* -Hopenbsd -Tx -Rx is OpenBSD ELF32
* -Hwindows -Tx -Rx is MS Windows PE32
*/
......@@ -225,6 +227,7 @@ main(int argc, char *argv[])
break;
case Hlinux: /* elf32 executable */
case Hfreebsd:
case Hnetbsd:
case Hopenbsd:
/*
* ELF uses TLS offsets negative from %gs.
......
......@@ -124,11 +124,11 @@ Uconv(Fmt *fp)
if(s && *s) {
if(upper)
str[0] = toupper(*s);
str[0] = toupper((uchar)*s);
else
str[0] = tolower(*s);
str[0] = tolower((uchar)*s);
for(i = 1; i < STRINGSZ && s[i] != 0; i++)
str[i] = tolower(s[i]);
str[i] = tolower((uchar)s[i]);
str[i] = 0;
}
......
......@@ -115,7 +115,7 @@ addlib(char *src, char *obj)
sprint(name, "");
i = 1;
} else
if(isalpha(histfrog[0]->name[1]) && histfrog[0]->name[2] == ':') {
if(isalpha((uchar)histfrog[0]->name[1]) && histfrog[0]->name[2] == ':') {
strcpy(name, histfrog[0]->name+1);
i = 1;
} else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment