mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/74286 This diff replaces `c10::optional` with the implementation it's based on in https://github.com/akrzemi1/Optional in order to help torch::deploy function without a dependency on `torch` https://github.com/pytorch/pytorch/pull/74002 Test Plan: buck test //caffe2/torch/csrc/deploy:test_deploy Reviewed By: d4l3k Differential Revision: D34907002 fbshipit-source-id: 93a3386f43d1c426f23c6dab5f898ed63b547a5c (cherry picked from commit 182d9f70459f761aaa80a03cc68dd6cb9c07bfae)
57 lines
1.5 KiB
C++
57 lines
1.5 KiB
C++
#include <c10/util/irange.h>
|
|
#include <torch/csrc/deploy/Exception.h>
|
|
#include <torch/csrc/deploy/elf_file.h>
|
|
#include <torch/csrc/deploy/interpreter/Optional.hpp>
|
|
|
|
namespace torch {
|
|
namespace deploy {
|
|
|
|
ElfFile::ElfFile(const char* filename) : memFile_(filename) {
|
|
const char* fileData = memFile_.data();
|
|
ehdr_ = (Elf64_Ehdr*)fileData;
|
|
checkFormat();
|
|
|
|
numSections_ = ehdr_->e_shnum;
|
|
shdrList_ = (Elf64_Shdr*)(fileData + ehdr_->e_shoff);
|
|
|
|
auto strtabSecNo = ehdr_->e_shstrndx;
|
|
MULTIPY_CHECK(
|
|
strtabSecNo >= 0 && strtabSecNo < numSections_,
|
|
"e_shstrndx out of range");
|
|
|
|
strtabSection_ = toSection(&shdrList_[strtabSecNo]);
|
|
|
|
sections_.reserve(numSections_);
|
|
for (const auto i : c10::irange(numSections_)) {
|
|
sections_.emplace_back(toSection(&shdrList_[i]));
|
|
}
|
|
}
|
|
|
|
multipy::optional<Section> ElfFile::findSection(const char* name) const {
|
|
MULTIPY_CHECK(name != nullptr, "Null name");
|
|
multipy::optional<Section> found = multipy::nullopt;
|
|
for (const auto& section : sections_) {
|
|
if (strcmp(name, section.name) == 0) {
|
|
found = section;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return found;
|
|
}
|
|
|
|
void ElfFile::checkFormat() const {
|
|
// check the magic numbers
|
|
MULTIPY_CHECK(
|
|
(ehdr_->e_ident[EI_MAG0] == ELFMAG0) &&
|
|
(ehdr_->e_ident[EI_MAG1] == ELFMAG1) &&
|
|
(ehdr_->e_ident[EI_MAG2] == ELFMAG2) &&
|
|
(ehdr_->e_ident[EI_MAG3] == ELFMAG3),
|
|
"Unexpected magic numbers");
|
|
MULTIPY_CHECK(
|
|
ehdr_->e_ident[EI_CLASS] == ELFCLASS64, "Only support 64bit ELF file");
|
|
}
|
|
|
|
} // namespace deploy
|
|
} // namespace torch
|