Initialize Elf header to Zero to ensure that bytes not assigned any value later on are initialized properly.
Details
- Reviewers
ruiu pcc - Commits
- rG7588a8e89d6b: Initialize Elf Header to zero to ensure that bytes not assigned any value later…
rLLD328902: Initialize Elf Header to zero to ensure that bytes not assigned any value later…
rL328902: Initialize Elf Header to zero to ensure that bytes not assigned any value later…
Diff Detail
Event Timeline
lld/ELF/Writer.cpp | ||
---|---|---|
2111 | Please make spacing uniform. memset(Buf, 0, sizeof(Elf_Ehdr) + sizeof(Elf_Phdr)); Also could you please add a unit test? |
lld/ELF/Writer.cpp | ||
---|---|---|
2111 | If -r option is given, we don't emit Phdr, so this is inaccurate. It's in practice not a problem because I can't think of an ELF file that is smaller than sizeof(Elf_Ehdr) + sizeof(Elf_Phdr), but being accurate helps readers understand the code. So it is better to just set the first sizeof(Elf_Ehdr). |
I thought this was a memory-mapped file. Aren't its contents initialized to zero by the operating system?
I see, it is because we write trap instructions to executable segments before writing the header. Please add a comment explaining this.
template <class ELFT> void Writer<ELFT>::writeTrapInstr() { if (Script->HasSectionsCommand) return; // Fill the last page. uint8_t *Buf = Buffer->getBufferStart(); for (PhdrEntry *P : Phdrs) if (P->p_type == PT_LOAD && (P->p_flags & PF_X)) /////////////////// fillTrap(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize), Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));
How about if (P->p_type == PT_LOAD && P->p_flags & PF_X && P->LastSec != Out::ProgramHeaders)
Other write* functions don't initialize the buffer to zero
For some cases, eg with -r option, Phdr is not emitted. Hence it is better to initialize ELF header to zero instead.
Please make spacing uniform.
Also could you please add a unit test?