-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libomptarget] Support BE ELF files in plugins-nextgen #85246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+90
−50
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,18 +36,10 @@ bool utils::elf::isELF(StringRef Buffer) { | |
} | ||
} | ||
|
||
Expected<bool> utils::elf::checkMachine(StringRef Object, uint16_t EMachine) { | ||
assert(isELF(Object) && "Input is not an ELF!"); | ||
|
||
Expected<ELF64LEObjectFile> ElfOrErr = | ||
ELF64LEObjectFile::create(MemoryBufferRef(Object, /*Identifier=*/""), | ||
/*InitContent=*/false); | ||
if (!ElfOrErr) | ||
return ElfOrErr.takeError(); | ||
|
||
const auto Header = ElfOrErr->getELFFile().getHeader(); | ||
if (Header.e_ident[EI_CLASS] != ELFCLASS64) | ||
return createError("Only 64-bit ELF files are supported"); | ||
template <class ELFT> | ||
static Expected<bool> | ||
checkMachineImpl(const object::ELFObjectFile<ELFT> &ELFObj, uint16_t EMachine) { | ||
const auto Header = ELFObj.getELFFile().getHeader(); | ||
if (Header.e_type != ET_EXEC && Header.e_type != ET_DYN) | ||
return createError("Only executable ELF files are supported"); | ||
|
||
|
@@ -71,6 +63,25 @@ Expected<bool> utils::elf::checkMachine(StringRef Object, uint16_t EMachine) { | |
return Header.e_machine == EMachine; | ||
} | ||
|
||
Expected<bool> utils::elf::checkMachine(StringRef Object, uint16_t EMachine) { | ||
assert(isELF(Object) && "Input is not an ELF!"); | ||
|
||
Expected<std::unique_ptr<ObjectFile>> ElfOrErr = | ||
ObjectFile::createELFObjectFile( | ||
MemoryBufferRef(Object, /*Identifier=*/""), | ||
/*InitContent=*/false); | ||
if (!ElfOrErr) | ||
return ElfOrErr.takeError(); | ||
|
||
if (const ELF64LEObjectFile *ELFObj = | ||
dyn_cast<ELF64LEObjectFile>(&**ElfOrErr)) | ||
return checkMachineImpl(*ELFObj, EMachine); | ||
if (const ELF64BEObjectFile *ELFObj = | ||
dyn_cast<ELF64BEObjectFile>(&**ElfOrErr)) | ||
return checkMachineImpl(*ELFObj, EMachine); | ||
return createError("Only 64-bit ELF files are supported"); | ||
} | ||
|
||
template <class ELFT> | ||
static Expected<const typename ELFT::Sym *> | ||
getSymbolFromGnuHashTable(StringRef Name, const typename ELFT::GnuHash &HashTab, | ||
|
@@ -138,9 +149,10 @@ getSymbolFromSysVHashTable(StringRef Name, const typename ELFT::Hash &HashTab, | |
} | ||
|
||
template <class ELFT> | ||
static Expected<const typename ELFT::Sym *> | ||
getHashTableSymbol(const ELFFile<ELFT> &Elf, const typename ELFT::Shdr &Sec, | ||
StringRef Name) { | ||
static Expected<std::optional<ELFSymbolRef>> | ||
getHashTableSymbol(const ELFObjectFile<ELFT> &ELFObj, | ||
const typename ELFT::Shdr &Sec, StringRef Name) { | ||
const ELFFile<ELFT> &Elf = ELFObj.getELFFile(); | ||
if (Sec.sh_type != ELF::SHT_HASH && Sec.sh_type != ELF::SHT_GNU_HASH) | ||
return createError( | ||
"invalid sh_type for hash table, expected SHT_HASH or SHT_GNU_HASH"); | ||
|
@@ -179,7 +191,10 @@ getHashTableSymbol(const ELFFile<ELFT> &Elf, const typename ELFT::Shdr &Sec, | |
sizeof(typename ELFT::Word) * HashTab->nbuckets + | ||
sizeof(typename ELFT::Word) * (SymTab.size() - HashTab->symndx)) | ||
return createError("section has invalid sh_size: " + Twine(Sec.sh_size)); | ||
return getSymbolFromGnuHashTable<ELFT>(Name, *HashTab, SymTab, StrTab); | ||
auto Sym = getSymbolFromGnuHashTable<ELFT>(Name, *HashTab, SymTab, StrTab); | ||
if (!Sym) | ||
return Sym.takeError(); | ||
return ELFObj.toSymbolRef(*SymTabOrErr, *Sym - &SymTab[0]); | ||
} | ||
|
||
// If this is a Sys-V hash table we verify its size and search the symbol | ||
|
@@ -197,16 +212,20 @@ getHashTableSymbol(const ELFFile<ELFT> &Elf, const typename ELFT::Shdr &Sec, | |
sizeof(typename ELFT::Word) * HashTab->nchain) | ||
return createError("section has invalid sh_size: " + Twine(Sec.sh_size)); | ||
|
||
return getSymbolFromSysVHashTable<ELFT>(Name, *HashTab, SymTab, StrTab); | ||
auto Sym = getSymbolFromSysVHashTable<ELFT>(Name, *HashTab, SymTab, StrTab); | ||
if (!Sym) | ||
return Sym.takeError(); | ||
return ELFObj.toSymbolRef(*SymTabOrErr, *Sym - &SymTab[0]); | ||
} | ||
|
||
return nullptr; | ||
return std::nullopt; | ||
} | ||
|
||
template <class ELFT> | ||
static Expected<const typename ELFT::Sym *> | ||
getSymTableSymbol(const ELFFile<ELFT> &Elf, const typename ELFT::Shdr &Sec, | ||
StringRef Name) { | ||
static Expected<std::optional<ELFSymbolRef>> | ||
getSymTableSymbol(const ELFObjectFile<ELFT> &ELFObj, | ||
const typename ELFT::Shdr &Sec, StringRef Name) { | ||
const ELFFile<ELFT> &Elf = ELFObj.getELFFile(); | ||
if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM) | ||
return createError( | ||
"invalid sh_type for hash table, expected SHT_SYMTAB or SHT_DYNSYM"); | ||
|
@@ -226,13 +245,14 @@ getSymTableSymbol(const ELFFile<ELFT> &Elf, const typename ELFT::Shdr &Sec, | |
|
||
for (const typename ELFT::Sym &Sym : SymTab) | ||
if (StrTab.drop_front(Sym.st_name).data() == Name) | ||
return &Sym; | ||
return ELFObj.toSymbolRef(&Sec, &Sym - &SymTab[0]); | ||
|
||
return nullptr; | ||
return std::nullopt; | ||
} | ||
|
||
Expected<const typename ELF64LE::Sym *> | ||
utils::elf::getSymbol(const ELFObjectFile<ELF64LE> &ELFObj, StringRef Name) { | ||
template <class ELFT> | ||
static Expected<std::optional<ELFSymbolRef>> | ||
getSymbolImpl(const ELFObjectFile<ELFT> &ELFObj, StringRef Name) { | ||
// First try to look up the symbol via the hash table. | ||
for (ELFSectionRef Sec : ELFObj.sections()) { | ||
if (Sec.getType() != SHT_HASH && Sec.getType() != SHT_GNU_HASH) | ||
|
@@ -241,8 +261,7 @@ utils::elf::getSymbol(const ELFObjectFile<ELF64LE> &ELFObj, StringRef Name) { | |
auto HashTabOrErr = ELFObj.getELFFile().getSection(Sec.getIndex()); | ||
if (!HashTabOrErr) | ||
return HashTabOrErr.takeError(); | ||
return getHashTableSymbol<ELF64LE>(ELFObj.getELFFile(), **HashTabOrErr, | ||
Name); | ||
return getHashTableSymbol<ELFT>(ELFObj, **HashTabOrErr, Name); | ||
} | ||
|
||
// If this is an executable file check the entire standard symbol table. | ||
|
@@ -253,16 +272,31 @@ utils::elf::getSymbol(const ELFObjectFile<ELF64LE> &ELFObj, StringRef Name) { | |
auto SymTabOrErr = ELFObj.getELFFile().getSection(Sec.getIndex()); | ||
if (!SymTabOrErr) | ||
return SymTabOrErr.takeError(); | ||
return getSymTableSymbol<ELF64LE>(ELFObj.getELFFile(), **SymTabOrErr, Name); | ||
return getSymTableSymbol<ELFT>(ELFObj, **SymTabOrErr, Name); | ||
} | ||
|
||
return nullptr; | ||
return std::nullopt; | ||
} | ||
|
||
Expected<const void *> utils::elf::getSymbolAddress( | ||
const object::ELFObjectFile<object::ELF64LE> &ELFObj, | ||
const object::ELF64LE::Sym &Symbol) { | ||
const ELFFile<ELF64LE> &ELFFile = ELFObj.getELFFile(); | ||
Expected<std::optional<ELFSymbolRef>> | ||
utils::elf::getSymbol(const ObjectFile &Obj, StringRef Name) { | ||
if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(&Obj)) | ||
return getSymbolImpl(*ELFObj, Name); | ||
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(&Obj)) | ||
return getSymbolImpl(*ELFObj, Name); | ||
Comment on lines
+283
to
+286
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you not just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the ObjectFile wrapper so we can call toSymbolRef, as discussed above. |
||
return createError("Only 64-bit ELF files are supported"); | ||
} | ||
|
||
template <class ELFT> | ||
static Expected<const void *> | ||
getSymbolAddressImpl(const ELFObjectFile<ELFT> &ELFObj, | ||
const ELFSymbolRef &SymRef) { | ||
const ELFFile<ELFT> &ELFFile = ELFObj.getELFFile(); | ||
|
||
auto SymOrErr = ELFObj.getSymbol(SymRef.getRawDataRefImpl()); | ||
if (!SymOrErr) | ||
return SymOrErr.takeError(); | ||
const auto &Symbol = **SymOrErr; | ||
|
||
auto SecOrErr = ELFFile.getSection(Symbol.st_shndx); | ||
if (!SecOrErr) | ||
|
@@ -283,3 +317,13 @@ Expected<const void *> utils::elf::getSymbolAddress( | |
|
||
return ELFFile.base() + Offset; | ||
} | ||
|
||
Expected<const void *> | ||
utils::elf::getSymbolAddress(const ELFSymbolRef &SymRef) { | ||
const ObjectFile *Obj = SymRef.getObject(); | ||
if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) | ||
return getSymbolAddressImpl(*ELFObj, SymRef); | ||
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) | ||
return getSymbolAddressImpl(*ELFObj, SymRef); | ||
return createError("Only 64-bit ELF files are supported"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this part changed? It's internal so it should be using the "actual" ELF format and not the Object file wrapper I would assume.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to at some point convert a ELFT::Sym pointer into a ELFSymbolRef. This requires access to both the symbol table (section header) containing the symbol, and the Object file wrapper (which provides the ELFObj.toSymbolRef routine that actually creates the ELFSymbolRef. We could move the call to toSymbolRef to the caller, but there we don't have ready access to the symbol table, so we'd have to re-compute it again. Doing it inside getHashTableSymbol seems the lesser change to me.