[llvm] 5f19fb5 - [NFC] add new function is64Bit for SymbolicFile class
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 6 07:44:05 PST 2023
Author: zhijian
Date: 2023-02-06T10:43:29-05:00
New Revision: 5f19fb5af53765baaebec61880e53e806a97a974
URL: https://github.com/llvm/llvm-project/commit/5f19fb5af53765baaebec61880e53e806a97a974
DIFF: https://github.com/llvm/llvm-project/commit/5f19fb5af53765baaebec61880e53e806a97a974.diff
LOG: [NFC] add new function is64Bit for SymbolicFile class
Summary:
since the class 'SymbolicFile ' do not have a is64Bit() API , when we need to check whether a SymbolicFile object is 64bit or not. we need to write a function to do it, it maybe cause duplication code.
Reviewers: James Henderson, Fangrui Song
Differential Revision: https://reviews.llvm.org/D143097
Added:
Modified:
llvm/include/llvm/Object/COFF.h
llvm/include/llvm/Object/COFFImportFile.h
llvm/include/llvm/Object/ELFObjectFile.h
llvm/include/llvm/Object/IRObjectFile.h
llvm/include/llvm/Object/MachO.h
llvm/include/llvm/Object/SymbolicFile.h
llvm/include/llvm/Object/TapiFile.h
llvm/include/llvm/Object/Wasm.h
llvm/include/llvm/Object/XCOFFObjectFile.h
llvm/tools/llvm-ar/llvm-ar.cpp
llvm/tools/llvm-nm/llvm-nm.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h
index 89e12f465d170..4afd11f5a1965 100644
--- a/llvm/include/llvm/Object/COFF.h
+++ b/llvm/include/llvm/Object/COFF.h
@@ -969,6 +969,8 @@ class COFFObjectFile : public ObjectFile {
section_iterator section_begin() const override;
section_iterator section_end() const override;
+ bool is64Bit() const override { return false; }
+
const coff_section *getCOFFSection(const SectionRef &Section) const;
COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
diff --git a/llvm/include/llvm/Object/COFFImportFile.h b/llvm/include/llvm/Object/COFFImportFile.h
index f8f0e0343b223..3d148112dcbb6 100644
--- a/llvm/include/llvm/Object/COFFImportFile.h
+++ b/llvm/include/llvm/Object/COFFImportFile.h
@@ -56,6 +56,8 @@ class COFFImportFile : public SymbolicFile {
return BasicSymbolRef(Symb, this);
}
+ bool is64Bit() const override { return false; }
+
const coff_import_header *getCOFFImportHeader() const {
return reinterpret_cast<const object::coff_import_header *>(
Data.getBufferStart());
diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h
index 8baf6f4c5af37..73e199fcd3565 100644
--- a/llvm/include/llvm/Object/ELFObjectFile.h
+++ b/llvm/include/llvm/Object/ELFObjectFile.h
@@ -432,6 +432,8 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
basic_symbol_iterator symbol_begin() const override;
basic_symbol_iterator symbol_end() const override;
+ bool is64Bit() const override { return getBytesInAddress() == 8; }
+
elf_symbol_iterator dynamic_symbol_begin() const;
elf_symbol_iterator dynamic_symbol_end() const;
diff --git a/llvm/include/llvm/Object/IRObjectFile.h b/llvm/include/llvm/Object/IRObjectFile.h
index ee9911025a17b..55d910fe970e8 100644
--- a/llvm/include/llvm/Object/IRObjectFile.h
+++ b/llvm/include/llvm/Object/IRObjectFile.h
@@ -37,7 +37,9 @@ class IRObjectFile : public SymbolicFile {
Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
basic_symbol_iterator symbol_begin() const override;
basic_symbol_iterator symbol_end() const override;
-
+ bool is64Bit() const override {
+ return Triple(getTargetTriple()).isArch64Bit();
+ }
StringRef getTargetTriple() const;
static bool classof(const Binary *v) {
diff --git a/llvm/include/llvm/Object/MachO.h b/llvm/include/llvm/Object/MachO.h
index 56e7c8580b4e9..47a4f1ee2b41c 100644
--- a/llvm/include/llvm/Object/MachO.h
+++ b/llvm/include/llvm/Object/MachO.h
@@ -503,6 +503,8 @@ class MachOObjectFile : public ObjectFile {
basic_symbol_iterator symbol_begin() const override;
basic_symbol_iterator symbol_end() const override;
+ bool is64Bit() const override;
+
// MachO specific.
symbol_iterator getSymbolByIndex(unsigned Index) const;
uint64_t getSymbolIndex(DataRefImpl Symb) const;
@@ -735,7 +737,7 @@ class MachOObjectFile : public ObjectFile {
ArrayRef<uint8_t> getUuid() const;
StringRef getStringTableData() const;
- bool is64Bit() const;
+
void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
diff --git a/llvm/include/llvm/Object/SymbolicFile.h b/llvm/include/llvm/Object/SymbolicFile.h
index ea51afce5d2ad..b13588c147d9b 100644
--- a/llvm/include/llvm/Object/SymbolicFile.h
+++ b/llvm/include/llvm/Object/SymbolicFile.h
@@ -158,6 +158,8 @@ class SymbolicFile : public Binary {
virtual basic_symbol_iterator symbol_end() const = 0;
+ virtual bool is64Bit() const = 0;
+
// convenience wrappers.
using basic_symbol_iterator_range = iterator_range<basic_symbol_iterator>;
basic_symbol_iterator_range symbols() const {
diff --git a/llvm/include/llvm/Object/TapiFile.h b/llvm/include/llvm/Object/TapiFile.h
index 410e58dceaf4e..24599991a9832 100644
--- a/llvm/include/llvm/Object/TapiFile.h
+++ b/llvm/include/llvm/Object/TapiFile.h
@@ -50,7 +50,7 @@ class TapiFile : public SymbolicFile {
static bool classof(const Binary *v) { return v->isTapiFile(); }
- bool is64Bit() { return MachO::is64Bit(Arch); }
+ bool is64Bit() const override { return MachO::is64Bit(Arch); }
private:
struct Symbol {
diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h
index 3c8c8a21bf1d7..8dd8918ddf21d 100644
--- a/llvm/include/llvm/Object/Wasm.h
+++ b/llvm/include/llvm/Object/Wasm.h
@@ -164,6 +164,8 @@ class WasmObjectFile : public ObjectFile {
basic_symbol_iterator symbol_end() const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
+ bool is64Bit() const override { return false; }
+
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h
index 14247804af450..2377d17d59390 100644
--- a/llvm/include/llvm/Object/XCOFFObjectFile.h
+++ b/llvm/include/llvm/Object/XCOFFObjectFile.h
@@ -576,7 +576,7 @@ class XCOFFObjectFile : public ObjectFile {
Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
basic_symbol_iterator symbol_begin() const override;
basic_symbol_iterator symbol_end() const override;
-
+ bool is64Bit() const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
@@ -625,7 +625,7 @@ class XCOFFObjectFile : public ObjectFile {
bool isRelocatableObject() const override;
// Below here is the non-inherited interface.
- bool is64Bit() const;
+
Expected<StringRef> getRawData(const char *Start, uint64_t Size,
StringRef Name) const;
diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index 12f3196a9844a..a6ed8211b99e3 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/tools/llvm-ar/llvm-ar.cpp
@@ -18,15 +18,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/ArchiveWriter.h"
-#include "llvm/Object/COFFImportFile.h"
-#include "llvm/Object/ELFObjectFile.h"
-#include "llvm/Object/IRObjectFile.h"
-#include "llvm/Object/MachO.h"
-#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/SymbolicFile.h"
-#include "llvm/Object/TapiFile.h"
-#include "llvm/Object/Wasm.h"
-#include "llvm/Object/XCOFFObjectFile.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h"
@@ -646,31 +638,12 @@ static bool shouldCreateArchive(ArchiveOperation Op) {
llvm_unreachable("Missing entry in covered switch.");
}
-static bool is64BitSymbolicFile(SymbolicFile &Obj) {
- if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj))
- return Triple(IRObj->getTargetTriple()).isArch64Bit();
- if (isa<COFFObjectFile>(Obj) || isa<COFFImportFile>(Obj))
- return false;
- if (XCOFFObjectFile *XCOFFObj = dyn_cast<XCOFFObjectFile>(&Obj))
- return XCOFFObj->is64Bit();
- if (isa<WasmObjectFile>(Obj))
- return false;
- if (TapiFile *Tapi = dyn_cast<TapiFile>(&Obj))
- return Tapi->is64Bit();
- if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
- return MachO->is64Bit();
- if (ELFObjectFileBase *ElfO = dyn_cast<ELFObjectFileBase>(&Obj))
- return ElfO->getBytesInAddress() == 8;
-
- fail("unsupported file format");
-}
-
static bool isValidInBitMode(Binary &Bin) {
if (BitMode == BitModeTy::Bit32_64 || BitMode == BitModeTy::Any)
return true;
if (SymbolicFile *SymFile = dyn_cast<SymbolicFile>(&Bin)) {
- bool Is64Bit = is64BitSymbolicFile(*SymFile);
+ bool Is64Bit = SymFile->is64Bit();
if ((Is64Bit && (BitMode == BitModeTy::Bit32)) ||
(!Is64Bit && (BitMode == BitModeTy::Bit64)))
return false;
diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index 55319d0e4c720..83a132796fde6 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/tools/llvm-nm/llvm-nm.cpp
@@ -293,22 +293,6 @@ bool operator==(const NMSymbol &A, const NMSymbol &B) {
}
} // anonymous namespace
-static char isSymbolList64Bit(SymbolicFile &Obj) {
- if (auto *IRObj = dyn_cast<IRObjectFile>(&Obj))
- return Triple(IRObj->getTargetTriple()).isArch64Bit();
- if (isa<COFFObjectFile>(Obj) || isa<COFFImportFile>(Obj))
- return false;
- if (XCOFFObjectFile *XCOFFObj = dyn_cast<XCOFFObjectFile>(&Obj))
- return XCOFFObj->is64Bit();
- if (isa<WasmObjectFile>(Obj))
- return false;
- if (TapiFile *Tapi = dyn_cast<TapiFile>(&Obj))
- return Tapi->is64Bit();
- if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(&Obj))
- return MachO->is64Bit();
- return cast<ELFObjectFileBase>(Obj).getBytesInAddress() == 8;
-}
-
static StringRef CurrentFilename;
static char getSymbolNMTypeChar(IRObjectFile &Obj, basic_symbol_iterator I);
@@ -722,7 +706,7 @@ static void printSymbolList(SymbolicFile &Obj,
outs() << '\n' << CurrentFilename << ":\n";
} else if (OutputFormat == sysv) {
outs() << "\n\nSymbols from " << CurrentFilename << ":\n\n";
- if (isSymbolList64Bit(Obj))
+ if (Obj.is64Bit())
outs() << "Name Value Class Type"
<< " Size Line Section\n";
else
@@ -732,7 +716,7 @@ static void printSymbolList(SymbolicFile &Obj,
}
const char *printBlanks, *printDashes, *printFormat;
- if (isSymbolList64Bit(Obj)) {
+ if (Obj.is64Bit()) {
printBlanks = " ";
printDashes = "----------------";
switch (AddressRadix) {
@@ -1671,8 +1655,8 @@ static bool shouldDump(SymbolicFile &Obj) {
!isa<IRObjectFile>(Obj))
return true;
- return isSymbolList64Bit(Obj) ? BitMode != BitModeTy::Bit32
- : BitMode != BitModeTy::Bit64;
+ return Obj.is64Bit() ? BitMode != BitModeTy::Bit32
+ : BitMode != BitModeTy::Bit64;
}
static void getXCOFFExports(XCOFFObjectFile *XCOFFObj,
More information about the llvm-commits
mailing list