Skip to content

Commit 6292d40

Browse files
zeroomegacyndyishida
authored andcommitted
[elfabi] Move llvm-elfabi related code to InterfaceStub library
This change moves elfabi related code to llvm/InterfaceStub library so it can be shared by multiple llvm tools without causing cyclic dependencies. Differential Revision: https://reviews.llvm.org/D85678
1 parent 1bad10b commit 6292d40

File tree

18 files changed

+82
-49
lines changed

18 files changed

+82
-49
lines changed

llvm/tools/llvm-elfabi/ELFObjHandler.h renamed to llvm/include/llvm/InterfaceStub/ELFObjHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#ifndef LLVM_TOOLS_ELFABI_ELFOBJHANDLER_H
1414
#define LLVM_TOOLS_ELFABI_ELFOBJHANDLER_H
1515

16+
#include "llvm/InterfaceStub/ELFStub.h"
1617
#include "llvm/Object/ELFObjectFile.h"
1718
#include "llvm/Object/ELFTypes.h"
18-
#include "llvm/TextAPI/ELF/ELFStub.h"
1919

2020
namespace llvm {
2121

llvm/include/llvm/TextAPI/ELF/ELFStub.h renamed to llvm/include/llvm/InterfaceStub/ELFStub.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#include "llvm/BinaryFormat/ELF.h"
1818
#include "llvm/Support/VersionTuple.h"
19-
#include <vector>
2019
#include <set>
20+
#include <vector>
2121

2222
namespace llvm {
2323
namespace elfabi {
@@ -42,15 +42,13 @@ struct ELFSymbol {
4242
bool Undefined;
4343
bool Weak;
4444
Optional<std::string> Warning;
45-
bool operator<(const ELFSymbol &RHS) const {
46-
return Name < RHS.Name;
47-
}
45+
bool operator<(const ELFSymbol &RHS) const { return Name < RHS.Name; }
4846
};
4947

5048
// A cumulative representation of ELF stubs.
5149
// Both textual and binary stubs will read into and write from this object.
5250
class ELFStub {
53-
// TODO: Add support for symbol versioning.
51+
// TODO: Add support for symbol versioning.
5452
public:
5553
VersionTuple TbeVersion;
5654
Optional<std::string> SoName;

llvm/include/llvm/TextAPI/ELF/TBEHandler.h renamed to llvm/include/llvm/InterfaceStub/TBEHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#ifndef LLVM_TEXTAPI_ELF_TBEHANDLER_H
1616
#define LLVM_TEXTAPI_ELF_TBEHANDLER_H
1717

18-
#include "llvm/Support/VersionTuple.h"
1918
#include "llvm/Support/Error.h"
19+
#include "llvm/Support/VersionTuple.h"
2020
#include <memory>
2121

2222
namespace llvm {

llvm/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
add_subdirectory(IR)
55
add_subdirectory(FuzzMutate)
6+
add_subdirectory(InterfaceStub)
67
add_subdirectory(IRReader)
78
add_subdirectory(CodeGen)
89
add_subdirectory(BinaryFormat)

llvm/lib/InterfaceStub/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_llvm_component_library(LLVMInterfaceStub
2+
ELFObjHandler.cpp
3+
ELFStub.cpp
4+
TBEHandler.cpp
5+
6+
ADDITIONAL_HEADER_DIRS
7+
"${LLVM_MAIN_INCLUDE_DIR}/llvm/InterfaceStub"
8+
)

llvm/tools/llvm-elfabi/ELFObjHandler.cpp renamed to llvm/lib/InterfaceStub/ELFObjHandler.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//
77
//===-----------------------------------------------------------------------===/
88

9-
#include "ELFObjHandler.h"
9+
#include "llvm/InterfaceStub/ELFObjHandler.h"
10+
#include "llvm/InterfaceStub/ELFStub.h"
1011
#include "llvm/Object/Binary.h"
1112
#include "llvm/Object/ELFObjectFile.h"
1213
#include "llvm/Object/ELFTypes.h"
1314
#include "llvm/Support/Errc.h"
1415
#include "llvm/Support/Error.h"
1516
#include "llvm/Support/MemoryBuffer.h"
16-
#include "llvm/TextAPI/ELF/ELFStub.h"
1717

1818
using llvm::MemoryBufferRef;
1919
using llvm::object::ELFObjectFile;
@@ -128,19 +128,17 @@ static Error populateDynamic(DynamicEntries &Dyn,
128128
"Couldn't locate dynamic symbol table (no DT_SYMTAB entry)");
129129
}
130130
if (Dyn.SONameOffset.hasValue() && *Dyn.SONameOffset >= Dyn.StrSize) {
131-
return createStringError(
132-
object_error::parse_failed,
133-
"DT_SONAME string offset (0x%016" PRIx64
134-
") outside of dynamic string table",
135-
*Dyn.SONameOffset);
131+
return createStringError(object_error::parse_failed,
132+
"DT_SONAME string offset (0x%016" PRIx64
133+
") outside of dynamic string table",
134+
*Dyn.SONameOffset);
136135
}
137136
for (uint64_t Offset : Dyn.NeededLibNames) {
138137
if (Offset >= Dyn.StrSize) {
139-
return createStringError(
140-
object_error::parse_failed,
141-
"DT_NEEDED string offset (0x%016" PRIx64
142-
") outside of dynamic string table",
143-
Offset);
138+
return createStringError(object_error::parse_failed,
139+
"DT_NEEDED string offset (0x%016" PRIx64
140+
") outside of dynamic string table",
141+
Offset);
144142
}
145143
}
146144

@@ -212,16 +210,16 @@ static Expected<uint64_t> getNumSyms(DynamicEntries &Dyn,
212210
static ELFSymbolType convertInfoToType(uint8_t Info) {
213211
Info = Info & 0xf;
214212
switch (Info) {
215-
case ELF::STT_NOTYPE:
216-
return ELFSymbolType::NoType;
217-
case ELF::STT_OBJECT:
218-
return ELFSymbolType::Object;
219-
case ELF::STT_FUNC:
220-
return ELFSymbolType::Func;
221-
case ELF::STT_TLS:
222-
return ELFSymbolType::TLS;
223-
default:
224-
return ELFSymbolType::Unknown;
213+
case ELF::STT_NOTYPE:
214+
return ELFSymbolType::NoType;
215+
case ELF::STT_OBJECT:
216+
return ELFSymbolType::Object;
217+
case ELF::STT_FUNC:
218+
return ELFSymbolType::Func;
219+
case ELF::STT_TLS:
220+
return ELFSymbolType::TLS;
221+
default:
222+
return ELFSymbolType::Unknown;
225223
}
226224
}
227225

@@ -259,8 +257,8 @@ static ELFSymbol createELFSym(StringRef SymName,
259257
/// @param DynStr StringRef to the dynamic string table.
260258
template <class ELFT>
261259
static Error populateSymbols(ELFStub &TargetStub,
262-
const typename ELFT::SymRange DynSym,
263-
StringRef DynStr) {
260+
const typename ELFT::SymRange DynSym,
261+
StringRef DynStr) {
264262
// Skips the first symbol since it's the NULL symbol.
265263
for (auto RawSym : DynSym.drop_front(1)) {
266264
// If a symbol does not have global or weak binding, ignore it.
@@ -311,7 +309,7 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
311309
if (Error Err = populateDynamic<ELFT>(DynEnt, *DynTable))
312310
return std::move(Err);
313311

314-
// Get pointer to in-memory location of .dynstr section.
312+
// Get pointer to in-memory location of .dynstr section.
315313
Expected<const uint8_t *> DynStrPtr =
316314
ElfFile->toMappedAddr(DynEnt.StrTabAddr);
317315
if (!DynStrPtr)
@@ -355,9 +353,8 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
355353
if (!DynSymPtr)
356354
return appendToError(DynSymPtr.takeError(),
357355
"when locating .dynsym section contents");
358-
Elf_Sym_Range DynSyms =
359-
ArrayRef<Elf_Sym>(reinterpret_cast<const Elf_Sym *>(*DynSymPtr),
360-
*SymCount);
356+
Elf_Sym_Range DynSyms = ArrayRef<Elf_Sym>(
357+
reinterpret_cast<const Elf_Sym *>(*DynSymPtr), *SymCount);
361358
Error SymReadError = populateSymbols<ELFT>(*DestStub, DynSyms, DynStr);
362359
if (SymReadError)
363360
return appendToError(std::move(SymReadError),

llvm/lib/TextAPI/ELF/ELFStub.cpp renamed to llvm/lib/InterfaceStub/ELFStub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===-----------------------------------------------------------------------===/
88

9-
#include "llvm/TextAPI/ELF/ELFStub.h"
9+
#include "llvm/InterfaceStub/ELFStub.h"
1010

1111
using namespace llvm;
1212
using namespace llvm::elfabi;

llvm/lib/InterfaceStub/LLVMBuild.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
;===- ./lib/InterfaceStub/LLVMBuild.txt ------------------------*- Conf -*--===;
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+
;===------------------------------------------------------------------------===;
8+
;
9+
; This is an LLVMBuild description file for the components in this subdirectory.
10+
;
11+
; For more information on the LLVMBuild system, please see:
12+
;
13+
; http://llvm.org/docs/LLVMBuild.html
14+
;
15+
;===------------------------------------------------------------------------===;
16+
17+
[component_0]
18+
type = Library
19+
name = InterfaceStub
20+
parent = Libraries
21+
required_libraries = Object Support

llvm/lib/TextAPI/ELF/TBEHandler.cpp renamed to llvm/lib/InterfaceStub/TBEHandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
//
77
//===-----------------------------------------------------------------------===/
88

9-
#include "llvm/TextAPI/ELF/TBEHandler.h"
10-
#include "llvm/ADT/StringSwitch.h"
9+
#include "llvm/InterfaceStub/TBEHandler.h"
1110
#include "llvm/ADT/StringRef.h"
11+
#include "llvm/ADT/StringSwitch.h"
12+
#include "llvm/InterfaceStub/ELFStub.h"
1213
#include "llvm/Support/Error.h"
1314
#include "llvm/Support/YAMLTraits.h"
14-
#include "llvm/TextAPI/ELF/ELFStub.h"
1515

1616
using namespace llvm;
1717
using namespace llvm::elfabi;

llvm/lib/LLVMBuild.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ subdirectories =
3030
FuzzMutate
3131
LineEditor
3232
Linker
33+
InterfaceStub
3334
IR
3435
IRReader
3536
LTO

llvm/lib/TextAPI/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
add_llvm_component_library(LLVMTextAPI
2-
ELF/ELFStub.cpp
3-
ELF/TBEHandler.cpp
42
MachO/Architecture.cpp
53
MachO/ArchitectureSet.cpp
64
MachO/InterfaceFile.cpp

llvm/tools/llvm-elfabi/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
set(LLVM_LINK_COMPONENTS
2+
InterfaceStub
23
Object
34
Support
45
TextAPI
56
)
67

78
add_llvm_tool(llvm-elfabi
8-
ELFObjHandler.cpp
99
ErrorCollector.cpp
1010
llvm-elfabi.cpp
1111
)

llvm/tools/llvm-elfabi/LLVMBuild.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
type = Tool
1919
name = llvm-elfabi
2020
parent = Tools
21-
required_libraries = Object Support TextAPI
21+
required_libraries = InterfaceStub Object Support TextAPI

llvm/tools/llvm-elfabi/llvm-elfabi.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
//
77
//===-----------------------------------------------------------------------===/
88

9-
#include "ELFObjHandler.h"
109
#include "ErrorCollector.h"
10+
#include "llvm/InterfaceStub/ELFObjHandler.h"
11+
#include "llvm/InterfaceStub/TBEHandler.h"
1112
#include "llvm/Support/CommandLine.h"
1213
#include "llvm/Support/Errc.h"
1314
#include "llvm/Support/FileOutputBuffer.h"
1415
#include "llvm/Support/MemoryBuffer.h"
1516
#include "llvm/Support/Path.h"
16-
#include "llvm/Support/raw_ostream.h"
1717
#include "llvm/Support/WithColor.h"
18-
#include "llvm/TextAPI/ELF/TBEHandler.h"
18+
#include "llvm/Support/raw_ostream.h"
1919
#include <string>
2020

2121
namespace llvm {

llvm/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_subdirectory(Demangle)
2626
add_subdirectory(ExecutionEngine)
2727
add_subdirectory(Frontend)
2828
add_subdirectory(FuzzMutate)
29+
add_subdirectory(InterfaceStub)
2930
add_subdirectory(IR)
3031
add_subdirectory(LineEditor)
3132
add_subdirectory(Linker)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set(LLVM_LINK_COMPONENTS
2+
InterfaceStub
3+
)
4+
5+
add_llvm_unittest(InterfaceStubTests
6+
ELFYAMLTest.cpp
7+
)
8+
9+
target_link_libraries(InterfaceStubTests PRIVATE LLVMTestingSupport)

llvm/unittests/TextAPI/ELFYAMLTest.cpp renamed to llvm/unittests/InterfaceStub/ELFYAMLTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===-----------------------------------------------------------------------===/
88

99
#include "llvm/ADT/StringRef.h"
10-
#include "llvm/TextAPI/ELF/ELFStub.h"
11-
#include "llvm/TextAPI/ELF/TBEHandler.h"
10+
#include "llvm/InterfaceStub/ELFStub.h"
11+
#include "llvm/InterfaceStub/TBEHandler.h"
1212
#include "llvm/Support/Error.h"
1313
#include "llvm/Testing/Support/Error.h"
1414
#include "gtest/gtest.h"

llvm/unittests/TextAPI/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ set(LLVM_LINK_COMPONENTS
33
)
44

55
add_llvm_unittest(TextAPITests
6-
ELFYAMLTest.cpp
76
TextStubV1Tests.cpp
87
TextStubV2Tests.cpp
98
TextStubV3Tests.cpp

0 commit comments

Comments
 (0)