Commit 80dbe743 authored by Russ Cox's avatar Russ Cox

cmd/gc, cmd/ld: use go.weak instead of weak as the weak symbol prefix

Also defend our symbol prefixes (now just "go" and "type")
from use as import paths.

Fixes #4257.

R=ken2
CC=golang-dev
https://golang.org/cl/6744072
parent 7c44edf4
......@@ -393,6 +393,8 @@ importimport(Sym *s, Strlit *z)
// human-readable messages.
Pkg *p;
if(isbadimport(z))
errorexit();
p = mkpkg(z);
if(p->name == nil) {
p->name = s->name;
......
......@@ -1030,10 +1030,16 @@ sym:
hidden_importsym:
'@' LLITERAL '.' LNAME
{
Pkg *p;
if($2.u.sval->len == 0)
$$ = pkglookup($4->name, importpkg);
else
$$ = pkglookup($4->name, mkpkg($2.u.sval));
p = importpkg;
else {
if(isbadimport($2.u.sval))
errorexit();
p = mkpkg($2.u.sval);
}
$$ = pkglookup($4->name, p);
}
name:
......
......@@ -216,9 +216,9 @@ main(int argc, char *argv[])
typepkg = mkpkg(strlit("type"));
typepkg->name = "type";
weaktypepkg = mkpkg(strlit("weak.type"));
weaktypepkg->name = "weak.type";
weaktypepkg->prefix = "weak.type"; // not weak%2etype
weaktypepkg = mkpkg(strlit("go.weak.type"));
weaktypepkg->name = "go.weak.type";
weaktypepkg->prefix = "go.weak.type"; // not go%2eweak%2etype
unsafepkg = mkpkg(strlit("unsafe"));
unsafepkg->name = "unsafe";
......@@ -664,6 +664,11 @@ importfile(Val *f, int line)
strcat(cleanbuf, path->s);
cleanname(cleanbuf);
path = strlit(cleanbuf);
if(isbadimport(path)) {
fakeimport();
return;
}
}
if(!findpkg(path)) {
......
......@@ -3572,9 +3572,6 @@ mkpkg(Strlit *path)
Pkg *p;
int h;
if(isbadimport(path))
errorexit();
h = stringhash(path->s) & (nelem(phash)-1);
for(p=phash[h]; p; p=p->link)
if(p->path->len == path->len && memcmp(path->s, p->path->s, path->len) == 0)
......@@ -3623,9 +3620,15 @@ addinit(Node **np, NodeList *init)
n->ullman = UINF;
}
static char* reservedimports[] = {
"go",
"type",
};
int
isbadimport(Strlit *path)
{
int i;
char *s;
Rune r;
......@@ -3634,6 +3637,13 @@ isbadimport(Strlit *path)
return 1;
}
for(i=0; i<nelem(reservedimports); i++) {
if(strcmp(path->s, reservedimports[i]) == 0) {
yyerror("import path \"%s\" is reserved and cannot be used", path->s);
return 1;
}
}
s = path->s;
while(*s) {
s += chartorune(&r, s);
......
This diff is collapsed.
/* A Bison parser, made by GNU Bison 2.6.2. */
/* A Bison parser, made by GNU Bison 2.3. */
/* Bison interface for Yacc-like parsers in C
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
......@@ -15,7 +16,9 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
......@@ -30,16 +33,6 @@
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef YY_Y_TAB_H
# define YY_Y_TAB_H
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
......@@ -150,42 +143,25 @@ extern int yydebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 2049 of yacc.c */
#line 28 "go.y"
{
Node* node;
NodeList* list;
Type* type;
Sym* sym;
struct Val val;
int i;
/* Line 2049 of yacc.c */
#line 169 "y.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
}
/* Line 1529 of yacc.c. */
#line 160 "y.tab.h"
YYSTYPE;
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif
extern YYSTYPE yylval;
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (void);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_Y_TAB_H */
......@@ -643,7 +643,7 @@ mark(Sym *s)
if(s == S || s->reachable)
return;
if(strncmp(s->name, "weak.", 5) == 0)
if(strncmp(s->name, "go.weak.", 8) == 0)
return;
s->reachable = 1;
if(s->text)
......@@ -751,7 +751,7 @@ deadcode(void)
last->next = nil;
for(s = allsym; s != S; s = s->allsym)
if(strncmp(s->name, "weak.", 5) == 0) {
if(strncmp(s->name, "go.weak.", 8) == 0) {
s->special = 1; // do not lay out in data segment
s->reachable = 1;
s->hide = 1;
......@@ -766,8 +766,8 @@ doweak(void)
// resolve weak references only if
// target symbol will be in binary anyway.
for(s = allsym; s != S; s = s->allsym) {
if(strncmp(s->name, "weak.", 5) == 0) {
t = rlookup(s->name+5, s->version);
if(strncmp(s->name, "go.weak.", 8) == 0) {
t = rlookup(s->name+8, s->version);
if(t && t->type != 0 && t->reachable) {
s->value = t->value;
s->type = t->type;
......
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