Skip to content

Commit f500895

Browse files
alavissAraq
authored andcommitted
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.
1 parent 3a5056d commit f500895

File tree

7 files changed

+52
-22
lines changed

7 files changed

+52
-22
lines changed

compiler/ccgexprs.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ proc rawConstExpr(p: BProc, n: PNode; d: var TLoc) =
13251325
if id == p.module.labels:
13261326
# expression not found in the cache:
13271327
inc(p.module.labels)
1328-
p.module.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n",
1328+
p.module.s[cfsData].addf("static NIM_CONST $1 $2 = $3;$n",
13291329
[getTypeDesc(p.module, t), d.r, genBracedInit(p, n, isConst = true)])
13301330

13311331
proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
@@ -2508,7 +2508,7 @@ proc exprComplexConst(p: BProc, n: PNode, d: var TLoc) =
25082508
if id == p.module.labels:
25092509
# expression not found in the cache:
25102510
inc(p.module.labels)
2511-
p.module.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n",
2511+
p.module.s[cfsData].addf("static NIM_CONST $1 $2 = $3;$n",
25122512
[getTypeDesc(p.module, t), tmp, genBracedInit(p, n, isConst = true)])
25132513

25142514
if d.k == locNone:
@@ -2896,7 +2896,7 @@ proc genConstSeq(p: BProc, n: PNode, t: PType; isConst: bool): Rope =
28962896
let base = t.skipTypes(abstractInst)[0]
28972897

28982898
appcg(p.module, cfsData,
2899-
"$5 struct {$n" &
2899+
"static $5 struct {$n" &
29002900
" #TGenericSeq Sup;$n" &
29012901
" $1 data[$2];$n" &
29022902
"} $3 = $4;$n", [

compiler/ccgthreadvars.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
3333
m.g.nimtv.addf("$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r])
3434
else:
3535
if isExtern: m.s[cfsVars].add("extern ")
36+
elif lfExportLib in s.loc.flags: m.s[cfsVars].add("N_LIB_EXPORT_VAR ")
37+
else: m.s[cfsVars].add("N_LIB_PRIVATE ")
3638
if optThreads in m.config.globalOptions: m.s[cfsVars].add("NIM_THREADVAR ")
3739
m.s[cfsVars].add(getTypeDesc(m, s.loc.t))
3840
m.s[cfsVars].addf(" $1;$n", [s.loc.r])

compiler/ccgtypes.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ proc genTypeInfoAuxBase(m: BModule; typ, origType: PType;
10261026
m.hcrCreateTypeInfosProc.addf("\thcrRegisterGlobal($2, \"$1\", sizeof(TNimType), NULL, (void**)&$1);$n",
10271027
[name, getModuleDllPath(m, m.module)])
10281028
else:
1029-
m.s[cfsData].addf("TNimType $1;$n", [name])
1029+
m.s[cfsData].addf("N_LIB_PRIVATE TNimType $1;$n", [name])
10301030

10311031
proc genTypeInfoAux(m: BModule, typ, origType: PType, name: Rope;
10321032
info: TLineInfo) =
@@ -1301,7 +1301,7 @@ proc genTypeInfoV2(m: BModule, t, origType: PType, name: Rope; info: TLineInfo)
13011301
else:
13021302
typeName = rope("NIM_NIL")
13031303

1304-
m.s[cfsData].addf("TNimType $1;$n", [name])
1304+
m.s[cfsData].addf("N_LIB_PRIVATE TNimType $1;$n", [name])
13051305
let destroyImpl = genHook(m, t, info, attachedDestructor)
13061306
let traceImpl = genHook(m, t, info, attachedTrace)
13071307
let disposeImpl = genHook(m, t, info, attachedDispose)

compiler/cgen.nim

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ proc assignGlobalVar(p: BProc, n: PNode; value: Rope) =
549549
decl.addf "NIM_ALIGN($1) ", [rope(s.alignment)]
550550
if p.hcrOn: decl.add("static ")
551551
elif sfImportc in s.flags: decl.add("extern ")
552+
elif lfExportLib in s.loc.flags: decl.add("N_LIB_EXPORT_VAR ")
553+
else: decl.add("N_LIB_PRIVATE ")
552554
if s.kind == skLet and value != nil: decl.add("NIM_CONST ")
553555
decl.add(td)
554556
if p.hcrOn: decl.add("*")
@@ -1175,7 +1177,7 @@ proc requestConstImpl(p: BProc, sym: PSym) =
11751177
var q = findPendingModule(m, sym)
11761178
if q != nil and not containsOrIncl(q.declaredThings, sym.id):
11771179
assert q.initProc.module == q
1178-
q.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n",
1180+
q.s[cfsData].addf("N_LIB_PRIVATE NIM_CONST $1 $2 = $3;$n",
11791181
[getTypeDesc(q, sym.typ), sym.loc.r, genBracedInit(q.initProc, sym.ast, isConst = true)])
11801182
# declare header:
11811183
if q != m and not containsOrIncl(m.declaredThings, sym.id):
@@ -1316,19 +1318,19 @@ proc genMainProc(m: BModule) =
13161318
const
13171319
# not a big deal if we always compile these 3 global vars... makes the HCR code easier
13181320
PosixCmdLine =
1319-
"int cmdCount;$N" &
1320-
"char** cmdLine;$N" &
1321-
"char** gEnv;$N"
1321+
"N_LIB_PRIVATE int cmdCount;$N" &
1322+
"N_LIB_PRIVATE char** cmdLine;$N" &
1323+
"N_LIB_PRIVATE char** gEnv;$N"
13221324

13231325
# The use of a volatile function pointer to call Pre/NimMainInner
13241326
# prevents inlining of the NimMainInner function and dependent
13251327
# functions, which might otherwise merge their stack frames.
13261328
PreMainBody = "$N" &
1327-
"void PreMainInner(void) {$N" &
1329+
"N_LIB_PRIVATE void PreMainInner(void) {$N" &
13281330
"$2" &
13291331
"}$N$N" &
13301332
PosixCmdLine &
1331-
"void PreMain(void) {$N" &
1333+
"N_LIB_PRIVATE void PreMain(void) {$N" &
13321334
"\tvoid (*volatile inner)(void);$N" &
13331335
"\tinner = PreMainInner;$N" &
13341336
"$1" &
@@ -1341,7 +1343,7 @@ proc genMainProc(m: BModule) =
13411343
MainProcsWithResult =
13421344
MainProcs & ("\treturn $1nim_program_result;$N")
13431345

1344-
NimMainInner = "N_CDECL(void, NimMainInner)(void) {$N" &
1346+
NimMainInner = "N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {$N" &
13451347
"$1" &
13461348
"}$N$N"
13471349

@@ -1388,7 +1390,7 @@ proc genMainProc(m: BModule) =
13881390
PosixNimDllMain = WinNimDllMain
13891391

13901392
PosixCDllMain =
1391-
"void NIM_POSIX_INIT NimMainInit(void) {$N" &
1393+
"N_LIB_PRIVATE void NIM_POSIX_INIT NimMainInit(void) {$N" &
13921394
MainProcs &
13931395
"}$N$N"
13941396

lib/nimbase.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,11 @@ __AVR__
182182
# define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)
183183

184184
# ifdef __cplusplus
185-
# define N_LIB_EXPORT extern "C" __declspec(dllexport)
185+
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
186186
# else
187-
# define N_LIB_EXPORT extern __declspec(dllexport)
187+
# define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
188188
# endif
189+
# define N_LIB_EXPORT_VAR __declspec(dllexport)
189190
# define N_LIB_IMPORT extern __declspec(dllimport)
190191
#else
191192
# define N_LIB_PRIVATE __attribute__((visibility("hidden")))
@@ -215,6 +216,7 @@ __AVR__
215216
# define N_SAFECALL_PTR(rettype, name) rettype (*name)
216217
# endif
217218
# define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
219+
# define N_LIB_EXPORT_VAR __attribute__((visibility("default")))
218220
# define N_LIB_IMPORT extern
219221
#endif
220222

lib/system/gc.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ proc gcMark(gch: var GcHeap, p: pointer) {.inline.} =
682682
`CLANG_NO_SANITIZE_ADDRESS` in `lib/nimbase.h`.
683683
]#
684684
proc markStackAndRegisters(gch: var GcHeap) {.noinline, cdecl,
685-
codegenDecl: "CLANG_NO_SANITIZE_ADDRESS $# $#$#".} =
685+
codegenDecl: "CLANG_NO_SANITIZE_ADDRESS N_LIB_PRIVATE $# $#$#".} =
686686
forEachStackSlot(gch, gcMark)
687687

688688
proc collectZCT(gch: var GcHeap): bool =

tests/dll/visibility.nim

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
discard """
2-
output: "could not import: foo"
3-
exitcode: 1
2+
output: ""
43
"""
54

65
const LibName {.used.} =
@@ -12,8 +11,33 @@ const LibName {.used.} =
1211
"libvisibility.so"
1312

1413
when compileOption("app", "lib"):
15-
proc foo() {.exportc.} =
16-
echo "failed"
14+
var
15+
bar {.exportc.}: int
16+
thr {.exportc, threadvar.}: int
17+
proc foo() {.exportc.} = discard
18+
19+
var
20+
exported {.exportc, dynlib.}: int
21+
exported_thr {.exportc, threadvar, dynlib.}: int
22+
proc exported_func() {.exportc, dynlib.} = discard
1723
elif isMainModule:
18-
proc foo() {.importc, dynlib: LibName.}
19-
foo()
24+
import dynlib
25+
26+
let handle = loadLib(LibName)
27+
28+
template check(sym: untyped) =
29+
const s = astToStr(sym)
30+
if handle.symAddr(s) != nil:
31+
echo s, " is exported"
32+
template checkE(sym: untyped) =
33+
const s = astToStr(sym)
34+
if handle.symAddr(s) == nil:
35+
echo s, " is not exported"
36+
37+
check foo
38+
check bar
39+
check thr
40+
41+
checkE exported
42+
checkE exported_thr
43+
checkE exported_func

0 commit comments

Comments
 (0)