Split out the ELF kind from the InputFile Kind.

There were at least two issues with having them together:
* For compatibility checks, we only want to look at the ELF kind.
* Adding support for shared libraries should introduce one InputFile kind,
  not 4.

llvm-svn: 246707
This commit is contained in:
Rafael Espindola 2015-09-02 20:43:43 +00:00
parent 86fdc85181
commit 905ad3442d
4 changed files with 36 additions and 29 deletions

View File

@ -99,17 +99,17 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
// Write the result.
ObjectFileBase &FirstObj = *Symtab.ObjectFiles[0];
switch (FirstObj.kind()) {
case InputFile::Object32LEKind:
switch (FirstObj.getELFKind()) {
case ELF32LEKind:
writeResult<object::ELF32LE>(&Symtab);
return;
case InputFile::Object32BEKind:
case ELF32BEKind:
writeResult<object::ELF32BE>(&Symtab);
return;
case InputFile::Object64LEKind:
case ELF64LEKind:
writeResult<object::ELF64LE>(&Symtab);
return;
case InputFile::Object64BEKind:
case ELF64BEKind:
writeResult<object::ELF64BE>(&Symtab);
return;
}

View File

@ -20,7 +20,7 @@ using namespace lld::elf2;
template <class ELFT>
bool ObjectFile<ELFT>::isCompatibleWith(const ObjectFileBase &Other) const {
if (kind() != Other.kind())
if (getELFKind() != Other.getELFKind())
return false;
return getObj()->getHeader()->e_machine ==
cast<ObjectFile<ELFT>>(Other).getObj()->getHeader()->e_machine;

View File

@ -23,7 +23,7 @@ class SymbolBody;
// The root class of input files.
class InputFile {
public:
enum Kind { Object32LEKind, Object32BEKind, Object64LEKind, Object64BEKind };
enum Kind { ObjectKind };
Kind kind() const { return FileKind; }
virtual ~InputFile() {}
@ -43,24 +43,27 @@ private:
const Kind FileKind;
};
enum ELFKind { ELF32LEKind, ELF32BEKind, ELF64LEKind, ELF64BEKind };
// .o file.
class ObjectFileBase : public InputFile {
public:
explicit ObjectFileBase(Kind K, MemoryBufferRef M) : InputFile(K, M) {}
static bool classof(const InputFile *F) {
Kind K = F->kind();
return K >= Object32LEKind && K <= Object64BEKind;
}
explicit ObjectFileBase(ELFKind EKind, MemoryBufferRef M)
: InputFile(ObjectKind, M), EKind(EKind) {}
static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
ArrayRef<SymbolBody *> getSymbols() override { return SymbolBodies; }
virtual bool isCompatibleWith(const ObjectFileBase &Other) const = 0;
ELFKind getELFKind() const { return EKind; }
protected:
// List of all symbols referenced or defined by this file.
std::vector<SymbolBody *> SymbolBodies;
llvm::BumpPtrAllocator Alloc;
const ELFKind EKind;
};
template <class ELFT> class ObjectFile : public ObjectFileBase {
@ -72,20 +75,24 @@ template <class ELFT> class ObjectFile : public ObjectFileBase {
public:
bool isCompatibleWith(const ObjectFileBase &Other) const override;
static Kind getKind() {
static ELFKind getStaticELFKind() {
if (!ELFT::Is64Bits) {
if (ELFT::TargetEndianness == llvm::support::little)
return Object32LEKind;
return Object32BEKind;
return ELF32LEKind;
return ELF32BEKind;
}
if (ELFT::TargetEndianness == llvm::support::little)
return Object64LEKind;
return Object64BEKind;
return ELF64LEKind;
return ELF64BEKind;
}
static bool classof(const InputFile *F) { return F->kind() == getKind(); }
static bool classof(const InputFile *F) {
return F->kind() == ObjectKind &&
cast<ObjectFileBase>(F)->getELFKind() == getStaticELFKind();
}
explicit ObjectFile(MemoryBufferRef M) : ObjectFileBase(getKind(), M) {}
explicit ObjectFile(MemoryBufferRef M)
: ObjectFileBase(getStaticELFKind(), M) {}
void parse() override;
// Returns the underying ELF file.

View File

@ -38,17 +38,17 @@ void SymbolTable::addObject(ObjectFileBase *File) {
if (!Old.isCompatibleWith(*File))
error(Twine(Old.getName() + " is incompatible with " + File->getName()));
} else {
switch (File->kind()) {
case InputFile::Object32LEKind:
switch (File->getELFKind()) {
case ELF32LEKind:
init<ELF32LE>();
break;
case InputFile::Object32BEKind:
case ELF32BEKind:
init<ELF32BE>();
break;
case InputFile::Object64LEKind:
case ELF64LEKind:
init<ELF64LE>();
break;
case InputFile::Object64BEKind:
case ELF64BEKind:
init<ELF64BE>();
break;
}
@ -56,17 +56,17 @@ void SymbolTable::addObject(ObjectFileBase *File) {
ObjectFiles.emplace_back(File);
for (SymbolBody *Body : File->getSymbols()) {
switch (File->kind()) {
case InputFile::Object32LEKind:
switch (File->getELFKind()) {
case ELF32LEKind:
resolve<ELF32LE>(Body);
break;
case InputFile::Object32BEKind:
case ELF32BEKind:
resolve<ELF32BE>(Body);
break;
case InputFile::Object64LEKind:
case ELF64LEKind:
resolve<ELF64LE>(Body);
break;
case InputFile::Object64BEKind:
case ELF64BEKind:
resolve<ELF64BE>(Body);
break;
}