summaryrefslogtreecommitdiffstats
path: root/llvm/lib/MC
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/MC')
-rw-r--r--llvm/lib/MC/MachObjectWriter.cpp67
-rw-r--r--llvm/lib/MC/StringTableBuilder.cpp20
2 files changed, 41 insertions, 46 deletions
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 95b92485eb6..7a1ab79e716 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -525,15 +525,10 @@ void MachObjectWriter::BindIndirectSymbols(MCAssembler &Asm) {
}
/// ComputeSymbolTable - Compute the symbol table data
-///
-/// \param StringTable [out] - The string table data.
-/// \param StringIndexMap [out] - Map from symbol names to offsets in the
-/// string table.
-void MachObjectWriter::
-ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
- std::vector<MachSymbolData> &LocalSymbolData,
- std::vector<MachSymbolData> &ExternalSymbolData,
- std::vector<MachSymbolData> &UndefinedSymbolData) {
+void MachObjectWriter::ComputeSymbolTable(
+ MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
+ std::vector<MachSymbolData> &ExternalSymbolData,
+ std::vector<MachSymbolData> &UndefinedSymbolData) {
// Build section lookup table.
DenseMap<const MCSection*, uint8_t> SectionIndexMap;
unsigned Index = 1;
@@ -542,37 +537,34 @@ ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
SectionIndexMap[&it->getSection()] = Index;
assert(Index <= 256 && "Too many sections!");
- // Index 0 is always the empty string.
- StringMap<uint64_t> StringIndexMap;
- StringTable += '\x00';
+ // Build the string table.
+ for (MCSymbolData &SD : Asm.symbols()) {
+ const MCSymbol &Symbol = SD.getSymbol();
+ if (!Asm.isSymbolLinkerVisible(Symbol))
+ continue;
+
+ StringTable.add(Symbol.getName());
+ }
+ StringTable.finalize(StringTableBuilder::MachO);
- // Build the symbol arrays and the string table, but only for non-local
- // symbols.
+ // Build the symbol arrays but only for non-local symbols.
//
- // The particular order that we collect the symbols and create the string
- // table, then sort the symbols is chosen to match 'as'. Even though it
- // doesn't matter for correctness, this is important for letting us diff .o
- // files.
+ // The particular order that we collect and then sort the symbols is chosen to
+ // match 'as'. Even though it doesn't matter for correctness, this is
+ // important for letting us diff .o files.
for (MCSymbolData &SD : Asm.symbols()) {
const MCSymbol &Symbol = SD.getSymbol();
// Ignore non-linker visible symbols.
- if (!Asm.isSymbolLinkerVisible(SD.getSymbol()))
+ if (!Asm.isSymbolLinkerVisible(Symbol))
continue;
if (!SD.isExternal() && !Symbol.isUndefined())
continue;
- uint64_t &Entry = StringIndexMap[Symbol.getName()];
- if (!Entry) {
- Entry = StringTable.size();
- StringTable += Symbol.getName();
- StringTable += '\x00';
- }
-
MachSymbolData MSD;
MSD.SymbolData = &SD;
- MSD.StringIndex = Entry;
+ MSD.StringIndex = StringTable.getOffset(Symbol.getName());
if (Symbol.isUndefined()) {
MSD.SectionIndex = 0;
@@ -592,22 +584,15 @@ ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
const MCSymbol &Symbol = SD.getSymbol();
// Ignore non-linker visible symbols.
- if (!Asm.isSymbolLinkerVisible(SD.getSymbol()))
+ if (!Asm.isSymbolLinkerVisible(Symbol))
continue;
if (SD.isExternal() || Symbol.isUndefined())
continue;
- uint64_t &Entry = StringIndexMap[Symbol.getName()];
- if (!Entry) {
- Entry = StringTable.size();
- StringTable += Symbol.getName();
- StringTable += '\x00';
- }
-
MachSymbolData MSD;
MSD.SymbolData = &SD;
- MSD.StringIndex = Entry;
+ MSD.StringIndex = StringTable.getOffset(Symbol.getName());
if (Symbol.isAbsolute()) {
MSD.SectionIndex = 0;
@@ -631,10 +616,6 @@ ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
ExternalSymbolData[i].SymbolData->setIndex(Index++);
for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
UndefinedSymbolData[i].SymbolData->setIndex(Index++);
-
- // The string table is padded to a multiple of 4.
- while (StringTable.size() % 4)
- StringTable += '\x00';
}
void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
@@ -683,7 +664,7 @@ void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
markAbsoluteVariableSymbols(Asm, Layout);
// Compute symbol table information and bind symbol indices.
- ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
+ ComputeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
UndefinedSymbolData);
}
@@ -922,7 +903,7 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm,
sizeof(MachO::nlist_64) :
sizeof(MachO::nlist));
WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
- StringTableOffset, StringTable.size());
+ StringTableOffset, StringTable.data().size());
WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
FirstExternalSymbol, NumExternalSymbols,
@@ -1028,7 +1009,7 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm,
WriteNlist(UndefinedSymbolData[i], Layout);
// Write the string table.
- OS << StringTable.str();
+ OS << StringTable.data();
}
}
diff --git a/llvm/lib/MC/StringTableBuilder.cpp b/llvm/lib/MC/StringTableBuilder.cpp
index 34e7e6fdfbb..9de9363611e 100644
--- a/llvm/lib/MC/StringTableBuilder.cpp
+++ b/llvm/lib/MC/StringTableBuilder.cpp
@@ -36,12 +36,16 @@ void StringTableBuilder::finalize(Kind kind) {
std::sort(Strings.begin(), Strings.end(), compareBySuffix);
- if (kind == ELF) {
+ switch (kind) {
+ case ELF:
+ case MachO:
// Start the table with a NUL byte.
StringTable += '\x00';
- } else if (kind == WinCOFF) {
+ break;
+ case WinCOFF:
// Make room to write the table size later.
StringTable.append(4, '\x00');
+ break;
}
StringRef Previous;
@@ -60,11 +64,21 @@ void StringTableBuilder::finalize(Kind kind) {
Previous = s;
}
- if (kind == WinCOFF) {
+ switch (kind) {
+ case ELF:
+ break;
+ case MachO:
+ // Pad to multiple of 4.
+ while (StringTable.size() % 4)
+ StringTable += '\x00';
+ break;
+ case WinCOFF:
+ // Write the table size in the first word.
assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
uint32_t size = static_cast<uint32_t>(StringTable.size());
support::endian::write<uint32_t, support::little, support::unaligned>(
StringTable.data(), size);
+ break;
}
}
OpenPOWER on IntegriCloud