[lld] r246583 - Don't include hidden or internal symbols in the symbol table.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 13:36:51 PDT 2015
Author: rafael
Date: Tue Sep 1 15:36:51 2015
New Revision: 246583
URL: http://llvm.org/viewvc/llvm-project?rev=246583&view=rev
Log:
Don't include hidden or internal symbols in the symbol table.
Modified:
lld/trunk/ELF/SymbolTable.h
lld/trunk/ELF/Writer.cpp
lld/trunk/test/elf2/symbols.s
Modified: lld/trunk/ELF/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.h?rev=246583&r1=246582&r2=246583&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.h (original)
+++ lld/trunk/ELF/SymbolTable.h Tue Sep 1 15:36:51 2015
@@ -41,7 +41,6 @@ public:
// The writer needs to infer the machine type from the object files.
std::vector<std::unique_ptr<ObjectFileBase>> ObjectFiles;
- unsigned getNumSymbols() { return Symtab.size(); }
llvm::StringTableBuilder &getStringBuilder() { return Builder; };
const llvm::DenseMap<StringRef, Symbol *> &getSymbols() const {
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=246583&r1=246582&r2=246583&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Sep 1 15:36:51 2015
@@ -133,8 +133,12 @@ public:
Header.sh_entsize = sizeof(Elf_Sym);
Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
- this->Header.sh_size = (Table.getNumSymbols() + 1) * sizeof(Elf_Sym);
}
+
+ void finalize() override {
+ this->Header.sh_size = (NumVisible + 1) * sizeof(Elf_Sym);
+ }
+
void setStringTableIndex(uint32_t Index) { this->Header.sh_link = Index; }
void writeTo(uint8_t *Buf) override;
@@ -142,6 +146,7 @@ public:
const SymbolTable &getSymTable() { return Table; }
OutputSection<ELFT> *BSSSec = nullptr;
+ unsigned NumVisible = 0;
private:
SymbolTable &Table;
@@ -301,6 +306,10 @@ template <class ELFT> void SymbolTableSe
SymbolBody *Body = Sym->Body;
const Elf_Sym &InputSym = cast<ELFSymbolBody<ELFT>>(Body)->Sym;
+ uint8_t V = InputSym.getVisibility();
+ if (V != STV_DEFAULT && V != STV_PROTECTED)
+ continue;
+
auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
ESym->st_name = Builder.getOffset(Name);
@@ -355,7 +364,7 @@ template <class ELFT> void SymbolTableSe
// FIXME: Experiment with passing in a custom hashing instead.
auto *Syms = reinterpret_cast<Elf_Sym *>(BufStart);
++Syms;
- array_pod_sort(Syms, Syms + Table.getSymbols().size(), compareSym<ELFT>);
+ array_pod_sort(Syms, Syms + NumVisible, compareSym<ELFT>);
}
template <bool Is64Bits>
@@ -439,11 +448,16 @@ template <class ELFT> void Writer<ELFT>:
SymTable.BSSSec = getSection(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
OutputSection<ELFT> *BSSSec = SymTable.BSSSec;
// FIXME: Try to avoid the extra walk over all global symbols.
+ unsigned &NumVisible = SymTable.NumVisible;
std::vector<DefinedCommon<ELFT> *> CommonSymbols;
for (auto &P : Symtab.getSymbols()) {
SymbolBody *Body = P.second->Body;
if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
CommonSymbols.push_back(C);
+ auto *E = cast<ELFSymbolBody<ELFT>>(Body);
+ uint8_t V = E->Sym.getVisibility();
+ if (V == STV_DEFAULT || V == STV_PROTECTED)
+ NumVisible++;
}
// Sort the common symbols by alignment as an heuristic to pack them better.
Modified: lld/trunk/test/elf2/symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/symbols.s?rev=246583&r1=246582&r2=246583&view=diff
==============================================================================
--- lld/trunk/test/elf2/symbols.s (original)
+++ lld/trunk/test/elf2/symbols.s Tue Sep 1 15:36:51 2015
@@ -37,6 +37,14 @@ abs = 0x123
.protected protected
protected:
+.global hidden
+.hidden hidden
+hidden:
+
+.global internal
+.internal internal
+internal:
+
// CHECK: Name: .text
// CHECK-NEXT: Type: SHT_PROGBITS
// CHECK-NEXT: Flags [
More information about the llvm-commits
mailing list