[lld] [llvm] [Object][ELF] Support extended header for Object Parser in ELF (PR #162288)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 01:42:43 PDT 2025


================
@@ -889,15 +896,42 @@ Expected<uint64_t> ELFFile<ELFT>::getDynSymtabSize() const {
   return 0;
 }
 
-template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {}
+template <class ELFT> ELFFile<ELFT>::ELFFile(StringRef Object) : Buf(Object) {
+  auto Header = getHeader();
+  e_phnum = Header.e_phnum;
+  e_shnum = Header.e_shnum;
+  e_shstrndx = Header.e_shstrndx;
+}
 
 template <class ELFT>
 Expected<ELFFile<ELFT>> ELFFile<ELFT>::create(StringRef Object) {
   if (sizeof(Elf_Ehdr) > Object.size())
     return createError("invalid buffer: the size (" + Twine(Object.size()) +
                        ") is smaller than an ELF header (" +
                        Twine(sizeof(Elf_Ehdr)) + ")");
-  return ELFFile(Object);
+  ELFFile Result(Object);
+
+  //
+  // sections() parse the total number of sections by considering the
+  // extended headers
+  //
+  if (Result.getHeader().HasHeaderExtension()) {
----------------
aokblast wrote:

The ELFFile::sections() and object::getSection() return an Expected. That means if you don't consume up the error, it will trigger panics by RAII. However, there is no way for us to pass error back from the constructor. The only way is through throw/catch. However, I don't  found any practice in llvm-prloject to use throw/catch so I think it is a bad idea for me to do so

https://github.com/llvm/llvm-project/pull/162288


More information about the llvm-commits mailing list