summaryrefslogtreecommitdiff
path: root/src/binary-reader-objdump.cc
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2018-03-06 19:38:07 -0800
committerGitHub <noreply@github.com>2018-03-06 19:38:07 -0800
commitb449997b18c4bf44aa9f954d4419b9ada4017d15 (patch)
tree4c3420f222daa183edf2fe8a39bb1ccb717b8b05 /src/binary-reader-objdump.cc
parent39193c96e4ba05393e045548727fca616a9c47ad (diff)
downloadwabt-b449997b18c4bf44aa9f954d4419b9ada4017d15.tar.gz
wabt-b449997b18c4bf44aa9f954d4419b9ada4017d15.tar.bz2
wabt-b449997b18c4bf44aa9f954d4419b9ada4017d15.zip
Generate symbol table when creating relocations in binary-writer.cc (#794)
Diffstat (limited to 'src/binary-reader-objdump.cc')
-rw-r--r--src/binary-reader-objdump.cc93
1 files changed, 75 insertions, 18 deletions
diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc
index 724c4a9f..06b9fb98 100644
--- a/src/binary-reader-objdump.cc
+++ b/src/binary-reader-objdump.cc
@@ -50,6 +50,7 @@ class BinaryReaderObjdumpBase : public BinaryReaderNop {
protected:
const char* GetFunctionName(Index index) const;
const char* GetGlobalName(Index index) const;
+ string_view GetSymbolName(Index symbol_index) const;
void PrintRelocation(const Reloc& reloc, Offset offset) const;
Offset GetSectionStart(BinarySection section_code) const {
return section_starts_[static_cast<size_t>(section_code)];
@@ -134,24 +135,30 @@ const char* BinaryReaderObjdumpBase::GetGlobalName(Index index) const {
return objdump_state_->global_names[index].c_str();
}
+string_view BinaryReaderObjdumpBase::GetSymbolName(Index symbol_index) const {
+ assert(symbol_index < objdump_state_->symtab.size());
+ ObjdumpSymbol& sym = objdump_state_->symtab[symbol_index];
+ switch (sym.kind) {
+ case SymbolType::Function:
+ return GetFunctionName(sym.index);
+ case SymbolType::Data:
+ return sym.name;
+ case SymbolType::Global:
+ return GetGlobalName(sym.index);
+ }
+ WABT_UNREACHABLE;
+}
+
void BinaryReaderObjdumpBase::PrintRelocation(const Reloc& reloc,
Offset offset) const {
printf(" %06" PRIzx ": %-18s %" PRIindex "", offset,
GetRelocTypeName(reloc.type), reloc.index);
- switch (reloc.type) {
- case RelocType::MemoryAddressLEB:
- case RelocType::MemoryAddressSLEB:
- case RelocType::MemoryAddressI32:
- printf(" + %d", reloc.addend);
- break;
- case RelocType::FuncIndexLEB:
- case RelocType::TableIndexSLEB:
- case RelocType::TableIndexI32:
- if (const char* name = GetFunctionName(reloc.index)) {
- printf(" <%s>", name);
- }
- default:
- break;
+ if (reloc.addend) {
+ printf(" + %d", reloc.addend);
+ }
+ if (reloc.type != RelocType::TypeIndexLEB) {
+ printf(" <" PRIstringview ">",
+ WABT_PRINTF_STRING_VIEW_ARG(GetSymbolName(reloc.index)));
}
printf("\n");
}
@@ -172,6 +179,21 @@ class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase {
return Result::Ok;
}
+ Result OnSymbolCount(Index count) override {
+ objdump_state_->symtab.resize(count);
+ return Result::Ok;
+ }
+
+ Result OnDataSymbol(Index index,
+ uint32_t flags,
+ string_view name,
+ Index segment,
+ uint32_t offset,
+ uint32_t size) override {
+ objdump_state_->symtab[index] = {SymbolType::Data, name.to_string(), 0};
+ return Result::Ok;
+ }
+
Result OnFunctionSymbol(Index index,
uint32_t flags,
string_view name,
@@ -179,6 +201,20 @@ class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase {
if (!name.empty()) {
SetFunctionName(func_index, name);
}
+ objdump_state_->symtab[index] = {SymbolType::Function, name.to_string(),
+ func_index};
+ return Result::Ok;
+ }
+
+ Result OnGlobalSymbol(Index index,
+ uint32_t flags,
+ string_view name,
+ Index global_index) override {
+ if (!name.empty()) {
+ SetGlobalName(global_index, name);
+ }
+ objdump_state_->symtab[index] = {SymbolType::Global, name.to_string(),
+ global_index};
return Result::Ok;
}
@@ -191,6 +227,16 @@ class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase {
return Result::Ok;
}
+ Result OnImportGlobal(Index import_index,
+ string_view module_name,
+ string_view field_name,
+ Index global_index,
+ Type type,
+ bool mutable_) override {
+ SetGlobalName(global_index, field_name);
+ return Result::Ok;
+ }
+
Result OnExport(Index index,
ExternalKind kind,
Index item_index,
@@ -207,6 +253,7 @@ class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase {
protected:
void SetFunctionName(Index index, string_view name);
+ void SetGlobalName(Index index, string_view name);
};
void BinaryReaderObjdumpPrepass::SetFunctionName(Index index,
@@ -216,6 +263,12 @@ void BinaryReaderObjdumpPrepass::SetFunctionName(Index index,
objdump_state_->function_names[index] = name.to_string();
}
+void BinaryReaderObjdumpPrepass::SetGlobalName(Index index, string_view name) {
+ if (objdump_state_->global_names.size() <= index)
+ objdump_state_->global_names.resize(index + 1);
+ objdump_state_->global_names[index] = name.to_string();
+}
+
Result BinaryReaderObjdumpPrepass::OnReloc(RelocType type,
Offset offset,
Index index,
@@ -1158,10 +1211,12 @@ Result BinaryReaderObjdump::OnReloc(RelocType type,
Offset total_offset = GetSectionStart(reloc_section_) + offset;
PrintDetails(" - %-18s offset=%#08" PRIoffset "(file=%#08" PRIoffset ") ",
GetRelocTypeName(type), offset, total_offset);
- if (type == RelocType::TypeIndexLEB)
+ if (type == RelocType::TypeIndexLEB) {
PrintDetails("type=%" PRIindex, index);
- else
- PrintDetails("symbol=%" PRIindex, index);
+ } else {
+ PrintDetails("symbol=%" PRIindex " <" PRIstringview ">", index,
+ WABT_PRINTF_STRING_VIEW_ARG(GetSymbolName(index)));
+ }
if (addend) {
int32_t signed_addend = static_cast<int32_t>(addend);
@@ -1251,7 +1306,9 @@ Result BinaryReaderObjdump::OnGlobalSymbol(Index index,
Index global_index) {
std::string sym_name = name.to_string();
if (sym_name.empty()) {
- sym_name = GetGlobalName(global_index);
+ if (const char* Name = GetGlobalName(global_index)) {
+ sym_name = Name;
+ }
}
assert(!sym_name.empty());
PrintDetails(" - sym[%d] <" PRIstringview "> global=%" PRIindex, index,