Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 12c9f92

Browse files
author
Alexey Samsonov
committed
llvm-symbolizer: use dynamic symbol table if the regular one is stripped.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202265 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2895793 commit 12c9f92

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed
Binary file not shown.

test/DebugInfo/llvm-symbolizer.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ BINARY_C: main
7777
BINARY_C-NEXT: /tmp/dbginfo{{[/\\]}}llvm-symbolizer-test.c:10
7878
BINARY_C: _start
7979
BINARY_C: {{g$}}
80+
81+
RUN: echo "0x1f1" > %t.input6
82+
RUN: llvm-symbolizer --obj %p/Inputs/shared-object-stripped.elf-i386 < %t.input6 \
83+
RUN: | FileCheck %s --check-prefix=STRIPPED
84+
85+
STRIPPED: global_func

tools/llvm-symbolizer/LLVMSymbolize.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "LLVMSymbolize.h"
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/Config/config.h"
17+
#include "llvm/Object/ELFObjectFile.h"
1718
#include "llvm/Object/MachO.h"
1819
#include "llvm/Support/Casting.h"
1920
#include "llvm/Support/Compression.h"
@@ -54,36 +55,49 @@ ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
5455
: Module(Obj), DebugInfoContext(DICtx) {
5556
for (symbol_iterator si = Module->symbol_begin(), se = Module->symbol_end();
5657
si != se; ++si) {
57-
SymbolRef::Type SymbolType;
58-
if (error(si->getType(SymbolType)))
59-
continue;
60-
if (SymbolType != SymbolRef::ST_Function &&
61-
SymbolType != SymbolRef::ST_Data)
62-
continue;
63-
uint64_t SymbolAddress;
64-
if (error(si->getAddress(SymbolAddress)) ||
65-
SymbolAddress == UnknownAddressOrSize)
66-
continue;
67-
uint64_t SymbolSize;
68-
// Getting symbol size is linear for Mach-O files, so assume that symbol
69-
// occupies the memory range up to the following symbol.
70-
if (isa<MachOObjectFile>(Obj))
71-
SymbolSize = 0;
72-
else if (error(si->getSize(SymbolSize)) ||
73-
SymbolSize == UnknownAddressOrSize)
74-
continue;
75-
StringRef SymbolName;
76-
if (error(si->getName(SymbolName)))
77-
continue;
78-
// Mach-O symbol table names have leading underscore, skip it.
79-
if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
80-
SymbolName = SymbolName.drop_front();
81-
// FIXME: If a function has alias, there are two entries in symbol table
82-
// with same address size. Make sure we choose the correct one.
83-
SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
84-
SymbolDesc SD = { SymbolAddress, SymbolSize };
85-
M.insert(std::make_pair(SD, SymbolName));
58+
addSymbol(si);
8659
}
60+
bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
61+
if (NoSymbolTable && Module->isELF()) {
62+
// Fallback to dynamic symbol table, if regular symbol table is stripped.
63+
std::pair<symbol_iterator, symbol_iterator> IDyn =
64+
getELFDynamicSymbolIterators(Module);
65+
for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
66+
addSymbol(si);
67+
}
68+
}
69+
}
70+
71+
void ModuleInfo::addSymbol(const symbol_iterator &Sym) {
72+
SymbolRef::Type SymbolType;
73+
if (error(Sym->getType(SymbolType)))
74+
return;
75+
if (SymbolType != SymbolRef::ST_Function &&
76+
SymbolType != SymbolRef::ST_Data)
77+
return;
78+
uint64_t SymbolAddress;
79+
if (error(Sym->getAddress(SymbolAddress)) ||
80+
SymbolAddress == UnknownAddressOrSize)
81+
return;
82+
uint64_t SymbolSize;
83+
// Getting symbol size is linear for Mach-O files, so assume that symbol
84+
// occupies the memory range up to the following symbol.
85+
if (isa<MachOObjectFile>(Module))
86+
SymbolSize = 0;
87+
else if (error(Sym->getSize(SymbolSize)) ||
88+
SymbolSize == UnknownAddressOrSize)
89+
return;
90+
StringRef SymbolName;
91+
if (error(Sym->getName(SymbolName)))
92+
return;
93+
// Mach-O symbol table names have leading underscore, skip it.
94+
if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
95+
SymbolName = SymbolName.drop_front();
96+
// FIXME: If a function has alias, there are two entries in symbol table
97+
// with same address size. Make sure we choose the correct one.
98+
SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
99+
SymbolDesc SD = { SymbolAddress, SymbolSize };
100+
M.insert(std::make_pair(SD, SymbolName));
87101
}
88102

89103
bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,

tools/llvm-symbolizer/LLVMSymbolize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class ModuleInfo {
102102
bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
103103
std::string &Name, uint64_t &Addr,
104104
uint64_t &Size) const;
105+
void addSymbol(const symbol_iterator &Sym);
105106
ObjectFile *Module;
106107
OwningPtr<DIContext> DebugInfoContext;
107108

0 commit comments

Comments
 (0)