[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / obj2yaml / wasm2yaml.cpp
blobe4a56524e36bc46d5fca6da7e2e9fa0f40e1977e
1 //===------ utils/wasm2yaml.cpp - obj2yaml conversion tool ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "obj2yaml.h"
10 #include "llvm/Object/COFF.h"
11 #include "llvm/ObjectYAML/WasmYAML.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/YAMLTraits.h"
15 using namespace llvm;
16 using object::WasmSection;
18 namespace {
20 class WasmDumper {
21 const object::WasmObjectFile &Obj;
23 public:
24 WasmDumper(const object::WasmObjectFile &O) : Obj(O) {}
26 ErrorOr<WasmYAML::Object *> dump();
28 std::unique_ptr<WasmYAML::CustomSection>
29 dumpCustomSection(const WasmSection &WasmSec);
32 } // namespace
34 static WasmYAML::Limits makeLimits(const wasm::WasmLimits &Limits) {
35 WasmYAML::Limits L;
36 L.Flags = Limits.Flags;
37 L.Minimum = Limits.Minimum;
38 L.Maximum = Limits.Maximum;
39 return L;
42 static WasmYAML::Table makeTable(uint32_t Index,
43 const wasm::WasmTableType &Type) {
44 WasmYAML::Table T;
45 T.Index = Index;
46 T.ElemType = Type.ElemType;
47 T.TableLimits = makeLimits(Type.Limits);
48 return T;
51 std::unique_ptr<WasmYAML::CustomSection>
52 WasmDumper::dumpCustomSection(const WasmSection &WasmSec) {
53 std::unique_ptr<WasmYAML::CustomSection> CustomSec;
54 if (WasmSec.Name == "dylink" || WasmSec.Name == "dylink.0") {
55 std::unique_ptr<WasmYAML::DylinkSection> DylinkSec =
56 std::make_unique<WasmYAML::DylinkSection>();
57 const wasm::WasmDylinkInfo& Info = Obj.dylinkInfo();
58 DylinkSec->MemorySize = Info.MemorySize;
59 DylinkSec->MemoryAlignment = Info.MemoryAlignment;
60 DylinkSec->TableSize = Info.TableSize;
61 DylinkSec->TableAlignment = Info.TableAlignment;
62 DylinkSec->Needed = Info.Needed;
63 for (const auto &Imp : Info.ImportInfo)
64 DylinkSec->ImportInfo.push_back({Imp.Module, Imp.Field, Imp.Flags});
65 for (const auto &Exp : Info.ExportInfo)
66 DylinkSec->ExportInfo.push_back({Exp.Name, Exp.Flags});
67 CustomSec = std::move(DylinkSec);
68 } else if (WasmSec.Name == "name") {
69 std::unique_ptr<WasmYAML::NameSection> NameSec =
70 std::make_unique<WasmYAML::NameSection>();
71 for (const llvm::wasm::WasmDebugName &Name : Obj.debugNames()) {
72 WasmYAML::NameEntry NameEntry;
73 NameEntry.Name = Name.Name;
74 NameEntry.Index = Name.Index;
75 if (Name.Type == llvm::wasm::NameType::FUNCTION) {
76 NameSec->FunctionNames.push_back(NameEntry);
77 } else if (Name.Type == llvm::wasm::NameType::GLOBAL) {
78 NameSec->GlobalNames.push_back(NameEntry);
79 } else {
80 assert(Name.Type == llvm::wasm::NameType::DATA_SEGMENT);
81 NameSec->DataSegmentNames.push_back(NameEntry);
84 CustomSec = std::move(NameSec);
85 } else if (WasmSec.Name == "linking") {
86 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec =
87 std::make_unique<WasmYAML::LinkingSection>();
88 LinkingSec->Version = Obj.linkingData().Version;
90 ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats;
91 for (StringRef ComdatName : Comdats)
92 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}});
93 for (auto &Func : Obj.functions()) {
94 if (Func.Comdat != UINT32_MAX) {
95 LinkingSec->Comdats[Func.Comdat].Entries.emplace_back(
96 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index});
100 uint32_t SegmentIndex = 0;
101 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
102 if (!Segment.Data.Name.empty()) {
103 WasmYAML::SegmentInfo SegmentInfo;
104 SegmentInfo.Name = Segment.Data.Name;
105 SegmentInfo.Index = SegmentIndex;
106 SegmentInfo.Alignment = Segment.Data.Alignment;
107 SegmentInfo.Flags = Segment.Data.LinkingFlags;
108 LinkingSec->SegmentInfos.push_back(SegmentInfo);
110 if (Segment.Data.Comdat != UINT32_MAX) {
111 LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back(
112 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
114 SegmentIndex++;
116 uint32_t SectionIndex = 0;
117 for (const auto &Sec : Obj.sections()) {
118 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
119 if (WasmSec.Comdat != UINT32_MAX)
120 LinkingSec->Comdats[WasmSec.Comdat].Entries.emplace_back(
121 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_SECTION, SectionIndex});
122 SectionIndex++;
125 uint32_t SymbolIndex = 0;
126 for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) {
127 WasmYAML::SymbolInfo Info;
128 Info.Index = SymbolIndex++;
129 Info.Kind = static_cast<uint32_t>(Symbol.Kind);
130 Info.Name = Symbol.Name;
131 Info.Flags = Symbol.Flags;
132 switch (Symbol.Kind) {
133 case wasm::WASM_SYMBOL_TYPE_DATA:
134 Info.DataRef = Symbol.DataRef;
135 break;
136 case wasm::WASM_SYMBOL_TYPE_FUNCTION:
137 case wasm::WASM_SYMBOL_TYPE_GLOBAL:
138 case wasm::WASM_SYMBOL_TYPE_TABLE:
139 case wasm::WASM_SYMBOL_TYPE_TAG:
140 Info.ElementIndex = Symbol.ElementIndex;
141 break;
142 case wasm::WASM_SYMBOL_TYPE_SECTION:
143 Info.ElementIndex = Symbol.ElementIndex;
144 break;
146 LinkingSec->SymbolTable.emplace_back(Info);
149 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) {
150 WasmYAML::InitFunction F{Func.Priority, Func.Symbol};
151 LinkingSec->InitFunctions.emplace_back(F);
154 CustomSec = std::move(LinkingSec);
155 } else if (WasmSec.Name == "producers") {
156 std::unique_ptr<WasmYAML::ProducersSection> ProducersSec =
157 std::make_unique<WasmYAML::ProducersSection>();
158 const llvm::wasm::WasmProducerInfo &Info = Obj.getProducerInfo();
159 for (auto &E : Info.Languages) {
160 WasmYAML::ProducerEntry Producer;
161 Producer.Name = E.first;
162 Producer.Version = E.second;
163 ProducersSec->Languages.push_back(Producer);
165 for (auto &E : Info.Tools) {
166 WasmYAML::ProducerEntry Producer;
167 Producer.Name = E.first;
168 Producer.Version = E.second;
169 ProducersSec->Tools.push_back(Producer);
171 for (auto &E : Info.SDKs) {
172 WasmYAML::ProducerEntry Producer;
173 Producer.Name = E.first;
174 Producer.Version = E.second;
175 ProducersSec->SDKs.push_back(Producer);
177 CustomSec = std::move(ProducersSec);
178 } else if (WasmSec.Name == "target_features") {
179 std::unique_ptr<WasmYAML::TargetFeaturesSection> TargetFeaturesSec =
180 std::make_unique<WasmYAML::TargetFeaturesSection>();
181 for (auto &E : Obj.getTargetFeatures()) {
182 WasmYAML::FeatureEntry Feature;
183 Feature.Prefix = E.Prefix;
184 Feature.Name = E.Name;
185 TargetFeaturesSec->Features.push_back(Feature);
187 CustomSec = std::move(TargetFeaturesSec);
188 } else {
189 CustomSec = std::make_unique<WasmYAML::CustomSection>(WasmSec.Name);
191 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content);
192 return CustomSec;
195 ErrorOr<WasmYAML::Object *> WasmDumper::dump() {
196 auto Y = std::make_unique<WasmYAML::Object>();
198 // Dump header
199 Y->Header.Version = Obj.getHeader().Version;
201 // Dump sections
202 for (const auto &Sec : Obj.sections()) {
203 const WasmSection &WasmSec = Obj.getWasmSection(Sec);
204 std::unique_ptr<WasmYAML::Section> S;
205 switch (WasmSec.Type) {
206 case wasm::WASM_SEC_CUSTOM: {
207 if (WasmSec.Name.startswith("reloc.")) {
208 // Relocations are attached the sections they apply to rather than
209 // being represented as a custom section in the YAML output.
210 continue;
212 S = dumpCustomSection(WasmSec);
213 break;
215 case wasm::WASM_SEC_TYPE: {
216 auto TypeSec = std::make_unique<WasmYAML::TypeSection>();
217 uint32_t Index = 0;
218 for (const auto &FunctionSig : Obj.types()) {
219 WasmYAML::Signature Sig;
220 Sig.Index = Index++;
221 for (const auto &ParamType : FunctionSig.Params)
222 Sig.ParamTypes.emplace_back(static_cast<uint32_t>(ParamType));
223 for (const auto &ReturnType : FunctionSig.Returns)
224 Sig.ReturnTypes.emplace_back(static_cast<uint32_t>(ReturnType));
225 TypeSec->Signatures.push_back(Sig);
227 S = std::move(TypeSec);
228 break;
230 case wasm::WASM_SEC_IMPORT: {
231 auto ImportSec = std::make_unique<WasmYAML::ImportSection>();
232 for (auto &Import : Obj.imports()) {
233 WasmYAML::Import Im;
234 Im.Module = Import.Module;
235 Im.Field = Import.Field;
236 Im.Kind = Import.Kind;
237 switch (Im.Kind) {
238 case wasm::WASM_EXTERNAL_FUNCTION:
239 Im.SigIndex = Import.SigIndex;
240 break;
241 case wasm::WASM_EXTERNAL_GLOBAL:
242 Im.GlobalImport.Type = Import.Global.Type;
243 Im.GlobalImport.Mutable = Import.Global.Mutable;
244 break;
245 case wasm::WASM_EXTERNAL_TAG:
246 Im.SigIndex = Import.SigIndex;
247 break;
248 case wasm::WASM_EXTERNAL_TABLE:
249 // FIXME: Currently we always output an index of 0 for any imported
250 // table.
251 Im.TableImport = makeTable(0, Import.Table);
252 break;
253 case wasm::WASM_EXTERNAL_MEMORY:
254 Im.Memory = makeLimits(Import.Memory);
255 break;
257 ImportSec->Imports.push_back(Im);
259 S = std::move(ImportSec);
260 break;
262 case wasm::WASM_SEC_FUNCTION: {
263 auto FuncSec = std::make_unique<WasmYAML::FunctionSection>();
264 for (const auto &Func : Obj.functions()) {
265 FuncSec->FunctionTypes.push_back(Func.SigIndex);
267 S = std::move(FuncSec);
268 break;
270 case wasm::WASM_SEC_TABLE: {
271 auto TableSec = std::make_unique<WasmYAML::TableSection>();
272 for (const wasm::WasmTable &Table : Obj.tables()) {
273 TableSec->Tables.push_back(makeTable(Table.Index, Table.Type));
275 S = std::move(TableSec);
276 break;
278 case wasm::WASM_SEC_MEMORY: {
279 auto MemorySec = std::make_unique<WasmYAML::MemorySection>();
280 for (const wasm::WasmLimits &Memory : Obj.memories()) {
281 MemorySec->Memories.push_back(makeLimits(Memory));
283 S = std::move(MemorySec);
284 break;
286 case wasm::WASM_SEC_TAG: {
287 auto TagSec = std::make_unique<WasmYAML::TagSection>();
288 for (auto &Tag : Obj.tags()) {
289 TagSec->TagTypes.push_back(Tag.SigIndex);
291 S = std::move(TagSec);
292 break;
294 case wasm::WASM_SEC_GLOBAL: {
295 auto GlobalSec = std::make_unique<WasmYAML::GlobalSection>();
296 for (auto &Global : Obj.globals()) {
297 WasmYAML::Global G;
298 G.Index = Global.Index;
299 G.Type = Global.Type.Type;
300 G.Mutable = Global.Type.Mutable;
301 G.InitExpr = Global.InitExpr;
302 GlobalSec->Globals.push_back(G);
304 S = std::move(GlobalSec);
305 break;
307 case wasm::WASM_SEC_START: {
308 auto StartSec = std::make_unique<WasmYAML::StartSection>();
309 StartSec->StartFunction = Obj.startFunction();
310 S = std::move(StartSec);
311 break;
313 case wasm::WASM_SEC_EXPORT: {
314 auto ExportSec = std::make_unique<WasmYAML::ExportSection>();
315 for (auto &Export : Obj.exports()) {
316 WasmYAML::Export Ex;
317 Ex.Name = Export.Name;
318 Ex.Kind = Export.Kind;
319 Ex.Index = Export.Index;
320 ExportSec->Exports.push_back(Ex);
322 S = std::move(ExportSec);
323 break;
325 case wasm::WASM_SEC_ELEM: {
326 auto ElemSec = std::make_unique<WasmYAML::ElemSection>();
327 for (auto &Segment : Obj.elements()) {
328 WasmYAML::ElemSegment Seg;
329 Seg.Flags = Segment.Flags;
330 Seg.TableNumber = Segment.TableNumber;
331 Seg.ElemKind = Segment.ElemKind;
332 Seg.Offset = Segment.Offset;
333 append_range(Seg.Functions, Segment.Functions);
334 ElemSec->Segments.push_back(Seg);
336 S = std::move(ElemSec);
337 break;
339 case wasm::WASM_SEC_CODE: {
340 auto CodeSec = std::make_unique<WasmYAML::CodeSection>();
341 for (auto &Func : Obj.functions()) {
342 WasmYAML::Function Function;
343 Function.Index = Func.Index;
344 for (auto &Local : Func.Locals) {
345 WasmYAML::LocalDecl LocalDecl;
346 LocalDecl.Type = Local.Type;
347 LocalDecl.Count = Local.Count;
348 Function.Locals.push_back(LocalDecl);
350 Function.Body = yaml::BinaryRef(Func.Body);
351 CodeSec->Functions.push_back(Function);
353 S = std::move(CodeSec);
354 break;
356 case wasm::WASM_SEC_DATA: {
357 auto DataSec = std::make_unique<WasmYAML::DataSection>();
358 for (const object::WasmSegment &Segment : Obj.dataSegments()) {
359 WasmYAML::DataSegment Seg;
360 Seg.SectionOffset = Segment.SectionOffset;
361 Seg.InitFlags = Segment.Data.InitFlags;
362 Seg.MemoryIndex = Segment.Data.MemoryIndex;
363 Seg.Offset = Segment.Data.Offset;
364 Seg.Content = yaml::BinaryRef(Segment.Data.Content);
365 DataSec->Segments.push_back(Seg);
367 S = std::move(DataSec);
368 break;
370 case wasm::WASM_SEC_DATACOUNT: {
371 auto DataCountSec = std::make_unique<WasmYAML::DataCountSection>();
372 DataCountSec->Count = Obj.dataSegments().size();
373 S = std::move(DataCountSec);
374 break;
376 default:
377 llvm_unreachable("Unknown section type");
378 break;
380 for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) {
381 WasmYAML::Relocation R;
382 R.Type = Reloc.Type;
383 R.Index = Reloc.Index;
384 R.Offset = Reloc.Offset;
385 R.Addend = Reloc.Addend;
386 S->Relocations.push_back(R);
388 Y->Sections.push_back(std::move(S));
391 return Y.release();
394 std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) {
395 WasmDumper Dumper(Obj);
396 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump();
397 if (std::error_code EC = YAMLOrErr.getError())
398 return EC;
400 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get());
401 yaml::Output Yout(Out);
402 Yout << *YAML;
404 return std::error_code();