2017-01-31 07:30:52 +08:00
|
|
|
//===-- WasmDumper.cpp - Wasm-specific object file dumper -----------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-01-31 07:30:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Wasm-specific dumper for llvm-readobj.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ObjDumper.h"
|
2017-04-15 03:50:44 +08:00
|
|
|
#include "llvm-readobj.h"
|
2017-01-31 07:30:52 +08:00
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
static const EnumEntry<unsigned> WasmSymbolTypes[] = {
|
2018-09-05 09:27:38 +08:00
|
|
|
#define ENUM_ENTRY(X) \
|
|
|
|
{ #X, wasm::WASM_SYMBOL_TYPE_##X }
|
2021-06-15 16:49:43 +08:00
|
|
|
ENUM_ENTRY(FUNCTION), ENUM_ENTRY(DATA), ENUM_ENTRY(GLOBAL),
|
|
|
|
ENUM_ENTRY(SECTION), ENUM_ENTRY(TAG), ENUM_ENTRY(TABLE),
|
2017-04-15 03:50:44 +08:00
|
|
|
#undef ENUM_ENTRY
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> WasmSectionTypes[] = {
|
2018-09-05 09:27:38 +08:00
|
|
|
#define ENUM_ENTRY(X) \
|
|
|
|
{ #X, wasm::WASM_SEC_##X }
|
2019-04-13 06:27:48 +08:00
|
|
|
ENUM_ENTRY(CUSTOM), ENUM_ENTRY(TYPE), ENUM_ENTRY(IMPORT),
|
|
|
|
ENUM_ENTRY(FUNCTION), ENUM_ENTRY(TABLE), ENUM_ENTRY(MEMORY),
|
2021-06-15 16:49:43 +08:00
|
|
|
ENUM_ENTRY(GLOBAL), ENUM_ENTRY(TAG), ENUM_ENTRY(EXPORT),
|
2019-04-13 06:27:48 +08:00
|
|
|
ENUM_ENTRY(START), ENUM_ENTRY(ELEM), ENUM_ENTRY(CODE),
|
|
|
|
ENUM_ENTRY(DATA), ENUM_ENTRY(DATACOUNT),
|
2017-04-15 03:50:44 +08:00
|
|
|
#undef ENUM_ENTRY
|
|
|
|
};
|
2017-01-31 07:30:52 +08:00
|
|
|
|
2019-02-07 09:17:34 +08:00
|
|
|
static const EnumEntry<unsigned> WasmSymbolFlags[] = {
|
|
|
|
#define ENUM_ENTRY(X) \
|
|
|
|
{ #X, wasm::WASM_SYMBOL_##X }
|
|
|
|
ENUM_ENTRY(BINDING_GLOBAL),
|
|
|
|
ENUM_ENTRY(BINDING_WEAK),
|
|
|
|
ENUM_ENTRY(BINDING_LOCAL),
|
|
|
|
ENUM_ENTRY(VISIBILITY_DEFAULT),
|
|
|
|
ENUM_ENTRY(VISIBILITY_HIDDEN),
|
|
|
|
ENUM_ENTRY(UNDEFINED),
|
2019-02-07 09:24:44 +08:00
|
|
|
ENUM_ENTRY(EXPORTED),
|
2019-05-01 03:30:24 +08:00
|
|
|
ENUM_ENTRY(EXPLICIT_NAME),
|
2019-08-30 06:40:00 +08:00
|
|
|
ENUM_ENTRY(NO_STRIP),
|
2019-02-07 09:17:34 +08:00
|
|
|
#undef ENUM_ENTRY
|
|
|
|
};
|
|
|
|
|
2017-01-31 07:30:52 +08:00
|
|
|
class WasmDumper : public ObjDumper {
|
|
|
|
public:
|
|
|
|
WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
|
2020-11-27 18:34:30 +08:00
|
|
|
: ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {}
|
2017-01-31 07:30:52 +08:00
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
void printFileHeaders() override;
|
[llvm-readelf] Make llvm-readelf more compatible with GNU readelf.
Summary:
This change adds a bunch of options that GNU readelf supports. There is one breaking change when invoked as `llvm-readobj`, and three breaking changes when invoked as `llvm-readelf`:
- Add --all (implies --file-header, --program-headers, etc.)
- [Breaking] -a is --all instead of --arm-attributes
- Add --file-header as an alias for --file-headers
- Replace --sections with --sections-headers, keeping --sections as an alias for it
- Add --relocs as an alias for --relocations
- Add --dynamic as an alias for --dynamic-table
- Add --segments as an alias for --program-headers
- Add --section-groups as an alias for --elf-section-groups
- Add --dyn-syms as an alias for --dyn-symbols
- Add --syms as an alias for --symbols
- Add --histogram as an alias for --elf-hash-histogram
- [Breaking] When invoked as `llvm-readelf`, -s is --symbols instead of --sections
- [Breaking] When invoked as `llvm-readelf`, -t is no longer an alias for --symbols
Reviewers: MaskRay, phosek, mcgrathr, jhenderson
Reviewed By: MaskRay, jhenderson
Subscribers: sbc100, aheejin, edd, jhenderson, silvas, echristo, compnerd, kristina, javed.absar, kristof.beyls, llvm-commits, Bigcheese
Differential Revision: https://reviews.llvm.org/D54124
llvm-svn: 346685
2018-11-13 02:02:38 +08:00
|
|
|
void printSectionHeaders() override;
|
2017-04-15 03:50:44 +08:00
|
|
|
void printRelocations() override;
|
2017-01-31 07:30:52 +08:00
|
|
|
void printUnwindInfo() override { llvm_unreachable("unimplemented"); }
|
|
|
|
void printStackMap() const override { llvm_unreachable("unimplemented"); }
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
protected:
|
|
|
|
void printSymbol(const SymbolRef &Sym);
|
|
|
|
void printRelocation(const SectionRef &Section, const RelocationRef &Reloc);
|
|
|
|
|
2017-01-31 07:30:52 +08:00
|
|
|
private:
|
2019-01-24 00:15:39 +08:00
|
|
|
void printSymbols() override;
|
|
|
|
void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
|
|
|
|
|
2017-01-31 07:30:52 +08:00
|
|
|
const WasmObjectFile *Obj;
|
|
|
|
};
|
2017-04-15 03:50:44 +08:00
|
|
|
|
|
|
|
void WasmDumper::printFileHeaders() {
|
|
|
|
W.printHex("Version", Obj->getHeader().Version);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printRelocation(const SectionRef &Section,
|
|
|
|
const RelocationRef &Reloc) {
|
|
|
|
SmallString<64> RelocTypeName;
|
|
|
|
uint64_t RelocType = Reloc.getType();
|
|
|
|
Reloc.getTypeName(RelocTypeName);
|
|
|
|
const wasm::WasmRelocation &WasmReloc = Obj->getWasmRelocation(Reloc);
|
|
|
|
|
2018-05-02 00:35:16 +08:00
|
|
|
StringRef SymName;
|
|
|
|
symbol_iterator SI = Reloc.getSymbol();
|
|
|
|
if (SI != Obj->symbol_end())
|
2019-08-09 19:03:21 +08:00
|
|
|
SymName = unwrapOrError(Obj->getFileName(), SI->getName());
|
2018-05-02 00:35:16 +08:00
|
|
|
|
2020-03-20 10:53:51 +08:00
|
|
|
bool HasAddend = wasm::relocTypeHasAddend(static_cast<uint32_t>(RelocType));
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
if (opts::ExpandRelocs) {
|
|
|
|
DictScope Group(W, "Relocation");
|
|
|
|
W.printNumber("Type", RelocTypeName, RelocType);
|
|
|
|
W.printHex("Offset", Reloc.getOffset());
|
2018-05-02 00:35:16 +08:00
|
|
|
if (!SymName.empty())
|
|
|
|
W.printString("Symbol", SymName);
|
|
|
|
else
|
|
|
|
W.printHex("Index", WasmReloc.Index);
|
2017-04-28 08:36:36 +08:00
|
|
|
if (HasAddend)
|
|
|
|
W.printNumber("Addend", WasmReloc.Addend);
|
2017-04-15 03:50:44 +08:00
|
|
|
} else {
|
2018-09-05 09:27:38 +08:00
|
|
|
raw_ostream &OS = W.startLine();
|
2018-05-02 00:35:16 +08:00
|
|
|
OS << W.hex(Reloc.getOffset()) << " " << RelocTypeName << " ";
|
|
|
|
if (!SymName.empty())
|
|
|
|
OS << SymName;
|
|
|
|
else
|
|
|
|
OS << WasmReloc.Index;
|
2017-04-28 08:36:36 +08:00
|
|
|
if (HasAddend)
|
|
|
|
OS << " " << WasmReloc.Addend;
|
|
|
|
OS << "\n";
|
2017-04-15 03:50:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printRelocations() {
|
|
|
|
ListScope D(W, "Relocations");
|
|
|
|
|
|
|
|
int SectionNumber = 0;
|
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
|
|
|
bool PrintedGroup = false;
|
2019-08-14 19:10:11 +08:00
|
|
|
StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
|
|
|
|
|
2017-04-15 03:50:44 +08:00
|
|
|
++SectionNumber;
|
|
|
|
|
|
|
|
for (const RelocationRef &Reloc : Section.relocations()) {
|
|
|
|
if (!PrintedGroup) {
|
|
|
|
W.startLine() << "Section (" << SectionNumber << ") " << Name << " {\n";
|
|
|
|
W.indent();
|
|
|
|
PrintedGroup = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
printRelocation(Section, Reloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintedGroup) {
|
|
|
|
W.unindent();
|
|
|
|
W.startLine() << "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printSymbols() {
|
|
|
|
ListScope Group(W, "Symbols");
|
|
|
|
|
|
|
|
for (const SymbolRef &Symbol : Obj->symbols())
|
|
|
|
printSymbol(Symbol);
|
|
|
|
}
|
|
|
|
|
[llvm-readelf] Make llvm-readelf more compatible with GNU readelf.
Summary:
This change adds a bunch of options that GNU readelf supports. There is one breaking change when invoked as `llvm-readobj`, and three breaking changes when invoked as `llvm-readelf`:
- Add --all (implies --file-header, --program-headers, etc.)
- [Breaking] -a is --all instead of --arm-attributes
- Add --file-header as an alias for --file-headers
- Replace --sections with --sections-headers, keeping --sections as an alias for it
- Add --relocs as an alias for --relocations
- Add --dynamic as an alias for --dynamic-table
- Add --segments as an alias for --program-headers
- Add --section-groups as an alias for --elf-section-groups
- Add --dyn-syms as an alias for --dyn-symbols
- Add --syms as an alias for --symbols
- Add --histogram as an alias for --elf-hash-histogram
- [Breaking] When invoked as `llvm-readelf`, -s is --symbols instead of --sections
- [Breaking] When invoked as `llvm-readelf`, -t is no longer an alias for --symbols
Reviewers: MaskRay, phosek, mcgrathr, jhenderson
Reviewed By: MaskRay, jhenderson
Subscribers: sbc100, aheejin, edd, jhenderson, silvas, echristo, compnerd, kristina, javed.absar, kristof.beyls, llvm-commits, Bigcheese
Differential Revision: https://reviews.llvm.org/D54124
llvm-svn: 346685
2018-11-13 02:02:38 +08:00
|
|
|
void WasmDumper::printSectionHeaders() {
|
2017-04-15 03:50:44 +08:00
|
|
|
ListScope Group(W, "Sections");
|
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
|
|
|
const WasmSection &WasmSec = Obj->getWasmSection(Section);
|
|
|
|
DictScope SectionD(W, "Section");
|
|
|
|
W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
|
2017-09-21 03:03:35 +08:00
|
|
|
W.printNumber("Size", static_cast<uint64_t>(WasmSec.Content.size()));
|
2017-04-15 03:50:44 +08:00
|
|
|
W.printNumber("Offset", WasmSec.Offset);
|
2017-04-29 05:12:09 +08:00
|
|
|
switch (WasmSec.Type) {
|
|
|
|
case wasm::WASM_SEC_CUSTOM:
|
2017-04-15 03:50:44 +08:00
|
|
|
W.printString("Name", WasmSec.Name);
|
2017-07-11 04:47:12 +08:00
|
|
|
if (WasmSec.Name == "linking") {
|
|
|
|
const wasm::WasmLinkingData &LinkingData = Obj->linkingData();
|
2017-12-19 08:04:41 +08:00
|
|
|
if (!LinkingData.InitFunctions.empty()) {
|
|
|
|
ListScope Group(W, "InitFunctions");
|
2018-09-05 09:27:38 +08:00
|
|
|
for (const wasm::WasmInitFunc &F : LinkingData.InitFunctions)
|
2018-02-23 13:08:34 +08:00
|
|
|
W.startLine() << F.Symbol << " (priority=" << F.Priority << ")\n";
|
2017-12-19 08:04:41 +08:00
|
|
|
}
|
2017-07-11 04:47:12 +08:00
|
|
|
}
|
2017-04-29 05:12:09 +08:00
|
|
|
break;
|
2017-09-21 03:03:35 +08:00
|
|
|
case wasm::WASM_SEC_DATA: {
|
|
|
|
ListScope Group(W, "Segments");
|
|
|
|
for (const WasmSegment &Segment : Obj->dataSegments()) {
|
2018-09-05 09:27:38 +08:00
|
|
|
const wasm::WasmDataSegment &Seg = Segment.Data;
|
2017-09-21 03:03:35 +08:00
|
|
|
DictScope Group(W, "Segment");
|
|
|
|
if (!Seg.Name.empty())
|
|
|
|
W.printString("Name", Seg.Name);
|
|
|
|
W.printNumber("Size", static_cast<uint64_t>(Seg.Content.size()));
|
|
|
|
if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST)
|
|
|
|
W.printNumber("Offset", Seg.Offset.Value.Int32);
|
2020-06-06 00:03:12 +08:00
|
|
|
else if (Seg.Offset.Opcode == wasm::WASM_OPCODE_I64_CONST)
|
|
|
|
W.printNumber("Offset", Seg.Offset.Value.Int64);
|
|
|
|
else
|
|
|
|
llvm_unreachable("unknown init expr opcode");
|
2017-09-21 03:03:35 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-04-29 05:12:09 +08:00
|
|
|
case wasm::WASM_SEC_MEMORY:
|
|
|
|
ListScope Group(W, "Memories");
|
|
|
|
for (const wasm::WasmLimits &Memory : Obj->memories()) {
|
|
|
|
DictScope Group(W, "Memory");
|
2021-03-23 21:46:32 +08:00
|
|
|
W.printNumber("MinPages", Memory.Minimum);
|
2017-04-29 05:12:09 +08:00
|
|
|
if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
|
|
|
|
W.printNumber("MaxPages", WasmSec.Offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-04-15 03:50:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts::SectionRelocations) {
|
|
|
|
ListScope D(W, "Relocations");
|
|
|
|
for (const RelocationRef &Reloc : Section.relocations())
|
|
|
|
printRelocation(Section, Reloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts::SectionData) {
|
|
|
|
W.printBinaryBlock("SectionData", WasmSec.Content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmDumper::printSymbol(const SymbolRef &Sym) {
|
|
|
|
DictScope D(W, "Symbol");
|
|
|
|
WasmSymbol Symbol = Obj->getWasmSymbol(Sym.getRawDataRefImpl());
|
2018-02-23 13:08:34 +08:00
|
|
|
W.printString("Name", Symbol.Info.Name);
|
|
|
|
W.printEnum("Type", Symbol.Info.Kind, makeArrayRef(WasmSymbolTypes));
|
2019-02-07 09:17:34 +08:00
|
|
|
W.printFlags("Flags", Symbol.Info.Flags, makeArrayRef(WasmSymbolFlags));
|
|
|
|
|
2019-02-08 06:03:32 +08:00
|
|
|
if (Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) {
|
2020-02-06 13:18:55 +08:00
|
|
|
if (Symbol.Info.ImportName) {
|
|
|
|
W.printString("ImportName", *Symbol.Info.ImportName);
|
|
|
|
}
|
|
|
|
if (Symbol.Info.ImportModule) {
|
|
|
|
W.printString("ImportModule", *Symbol.Info.ImportModule);
|
|
|
|
}
|
2019-02-08 06:03:32 +08:00
|
|
|
}
|
2019-02-05 06:27:46 +08:00
|
|
|
if (Symbol.Info.Kind != wasm::WASM_SYMBOL_TYPE_DATA) {
|
|
|
|
W.printHex("ElementIndex", Symbol.Info.ElementIndex);
|
|
|
|
} else if (!(Symbol.Info.Flags & wasm::WASM_SYMBOL_UNDEFINED)) {
|
|
|
|
W.printHex("Offset", Symbol.Info.DataRef.Offset);
|
|
|
|
W.printHex("Segment", Symbol.Info.DataRef.Segment);
|
|
|
|
W.printHex("Size", Symbol.Info.DataRef.Size);
|
|
|
|
}
|
2017-04-15 03:50:44 +08:00
|
|
|
}
|
|
|
|
|
2018-09-05 09:27:38 +08:00
|
|
|
} // namespace
|
2017-01-31 07:30:52 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2020-08-27 23:20:13 +08:00
|
|
|
std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
|
|
|
|
ScopedPrinter &Writer) {
|
|
|
|
return std::make_unique<WasmDumper>(&Obj, Writer);
|
2017-01-31 07:30:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|