llvm.org GIT mirror llvm / 6f9896f
Implement protected visibility. This partly implements PR1363. Linker should be taught to deal with protected symbols. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36565 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 12 years ago
14 changed file(s) with 57 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
10431043
10441044
10451045 bit(10-12)
1046 Visibility style: 0=Default, 1=Hidden.
1046 Visibility style: 0=Default, 1=Hidden, 2=Protected.
10471047
10481048
10491049 bit(13-31)
15051505
15061506
15071507 bit(16-18)
1508 Visibility style: 0=Default, 1=Hidden.
1508 Visibility style: 0=Default, 1=Hidden, 2=Protected.
15091509
15101510
15111511 bit(19-31)
590590 directly.
591591
592592
593
"protected" - Protected style:
594
595
On ELF, protected visibility indicates that the symbol will be placed in
596 the dynamic symbol table, but that references within the defining module will
597 bind to the local symbol. That is, the symbol cannot be overridden by another
598 module.
599
593600
594601
595602
4242 /// @brief An enumeration for the kinds of visibility of global values.
4343 enum VisibilityTypes {
4444 DefaultVisibility = 0, ///< The GV is visible
45 HiddenVisibility ///< The GV is hidden
45 HiddenVisibility, ///< The GV is hidden
46 ProtectedVisibility ///< The GV is protected
4647 };
4748
4849 protected:
5758 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
5859 // Linkage and Visibility from turning into negative values.
5960 LinkageTypes Linkage : 5; // The linkage of this global
60 unsigned Visibility : 1; // The visibility style of this global
61 unsigned Visibility : 2; // The visibility style of this global
6162 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
6263 std::string Section; // Section to emit this into, empty mean default
6364 public:
7374
7475 VisibilityTypes getVisibility() const { return (VisibilityTypes)Visibility; }
7576 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
77 bool hasProtectedVisibility() const {
78 return Visibility == ProtectedVisibility;
79 }
7680 void setVisibility(VisibilityTypes V) { Visibility = V; }
7781
7882 bool hasSection() const { return !Section.empty(); }
255255 /// HiddenDirective - This directive, if non-null, is used to declare a
256256 /// global or function as having hidden visibility.
257257 const char *HiddenDirective; // Defaults to "\t.hidden\t".
258
258
259 /// ProtectedDirective - This directive, if non-null, is used to declare a
260 /// global or function as having protected visibility.
261 const char *ProtectedDirective; // Defaults to "\t.protected\t".
262
259263 //===--- Dwarf Emission Directives -----------------------------------===//
260264
261265 /// AbsoluteSectionOffsets - True if we should emit abolute section
522526 const char *getHiddenDirective() const {
523527 return HiddenDirective;
524528 }
529 const char *getProtectedDirective() const {
530 return ProtectedDirective;
531 }
525532 bool isAbsoluteSectionOffsets() const {
526533 return AbsoluteSectionOffsets;
527534 }
205205 dllimport { return DLLIMPORT; }
206206 dllexport { return DLLEXPORT; }
207207 hidden { return HIDDEN; }
208 protected { return PROTECTED; }
208209 extern_weak { return EXTERN_WEAK; }
209210 external { return EXTERNAL; }
210211 thread_local { return THREAD_LOCAL; }
205205 dllimport { return DLLIMPORT; }
206206 dllexport { return DLLEXPORT; }
207207 hidden { return HIDDEN; }
208 protected { return PROTECTED; }
208209 extern_weak { return EXTERN_WEAK; }
209210 external { return EXTERNAL; }
210211 thread_local { return THREAD_LOCAL; }
10981098 %token NORETURN INREG SRET NOUNWIND
10991099
11001100 // Visibility Styles
1101 %token DEFAULT HIDDEN
1101 %token DEFAULT HIDDEN PROTECTED
11021102
11031103 %start Module
11041104 %%
11791179 ;
11801180
11811181 GVVisibilityStyle
1182 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1183 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1184 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1182 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1183 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1184 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1185 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
11851186 ;
11861187
11871188 FunctionDeclareLinkage
10981098 %token NORETURN INREG SRET NOUNWIND
10991099
11001100 // Visibility Styles
1101 %token DEFAULT HIDDEN
1101 %token DEFAULT HIDDEN PROTECTED
11021102
11031103 %start Module
11041104 %%
11791179 ;
11801180
11811181 GVVisibilityStyle
1182 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1183 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1184 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1182 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1183 | DEFAULT { $$ = GlobalValue::DefaultVisibility; }
1184 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1185 | PROTECTED { $$ = GlobalValue::ProtectedVisibility; }
11851186 ;
11861187
11871188 FunctionDeclareLinkage
15311531 switch (VisibilityID) {
15321532 case 0: Visibility = GlobalValue::DefaultVisibility; break;
15331533 case 1: Visibility = GlobalValue::HiddenVisibility; break;
1534 case 2: Visibility = GlobalValue::ProtectedVisibility; break;
15341535 default:
15351536 error("Unknown visibility type: " + utostr(VisibilityID));
15361537 Visibility = GlobalValue::DefaultVisibility;
17661767 switch (VisibilityID) {
17671768 case 0: Visibility = GlobalValue::DefaultVisibility; break;
17681769 case 1: Visibility = GlobalValue::HiddenVisibility; break;
1770 case 2: Visibility = GlobalValue::ProtectedVisibility; break;
17691771 default:
17701772 error("Unknown visibility type: " + utostr(VisibilityID));
17711773 Visibility = GlobalValue::DefaultVisibility;
956956 static unsigned getEncodedVisibility(const GlobalValue *GV) {
957957 switch (GV->getVisibility()) {
958958 default: assert(0 && "Invalid visibility!");
959 case GlobalValue::DefaultVisibility: return 0;
960 case GlobalValue::HiddenVisibility: return 1;
959 case GlobalValue::DefaultVisibility: return 0;
960 case GlobalValue::HiddenVisibility: return 1;
961 case GlobalValue::ProtectedVisibility: return 2;
961962 }
962963 }
963964
7373 UsedDirective(0),
7474 WeakRefDirective(0),
7575 HiddenDirective("\t.hidden\t"),
76 ProtectedDirective("\t.protected\t"),
7677 AbsoluteSectionOffsets(false),
7778 HasLEB128(false),
7879 HasDotLoc(false),
124124 }
125125 break;
126126 }
127 if (F->hasHiddenVisibility())
127 if (F->hasHiddenVisibility()) {
128128 if (const char *Directive = TAI->getHiddenDirective())
129129 O << Directive << CurrentFnName << "\n";
130 } else if (F->hasProtectedVisibility()) {
131 if (const char *Directive = TAI->getProtectedDirective())
132 O << Directive << CurrentFnName << "\n";
133 }
130134
131135 if (Subtarget->isTargetELF())
132136 O << "\t.type " << CurrentFnName << ",@function\n";
321325 if (isCallOp && isa(GV)) {
322326 if (printGOT(TM, Subtarget)) {
323327 // Assemble call via PLT for non-local symbols
324 if (!GV->hasHiddenVisibility() || GV->isDeclaration())
328 if (!(GV->hasHiddenVisibility() || GV->hasProtectedVisibility()) ||
329 GV->isDeclaration())
325330 O << "@PLT";
326331 }
327332 if (Subtarget->isTargetCygMing() && GV->isDeclaration())
154154 unsigned Size = TD->getTypeSize(Type);
155155 unsigned Align = TD->getPreferredAlignmentLog(I);
156156
157 if (I->hasHiddenVisibility())
157 if (I->hasHiddenVisibility()) {
158158 if (const char *Directive = TAI->getHiddenDirective())
159159 O << Directive << name << "\n";
160 } else if (I->hasProtectedVisibility()) {
161 if (const char *Directive = TAI->getProtectedDirective())
162 O << Directive << name << "\n";
163 }
164
160165 if (Subtarget->isTargetELF())
161166 O << "\t.type " << name << ",@object\n";
162167
885885 default: assert(0 && "Invalid visibility style!");
886886 case GlobalValue::DefaultVisibility: break;
887887 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
888 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
888889 }
889890 }
890891
913914 default: assert(0 && "Invalid visibility style!");
914915 case GlobalValue::DefaultVisibility: break;
915916 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
917 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
916918 }
917919
918920 Out << "alias ";
997999 default: assert(0 && "Invalid visibility style!");
9981000 case GlobalValue::DefaultVisibility: break;
9991001 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1002 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
10001003 }
10011004 }
10021005