#include "startup/linux/gnu_property_section.h"
#include "hdr/elf_proxy.h"
#include "hdr/link_macros.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/utils.h"
namespace LIBC_NAMESPACE_DECL {
struct Elf64_ProgramPropertyNote {
Elf64_Nhdr nhdr;
unsigned char n_name[4];
unsigned char n_desc[0]; };
struct Elf32_ProgramPropertyNote {
Elf32_Nhdr nhdr;
unsigned char n_name[4];
unsigned char n_desc[0]; };
struct Elf64_ProgramProperty {
Elf64_Word pr_type;
Elf64_Word pr_datasz;
unsigned char pr_data[0];
};
struct Elf32_ProgramProperty {
Elf32_Word pr_type;
Elf32_Word pr_datasz;
unsigned char pr_data[0];
};
bool GnuPropertySection::parse(const ElfW(Phdr) * gnu_property_phdr,
const ElfW(Addr) base) {
if (!gnu_property_phdr)
return false;
const auto note_nhdr_size = gnu_property_phdr->p_memsz;
if (gnu_property_phdr->p_type != PT_GNU_PROPERTY ||
note_nhdr_size < sizeof(ElfW(ProgramPropertyNote)))
return false;
const ElfW(ProgramPropertyNote) *note_nhdr =
reinterpret_cast<ElfW(ProgramPropertyNote) *>(base +
gnu_property_phdr->p_vaddr);
if (!note_nhdr)
return false;
const ElfW(Word) nhdr_desc_size = note_nhdr->nhdr.n_descsz;
if ((sizeof(*note_nhdr) + nhdr_desc_size) > note_nhdr_size)
return false;
if (note_nhdr->nhdr.n_namesz != 4 ||
note_nhdr->nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
cpp::string_view(reinterpret_cast<const char *>(note_nhdr->n_name), 4) !=
cpp::string_view("GNU", 4))
return false;
ElfW(Word) offset = 0;
while (offset + sizeof(ElfW(ProgramProperty)) <= nhdr_desc_size) {
const ElfW(ProgramProperty) *property =
reinterpret_cast<const ElfW(ProgramProperty) *>(
¬e_nhdr->n_desc[offset]);
if (distance_to_align_up<sizeof(ElfW(Word))>(property) > 0)
return false;
const ElfW(Xword) property_size = sizeof(*property) + property->pr_datasz;
if ((offset + property_size) > nhdr_desc_size)
return false;
switch (property->pr_type) {
#ifdef LIBC_TARGET_ARCH_IS_X86_64
case GNU_PROPERTY_X86_FEATURE_1_AND: {
if (property->pr_datasz != 4)
return false;
const uint32_t feature_bitmap =
*reinterpret_cast<const uint32_t *>(&property->pr_data[0]);
features_.shstk_supported =
(feature_bitmap & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
break;
}
#endif
default:
break;
}
offset += static_cast<ElfW(Word)>(property_size);
}
return true;
}
}