+ phdrs = xmalloc (elf_header.e_phnum * sizeof (*phdrs));
+
+ if (elf_header.e_ident[EI_CLASS] == ELFCLASS32)
+ {
+ Elf32_External_Phdr *phdrs32
+ = (Elf32_External_Phdr *) (map + elf_header.e_phoff);
+ for (i = 0; i < elf_header.e_phnum; i++)
+ {
+ phdrs[i].p_type = BYTE_GET (phdrs32[i].p_type);
+ phdrs[i].p_offset = BYTE_GET (phdrs32[i].p_offset);
+ phdrs[i].p_vaddr = BYTE_GET (phdrs32[i].p_vaddr);
+ phdrs[i].p_paddr = BYTE_GET (phdrs32[i].p_paddr);
+ phdrs[i].p_filesz = BYTE_GET (phdrs32[i].p_filesz);
+ phdrs[i].p_memsz = BYTE_GET (phdrs32[i].p_memsz);
+ phdrs[i].p_flags = BYTE_GET (phdrs32[i].p_flags);
+ phdrs[i].p_align = BYTE_GET (phdrs32[i].p_align);
+ }
+ }
+ else
+ {
+ Elf64_External_Phdr *phdrs64
+ = (Elf64_External_Phdr *) (map + elf_header.e_phoff);
+ for (i = 0; i < elf_header.e_phnum; i++)
+ {
+ phdrs[i].p_type = BYTE_GET (phdrs64[i].p_type);
+ phdrs[i].p_offset = BYTE_GET (phdrs64[i].p_offset);
+ phdrs[i].p_vaddr = BYTE_GET (phdrs64[i].p_vaddr);
+ phdrs[i].p_paddr = BYTE_GET (phdrs64[i].p_paddr);
+ phdrs[i].p_filesz = BYTE_GET (phdrs64[i].p_filesz);
+ phdrs[i].p_memsz = BYTE_GET (phdrs64[i].p_memsz);
+ phdrs[i].p_flags = BYTE_GET (phdrs64[i].p_flags);
+ phdrs[i].p_align = BYTE_GET (phdrs64[i].p_align);
+ }
+ }
+
+ ret = 0;
+ for (i = 0; i < elf_header.e_phnum; i++)
+ if (phdrs[i].p_type == PT_NOTE)
+ {
+ size_t offset = phdrs[i].p_offset;
+ size_t size = phdrs[i].p_filesz;
+ size_t align = phdrs[i].p_align;
+ char *buf = map + offset;
+ char *p = buf;
+
+ while (p < buf + size)
+ {
+ Elf_External_Note *xnp = (Elf_External_Note *) p;
+ Elf_Internal_Note in;
+
+ if (offsetof (Elf_External_Note, name) > buf - p + size)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ in.type = BYTE_GET (xnp->type);
+ in.namesz = BYTE_GET (xnp->namesz);
+ in.namedata = xnp->name;
+ if (in.namesz > buf - in.namedata + size)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ in.descsz = BYTE_GET (xnp->descsz);
+ in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
+ in.descpos = offset + (in.descdata - buf);
+ if (in.descsz != 0
+ && (in.descdata >= buf + size
+ || in.descsz > buf - in.descdata + size))
+ {
+ ret = 1;
+ goto out;
+ }
+
+ if (in.namesz == sizeof "GNU"
+ && strcmp (in.namedata, "GNU") == 0
+ && in.type == NT_GNU_PROPERTY_TYPE_0)
+ {
+ unsigned char *ptr;
+ unsigned char *ptr_end;
+
+ if (in.descsz < 8 || (in.descsz % align) != 0)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ ptr = (unsigned char *) in.descdata;
+ ptr_end = ptr + in.descsz;
+
+ do
+ {
+ unsigned int type = byte_get (ptr, 4);
+ unsigned int datasz = byte_get (ptr + 4, 4);
+ unsigned int bitmask, old_bitmask;
+
+ ptr += 8;
+ if ((ptr + datasz) > ptr_end)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
+ {
+ if (datasz != 4)
+ {
+ ret = 1;
+ goto out;
+ }
+
+ old_bitmask = byte_get (ptr, 4);
+ bitmask = old_bitmask;
+ if (enable_x86_features)
+ bitmask |= enable_x86_features;
+ if (disable_x86_features)
+ bitmask &= ~disable_x86_features;
+ if (old_bitmask != bitmask)
+ byte_put (ptr, bitmask, 4);
+ goto out;
+ }
+
+ ptr += ELF_ALIGN_UP (datasz, align);
+ }
+ while ((ptr_end - ptr) >= 8);
+ }
+
+ p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
+ }
+ }
+
+ out:
+ if (ret != 0)
+ error (_("%s: Invalid PT_NOTE segment\n"), file_name);
+
+ free (phdrs);
+ munmap (map, st_buf.st_size);
+
+ return ret;
+}
+
+/* Set enable_x86_features and disable_x86_features for a feature
+ string, FEATURE. */
+
+static int
+elf_x86_feature (const char *feature, int enable)
+{
+ unsigned int x86_feature;
+ if (strcasecmp (feature, "ibt") == 0)
+ x86_feature = GNU_PROPERTY_X86_FEATURE_1_IBT;
+ else if (strcasecmp (feature, "shstk") == 0)
+ x86_feature = GNU_PROPERTY_X86_FEATURE_1_SHSTK;
+ else
+ {
+ error (_("Unknown x86 feature: %s\n"), feature);
+ return -1;
+ }
+
+ if (enable)
+ {
+ enable_x86_features |= x86_feature;
+ disable_x86_features &= ~x86_feature;
+ }
+ else
+ {
+ disable_x86_features |= x86_feature;
+ enable_x86_features &= ~x86_feature;
+ }
+
+ return 0;
+}
+#endif
+
+/* Return ELF class for a machine type, MACH. */
+
+static enum elfclass
+elf_class (int mach)
+{
+ switch (mach)
+ {
+ case EM_386:
+ case EM_IAMCU:
+ return ELF_CLASS_32;
+ case EM_L1OM:
+ case EM_K1OM:
+ return ELF_CLASS_64;
+ case EM_X86_64:
+ case EM_NONE:
+ return ELF_CLASS_BOTH;
+ default:
+ return ELF_CLASS_BOTH;
+ }
+}
+
+static int
+update_elf_header (const char *file_name, FILE *file)
+{
+ int class, machine, type, status, osabi;
+