Skip to content

Commit e895c52

Browse files
committed
[Object] getBuildAttributes: check e_machine. NFC
getBuildAttributes is only called for ARM/RISCV object files and `SHT_ARM_ATTRIBUTES == SHT_RISCV_ATTRIBUTES`, so the following check `Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES || Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES` is actually fine. But the convention is to guard such processor-specific section type checks with an e_machine test.
1 parent c54f22f commit e895c52

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -389,25 +389,35 @@ template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
389389
}
390390

391391
Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
392+
uint32_t Type;
393+
switch (getEMachine()) {
394+
case ELF::EM_ARM:
395+
Type = ELF::SHT_ARM_ATTRIBUTES;
396+
break;
397+
case ELF::EM_RISCV:
398+
Type = ELF::SHT_RISCV_ATTRIBUTES;
399+
break;
400+
default:
401+
return Error::success();
402+
}
403+
392404
auto SectionsOrErr = EF.sections();
393405
if (!SectionsOrErr)
394406
return SectionsOrErr.takeError();
395-
396407
for (const Elf_Shdr &Sec : *SectionsOrErr) {
397-
if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES ||
398-
Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) {
399-
auto ErrorOrContents = EF.getSectionContents(Sec);
400-
if (!ErrorOrContents)
401-
return ErrorOrContents.takeError();
402-
403-
auto Contents = ErrorOrContents.get();
404-
if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
405-
return Error::success();
406-
407-
if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
408-
return E;
409-
break;
410-
}
408+
if (Sec.sh_type != Type)
409+
continue;
410+
auto ErrorOrContents = EF.getSectionContents(Sec);
411+
if (!ErrorOrContents)
412+
return ErrorOrContents.takeError();
413+
414+
auto Contents = ErrorOrContents.get();
415+
if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
416+
return Error::success();
417+
418+
if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
419+
return E;
420+
break;
411421
}
412422
return Error::success();
413423
}

0 commit comments

Comments
 (0)