[lld] r309829 - [ELF] When the code segment is the last, align it to the page boundary

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 2 15:36:06 PDT 2017


Petr Hosek via llvm-commits <llvm-commits at lists.llvm.org> writes:


> +// Fill the first and the last page of executable segments with trap
> +// instructions instead of leaving them as zero. Even though it is not required
> +// by any standard , it is in general a good thing to do for security reasons.
> +template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
> +  if (Script->Opt.HasSections)
> +    return;
> +
> +  uint8_t *Buf = Buffer->getBufferStart();
> +
> +  for (PhdrEntry *P : Phdrs) {
> +    if (P->p_type != PT_LOAD || !(P->p_flags & PF_X))
> +      continue;
> +
> +    // We only fill the first and the last page of the segment because the
> +    // middle part will be overwritten by output sections.
> +    fillTrapInstr(Buf + alignDown(P->p_offset, Target->PageSize),
> +                  Buf + alignTo(P->p_offset, Target->PageSize));
> +    fillTrapInstr(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize),
> +                  Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));

Why do you need to fill the first page? All tests pass if I comment
that.

Cheers,
Rafael


More information about the llvm-commits mailing list