From 420fe2292139123a31dbd90420417f63d4191c76 Mon Sep 17 00:00:00 2001 From: Christopher Nielsen Date: Mon, 12 Dec 2011 15:42:11 -0500 Subject: [PATCH] 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 --- src/cmd/6l/asm.c | 14 +++++++++++++- src/cmd/6l/doc.go | 2 ++ src/cmd/6l/obj.c | 23 +++++++++++++---------- src/cmd/6l/pass.c | 4 ++-- src/cmd/8l/asm.c | 7 +++++++ src/cmd/8l/doc.go | 2 ++ src/cmd/8l/obj.c | 27 +++++++++++++++------------ src/cmd/cc/godefs.c | 6 +++--- src/cmd/ld/lib.c | 2 +- 9 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/cmd/6l/asm.c b/src/cmd/6l/asm.c index 8c6bab8399..c3b2ba42f6 100644 --- a/src/cmd/6l/asm.c +++ b/src/cmd/6l/asm.c @@ -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; diff --git a/src/cmd/6l/doc.go b/src/cmd/6l/doc.go index b17642abbd..c18b0f29d5 100644 --- a/src/cmd/6l/doc.go +++ b/src/cmd/6l/doc.go @@ -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 diff --git a/src/cmd/6l/obj.c b/src/cmd/6l/obj.c index 3ef8027fa9..de96468445 100644 --- a/src/cmd/6l/obj.c +++ b/src/cmd/6l/obj.c @@ -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. diff --git a/src/cmd/6l/pass.c b/src/cmd/6l/pass.c index d9e0b2fc1b..9f450d8fa4 100644 --- a/src/cmd/6l/pass.c +++ b/src/cmd/6l/pass.c @@ -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; diff --git a/src/cmd/8l/asm.c b/src/cmd/8l/asm.c index 948d4889ca..3612c2bf2a 100644 --- a/src/cmd/8l/asm.c +++ b/src/cmd/8l/asm.c @@ -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; diff --git a/src/cmd/8l/doc.go b/src/cmd/8l/doc.go index de877bb243..edd683823f 100644 --- a/src/cmd/8l/doc.go +++ b/src/cmd/8l/doc.go @@ -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 diff --git a/src/cmd/8l/obj.c b/src/cmd/8l/obj.c index c2f409e91d..e17b667681 100644 --- a/src/cmd/8l/obj.c +++ b/src/cmd/8l/obj.c @@ -47,18 +47,19 @@ char *noname = ""; 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. diff --git a/src/cmd/cc/godefs.c b/src/cmd/cc/godefs.c index 3ba979c8a3..4274c56265 100644 --- a/src/cmd/cc/godefs.c +++ b/src/cmd/cc/godefs.c @@ -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; } diff --git a/src/cmd/ld/lib.c b/src/cmd/ld/lib.c index 7797d42ace..34440b8758 100644 --- a/src/cmd/ld/lib.c +++ b/src/cmd/ld/lib.c @@ -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