Unexport even more symbols (#13214)
* system/gc: don't export markStackAndRegisters * compiler/cgen: unexport internal symbols As these functions are Nim-specific walkaround against C's optimization schemes, they don't serve any purpose being exported. * compiler/cgen: don't export global var unless marked * compiler/ccgthreadvars: don't export threadvar unless marked * tests/dll/visibility: also check for exports This ensure that these changes don't break manual exports. * compiler/cgen: hide all variables created for constants * compiler/ccgtypes: don't export RTTI variables * compiler/ccgexprs: make all complex const static * nimbase.h: fix export for windows * compiler/cgen, ccgthreadvars: export variables correctly For C/C++ variables, `extern` means that the variable is defined in an another unit. Added a new N_LIB_EXPORT_VAR to correctly export variables.
This commit is contained in:
parent
3a5056dc70
commit
f500895efe
7 changed files with 52 additions and 22 deletions
|
|
@ -1325,7 +1325,7 @@ proc rawConstExpr(p: BProc, n: PNode; d: var TLoc) =
|
||||||
if id == p.module.labels:
|
if id == p.module.labels:
|
||||||
# expression not found in the cache:
|
# expression not found in the cache:
|
||||||
inc(p.module.labels)
|
inc(p.module.labels)
|
||||||
p.module.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n",
|
p.module.s[cfsData].addf("static NIM_CONST $1 $2 = $3;$n",
|
||||||
[getTypeDesc(p.module, t), d.r, genBracedInit(p, n, isConst = true)])
|
[getTypeDesc(p.module, t), d.r, genBracedInit(p, n, isConst = true)])
|
||||||
|
|
||||||
proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
|
proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
|
||||||
|
|
@ -2508,7 +2508,7 @@ proc exprComplexConst(p: BProc, n: PNode, d: var TLoc) =
|
||||||
if id == p.module.labels:
|
if id == p.module.labels:
|
||||||
# expression not found in the cache:
|
# expression not found in the cache:
|
||||||
inc(p.module.labels)
|
inc(p.module.labels)
|
||||||
p.module.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n",
|
p.module.s[cfsData].addf("static NIM_CONST $1 $2 = $3;$n",
|
||||||
[getTypeDesc(p.module, t), tmp, genBracedInit(p, n, isConst = true)])
|
[getTypeDesc(p.module, t), tmp, genBracedInit(p, n, isConst = true)])
|
||||||
|
|
||||||
if d.k == locNone:
|
if d.k == locNone:
|
||||||
|
|
@ -2896,7 +2896,7 @@ proc genConstSeq(p: BProc, n: PNode, t: PType; isConst: bool): Rope =
|
||||||
let base = t.skipTypes(abstractInst)[0]
|
let base = t.skipTypes(abstractInst)[0]
|
||||||
|
|
||||||
appcg(p.module, cfsData,
|
appcg(p.module, cfsData,
|
||||||
"$5 struct {$n" &
|
"static $5 struct {$n" &
|
||||||
" #TGenericSeq Sup;$n" &
|
" #TGenericSeq Sup;$n" &
|
||||||
" $1 data[$2];$n" &
|
" $1 data[$2];$n" &
|
||||||
"} $3 = $4;$n", [
|
"} $3 = $4;$n", [
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
|
||||||
m.g.nimtv.addf("$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r])
|
m.g.nimtv.addf("$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r])
|
||||||
else:
|
else:
|
||||||
if isExtern: m.s[cfsVars].add("extern ")
|
if isExtern: m.s[cfsVars].add("extern ")
|
||||||
|
elif lfExportLib in s.loc.flags: m.s[cfsVars].add("N_LIB_EXPORT_VAR ")
|
||||||
|
else: m.s[cfsVars].add("N_LIB_PRIVATE ")
|
||||||
if optThreads in m.config.globalOptions: m.s[cfsVars].add("NIM_THREADVAR ")
|
if optThreads in m.config.globalOptions: m.s[cfsVars].add("NIM_THREADVAR ")
|
||||||
m.s[cfsVars].add(getTypeDesc(m, s.loc.t))
|
m.s[cfsVars].add(getTypeDesc(m, s.loc.t))
|
||||||
m.s[cfsVars].addf(" $1;$n", [s.loc.r])
|
m.s[cfsVars].addf(" $1;$n", [s.loc.r])
|
||||||
|
|
|
||||||
|
|
@ -1026,7 +1026,7 @@ proc genTypeInfoAuxBase(m: BModule; typ, origType: PType;
|
||||||
m.hcrCreateTypeInfosProc.addf("\thcrRegisterGlobal($2, \"$1\", sizeof(TNimType), NULL, (void**)&$1);$n",
|
m.hcrCreateTypeInfosProc.addf("\thcrRegisterGlobal($2, \"$1\", sizeof(TNimType), NULL, (void**)&$1);$n",
|
||||||
[name, getModuleDllPath(m, m.module)])
|
[name, getModuleDllPath(m, m.module)])
|
||||||
else:
|
else:
|
||||||
m.s[cfsData].addf("TNimType $1;$n", [name])
|
m.s[cfsData].addf("N_LIB_PRIVATE TNimType $1;$n", [name])
|
||||||
|
|
||||||
proc genTypeInfoAux(m: BModule, typ, origType: PType, name: Rope;
|
proc genTypeInfoAux(m: BModule, typ, origType: PType, name: Rope;
|
||||||
info: TLineInfo) =
|
info: TLineInfo) =
|
||||||
|
|
@ -1301,7 +1301,7 @@ proc genTypeInfoV2(m: BModule, t, origType: PType, name: Rope; info: TLineInfo)
|
||||||
else:
|
else:
|
||||||
typeName = rope("NIM_NIL")
|
typeName = rope("NIM_NIL")
|
||||||
|
|
||||||
m.s[cfsData].addf("TNimType $1;$n", [name])
|
m.s[cfsData].addf("N_LIB_PRIVATE TNimType $1;$n", [name])
|
||||||
let destroyImpl = genHook(m, t, info, attachedDestructor)
|
let destroyImpl = genHook(m, t, info, attachedDestructor)
|
||||||
let traceImpl = genHook(m, t, info, attachedTrace)
|
let traceImpl = genHook(m, t, info, attachedTrace)
|
||||||
let disposeImpl = genHook(m, t, info, attachedDispose)
|
let disposeImpl = genHook(m, t, info, attachedDispose)
|
||||||
|
|
|
||||||
|
|
@ -549,6 +549,8 @@ proc assignGlobalVar(p: BProc, n: PNode; value: Rope) =
|
||||||
decl.addf "NIM_ALIGN($1) ", [rope(s.alignment)]
|
decl.addf "NIM_ALIGN($1) ", [rope(s.alignment)]
|
||||||
if p.hcrOn: decl.add("static ")
|
if p.hcrOn: decl.add("static ")
|
||||||
elif sfImportc in s.flags: decl.add("extern ")
|
elif sfImportc in s.flags: decl.add("extern ")
|
||||||
|
elif lfExportLib in s.loc.flags: decl.add("N_LIB_EXPORT_VAR ")
|
||||||
|
else: decl.add("N_LIB_PRIVATE ")
|
||||||
if s.kind == skLet and value != nil: decl.add("NIM_CONST ")
|
if s.kind == skLet and value != nil: decl.add("NIM_CONST ")
|
||||||
decl.add(td)
|
decl.add(td)
|
||||||
if p.hcrOn: decl.add("*")
|
if p.hcrOn: decl.add("*")
|
||||||
|
|
@ -1175,7 +1177,7 @@ proc requestConstImpl(p: BProc, sym: PSym) =
|
||||||
var q = findPendingModule(m, sym)
|
var q = findPendingModule(m, sym)
|
||||||
if q != nil and not containsOrIncl(q.declaredThings, sym.id):
|
if q != nil and not containsOrIncl(q.declaredThings, sym.id):
|
||||||
assert q.initProc.module == q
|
assert q.initProc.module == q
|
||||||
q.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n",
|
q.s[cfsData].addf("N_LIB_PRIVATE NIM_CONST $1 $2 = $3;$n",
|
||||||
[getTypeDesc(q, sym.typ), sym.loc.r, genBracedInit(q.initProc, sym.ast, isConst = true)])
|
[getTypeDesc(q, sym.typ), sym.loc.r, genBracedInit(q.initProc, sym.ast, isConst = true)])
|
||||||
# declare header:
|
# declare header:
|
||||||
if q != m and not containsOrIncl(m.declaredThings, sym.id):
|
if q != m and not containsOrIncl(m.declaredThings, sym.id):
|
||||||
|
|
@ -1316,19 +1318,19 @@ proc genMainProc(m: BModule) =
|
||||||
const
|
const
|
||||||
# not a big deal if we always compile these 3 global vars... makes the HCR code easier
|
# not a big deal if we always compile these 3 global vars... makes the HCR code easier
|
||||||
PosixCmdLine =
|
PosixCmdLine =
|
||||||
"int cmdCount;$N" &
|
"N_LIB_PRIVATE int cmdCount;$N" &
|
||||||
"char** cmdLine;$N" &
|
"N_LIB_PRIVATE char** cmdLine;$N" &
|
||||||
"char** gEnv;$N"
|
"N_LIB_PRIVATE char** gEnv;$N"
|
||||||
|
|
||||||
# The use of a volatile function pointer to call Pre/NimMainInner
|
# The use of a volatile function pointer to call Pre/NimMainInner
|
||||||
# prevents inlining of the NimMainInner function and dependent
|
# prevents inlining of the NimMainInner function and dependent
|
||||||
# functions, which might otherwise merge their stack frames.
|
# functions, which might otherwise merge their stack frames.
|
||||||
PreMainBody = "$N" &
|
PreMainBody = "$N" &
|
||||||
"void PreMainInner(void) {$N" &
|
"N_LIB_PRIVATE void PreMainInner(void) {$N" &
|
||||||
"$2" &
|
"$2" &
|
||||||
"}$N$N" &
|
"}$N$N" &
|
||||||
PosixCmdLine &
|
PosixCmdLine &
|
||||||
"void PreMain(void) {$N" &
|
"N_LIB_PRIVATE void PreMain(void) {$N" &
|
||||||
"\tvoid (*volatile inner)(void);$N" &
|
"\tvoid (*volatile inner)(void);$N" &
|
||||||
"\tinner = PreMainInner;$N" &
|
"\tinner = PreMainInner;$N" &
|
||||||
"$1" &
|
"$1" &
|
||||||
|
|
@ -1341,7 +1343,7 @@ proc genMainProc(m: BModule) =
|
||||||
MainProcsWithResult =
|
MainProcsWithResult =
|
||||||
MainProcs & ("\treturn $1nim_program_result;$N")
|
MainProcs & ("\treturn $1nim_program_result;$N")
|
||||||
|
|
||||||
NimMainInner = "N_CDECL(void, NimMainInner)(void) {$N" &
|
NimMainInner = "N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {$N" &
|
||||||
"$1" &
|
"$1" &
|
||||||
"}$N$N"
|
"}$N$N"
|
||||||
|
|
||||||
|
|
@ -1388,7 +1390,7 @@ proc genMainProc(m: BModule) =
|
||||||
PosixNimDllMain = WinNimDllMain
|
PosixNimDllMain = WinNimDllMain
|
||||||
|
|
||||||
PosixCDllMain =
|
PosixCDllMain =
|
||||||
"void NIM_POSIX_INIT NimMainInit(void) {$N" &
|
"N_LIB_PRIVATE void NIM_POSIX_INIT NimMainInit(void) {$N" &
|
||||||
MainProcs &
|
MainProcs &
|
||||||
"}$N$N"
|
"}$N$N"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -182,10 +182,11 @@ __AVR__
|
||||||
# define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)
|
# define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)
|
||||||
|
|
||||||
# ifdef __cplusplus
|
# ifdef __cplusplus
|
||||||
# define N_LIB_EXPORT extern "C" __declspec(dllexport)
|
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
|
||||||
# else
|
# else
|
||||||
# define N_LIB_EXPORT extern __declspec(dllexport)
|
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
|
||||||
# endif
|
# endif
|
||||||
|
# define N_LIB_EXPORT_VAR __declspec(dllexport)
|
||||||
# define N_LIB_IMPORT extern __declspec(dllimport)
|
# define N_LIB_IMPORT extern __declspec(dllimport)
|
||||||
#else
|
#else
|
||||||
# define N_LIB_PRIVATE __attribute__((visibility("hidden")))
|
# define N_LIB_PRIVATE __attribute__((visibility("hidden")))
|
||||||
|
|
@ -215,6 +216,7 @@ __AVR__
|
||||||
# define N_SAFECALL_PTR(rettype, name) rettype (*name)
|
# define N_SAFECALL_PTR(rettype, name) rettype (*name)
|
||||||
# endif
|
# endif
|
||||||
# define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
|
# define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
|
||||||
|
# define N_LIB_EXPORT_VAR __attribute__((visibility("default")))
|
||||||
# define N_LIB_IMPORT extern
|
# define N_LIB_IMPORT extern
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -682,7 +682,7 @@ proc gcMark(gch: var GcHeap, p: pointer) {.inline.} =
|
||||||
`CLANG_NO_SANITIZE_ADDRESS` in `lib/nimbase.h`.
|
`CLANG_NO_SANITIZE_ADDRESS` in `lib/nimbase.h`.
|
||||||
]#
|
]#
|
||||||
proc markStackAndRegisters(gch: var GcHeap) {.noinline, cdecl,
|
proc markStackAndRegisters(gch: var GcHeap) {.noinline, cdecl,
|
||||||
codegenDecl: "CLANG_NO_SANITIZE_ADDRESS $# $#$#".} =
|
codegenDecl: "CLANG_NO_SANITIZE_ADDRESS N_LIB_PRIVATE $# $#$#".} =
|
||||||
forEachStackSlot(gch, gcMark)
|
forEachStackSlot(gch, gcMark)
|
||||||
|
|
||||||
proc collectZCT(gch: var GcHeap): bool =
|
proc collectZCT(gch: var GcHeap): bool =
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
discard """
|
discard """
|
||||||
output: "could not import: foo"
|
output: ""
|
||||||
exitcode: 1
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
const LibName {.used.} =
|
const LibName {.used.} =
|
||||||
|
|
@ -12,8 +11,33 @@ const LibName {.used.} =
|
||||||
"libvisibility.so"
|
"libvisibility.so"
|
||||||
|
|
||||||
when compileOption("app", "lib"):
|
when compileOption("app", "lib"):
|
||||||
proc foo() {.exportc.} =
|
var
|
||||||
echo "failed"
|
bar {.exportc.}: int
|
||||||
|
thr {.exportc, threadvar.}: int
|
||||||
|
proc foo() {.exportc.} = discard
|
||||||
|
|
||||||
|
var
|
||||||
|
exported {.exportc, dynlib.}: int
|
||||||
|
exported_thr {.exportc, threadvar, dynlib.}: int
|
||||||
|
proc exported_func() {.exportc, dynlib.} = discard
|
||||||
elif isMainModule:
|
elif isMainModule:
|
||||||
proc foo() {.importc, dynlib: LibName.}
|
import dynlib
|
||||||
foo()
|
|
||||||
|
let handle = loadLib(LibName)
|
||||||
|
|
||||||
|
template check(sym: untyped) =
|
||||||
|
const s = astToStr(sym)
|
||||||
|
if handle.symAddr(s) != nil:
|
||||||
|
echo s, " is exported"
|
||||||
|
template checkE(sym: untyped) =
|
||||||
|
const s = astToStr(sym)
|
||||||
|
if handle.symAddr(s) == nil:
|
||||||
|
echo s, " is not exported"
|
||||||
|
|
||||||
|
check foo
|
||||||
|
check bar
|
||||||
|
check thr
|
||||||
|
|
||||||
|
checkE exported
|
||||||
|
checkE exported_thr
|
||||||
|
checkE exported_func
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue