Skip to content

Commit c6f13ce

Browse files
[llvm-objdump][NFC] MachODump.cpp interface cleanup
Continuing from D77388, this patch moves interface declarations associated with `MachODump.cpp` into the headers corresponding to the file that defines the variable. At the same time, these externs are moved into the `llvm::objdump` namespace. The externs defined in `MachODump.cpp` that are not referenced outside of it are given internal linkage. This patch does not rename the external functions defined by `MachODump.cpp` that are not clearly named as being specific to Mach-O. Reviewed By: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D77730
1 parent 64868cb commit c6f13ce

File tree

4 files changed

+64
-48
lines changed

4 files changed

+64
-48
lines changed

llvm/tools/llvm-objdump/MachODump.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm-c/Disassembler.h"
1717
#include "llvm/ADT/STLExtras.h"
1818
#include "llvm/ADT/StringExtras.h"
19-
#include "llvm/ADT/StringSet.h"
2019
#include "llvm/ADT/Triple.h"
2120
#include "llvm/BinaryFormat/MachO.h"
2221
#include "llvm/Config/config.h"
@@ -178,11 +177,7 @@ static cl::list<std::string>
178177
ArchFlags("arch", cl::desc("architecture(s) from a Mach-O file to dump"),
179178
cl::ZeroOrMore, cl::cat(MachOCat));
180179

181-
namespace llvm {
182-
183-
extern StringSet<> FoundSectionSet;
184-
185-
bool ArchAll = false;
180+
static bool ArchAll = false;
186181

187182
static std::string ThumbTripleName;
188183

@@ -224,6 +219,7 @@ static const Target *GetTarget(const MachOObjectFile *MachOObj,
224219
return nullptr;
225220
}
226221

222+
namespace {
227223
struct SymbolSorter {
228224
bool operator()(const SymbolRef &A, const SymbolRef &B) {
229225
Expected<SymbolRef::Type> ATypeOrErr = A.getType();
@@ -239,6 +235,7 @@ struct SymbolSorter {
239235
return AAddr < BAddr;
240236
}
241237
};
238+
} // namespace
242239

243240
// Types for the storted data in code table that is built before disassembly
244241
// and the predicate function to sort them.
@@ -488,9 +485,9 @@ static void printRelocationTargetName(const MachOObjectFile *O,
488485
Fmt << S;
489486
}
490487

491-
Error getMachORelocationValueString(const MachOObjectFile *Obj,
492-
const RelocationRef &RelRef,
493-
SmallVectorImpl<char> &Result) {
488+
Error objdump::getMachORelocationValueString(const MachOObjectFile *Obj,
489+
const RelocationRef &RelRef,
490+
SmallVectorImpl<char> &Result) {
494491
DataRefImpl Rel = RelRef.getRawDataRefImpl();
495492
MachO::any_relocation_info RE = Obj->getRelocation(Rel);
496493

@@ -2313,7 +2310,7 @@ static bool ValidateArchFlags() {
23132310
// -arch flags selecting just those slices as specified by them and also parses
23142311
// archive files. Then for each individual Mach-O file ProcessMachO() is
23152312
// called to process the file based on the command line options.
2316-
void parseInputMachO(StringRef Filename) {
2313+
void objdump::parseInputMachO(StringRef Filename) {
23172314
if (!ValidateArchFlags())
23182315
return;
23192316

@@ -2371,7 +2368,7 @@ void parseInputMachO(StringRef Filename) {
23712368
llvm_unreachable("Input object can't be invalid at this point");
23722369
}
23732370

2374-
void parseInputMachO(MachOUniversalBinary *UB) {
2371+
void objdump::parseInputMachO(MachOUniversalBinary *UB) {
23752372
if (!ValidateArchFlags())
23762373
return;
23772374

@@ -2557,6 +2554,7 @@ void parseInputMachO(MachOUniversalBinary *UB) {
25572554
}
25582555
}
25592556

2557+
namespace {
25602558
// The block of info used by the Symbolizer call backs.
25612559
struct DisassembleInfo {
25622560
DisassembleInfo(MachOObjectFile *O, SymbolAddressMap *AddrMap,
@@ -2576,6 +2574,7 @@ struct DisassembleInfo {
25762574
std::unique_ptr<SymbolAddressMap> bindtable;
25772575
uint32_t depth = 0;
25782576
};
2577+
} // namespace
25792578

25802579
// SymbolizerGetOpInfo() is the operand information call back function.
25812580
// This is called to get the symbolic information for operand(s) of an
@@ -3384,6 +3383,8 @@ static const char *get_symbol_32(uint32_t sect_offset, SectionRef S,
33843383
return get_symbol_64(sect_offset, S, info, n_value64, ReferenceValue);
33853384
}
33863385

3386+
namespace {
3387+
33873388
// These are structs in the Objective-C meta data and read to produce the
33883389
// comments for disassembly. While these are part of the ABI they are no
33893390
// public defintions. So the are here not in include/llvm/BinaryFormat/MachO.h
@@ -3969,6 +3970,8 @@ inline void swapStruct(struct objc_method_description_t &md) {
39693970
sys::swapByteOrder(md.types);
39703971
}
39713972

3973+
} // namespace
3974+
39723975
static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
39733976
struct DisassembleInfo *info);
39743977

@@ -6468,7 +6471,7 @@ static void DumpProtocolSection(MachOObjectFile *O, const char *sect,
64686471
}
64696472

64706473
#ifdef HAVE_LIBXAR
6471-
inline void swapStruct(struct xar_header &xar) {
6474+
static inline void swapStruct(struct xar_header &xar) {
64726475
sys::swapByteOrder(xar.magic);
64736476
sys::swapByteOrder(xar.size);
64746477
sys::swapByteOrder(xar.version);
@@ -8189,7 +8192,7 @@ static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
81898192
}
81908193
}
81918194

8192-
void printMachOUnwindInfo(const MachOObjectFile *Obj) {
8195+
void objdump::printMachOUnwindInfo(const MachOObjectFile *Obj) {
81938196
std::map<uint64_t, SymbolRef> Symbols;
81948197
for (const SymbolRef &SymRef : Obj->symbols()) {
81958198
// Discard any undefined or absolute symbols. They're not going to take part
@@ -10197,12 +10200,12 @@ static void PrintMachHeader(const MachOObjectFile *Obj, bool verbose) {
1019710200
}
1019810201
}
1019910202

10200-
void printMachOFileHeader(const object::ObjectFile *Obj) {
10203+
void objdump::printMachOFileHeader(const object::ObjectFile *Obj) {
1020110204
const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
1020210205
PrintMachHeader(file, !NonVerbose);
1020310206
}
1020410207

10205-
void printMachOLoadCommands(const object::ObjectFile *Obj) {
10208+
void objdump::printMachOLoadCommands(const object::ObjectFile *Obj) {
1020610209
const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
1020710210
uint32_t filetype = 0;
1020810211
uint32_t cputype = 0;
@@ -10224,7 +10227,7 @@ void printMachOLoadCommands(const object::ObjectFile *Obj) {
1022410227
// export trie dumping
1022510228
//===----------------------------------------------------------------------===//
1022610229

10227-
void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
10230+
static void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
1022810231
uint64_t BaseSegmentAddress = 0;
1022910232
for (const auto &Command : Obj->load_commands()) {
1023010233
if (Command.C.cmd == MachO::LC_SEGMENT) {
@@ -10303,7 +10306,7 @@ void printMachOExportsTrie(const object::MachOObjectFile *Obj) {
1030310306
// rebase table dumping
1030410307
//===----------------------------------------------------------------------===//
1030510308

10306-
void printMachORebaseTable(object::MachOObjectFile *Obj) {
10309+
static void printMachORebaseTable(object::MachOObjectFile *Obj) {
1030710310
outs() << "segment section address type\n";
1030810311
Error Err = Error::success();
1030910312
for (const object::MachORebaseEntry &Entry : Obj->rebaseTable(Err)) {
@@ -10345,7 +10348,7 @@ static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
1034510348
// bind table dumping
1034610349
//===----------------------------------------------------------------------===//
1034710350

10348-
void printMachOBindTable(object::MachOObjectFile *Obj) {
10351+
static void printMachOBindTable(object::MachOObjectFile *Obj) {
1034910352
// Build table of sections so names can used in final output.
1035010353
outs() << "segment section address type "
1035110354
"addend dylib symbol\n";
@@ -10376,7 +10379,7 @@ void printMachOBindTable(object::MachOObjectFile *Obj) {
1037610379
// lazy bind table dumping
1037710380
//===----------------------------------------------------------------------===//
1037810381

10379-
void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
10382+
static void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
1038010383
outs() << "segment section address "
1038110384
"dylib symbol\n";
1038210385
Error Err = Error::success();
@@ -10401,7 +10404,7 @@ void printMachOLazyBindTable(object::MachOObjectFile *Obj) {
1040110404
// weak bind table dumping
1040210405
//===----------------------------------------------------------------------===//
1040310406

10404-
void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
10407+
static void printMachOWeakBindTable(object::MachOObjectFile *Obj) {
1040510408
outs() << "segment section address "
1040610409
"type addend symbol\n";
1040710410
Error Err = Error::success();
@@ -10451,7 +10454,7 @@ static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
1045110454
return !name.empty() ? name.data() : nullptr;
1045210455
}
1045310456

10454-
void printLazyBindTable(ObjectFile *o) {
10457+
void objdump::printLazyBindTable(ObjectFile *o) {
1045510458
outs() << "Lazy bind table:\n";
1045610459
if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1045710460
printMachOLazyBindTable(MachO);
@@ -10461,7 +10464,7 @@ void printLazyBindTable(ObjectFile *o) {
1046110464
"for Mach-O executable files.\n";
1046210465
}
1046310466

10464-
void printWeakBindTable(ObjectFile *o) {
10467+
void objdump::printWeakBindTable(ObjectFile *o) {
1046510468
outs() << "Weak bind table:\n";
1046610469
if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1046710470
printMachOWeakBindTable(MachO);
@@ -10471,7 +10474,7 @@ void printWeakBindTable(ObjectFile *o) {
1047110474
"for Mach-O executable files.\n";
1047210475
}
1047310476

10474-
void printExportsTrie(const ObjectFile *o) {
10477+
void objdump::printExportsTrie(const ObjectFile *o) {
1047510478
outs() << "Exports trie:\n";
1047610479
if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1047710480
printMachOExportsTrie(MachO);
@@ -10481,7 +10484,7 @@ void printExportsTrie(const ObjectFile *o) {
1048110484
"for Mach-O executable files.\n";
1048210485
}
1048310486

10484-
void printRebaseTable(ObjectFile *o) {
10487+
void objdump::printRebaseTable(ObjectFile *o) {
1048510488
outs() << "Rebase table:\n";
1048610489
if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1048710490
printMachORebaseTable(MachO);
@@ -10491,7 +10494,7 @@ void printRebaseTable(ObjectFile *o) {
1049110494
"for Mach-O executable files.\n";
1049210495
}
1049310496

10494-
void printBindTable(ObjectFile *o) {
10497+
void objdump::printBindTable(ObjectFile *o) {
1049510498
outs() << "Bind table:\n";
1049610499
if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1049710500
printMachOBindTable(MachO);
@@ -10500,4 +10503,3 @@ void printBindTable(ObjectFile *o) {
1050010503
<< "This operation is only currently supported "
1050110504
"for Mach-O executable files.\n";
1050210505
}
10503-
} // namespace llvm

llvm/tools/llvm-objdump/MachODump.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@
99
#ifndef LLVM_TOOLS_LLVM_OBJDUMP_MACHODUMP_H
1010
#define LLVM_TOOLS_LLVM_OBJDUMP_MACHODUMP_H
1111

12+
#include "llvm/ADT/SmallVector.h"
1213
#include "llvm/Support/CommandLine.h"
1314

1415
namespace llvm {
16+
17+
class Error;
18+
class StringRef;
19+
20+
namespace object {
21+
class MachOObjectFile;
22+
class MachOUniversalBinary;
23+
class ObjectFile;
24+
class RelocationRef;
25+
} // namespace object
26+
1527
namespace objdump {
1628

1729
// MachO specific options
@@ -31,6 +43,23 @@ extern cl::opt<bool> Rebase;
3143
extern cl::opt<bool> UniversalHeaders;
3244
extern cl::opt<bool> WeakBind;
3345

46+
Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
47+
const object::RelocationRef &RelRef,
48+
llvm::SmallVectorImpl<char> &Result);
49+
50+
void parseInputMachO(StringRef Filename);
51+
void parseInputMachO(object::MachOUniversalBinary *UB);
52+
53+
void printMachOUnwindInfo(const object::MachOObjectFile *O);
54+
void printMachOFileHeader(const object::ObjectFile *O);
55+
void printMachOLoadCommands(const object::ObjectFile *O);
56+
57+
void printExportsTrie(const object::ObjectFile *O);
58+
void printRebaseTable(object::ObjectFile *O);
59+
void printBindTable(object::ObjectFile *O);
60+
void printLazyBindTable(object::ObjectFile *O);
61+
void printWeakBindTable(object::ObjectFile *O);
62+
3463
} // namespace objdump
3564
} // namespace llvm
3665

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,8 @@ static cl::alias WideShort("w", cl::Grouping, cl::aliasopt(Wide));
345345
static cl::extrahelp
346346
HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
347347

348-
namespace llvm {
349-
350348
static StringSet<> DisasmSymbolSet;
351-
StringSet<> FoundSectionSet;
349+
StringSet<> objdump::FoundSectionSet;
352350
static StringRef ToolName;
353351

354352
namespace {
@@ -386,6 +384,8 @@ static FilterResult checkSectionFilter(object::SectionRef S) {
386384
/*IncrementIndex=*/true};
387385
}
388386

387+
namespace llvm {
388+
389389
SectionFilter ToolSectionFilter(object::ObjectFile const &O, uint64_t *Idx) {
390390
// Start at UINT64_MAX so that the first index returned after an increment is
391391
// zero (after the unsigned wrap).

llvm/tools/llvm-objdump/llvm-objdump.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
#ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
99
#define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
1010

11+
#include "llvm/ADT/StringSet.h"
1112
#include "llvm/DebugInfo/DIContext.h"
13+
#include "llvm/Object/Archive.h"
1214
#include "llvm/Support/CommandLine.h"
1315
#include "llvm/Support/Compiler.h"
1416
#include "llvm/Support/DataTypes.h"
15-
#include "llvm/Object/Archive.h"
1617

1718
namespace llvm {
1819
class StringRef;
@@ -46,6 +47,8 @@ extern cl::opt<bool> SymbolTable;
4647
extern cl::opt<std::string> TripleName;
4748
extern cl::opt<bool> UnwindInfo;
4849

50+
extern StringSet<> FoundSectionSet;
51+
4952
} // namespace objdump
5053

5154
typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
@@ -118,32 +121,14 @@ Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
118121
Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
119122
const object::RelocationRef &RelRef,
120123
llvm::SmallVectorImpl<char> &Result);
121-
Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
122-
const object::RelocationRef &RelRef,
123-
llvm::SmallVectorImpl<char> &Result);
124124

125125
uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
126126

127127
bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
128-
void parseInputMachO(StringRef Filename);
129-
void parseInputMachO(object::MachOUniversalBinary *UB);
130-
void printMachOUnwindInfo(const object::MachOObjectFile *O);
131-
void printMachOExportsTrie(const object::MachOObjectFile *O);
132-
void printMachORebaseTable(object::MachOObjectFile *O);
133-
void printMachOBindTable(object::MachOObjectFile *O);
134-
void printMachOLazyBindTable(object::MachOObjectFile *O);
135-
void printMachOWeakBindTable(object::MachOObjectFile *O);
136128
void printELFFileHeader(const object::ObjectFile *O);
137129
void printELFDynamicSection(const object::ObjectFile *Obj);
138130
void printELFSymbolVersionInfo(const object::ObjectFile *Obj);
139-
void printMachOFileHeader(const object::ObjectFile *O);
140-
void printMachOLoadCommands(const object::ObjectFile *O);
141131
void printWasmFileHeader(const object::ObjectFile *O);
142-
void printExportsTrie(const object::ObjectFile *O);
143-
void printRebaseTable(object::ObjectFile *O);
144-
void printBindTable(object::ObjectFile *O);
145-
void printLazyBindTable(object::ObjectFile *O);
146-
void printWeakBindTable(object::ObjectFile *O);
147132
void printRawClangAST(const object::ObjectFile *O);
148133
void printRelocations(const object::ObjectFile *O);
149134
void printDynamicRelocations(const object::ObjectFile *O);

0 commit comments

Comments
 (0)