Bug Summary

File:tools/obj2yaml/macho2yaml.cpp
Warning:line 210, column 17
Value stored to 'EndPtr' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name macho2yaml.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/obj2yaml -I /build/llvm-toolchain-snapshot-8~svn345461/tools/obj2yaml -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/obj2yaml -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/tools/obj2yaml/macho2yaml.cpp -faddrsig
1//===------ macho2yaml.cpp - obj2yaml conversion tool -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Error.h"
11#include "obj2yaml.h"
12#include "llvm/DebugInfo/DWARF/DWARFContext.h"
13#include "llvm/Object/MachOUniversal.h"
14#include "llvm/ObjectYAML/ObjectYAML.h"
15#include "llvm/Support/ErrorHandling.h"
16#include "llvm/Support/LEB128.h"
17
18#include <string.h> // for memcpy
19
20using namespace llvm;
21
22class MachODumper {
23
24 template <typename StructType>
25 const char *processLoadCommandData(
26 MachOYAML::LoadCommand &LC,
27 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd);
28
29 const object::MachOObjectFile &Obj;
30 void dumpHeader(std::unique_ptr<MachOYAML::Object> &Y);
31 void dumpLoadCommands(std::unique_ptr<MachOYAML::Object> &Y);
32 void dumpLinkEdit(std::unique_ptr<MachOYAML::Object> &Y);
33 void dumpRebaseOpcodes(std::unique_ptr<MachOYAML::Object> &Y);
34 void dumpBindOpcodes(std::vector<MachOYAML::BindOpcode> &BindOpcodes,
35 ArrayRef<uint8_t> OpcodeBuffer, bool Lazy = false);
36 void dumpExportTrie(std::unique_ptr<MachOYAML::Object> &Y);
37 void dumpSymbols(std::unique_ptr<MachOYAML::Object> &Y);
38 void dumpDebugAbbrev(DWARFContext &DCtx,
39 std::unique_ptr<MachOYAML::Object> &Y);
40 void dumpDebugStrings(DWARFContext &DCtx,
41 std::unique_ptr<MachOYAML::Object> &Y);
42
43public:
44 MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
45 Expected<std::unique_ptr<MachOYAML::Object>> dump();
46};
47
48#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
49 case MachO::LCName: \
50 memcpy((void *) & (LC.Data.LCStruct##_data), LoadCmd.Ptr, \
51 sizeof(MachO::LCStruct)); \
52 if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \
53 MachO::swapStruct(LC.Data.LCStruct##_data); \
54 EndPtr = processLoadCommandData<MachO::LCStruct>(LC, LoadCmd); \
55 break;
56
57template <typename SectionType>
58MachOYAML::Section constructSectionCommon(SectionType Sec) {
59 MachOYAML::Section TempSec;
60 memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16);
61 memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16);
62 TempSec.addr = Sec.addr;
63 TempSec.size = Sec.size;
64 TempSec.offset = Sec.offset;
65 TempSec.align = Sec.align;
66 TempSec.reloff = Sec.reloff;
67 TempSec.nreloc = Sec.nreloc;
68 TempSec.flags = Sec.flags;
69 TempSec.reserved1 = Sec.reserved1;
70 TempSec.reserved2 = Sec.reserved2;
71 TempSec.reserved3 = 0;
72 return TempSec;
73}
74
75template <typename SectionType>
76MachOYAML::Section constructSection(SectionType Sec);
77
78template <> MachOYAML::Section constructSection(MachO::section Sec) {
79 MachOYAML::Section TempSec = constructSectionCommon(Sec);
80 TempSec.reserved3 = 0;
81 return TempSec;
82}
83
84template <> MachOYAML::Section constructSection(MachO::section_64 Sec) {
85 MachOYAML::Section TempSec = constructSectionCommon(Sec);
86 TempSec.reserved3 = Sec.reserved3;
87 return TempSec;
88}
89
90template <typename SectionType, typename SegmentType>
91const char *
92extractSections(const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
93 std::vector<MachOYAML::Section> &Sections,
94 bool IsLittleEndian) {
95 auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
96 const SectionType *Curr =
97 reinterpret_cast<const SectionType *>(LoadCmd.Ptr + sizeof(SegmentType));
98 for (; reinterpret_cast<const void *>(Curr) < End; Curr++) {
99 if (IsLittleEndian != sys::IsLittleEndianHost) {
100 SectionType Sec;
101 memcpy((void *)&Sec, Curr, sizeof(SectionType));
102 MachO::swapStruct(Sec);
103 Sections.push_back(constructSection(Sec));
104 } else {
105 Sections.push_back(constructSection(*Curr));
106 }
107 }
108 return reinterpret_cast<const char *>(Curr);
109}
110
111template <typename StructType>
112const char *MachODumper::processLoadCommandData(
113 MachOYAML::LoadCommand &LC,
114 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
115 return LoadCmd.Ptr + sizeof(StructType);
116}
117
118template <>
119const char *MachODumper::processLoadCommandData<MachO::segment_command>(
120 MachOYAML::LoadCommand &LC,
121 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
122 return extractSections<MachO::section, MachO::segment_command>(
123 LoadCmd, LC.Sections, Obj.isLittleEndian());
124}
125
126template <>
127const char *MachODumper::processLoadCommandData<MachO::segment_command_64>(
128 MachOYAML::LoadCommand &LC,
129 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
130 return extractSections<MachO::section_64, MachO::segment_command_64>(
131 LoadCmd, LC.Sections, Obj.isLittleEndian());
132}
133
134template <typename StructType>
135const char *
136readString(MachOYAML::LoadCommand &LC,
137 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
138 auto Start = LoadCmd.Ptr + sizeof(StructType);
139 auto MaxSize = LoadCmd.C.cmdsize - sizeof(StructType);
140 auto Size = strnlen(Start, MaxSize);
141 LC.PayloadString = StringRef(Start, Size).str();
142 return Start + Size;
143}
144
145template <>
146const char *MachODumper::processLoadCommandData<MachO::dylib_command>(
147 MachOYAML::LoadCommand &LC,
148 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
149 return readString<MachO::dylib_command>(LC, LoadCmd);
150}
151
152template <>
153const char *MachODumper::processLoadCommandData<MachO::dylinker_command>(
154 MachOYAML::LoadCommand &LC,
155 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
156 return readString<MachO::dylinker_command>(LC, LoadCmd);
157}
158
159template <>
160const char *MachODumper::processLoadCommandData<MachO::rpath_command>(
161 MachOYAML::LoadCommand &LC,
162 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
163 return readString<MachO::rpath_command>(LC, LoadCmd);
164}
165
166template <>
167const char *MachODumper::processLoadCommandData<MachO::build_version_command>(
168 MachOYAML::LoadCommand &LC,
169 const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
170 auto Start = LoadCmd.Ptr + sizeof(MachO::build_version_command);
171 auto NTools = LC.Data.build_version_command_data.ntools;
172 for (unsigned i = 0; i < NTools; ++i) {
173 auto Curr = Start + i * sizeof(MachO::build_tool_version);
174 MachO::build_tool_version BV;
175 memcpy((void *)&BV, Curr, sizeof(MachO::build_tool_version));
176 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
177 MachO::swapStruct(BV);
178 LC.Tools.push_back(BV);
179 }
180 return Start + NTools * sizeof(MachO::build_tool_version);
181}
182
183Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
184 auto Y = make_unique<MachOYAML::Object>();
185 Y->IsLittleEndian = Obj.isLittleEndian();
186 dumpHeader(Y);
187 dumpLoadCommands(Y);
188 dumpLinkEdit(Y);
189
190 std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
191 if (auto Err = dwarf2yaml(*DICtx, Y->DWARF))
192 return errorCodeToError(Err);
193 return std::move(Y);
194}
195
196void MachODumper::dumpHeader(std::unique_ptr<MachOYAML::Object> &Y) {
197 Y->Header.magic = Obj.getHeader().magic;
198 Y->Header.cputype = Obj.getHeader().cputype;
199 Y->Header.cpusubtype = Obj.getHeader().cpusubtype;
200 Y->Header.filetype = Obj.getHeader().filetype;
201 Y->Header.ncmds = Obj.getHeader().ncmds;
202 Y->Header.sizeofcmds = Obj.getHeader().sizeofcmds;
203 Y->Header.flags = Obj.getHeader().flags;
204 Y->Header.reserved = 0;
205}
206
207void MachODumper::dumpLoadCommands(std::unique_ptr<MachOYAML::Object> &Y) {
208 for (auto LoadCmd : Obj.load_commands()) {
209 MachOYAML::LoadCommand LC;
210 const char *EndPtr = LoadCmd.Ptr;
Value stored to 'EndPtr' during its initialization is never read
211 switch (LoadCmd.C.cmd) {
212 default:
213 memcpy((void *)&(LC.Data.load_command_data), LoadCmd.Ptr,
214 sizeof(MachO::load_command));
215 if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
216 MachO::swapStruct(LC.Data.load_command_data);
217 EndPtr = processLoadCommandData<MachO::load_command>(LC, LoadCmd);
218 break;
219#include "llvm/BinaryFormat/MachO.def"
220 }
221 auto RemainingBytes = LoadCmd.C.cmdsize - (EndPtr - LoadCmd.Ptr);
222 if (!std::all_of(EndPtr, &EndPtr[RemainingBytes],
223 [](const char C) { return C == 0; })) {
224 LC.PayloadBytes.insert(LC.PayloadBytes.end(), EndPtr,
225 &EndPtr[RemainingBytes]);
226 RemainingBytes = 0;
227 }
228 LC.ZeroPadBytes = RemainingBytes;
229 Y->LoadCommands.push_back(std::move(LC));
230 }
231}
232
233void MachODumper::dumpLinkEdit(std::unique_ptr<MachOYAML::Object> &Y) {
234 dumpRebaseOpcodes(Y);
235 dumpBindOpcodes(Y->LinkEdit.BindOpcodes, Obj.getDyldInfoBindOpcodes());
236 dumpBindOpcodes(Y->LinkEdit.WeakBindOpcodes,
237 Obj.getDyldInfoWeakBindOpcodes());
238 dumpBindOpcodes(Y->LinkEdit.LazyBindOpcodes, Obj.getDyldInfoLazyBindOpcodes(),
239 true);
240 dumpExportTrie(Y);
241 dumpSymbols(Y);
242}
243
244void MachODumper::dumpRebaseOpcodes(std::unique_ptr<MachOYAML::Object> &Y) {
245 MachOYAML::LinkEditData &LEData = Y->LinkEdit;
246
247 auto RebaseOpcodes = Obj.getDyldInfoRebaseOpcodes();
248 for (auto OpCode = RebaseOpcodes.begin(); OpCode != RebaseOpcodes.end();
249 ++OpCode) {
250 MachOYAML::RebaseOpcode RebaseOp;
251 RebaseOp.Opcode =
252 static_cast<MachO::RebaseOpcode>(*OpCode & MachO::REBASE_OPCODE_MASK);
253 RebaseOp.Imm = *OpCode & MachO::REBASE_IMMEDIATE_MASK;
254
255 unsigned Count;
256 uint64_t ULEB = 0;
257
258 switch (RebaseOp.Opcode) {
259 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
260
261 ULEB = decodeULEB128(OpCode + 1, &Count);
262 RebaseOp.ExtraData.push_back(ULEB);
263 OpCode += Count;
264 LLVM_FALLTHROUGH[[clang::fallthrough]];
265 // Intentionally no break here -- This opcode has two ULEB values
266 case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
267 case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
268 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
269 case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
270
271 ULEB = decodeULEB128(OpCode + 1, &Count);
272 RebaseOp.ExtraData.push_back(ULEB);
273 OpCode += Count;
274 break;
275 default:
276 break;
277 }
278
279 LEData.RebaseOpcodes.push_back(RebaseOp);
280
281 if (RebaseOp.Opcode == MachO::REBASE_OPCODE_DONE)
282 break;
283 }
284}
285
286StringRef ReadStringRef(const uint8_t *Start) {
287 const uint8_t *Itr = Start;
288 for (; *Itr; ++Itr)
289 ;
290 return StringRef(reinterpret_cast<const char *>(Start), Itr - Start);
291}
292
293void MachODumper::dumpBindOpcodes(
294 std::vector<MachOYAML::BindOpcode> &BindOpcodes,
295 ArrayRef<uint8_t> OpcodeBuffer, bool Lazy) {
296 for (auto OpCode = OpcodeBuffer.begin(); OpCode != OpcodeBuffer.end();
297 ++OpCode) {
298 MachOYAML::BindOpcode BindOp;
299 BindOp.Opcode =
300 static_cast<MachO::BindOpcode>(*OpCode & MachO::BIND_OPCODE_MASK);
301 BindOp.Imm = *OpCode & MachO::BIND_IMMEDIATE_MASK;
302
303 unsigned Count;
304 uint64_t ULEB = 0;
305 int64_t SLEB = 0;
306
307 switch (BindOp.Opcode) {
308 case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
309 ULEB = decodeULEB128(OpCode + 1, &Count);
310 BindOp.ULEBExtraData.push_back(ULEB);
311 OpCode += Count;
312 LLVM_FALLTHROUGH[[clang::fallthrough]];
313 // Intentionally no break here -- this opcode has two ULEB values
314
315 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
316 case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
317 case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
318 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
319 ULEB = decodeULEB128(OpCode + 1, &Count);
320 BindOp.ULEBExtraData.push_back(ULEB);
321 OpCode += Count;
322 break;
323
324 case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
325 SLEB = decodeSLEB128(OpCode + 1, &Count);
326 BindOp.SLEBExtraData.push_back(SLEB);
327 OpCode += Count;
328 break;
329
330 case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
331 BindOp.Symbol = ReadStringRef(OpCode + 1);
332 OpCode += BindOp.Symbol.size() + 1;
333 break;
334 default:
335 break;
336 }
337
338 BindOpcodes.push_back(BindOp);
339
340 // Lazy bindings have DONE opcodes between operations, so we need to keep
341 // processing after a DONE.
342 if (!Lazy && BindOp.Opcode == MachO::BIND_OPCODE_DONE)
343 break;
344 }
345}
346
347/*!
348 * /brief processes a node from the export trie, and its children.
349 *
350 * To my knowledge there is no documentation of the encoded format of this data
351 * other than in the heads of the Apple linker engineers. To that end hopefully
352 * this comment and the implementation below can serve to light the way for
353 * anyone crazy enough to come down this path in the future.
354 *
355 * This function reads and preserves the trie structure of the export trie. To
356 * my knowledge there is no code anywhere else that reads the data and preserves
357 * the Trie. LD64 (sources available at opensource.apple.com) has a similar
358 * implementation that parses the export trie into a vector. That code as well
359 * as LLVM's libObject MachO implementation were the basis for this.
360 *
361 * The export trie is an encoded trie. The node serialization is a bit awkward.
362 * The below pseudo-code is the best description I've come up with for it.
363 *
364 * struct SerializedNode {
365 * ULEB128 TerminalSize;
366 * struct TerminalData { <-- This is only present if TerminalSize > 0
367 * ULEB128 Flags;
368 * ULEB128 Address; <-- Present if (! Flags & REEXPORT )
369 * ULEB128 Other; <-- Present if ( Flags & REEXPORT ||
370 * Flags & STUB_AND_RESOLVER )
371 * char[] ImportName; <-- Present if ( Flags & REEXPORT )
372 * }
373 * uint8_t ChildrenCount;
374 * Pair<char[], ULEB128> ChildNameOffsetPair[ChildrenCount];
375 * SerializedNode Children[ChildrenCount]
376 * }
377 *
378 * Terminal nodes are nodes that represent actual exports. They can appear
379 * anywhere in the tree other than at the root; they do not need to be leaf
380 * nodes. When reading the data out of the trie this routine reads it in-order,
381 * but it puts the child names and offsets directly into the child nodes. This
382 * results in looping over the children twice during serialization and
383 * de-serialization, but it makes the YAML representation more human readable.
384 *
385 * Below is an example of the graph from a "Hello World" executable:
386 *
387 * -------
388 * | '' |
389 * -------
390 * |
391 * -------
392 * | '_' |
393 * -------
394 * |
395 * |----------------------------------------|
396 * | |
397 * ------------------------ ---------------------
398 * | '_mh_execute_header' | | 'main' |
399 * | Flags: 0x00000000 | | Flags: 0x00000000 |
400 * | Addr: 0x00000000 | | Addr: 0x00001160 |
401 * ------------------------ ---------------------
402 *
403 * This graph represents the trie for the exports "__mh_execute_header" and
404 * "_main". In the graph only the "_main" and "__mh_execute_header" nodes are
405 * terminal.
406*/
407
408const uint8_t *processExportNode(const uint8_t *CurrPtr,
409 const uint8_t *const End,
410 MachOYAML::ExportEntry &Entry) {
411 if (CurrPtr >= End)
412 return CurrPtr;
413 unsigned Count = 0;
414 Entry.TerminalSize = decodeULEB128(CurrPtr, &Count);
415 CurrPtr += Count;
416 if (Entry.TerminalSize != 0) {
417 Entry.Flags = decodeULEB128(CurrPtr, &Count);
418 CurrPtr += Count;
419 if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
420 Entry.Address = 0;
421 Entry.Other = decodeULEB128(CurrPtr, &Count);
422 CurrPtr += Count;
423 Entry.ImportName = std::string(reinterpret_cast<const char *>(CurrPtr));
424 } else {
425 Entry.Address = decodeULEB128(CurrPtr, &Count);
426 CurrPtr += Count;
427 if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
428 Entry.Other = decodeULEB128(CurrPtr, &Count);
429 CurrPtr += Count;
430 } else
431 Entry.Other = 0;
432 }
433 }
434 uint8_t childrenCount = *CurrPtr++;
435 if (childrenCount == 0)
436 return CurrPtr;
437
438 Entry.Children.insert(Entry.Children.begin(), (size_t)childrenCount,
439 MachOYAML::ExportEntry());
440 for (auto &Child : Entry.Children) {
441 Child.Name = std::string(reinterpret_cast<const char *>(CurrPtr));
442 CurrPtr += Child.Name.length() + 1;
443 Child.NodeOffset = decodeULEB128(CurrPtr, &Count);
444 CurrPtr += Count;
445 }
446 for (auto &Child : Entry.Children) {
447 CurrPtr = processExportNode(CurrPtr, End, Child);
448 }
449 return CurrPtr;
450}
451
452void MachODumper::dumpExportTrie(std::unique_ptr<MachOYAML::Object> &Y) {
453 MachOYAML::LinkEditData &LEData = Y->LinkEdit;
454 auto ExportsTrie = Obj.getDyldInfoExportsTrie();
455 processExportNode(ExportsTrie.begin(), ExportsTrie.end(), LEData.ExportTrie);
456}
457
458template <typename nlist_t>
459MachOYAML::NListEntry constructNameList(const nlist_t &nlist) {
460 MachOYAML::NListEntry NL;
461 NL.n_strx = nlist.n_strx;
462 NL.n_type = nlist.n_type;
463 NL.n_sect = nlist.n_sect;
464 NL.n_desc = nlist.n_desc;
465 NL.n_value = nlist.n_value;
466 return NL;
467}
468
469void MachODumper::dumpSymbols(std::unique_ptr<MachOYAML::Object> &Y) {
470 MachOYAML::LinkEditData &LEData = Y->LinkEdit;
471
472 for (auto Symbol : Obj.symbols()) {
473 MachOYAML::NListEntry NLE =
474 Obj.is64Bit()
475 ? constructNameList<MachO::nlist_64>(
476 Obj.getSymbol64TableEntry(Symbol.getRawDataRefImpl()))
477 : constructNameList<MachO::nlist>(
478 Obj.getSymbolTableEntry(Symbol.getRawDataRefImpl()));
479 LEData.NameList.push_back(NLE);
480 }
481
482 StringRef RemainingTable = Obj.getStringTableData();
483 while (RemainingTable.size() > 0) {
484 auto SymbolPair = RemainingTable.split('\0');
485 RemainingTable = SymbolPair.second;
486 LEData.StringTable.push_back(SymbolPair.first);
487 }
488}
489
490Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
491 MachODumper Dumper(Obj);
492 Expected<std::unique_ptr<MachOYAML::Object>> YAML = Dumper.dump();
493 if (!YAML)
494 return YAML.takeError();
495
496 yaml::YamlObjectFile YAMLFile;
497 YAMLFile.MachO = std::move(YAML.get());
498
499 yaml::Output Yout(Out);
500 Yout << YAMLFile;
501 return Error::success();
502}
503
504Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) {
505 yaml::YamlObjectFile YAMLFile;
506 YAMLFile.FatMachO.reset(new MachOYAML::UniversalBinary());
507 MachOYAML::UniversalBinary &YAML = *YAMLFile.FatMachO;
508 YAML.Header.magic = Obj.getMagic();
509 YAML.Header.nfat_arch = Obj.getNumberOfObjects();
510
511 for (auto Slice : Obj.objects()) {
512 MachOYAML::FatArch arch;
513 arch.cputype = Slice.getCPUType();
514 arch.cpusubtype = Slice.getCPUSubType();
515 arch.offset = Slice.getOffset();
516 arch.size = Slice.getSize();
517 arch.align = Slice.getAlign();
518 arch.reserved = Slice.getReserved();
519 YAML.FatArchs.push_back(arch);
520
521 auto SliceObj = Slice.getAsObjectFile();
522 if (!SliceObj)
523 return SliceObj.takeError();
524
525 MachODumper Dumper(*SliceObj.get());
526 Expected<std::unique_ptr<MachOYAML::Object>> YAMLObj = Dumper.dump();
527 if (!YAMLObj)
528 return YAMLObj.takeError();
529 YAML.Slices.push_back(*YAMLObj.get());
530 }
531
532 yaml::Output Yout(Out);
533 Yout << YAML;
534 return Error::success();
535}
536
537std::error_code macho2yaml(raw_ostream &Out, const object::Binary &Binary) {
538 if (const auto *MachOObj = dyn_cast<object::MachOUniversalBinary>(&Binary)) {
539 if (auto Err = macho2yaml(Out, *MachOObj)) {
540 return errorToErrorCode(std::move(Err));
541 }
542 return obj2yaml_error::success;
543 }
544
545 if (const auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Binary)) {
546 if (auto Err = macho2yaml(Out, *MachOObj)) {
547 return errorToErrorCode(std::move(Err));
548 }
549 return obj2yaml_error::success;
550 }
551
552 return obj2yaml_error::unsupported_obj_file_format;
553}